bouncing_balls_backup.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. /**
  2. * Statechart compiler by Glenn De Jonghe
  3. * Javascript generator by Joeri Exelmans
  4. *
  5. * Date: Thu Feb 04 14:38:35 2016
  6. *
  7. * Model author: Simon Van Mierlo+Joeri Exelmans+Raphael Mannadiar
  8. * Model name: Bouncing_Balls
  9. * Model description:
  10. Tkinter frame with bouncing balls in it.
  11. */
  12. // put everything in an object (serves as "namespace")
  13. Bouncing_Balls = {};
  14. // closure scope
  15. (function() {
  16. // The actual constructor
  17. var Button = function(controller, parent, event_name, button_text) {
  18. Button.prototype.commonConstructor.call(this,controller);
  19. // constructor body (user-defined)
  20. this.event_name = event_name;
  21. var button = ui.append_button(parent.field_window, event_name);
  22. ui.bind_event(button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'mouse_click');
  23. };
  24. Button.prototype = new RuntimeClassBase();
  25. // Unique IDs for all statechart nodes
  26. Button.prototype.Root = 0;
  27. Button.prototype.Root_initializing = 1;
  28. Button.prototype.Root_running = 2;
  29. // Statechart enter/exit action method(s) :
  30. Button.prototype.enter_Root_initializing = function() {
  31. this.current_state[this.Root].push(this.Root_initializing);
  32. };
  33. Button.prototype.exit_Root_initializing = function() {
  34. this.current_state[this.Root] = new Array();
  35. };
  36. Button.prototype.enter_Root_running = function() {
  37. this.current_state[this.Root].push(this.Root_running);
  38. };
  39. Button.prototype.exit_Root_running = function() {
  40. this.current_state[this.Root] = new Array();
  41. };
  42. // Statechart transitions :
  43. Button.prototype.transition_Root = function(event) {
  44. var catched = false;
  45. if (!catched) {
  46. if (this.current_state[this.Root][0] === this.Root_initializing) {
  47. catched = this.transition_Root_initializing(event);
  48. }
  49. else if (this.current_state[this.Root][0] === this.Root_running) {
  50. catched = this.transition_Root_running(event);
  51. }
  52. }
  53. return catched;
  54. };
  55. Button.prototype.transition_Root_initializing = function(event) {
  56. var catched = false;
  57. var enableds = new Array();
  58. enableds.push(1);
  59. if (enableds.length > 1) {
  60. console.log("Runtime warning : indeterminism detected in a transition from node Root_initializing. Only the first in document order enabled transition is executed.")
  61. }
  62. if (enableds.length > 0) {
  63. var enabled = enableds[0];
  64. if (enabled === 1) {
  65. this.exit_Root_initializing();
  66. var send_event = new Event("button_created", null, []);
  67. this.object_manager.addEvent(new Event("narrow_cast", null, [this, 'parent' , send_event]));
  68. this.enter_Root_running();
  69. }
  70. catched = true;
  71. }
  72. return catched;
  73. };
  74. Button.prototype.transition_Root_running = function(event) {
  75. var catched = false;
  76. var enableds = new Array();
  77. if (event.name === "mouse_click" && event.port === "ui") {
  78. var parameters = event.parameters;
  79. var x = parameters[0];
  80. var y = parameters[1];
  81. var button = parameters[2];
  82. if (button == ui.MOUSE_BUTTONS.LEFT) {
  83. enableds.push(1);
  84. }
  85. }
  86. if (enableds.length > 1) {
  87. console.log("Runtime warning : indeterminism detected in a transition from node Root_running. Only the first in document order enabled transition is executed.")
  88. }
  89. if (enableds.length > 0) {
  90. var enabled = enableds[0];
  91. if (enabled === 1) {
  92. var parameters = event.parameters;
  93. var x = parameters[0];
  94. var y = parameters[1];
  95. var button = parameters[2];
  96. this.exit_Root_running();
  97. var send_event = new Event("button_pressed", null, [this.event_name]);
  98. this.object_manager.addEvent(new Event("narrow_cast", null, [this, 'parent' , send_event]));
  99. this.enter_Root_running();
  100. }
  101. catched = true;
  102. }
  103. return catched;
  104. };
  105. // Execute transitions
  106. Button.prototype.transition = function(event) {
  107. if (!event) event = new Event();
  108. this.state_changed = this.transition_Root(event);
  109. };
  110. // inState method for statechart
  111. Button.prototype.inState = function(nodes) {
  112. for (var c in this.current_state) {
  113. if (!this.current_state.hasOwnProperty(c)) continue;
  114. var new_nodes = new Array();
  115. for (var n in nodes) {
  116. if (!nodes.hasOwnProperty(n)) continue;
  117. if (this.current_state[c].indexOf(nodes[n]) === -1) {
  118. new_nodes.push(nodes[n]);
  119. }
  120. }
  121. nodes = new_nodes;
  122. if (nodes.length === 0) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. };
  128. Button.prototype.commonConstructor = function(controller) {
  129. if (!controller) controller = null;
  130. // Constructor part that is common for all constructors.
  131. RuntimeClassBase.call(this);
  132. // User defined input ports
  133. this.inports = new Object();
  134. this.controller = controller;
  135. this.object_manager = (controller == null ? null : controller.object_manager);
  136. this.current_state = new Object();
  137. this.history_state = new Object();
  138. // Initialize statechart
  139. this.current_state[this.Root] = new Array();
  140. };
  141. Button.prototype.start = function() {
  142. RuntimeClassBase.prototype.start.call(this);
  143. this.enter_Root_initializing();
  144. };
  145. // put class in global diagram object
  146. Bouncing_Balls.Button = Button;
  147. // The actual constructor
  148. var Field = function(controller) {
  149. Field.prototype.commonConstructor.call(this,controller);
  150. // constructor body (user-defined)
  151. this.field_window = ui.new_window(400, 450);
  152. this.canvas = ui.append_canvas(this.field_window, 400, 400, {'background':'#eee'});
  153. ui.bind_event(this.field_window, ui.EVENTS.WINDOW_CLOSE, this.controller, 'window_close');
  154. ui.bind_event(this.field_window, ui.EVENTS.KEY_PRESS, this.controller, 'key_press');
  155. ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_RIGHT_CLICK, this.controller, 'right_click', this.inports['field_ui']);
  156. ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_MOVE, this.controller, 'mouse_move');
  157. ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_RELEASE, this.controller, 'mouse_release');
  158. };
  159. Field.prototype = new RuntimeClassBase();
  160. // Unique IDs for all statechart nodes
  161. Field.prototype.Root = 0;
  162. Field.prototype.Root_root = 1;
  163. Field.prototype.Root_root_running = 2;
  164. Field.prototype.Root_root_running_main_behaviour = 3;
  165. Field.prototype.Root_root_running_deleting_behaviour = 4;
  166. Field.prototype.Root_root_running_child_behaviour = 5;
  167. Field.prototype.Root_root_waiting = 6;
  168. Field.prototype.Root_root_packing = 7;
  169. Field.prototype.Root_root_deleting = 8;
  170. Field.prototype.Root_root_creating = 9;
  171. Field.prototype.Root_root_initializing = 10;
  172. Field.prototype.Root_root_deleted = 11;
  173. Field.prototype.Root_root_running_main_behaviour_running = 12;
  174. Field.prototype.Root_root_running_main_behaviour_creating = 13;
  175. Field.prototype.Root_root_running_deleting_behaviour_running = 14;
  176. Field.prototype.Root_root_running_child_behaviour_listening = 15;
  177. // User defined destructor
  178. Field.prototype.destructor = function() {
  179. ui.close_window(this.field_window);
  180. };
  181. // Statechart enter/exit action method(s) :
  182. Field.prototype.enter_Root_root = function() {
  183. this.current_state[this.Root].push(this.Root_root);
  184. };
  185. Field.prototype.exit_Root_root = function() {
  186. if (this.current_state[this.Root_root].indexOf(this.Root_root_waiting) !== -1) {
  187. this.exit_Root_root_waiting();
  188. }
  189. if (this.current_state[this.Root_root].indexOf(this.Root_root_packing) !== -1) {
  190. this.exit_Root_root_packing();
  191. }
  192. if (this.current_state[this.Root_root].indexOf(this.Root_root_deleting) !== -1) {
  193. this.exit_Root_root_deleting();
  194. }
  195. if (this.current_state[this.Root_root].indexOf(this.Root_root_creating) !== -1) {
  196. this.exit_Root_root_creating();
  197. }
  198. if (this.current_state[this.Root_root].indexOf(this.Root_root_initializing) !== -1) {
  199. this.exit_Root_root_initializing();
  200. }
  201. if (this.current_state[this.Root_root].indexOf(this.Root_root_deleted) !== -1) {
  202. this.exit_Root_root_deleted();
  203. }
  204. if (this.current_state[this.Root_root].indexOf(this.Root_root_running) !== -1) {
  205. this.exit_Root_root_running();
  206. }
  207. this.current_state[this.Root] = new Array();
  208. };
  209. Field.prototype.enter_Root_root_running = function() {
  210. this.current_state[this.Root_root].push(this.Root_root_running);
  211. };
  212. Field.prototype.exit_Root_root_running = function() {
  213. this.exit_Root_root_running_main_behaviour();
  214. this.exit_Root_root_running_deleting_behaviour();
  215. this.exit_Root_root_running_child_behaviour();
  216. this.current_state[this.Root_root] = new Array();
  217. };
  218. Field.prototype.enter_Root_root_running_main_behaviour = function() {
  219. this.current_state[this.Root_root_running].push(this.Root_root_running_main_behaviour);
  220. };
  221. Field.prototype.exit_Root_root_running_main_behaviour = function() {
  222. if (this.current_state[this.Root_root_running_main_behaviour].indexOf(this.Root_root_running_main_behaviour_running) !== -1) {
  223. this.exit_Root_root_running_main_behaviour_running();
  224. }
  225. if (this.current_state[this.Root_root_running_main_behaviour].indexOf(this.Root_root_running_main_behaviour_creating) !== -1) {
  226. this.exit_Root_root_running_main_behaviour_creating();
  227. }
  228. this.current_state[this.Root_root_running] = new Array();
  229. };
  230. Field.prototype.enter_Root_root_running_deleting_behaviour = function() {
  231. this.current_state[this.Root_root_running].push(this.Root_root_running_deleting_behaviour);
  232. };
  233. Field.prototype.exit_Root_root_running_deleting_behaviour = function() {
  234. if (this.current_state[this.Root_root_running_deleting_behaviour].indexOf(this.Root_root_running_deleting_behaviour_running) !== -1) {
  235. this.exit_Root_root_running_deleting_behaviour_running();
  236. }
  237. this.current_state[this.Root_root_running] = new Array();
  238. };
  239. Field.prototype.enter_Root_root_running_child_behaviour = function() {
  240. this.current_state[this.Root_root_running].push(this.Root_root_running_child_behaviour);
  241. };
  242. Field.prototype.exit_Root_root_running_child_behaviour = function() {
  243. if (this.current_state[this.Root_root_running_child_behaviour].indexOf(this.Root_root_running_child_behaviour_listening) !== -1) {
  244. this.exit_Root_root_running_child_behaviour_listening();
  245. }
  246. this.current_state[this.Root_root_running] = new Array();
  247. };
  248. Field.prototype.enter_Root_root_waiting = function() {
  249. this.current_state[this.Root_root].push(this.Root_root_waiting);
  250. };
  251. Field.prototype.exit_Root_root_waiting = function() {
  252. this.current_state[this.Root_root] = new Array();
  253. };
  254. Field.prototype.enter_Root_root_packing = function() {
  255. this.current_state[this.Root_root].push(this.Root_root_packing);
  256. };
  257. Field.prototype.exit_Root_root_packing = function() {
  258. this.current_state[this.Root_root] = new Array();
  259. };
  260. Field.prototype.enter_Root_root_deleting = function() {
  261. this.timers[0] = 0.05 * 1000.0; /* convert ms to s */
  262. this.current_state[this.Root_root].push(this.Root_root_deleting);
  263. };
  264. Field.prototype.exit_Root_root_deleting = function() {
  265. delete this.timers[0];
  266. this.current_state[this.Root_root] = new Array();
  267. };
  268. Field.prototype.enter_Root_root_creating = function() {
  269. this.current_state[this.Root_root].push(this.Root_root_creating);
  270. };
  271. Field.prototype.exit_Root_root_creating = function() {
  272. this.current_state[this.Root_root] = new Array();
  273. };
  274. Field.prototype.enter_Root_root_initializing = function() {
  275. this.current_state[this.Root_root].push(this.Root_root_initializing);
  276. };
  277. Field.prototype.exit_Root_root_initializing = function() {
  278. this.current_state[this.Root_root] = new Array();
  279. };
  280. Field.prototype.enter_Root_root_deleted = function() {
  281. this.current_state[this.Root_root].push(this.Root_root_deleted);
  282. };
  283. Field.prototype.exit_Root_root_deleted = function() {
  284. this.current_state[this.Root_root] = new Array();
  285. };
  286. Field.prototype.enter_Root_root_running_main_behaviour_running = function() {
  287. this.current_state[this.Root_root_running_main_behaviour].push(this.Root_root_running_main_behaviour_running);
  288. };
  289. Field.prototype.exit_Root_root_running_main_behaviour_running = function() {
  290. this.current_state[this.Root_root_running_main_behaviour] = new Array();
  291. };
  292. Field.prototype.enter_Root_root_running_main_behaviour_creating = function() {
  293. this.current_state[this.Root_root_running_main_behaviour].push(this.Root_root_running_main_behaviour_creating);
  294. };
  295. Field.prototype.exit_Root_root_running_main_behaviour_creating = function() {
  296. this.current_state[this.Root_root_running_main_behaviour] = new Array();
  297. };
  298. Field.prototype.enter_Root_root_running_deleting_behaviour_running = function() {
  299. this.current_state[this.Root_root_running_deleting_behaviour].push(this.Root_root_running_deleting_behaviour_running);
  300. };
  301. Field.prototype.exit_Root_root_running_deleting_behaviour_running = function() {
  302. this.current_state[this.Root_root_running_deleting_behaviour] = new Array();
  303. };
  304. Field.prototype.enter_Root_root_running_child_behaviour_listening = function() {
  305. this.current_state[this.Root_root_running_child_behaviour].push(this.Root_root_running_child_behaviour_listening);
  306. };
  307. Field.prototype.exit_Root_root_running_child_behaviour_listening = function() {
  308. this.current_state[this.Root_root_running_child_behaviour] = new Array();
  309. };
  310. // Statechart enter/exit default method(s) :
  311. Field.prototype.enterDefault_Root_root = function() {
  312. this.enter_Root_root();
  313. this.enter_Root_root_waiting();
  314. };
  315. Field.prototype.enterDefault_Root_root_running = function() {
  316. this.enter_Root_root_running();
  317. this.enterDefault_Root_root_running_main_behaviour();
  318. this.enterDefault_Root_root_running_deleting_behaviour();
  319. this.enterDefault_Root_root_running_child_behaviour();
  320. };
  321. Field.prototype.enterDefault_Root_root_running_main_behaviour = function() {
  322. this.enter_Root_root_running_main_behaviour();
  323. this.enter_Root_root_running_main_behaviour_running();
  324. };
  325. Field.prototype.enterDefault_Root_root_running_deleting_behaviour = function() {
  326. this.enter_Root_root_running_deleting_behaviour();
  327. this.enter_Root_root_running_deleting_behaviour_running();
  328. };
  329. Field.prototype.enterDefault_Root_root_running_child_behaviour = function() {
  330. this.enter_Root_root_running_child_behaviour();
  331. this.enter_Root_root_running_child_behaviour_listening();
  332. };
  333. // Statechart transitions :
  334. Field.prototype.transition_Root = function(event) {
  335. var catched = false;
  336. if (!catched) {
  337. if (this.current_state[this.Root][0] === this.Root_root) {
  338. catched = this.transition_Root_root(event);
  339. }
  340. }
  341. return catched;
  342. };
  343. Field.prototype.transition_Root_root = function(event) {
  344. var catched = false;
  345. if (!catched) {
  346. if (this.current_state[this.Root_root][0] === this.Root_root_waiting) {
  347. catched = this.transition_Root_root_waiting(event);
  348. }
  349. else if (this.current_state[this.Root_root][0] === this.Root_root_packing) {
  350. catched = this.transition_Root_root_packing(event);
  351. }
  352. else if (this.current_state[this.Root_root][0] === this.Root_root_deleting) {
  353. catched = this.transition_Root_root_deleting(event);
  354. }
  355. else if (this.current_state[this.Root_root][0] === this.Root_root_creating) {
  356. catched = this.transition_Root_root_creating(event);
  357. }
  358. else if (this.current_state[this.Root_root][0] === this.Root_root_initializing) {
  359. catched = this.transition_Root_root_initializing(event);
  360. }
  361. else if (this.current_state[this.Root_root][0] === this.Root_root_deleted) {
  362. catched = this.transition_Root_root_deleted(event);
  363. }
  364. else if (this.current_state[this.Root_root][0] === this.Root_root_running) {
  365. catched = this.transition_Root_root_running(event);
  366. }
  367. }
  368. return catched;
  369. };
  370. Field.prototype.transition_Root_root_waiting = function(event) {
  371. var catched = false;
  372. var enableds = new Array();
  373. if (event.name === "set_association_name") {
  374. enableds.push(1);
  375. }
  376. if (enableds.length > 1) {
  377. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_waiting. Only the first in document order enabled transition is executed.")
  378. }
  379. if (enableds.length > 0) {
  380. var enabled = enableds[0];
  381. if (enabled === 1) {
  382. var parameters = event.parameters;
  383. var association_name = parameters[0];
  384. this.exit_Root_root_waiting();
  385. this.association_name = association_name;
  386. this.enter_Root_root_initializing();
  387. }
  388. catched = true;
  389. }
  390. return catched;
  391. };
  392. Field.prototype.transition_Root_root_packing = function(event) {
  393. var catched = false;
  394. var enableds = new Array();
  395. if (event.name === "button_created") {
  396. enableds.push(1);
  397. }
  398. if (enableds.length > 1) {
  399. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_packing. Only the first in document order enabled transition is executed.")
  400. }
  401. if (enableds.length > 0) {
  402. var enabled = enableds[0];
  403. if (enabled === 1) {
  404. this.exit_Root_root_packing();
  405. this.enterDefault_Root_root_running();
  406. }
  407. catched = true;
  408. }
  409. return catched;
  410. };
  411. Field.prototype.transition_Root_root_deleting = function(event) {
  412. var catched = false;
  413. var enableds = new Array();
  414. if (event.name === "_0after") {
  415. enableds.push(1);
  416. }
  417. if (enableds.length > 1) {
  418. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_deleting. Only the first in document order enabled transition is executed.")
  419. }
  420. if (enableds.length > 0) {
  421. var enabled = enableds[0];
  422. if (enabled === 1) {
  423. this.exit_Root_root_deleting();
  424. var send_event = new Event("delete_field", null, [this.association_name]);
  425. this.object_manager.addEvent(new Event("narrow_cast", null, [this, 'parent' , send_event]));
  426. this.enter_Root_root_deleted();
  427. }
  428. catched = true;
  429. }
  430. return catched;
  431. };
  432. Field.prototype.transition_Root_root_creating = function(event) {
  433. var catched = false;
  434. var enableds = new Array();
  435. if (event.name === "instance_created") {
  436. enableds.push(1);
  437. }
  438. if (enableds.length > 1) {
  439. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_creating. Only the first in document order enabled transition is executed.")
  440. }
  441. if (enableds.length > 0) {
  442. var enabled = enableds[0];
  443. if (enabled === 1) {
  444. var parameters = event.parameters;
  445. var association_name = parameters[0];
  446. this.exit_Root_root_creating();
  447. this.object_manager.addEvent(new Event("start_instance", null, [this, association_name]));
  448. this.enter_Root_root_packing();
  449. }
  450. catched = true;
  451. }
  452. return catched;
  453. };
  454. Field.prototype.transition_Root_root_initializing = function(event) {
  455. var catched = false;
  456. var enableds = new Array();
  457. enableds.push(1);
  458. if (enableds.length > 1) {
  459. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_initializing. Only the first in document order enabled transition is executed.")
  460. }
  461. if (enableds.length > 0) {
  462. var enabled = enableds[0];
  463. if (enabled === 1) {
  464. this.exit_Root_root_initializing();
  465. this.object_manager.addEvent(new Event("create_instance", null, [this, 'buttons','Button',this,'create_new_field','Spawn New Window']));
  466. this.enter_Root_root_creating();
  467. }
  468. catched = true;
  469. }
  470. return catched;
  471. };
  472. Field.prototype.transition_Root_root_deleted = function(event) {
  473. var catched = false;
  474. return catched;
  475. };
  476. Field.prototype.transition_Root_root_running = function(event) {
  477. var catched = false;
  478. var enableds = new Array();
  479. if (event.name === "window_close" && event.port === "ui") {
  480. var parameters = event.parameters;
  481. var window = parameters[0];
  482. if (window == this.field_window || window == ui.window) {
  483. enableds.push(1);
  484. }
  485. }
  486. if (enableds.length > 1) {
  487. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_running. Only the first in document order enabled transition is executed.")
  488. }
  489. if (enableds.length > 0) {
  490. var enabled = enableds[0];
  491. if (enabled === 1) {
  492. var parameters = event.parameters;
  493. var window = parameters[0];
  494. this.exit_Root_root_running();
  495. this.object_manager.addEvent(new Event("delete_instance", null, [this, 'buttons']));
  496. var send_event = new Event("delete_self", null, []);
  497. this.object_manager.addEvent(new Event("narrow_cast", null, [this, 'balls' , send_event]));
  498. this.enter_Root_root_deleting();
  499. }
  500. catched = true;
  501. }
  502. if (!catched) {
  503. catched = this.transition_Root_root_running_main_behaviour(event) || catched
  504. catched = this.transition_Root_root_running_deleting_behaviour(event) || catched
  505. catched = this.transition_Root_root_running_child_behaviour(event) || catched
  506. }
  507. return catched;
  508. };
  509. Field.prototype.transition_Root_root_running_main_behaviour = function(event) {
  510. var catched = false;
  511. if (!catched) {
  512. if (this.current_state[this.Root_root_running_main_behaviour][0] === this.Root_root_running_main_behaviour_running) {
  513. catched = this.transition_Root_root_running_main_behaviour_running(event);
  514. }
  515. else if (this.current_state[this.Root_root_running_main_behaviour][0] === this.Root_root_running_main_behaviour_creating) {
  516. catched = this.transition_Root_root_running_main_behaviour_creating(event);
  517. }
  518. }
  519. return catched;
  520. };
  521. Field.prototype.transition_Root_root_running_main_behaviour_running = function(event) {
  522. var catched = false;
  523. var enableds = new Array();
  524. if (event.name === "right_click" && event.port === "field_ui") {
  525. enableds.push(1);
  526. }
  527. if (enableds.length > 1) {
  528. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_running_main_behaviour_running. Only the first in document order enabled transition is executed.")
  529. }
  530. if (enableds.length > 0) {
  531. var enabled = enableds[0];
  532. if (enabled === 1) {
  533. var parameters = event.parameters;
  534. var x = parameters[0];
  535. var y = parameters[1];
  536. var button = parameters[2];
  537. this.exit_Root_root_running_main_behaviour_running();
  538. this.object_manager.addEvent(new Event("create_instance", null, [this, 'balls','Ball',this.canvas,x,y,this.field_window]));
  539. this.enter_Root_root_running_main_behaviour_creating();
  540. }
  541. catched = true;
  542. }
  543. return catched;
  544. };
  545. Field.prototype.transition_Root_root_running_main_behaviour_creating = function(event) {
  546. var catched = false;
  547. var enableds = new Array();
  548. if (event.name === "instance_created") {
  549. enableds.push(1);
  550. }
  551. if (enableds.length > 1) {
  552. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_running_main_behaviour_creating. Only the first in document order enabled transition is executed.")
  553. }
  554. if (enableds.length > 0) {
  555. var enabled = enableds[0];
  556. if (enabled === 1) {
  557. var parameters = event.parameters;
  558. var association_name = parameters[0];
  559. this.exit_Root_root_running_main_behaviour_creating();
  560. var send_event = new Event("set_association_name", null, [association_name]);
  561. this.object_manager.addEvent(new Event("narrow_cast", null, [this, association_name , send_event]));
  562. this.object_manager.addEvent(new Event("start_instance", null, [this, association_name]));
  563. this.enter_Root_root_running_main_behaviour_running();
  564. }
  565. catched = true;
  566. }
  567. return catched;
  568. };
  569. Field.prototype.transition_Root_root_running_deleting_behaviour = function(event) {
  570. var catched = false;
  571. if (!catched) {
  572. if (this.current_state[this.Root_root_running_deleting_behaviour][0] === this.Root_root_running_deleting_behaviour_running) {
  573. catched = this.transition_Root_root_running_deleting_behaviour_running(event);
  574. }
  575. }
  576. return catched;
  577. };
  578. Field.prototype.transition_Root_root_running_deleting_behaviour_running = function(event) {
  579. var catched = false;
  580. var enableds = new Array();
  581. if (event.name === "delete_ball") {
  582. enableds.push(1);
  583. }
  584. if (enableds.length > 1) {
  585. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_running_deleting_behaviour_running. Only the first in document order enabled transition is executed.")
  586. }
  587. if (enableds.length > 0) {
  588. var enabled = enableds[0];
  589. if (enabled === 1) {
  590. var parameters = event.parameters;
  591. var association_name = parameters[0];
  592. this.exit_Root_root_running_deleting_behaviour_running();
  593. this.object_manager.addEvent(new Event("delete_instance", null, [this, association_name]));
  594. this.enter_Root_root_running_deleting_behaviour_running();
  595. }
  596. catched = true;
  597. }
  598. return catched;
  599. };
  600. Field.prototype.transition_Root_root_running_child_behaviour = function(event) {
  601. var catched = false;
  602. if (!catched) {
  603. if (this.current_state[this.Root_root_running_child_behaviour][0] === this.Root_root_running_child_behaviour_listening) {
  604. catched = this.transition_Root_root_running_child_behaviour_listening(event);
  605. }
  606. }
  607. return catched;
  608. };
  609. Field.prototype.transition_Root_root_running_child_behaviour_listening = function(event) {
  610. var catched = false;
  611. var enableds = new Array();
  612. if (event.name === "button_pressed") {
  613. enableds.push(1);
  614. }
  615. if (enableds.length > 1) {
  616. console.log("Runtime warning : indeterminism detected in a transition from node Root_root_running_child_behaviour_listening. Only the first in document order enabled transition is executed.")
  617. }
  618. if (enableds.length > 0) {
  619. var enabled = enableds[0];
  620. if (enabled === 1) {
  621. var parameters = event.parameters;
  622. var event_name = parameters[0];
  623. this.exit_Root_root_running_child_behaviour_listening();
  624. var send_event = new Event("button_pressed", null, [event_name]);
  625. this.object_manager.addEvent(new Event("narrow_cast", null, [this, 'parent' , send_event]));
  626. this.enter_Root_root_running_child_behaviour_listening();
  627. }
  628. catched = true;
  629. }
  630. return catched;
  631. };
  632. // Execute transitions
  633. Field.prototype.transition = function(event) {
  634. if (!event) event = new Event();
  635. this.state_changed = this.transition_Root(event);
  636. };
  637. // inState method for statechart
  638. Field.prototype.inState = function(nodes) {
  639. for (var c in this.current_state) {
  640. if (!this.current_state.hasOwnProperty(c)) continue;
  641. var new_nodes = new Array();
  642. for (var n in nodes) {
  643. if (!nodes.hasOwnProperty(n)) continue;
  644. if (this.current_state[c].indexOf(nodes[n]) === -1) {
  645. new_nodes.push(nodes[n]);
  646. }
  647. }
  648. nodes = new_nodes;
  649. if (nodes.length === 0) {
  650. return true;
  651. }
  652. }
  653. return false;
  654. };
  655. Field.prototype.commonConstructor = function(controller) {
  656. if (!controller) controller = null;
  657. // Constructor part that is common for all constructors.
  658. RuntimeClassBase.call(this);
  659. // User defined input ports
  660. this.inports = new Object();
  661. this.inports["field_ui"] = controller.addInputPort("field_ui", this);
  662. // User defined attributes
  663. this.field_window = null;
  664. this.canvas = null;
  665. this.controller = controller;
  666. this.object_manager = (controller == null ? null : controller.object_manager);
  667. this.current_state = new Object();
  668. this.history_state = new Object();
  669. this.timers = new Object();
  670. // Initialize statechart
  671. this.current_state[this.Root] = new Array();
  672. this.current_state[this.Root_root] = new Array();
  673. this.current_state[this.Root_root_running] = new Array();
  674. this.current_state[this.Root_root_running_main_behaviour] = new Array();
  675. this.current_state[this.Root_root_running_deleting_behaviour] = new Array();
  676. this.current_state[this.Root_root_running_child_behaviour] = new Array();
  677. };
  678. Field.prototype.start = function() {
  679. RuntimeClassBase.prototype.start.call(this);
  680. this.enterDefault_Root_root();
  681. };
  682. // put class in global diagram object
  683. Bouncing_Balls.Field = Field;
  684. // The actual constructor
  685. var MainApp = function(controller) {
  686. MainApp.prototype.commonConstructor.call(this,controller);
  687. // constructor body (user-defined)
  688. this.nr_of_fields = 0;
  689. ui.bind_event(ui.window, ui.EVENTS.WINDOW_CLOSE, this.controller, 'window_close');
  690. };
  691. MainApp.prototype = new RuntimeClassBase();
  692. // Unique IDs for all statechart nodes
  693. MainApp.prototype.Root = 0;
  694. MainApp.prototype.Root_running = 1;
  695. MainApp.prototype.Root_running_root = 2;
  696. MainApp.prototype.Root_running_root_main_behaviour = 3;
  697. MainApp.prototype.Root_running_root_cd_behaviour = 4;
  698. MainApp.prototype.Root_running_stopped = 5;
  699. MainApp.prototype.Root_running_root_main_behaviour_initializing = 6;
  700. MainApp.prototype.Root_running_root_main_behaviour_running = 7;
  701. MainApp.prototype.Root_running_root_cd_behaviour_creating = 8;
  702. MainApp.prototype.Root_running_root_cd_behaviour_waiting = 9;
  703. MainApp.prototype.Root_running_root_cd_behaviour_check_nr_of_fields = 10;
  704. // Statechart enter/exit action method(s) :
  705. MainApp.prototype.enter_Root_running = function() {
  706. this.current_state[this.Root].push(this.Root_running);
  707. };
  708. MainApp.prototype.exit_Root_running = function() {
  709. if (this.current_state[this.Root_running].indexOf(this.Root_running_stopped) !== -1) {
  710. this.exit_Root_running_stopped();
  711. }
  712. if (this.current_state[this.Root_running].indexOf(this.Root_running_root) !== -1) {
  713. this.exit_Root_running_root();
  714. }
  715. this.current_state[this.Root] = new Array();
  716. };
  717. MainApp.prototype.enter_Root_running_root = function() {
  718. this.current_state[this.Root_running].push(this.Root_running_root);
  719. };
  720. MainApp.prototype.exit_Root_running_root = function() {
  721. this.exit_Root_running_root_main_behaviour();
  722. this.exit_Root_running_root_cd_behaviour();
  723. this.current_state[this.Root_running] = new Array();
  724. };
  725. MainApp.prototype.enter_Root_running_root_main_behaviour = function() {
  726. this.current_state[this.Root_running_root].push(this.Root_running_root_main_behaviour);
  727. };
  728. MainApp.prototype.exit_Root_running_root_main_behaviour = function() {
  729. if (this.current_state[this.Root_running_root_main_behaviour].indexOf(this.Root_running_root_main_behaviour_initializing) !== -1) {
  730. this.exit_Root_running_root_main_behaviour_initializing();
  731. }
  732. if (this.current_state[this.Root_running_root_main_behaviour].indexOf(this.Root_running_root_main_behaviour_running) !== -1) {
  733. this.exit_Root_running_root_main_behaviour_running();
  734. }
  735. this.current_state[this.Root_running_root] = new Array();
  736. };
  737. MainApp.prototype.enter_Root_running_root_cd_behaviour = function() {
  738. this.current_state[this.Root_running_root].push(this.Root_running_root_cd_behaviour);
  739. };
  740. MainApp.prototype.exit_Root_running_root_cd_behaviour = function() {
  741. if (this.current_state[this.Root_running_root_cd_behaviour].indexOf(this.Root_running_root_cd_behaviour_creating) !== -1) {
  742. this.exit_Root_running_root_cd_behaviour_creating();
  743. }
  744. if (this.current_state[this.Root_running_root_cd_behaviour].indexOf(this.Root_running_root_cd_behaviour_waiting) !== -1) {
  745. this.exit_Root_running_root_cd_behaviour_waiting();
  746. }
  747. if (this.current_state[this.Root_running_root_cd_behaviour].indexOf(this.Root_running_root_cd_behaviour_check_nr_of_fields) !== -1) {
  748. this.exit_Root_running_root_cd_behaviour_check_nr_of_fields();
  749. }
  750. this.current_state[this.Root_running_root] = new Array();
  751. };
  752. MainApp.prototype.enter_Root_running_stopped = function() {
  753. this.current_state[this.Root_running].push(this.Root_running_stopped);
  754. };
  755. MainApp.prototype.exit_Root_running_stopped = function() {
  756. this.current_state[this.Root_running] = new Array();
  757. };
  758. MainApp.prototype.enter_Root_running_root_main_behaviour_initializing = function() {
  759. this.current_state[this.Root_running_root_main_behaviour].push(this.Root_running_root_main_behaviour_initializing);
  760. };
  761. MainApp.prototype.exit_Root_running_root_main_behaviour_initializing = function() {
  762. this.current_state[this.Root_running_root_main_behaviour] = new Array();
  763. };
  764. MainApp.prototype.enter_Root_running_root_main_behaviour_running = function() {
  765. this.current_state[this.Root_running_root_main_behaviour].push(this.Root_running_root_main_behaviour_running);
  766. };
  767. MainApp.prototype.exit_Root_running_root_main_behaviour_running = function() {
  768. this.current_state[this.Root_running_root_main_behaviour] = new Array();
  769. };
  770. MainApp.prototype.enter_Root_running_root_cd_behaviour_creating = function() {
  771. this.current_state[this.Root_running_root_cd_behaviour].push(this.Root_running_root_cd_behaviour_creating);
  772. };
  773. MainApp.prototype.exit_Root_running_root_cd_behaviour_creating = function() {
  774. this.current_state[this.Root_running_root_cd_behaviour] = new Array();
  775. };
  776. MainApp.prototype.enter_Root_running_root_cd_behaviour_waiting = function() {
  777. this.current_state[this.Root_running_root_cd_behaviour].push(this.Root_running_root_cd_behaviour_waiting);
  778. };
  779. MainApp.prototype.exit_Root_running_root_cd_behaviour_waiting = function() {
  780. this.current_state[this.Root_running_root_cd_behaviour] = new Array();
  781. };
  782. MainApp.prototype.enter_Root_running_root_cd_behaviour_check_nr_of_fields = function() {
  783. this.current_state[this.Root_running_root_cd_behaviour].push(this.Root_running_root_cd_behaviour_check_nr_of_fields);
  784. };
  785. MainApp.prototype.exit_Root_running_root_cd_behaviour_check_nr_of_fields = function() {
  786. this.current_state[this.Root_running_root_cd_behaviour] = new Array();
  787. };
  788. // Statechart enter/exit default method(s) :
  789. MainApp.prototype.enterDefault_Root_running = function() {
  790. this.enter_Root_running();
  791. this.enterDefault_Root_running_root();
  792. };
  793. MainApp.prototype.enterDefault_Root_running_root = function() {
  794. this.enter_Root_running_root();
  795. this.enterDefault_Root_running_root_main_behaviour();
  796. this.enterDefault_Root_running_root_cd_behaviour();
  797. };
  798. MainApp.prototype.enterDefault_Root_running_root_main_behaviour = function() {
  799. this.enter_Root_running_root_main_behaviour();
  800. this.enter_Root_running_root_main_behaviour_initializing();
  801. };
  802. MainApp.prototype.enterDefault_Root_running_root_cd_behaviour = function() {
  803. this.enter_Root_running_root_cd_behaviour();
  804. this.enter_Root_running_root_cd_behaviour_waiting();
  805. };
  806. // Statechart transitions :
  807. MainApp.prototype.transition_Root = function(event) {
  808. var catched = false;
  809. if (!catched) {
  810. if (this.current_state[this.Root][0] === this.Root_running) {
  811. catched = this.transition_Root_running(event);
  812. }
  813. }
  814. return catched;
  815. };
  816. MainApp.prototype.transition_Root_running = function(event) {
  817. var catched = false;
  818. if (!catched) {
  819. if (this.current_state[this.Root_running][0] === this.Root_running_stopped) {
  820. catched = this.transition_Root_running_stopped(event);
  821. }
  822. else if (this.current_state[this.Root_running][0] === this.Root_running_root) {
  823. catched = this.transition_Root_running_root(event);
  824. }
  825. }
  826. return catched;
  827. };
  828. MainApp.prototype.transition_Root_running_stopped = function(event) {
  829. var catched = false;
  830. return catched;
  831. };
  832. MainApp.prototype.transition_Root_running_root = function(event) {
  833. var catched = false;
  834. if (!catched) {
  835. catched = this.transition_Root_running_root_main_behaviour(event) || catched
  836. catched = this.transition_Root_running_root_cd_behaviour(event) || catched
  837. }
  838. return catched;
  839. };
  840. MainApp.prototype.transition_Root_running_root_main_behaviour = function(event) {
  841. var catched = false;
  842. if (!catched) {
  843. if (this.current_state[this.Root_running_root_main_behaviour][0] === this.Root_running_root_main_behaviour_initializing) {
  844. catched = this.transition_Root_running_root_main_behaviour_initializing(event);
  845. }
  846. else if (this.current_state[this.Root_running_root_main_behaviour][0] === this.Root_running_root_main_behaviour_running) {
  847. catched = this.transition_Root_running_root_main_behaviour_running(event);
  848. }
  849. }
  850. return catched;
  851. };
  852. MainApp.prototype.transition_Root_running_root_main_behaviour_initializing = function(event) {
  853. var catched = false;
  854. var enableds = new Array();
  855. enableds.push(1);
  856. if (enableds.length > 1) {
  857. console.log("Runtime warning : indeterminism detected in a transition from node Root_running_root_main_behaviour_initializing. Only the first in document order enabled transition is executed.")
  858. }
  859. if (enableds.length > 0) {
  860. var enabled = enableds[0];
  861. if (enabled === 1) {
  862. this.exit_Root_running_root_main_behaviour_initializing();
  863. this.addEvent(new Event("create_field", null, []));
  864. this.enter_Root_running_root_main_behaviour_running();
  865. }
  866. catched = true;
  867. }
  868. return catched;
  869. };
  870. MainApp.prototype.transition_Root_running_root_main_behaviour_running = function(event) {
  871. var catched = false;
  872. var enableds = new Array();
  873. if (event.name === "button_pressed") {
  874. var parameters = event.parameters;
  875. var event_name = parameters[0];
  876. if (event_name == 'create_new_field') {
  877. enableds.push(1);
  878. }
  879. }
  880. if (enableds.length > 1) {
  881. console.log("Runtime warning : indeterminism detected in a transition from node Root_running_root_main_behaviour_running. Only the first in document order enabled transition is executed.")
  882. }
  883. if (enableds.length > 0) {
  884. var enabled = enableds[0];
  885. if (enabled === 1) {
  886. var parameters = event.parameters;
  887. var event_name = parameters[0];
  888. this.exit_Root_running_root_main_behaviour_running();
  889. this.addEvent(new Event("create_field", null, []));
  890. this.enter_Root_running_root_main_behaviour_running();
  891. }
  892. catched = true;
  893. }
  894. return catched;
  895. };
  896. MainApp.prototype.transition_Root_running_root_cd_behaviour = function(event) {
  897. var catched = false;
  898. if (!catched) {
  899. if (this.current_state[this.Root_running_root_cd_behaviour][0] === this.Root_running_root_cd_behaviour_creating) {
  900. catched = this.transition_Root_running_root_cd_behaviour_creating(event);
  901. }
  902. else if (this.current_state[this.Root_running_root_cd_behaviour][0] === this.Root_running_root_cd_behaviour_waiting) {
  903. catched = this.transition_Root_running_root_cd_behaviour_waiting(event);
  904. }
  905. else if (this.current_state[this.Root_running_root_cd_behaviour][0] === this.Root_running_root_cd_behaviour_check_nr_of_fields) {
  906. catched = this.transition_Root_running_root_cd_behaviour_check_nr_of_fields(event);
  907. }
  908. }
  909. return catched;
  910. };
  911. MainApp.prototype.transition_Root_running_root_cd_behaviour_creating = function(event) {
  912. var catched = false;
  913. var enableds = new Array();
  914. if (event.name === "instance_created") {
  915. enableds.push(1);
  916. }
  917. if (enableds.length > 1) {
  918. console.log("Runtime warning : indeterminism detected in a transition from node Root_running_root_cd_behaviour_creating. Only the first in document order enabled transition is executed.")
  919. }
  920. if (enableds.length > 0) {
  921. var enabled = enableds[0];
  922. if (enabled === 1) {
  923. var parameters = event.parameters;
  924. var association_name = parameters[0];
  925. this.exit_Root_running_root_cd_behaviour_creating();
  926. this.object_manager.addEvent(new Event("start_instance", null, [this, association_name]));
  927. var send_event = new Event("set_association_name", null, [association_name]);
  928. this.object_manager.addEvent(new Event("narrow_cast", null, [this, association_name , send_event]));
  929. this.nr_of_fields += 1;
  930. this.enter_Root_running_root_cd_behaviour_waiting();
  931. }
  932. catched = true;
  933. }
  934. return catched;
  935. };
  936. MainApp.prototype.transition_Root_running_root_cd_behaviour_waiting = function(event) {
  937. var catched = false;
  938. var enableds = new Array();
  939. if (event.name === "create_field") {
  940. enableds.push(1);
  941. }
  942. if (event.name === "delete_field") {
  943. enableds.push(2);
  944. }
  945. if (enableds.length > 1) {
  946. console.log("Runtime warning : indeterminism detected in a transition from node Root_running_root_cd_behaviour_waiting. Only the first in document order enabled transition is executed.")
  947. }
  948. if (enableds.length > 0) {
  949. var enabled = enableds[0];
  950. if (enabled === 1) {
  951. this.exit_Root_running_root_cd_behaviour_waiting();
  952. this.object_manager.addEvent(new Event("create_instance", null, [this, 'fields']));
  953. this.enter_Root_running_root_cd_behaviour_creating();
  954. }
  955. else if (enabled === 2) {
  956. var parameters = event.parameters;
  957. var association_name = parameters[0];
  958. this.exit_Root_running_root_cd_behaviour_waiting();
  959. this.object_manager.addEvent(new Event("delete_instance", null, [this, association_name]));
  960. this.nr_of_fields -= 1;
  961. this.enter_Root_running_root_cd_behaviour_check_nr_of_fields();
  962. }
  963. catched = true;
  964. }
  965. return catched;
  966. };
  967. MainApp.prototype.transition_Root_running_root_cd_behaviour_check_nr_of_fields = function(event) {
  968. var catched = false;
  969. var enableds = new Array();
  970. if (this.nr_of_fields != 0) {
  971. enableds.push(1);
  972. }
  973. if (this.nr_of_fields == 0) {
  974. enableds.push(2);
  975. }
  976. if (enableds.length > 1) {
  977. console.log("Runtime warning : indeterminism detected in a transition from node Root_running_root_cd_behaviour_check_nr_of_fields. Only the first in document order enabled transition is executed.")
  978. }
  979. if (enableds.length > 0) {
  980. var enabled = enableds[0];
  981. if (enabled === 1) {
  982. this.exit_Root_running_root_cd_behaviour_check_nr_of_fields();
  983. this.enter_Root_running_root_cd_behaviour_waiting();
  984. }
  985. else if (enabled === 2) {
  986. this.exit_Root_running_root();
  987. ui.close_window(ui.window);
  988. this.enter_Root_running_stopped();
  989. }
  990. catched = true;
  991. }
  992. return catched;
  993. };
  994. // Execute transitions
  995. MainApp.prototype.transition = function(event) {
  996. if (!event) event = new Event();
  997. this.state_changed = this.transition_Root(event);
  998. };
  999. // inState method for statechart
  1000. MainApp.prototype.inState = function(nodes) {
  1001. for (var c in this.current_state) {
  1002. if (!this.current_state.hasOwnProperty(c)) continue;
  1003. var new_nodes = new Array();
  1004. for (var n in nodes) {
  1005. if (!nodes.hasOwnProperty(n)) continue;
  1006. if (this.current_state[c].indexOf(nodes[n]) === -1) {
  1007. new_nodes.push(nodes[n]);
  1008. }
  1009. }
  1010. nodes = new_nodes;
  1011. if (nodes.length === 0) {
  1012. return true;
  1013. }
  1014. }
  1015. return false;
  1016. };
  1017. MainApp.prototype.commonConstructor = function(controller) {
  1018. if (!controller) controller = null;
  1019. // Constructor part that is common for all constructors.
  1020. RuntimeClassBase.call(this);
  1021. // User defined input ports
  1022. this.inports = new Object();
  1023. this.controller = controller;
  1024. this.object_manager = (controller == null ? null : controller.object_manager);
  1025. this.current_state = new Object();
  1026. this.history_state = new Object();
  1027. // Initialize statechart
  1028. this.current_state[this.Root] = new Array();
  1029. this.current_state[this.Root_running] = new Array();
  1030. this.current_state[this.Root_running_root] = new Array();
  1031. this.current_state[this.Root_running_root_main_behaviour] = new Array();
  1032. this.current_state[this.Root_running_root_cd_behaviour] = new Array();
  1033. };
  1034. MainApp.prototype.start = function() {
  1035. RuntimeClassBase.prototype.start.call(this);
  1036. this.enterDefault_Root_running();
  1037. };
  1038. // put class in global diagram object
  1039. Bouncing_Balls.MainApp = MainApp;
  1040. // The actual constructor
  1041. var Ball = function(controller, canvas, x, y, field_window) {
  1042. Ball.prototype.commonConstructor.call(this,controller);
  1043. // constructor body (user-defined)
  1044. this.canvas = canvas;
  1045. this.field_window = field_window;
  1046. this.r = 20.0;
  1047. this.vel = {'x':utils.random() * 2.0 - 1.0, 'y':utils.random() * 2.0 - 1.0};
  1048. this.mouse_pos = {'':''};
  1049. this.smooth = 0.4;
  1050. var circle = this.canvas.add_circle(x, y, this.r, {'fill':'#000'});
  1051. ui.bind_event(circle, ui.EVENTS.MOUSE_PRESS, this.controller, 'mouse_press', this.inports['ball_ui']);
  1052. ui.bind_event(circle, ui.EVENTS.MOUSE_RIGHT_CLICK, this.controller, 'right_click');
  1053. this.element = circle;
  1054. };
  1055. Ball.prototype = new RuntimeClassBase();
  1056. // Unique IDs for all statechart nodes
  1057. Ball.prototype.Root = 0;
  1058. Ball.prototype.Root_main_behaviour = 1;
  1059. Ball.prototype.Root_main_behaviour_dragging = 2;
  1060. Ball.prototype.Root_main_behaviour_selected = 3;
  1061. Ball.prototype.Root_main_behaviour_initializing = 4;
  1062. Ball.prototype.Root_main_behaviour_bouncing = 5;
  1063. Ball.prototype.Root_deleted = 6;
  1064. // User defined destructor
  1065. Ball.prototype.destructor = function() {
  1066. this.canvas.remove_element(this.element);
  1067. };
  1068. // Statechart enter/exit action method(s) :
  1069. Ball.prototype.enter_Root_main_behaviour = function() {
  1070. this.current_state[this.Root].push(this.Root_main_behaviour);
  1071. };
  1072. Ball.prototype.exit_Root_main_behaviour = function() {
  1073. if (this.current_state[this.Root_main_behaviour].indexOf(this.Root_main_behaviour_dragging) !== -1) {
  1074. this.exit_Root_main_behaviour_dragging();
  1075. }
  1076. if (this.current_state[this.Root_main_behaviour].indexOf(this.Root_main_behaviour_selected) !== -1) {
  1077. this.exit_Root_main_behaviour_selected();
  1078. }
  1079. if (this.current_state[this.Root_main_behaviour].indexOf(this.Root_main_behaviour_initializing) !== -1) {
  1080. this.exit_Root_main_behaviour_initializing();
  1081. }
  1082. if (this.current_state[this.Root_main_behaviour].indexOf(this.Root_main_behaviour_bouncing) !== -1) {
  1083. this.exit_Root_main_behaviour_bouncing();
  1084. }
  1085. this.current_state[this.Root] = new Array();
  1086. };
  1087. Ball.prototype.enter_Root_main_behaviour_dragging = function() {
  1088. this.current_state[this.Root_main_behaviour].push(this.Root_main_behaviour_dragging);
  1089. };
  1090. Ball.prototype.exit_Root_main_behaviour_dragging = function() {
  1091. this.current_state[this.Root_main_behaviour] = new Array();
  1092. };
  1093. Ball.prototype.enter_Root_main_behaviour_selected = function() {
  1094. this.current_state[this.Root_main_behaviour].push(this.Root_main_behaviour_selected);
  1095. };
  1096. Ball.prototype.exit_Root_main_behaviour_selected = function() {
  1097. this.current_state[this.Root_main_behaviour] = new Array();
  1098. };
  1099. Ball.prototype.enter_Root_main_behaviour_initializing = function() {
  1100. this.current_state[this.Root_main_behaviour].push(this.Root_main_behaviour_initializing);
  1101. };
  1102. Ball.prototype.exit_Root_main_behaviour_initializing = function() {
  1103. this.current_state[this.Root_main_behaviour] = new Array();
  1104. };
  1105. Ball.prototype.enter_Root_main_behaviour_bouncing = function() {
  1106. this.timers[0] = 0.01 * 1000.0; /* convert ms to s */
  1107. this.current_state[this.Root_main_behaviour].push(this.Root_main_behaviour_bouncing);
  1108. };
  1109. Ball.prototype.exit_Root_main_behaviour_bouncing = function() {
  1110. delete this.timers[0];
  1111. this.current_state[this.Root_main_behaviour] = new Array();
  1112. };
  1113. Ball.prototype.enter_Root_deleted = function() {
  1114. this.current_state[this.Root].push(this.Root_deleted);
  1115. };
  1116. Ball.prototype.exit_Root_deleted = function() {
  1117. this.current_state[this.Root] = new Array();
  1118. };
  1119. // Statechart enter/exit default method(s) :
  1120. Ball.prototype.enterDefault_Root_main_behaviour = function() {
  1121. this.enter_Root_main_behaviour();
  1122. this.enter_Root_main_behaviour_initializing();
  1123. };
  1124. // Statechart transitions :
  1125. Ball.prototype.transition_Root = function(event) {
  1126. var catched = false;
  1127. if (!catched) {
  1128. if (this.current_state[this.Root][0] === this.Root_main_behaviour) {
  1129. catched = this.transition_Root_main_behaviour(event);
  1130. }
  1131. else if (this.current_state[this.Root][0] === this.Root_deleted) {
  1132. catched = this.transition_Root_deleted(event);
  1133. }
  1134. }
  1135. return catched;
  1136. };
  1137. Ball.prototype.transition_Root_main_behaviour = function(event) {
  1138. var catched = false;
  1139. var enableds = new Array();
  1140. if (event.name === "delete_self") {
  1141. enableds.push(1);
  1142. }
  1143. if (enableds.length > 1) {
  1144. console.log("Runtime warning : indeterminism detected in a transition from node Root_main_behaviour. Only the first in document order enabled transition is executed.")
  1145. }
  1146. if (enableds.length > 0) {
  1147. var enabled = enableds[0];
  1148. if (enabled === 1) {
  1149. this.exit_Root_main_behaviour();
  1150. var send_event = new Event("delete_ball", null, [this.association_name]);
  1151. this.object_manager.addEvent(new Event("narrow_cast", null, [this, 'parent' , send_event]));
  1152. this.enter_Root_deleted();
  1153. }
  1154. catched = true;
  1155. }
  1156. if (!catched) {
  1157. if (this.current_state[this.Root_main_behaviour][0] === this.Root_main_behaviour_dragging) {
  1158. catched = this.transition_Root_main_behaviour_dragging(event);
  1159. }
  1160. else if (this.current_state[this.Root_main_behaviour][0] === this.Root_main_behaviour_selected) {
  1161. catched = this.transition_Root_main_behaviour_selected(event);
  1162. }
  1163. else if (this.current_state[this.Root_main_behaviour][0] === this.Root_main_behaviour_initializing) {
  1164. catched = this.transition_Root_main_behaviour_initializing(event);
  1165. }
  1166. else if (this.current_state[this.Root_main_behaviour][0] === this.Root_main_behaviour_bouncing) {
  1167. catched = this.transition_Root_main_behaviour_bouncing(event);
  1168. }
  1169. }
  1170. return catched;
  1171. };
  1172. Ball.prototype.transition_Root_main_behaviour_dragging = function(event) {
  1173. var catched = false;
  1174. var enableds = new Array();
  1175. if (event.name === "mouse_release" && event.port === "ui") {
  1176. enableds.push(1);
  1177. }
  1178. if (event.name === "mouse_move" && event.port === "ui") {
  1179. enableds.push(2);
  1180. }
  1181. if (enableds.length > 1) {
  1182. console.log("Runtime warning : indeterminism detected in a transition from node Root_main_behaviour_dragging. Only the first in document order enabled transition is executed.")
  1183. }
  1184. if (enableds.length > 0) {
  1185. var enabled = enableds[0];
  1186. if (enabled === 1) {
  1187. var parameters = event.parameters;
  1188. var x = parameters[0];
  1189. var y = parameters[1];
  1190. this.exit_Root_main_behaviour_dragging();
  1191. this.element.set_color('#f00');
  1192. this.enter_Root_main_behaviour_bouncing();
  1193. }
  1194. else if (enabled === 2) {
  1195. var parameters = event.parameters;
  1196. var x = parameters[0];
  1197. var y = parameters[1];
  1198. var button = parameters[2];
  1199. this.exit_Root_main_behaviour_dragging();
  1200. var dx = x - this.mouse_pos['x'];
  1201. var dy = y - this.mouse_pos['y'];
  1202. this.element.move(dx, dy);
  1203. var pos = this.element.get_position();
  1204. if(pos.x - this.r <= 0) {
  1205. pos.x = this.r + 1;
  1206. } else {
  1207. if(pos.x + this.r >= this.canvas.width) {
  1208. pos.x = this.canvas.width - this.r - 1;
  1209. }
  1210. }
  1211. if(pos.y - this.r <= 0) {
  1212. pos.y = this.r + 1;
  1213. } else {
  1214. if(pos.y + this.r >= this.canvas.height) {
  1215. pos.y = this.canvas.height - this.r - 1;
  1216. }
  1217. }
  1218. this.element.set_position(pos.x, pos.y);
  1219. this.mouse_pos = {'x':x, 'y':y};
  1220. this.vel = {'x':(1 - this.smooth) * dx + this.smooth * this.vel['x'], 'y':(1 - this.smooth) * dy + this.smooth * this.vel['y']};
  1221. this.enter_Root_main_behaviour_dragging();
  1222. }
  1223. catched = true;
  1224. }
  1225. return catched;
  1226. };
  1227. Ball.prototype.transition_Root_main_behaviour_selected = function(event) {
  1228. var catched = false;
  1229. var enableds = new Array();
  1230. if (event.name === "mouse_press" && event.port === "ball_ui") {
  1231. var parameters = event.parameters;
  1232. var x = parameters[0];
  1233. var y = parameters[1];
  1234. var button = parameters[2];
  1235. if (button == ui.MOUSE_BUTTONS.LEFT) {
  1236. enableds.push(1);
  1237. }
  1238. }
  1239. if (event.name === "key_press" && event.port === "ui") {
  1240. var parameters = event.parameters;
  1241. var key = parameters[0];
  1242. var active_window = parameters[1];
  1243. if (key == ui.KEYCODES.DELETE && active_window == this.field_window) {
  1244. enableds.push(2);
  1245. }
  1246. }
  1247. if (enableds.length > 1) {
  1248. console.log("Runtime warning : indeterminism detected in a transition from node Root_main_behaviour_selected. Only the first in document order enabled transition is executed.")
  1249. }
  1250. if (enableds.length > 0) {
  1251. var enabled = enableds[0];
  1252. if (enabled === 1) {
  1253. var parameters = event.parameters;
  1254. var x = parameters[0];
  1255. var y = parameters[1];
  1256. var button = parameters[2];
  1257. this.exit_Root_main_behaviour_selected();
  1258. this.mouse_pos = {'x':x, 'y':y};
  1259. this.enter_Root_main_behaviour_dragging();
  1260. }
  1261. else if (enabled === 2) {
  1262. var parameters = event.parameters;
  1263. var key = parameters[0];
  1264. var active_window = parameters[1];
  1265. this.exit_Root_main_behaviour_selected();
  1266. this.addEvent(new Event("delete_self", null, []));
  1267. this.enter_Root_main_behaviour_selected();
  1268. }
  1269. catched = true;
  1270. }
  1271. return catched;
  1272. };
  1273. Ball.prototype.transition_Root_main_behaviour_initializing = function(event) {
  1274. var catched = false;
  1275. var enableds = new Array();
  1276. if (event.name === "set_association_name") {
  1277. enableds.push(1);
  1278. }
  1279. if (enableds.length > 1) {
  1280. console.log("Runtime warning : indeterminism detected in a transition from node Root_main_behaviour_initializing. Only the first in document order enabled transition is executed.")
  1281. }
  1282. if (enableds.length > 0) {
  1283. var enabled = enableds[0];
  1284. if (enabled === 1) {
  1285. var parameters = event.parameters;
  1286. var association_name = parameters[0];
  1287. this.exit_Root_main_behaviour_initializing();
  1288. this.association_name = association_name;
  1289. this.enter_Root_main_behaviour_bouncing();
  1290. }
  1291. catched = true;
  1292. }
  1293. return catched;
  1294. };
  1295. Ball.prototype.transition_Root_main_behaviour_bouncing = function(event) {
  1296. var catched = false;
  1297. var enableds = new Array();
  1298. if (event.name === "_0after") {
  1299. enableds.push(1);
  1300. }
  1301. if (event.name === "mouse_press" && event.port === "ball_ui") {
  1302. var parameters = event.parameters;
  1303. var x = parameters[0];
  1304. var y = parameters[1];
  1305. var button = parameters[2];
  1306. if (button == ui.MOUSE_BUTTONS.LEFT) {
  1307. enableds.push(2);
  1308. }
  1309. }
  1310. if (enableds.length > 1) {
  1311. console.log("Runtime warning : indeterminism detected in a transition from node Root_main_behaviour_bouncing. Only the first in document order enabled transition is executed.")
  1312. }
  1313. if (enableds.length > 0) {
  1314. var enabled = enableds[0];
  1315. if (enabled === 1) {
  1316. this.exit_Root_main_behaviour_bouncing();
  1317. var pos = this.element.get_position();
  1318. if(pos.x - this.r <= 0 || pos.x + this.r >= this.canvas.width) {
  1319. this.vel['x'] = -this.vel['x'];
  1320. }
  1321. if(pos.y - this.r <= 0 || pos.y + this.r >= this.canvas.height) {
  1322. this.vel['y'] = -this.vel['y'];
  1323. }
  1324. this.element.move(this.vel['x'], this.vel['y']);
  1325. this.enter_Root_main_behaviour_bouncing();
  1326. }
  1327. else if (enabled === 2) {
  1328. var parameters = event.parameters;
  1329. var x = parameters[0];
  1330. var y = parameters[1];
  1331. var button = parameters[2];
  1332. this.exit_Root_main_behaviour_bouncing();
  1333. this.element.set_color('#ff0');
  1334. this.enter_Root_main_behaviour_selected();
  1335. }
  1336. catched = true;
  1337. }
  1338. return catched;
  1339. };
  1340. Ball.prototype.transition_Root_deleted = function(event) {
  1341. var catched = false;
  1342. return catched;
  1343. };
  1344. // Execute transitions
  1345. Ball.prototype.transition = function(event) {
  1346. if (!event) event = new Event();
  1347. this.state_changed = this.transition_Root(event);
  1348. };
  1349. // inState method for statechart
  1350. Ball.prototype.inState = function(nodes) {
  1351. for (var c in this.current_state) {
  1352. if (!this.current_state.hasOwnProperty(c)) continue;
  1353. var new_nodes = new Array();
  1354. for (var n in nodes) {
  1355. if (!nodes.hasOwnProperty(n)) continue;
  1356. if (this.current_state[c].indexOf(nodes[n]) === -1) {
  1357. new_nodes.push(nodes[n]);
  1358. }
  1359. }
  1360. nodes = new_nodes;
  1361. if (nodes.length === 0) {
  1362. return true;
  1363. }
  1364. }
  1365. return false;
  1366. };
  1367. Ball.prototype.commonConstructor = function(controller) {
  1368. if (!controller) controller = null;
  1369. // Constructor part that is common for all constructors.
  1370. RuntimeClassBase.call(this);
  1371. // User defined input ports
  1372. this.inports = new Object();
  1373. this.inports["ball_ui"] = controller.addInputPort("ball_ui", this);
  1374. // User defined attributes
  1375. this.field_window = null;
  1376. this.canvas = null;
  1377. this.element = null;
  1378. this.controller = controller;
  1379. this.object_manager = (controller == null ? null : controller.object_manager);
  1380. this.current_state = new Object();
  1381. this.history_state = new Object();
  1382. this.timers = new Object();
  1383. // Initialize statechart
  1384. this.current_state[this.Root] = new Array();
  1385. this.current_state[this.Root_main_behaviour] = new Array();
  1386. };
  1387. Ball.prototype.start = function() {
  1388. RuntimeClassBase.prototype.start.call(this);
  1389. this.enterDefault_Root_main_behaviour();
  1390. };
  1391. // put class in global diagram object
  1392. Bouncing_Balls.Ball = Ball;
  1393. var ObjectManager = function(controller) {
  1394. ObjectManagerBase.call(this, controller);
  1395. };
  1396. ObjectManager.prototype = new ObjectManagerBase();
  1397. ObjectManager.prototype.instantiate = function(class_name, construct_params) {
  1398. if (class_name === "Button") {
  1399. var instance = new Button(this.controller, construct_params[0], construct_params[1], construct_params[2]);
  1400. instance.associations = new Object();
  1401. instance.associations["parent"] = new Association("Field", 1, 1);
  1402. } else if (class_name === "Field") {
  1403. var instance = new Field(this.controller);
  1404. instance.associations = new Object();
  1405. instance.associations["parent"] = new Association("MainApp", 1, 1);
  1406. instance.associations["buttons"] = new Association("Button", 0, -1);
  1407. instance.associations["balls"] = new Association("Ball", 0, -1);
  1408. } else if (class_name === "MainApp") {
  1409. var instance = new MainApp(this.controller);
  1410. instance.associations = new Object();
  1411. instance.associations["fields"] = new Association("Field", 0, -1);
  1412. } else if (class_name === "Ball") {
  1413. var instance = new Ball(this.controller, construct_params[0], construct_params[1], construct_params[2], construct_params[3]);
  1414. instance.associations = new Object();
  1415. instance.associations["parent"] = new Association("Field", 1, 1);
  1416. }
  1417. return instance;
  1418. };
  1419. // put in global diagram object
  1420. Bouncing_Balls.ObjectManager = ObjectManager;
  1421. var Controller = function(keep_running, finished_callback) {
  1422. if (keep_running === undefined) keep_running = true;
  1423. JsEventLoopControllerBase.call(this, new ObjectManager(this), keep_running, finished_callback);
  1424. this.addInputPort("field_ui");
  1425. this.addInputPort("ball_ui");
  1426. this.addInputPort("input");
  1427. this.object_manager.createInstance("MainApp", []);
  1428. };
  1429. Controller.prototype = new JsEventLoopControllerBase();
  1430. // put in global diagram object
  1431. Bouncing_Balls.Controller = Controller;
  1432. })();