target.py 8.7 KB

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