target.py 9.8 KB

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