sccd.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 Jul 28 15:43:05 2016
  4. Model author: Raphael Mannadiar
  5. Model name: Traffic_Light_Python_Version
  6. """
  7. from python_runtime.statecharts_core import *
  8. from python_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 /initializing
  33. self.states["/initializing"] = State(0, self)
  34. # state /creating
  35. self.states["/creating"] = State(1, self)
  36. # state /initialized
  37. self.states["/initialized"] = State(2, self)
  38. # state <root>
  39. self.states[""] = State(3, self)
  40. # add children
  41. self.states[""].children.add(self.states["/initializing"])
  42. self.states[""].children.add(self.states["/creating"])
  43. self.states[""].children.add(self.states["/initialized"])
  44. self.states[""].fixTree()
  45. self.states[""].default = self.states["/initializing"]
  46. # transition /initializing
  47. _initializing_0 = Transition(self, self.states["/initializing"], self.states["/creating"])
  48. _initializing_0.executables["action"] = self._initializing_0_exec
  49. self.states["/initializing"].transitions.add(_initializing_0)
  50. # transition /creating
  51. _creating_0 = Transition(self, self.states["/creating"], self.states["/initialized"])
  52. _creating_0.executables["action"] = self._creating_0_exec
  53. _creating_0.trigger = Event("instance_created", None)
  54. self.states["/creating"].transitions.add(_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. # generate transition candidates for current small step
  62. def generateCandidates(self):
  63. enabledEvents = self.getEnabledEvents()
  64. enabledTransitions = OrderedSet()
  65. for s in self.configuration:
  66. if not (s in self.combo_step.changed):
  67. for t in s.transitions:
  68. if t.isEnabled(enabledEvents):
  69. enabledTransitions.add(t)
  70. return enabledTransitions
  71. def initializeStatechart(self):
  72. # enter default state
  73. states = self.states["/initializing"].getEffectiveTargetStates()
  74. self.configuration.update(states)
  75. for state in states:
  76. if state.executables["enter"]:
  77. state.executables["enter"]()
  78. class TrafficLight(RuntimeClassBase):
  79. def __init__(self, controller, canvas):
  80. RuntimeClassBase.__init__(self, controller)
  81. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  82. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  83. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  84. self.semantics.priority = StatechartSemantics.SourceParent
  85. self.semantics.concurrency = StatechartSemantics.Single
  86. # build Statechart structure
  87. self.build_statechart_structure()
  88. # call user defined constructor
  89. TrafficLight.user_defined_constructor(self, canvas)
  90. def user_defined_constructor(self, canvas):
  91. size = 100;
  92. offset = size+5;
  93. self.RED = 0;
  94. self.YELLOW = 1;
  95. self.GREEN = 2;
  96. self.colors = ['#f00','#ff0','#0f0']
  97. self.lights = [
  98. canvas.add_rectangle(size/2, size/2, size, size, {'fill':'#000'}),
  99. canvas.add_rectangle(size/2, size/2+offset, size, size, {'fill':'#000'}),
  100. canvas.add_rectangle(size/2, size/2+2*offset, size, size, {'fill':'#000'})];
  101. self.my_after = 1
  102. def user_defined_destructor(self):
  103. pass
  104. # user defined method
  105. def clear(self):
  106. self.lights[self.RED].set_color('#000');
  107. self.lights[self.YELLOW].set_color('#000');
  108. self.lights[self.GREEN].set_color('#000');
  109. # user defined method
  110. def setGreen(self):
  111. self.clear();
  112. self.lights[self.GREEN].set_color(self.colors[self.GREEN]);
  113. # user defined method
  114. def setYellow(self):
  115. self.clear();
  116. self.lights[self.YELLOW].set_color(self.colors[self.YELLOW]);
  117. # user defined method
  118. def setRed(self):
  119. self.clear();
  120. self.lights[self.RED].set_color(self.colors[self.RED]);
  121. # builds Statechart structure
  122. def build_statechart_structure(self):
  123. # state /on/normal/red
  124. self.states["/on/normal/red"] = State(0, self)
  125. self.states["/on/normal/red"].executables["enter"] = self._on_normal_red_enter
  126. self.states["/on/normal/red"].executables["exit"] = self._on_normal_red_exit
  127. # state /on/normal/green
  128. self.states["/on/normal/green"] = State(1, self)
  129. self.states["/on/normal/green"].executables["enter"] = self._on_normal_green_enter
  130. self.states["/on/normal/green"].executables["exit"] = self._on_normal_green_exit
  131. # state /on/normal/yellow
  132. self.states["/on/normal/yellow"] = State(2, self)
  133. self.states["/on/normal/yellow"].executables["enter"] = self._on_normal_yellow_enter
  134. self.states["/on/normal/yellow"].executables["exit"] = self._on_normal_yellow_exit
  135. # state /on/interrupted/yellow
  136. self.states["/on/interrupted/yellow"] = State(3, self)
  137. self.states["/on/interrupted/yellow"].executables["enter"] = self._on_interrupted_yellow_enter
  138. self.states["/on/interrupted/yellow"].executables["exit"] = self._on_interrupted_yellow_exit
  139. # state /on/interrupted/black
  140. self.states["/on/interrupted/black"] = State(4, self)
  141. self.states["/on/interrupted/black"].executables["enter"] = self._on_interrupted_black_enter
  142. self.states["/on/interrupted/black"].executables["exit"] = self._on_interrupted_black_exit
  143. # state /off
  144. self.states["/off"] = State(5, self)
  145. self.states["/off"].executables["enter"] = self._off_enter
  146. # state <root>
  147. self.states[""] = State(6, self)
  148. # state /on
  149. self.states["/on"] = State(7, self)
  150. # state /on/normal
  151. self.states["/on/normal"] = State(8, self)
  152. # state /on/interrupted
  153. self.states["/on/interrupted"] = State(9, self)
  154. # state /on/normal/history
  155. self.states["/on/normal/history"] = ShallowHistoryState(10, self)
  156. # add children
  157. self.states[""].children.add(self.states["/on"])
  158. self.states[""].children.add(self.states["/off"])
  159. self.states["/on"].children.add(self.states["/on/normal"])
  160. self.states["/on"].children.add(self.states["/on/interrupted"])
  161. self.states["/on/normal"].children.add(self.states["/on/normal/red"])
  162. self.states["/on/normal"].children.add(self.states["/on/normal/green"])
  163. self.states["/on/normal"].children.add(self.states["/on/normal/yellow"])
  164. self.states["/on/normal"].children.add(self.states["/on/normal/history"])
  165. self.states["/on/interrupted"].children.add(self.states["/on/interrupted/yellow"])
  166. self.states["/on/interrupted"].children.add(self.states["/on/interrupted/black"])
  167. self.states[""].fixTree()
  168. self.states[""].default = self.states["/on"]
  169. self.states["/on"].default = self.states["/on/normal"]
  170. self.states["/on/normal"].default = self.states["/on/normal/red"]
  171. self.states["/on/interrupted"].default = self.states["/on/interrupted/yellow"]
  172. # transition /on/normal/red
  173. _on_normal_red_0 = Transition(self, self.states["/on/normal/red"], self.states["/on/normal/green"])
  174. _on_normal_red_0.trigger = Event("_0after")
  175. self.states["/on/normal/red"].transitions.add(_on_normal_red_0)
  176. # transition /on/normal/green
  177. _on_normal_green_0 = Transition(self, self.states["/on/normal/green"], self.states["/on/normal/yellow"])
  178. _on_normal_green_0.trigger = Event("_1after")
  179. self.states["/on/normal/green"].transitions.add(_on_normal_green_0)
  180. # transition /on/normal/yellow
  181. _on_normal_yellow_0 = Transition(self, self.states["/on/normal/yellow"], self.states["/on/normal/red"])
  182. _on_normal_yellow_0.trigger = Event("_2after")
  183. self.states["/on/normal/yellow"].transitions.add(_on_normal_yellow_0)
  184. # transition /on/interrupted/yellow
  185. _on_interrupted_yellow_0 = Transition(self, self.states["/on/interrupted/yellow"], self.states["/on/interrupted/black"])
  186. _on_interrupted_yellow_0.trigger = Event("_3after")
  187. self.states["/on/interrupted/yellow"].transitions.add(_on_interrupted_yellow_0)
  188. # transition /on/interrupted/black
  189. _on_interrupted_black_0 = Transition(self, self.states["/on/interrupted/black"], self.states["/on/interrupted/yellow"])
  190. _on_interrupted_black_0.trigger = Event("_4after")
  191. self.states["/on/interrupted/black"].transitions.add(_on_interrupted_black_0)
  192. # transition /on
  193. _on_0 = Transition(self, self.states["/on"], self.states["/off"])
  194. _on_0.trigger = Event("quit_clicked", "ui")
  195. self.states["/on"].transitions.add(_on_0)
  196. # transition /on/normal
  197. _on_normal_0 = Transition(self, self.states["/on/normal"], self.states["/on/interrupted"])
  198. _on_normal_0.trigger = Event("police_interrupt_clicked", "ui")
  199. self.states["/on/normal"].transitions.add(_on_normal_0)
  200. # transition /on/interrupted
  201. _on_interrupted_0 = Transition(self, self.states["/on/interrupted"], self.states["/on/normal/history"])
  202. _on_interrupted_0.trigger = Event("police_interrupt_clicked", "ui")
  203. self.states["/on/interrupted"].transitions.add(_on_interrupted_0)
  204. def _on_normal_red_enter(self):
  205. self.addTimer(0, 3)
  206. self.setRed();
  207. def _on_normal_red_exit(self):
  208. self.removeTimer(0)
  209. def _on_normal_green_enter(self):
  210. self.addTimer(1, 2)
  211. self.setGreen();
  212. def _on_normal_green_exit(self):
  213. self.removeTimer(1)
  214. def _on_normal_yellow_enter(self):
  215. self.addTimer(2, self.my_after)
  216. self.setYellow();
  217. def _on_normal_yellow_exit(self):
  218. self.removeTimer(2)
  219. def _on_interrupted_yellow_enter(self):
  220. self.addTimer(3, .5)
  221. self.setYellow();
  222. def _on_interrupted_yellow_exit(self):
  223. self.removeTimer(3)
  224. def _on_interrupted_black_enter(self):
  225. self.addTimer(4, .5)
  226. self.clear();
  227. def _on_interrupted_black_exit(self):
  228. self.removeTimer(4)
  229. def _off_enter(self):
  230. self.clear();
  231. # generate transition candidates for current small step
  232. def generateCandidates(self):
  233. enabledEvents = self.getEnabledEvents()
  234. enabledTransitions = OrderedSet()
  235. for s in self.configuration:
  236. if not (s in self.combo_step.changed):
  237. for t in s.transitions:
  238. if t.isEnabled(enabledEvents):
  239. enabledTransitions.add(t)
  240. return enabledTransitions
  241. def initializeStatechart(self):
  242. # enter default state
  243. states = self.states["/on"].getEffectiveTargetStates()
  244. self.configuration.update(states)
  245. for state in states:
  246. if state.executables["enter"]:
  247. state.executables["enter"]()
  248. class ObjectManager(ObjectManagerBase):
  249. def __init__(self, controller):
  250. ObjectManagerBase.__init__(self, controller)
  251. def instantiate(self, class_name, construct_params):
  252. if class_name == "MainApp":
  253. instance = MainApp(self.controller)
  254. instance.associations = {}
  255. instance.associations["trafficlight"] = Association("TrafficLight", 0, -1)
  256. elif class_name == "TrafficLight":
  257. instance = TrafficLight(self.controller, construct_params[0])
  258. instance.associations = {}
  259. return instance
  260. class Controller(EventLoopControllerBase):
  261. def __init__(self, event_loop_callbacks, finished_callback = None):
  262. if finished_callback == None: finished_callback = None
  263. EventLoopControllerBase.__init__(self, ObjectManager(self), event_loop_callbacks, finished_callback)
  264. self.addInputPort("ui")
  265. self.object_manager.createInstance("MainApp", [])