target_performance_threads.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Tue Aug 09 10:51:10 2016
  4. Model author: Simon Van Mierlo+Raphael Mannadiar
  5. Model name: Bouncing_Balls_Python_Version
  6. Model description:
  7. Tkinter frame with bouncing balls in it.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. import random, sys
  11. # package "Bouncing_Balls_Python_Version"
  12. class MainApp(RuntimeClassBase):
  13. def __init__(self, controller):
  14. RuntimeClassBase.__init__(self, controller)
  15. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  16. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  17. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  18. self.semantics.priority = StatechartSemantics.SourceParent
  19. self.semantics.concurrency = StatechartSemantics.Single
  20. # build Statechart structure
  21. self.build_statechart_structure()
  22. # call user defined constructor
  23. MainApp.user_defined_constructor(self)
  24. def user_defined_constructor(self):
  25. self.nr_of_fields = 0
  26. def user_defined_destructor(self):
  27. pass
  28. # builds Statechart structure
  29. def build_statechart_structure(self):
  30. # state <root>
  31. self.states[""] = State(0, self)
  32. # state /running
  33. self.states["/running"] = State(1, self)
  34. # state /running/root
  35. self.states["/running/root"] = ParallelState(2, self)
  36. # state /running/root/cd_behaviour
  37. self.states["/running/root/cd_behaviour"] = State(3, self)
  38. # state /running/root/cd_behaviour/waiting
  39. self.states["/running/root/cd_behaviour/waiting"] = State(4, self)
  40. # state /running/root/cd_behaviour/creating
  41. self.states["/running/root/cd_behaviour/creating"] = State(5, self)
  42. # state /running/root/spawn_windows
  43. self.states["/running/root/spawn_windows"] = State(6, self)
  44. # state /running/root/spawn_windows/spawning
  45. self.states["/running/root/spawn_windows/spawning"] = State(7, self)
  46. self.states["/running/root/spawn_windows/spawning"].setEnter(self._running_root_spawn_windows_spawning_enter)
  47. self.states["/running/root/spawn_windows/spawning"].setExit(self._running_root_spawn_windows_spawning_exit)
  48. # state /running/stopped
  49. self.states["/running/stopped"] = State(8, self)
  50. # add children
  51. self.states[""].addChild(self.states["/running"])
  52. self.states["/running"].addChild(self.states["/running/root"])
  53. self.states["/running"].addChild(self.states["/running/stopped"])
  54. self.states["/running/root"].addChild(self.states["/running/root/cd_behaviour"])
  55. self.states["/running/root"].addChild(self.states["/running/root/spawn_windows"])
  56. self.states["/running/root/cd_behaviour"].addChild(self.states["/running/root/cd_behaviour/waiting"])
  57. self.states["/running/root/cd_behaviour"].addChild(self.states["/running/root/cd_behaviour/creating"])
  58. self.states["/running/root/spawn_windows"].addChild(self.states["/running/root/spawn_windows/spawning"])
  59. self.states[""].fixTree()
  60. self.states[""].default_state = self.states["/running"]
  61. self.states["/running"].default_state = self.states["/running/root"]
  62. self.states["/running/root/cd_behaviour"].default_state = self.states["/running/root/cd_behaviour/waiting"]
  63. self.states["/running/root/spawn_windows"].default_state = self.states["/running/root/spawn_windows/spawning"]
  64. # transition /running/root/cd_behaviour/waiting
  65. _running_root_cd_behaviour_waiting_0 = Transition(self, self.states["/running/root/cd_behaviour/waiting"], [self.states["/running/root/cd_behaviour/creating"]])
  66. _running_root_cd_behaviour_waiting_0.setAction(self._running_root_cd_behaviour_waiting_0_exec)
  67. _running_root_cd_behaviour_waiting_0.setTrigger(Event("create_field", None))
  68. self.states["/running/root/cd_behaviour/waiting"].addTransition(_running_root_cd_behaviour_waiting_0)
  69. # transition /running/root/cd_behaviour/creating
  70. _running_root_cd_behaviour_creating_0 = Transition(self, self.states["/running/root/cd_behaviour/creating"], [self.states["/running/root/cd_behaviour/waiting"]])
  71. _running_root_cd_behaviour_creating_0.setAction(self._running_root_cd_behaviour_creating_0_exec)
  72. _running_root_cd_behaviour_creating_0.setTrigger(Event("instance_created", None))
  73. self.states["/running/root/cd_behaviour/creating"].addTransition(_running_root_cd_behaviour_creating_0)
  74. # transition /running/root/spawn_windows/spawning
  75. _running_root_spawn_windows_spawning_0 = Transition(self, self.states["/running/root/spawn_windows/spawning"], [self.states["/running/root/spawn_windows/spawning"]])
  76. _running_root_spawn_windows_spawning_0.setAction(self._running_root_spawn_windows_spawning_0_exec)
  77. _running_root_spawn_windows_spawning_0.setTrigger(Event("_0after"))
  78. _running_root_spawn_windows_spawning_0.setGuard(self._running_root_spawn_windows_spawning_0_guard)
  79. self.states["/running/root/spawn_windows/spawning"].addTransition(_running_root_spawn_windows_spawning_0)
  80. def _running_root_spawn_windows_spawning_enter(self):
  81. self.addTimer(0, (1000 - self.getSimulatedTime() % 1000) / 1000.0)
  82. def _running_root_spawn_windows_spawning_exit(self):
  83. self.removeTimer(0)
  84. def _running_root_cd_behaviour_waiting_0_exec(self, parameters):
  85. self.big_step.outputEventOM(Event("create_instance", None, [self, "fields"]))
  86. def _running_root_cd_behaviour_creating_0_exec(self, parameters):
  87. association_name = parameters[0]
  88. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  89. def _running_root_spawn_windows_spawning_0_exec(self, parameters):
  90. self.raiseInternalEvent(Event("create_field", None, []))
  91. def _running_root_spawn_windows_spawning_0_guard(self, parameters):
  92. return self.nr_of_fields < 10
  93. def initializeStatechart(self):
  94. # enter default state
  95. self.default_targets = self.states["/running"].getEffectiveTargetStates()
  96. RuntimeClassBase.initializeStatechart(self)
  97. class Field(RuntimeClassBase):
  98. def __init__(self, controller):
  99. RuntimeClassBase.__init__(self, controller)
  100. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  101. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  102. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  103. self.semantics.priority = StatechartSemantics.SourceParent
  104. self.semantics.concurrency = StatechartSemantics.Single
  105. # build Statechart structure
  106. self.build_statechart_structure()
  107. # call user defined constructor
  108. Field.user_defined_constructor(self)
  109. def user_defined_constructor(self):
  110. pass
  111. def user_defined_destructor(self):
  112. pass
  113. # builds Statechart structure
  114. def build_statechart_structure(self):
  115. # state <root>
  116. self.states[""] = State(0, self)
  117. # state /root
  118. self.states["/root"] = State(1, self)
  119. # state /root/running
  120. self.states["/root/running"] = ParallelState(2, self)
  121. # state /root/running/main_behaviour
  122. self.states["/root/running/main_behaviour"] = State(3, self)
  123. # state /root/running/main_behaviour/running
  124. self.states["/root/running/main_behaviour/running"] = State(4, self)
  125. # state /root/running/main_behaviour/creating
  126. self.states["/root/running/main_behaviour/creating"] = State(5, self)
  127. # state /root/running/spawn_balls
  128. self.states["/root/running/spawn_balls"] = State(6, self)
  129. # state /root/running/spawn_balls/spawning
  130. self.states["/root/running/spawn_balls/spawning"] = State(7, self)
  131. self.states["/root/running/spawn_balls/spawning"].setEnter(self._root_running_spawn_balls_spawning_enter)
  132. self.states["/root/running/spawn_balls/spawning"].setExit(self._root_running_spawn_balls_spawning_exit)
  133. # state /root/deleting
  134. self.states["/root/deleting"] = State(8, self)
  135. # state /root/deleted
  136. self.states["/root/deleted"] = State(9, self)
  137. # add children
  138. self.states[""].addChild(self.states["/root"])
  139. self.states["/root"].addChild(self.states["/root/running"])
  140. self.states["/root"].addChild(self.states["/root/deleting"])
  141. self.states["/root"].addChild(self.states["/root/deleted"])
  142. self.states["/root/running"].addChild(self.states["/root/running/main_behaviour"])
  143. self.states["/root/running"].addChild(self.states["/root/running/spawn_balls"])
  144. self.states["/root/running/main_behaviour"].addChild(self.states["/root/running/main_behaviour/running"])
  145. self.states["/root/running/main_behaviour"].addChild(self.states["/root/running/main_behaviour/creating"])
  146. self.states["/root/running/spawn_balls"].addChild(self.states["/root/running/spawn_balls/spawning"])
  147. self.states[""].fixTree()
  148. self.states[""].default_state = self.states["/root"]
  149. self.states["/root"].default_state = self.states["/root/running"]
  150. self.states["/root/running/main_behaviour"].default_state = self.states["/root/running/main_behaviour/running"]
  151. self.states["/root/running/spawn_balls"].default_state = self.states["/root/running/spawn_balls/spawning"]
  152. # transition /root/running/main_behaviour/running
  153. _root_running_main_behaviour_running_0 = Transition(self, self.states["/root/running/main_behaviour/running"], [self.states["/root/running/main_behaviour/creating"]])
  154. _root_running_main_behaviour_running_0.setAction(self._root_running_main_behaviour_running_0_exec)
  155. _root_running_main_behaviour_running_0.setTrigger(Event("spawn_ball", None))
  156. self.states["/root/running/main_behaviour/running"].addTransition(_root_running_main_behaviour_running_0)
  157. # transition /root/running/main_behaviour/creating
  158. _root_running_main_behaviour_creating_0 = Transition(self, self.states["/root/running/main_behaviour/creating"], [self.states["/root/running/main_behaviour/running"]])
  159. _root_running_main_behaviour_creating_0.setAction(self._root_running_main_behaviour_creating_0_exec)
  160. _root_running_main_behaviour_creating_0.setTrigger(Event("instance_created", None))
  161. self.states["/root/running/main_behaviour/creating"].addTransition(_root_running_main_behaviour_creating_0)
  162. # transition /root/running/spawn_balls/spawning
  163. _root_running_spawn_balls_spawning_0 = Transition(self, self.states["/root/running/spawn_balls/spawning"], [self.states["/root/running/spawn_balls/spawning"]])
  164. _root_running_spawn_balls_spawning_0.setAction(self._root_running_spawn_balls_spawning_0_exec)
  165. _root_running_spawn_balls_spawning_0.setTrigger(Event("_0after"))
  166. self.states["/root/running/spawn_balls/spawning"].addTransition(_root_running_spawn_balls_spawning_0)
  167. # transition /root/deleting
  168. _root_deleting_0 = Transition(self, self.states["/root/deleting"], [self.states["/root/deleted"]])
  169. _root_deleting_0.setAction(self._root_deleting_0_exec)
  170. _root_deleting_0.setTrigger(None)
  171. self.states["/root/deleting"].addTransition(_root_deleting_0)
  172. def _root_running_spawn_balls_spawning_enter(self):
  173. self.addTimer(0, (50 - self.getSimulatedTime() % 50) / 1000.0)
  174. def _root_running_spawn_balls_spawning_exit(self):
  175. self.removeTimer(0)
  176. def _root_running_main_behaviour_running_0_exec(self, parameters):
  177. x = parameters[0]
  178. y = parameters[1]
  179. self.big_step.outputEventOM(Event("create_instance", None, [self, "balls", "Ball", x, y]))
  180. def _root_running_main_behaviour_creating_0_exec(self, parameters):
  181. association_name = parameters[0]
  182. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  183. def _root_running_spawn_balls_spawning_0_exec(self, parameters):
  184. self.raiseInternalEvent(Event("spawn_ball", None, [150, 150]))
  185. def _root_deleting_0_exec(self, parameters):
  186. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("delete_field", None, [self.association_name])]))
  187. def initializeStatechart(self):
  188. # enter default state
  189. self.default_targets = self.states["/root"].getEffectiveTargetStates()
  190. RuntimeClassBase.initializeStatechart(self)
  191. class Ball(RuntimeClassBase):
  192. def __init__(self, controller, x, y):
  193. RuntimeClassBase.__init__(self, controller)
  194. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  195. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  196. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  197. self.semantics.priority = StatechartSemantics.SourceParent
  198. self.semantics.concurrency = StatechartSemantics.Single
  199. # build Statechart structure
  200. self.build_statechart_structure()
  201. # call user defined constructor
  202. Ball.user_defined_constructor(self, x, y)
  203. def user_defined_constructor(self, x, y):
  204. self.r = 20.0;
  205. self.vel = {'x': random.uniform(-5.0, 5.0), 'y': random.uniform(-5.0, 5.0)};
  206. self.mouse_pos = {};
  207. self.smooth = 0.4; # value between 0 and 1
  208. self.pos = {'x': x, 'y': y}
  209. def user_defined_destructor(self):
  210. pass
  211. # builds Statechart structure
  212. def build_statechart_structure(self):
  213. # state <root>
  214. self.states[""] = State(0, self)
  215. # state /main_behaviour
  216. self.states["/main_behaviour"] = State(1, self)
  217. # state /main_behaviour/bouncing
  218. self.states["/main_behaviour/bouncing"] = State(2, self)
  219. self.states["/main_behaviour/bouncing"].setEnter(self._main_behaviour_bouncing_enter)
  220. self.states["/main_behaviour/bouncing"].setExit(self._main_behaviour_bouncing_exit)
  221. # add children
  222. self.states[""].addChild(self.states["/main_behaviour"])
  223. self.states["/main_behaviour"].addChild(self.states["/main_behaviour/bouncing"])
  224. self.states[""].fixTree()
  225. self.states[""].default_state = self.states["/main_behaviour"]
  226. self.states["/main_behaviour"].default_state = self.states["/main_behaviour/bouncing"]
  227. # transition /main_behaviour/bouncing
  228. _main_behaviour_bouncing_0 = Transition(self, self.states["/main_behaviour/bouncing"], [self.states["/main_behaviour/bouncing"]])
  229. _main_behaviour_bouncing_0.setAction(self._main_behaviour_bouncing_0_exec)
  230. _main_behaviour_bouncing_0.setTrigger(Event("_0after"))
  231. self.states["/main_behaviour/bouncing"].addTransition(_main_behaviour_bouncing_0)
  232. def _main_behaviour_bouncing_enter(self):
  233. self.addTimer(0, (20 - self.getSimulatedTime() % 20) / 1000.0)
  234. def _main_behaviour_bouncing_exit(self):
  235. self.removeTimer(0)
  236. def _main_behaviour_bouncing_0_exec(self, parameters):
  237. self.pos
  238. if self.pos['x'] - self.r <= 0 or self.pos['x'] + self.r >= 800 :
  239. self.vel['x'] = -self.vel['x'];
  240. if self.pos['y'] - self.r <= 0 or self.pos['y'] + self.r >= 600 :
  241. self.vel['y'] = -self.vel['y'];
  242. self.pos['x'] += self.vel['x']
  243. self.pos['y'] += self.vel['y']
  244. def initializeStatechart(self):
  245. # enter default state
  246. self.default_targets = self.states["/main_behaviour"].getEffectiveTargetStates()
  247. RuntimeClassBase.initializeStatechart(self)
  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["fields"] = Association("Field", 0, -1)
  256. elif class_name == "Field":
  257. instance = Field(self.controller)
  258. instance.associations = {}
  259. instance.associations["balls"] = Association("Ball", 0, -1)
  260. instance.associations["parent"] = Association("MainApp", 1, 1)
  261. elif class_name == "Ball":
  262. instance = Ball(self.controller, construct_params[0], construct_params[1])
  263. instance.associations = {}
  264. instance.associations["parent"] = Association("Field", 1, 1)
  265. else:
  266. raise Exception("Cannot instantiate class " + class_name)
  267. return instance
  268. class Controller(ThreadsControllerBase):
  269. def __init__(self, keep_running = None, behind_schedule_callback = None):
  270. if keep_running == None: keep_running = True
  271. if behind_schedule_callback == None: behind_schedule_callback = None
  272. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  273. self.object_manager.createInstance("MainApp", [])