target.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Multiple Instances Of The Same Class
  5. Model description:
  6. Test 9: Create and start multiple instances of the same class
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "Create and Start Multiple Instances Of The Same Class"
  10. class MainAppInstance(RuntimeClassBase):
  11. def __init__(self, atomdevs, id, start_port_id):
  12. RuntimeClassBase.__init__(self, atomdevs, id)
  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 = addInputPort("ui", start_port_id, True)
  25. atomdevs.addInPort(port_name)
  26. atomdevs.state.port_mappings[port_name] = id
  27. port_name = addInputPort("<narrow_cast>", start_port_id)
  28. atomdevs.addInPort(port_name)
  29. atomdevs.state.port_mappings[port_name] = id
  30. def user_defined_constructor(self):
  31. self.instances = 10
  32. def user_defined_destructor(self):
  33. pass
  34. # builds Statechart structure
  35. def build_statechart_structure(self):
  36. # state <root>
  37. self.states[""] = State(0, "", self)
  38. # state /state1
  39. self.states["/state1"] = State(1, "/state1", self)
  40. self.states["/state1"].setEnter(self._state1_enter)
  41. # state /state2
  42. self.states["/state2"] = State(2, "/state2", self)
  43. # state /state3
  44. self.states["/state3"] = State(3, "/state3", self)
  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[""].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["/state2"]])
  58. _state2_0.setAction(self._state2_0_exec)
  59. _state2_0.setTrigger(Event("instance_started", None))
  60. self.states["/state2"].addTransition(_state2_0)
  61. _state2_1 = Transition(self, self.states["/state2"], [self.states["/state1"]])
  62. _state2_1.setTrigger(None)
  63. _state2_1.setGuard(self._state2_1_guard)
  64. self.states["/state2"].addTransition(_state2_1)
  65. _state2_2 = Transition(self, self.states["/state2"], [self.states["/state3"]])
  66. _state2_2.setTrigger(None)
  67. _state2_2.setGuard(self._state2_2_guard)
  68. self.states["/state2"].addTransition(_state2_2)
  69. def _state1_enter(self):
  70. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A", self.instances]))
  71. def _state1_0_exec(self, parameters):
  72. association_name = parameters[0]
  73. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  74. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  75. self.instances -= 1
  76. def _state2_0_exec(self, parameters):
  77. association_name = parameters[0]
  78. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  79. def _state2_1_guard(self, parameters):
  80. return self.instances != 0
  81. def _state2_2_guard(self, parameters):
  82. return self.instances == 0
  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. new_instance = self.constructObject(0, 0, [])
  94. self.state.instances[new_instance.instance_id] = new_instance
  95. self.state.next_instance = self.state.next_instance + 1
  96. def constructObject(self, id, start_port_id, parameters):
  97. new_instance = MainAppInstance(self, id, start_port_id)
  98. return new_instance
  99. class AInstance(RuntimeClassBase):
  100. def __init__(self, atomdevs, id, start_port_id, instance_number):
  101. RuntimeClassBase.__init__(self, atomdevs, id)
  102. self.associations = {}
  103. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  104. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  105. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  106. self.semantics.priority = StatechartSemantics.SourceParent
  107. self.semantics.concurrency = StatechartSemantics.Single
  108. # build Statechart structure
  109. self.build_statechart_structure()
  110. # call user defined constructor
  111. AInstance.user_defined_constructor(self, instance_number)
  112. port_name = addInputPort("ui", start_port_id, True)
  113. atomdevs.addInPort(port_name)
  114. atomdevs.state.port_mappings[port_name] = id
  115. port_name = addInputPort("<narrow_cast>", start_port_id)
  116. atomdevs.addInPort(port_name)
  117. atomdevs.state.port_mappings[port_name] = id
  118. def user_defined_constructor(self, instance_number):
  119. self.number = instance_number
  120. def user_defined_destructor(self):
  121. pass
  122. # builds Statechart structure
  123. def build_statechart_structure(self):
  124. # state <root>
  125. self.states[""] = State(0, "", self)
  126. # state /state1
  127. self.states["/state1"] = State(1, "/state1", self)
  128. self.states["/state1"].setEnter(self._state1_enter)
  129. # add children
  130. self.states[""].addChild(self.states["/state1"])
  131. self.states[""].fixTree()
  132. self.states[""].default_state = self.states["/state1"]
  133. def _state1_enter(self):
  134. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), self.number]))
  135. self.big_step.outputEvent(Event("constructor_initialized_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), self.number]))
  136. def initializeStatechart(self):
  137. # enter default state
  138. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  139. RuntimeClassBase.initializeStatechart(self)
  140. class A(ClassBase):
  141. def __init__(self, name):
  142. ClassBase.__init__(self, name)
  143. self.input = self.addInPort("input")
  144. self.glob_outputs["ui"] = self.addOutPort("ui")
  145. def constructObject(self, id, start_port_id, parameters):
  146. new_instance = AInstance(self, id, start_port_id, parameters[1])
  147. return new_instance
  148. class Dummy(ObjectManagerState):
  149. def __init__(self):
  150. ObjectManagerState.__init__(self)
  151. def instantiate(self, class_name, construct_params):
  152. instance = {}
  153. instance["name"] = class_name
  154. if class_name == "MainApp":
  155. self.narrow_cast_id = self.narrow_cast_id + 0
  156. instance["associations"] = {}
  157. instance["associations"]["linkA"] = Association("A", 0, -1)
  158. elif class_name == "A":
  159. self.narrow_cast_id = self.narrow_cast_id + 0
  160. instance["associations"] = {}
  161. else:
  162. raise Exception("Cannot instantiate class " + class_name)
  163. return instance
  164. class ObjectManager(ObjectManagerBase):
  165. def __init__(self, name):
  166. ObjectManagerBase.__init__(self, name)
  167. self.state = Dummy()
  168. self.input = self.addInPort("input")
  169. self.output["MainApp"] = self.addOutPort()
  170. self.output["A"] = self.addOutPort()
  171. self.state.createInstance("MainApp", [])
  172. self.state.to_send.append((("MainApp", 0), ("MainApp", 0), Event("start_instance", None, ["MainApp[0]"])))
  173. class Controller(CoupledDEVS):
  174. def __init__(self, name):
  175. CoupledDEVS.__init__(self, name)
  176. self.in_ui = self.addInPort("ui")
  177. self.out_ui = self.addOutPort("ui")
  178. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  179. self.atomics = []
  180. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  181. self.atomics.append(self.addSubModel(A("A")))
  182. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  183. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  184. self.connectPorts(self.atomics[0].outputs["linkA"], self.atomics[1].input)
  185. self.connectPorts(self.atomics[1].obj_manager_out, self.objectmanager.input)
  186. self.connectPorts(self.objectmanager.output["A"], self.atomics[1].obj_manager_in)
  187. self.connectPorts(self.atomics[0].glob_outputs["ui"], self.out_ui)
  188. self.connectPorts(self.in_ui, self.atomics[0].input)
  189. self.connectPorts(self.atomics[1].glob_outputs["ui"], self.out_ui)
  190. self.connectPorts(self.in_ui, self.atomics[1].input)