target.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Start Instance Of Multiple Classes
  5. Model description:
  6. TODO
  7. Test 10: Check if an instance of multiple classes are created and started successfully
  8. """
  9. from sccd.runtime.DEVS_statecharts_core import *
  10. # package "Create and Start Instance Of Multiple Classes"
  11. class MainAppInstance(RuntimeClassBase):
  12. def __init__(self, atomdevs):
  13. RuntimeClassBase.__init__(self, atomdevs)
  14. self.associations = {}
  15. self.associations["linkA"] = Association("A", 0, -1)
  16. self.associations["linkB"] = Association("B", 0, -1)
  17. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  18. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  19. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  20. self.semantics.priority = StatechartSemantics.SourceParent
  21. self.semantics.concurrency = StatechartSemantics.Single
  22. # build Statechart structure
  23. self.build_statechart_structure()
  24. # call user defined constructor
  25. MainAppInstance.user_defined_constructor(self)
  26. port_name = Ports.addInputPort("<narrow_cast>", self)
  27. atomdevs.addInPort(port_name)
  28. def user_defined_constructor(self):
  29. pass
  30. def user_defined_destructor(self):
  31. pass
  32. # builds Statechart structure
  33. def build_statechart_structure(self):
  34. # state <root>
  35. self.states[""] = State(0, "", self)
  36. # state /state1
  37. self.states["/state1"] = State(1, "/state1", self)
  38. self.states["/state1"].setEnter(self._state1_enter)
  39. # state /state2
  40. self.states["/state2"] = State(2, "/state2", self)
  41. # add children
  42. self.states[""].addChild(self.states["/state1"])
  43. self.states[""].addChild(self.states["/state2"])
  44. self.states[""].fixTree()
  45. self.states[""].default_state = self.states["/state1"]
  46. # transition /state1
  47. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  48. _state1_0.setAction(self._state1_0_exec)
  49. _state1_0.setTrigger(Event("instance_created", None))
  50. self.states["/state1"].addTransition(_state1_0)
  51. # transition /state2
  52. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  53. _state2_0.setAction(self._state2_0_exec)
  54. _state2_0.setTrigger(Event("instance_started", None))
  55. self.states["/state2"].addTransition(_state2_0)
  56. def _state1_enter(self):
  57. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  58. def _state1_0_exec(self, parameters):
  59. association_name = parameters[0]
  60. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  61. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  62. def _state2_0_exec(self, parameters):
  63. association_name = parameters[0]
  64. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  65. def initializeStatechart(self):
  66. # enter default state
  67. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  68. RuntimeClassBase.initializeStatechart(self)
  69. class MainApp(ClassBase):
  70. def __init__(self, name):
  71. ClassBase.__init__(self, name)
  72. self.input = self.addInPort("input")
  73. self.glob_outputs["ui"] = self.addOutPort("ui")
  74. self.outputs["linkA"] = self.addOutPort("linkA")
  75. self.outputs["linkB"] = self.addOutPort("linkB")
  76. self.state.instances[self.state.next_instance] = MainAppInstance(self)
  77. self.state.next_instance = self.state.next_instance + 1
  78. def constructObject(self, parameters):
  79. new_instance = MainAppInstance(self)
  80. return new_instance
  81. class AInstance(RuntimeClassBase):
  82. def __init__(self, atomdevs):
  83. RuntimeClassBase.__init__(self, atomdevs)
  84. self.associations = {}
  85. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  86. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  87. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  88. self.semantics.priority = StatechartSemantics.SourceParent
  89. self.semantics.concurrency = StatechartSemantics.Single
  90. # build Statechart structure
  91. self.build_statechart_structure()
  92. # call user defined constructor
  93. AInstance.user_defined_constructor(self)
  94. port_name = Ports.addInputPort("<narrow_cast>", self)
  95. atomdevs.addInPort(port_name)
  96. def user_defined_constructor(self):
  97. pass
  98. def user_defined_destructor(self):
  99. pass
  100. # builds Statechart structure
  101. def build_statechart_structure(self):
  102. # state <root>
  103. self.states[""] = State(0, "", self)
  104. # state /state1
  105. self.states["/state1"] = State(1, "/state1", self)
  106. self.states["/state1"].setEnter(self._state1_enter)
  107. # add children
  108. self.states[""].addChild(self.states["/state1"])
  109. self.states[""].fixTree()
  110. self.states[""].default_state = self.states["/state1"]
  111. def _state1_enter(self):
  112. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  113. self.big_step.outputEvent(Event("constructor_initialized_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  114. def initializeStatechart(self):
  115. # enter default state
  116. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  117. RuntimeClassBase.initializeStatechart(self)
  118. class A(ClassBase):
  119. def __init__(self, name):
  120. ClassBase.__init__(self, name)
  121. self.input = self.addInPort("input")
  122. self.glob_outputs["ui"] = self.addOutPort("ui")
  123. def constructObject(self, parameters):
  124. new_instance = AInstance(self)
  125. return new_instance
  126. class BInstance(RuntimeClassBase):
  127. def __init__(self, atomdevs):
  128. RuntimeClassBase.__init__(self, atomdevs)
  129. self.associations = {}
  130. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  131. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  132. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  133. self.semantics.priority = StatechartSemantics.SourceParent
  134. self.semantics.concurrency = StatechartSemantics.Single
  135. # build Statechart structure
  136. self.build_statechart_structure()
  137. # call user defined constructor
  138. BInstance.user_defined_constructor(self)
  139. port_name = Ports.addInputPort("<narrow_cast>", self)
  140. atomdevs.addInPort(port_name)
  141. def user_defined_constructor(self):
  142. pass
  143. def user_defined_destructor(self):
  144. pass
  145. # builds Statechart structure
  146. def build_statechart_structure(self):
  147. # state <root>
  148. self.states[""] = State(0, "", self)
  149. # state /state1
  150. self.states["/state1"] = State(1, "/state1", self)
  151. self.states["/state1"].setEnter(self._state1_enter)
  152. # add children
  153. self.states[""].addChild(self.states["/state1"])
  154. self.states[""].fixTree()
  155. self.states[""].default_state = self.states["/state1"]
  156. def _state1_enter(self):
  157. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  158. self.big_step.outputEvent(Event("constructor_initialized_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  159. def initializeStatechart(self):
  160. # enter default state
  161. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  162. RuntimeClassBase.initializeStatechart(self)
  163. class B(ClassBase):
  164. def __init__(self, name):
  165. ClassBase.__init__(self, name)
  166. self.input = self.addInPort("input")
  167. self.glob_outputs["ui"] = self.addOutPort("ui")
  168. def constructObject(self, parameters):
  169. new_instance = BInstance(self)
  170. return new_instance
  171. class ObjectManagerState:
  172. def __init__(self):
  173. self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]
  174. class ObjectManager(ObjectManagerBase):
  175. def __init__(self, name):
  176. ObjectManagerBase.__init__(self, name)
  177. self.state = ObjectManagerState()
  178. self.input = self.addInPort("input")
  179. self.output["MainApp"] = self.addOutPort()
  180. self.output["A"] = self.addOutPort()
  181. self.output["B"] = self.addOutPort()
  182. class Controller(CoupledDEVS):
  183. def __init__(self, name):
  184. CoupledDEVS.__init__(self, name)
  185. self.in_ui = self.addInPort("ui")
  186. Ports.addInputPort("ui")
  187. self.out_ui = self.addOutPort("ui")
  188. Ports.addOutputPort("ui")
  189. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  190. self.atomics = []
  191. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  192. self.atomics.append(self.addSubModel(A("A")))
  193. self.atomics.append(self.addSubModel(B("B")))
  194. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  195. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  196. self.connectPorts(self.atomics[0].outputs["linkA"], self.atomics[1].input)
  197. self.connectPorts(self.atomics[0].outputs["linkB"], self.atomics[2].input)
  198. self.connectPorts(self.atomics[1].obj_manager_out, self.objectmanager.input)
  199. self.connectPorts(self.objectmanager.output["A"], self.atomics[1].obj_manager_in)
  200. self.connectPorts(self.atomics[2].obj_manager_out, self.objectmanager.input)
  201. self.connectPorts(self.objectmanager.output["B"], self.atomics[2].obj_manager_in)
  202. self.connectPorts(self.atomics[0].glob_outputs["ui"], self.out_ui)
  203. self.connectPorts(self.in_ui, self.atomics[0].input)
  204. self.connectPorts(self.atomics[1].glob_outputs["ui"], self.out_ui)
  205. self.connectPorts(self.in_ui, self.atomics[1].input)
  206. self.connectPorts(self.atomics[2].glob_outputs["ui"], self.out_ui)
  207. self.connectPorts(self.in_ui, self.atomics[2].input)