target.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Model author: Sam Pieters
  4. Model name: Dissasociate multiple instances
  5. Model description:
  6. Test 23: Dissasociate multiple instances
  7. """
  8. from sccd.runtime.statecharts_core import *
  9. # package "Dissasociate multiple instances"
  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.association_name = None
  24. def user_defined_destructor(self):
  25. pass
  26. # builds Statechart structure
  27. def build_statechart_structure(self):
  28. # state <root>
  29. self.states[""] = State(0, "", self)
  30. # state /state1
  31. self.states["/state1"] = State(1, "/state1", self)
  32. self.states["/state1"].setEnter(self._state1_enter)
  33. # state /state2
  34. self.states["/state2"] = State(2, "/state2", self)
  35. self.states["/state2"].setEnter(self._state2_enter)
  36. # state /state3
  37. self.states["/state3"] = State(3, "/state3", self)
  38. # state /state4
  39. self.states["/state4"] = State(4, "/state4", self)
  40. self.states["/state4"].setEnter(self._state4_enter)
  41. # state /state5
  42. self.states["/state5"] = State(5, "/state5", self)
  43. self.states["/state5"].setEnter(self._state5_enter)
  44. # add children
  45. self.states[""].addChild(self.states["/state1"])
  46. self.states[""].addChild(self.states["/state2"])
  47. self.states[""].addChild(self.states["/state3"])
  48. self.states[""].addChild(self.states["/state4"])
  49. self.states[""].addChild(self.states["/state5"])
  50. self.states[""].fixTree()
  51. self.states[""].default_state = self.states["/state1"]
  52. # transition /state1
  53. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  54. _state1_0.setAction(self._state1_0_exec)
  55. _state1_0.setTrigger(Event("instance_created", None))
  56. self.states["/state1"].addTransition(_state1_0)
  57. # transition /state2
  58. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state3"]])
  59. _state2_0.setAction(self._state2_0_exec)
  60. _state2_0.setTrigger(Event("instance_created", None))
  61. self.states["/state2"].addTransition(_state2_0)
  62. # transition /state3
  63. _state3_0 = Transition(self, self.states["/state3"], [self.states["/state4"]])
  64. _state3_0.setAction(self._state3_0_exec)
  65. _state3_0.setTrigger(Event("instance_started", None))
  66. self.states["/state3"].addTransition(_state3_0)
  67. # transition /state4
  68. _state4_0 = Transition(self, self.states["/state4"], [self.states["/state5"]])
  69. _state4_0.setAction(self._state4_0_exec)
  70. _state4_0.setTrigger(Event("instance_disassociated", None))
  71. self.states["/state4"].addTransition(_state4_0)
  72. def _state1_enter(self):
  73. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  74. def _state2_enter(self):
  75. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  76. def _state4_enter(self):
  77. self.big_step.outputEventOM(Event("disassociate_instance", None, [self, "linkA"]))
  78. def _state5_enter(self):
  79. self.big_step.outputEventOM(Event("narrow_cast", None, [self, self.association_name, Event("sanity_check", None, [])]))
  80. def _state1_0_exec(self, parameters):
  81. association_name = parameters[0]
  82. self.association_name = association_name
  83. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  84. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  85. def _state2_0_exec(self, parameters):
  86. association_name = parameters[0]
  87. self.association_name = association_name
  88. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  89. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  90. def _state3_0_exec(self, parameters):
  91. association_name = parameters[0]
  92. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  93. self.big_step.outputEventOM(Event("narrow_cast", None, [self, association_name, Event("link_check", None, [association_name])]))
  94. def _state4_0_exec(self, parameters):
  95. deleted_links = parameters[0]
  96. self.big_step.outputEvent(Event("instance_disassociated_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), deleted_links]))
  97. def initializeStatechart(self):
  98. # enter default state
  99. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  100. RuntimeClassBase.initializeStatechart(self)
  101. class A(RuntimeClassBase):
  102. def __init__(self, controller):
  103. RuntimeClassBase.__init__(self, controller)
  104. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  105. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  106. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  107. self.semantics.priority = StatechartSemantics.SourceParent
  108. self.semantics.concurrency = StatechartSemantics.Single
  109. # build Statechart structure
  110. self.build_statechart_structure()
  111. # call user defined constructor
  112. A.user_defined_constructor(self)
  113. def user_defined_constructor(self):
  114. pass
  115. def user_defined_destructor(self):
  116. pass
  117. # builds Statechart structure
  118. def build_statechart_structure(self):
  119. # state <root>
  120. self.states[""] = State(0, "", self)
  121. # state /state1
  122. self.states["/state1"] = State(1, "/state1", self)
  123. self.states["/state1"].setEnter(self._state1_enter)
  124. # state /state2
  125. self.states["/state2"] = State(2, "/state2", self)
  126. # add children
  127. self.states[""].addChild(self.states["/state1"])
  128. self.states[""].addChild(self.states["/state2"])
  129. self.states[""].fixTree()
  130. self.states[""].default_state = self.states["/state1"]
  131. # transition /state1
  132. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  133. _state1_0.setTrigger(None)
  134. self.states["/state1"].addTransition(_state1_0)
  135. # transition /state2
  136. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  137. _state2_0.setAction(self._state2_0_exec)
  138. _state2_0.setTrigger(Event("link_check", None))
  139. self.states["/state2"].addTransition(_state2_0)
  140. _state2_1 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  141. _state2_1.setAction(self._state2_1_exec)
  142. _state2_1.setTrigger(Event("sanity_check", None))
  143. self.states["/state2"].addTransition(_state2_1)
  144. def _state1_enter(self):
  145. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  146. def _state2_0_exec(self, parameters):
  147. link_name = parameters[0]
  148. self.big_step.outputEvent(Event("instance_linked_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), link_name]))
  149. def _state2_1_exec(self, parameters):
  150. self.big_step.outputEvent(Event("not_possible", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  151. def initializeStatechart(self):
  152. # enter default state
  153. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  154. RuntimeClassBase.initializeStatechart(self)
  155. class ObjectManager(ObjectManagerBase):
  156. def __init__(self, controller):
  157. ObjectManagerBase.__init__(self, controller)
  158. def instantiate(self, class_name, construct_params):
  159. if class_name == "MainApp":
  160. instance = MainApp(self.controller)
  161. instance.associations = {}
  162. instance.associations["linkA"] = Association("A", 0, -1)
  163. elif class_name == "A":
  164. instance = A(self.controller)
  165. instance.associations = {}
  166. else:
  167. raise Exception("Cannot instantiate class " + class_name)
  168. return instance
  169. class Controller(ThreadsControllerBase):
  170. def __init__(self, keep_running = None, behind_schedule_callback = None):
  171. if keep_running == None: keep_running = True
  172. if behind_schedule_callback == None: behind_schedule_callback = None
  173. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  174. self.addInputPort("ui")
  175. self.addOutputPort("ui")
  176. self.object_manager.createInstance("MainApp", [])