behavioursc_inputbar.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*******************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2012 Conner Hansen (chansen@crimson.ua.edu)
  4. This file is part of AToMPM.
  5. AToMPM is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU Lesser General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or (at your option) any later
  8. version.
  9. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  14. *******************************************************************************/
  15. /**
  16. * This object defines my DummyStateChart formalism. It has
  17. * State and Transition methods.
  18. */
  19. var DummyStateChart = function(){
  20. var states = new Array();
  21. var transitions = new Array();
  22. var currentState = null;
  23. this.State = function(name){
  24. var out = new Array();
  25. this.addTransition = function( transition ){
  26. out.push( transition );
  27. };
  28. this.fire = function( trigger ){
  29. for(var i=0; i<out.length; i++){
  30. if( out[i].isTrigger( trigger ) ){
  31. return out[i];
  32. }
  33. }
  34. return false;
  35. };
  36. this.onEntry = function(){
  37. //console.debug("OnEntry: " + name);
  38. };
  39. this.onExit = function(){
  40. //console.debug("OnExit: " + name);
  41. };
  42. return this;
  43. };
  44. this.Transition = function(trigger, from, to){
  45. this.fire = function(){
  46. return to;
  47. };
  48. this.getTrigger = function(){
  49. return trigger;
  50. };
  51. this.isTrigger = function( input ){
  52. return input == trigger;
  53. };
  54. from.addTransition( this );
  55. return this;
  56. };
  57. this.addState = function( state ){
  58. states.push( state );
  59. };
  60. this.addTransition = function( transition ){
  61. transitions.push( transition );
  62. };
  63. this.fireEvent = function( event ){
  64. var transition = currentState.fire( event );
  65. if( transition ){
  66. // Fire the on exit event
  67. currentState.onExit();
  68. // Move to the new state and
  69. // fire the entry event
  70. currentState = transition.fire( event );
  71. currentState.onEntry();
  72. }
  73. };
  74. this.getCurrentState = function(){
  75. return currentState;
  76. };
  77. this.setInitialState = function( index ){
  78. currentState = states[index];
  79. currentState.onEntry();
  80. };
  81. return this;
  82. };
  83. var DummyBNF = function(){
  84. };
  85. InputBarStateChart = function(){
  86. var sc = new DummyStateChart();
  87. var storage = null;
  88. var triggers = new Array(
  89. "inputEntered",
  90. "validInput",
  91. "invalidInput",
  92. "errorDisplayed",
  93. "noCommandFound",
  94. "commandFound",
  95. "executedCommand");
  96. ////////////////////////////////////////
  97. // STATES
  98. ////////////////////////////////////////
  99. var STATE_WAIT = new sc.State( "Wait" );
  100. var STATE_PROCESS_INPUT = new sc.State( "ProcessInput" );
  101. var STATE_SHOW_ERROR = new sc.State( "ShowError" );
  102. var STATE_MATCH_COMMAND = new sc.State( "MatchCommand" );
  103. var STATE_EXECUTE_COMMAND = new sc.State( "ExecuteCommand" );
  104. STATE_PROCESS_INPUT.onEntry = function(){
  105. // split on any amount of whitespace
  106. storage = $('#mainInput').value.split(/[ ]+/);
  107. $('#mainInput').className.replace("error", "");
  108. // always return valid, since we don't yet have
  109. // a BNF to define what is good/bad input
  110. return sc.fireEvent( triggers[1] );
  111. };
  112. STATE_SHOW_ERROR.onEntry = function(){
  113. $('#mainInput').className += " error";
  114. return sc.fireEvent( triggers[3] );
  115. };
  116. STATE_MATCH_COMMAND.onEntry = function(){
  117. // No BNF, no command matching
  118. return sc.fireEvent( triggers[5] );
  119. };
  120. STATE_EXECUTE_COMMAND.onEntry = function(){
  121. // Stop gap measure until the BNF is implemented
  122. eval( $('#mainInput').value );
  123. $('#mainInput').value = "";
  124. return sc.fireEvent( triggers[6] );
  125. };
  126. ////////////////////////////////////////
  127. // TRANSITIONS
  128. ////////////////////////////////////////
  129. var TRANS_INPUT_ENTERED = new sc.Transition( triggers[0], STATE_WAIT, STATE_PROCESS_INPUT);
  130. var TRANS_VALID_INPUT = new sc.Transition( triggers[1], STATE_PROCESS_INPUT, STATE_MATCH_COMMAND);
  131. var TRANS_INVALID_INPUT = new sc.Transition( triggers[2], STATE_PROCESS_INPUT, STATE_SHOW_ERROR);
  132. var TRANS_ERROR_DISPLAYED = new sc.Transition( triggers[3], STATE_SHOW_ERROR, STATE_WAIT);
  133. var TRANS_NO_COMMAND_FOUND = new sc.Transition( triggers[4], STATE_PROCESS_INPUT, STATE_SHOW_ERROR);
  134. var TRANS_COMMAND_FOUND = new sc.Transition( triggers[5], STATE_MATCH_COMMAND, STATE_EXECUTE_COMMAND);
  135. var TRANS_EXECUTE_COMMAND = new sc.Transition( triggers[6], STATE_EXECUTE_COMMAND, STATE_WAIT);
  136. ////////////////////////////////////////
  137. // ADD ELEMENTS
  138. ////////////////////////////////////////
  139. sc.addState(STATE_WAIT);
  140. sc.addState(STATE_PROCESS_INPUT);
  141. sc.addState(STATE_SHOW_ERROR);
  142. sc.addState(STATE_MATCH_COMMAND);
  143. sc.addState(STATE_EXECUTE_COMMAND);
  144. sc.addTransition(TRANS_INPUT_ENTERED);
  145. sc.addTransition(TRANS_VALID_INPUT);
  146. sc.addTransition(TRANS_INVALID_INPUT);
  147. sc.addTransition(TRANS_ERROR_DISPLAYED);
  148. sc.addTransition(TRANS_NO_COMMAND_FOUND);
  149. sc.addTransition(TRANS_COMMAND_FOUND);
  150. sc.addTransition(TRANS_EXECUTE_COMMAND);
  151. sc.setInitialState(0);
  152. this.fireEvent = function( trigger ){
  153. sc.fireEvent( trigger );
  154. };
  155. return this;
  156. }();