target.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  2. Date: Thu Aug 04 13:29:22 2016
  3. Model author: Joeri Exelmans+Raphael Mannadiar+Simon Van Mierlo
  4. Model name: Bouncing_Balls_JavaScript_Version
  5. Model description:
  6. SVG canvas with bouncing balls in it.*/
  7. // package "Bouncing_Balls_JavaScript_Version"
  8. var Bouncing_Balls_JavaScript_Version = {};
  9. (function() {
  10. var MainApp = function(controller) {
  11. RuntimeClassBase.call(this, controller);
  12. this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
  13. this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
  14. this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
  15. this.semantics.priority = StatechartSemantics.SourceParent;
  16. this.semantics.concurrency = StatechartSemantics.Single;
  17. // build Statechart structure
  18. this.build_statechart_structure();
  19. // call user defined constructor
  20. MainApp.prototype.user_defined_constructor.call(this);
  21. };
  22. MainApp.prototype = new Object();
  23. (function() {
  24. var proto = new RuntimeClassBase();
  25. for (prop in proto) {
  26. MainApp.prototype[prop] = proto[prop];
  27. }
  28. })();
  29. MainApp.prototype.user_defined_constructor = function() {
  30. this.nr_of_fields = 0;
  31. };
  32. MainApp.prototype.user_defined_destructor = function() {
  33. };
  34. // builds Statechart structure
  35. MainApp.prototype.build_statechart_structure = function() {
  36. // state <root>
  37. this.states[""] = new State(0, this);
  38. // state /running
  39. this.states["/running"] = new State(1, this);
  40. // state /running/root
  41. this.states["/running/root"] = new ParallelState(2, this);
  42. // state /running/root/main_behaviour
  43. this.states["/running/root/main_behaviour"] = new State(3, this);
  44. // state /running/root/main_behaviour/initializing
  45. this.states["/running/root/main_behaviour/initializing"] = new State(4, this);
  46. // state /running/root/main_behaviour/running
  47. this.states["/running/root/main_behaviour/running"] = new State(5, this);
  48. // state /running/root/cd_behaviour
  49. this.states["/running/root/cd_behaviour"] = new State(6, this);
  50. // state /running/root/cd_behaviour/waiting
  51. this.states["/running/root/cd_behaviour/waiting"] = new State(7, this);
  52. // state /running/root/cd_behaviour/creating
  53. this.states["/running/root/cd_behaviour/creating"] = new State(8, this);
  54. // state /running/root/cd_behaviour/check_nr_of_fields
  55. this.states["/running/root/cd_behaviour/check_nr_of_fields"] = new State(9, this);
  56. // state /running/stopped
  57. this.states["/running/stopped"] = new State(10, this);
  58. // add children
  59. this.states[""].addChild(this.states["/running"]);
  60. this.states["/running"].addChild(this.states["/running/root"]);
  61. this.states["/running"].addChild(this.states["/running/stopped"]);
  62. this.states["/running/root"].addChild(this.states["/running/root/main_behaviour"]);
  63. this.states["/running/root"].addChild(this.states["/running/root/cd_behaviour"]);
  64. this.states["/running/root/main_behaviour"].addChild(this.states["/running/root/main_behaviour/initializing"]);
  65. this.states["/running/root/main_behaviour"].addChild(this.states["/running/root/main_behaviour/running"]);
  66. this.states["/running/root/cd_behaviour"].addChild(this.states["/running/root/cd_behaviour/waiting"]);
  67. this.states["/running/root/cd_behaviour"].addChild(this.states["/running/root/cd_behaviour/creating"]);
  68. this.states["/running/root/cd_behaviour"].addChild(this.states["/running/root/cd_behaviour/check_nr_of_fields"]);
  69. this.states[""].fixTree();
  70. this.states[""].default_state = this.states["/running"];
  71. this.states["/running"].default_state = this.states["/running/root"];
  72. this.states["/running/root/main_behaviour"].default_state = this.states["/running/root/main_behaviour/initializing"];
  73. this.states["/running/root/cd_behaviour"].default_state = this.states["/running/root/cd_behaviour/waiting"];
  74. // transition /running/root/main_behaviour/initializing
  75. var _running_root_main_behaviour_initializing_0 = new Transition(this, this.states["/running/root/main_behaviour/initializing"], [this.states["/running/root/main_behaviour/running"]]);
  76. _running_root_main_behaviour_initializing_0.setAction(this._running_root_main_behaviour_initializing_0_exec);
  77. this.states["/running/root/main_behaviour/initializing"].addTransition(_running_root_main_behaviour_initializing_0);
  78. // transition /running/root/main_behaviour/running
  79. var _running_root_main_behaviour_running_0 = new Transition(this, this.states["/running/root/main_behaviour/running"], [this.states["/running/root/main_behaviour/running"]]);
  80. _running_root_main_behaviour_running_0.setAction(this._running_root_main_behaviour_running_0_exec);
  81. _running_root_main_behaviour_running_0.trigger = new Event("button_pressed", null);
  82. _running_root_main_behaviour_running_0.setGuard(this._running_root_main_behaviour_running_0_guard);
  83. this.states["/running/root/main_behaviour/running"].addTransition(_running_root_main_behaviour_running_0);
  84. // transition /running/root/cd_behaviour/waiting
  85. var _running_root_cd_behaviour_waiting_0 = new Transition(this, this.states["/running/root/cd_behaviour/waiting"], [this.states["/running/root/cd_behaviour/creating"]]);
  86. _running_root_cd_behaviour_waiting_0.setAction(this._running_root_cd_behaviour_waiting_0_exec);
  87. _running_root_cd_behaviour_waiting_0.trigger = new Event("create_field", null);
  88. this.states["/running/root/cd_behaviour/waiting"].addTransition(_running_root_cd_behaviour_waiting_0);
  89. var _running_root_cd_behaviour_waiting_1 = new Transition(this, this.states["/running/root/cd_behaviour/waiting"], [this.states["/running/root/cd_behaviour/check_nr_of_fields"]]);
  90. _running_root_cd_behaviour_waiting_1.setAction(this._running_root_cd_behaviour_waiting_1_exec);
  91. _running_root_cd_behaviour_waiting_1.trigger = new Event("delete_field", null);
  92. this.states["/running/root/cd_behaviour/waiting"].addTransition(_running_root_cd_behaviour_waiting_1);
  93. // transition /running/root/cd_behaviour/creating
  94. var _running_root_cd_behaviour_creating_0 = new Transition(this, this.states["/running/root/cd_behaviour/creating"], [this.states["/running/root/cd_behaviour/waiting"]]);
  95. _running_root_cd_behaviour_creating_0.setAction(this._running_root_cd_behaviour_creating_0_exec);
  96. _running_root_cd_behaviour_creating_0.trigger = new Event("instance_created", null);
  97. this.states["/running/root/cd_behaviour/creating"].addTransition(_running_root_cd_behaviour_creating_0);
  98. // transition /running/root/cd_behaviour/check_nr_of_fields
  99. var _running_root_cd_behaviour_check_nr_of_fields_0 = new Transition(this, this.states["/running/root/cd_behaviour/check_nr_of_fields"], [this.states["/running/stopped"]]);
  100. _running_root_cd_behaviour_check_nr_of_fields_0.setAction(this._running_root_cd_behaviour_check_nr_of_fields_0_exec);
  101. _running_root_cd_behaviour_check_nr_of_fields_0.setGuard(this._running_root_cd_behaviour_check_nr_of_fields_0_guard);
  102. this.states["/running/root/cd_behaviour/check_nr_of_fields"].addTransition(_running_root_cd_behaviour_check_nr_of_fields_0);
  103. var _running_root_cd_behaviour_check_nr_of_fields_1 = new Transition(this, this.states["/running/root/cd_behaviour/check_nr_of_fields"], [this.states["/running/root/cd_behaviour/waiting"]]);
  104. _running_root_cd_behaviour_check_nr_of_fields_1.setGuard(this._running_root_cd_behaviour_check_nr_of_fields_1_guard);
  105. this.states["/running/root/cd_behaviour/check_nr_of_fields"].addTransition(_running_root_cd_behaviour_check_nr_of_fields_1);
  106. };
  107. MainApp.prototype._running_root_main_behaviour_initializing_0_exec = function(parameters) {
  108. this.raiseInternalEvent(new Event("create_field", null, new Array()));
  109. };
  110. MainApp.prototype._running_root_main_behaviour_running_0_exec = function(parameters) {
  111. var event_name = parameters[0];
  112. this.raiseInternalEvent(new Event("create_field", null, new Array()));
  113. };
  114. MainApp.prototype._running_root_main_behaviour_running_0_guard = function(parameters) {
  115. var event_name = parameters[0];
  116. return event_name == "create_new_field";
  117. };
  118. MainApp.prototype._running_root_cd_behaviour_waiting_0_exec = function(parameters) {
  119. this.big_step.outputEventOM(new Event("create_instance", null, [this, "fields"]));
  120. };
  121. MainApp.prototype._running_root_cd_behaviour_waiting_1_exec = function(parameters) {
  122. var association_name = parameters[0];
  123. this.big_step.outputEventOM(new Event("delete_instance", null, [this, association_name]));
  124. this.nr_of_fields -= 1
  125. };
  126. MainApp.prototype._running_root_cd_behaviour_creating_0_exec = function(parameters) {
  127. var association_name = parameters[0];
  128. this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
  129. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, association_name, new Event("set_association_name", null, [association_name])]));
  130. this.nr_of_fields += 1
  131. };
  132. MainApp.prototype._running_root_cd_behaviour_check_nr_of_fields_0_exec = function(parameters) {
  133. //NOTE: ideally, this should be ui.close_window(ui.window). we can't do this though
  134. // due to a security measure preventing scripts from closing pages they didn't
  135. // open themselves
  136. ui.println('refresh to play again');
  137. };
  138. MainApp.prototype._running_root_cd_behaviour_check_nr_of_fields_0_guard = function(parameters) {
  139. return this.nr_of_fields == 0;
  140. };
  141. MainApp.prototype._running_root_cd_behaviour_check_nr_of_fields_1_guard = function(parameters) {
  142. return this.nr_of_fields != 0;
  143. };
  144. MainApp.prototype.initializeStatechart = function() {
  145. // enter default state
  146. var states = this.states["/running"].getEffectiveTargetStates();
  147. this.updateConfiguration(states);
  148. for (var state_idx in states) {
  149. if (!states.hasOwnProperty(state_idx)) continue;
  150. var state = states[state_idx]
  151. if (state.enter) {
  152. state.enter();
  153. }
  154. }
  155. };
  156. // add symbol 'MainApp' to package 'Bouncing_Balls_JavaScript_Version'
  157. Bouncing_Balls_JavaScript_Version.MainApp = MainApp;
  158. var Field = function(controller) {
  159. RuntimeClassBase.call(this, controller);
  160. this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
  161. this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
  162. this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
  163. this.semantics.priority = StatechartSemantics.SourceParent;
  164. this.semantics.concurrency = StatechartSemantics.Single;
  165. // build Statechart structure
  166. this.build_statechart_structure();
  167. this.inports["field_ui"] = controller.addInputPort("field_ui", this);
  168. // user defined attributes
  169. this.canvas = null;
  170. this.field_window = null;
  171. // call user defined constructor
  172. Field.prototype.user_defined_constructor.call(this);
  173. };
  174. Field.prototype = new Object();
  175. (function() {
  176. var proto = new RuntimeClassBase();
  177. for (prop in proto) {
  178. Field.prototype[prop] = proto[prop];
  179. }
  180. })();
  181. Field.prototype.user_defined_constructor = function() {
  182. this.field_window = ui.new_window(400,450);
  183. this.canvas = ui.append_canvas(this.field_window,400,400,{'background':'#eee'});
  184. ui.bind_event(this.field_window, ui.EVENTS.WINDOW_CLOSE, this.controller, 'window_close', this.inports['field_ui']);
  185. ui.bind_event(this.field_window, ui.EVENTS.KEY_PRESS, this.controller, 'key_press', this.inports['field_ui']);
  186. ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_RIGHT_CLICK, this.controller, 'right_click', this.inports['field_ui']);
  187. ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_MOVE, this.controller, 'mouse_move');
  188. ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_RELEASE, this.controller, 'mouse_release');
  189. };
  190. Field.prototype.user_defined_destructor = function() {
  191. ui.close_window(this.field_window);
  192. };
  193. // builds Statechart structure
  194. Field.prototype.build_statechart_structure = function() {
  195. // state <root>
  196. this.states[""] = new State(0, this);
  197. // state /root
  198. this.states["/root"] = new State(1, this);
  199. // state /root/waiting
  200. this.states["/root/waiting"] = new State(2, this);
  201. // state /root/initializing
  202. this.states["/root/initializing"] = new State(3, this);
  203. // state /root/creating
  204. this.states["/root/creating"] = new State(4, this);
  205. // state /root/packing
  206. this.states["/root/packing"] = new State(5, this);
  207. // state /root/running
  208. this.states["/root/running"] = new ParallelState(6, this);
  209. // state /root/running/main_behaviour
  210. this.states["/root/running/main_behaviour"] = new State(7, this);
  211. // state /root/running/main_behaviour/running
  212. this.states["/root/running/main_behaviour/running"] = new State(8, this);
  213. // state /root/running/main_behaviour/creating
  214. this.states["/root/running/main_behaviour/creating"] = new State(9, this);
  215. // state /root/running/deleting_behaviour
  216. this.states["/root/running/deleting_behaviour"] = new State(10, this);
  217. // state /root/running/deleting_behaviour/running
  218. this.states["/root/running/deleting_behaviour/running"] = new State(11, this);
  219. // state /root/running/child_behaviour
  220. this.states["/root/running/child_behaviour"] = new State(12, this);
  221. // state /root/running/child_behaviour/listening
  222. this.states["/root/running/child_behaviour/listening"] = new State(13, this);
  223. // state /root/running/deleting_balls_behaviour
  224. this.states["/root/running/deleting_balls_behaviour"] = new State(14, this);
  225. // state /root/running/deleting_balls_behaviour/listening
  226. this.states["/root/running/deleting_balls_behaviour/listening"] = new State(15, this);
  227. // state /root/deleting
  228. this.states["/root/deleting"] = new State(16, this);
  229. // state /root/deleted
  230. this.states["/root/deleted"] = new State(17, this);
  231. // add children
  232. this.states[""].addChild(this.states["/root"]);
  233. this.states["/root"].addChild(this.states["/root/waiting"]);
  234. this.states["/root"].addChild(this.states["/root/initializing"]);
  235. this.states["/root"].addChild(this.states["/root/creating"]);
  236. this.states["/root"].addChild(this.states["/root/packing"]);
  237. this.states["/root"].addChild(this.states["/root/running"]);
  238. this.states["/root"].addChild(this.states["/root/deleting"]);
  239. this.states["/root"].addChild(this.states["/root/deleted"]);
  240. this.states["/root/running"].addChild(this.states["/root/running/main_behaviour"]);
  241. this.states["/root/running"].addChild(this.states["/root/running/deleting_behaviour"]);
  242. this.states["/root/running"].addChild(this.states["/root/running/child_behaviour"]);
  243. this.states["/root/running"].addChild(this.states["/root/running/deleting_balls_behaviour"]);
  244. this.states["/root/running/main_behaviour"].addChild(this.states["/root/running/main_behaviour/running"]);
  245. this.states["/root/running/main_behaviour"].addChild(this.states["/root/running/main_behaviour/creating"]);
  246. this.states["/root/running/deleting_behaviour"].addChild(this.states["/root/running/deleting_behaviour/running"]);
  247. this.states["/root/running/child_behaviour"].addChild(this.states["/root/running/child_behaviour/listening"]);
  248. this.states["/root/running/deleting_balls_behaviour"].addChild(this.states["/root/running/deleting_balls_behaviour/listening"]);
  249. this.states[""].fixTree();
  250. this.states[""].default_state = this.states["/root"];
  251. this.states["/root"].default_state = this.states["/root/waiting"];
  252. this.states["/root/running/main_behaviour"].default_state = this.states["/root/running/main_behaviour/running"];
  253. this.states["/root/running/deleting_behaviour"].default_state = this.states["/root/running/deleting_behaviour/running"];
  254. this.states["/root/running/child_behaviour"].default_state = this.states["/root/running/child_behaviour/listening"];
  255. this.states["/root/running/deleting_balls_behaviour"].default_state = this.states["/root/running/deleting_balls_behaviour/listening"];
  256. // transition /root/waiting
  257. var _root_waiting_0 = new Transition(this, this.states["/root/waiting"], [this.states["/root/initializing"]]);
  258. _root_waiting_0.setAction(this._root_waiting_0_exec);
  259. _root_waiting_0.trigger = new Event("set_association_name", null);
  260. this.states["/root/waiting"].addTransition(_root_waiting_0);
  261. // transition /root/initializing
  262. var _root_initializing_0 = new Transition(this, this.states["/root/initializing"], [this.states["/root/creating"]]);
  263. _root_initializing_0.setAction(this._root_initializing_0_exec);
  264. this.states["/root/initializing"].addTransition(_root_initializing_0);
  265. // transition /root/creating
  266. var _root_creating_0 = new Transition(this, this.states["/root/creating"], [this.states["/root/packing"]]);
  267. _root_creating_0.setAction(this._root_creating_0_exec);
  268. _root_creating_0.trigger = new Event("instance_created", null);
  269. this.states["/root/creating"].addTransition(_root_creating_0);
  270. // transition /root/packing
  271. var _root_packing_0 = new Transition(this, this.states["/root/packing"], [this.states["/root/running"]]);
  272. _root_packing_0.trigger = new Event("button_created", null);
  273. this.states["/root/packing"].addTransition(_root_packing_0);
  274. // transition /root/running/main_behaviour/running
  275. var _root_running_main_behaviour_running_0 = new Transition(this, this.states["/root/running/main_behaviour/running"], [this.states["/root/running/main_behaviour/creating"]]);
  276. _root_running_main_behaviour_running_0.setAction(this._root_running_main_behaviour_running_0_exec);
  277. _root_running_main_behaviour_running_0.trigger = new Event("right_click", "field_ui");
  278. this.states["/root/running/main_behaviour/running"].addTransition(_root_running_main_behaviour_running_0);
  279. // transition /root/running/main_behaviour/creating
  280. var _root_running_main_behaviour_creating_0 = new Transition(this, this.states["/root/running/main_behaviour/creating"], [this.states["/root/running/main_behaviour/running"]]);
  281. _root_running_main_behaviour_creating_0.setAction(this._root_running_main_behaviour_creating_0_exec);
  282. _root_running_main_behaviour_creating_0.trigger = new Event("instance_created", null);
  283. this.states["/root/running/main_behaviour/creating"].addTransition(_root_running_main_behaviour_creating_0);
  284. // transition /root/running/deleting_behaviour/running
  285. var _root_running_deleting_behaviour_running_0 = new Transition(this, this.states["/root/running/deleting_behaviour/running"], [this.states["/root/running/deleting_behaviour/running"]]);
  286. _root_running_deleting_behaviour_running_0.setAction(this._root_running_deleting_behaviour_running_0_exec);
  287. _root_running_deleting_behaviour_running_0.trigger = new Event("delete_ball", null);
  288. this.states["/root/running/deleting_behaviour/running"].addTransition(_root_running_deleting_behaviour_running_0);
  289. // transition /root/running/child_behaviour/listening
  290. var _root_running_child_behaviour_listening_0 = new Transition(this, this.states["/root/running/child_behaviour/listening"], [this.states["/root/running/child_behaviour/listening"]]);
  291. _root_running_child_behaviour_listening_0.setAction(this._root_running_child_behaviour_listening_0_exec);
  292. _root_running_child_behaviour_listening_0.trigger = new Event("button_pressed", null);
  293. this.states["/root/running/child_behaviour/listening"].addTransition(_root_running_child_behaviour_listening_0);
  294. // transition /root/running/deleting_balls_behaviour/listening
  295. var _root_running_deleting_balls_behaviour_listening_0 = new Transition(this, this.states["/root/running/deleting_balls_behaviour/listening"], [this.states["/root/running/deleting_balls_behaviour/listening"]]);
  296. _root_running_deleting_balls_behaviour_listening_0.setAction(this._root_running_deleting_balls_behaviour_listening_0_exec);
  297. _root_running_deleting_balls_behaviour_listening_0.trigger = new Event("key_press", "field_ui");
  298. _root_running_deleting_balls_behaviour_listening_0.setGuard(this._root_running_deleting_balls_behaviour_listening_0_guard);
  299. this.states["/root/running/deleting_balls_behaviour/listening"].addTransition(_root_running_deleting_balls_behaviour_listening_0);
  300. // transition /root/deleting
  301. var _root_deleting_0 = new Transition(this, this.states["/root/deleting"], [this.states["/root/deleted"]]);
  302. _root_deleting_0.setAction(this._root_deleting_0_exec);
  303. this.states["/root/deleting"].addTransition(_root_deleting_0);
  304. // transition /root/running
  305. var _root_running_0 = new Transition(this, this.states["/root/running"], [this.states["/root/deleting"]]);
  306. _root_running_0.setAction(this._root_running_0_exec);
  307. _root_running_0.trigger = new Event("window_close", "field_ui");
  308. this.states["/root/running"].addTransition(_root_running_0);
  309. };
  310. Field.prototype._root_running_0_exec = function(parameters) {
  311. this.big_step.outputEventOM(new Event("delete_instance", null, [this, "buttons"]));
  312. this.big_step.outputEventOM(new Event("delete_instance", null, [this, "balls"]));
  313. };
  314. Field.prototype._root_waiting_0_exec = function(parameters) {
  315. var association_name = parameters[0];
  316. this.association_name = association_name
  317. };
  318. Field.prototype._root_initializing_0_exec = function(parameters) {
  319. this.big_step.outputEventOM(new Event("create_instance", null, [this, "buttons", "Button", this, 'create_new_field', 'Spawn New Window']));
  320. };
  321. Field.prototype._root_creating_0_exec = function(parameters) {
  322. var association_name = parameters[0];
  323. this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
  324. };
  325. Field.prototype._root_running_main_behaviour_running_0_exec = function(parameters) {
  326. var x = parameters[0];
  327. var y = parameters[1];
  328. var button = parameters[2];
  329. this.big_step.outputEventOM(new Event("create_instance", null, [this, "balls", "Ball", this.canvas, x, y]));
  330. };
  331. Field.prototype._root_running_main_behaviour_creating_0_exec = function(parameters) {
  332. var association_name = parameters[0];
  333. this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
  334. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, association_name, new Event("set_association_name", null, [association_name])]));
  335. };
  336. Field.prototype._root_running_deleting_behaviour_running_0_exec = function(parameters) {
  337. var association_name = parameters[0];
  338. this.big_step.outputEventOM(new Event("delete_instance", null, [this, association_name]));
  339. };
  340. Field.prototype._root_running_child_behaviour_listening_0_exec = function(parameters) {
  341. var event_name = parameters[0];
  342. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("button_pressed", null, [event_name])]));
  343. };
  344. Field.prototype._root_running_deleting_balls_behaviour_listening_0_exec = function(parameters) {
  345. var key = parameters[0];
  346. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'balls', new Event("delete_self", null, new Array())]));
  347. };
  348. Field.prototype._root_running_deleting_balls_behaviour_listening_0_guard = function(parameters) {
  349. var key = parameters[0];
  350. return key == ui.KEYCODES.DELETE;
  351. };
  352. Field.prototype._root_deleting_0_exec = function(parameters) {
  353. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("delete_field", null, [this.association_name])]));
  354. };
  355. Field.prototype.initializeStatechart = function() {
  356. // enter default state
  357. var states = this.states["/root"].getEffectiveTargetStates();
  358. this.updateConfiguration(states);
  359. for (var state_idx in states) {
  360. if (!states.hasOwnProperty(state_idx)) continue;
  361. var state = states[state_idx]
  362. if (state.enter) {
  363. state.enter();
  364. }
  365. }
  366. };
  367. // add symbol 'Field' to package 'Bouncing_Balls_JavaScript_Version'
  368. Bouncing_Balls_JavaScript_Version.Field = Field;
  369. var Button = function(controller, parent, event_name, button_text) {
  370. RuntimeClassBase.call(this, controller);
  371. this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
  372. this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
  373. this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
  374. this.semantics.priority = StatechartSemantics.SourceParent;
  375. this.semantics.concurrency = StatechartSemantics.Single;
  376. // build Statechart structure
  377. this.build_statechart_structure();
  378. this.inports["button_ui"] = controller.addInputPort("button_ui", this);
  379. // call user defined constructor
  380. Button.prototype.user_defined_constructor.call(this, parent, event_name, button_text);
  381. };
  382. Button.prototype = new Object();
  383. (function() {
  384. var proto = new RuntimeClassBase();
  385. for (prop in proto) {
  386. Button.prototype[prop] = proto[prop];
  387. }
  388. })();
  389. Button.prototype.user_defined_constructor = function(parent, event_name, button_text) {
  390. this.event_name = event_name;
  391. var button = ui.append_button(parent.field_window, event_name);
  392. ui.bind_event(button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'mouse_click', this.inports["button_ui"]);
  393. };
  394. Button.prototype.user_defined_destructor = function() {
  395. };
  396. // builds Statechart structure
  397. Button.prototype.build_statechart_structure = function() {
  398. // state <root>
  399. this.states[""] = new State(0, this);
  400. // state /initializing
  401. this.states["/initializing"] = new State(1, this);
  402. // state /running
  403. this.states["/running"] = new State(2, this);
  404. // add children
  405. this.states[""].addChild(this.states["/initializing"]);
  406. this.states[""].addChild(this.states["/running"]);
  407. this.states[""].fixTree();
  408. this.states[""].default_state = this.states["/initializing"];
  409. // transition /initializing
  410. var _initializing_0 = new Transition(this, this.states["/initializing"], [this.states["/running"]]);
  411. _initializing_0.setAction(this._initializing_0_exec);
  412. this.states["/initializing"].addTransition(_initializing_0);
  413. // transition /running
  414. var _running_0 = new Transition(this, this.states["/running"], [this.states["/running"]]);
  415. _running_0.setAction(this._running_0_exec);
  416. _running_0.trigger = new Event("mouse_click", "button_ui");
  417. _running_0.setGuard(this._running_0_guard);
  418. this.states["/running"].addTransition(_running_0);
  419. };
  420. Button.prototype._initializing_0_exec = function(parameters) {
  421. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("button_created", null, new Array())]));
  422. };
  423. Button.prototype._running_0_exec = function(parameters) {
  424. var x = parameters[0];
  425. var y = parameters[1];
  426. var button = parameters[2];
  427. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("button_pressed", null, [this.event_name])]));
  428. };
  429. Button.prototype._running_0_guard = function(parameters) {
  430. var x = parameters[0];
  431. var y = parameters[1];
  432. var button = parameters[2];
  433. return button == ui.MOUSE_BUTTONS.LEFT;
  434. };
  435. Button.prototype.initializeStatechart = function() {
  436. // enter default state
  437. var states = this.states["/initializing"].getEffectiveTargetStates();
  438. this.updateConfiguration(states);
  439. for (var state_idx in states) {
  440. if (!states.hasOwnProperty(state_idx)) continue;
  441. var state = states[state_idx]
  442. if (state.enter) {
  443. state.enter();
  444. }
  445. }
  446. };
  447. // add symbol 'Button' to package 'Bouncing_Balls_JavaScript_Version'
  448. Bouncing_Balls_JavaScript_Version.Button = Button;
  449. var Ball = function(controller, canvas, x, y) {
  450. RuntimeClassBase.call(this, controller);
  451. this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
  452. this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
  453. this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
  454. this.semantics.priority = StatechartSemantics.SourceParent;
  455. this.semantics.concurrency = StatechartSemantics.Single;
  456. // build Statechart structure
  457. this.build_statechart_structure();
  458. this.inports["ball_ui"] = controller.addInputPort("ball_ui", this);
  459. // user defined attributes
  460. this.canvas = null;
  461. // call user defined constructor
  462. Ball.prototype.user_defined_constructor.call(this, canvas, x, y);
  463. };
  464. Ball.prototype = new Object();
  465. (function() {
  466. var proto = new RuntimeClassBase();
  467. for (prop in proto) {
  468. Ball.prototype[prop] = proto[prop];
  469. }
  470. })();
  471. Ball.prototype.user_defined_constructor = function(canvas, x, y) {
  472. this.canvas = canvas;
  473. this.r = 20.0;
  474. this.vel = {'x': utils.random() * 3.0, 'y': utils.random() * 3.0};
  475. this.mouse_pos = {};
  476. this.smooth = 0.4; // value between 0 and 1
  477. var circle = this.canvas.add_circle(x, y, this.r, {'fill':'#000'});
  478. ui.bind_event(circle, ui.EVENTS.MOUSE_PRESS, this.controller, 'mouse_press', this.inports["ball_ui"]);
  479. this.element = circle;
  480. };
  481. Ball.prototype.user_defined_destructor = function() {
  482. this.canvas.remove_element(this.element);
  483. };
  484. // builds Statechart structure
  485. Ball.prototype.build_statechart_structure = function() {
  486. // state <root>
  487. this.states[""] = new State(0, this);
  488. // state /main_behaviour
  489. this.states["/main_behaviour"] = new State(1, this);
  490. // state /main_behaviour/initializing
  491. this.states["/main_behaviour/initializing"] = new State(2, this);
  492. // state /main_behaviour/bouncing
  493. this.states["/main_behaviour/bouncing"] = new State(3, this);
  494. this.states["/main_behaviour/bouncing"].setEnter(this._main_behaviour_bouncing_enter);
  495. this.states["/main_behaviour/bouncing"].setExit(this._main_behaviour_bouncing_exit);
  496. // state /main_behaviour/dragging
  497. this.states["/main_behaviour/dragging"] = new State(4, this);
  498. // state /main_behaviour/selected
  499. this.states["/main_behaviour/selected"] = new State(5, this);
  500. // state /deleted
  501. this.states["/deleted"] = new State(6, this);
  502. // add children
  503. this.states[""].addChild(this.states["/main_behaviour"]);
  504. this.states[""].addChild(this.states["/deleted"]);
  505. this.states["/main_behaviour"].addChild(this.states["/main_behaviour/initializing"]);
  506. this.states["/main_behaviour"].addChild(this.states["/main_behaviour/bouncing"]);
  507. this.states["/main_behaviour"].addChild(this.states["/main_behaviour/dragging"]);
  508. this.states["/main_behaviour"].addChild(this.states["/main_behaviour/selected"]);
  509. this.states[""].fixTree();
  510. this.states[""].default_state = this.states["/main_behaviour"];
  511. this.states["/main_behaviour"].default_state = this.states["/main_behaviour/initializing"];
  512. // transition /main_behaviour/initializing
  513. var _main_behaviour_initializing_0 = new Transition(this, this.states["/main_behaviour/initializing"], [this.states["/main_behaviour/bouncing"]]);
  514. _main_behaviour_initializing_0.setAction(this._main_behaviour_initializing_0_exec);
  515. _main_behaviour_initializing_0.trigger = new Event("set_association_name", null);
  516. this.states["/main_behaviour/initializing"].addTransition(_main_behaviour_initializing_0);
  517. // transition /main_behaviour/bouncing
  518. var _main_behaviour_bouncing_0 = new Transition(this, this.states["/main_behaviour/bouncing"], [this.states["/main_behaviour/bouncing"]]);
  519. _main_behaviour_bouncing_0.setAction(this._main_behaviour_bouncing_0_exec);
  520. _main_behaviour_bouncing_0.trigger = new Event("_0after");
  521. this.states["/main_behaviour/bouncing"].addTransition(_main_behaviour_bouncing_0);
  522. var _main_behaviour_bouncing_1 = new Transition(this, this.states["/main_behaviour/bouncing"], [this.states["/main_behaviour/selected"]]);
  523. _main_behaviour_bouncing_1.setAction(this._main_behaviour_bouncing_1_exec);
  524. _main_behaviour_bouncing_1.trigger = new Event("mouse_press", "ball_ui");
  525. _main_behaviour_bouncing_1.setGuard(this._main_behaviour_bouncing_1_guard);
  526. this.states["/main_behaviour/bouncing"].addTransition(_main_behaviour_bouncing_1);
  527. // transition /main_behaviour/dragging
  528. var _main_behaviour_dragging_0 = new Transition(this, this.states["/main_behaviour/dragging"], [this.states["/main_behaviour/dragging"]]);
  529. _main_behaviour_dragging_0.setAction(this._main_behaviour_dragging_0_exec);
  530. _main_behaviour_dragging_0.trigger = new Event("mouse_move", "ui");
  531. this.states["/main_behaviour/dragging"].addTransition(_main_behaviour_dragging_0);
  532. var _main_behaviour_dragging_1 = new Transition(this, this.states["/main_behaviour/dragging"], [this.states["/main_behaviour/bouncing"]]);
  533. _main_behaviour_dragging_1.setAction(this._main_behaviour_dragging_1_exec);
  534. _main_behaviour_dragging_1.trigger = new Event("mouse_release", "ui");
  535. this.states["/main_behaviour/dragging"].addTransition(_main_behaviour_dragging_1);
  536. // transition /main_behaviour/selected
  537. var _main_behaviour_selected_0 = new Transition(this, this.states["/main_behaviour/selected"], [this.states["/main_behaviour/dragging"]]);
  538. _main_behaviour_selected_0.setAction(this._main_behaviour_selected_0_exec);
  539. _main_behaviour_selected_0.trigger = new Event("mouse_press", "ball_ui");
  540. _main_behaviour_selected_0.setGuard(this._main_behaviour_selected_0_guard);
  541. this.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_0);
  542. var _main_behaviour_selected_1 = new Transition(this, this.states["/main_behaviour/selected"], [this.states["/deleted"]]);
  543. _main_behaviour_selected_1.setAction(this._main_behaviour_selected_1_exec);
  544. _main_behaviour_selected_1.trigger = new Event("delete_self", null);
  545. this.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_1);
  546. };
  547. Ball.prototype._main_behaviour_bouncing_enter = function() {
  548. this.addTimer(0, 0.05);
  549. };
  550. Ball.prototype._main_behaviour_bouncing_exit = function() {
  551. this.removeTimer(0);
  552. };
  553. Ball.prototype._main_behaviour_initializing_0_exec = function(parameters) {
  554. var association_name = parameters[0];
  555. this.association_name = association_name;
  556. };
  557. Ball.prototype._main_behaviour_bouncing_0_exec = function(parameters) {
  558. var pos = this.element.get_position();
  559. if (pos.x-this.r <= 0 || pos.x+this.r >= this.canvas.width)
  560. this.vel.x = -this.vel.x;
  561. if (pos.y-this.r <= 0 || pos.y+this.r >= this.canvas.height)
  562. this.vel.y = -this.vel.y;
  563. this.element.move(this.vel.x, this.vel.y);
  564. };
  565. Ball.prototype._main_behaviour_bouncing_1_exec = function(parameters) {
  566. var x = parameters[0];
  567. var y = parameters[1];
  568. var button = parameters[2];
  569. this.element.set_color("#ff0");
  570. };
  571. Ball.prototype._main_behaviour_bouncing_1_guard = function(parameters) {
  572. var x = parameters[0];
  573. var y = parameters[1];
  574. var button = parameters[2];
  575. return button == ui.MOUSE_BUTTONS.LEFT;
  576. };
  577. Ball.prototype._main_behaviour_dragging_0_exec = function(parameters) {
  578. var x = parameters[0];
  579. var y = parameters[1];
  580. var button = parameters[2];
  581. var dx = x - this.mouse_pos.x;
  582. var dy = y - this.mouse_pos.y;
  583. this.element.move(dx, dy);
  584. // keep ball within boundaries
  585. var pos = this.element.get_position();
  586. if (pos.x-this.r <= 0)
  587. pos.x = this.r + 1;
  588. else if (pos.x+this.r >= this.canvas.width)
  589. pos.x = this.canvas.width-this.r-1;
  590. if (pos.y-this.r <= 0)
  591. pos.y = this.r + 1;
  592. else if (pos.y+this.r >= this.canvas.height)
  593. pos.y = this.canvas.height-this.r-1;
  594. this.element.set_position(pos.x, pos.y);
  595. this.mouse_pos = {'x':x, 'y':y};
  596. this.vel = {
  597. 'x': (1-this.smooth)*dx + this.smooth*this.vel.x,
  598. 'y': (1-this.smooth)*dy + this.smooth*this.vel.y
  599. };
  600. };
  601. Ball.prototype._main_behaviour_dragging_1_exec = function(parameters) {
  602. var x = parameters[0];
  603. var y = parameters[1];
  604. this.element.set_color("#f00");
  605. };
  606. Ball.prototype._main_behaviour_selected_0_exec = function(parameters) {
  607. var x = parameters[0];
  608. var y = parameters[1];
  609. var button = parameters[2];
  610. this.mouse_pos = {'x':x, 'y':y};
  611. };
  612. Ball.prototype._main_behaviour_selected_0_guard = function(parameters) {
  613. var x = parameters[0];
  614. var y = parameters[1];
  615. var button = parameters[2];
  616. return button == ui.MOUSE_BUTTONS.LEFT;
  617. };
  618. Ball.prototype._main_behaviour_selected_1_exec = function(parameters) {
  619. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("delete_ball", null, [this.association_name])]));
  620. };
  621. Ball.prototype.initializeStatechart = function() {
  622. // enter default state
  623. var states = this.states["/main_behaviour"].getEffectiveTargetStates();
  624. this.updateConfiguration(states);
  625. for (var state_idx in states) {
  626. if (!states.hasOwnProperty(state_idx)) continue;
  627. var state = states[state_idx]
  628. if (state.enter) {
  629. state.enter();
  630. }
  631. }
  632. };
  633. // add symbol 'Ball' to package 'Bouncing_Balls_JavaScript_Version'
  634. Bouncing_Balls_JavaScript_Version.Ball = Ball;
  635. var ObjectManager = function(controller) {
  636. ObjectManagerBase.call(this, controller);
  637. };
  638. ObjectManager.prototype = new Object();
  639. (function() {
  640. var proto = new ObjectManagerBase();
  641. for (prop in proto) {
  642. ObjectManager.prototype[prop] = proto[prop];
  643. }
  644. })();
  645. ObjectManager.prototype.instantiate = function(class_name, construct_params) {
  646. if (class_name === "MainApp") {
  647. var instance = new MainApp(this.controller);
  648. instance.associations = new Object();
  649. instance.associations["fields"] = new Association("Field", 0, -1);
  650. } else if (class_name === "Field") {
  651. var instance = new Field(this.controller);
  652. instance.associations = new Object();
  653. instance.associations["balls"] = new Association("Ball", 0, -1);
  654. instance.associations["buttons"] = new Association("Button", 0, -1);
  655. instance.associations["parent"] = new Association("MainApp", 1, 1);
  656. } else if (class_name === "Button") {
  657. var instance = new Button(this.controller, construct_params[0], construct_params[1], construct_params[2]);
  658. instance.associations = new Object();
  659. instance.associations["parent"] = new Association("Field", 1, 1);
  660. } else if (class_name === "Ball") {
  661. var instance = new Ball(this.controller, construct_params[0], construct_params[1], construct_params[2]);
  662. instance.associations = new Object();
  663. instance.associations["parent"] = new Association("Field", 1, 1);
  664. } else {
  665. throw new Error("Cannot instantiate class " + class_name);
  666. }
  667. return instance;
  668. };
  669. // add symbol 'ObjectManager' to package 'Bouncing_Balls_JavaScript_Version'
  670. Bouncing_Balls_JavaScript_Version.ObjectManager = ObjectManager;
  671. var Controller = function(event_loop_callbacks, finished_callback) {
  672. if (finished_callback === undefined) finished_callback = null;
  673. EventLoopControllerBase.call(this, new ObjectManager(this), event_loop_callbacks, finished_callback);
  674. this.addInputPort("ui");
  675. this.object_manager.createInstance("MainApp", new Array());
  676. };
  677. Controller.prototype = new Object();
  678. (function() {
  679. var proto = new EventLoopControllerBase();
  680. for (prop in proto) {
  681. Controller.prototype[prop] = proto[prop];
  682. }
  683. })();
  684. // add symbol 'Controller' to package 'Bouncing_Balls_JavaScript_Version'
  685. Bouncing_Balls_JavaScript_Version.Controller = Controller;
  686. })();