target.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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:32:47 2016
  3. Model author: Raphael Mannadiar
  4. Model name: Traffic_Light_JavaScript_Version
  5. */
  6. // package "Traffic_Light_JavaScript_Version"
  7. var Traffic_Light_JavaScript_Version = {};
  8. (function() {
  9. var MainApp = function(controller) {
  10. RuntimeClassBase.call(this, controller);
  11. this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
  12. this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
  13. this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
  14. this.semantics.priority = StatechartSemantics.SourceParent;
  15. this.semantics.concurrency = StatechartSemantics.Single;
  16. // build Statechart structure
  17. this.build_statechart_structure();
  18. // call user defined constructor
  19. MainApp.prototype.user_defined_constructor.call(this);
  20. };
  21. MainApp.prototype = new Object();
  22. (function() {
  23. var proto = new RuntimeClassBase();
  24. for (prop in proto) {
  25. MainApp.prototype[prop] = proto[prop];
  26. }
  27. })();
  28. MainApp.prototype.user_defined_constructor = function() {
  29. this.canvas = ui.append_canvas(ui.window,100,310,{'background':'#eee'});
  30. var police_button = ui.append_button(ui.window, 'Police interrupt');
  31. var quit_button = ui.append_button(ui.window, 'Quit');
  32. ui.bind_event(police_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'police_interrupt_clicked');
  33. ui.bind_event(quit_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'quit_clicked');
  34. };
  35. MainApp.prototype.user_defined_destructor = function() {
  36. };
  37. // builds Statechart structure
  38. MainApp.prototype.build_statechart_structure = function() {
  39. // state <root>
  40. this.states[""] = new State(0, this);
  41. // state /initializing
  42. this.states["/initializing"] = new State(1, this);
  43. // state /creating
  44. this.states["/creating"] = new State(2, this);
  45. // state /initialized
  46. this.states["/initialized"] = new State(3, this);
  47. // add children
  48. this.states[""].addChild(this.states["/initializing"]);
  49. this.states[""].addChild(this.states["/creating"]);
  50. this.states[""].addChild(this.states["/initialized"]);
  51. this.states[""].fixTree();
  52. this.states[""].default_state = this.states["/initializing"];
  53. // transition /initializing
  54. var _initializing_0 = new Transition(this, this.states["/initializing"], [this.states["/creating"]]);
  55. _initializing_0.setAction(this._initializing_0_exec);
  56. this.states["/initializing"].addTransition(_initializing_0);
  57. // transition /creating
  58. var _creating_0 = new Transition(this, this.states["/creating"], [this.states["/initialized"]]);
  59. _creating_0.setAction(this._creating_0_exec);
  60. _creating_0.trigger = new Event("instance_created", null);
  61. this.states["/creating"].addTransition(_creating_0);
  62. };
  63. MainApp.prototype._initializing_0_exec = function(parameters) {
  64. this.big_step.outputEventOM(new Event("create_instance", null, [this, "trafficlight", "TrafficLight", this.canvas]));
  65. };
  66. MainApp.prototype._creating_0_exec = function(parameters) {
  67. var association_name = parameters[0];
  68. this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
  69. this.big_step.outputEventOM(new Event("narrow_cast", null, [this, association_name, new Event("set_association_name", null, [association_name])]));
  70. };
  71. MainApp.prototype.initializeStatechart = function() {
  72. // enter default state
  73. var states = this.states["/initializing"].getEffectiveTargetStates();
  74. this.updateConfiguration(states);
  75. for (var state_idx in states) {
  76. if (!states.hasOwnProperty(state_idx)) continue;
  77. var state = states[state_idx]
  78. if (state.enter) {
  79. state.enter();
  80. }
  81. }
  82. };
  83. // add symbol 'MainApp' to package 'Traffic_Light_JavaScript_Version'
  84. Traffic_Light_JavaScript_Version.MainApp = MainApp;
  85. var TrafficLight = function(controller, canvas) {
  86. RuntimeClassBase.call(this, controller);
  87. this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
  88. this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
  89. this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
  90. this.semantics.priority = StatechartSemantics.SourceParent;
  91. this.semantics.concurrency = StatechartSemantics.Single;
  92. // build Statechart structure
  93. this.build_statechart_structure();
  94. // call user defined constructor
  95. TrafficLight.prototype.user_defined_constructor.call(this, canvas);
  96. };
  97. TrafficLight.prototype = new Object();
  98. (function() {
  99. var proto = new RuntimeClassBase();
  100. for (prop in proto) {
  101. TrafficLight.prototype[prop] = proto[prop];
  102. }
  103. })();
  104. TrafficLight.prototype.user_defined_constructor = function(canvas) {
  105. var size = 100;
  106. var offset = size+5;
  107. this.RED = 0;
  108. this.YELLOW = 1;
  109. this.GREEN = 2;
  110. this.colors = ['#f00','#ff0','#0f0']
  111. this.lights = [canvas.add_rectangle(size/2, size/2, size, size, {'fill':'#000'}),
  112. canvas.add_rectangle(size/2, size/2+offset, size, size, {'fill':'#000'}),
  113. canvas.add_rectangle(size/2, size/2+2*offset, size, size, {'fill':'#000'})];
  114. };
  115. TrafficLight.prototype.user_defined_destructor = function() {
  116. };
  117. // user defined method
  118. TrafficLight.prototype.clear = function() {
  119. this.lights[this.RED].set_color('#000');
  120. this.lights[this.YELLOW].set_color('#000');
  121. this.lights[this.GREEN].set_color('#000');
  122. };
  123. // user defined method
  124. TrafficLight.prototype.setGreen = function() {
  125. this.clear();
  126. this.lights[this.GREEN].set_color(this.colors[this.GREEN]);
  127. };
  128. // user defined method
  129. TrafficLight.prototype.setYellow = function() {
  130. this.clear();
  131. this.lights[this.YELLOW].set_color(this.colors[this.YELLOW]);
  132. };
  133. // user defined method
  134. TrafficLight.prototype.setRed = function() {
  135. this.clear();
  136. this.lights[this.RED].set_color(this.colors[this.RED]);
  137. };
  138. // builds Statechart structure
  139. TrafficLight.prototype.build_statechart_structure = function() {
  140. // state <root>
  141. this.states[""] = new State(0, this);
  142. // state /on
  143. this.states["/on"] = new State(1, this);
  144. // state /on/normal
  145. this.states["/on/normal"] = new State(2, this);
  146. // state /on/normal/red
  147. this.states["/on/normal/red"] = new State(3, this);
  148. this.states["/on/normal/red"].setEnter(this._on_normal_red_enter);
  149. this.states["/on/normal/red"].setExit(this._on_normal_red_exit);
  150. // state /on/normal/green
  151. this.states["/on/normal/green"] = new State(4, this);
  152. this.states["/on/normal/green"].setEnter(this._on_normal_green_enter);
  153. this.states["/on/normal/green"].setExit(this._on_normal_green_exit);
  154. // state /on/normal/yellow
  155. this.states["/on/normal/yellow"] = new State(5, this);
  156. this.states["/on/normal/yellow"].setEnter(this._on_normal_yellow_enter);
  157. this.states["/on/normal/yellow"].setExit(this._on_normal_yellow_exit);
  158. // state /on/normal/history
  159. this.states["/on/normal/history"] = new ShallowHistoryState(6, this);
  160. // state /on/interrupted
  161. this.states["/on/interrupted"] = new State(7, this);
  162. // state /on/interrupted/yellow
  163. this.states["/on/interrupted/yellow"] = new State(8, this);
  164. this.states["/on/interrupted/yellow"].setEnter(this._on_interrupted_yellow_enter);
  165. this.states["/on/interrupted/yellow"].setExit(this._on_interrupted_yellow_exit);
  166. // state /on/interrupted/black
  167. this.states["/on/interrupted/black"] = new State(9, this);
  168. this.states["/on/interrupted/black"].setEnter(this._on_interrupted_black_enter);
  169. this.states["/on/interrupted/black"].setExit(this._on_interrupted_black_exit);
  170. // state /off
  171. this.states["/off"] = new State(10, this);
  172. this.states["/off"].setEnter(this._off_enter);
  173. // add children
  174. this.states[""].addChild(this.states["/on"]);
  175. this.states[""].addChild(this.states["/off"]);
  176. this.states["/on"].addChild(this.states["/on/normal"]);
  177. this.states["/on"].addChild(this.states["/on/interrupted"]);
  178. this.states["/on/normal"].addChild(this.states["/on/normal/red"]);
  179. this.states["/on/normal"].addChild(this.states["/on/normal/green"]);
  180. this.states["/on/normal"].addChild(this.states["/on/normal/yellow"]);
  181. this.states["/on/normal"].addChild(this.states["/on/normal/history"]);
  182. this.states["/on/interrupted"].addChild(this.states["/on/interrupted/yellow"]);
  183. this.states["/on/interrupted"].addChild(this.states["/on/interrupted/black"]);
  184. this.states[""].fixTree();
  185. this.states[""].default_state = this.states["/on"];
  186. this.states["/on"].default_state = this.states["/on/normal"];
  187. this.states["/on/normal"].default_state = this.states["/on/normal/red"];
  188. this.states["/on/interrupted"].default_state = this.states["/on/interrupted/yellow"];
  189. // transition /on/normal/red
  190. var _on_normal_red_0 = new Transition(this, this.states["/on/normal/red"], [this.states["/on/normal/green"]]);
  191. _on_normal_red_0.trigger = new Event("_0after");
  192. this.states["/on/normal/red"].addTransition(_on_normal_red_0);
  193. // transition /on/normal/green
  194. var _on_normal_green_0 = new Transition(this, this.states["/on/normal/green"], [this.states["/on/normal/yellow"]]);
  195. _on_normal_green_0.trigger = new Event("_1after");
  196. this.states["/on/normal/green"].addTransition(_on_normal_green_0);
  197. // transition /on/normal/yellow
  198. var _on_normal_yellow_0 = new Transition(this, this.states["/on/normal/yellow"], [this.states["/on/normal/red"]]);
  199. _on_normal_yellow_0.trigger = new Event("_2after");
  200. this.states["/on/normal/yellow"].addTransition(_on_normal_yellow_0);
  201. // transition /on/interrupted/yellow
  202. var _on_interrupted_yellow_0 = new Transition(this, this.states["/on/interrupted/yellow"], [this.states["/on/interrupted/black"]]);
  203. _on_interrupted_yellow_0.trigger = new Event("_3after");
  204. this.states["/on/interrupted/yellow"].addTransition(_on_interrupted_yellow_0);
  205. // transition /on/interrupted/black
  206. var _on_interrupted_black_0 = new Transition(this, this.states["/on/interrupted/black"], [this.states["/on/interrupted/yellow"]]);
  207. _on_interrupted_black_0.trigger = new Event("_4after");
  208. this.states["/on/interrupted/black"].addTransition(_on_interrupted_black_0);
  209. // transition /on
  210. var _on_0 = new Transition(this, this.states["/on"], [this.states["/off"]]);
  211. _on_0.trigger = new Event("quit_clicked", "ui");
  212. this.states["/on"].addTransition(_on_0);
  213. // transition /on/normal
  214. var _on_normal_0 = new Transition(this, this.states["/on/normal"], [this.states["/on/interrupted"]]);
  215. _on_normal_0.trigger = new Event("police_interrupt_clicked", "ui");
  216. this.states["/on/normal"].addTransition(_on_normal_0);
  217. // transition /on/interrupted
  218. var _on_interrupted_0 = new Transition(this, this.states["/on/interrupted"], [this.states["/on/normal/history"]]);
  219. _on_interrupted_0.trigger = new Event("police_interrupt_clicked", "ui");
  220. this.states["/on/interrupted"].addTransition(_on_interrupted_0);
  221. };
  222. TrafficLight.prototype._on_normal_red_enter = function() {
  223. this.addTimer(0, 3);
  224. this.setRed();
  225. };
  226. TrafficLight.prototype._on_normal_red_exit = function() {
  227. this.removeTimer(0);
  228. };
  229. TrafficLight.prototype._on_normal_green_enter = function() {
  230. this.addTimer(1, 2);
  231. this.setGreen();
  232. };
  233. TrafficLight.prototype._on_normal_green_exit = function() {
  234. this.removeTimer(1);
  235. };
  236. TrafficLight.prototype._on_normal_yellow_enter = function() {
  237. this.addTimer(2, 1);
  238. this.setYellow();
  239. };
  240. TrafficLight.prototype._on_normal_yellow_exit = function() {
  241. this.removeTimer(2);
  242. };
  243. TrafficLight.prototype._on_interrupted_yellow_enter = function() {
  244. this.addTimer(3, .5);
  245. this.setYellow();
  246. };
  247. TrafficLight.prototype._on_interrupted_yellow_exit = function() {
  248. this.removeTimer(3);
  249. };
  250. TrafficLight.prototype._on_interrupted_black_enter = function() {
  251. this.addTimer(4, .5);
  252. this.clear();
  253. };
  254. TrafficLight.prototype._on_interrupted_black_exit = function() {
  255. this.removeTimer(4);
  256. };
  257. TrafficLight.prototype._off_enter = function() {
  258. this.clear();
  259. };
  260. TrafficLight.prototype.initializeStatechart = function() {
  261. // enter default state
  262. var states = this.states["/on"].getEffectiveTargetStates();
  263. this.updateConfiguration(states);
  264. for (var state_idx in states) {
  265. if (!states.hasOwnProperty(state_idx)) continue;
  266. var state = states[state_idx]
  267. if (state.enter) {
  268. state.enter();
  269. }
  270. }
  271. };
  272. // add symbol 'TrafficLight' to package 'Traffic_Light_JavaScript_Version'
  273. Traffic_Light_JavaScript_Version.TrafficLight = TrafficLight;
  274. var ObjectManager = function(controller) {
  275. ObjectManagerBase.call(this, controller);
  276. };
  277. ObjectManager.prototype = new Object();
  278. (function() {
  279. var proto = new ObjectManagerBase();
  280. for (prop in proto) {
  281. ObjectManager.prototype[prop] = proto[prop];
  282. }
  283. })();
  284. ObjectManager.prototype.instantiate = function(class_name, construct_params) {
  285. if (class_name === "MainApp") {
  286. var instance = new MainApp(this.controller);
  287. instance.associations = new Object();
  288. instance.associations["trafficlight"] = new Association("TrafficLight", 0, -1);
  289. } else if (class_name === "TrafficLight") {
  290. var instance = new TrafficLight(this.controller, construct_params[0]);
  291. instance.associations = new Object();
  292. } else {
  293. throw new Error("Cannot instantiate class " + class_name);
  294. }
  295. return instance;
  296. };
  297. // add symbol 'ObjectManager' to package 'Traffic_Light_JavaScript_Version'
  298. Traffic_Light_JavaScript_Version.ObjectManager = ObjectManager;
  299. var Controller = function(event_loop_callbacks, finished_callback) {
  300. if (finished_callback === undefined) finished_callback = null;
  301. EventLoopControllerBase.call(this, new ObjectManager(this), event_loop_callbacks, finished_callback);
  302. this.addInputPort("ui");
  303. this.object_manager.createInstance("MainApp", new Array());
  304. };
  305. Controller.prototype = new Object();
  306. (function() {
  307. var proto = new EventLoopControllerBase();
  308. for (prop in proto) {
  309. Controller.prototype[prop] = proto[prop];
  310. }
  311. })();
  312. // add symbol 'Controller' to package 'Traffic_Light_JavaScript_Version'
  313. Traffic_Light_JavaScript_Version.Controller = Controller;
  314. })();