target.py 13 KB

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