target.js 14 KB

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