target.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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: Create and Delete A Running Instance (other than the MainApp)
  5. Model description:
  6. Test 14: Check if a running instance can be deleted after the creation.
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "Create and Delete A Running Instance (other than the MainApp)"
  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. pass
  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_deleted", 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("delete_instance", None, [self, 'linkA[0]']))
  71. def _state4_enter(self):
  72. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'linkA[0]', Event("sanity_check", None, [])]))
  73. def _state1_0_exec(self, parameters):
  74. association_name = parameters[0]
  75. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  76. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  77. def _state2_0_exec(self, parameters):
  78. association_name = parameters[0]
  79. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  80. def _state3_0_exec(self, parameters):
  81. deleted_links = parameters[0]
  82. self.big_step.outputEvent(Event("instance_deleted_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), deleted_links]))
  83. def initializeStatechart(self):
  84. # enter default state
  85. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  86. RuntimeClassBase.initializeStatechart(self)
  87. class MainApp(ClassBase):
  88. def __init__(self, name):
  89. ClassBase.__init__(self, name)
  90. self.input = self.addInPort("input")
  91. self.glob_outputs["ui"] = self.addOutPort("ui")
  92. self.outputs["linkA"] = self.addOutPort("linkA")
  93. self.state.instances[self.state.next_instance] = MainAppInstance(self)
  94. self.state.next_instance = self.state.next_instance + 1
  95. def constructObject(self, parameters):
  96. new_instance = MainAppInstance(self)
  97. return new_instance
  98. class AInstance(RuntimeClassBase):
  99. def __init__(self, atomdevs):
  100. RuntimeClassBase.__init__(self, atomdevs)
  101. self.associations = {}
  102. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  103. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  104. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  105. self.semantics.priority = StatechartSemantics.SourceParent
  106. self.semantics.concurrency = StatechartSemantics.Single
  107. # build Statechart structure
  108. self.build_statechart_structure()
  109. # call user defined constructor
  110. AInstance.user_defined_constructor(self)
  111. port_name = Ports.addInputPort("<narrow_cast>", self)
  112. atomdevs.addInPort(port_name)
  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. # add children
  125. self.states[""].addChild(self.states["/state1"])
  126. self.states[""].fixTree()
  127. self.states[""].default_state = self.states["/state1"]
  128. # transition /state1
  129. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state1"]])
  130. _state1_0.setAction(self._state1_0_exec)
  131. _state1_0.setTrigger(Event("sanity_check", None))
  132. self.states["/state1"].addTransition(_state1_0)
  133. def _state1_enter(self):
  134. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  135. def _state1_0_exec(self, parameters):
  136. self.big_step.outputEvent(Event("not_possible", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  137. def initializeStatechart(self):
  138. # enter default state
  139. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  140. RuntimeClassBase.initializeStatechart(self)
  141. class A(ClassBase):
  142. def __init__(self, name):
  143. ClassBase.__init__(self, name)
  144. self.input = self.addInPort("input")
  145. self.glob_outputs["ui"] = self.addOutPort("ui")
  146. def constructObject(self, parameters):
  147. new_instance = AInstance(self)
  148. return new_instance
  149. class ObjectManagerState:
  150. def __init__(self):
  151. self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]
  152. class ObjectManager(ObjectManagerBase):
  153. def __init__(self, name):
  154. ObjectManagerBase.__init__(self, name)
  155. self.state = ObjectManagerState()
  156. self.input = self.addInPort("input")
  157. self.output["MainApp"] = self.addOutPort()
  158. self.output["A"] = self.addOutPort()
  159. class Controller(CoupledDEVS):
  160. def __init__(self, name):
  161. CoupledDEVS.__init__(self, name)
  162. self.in_ui = self.addInPort("ui")
  163. Ports.addInputPort("ui")
  164. self.out_ui = self.addOutPort("ui")
  165. Ports.addOutputPort("ui")
  166. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  167. self.atomics = []
  168. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  169. self.atomics.append(self.addSubModel(A("A")))
  170. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  171. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  172. self.connectPorts(self.atomics[0].outputs["linkA"], self.atomics[1].input)
  173. self.connectPorts(self.atomics[1].obj_manager_out, self.objectmanager.input)
  174. self.connectPorts(self.objectmanager.output["A"], self.atomics[1].obj_manager_in)
  175. self.connectPorts(self.atomics[0].glob_outputs["ui"], self.out_ui)
  176. self.connectPorts(self.in_ui, self.atomics[0].input)
  177. self.connectPorts(self.atomics[1].glob_outputs["ui"], self.out_ui)
  178. self.connectPorts(self.in_ui, self.atomics[1].input)