target.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration) and Sam Pieters (DEVS)
  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.DEVS_statecharts_core import *
  9. # package "Dissasociate an instance"
  10. class MainAppInstance(RuntimeClassBase):
  11. def __init__(self, atomdevs):
  12. RuntimeClassBase.__init__(self, atomdevs)
  13. self.associations = {}
  14. self.associations["linkA"] = Association("A", 0, -1)
  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. MainAppInstance.user_defined_constructor(self)
  24. port_name = Ports.addInputPort("<narrow_cast>", self)
  25. atomdevs.addInPort(port_name)
  26. def user_defined_constructor(self):
  27. self.association_name = None
  28. def user_defined_destructor(self):
  29. pass
  30. # builds Statechart structure
  31. def build_statechart_structure(self):
  32. # state <root>
  33. self.states[""] = State(0, "", self)
  34. # state /state1
  35. self.states["/state1"] = State(1, "/state1", self)
  36. self.states["/state1"].setEnter(self._state1_enter)
  37. # state /state2
  38. self.states["/state2"] = State(2, "/state2", self)
  39. # state /state3
  40. self.states["/state3"] = State(3, "/state3", self)
  41. self.states["/state3"].setEnter(self._state3_enter)
  42. # state /state4
  43. self.states["/state4"] = State(4, "/state4", self)
  44. self.states["/state4"].setEnter(self._state4_enter)
  45. # add children
  46. self.states[""].addChild(self.states["/state1"])
  47. self.states[""].addChild(self.states["/state2"])
  48. self.states[""].addChild(self.states["/state3"])
  49. self.states[""].addChild(self.states["/state4"])
  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_started", 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_disassociated", None))
  66. self.states["/state3"].addTransition(_state3_0)
  67. def _state1_enter(self):
  68. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  69. def _state3_enter(self):
  70. self.big_step.outputEventOM(Event("disassociate_instance", None, [self, "linkA"]))
  71. def _state4_enter(self):
  72. self.big_step.outputEventOM(Event("narrow_cast", None, [self, self.association_name, Event("sanity_check", None, [])]))
  73. def _state1_0_exec(self, parameters):
  74. association_name = parameters[0]
  75. self.association_name = association_name
  76. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  77. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  78. def _state2_0_exec(self, parameters):
  79. association_name = parameters[0]
  80. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  81. self.big_step.outputEventOM(Event("narrow_cast", None, [self, association_name, Event("link_check", None, [association_name])]))
  82. def _state3_0_exec(self, parameters):
  83. deleted_links = parameters[0]
  84. self.big_step.outputEvent(Event("instance_disassociated_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), deleted_links]))
  85. def initializeStatechart(self):
  86. # enter default state
  87. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  88. RuntimeClassBase.initializeStatechart(self)
  89. class MainApp(ObjectManagerBase):
  90. def __init__(self, name):
  91. ObjectManagerBase.__init__(self, name)
  92. self.input = self.addInPort("input")
  93. self.output = self.addOutPort("ui")
  94. self.outputs["linkA"] = self.addOutPort("linkA")
  95. self.instances[self.next_instance] = MainAppInstance(self)
  96. self.next_instance = self.next_instance + 1
  97. def constructObject(self, parameters):
  98. new_instance = MainAppInstance(self)
  99. return new_instance
  100. class AInstance(RuntimeClassBase):
  101. def __init__(self, atomdevs):
  102. RuntimeClassBase.__init__(self, atomdevs)
  103. self.associations = {}
  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. AInstance.user_defined_constructor(self)
  113. port_name = Ports.addInputPort("<narrow_cast>", self)
  114. atomdevs.addInPort(port_name)
  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 A(ObjectManagerBase):
  158. def __init__(self, name):
  159. ObjectManagerBase.__init__(self, name)
  160. self.input = self.addInPort("input")
  161. self.output = self.addOutPort("ui")
  162. def constructObject(self, parameters):
  163. new_instance = AInstance(self)
  164. return new_instance
  165. class ObjectManagerState:
  166. def __init__(self):
  167. self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]
  168. class ObjectManager(TheObjectManager):
  169. def __init__(self, name):
  170. TheObjectManager.__init__(self, name)
  171. self.State = ObjectManagerState()
  172. self.input = self.addInPort("input")
  173. self.output["MainApp"] = self.addOutPort()
  174. self.output["A"] = self.addOutPort()
  175. class Controller(CoupledDEVS):
  176. def __init__(self, name):
  177. CoupledDEVS.__init__(self, name)
  178. self.in_ui = self.addInPort("ui")
  179. Ports.addInputPort("ui")
  180. self.out_ui = self.addOutPort("ui")
  181. Ports.addOutputPort("ui")
  182. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  183. self.atomic0 = self.addSubModel(MainApp("MainApp"))
  184. self.atomic1 = self.addSubModel(A("A"))
  185. self.connectPorts(self.atomic0.obj_manager_out, self.objectmanager.input)
  186. self.connectPorts(self.objectmanager.output["MainApp"], self.atomic0.obj_manager_in)
  187. self.connectPorts(self.atomic0.outputs["linkA"], self.atomic1.input)
  188. self.connectPorts(self.atomic1.obj_manager_out, self.objectmanager.input)
  189. self.connectPorts(self.objectmanager.output["A"], self.atomic1.obj_manager_in)
  190. self.connectPorts(self.atomic0.output, self.out_ui)
  191. self.connectPorts(self.atomic1.output, self.out_ui)