target.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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: Simon Van Mierlo
  4. Model name: Timer
  5. */
  6. // package "Timer"
  7. var Timer = {};
  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,400,150,{'background':'#eee'})
  30. this.clock_text = this.canvas.add_text(25,25,'0.0')
  31. this.actual_clock_text = this.canvas.add_text(25,50,'0.0')
  32. var interrupt_button = ui.append_button(ui.window, 'INTERRUPT');
  33. var continue_button = ui.append_button(ui.window, 'CONTINUE');
  34. ui.bind_event(interrupt_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'interrupt_clicked');
  35. ui.bind_event(continue_button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'continue_clicked');
  36. };
  37. MainApp.prototype.user_defined_destructor = function() {
  38. };
  39. // user defined method
  40. MainApp.prototype.update_timers = function() {
  41. this.clock_text.set_text(get_simulated_time().toFixed(2));
  42. this.actual_clock_text.set_text(time().toFixed(2));
  43. };
  44. // builds Statechart structure
  45. MainApp.prototype.build_statechart_structure = function() {
  46. // state <root>
  47. this.states[""] = new State(0, this);
  48. // state /running
  49. this.states["/running"] = new State(1, this);
  50. this.states["/running"].setEnter(this._running_enter);
  51. this.states["/running"].setExit(this._running_exit);
  52. // state /interrupted
  53. this.states["/interrupted"] = new State(2, this);
  54. this.states["/interrupted"].setEnter(this._interrupted_enter);
  55. this.states["/interrupted"].setExit(this._interrupted_exit);
  56. // add children
  57. this.states[""].addChild(this.states["/running"]);
  58. this.states[""].addChild(this.states["/interrupted"]);
  59. this.states[""].fixTree();
  60. this.states[""].default_state = this.states["/running"];
  61. // transition /running
  62. var _running_0 = new Transition(this, this.states["/running"], [this.states["/running"]]);
  63. _running_0.setAction(this._running_0_exec);
  64. _running_0.setTrigger(new Event("_0after"));
  65. this.states["/running"].addTransition(_running_0);
  66. var _running_1 = new Transition(this, this.states["/running"], [this.states["/interrupted"]]);
  67. _running_1.setAction(this._running_1_exec);
  68. _running_1.setTrigger(new Event("interrupt_clicked", "ui"));
  69. this.states["/running"].addTransition(_running_1);
  70. // transition /interrupted
  71. var _interrupted_0 = new Transition(this, this.states["/interrupted"], [this.states["/interrupted"]]);
  72. _interrupted_0.setAction(this._interrupted_0_exec);
  73. _interrupted_0.setTrigger(new Event("interrupt_clicked", "ui"));
  74. this.states["/interrupted"].addTransition(_interrupted_0);
  75. var _interrupted_1 = new Transition(this, this.states["/interrupted"], [this.states["/running"]]);
  76. _interrupted_1.setAction(this._interrupted_1_exec);
  77. _interrupted_1.setTrigger(new Event("continue_clicked", "ui"));
  78. this.states["/interrupted"].addTransition(_interrupted_1);
  79. };
  80. MainApp.prototype._running_enter = function() {
  81. this.addTimer(0, 0.05);
  82. };
  83. MainApp.prototype._running_exit = function() {
  84. this.removeTimer(0);
  85. };
  86. MainApp.prototype._interrupted_enter = function() {
  87. console.log("entering interrupted");
  88. };
  89. MainApp.prototype._interrupted_exit = function() {
  90. console.log("entering interrupted");
  91. };
  92. MainApp.prototype._running_0_exec = function(parameters) {
  93. this.update_timers();
  94. };
  95. MainApp.prototype._running_1_exec = function(parameters) {
  96. this.update_timers();
  97. };
  98. MainApp.prototype._interrupted_0_exec = function(parameters) {
  99. this.update_timers();
  100. };
  101. MainApp.prototype._interrupted_1_exec = function(parameters) {
  102. this.update_timers();
  103. };
  104. MainApp.prototype.initializeStatechart = function() {
  105. // enter default state
  106. var states = this.states["/running"].getEffectiveTargetStates();
  107. this.updateConfiguration(states);
  108. for (var state_idx in states) {
  109. if (!states.hasOwnProperty(state_idx)) continue;
  110. var state = states[state_idx]
  111. if (state.enter) {
  112. state.enter();
  113. }
  114. }
  115. };
  116. // add symbol 'MainApp' to package 'Timer'
  117. Timer.MainApp = MainApp;
  118. var ObjectManager = function(controller) {
  119. ObjectManagerBase.call(this, controller);
  120. };
  121. ObjectManager.prototype = new Object();
  122. (function() {
  123. var proto = new ObjectManagerBase();
  124. for (prop in proto) {
  125. ObjectManager.prototype[prop] = proto[prop];
  126. }
  127. })();
  128. ObjectManager.prototype.instantiate = function(class_name, construct_params) {
  129. if (class_name === "MainApp") {
  130. var instance = new MainApp(this.controller);
  131. instance.associations = new Object();
  132. } else {
  133. throw new Error("Cannot instantiate class " + class_name);
  134. }
  135. return instance;
  136. };
  137. // add symbol 'ObjectManager' to package 'Timer'
  138. Timer.ObjectManager = ObjectManager;
  139. var Controller = function(event_loop_callbacks, finished_callback, behind_schedule_callback) {
  140. if (finished_callback === undefined) finished_callback = null;
  141. if (behind_schedule_callback === undefined) behind_schedule_callback = null;
  142. EventLoopControllerBase.call(this, new ObjectManager(this), event_loop_callbacks, finished_callback, behind_schedule_callback);
  143. this.addInputPort("ui");
  144. this.object_manager.createInstance("MainApp", new Array());
  145. };
  146. Controller.prototype = new Object();
  147. (function() {
  148. var proto = new EventLoopControllerBase();
  149. for (prop in proto) {
  150. Controller.prototype[prop] = proto[prop];
  151. }
  152. })();
  153. // add symbol 'Controller' to package 'Timer'
  154. Timer.Controller = Controller;
  155. })();