target.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Fri Aug 05 16:08:37 2016
  4. Model author: Raphael Mannadiar
  5. Model name: Traffic_Light_Python_Version
  6. """
  7. from sccd.runtime.statecharts_core import *
  8. from sccd.runtime.libs.ui import ui
  9. # package "Traffic_Light_Python_Version"
  10. class MainApp(RuntimeClassBase):
  11. def __init__(self, controller):
  12. RuntimeClassBase.__init__(self, controller)
  13. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  14. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  15. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  16. self.semantics.priority = StatechartSemantics.SourceParent
  17. self.semantics.concurrency = StatechartSemantics.Single
  18. # build Statechart structure
  19. self.build_statechart_structure()
  20. # call user defined constructor
  21. MainApp.user_defined_constructor(self)
  22. def user_defined_constructor(self):
  23. self.canvas = ui.append_canvas(ui.window,100,310,{'background':'#eee'});
  24. police_button = ui.append_button(ui.window, 'Police interrupt');
  25. quit_button = ui.append_button(ui.window, 'Quit');
  26. ui.bind_event(police_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'police_interrupt_clicked');
  27. ui.bind_event(quit_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'quit_clicked');
  28. def user_defined_destructor(self):
  29. pass
  30. # builds Statechart structure
  31. def build_statechart_structure(self):
  32. # state <root>
  33. self.states[""] = State(0, self)
  34. # state /initializing
  35. self.states["/initializing"] = State(1, self)
  36. # state /creating
  37. self.states["/creating"] = State(2, self)
  38. # state /initialized
  39. self.states["/initialized"] = State(3, self)
  40. # add children
  41. self.states[""].addChild(self.states["/initializing"])
  42. self.states[""].addChild(self.states["/creating"])
  43. self.states[""].addChild(self.states["/initialized"])
  44. self.states[""].fixTree()
  45. self.states[""].default_state = self.states["/initializing"]
  46. # transition /initializing
  47. _initializing_0 = Transition(self, self.states["/initializing"], [self.states["/creating"]])
  48. _initializing_0.setAction(self._initializing_0_exec)
  49. _initializing_0.setTrigger(None)
  50. self.states["/initializing"].addTransition(_initializing_0)
  51. # transition /creating
  52. _creating_0 = Transition(self, self.states["/creating"], [self.states["/initialized"]])
  53. _creating_0.setAction(self._creating_0_exec)
  54. _creating_0.setTrigger(Event("instance_created", None))
  55. self.states["/creating"].addTransition(_creating_0)
  56. def _initializing_0_exec(self, parameters):
  57. self.big_step.outputEventOM(Event("create_instance", None, [self, "trafficlight", "TrafficLight", self.canvas]))
  58. def _creating_0_exec(self, parameters):
  59. association_name = parameters[0]
  60. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  61. self.big_step.outputEventOM(Event("narrow_cast", None, [self, association_name, Event("set_association_name", None, [association_name])]))
  62. def initializeStatechart(self):
  63. # enter default state
  64. states = self.states["/initializing"].getEffectiveTargetStates()
  65. self.updateConfiguration(states)
  66. for state in states:
  67. if state.enter:
  68. state.enter()
  69. class TrafficLight(RuntimeClassBase):
  70. def __init__(self, controller, canvas):
  71. RuntimeClassBase.__init__(self, controller)
  72. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  73. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  74. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  75. self.semantics.priority = StatechartSemantics.SourceParent
  76. self.semantics.concurrency = StatechartSemantics.Single
  77. # build Statechart structure
  78. self.build_statechart_structure()
  79. # call user defined constructor
  80. TrafficLight.user_defined_constructor(self, canvas)
  81. def user_defined_constructor(self, canvas):
  82. size = 100;
  83. offset = size+5;
  84. self.RED = 0;
  85. self.YELLOW = 1;
  86. self.GREEN = 2;
  87. self.colors = ['#f00','#ff0','#0f0']
  88. self.lights = [
  89. canvas.add_rectangle(size/2, size/2, size, size, {'fill':'#000'}),
  90. canvas.add_rectangle(size/2, size/2+offset, size, size, {'fill':'#000'}),
  91. canvas.add_rectangle(size/2, size/2+2*offset, size, size, {'fill':'#000'})];
  92. def user_defined_destructor(self):
  93. pass
  94. # user defined method
  95. def clear(self):
  96. self.lights[self.RED].set_color('#000');
  97. self.lights[self.YELLOW].set_color('#000');
  98. self.lights[self.GREEN].set_color('#000');
  99. # user defined method
  100. def setGreen(self):
  101. self.clear();
  102. self.lights[self.GREEN].set_color(self.colors[self.GREEN]);
  103. # user defined method
  104. def setYellow(self):
  105. self.clear();
  106. self.lights[self.YELLOW].set_color(self.colors[self.YELLOW]);
  107. # user defined method
  108. def setRed(self):
  109. self.clear();
  110. self.lights[self.RED].set_color(self.colors[self.RED]);
  111. # builds Statechart structure
  112. def build_statechart_structure(self):
  113. # state <root>
  114. self.states[""] = State(0, self)
  115. # state /on
  116. self.states["/on"] = State(1, self)
  117. # state /on/normal
  118. self.states["/on/normal"] = State(2, self)
  119. # state /on/normal/red
  120. self.states["/on/normal/red"] = State(3, self)
  121. self.states["/on/normal/red"].setEnter(self._on_normal_red_enter)
  122. self.states["/on/normal/red"].setExit(self._on_normal_red_exit)
  123. # state /on/normal/green
  124. self.states["/on/normal/green"] = State(4, self)
  125. self.states["/on/normal/green"].setEnter(self._on_normal_green_enter)
  126. self.states["/on/normal/green"].setExit(self._on_normal_green_exit)
  127. # state /on/normal/yellow
  128. self.states["/on/normal/yellow"] = State(5, self)
  129. self.states["/on/normal/yellow"].setEnter(self._on_normal_yellow_enter)
  130. self.states["/on/normal/yellow"].setExit(self._on_normal_yellow_exit)
  131. # state /on/normal/history
  132. self.states["/on/normal/history"] = ShallowHistoryState(6, self)
  133. # state /on/interrupted
  134. self.states["/on/interrupted"] = State(7, self)
  135. # state /on/interrupted/yellow
  136. self.states["/on/interrupted/yellow"] = State(8, self)
  137. self.states["/on/interrupted/yellow"].setEnter(self._on_interrupted_yellow_enter)
  138. self.states["/on/interrupted/yellow"].setExit(self._on_interrupted_yellow_exit)
  139. # state /on/interrupted/black
  140. self.states["/on/interrupted/black"] = State(9, self)
  141. self.states["/on/interrupted/black"].setEnter(self._on_interrupted_black_enter)
  142. self.states["/on/interrupted/black"].setExit(self._on_interrupted_black_exit)
  143. # state /off
  144. self.states["/off"] = State(10, self)
  145. self.states["/off"].setEnter(self._off_enter)
  146. # add children
  147. self.states[""].addChild(self.states["/on"])
  148. self.states[""].addChild(self.states["/off"])
  149. self.states["/on"].addChild(self.states["/on/normal"])
  150. self.states["/on"].addChild(self.states["/on/interrupted"])
  151. self.states["/on/normal"].addChild(self.states["/on/normal/red"])
  152. self.states["/on/normal"].addChild(self.states["/on/normal/green"])
  153. self.states["/on/normal"].addChild(self.states["/on/normal/yellow"])
  154. self.states["/on/normal"].addChild(self.states["/on/normal/history"])
  155. self.states["/on/interrupted"].addChild(self.states["/on/interrupted/yellow"])
  156. self.states["/on/interrupted"].addChild(self.states["/on/interrupted/black"])
  157. self.states[""].fixTree()
  158. self.states[""].default_state = self.states["/on"]
  159. self.states["/on"].default_state = self.states["/on/normal"]
  160. self.states["/on/normal"].default_state = self.states["/on/normal/red"]
  161. self.states["/on/interrupted"].default_state = self.states["/on/interrupted/yellow"]
  162. # transition /on/normal/red
  163. _on_normal_red_0 = Transition(self, self.states["/on/normal/red"], [self.states["/on/normal/green"]])
  164. _on_normal_red_0.setTrigger(Event("_0after"))
  165. self.states["/on/normal/red"].addTransition(_on_normal_red_0)
  166. # transition /on/normal/green
  167. _on_normal_green_0 = Transition(self, self.states["/on/normal/green"], [self.states["/on/normal/yellow"]])
  168. _on_normal_green_0.setTrigger(Event("_1after"))
  169. self.states["/on/normal/green"].addTransition(_on_normal_green_0)
  170. # transition /on/normal/yellow
  171. _on_normal_yellow_0 = Transition(self, self.states["/on/normal/yellow"], [self.states["/on/normal/red"]])
  172. _on_normal_yellow_0.setTrigger(Event("_2after"))
  173. self.states["/on/normal/yellow"].addTransition(_on_normal_yellow_0)
  174. # transition /on/interrupted/yellow
  175. _on_interrupted_yellow_0 = Transition(self, self.states["/on/interrupted/yellow"], [self.states["/on/interrupted/black"]])
  176. _on_interrupted_yellow_0.setTrigger(Event("_3after"))
  177. self.states["/on/interrupted/yellow"].addTransition(_on_interrupted_yellow_0)
  178. # transition /on/interrupted/black
  179. _on_interrupted_black_0 = Transition(self, self.states["/on/interrupted/black"], [self.states["/on/interrupted/yellow"]])
  180. _on_interrupted_black_0.setTrigger(Event("_4after"))
  181. self.states["/on/interrupted/black"].addTransition(_on_interrupted_black_0)
  182. # transition /on
  183. _on_0 = Transition(self, self.states["/on"], [self.states["/off"]])
  184. _on_0.setTrigger(Event("quit_clicked", "ui"))
  185. self.states["/on"].addTransition(_on_0)
  186. # transition /on/normal
  187. _on_normal_0 = Transition(self, self.states["/on/normal"], [self.states["/on/interrupted"]])
  188. _on_normal_0.setTrigger(Event("police_interrupt_clicked", "ui"))
  189. self.states["/on/normal"].addTransition(_on_normal_0)
  190. # transition /on/interrupted
  191. _on_interrupted_0 = Transition(self, self.states["/on/interrupted"], [self.states["/on/normal/history"]])
  192. _on_interrupted_0.setTrigger(Event("police_interrupt_clicked", "ui"))
  193. self.states["/on/interrupted"].addTransition(_on_interrupted_0)
  194. def _on_normal_red_enter(self):
  195. self.addTimer(0, 3)
  196. self.setRed();
  197. def _on_normal_red_exit(self):
  198. self.removeTimer(0)
  199. def _on_normal_green_enter(self):
  200. self.addTimer(1, 2)
  201. self.setGreen();
  202. def _on_normal_green_exit(self):
  203. self.removeTimer(1)
  204. def _on_normal_yellow_enter(self):
  205. self.addTimer(2, 1)
  206. self.setYellow();
  207. def _on_normal_yellow_exit(self):
  208. self.removeTimer(2)
  209. def _on_interrupted_yellow_enter(self):
  210. self.addTimer(3, .5)
  211. self.setYellow();
  212. def _on_interrupted_yellow_exit(self):
  213. self.removeTimer(3)
  214. def _on_interrupted_black_enter(self):
  215. self.addTimer(4, .5)
  216. self.clear();
  217. def _on_interrupted_black_exit(self):
  218. self.removeTimer(4)
  219. def _off_enter(self):
  220. self.clear();
  221. def initializeStatechart(self):
  222. # enter default state
  223. states = self.states["/on"].getEffectiveTargetStates()
  224. self.updateConfiguration(states)
  225. for state in states:
  226. if state.enter:
  227. state.enter()
  228. class ObjectManager(ObjectManagerBase):
  229. def __init__(self, controller):
  230. ObjectManagerBase.__init__(self, controller)
  231. def instantiate(self, class_name, construct_params):
  232. if class_name == "MainApp":
  233. instance = MainApp(self.controller)
  234. instance.associations = {}
  235. instance.associations["trafficlight"] = Association("TrafficLight", 0, -1)
  236. elif class_name == "TrafficLight":
  237. instance = TrafficLight(self.controller, construct_params[0])
  238. instance.associations = {}
  239. else:
  240. raise Exception("Cannot instantiate class " + class_name)
  241. return instance
  242. class Controller(EventLoopControllerBase):
  243. def __init__(self, event_loop_callbacks, finished_callback = None, behind_schedule_callback = None):
  244. if finished_callback == None: finished_callback = None
  245. if behind_schedule_callback == None: behind_schedule_callback = None
  246. EventLoopControllerBase.__init__(self, ObjectManager(self), event_loop_callbacks, finished_callback, behind_schedule_callback)
  247. self.addInputPort("ui")
  248. self.object_manager.createInstance("MainApp", [])