target.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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):
  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.instances = 10
  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. # 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[""].fixTree()
  46. self.states[""].default_state = self.states["/state1"]
  47. # transition /state1
  48. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  49. _state1_0.setAction(self._state1_0_exec)
  50. _state1_0.setTrigger(Event("instance_created", None))
  51. self.states["/state1"].addTransition(_state1_0)
  52. # transition /state2
  53. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  54. _state2_0.setAction(self._state2_0_exec)
  55. _state2_0.setTrigger(Event("instance_started", None))
  56. self.states["/state2"].addTransition(_state2_0)
  57. _state2_1 = Transition(self, self.states["/state2"], [self.states["/state1"]])
  58. _state2_1.setTrigger(None)
  59. _state2_1.setGuard(self._state2_1_guard)
  60. self.states["/state2"].addTransition(_state2_1)
  61. _state2_2 = Transition(self, self.states["/state2"], [self.states["/state3"]])
  62. _state2_2.setTrigger(None)
  63. _state2_2.setGuard(self._state2_2_guard)
  64. self.states["/state2"].addTransition(_state2_2)
  65. def _state1_enter(self):
  66. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A", self.instances]))
  67. def _state1_0_exec(self, parameters):
  68. association_name = parameters[0]
  69. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  70. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  71. self.instances -= 1
  72. def _state2_0_exec(self, parameters):
  73. association_name = parameters[0]
  74. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  75. def _state2_1_guard(self, parameters):
  76. return self.instances != 0
  77. def _state2_2_guard(self, parameters):
  78. return self.instances == 0
  79. def initializeStatechart(self):
  80. # enter default state
  81. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  82. RuntimeClassBase.initializeStatechart(self)
  83. class MainApp(ClassBase):
  84. def __init__(self, name):
  85. ClassBase.__init__(self, name)
  86. self.input = self.addInPort("input")
  87. self.glob_outputs["ui"] = self.addOutPort("ui")
  88. self.outputs["linkA"] = self.addOutPort("linkA")
  89. self.state.instances[self.state.next_instance] = MainAppInstance(self)
  90. self.state.next_instance = self.state.next_instance + 1
  91. def constructObject(self, parameters):
  92. new_instance = MainAppInstance(self)
  93. return new_instance
  94. class AInstance(RuntimeClassBase):
  95. def __init__(self, atomdevs, instance_number):
  96. RuntimeClassBase.__init__(self, atomdevs)
  97. self.associations = {}
  98. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  99. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  100. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  101. self.semantics.priority = StatechartSemantics.SourceParent
  102. self.semantics.concurrency = StatechartSemantics.Single
  103. # build Statechart structure
  104. self.build_statechart_structure()
  105. # call user defined constructor
  106. AInstance.user_defined_constructor(self, instance_number)
  107. port_name = Ports.addInputPort("<narrow_cast>", self)
  108. atomdevs.addInPort(port_name)
  109. def user_defined_constructor(self, instance_number):
  110. self.number = instance_number
  111. def user_defined_destructor(self):
  112. pass
  113. # builds Statechart structure
  114. def build_statechart_structure(self):
  115. # state <root>
  116. self.states[""] = State(0, "", self)
  117. # state /state1
  118. self.states["/state1"] = State(1, "/state1", self)
  119. self.states["/state1"].setEnter(self._state1_enter)
  120. # add children
  121. self.states[""].addChild(self.states["/state1"])
  122. self.states[""].fixTree()
  123. self.states[""].default_state = self.states["/state1"]
  124. def _state1_enter(self):
  125. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), self.number]))
  126. self.big_step.outputEvent(Event("constructor_initialized_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), self.number]))
  127. def initializeStatechart(self):
  128. # enter default state
  129. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  130. RuntimeClassBase.initializeStatechart(self)
  131. class A(ClassBase):
  132. def __init__(self, name):
  133. ClassBase.__init__(self, name)
  134. self.input = self.addInPort("input")
  135. self.glob_outputs["ui"] = self.addOutPort("ui")
  136. def constructObject(self, parameters):
  137. new_instance = AInstance(self, parameters[2])
  138. return new_instance
  139. class ObjectManagerState:
  140. def __init__(self):
  141. self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]
  142. class ObjectManager(ObjectManagerBase):
  143. def __init__(self, name):
  144. ObjectManagerBase.__init__(self, name)
  145. self.state = ObjectManagerState()
  146. self.input = self.addInPort("input")
  147. self.output["MainApp"] = self.addOutPort()
  148. self.output["A"] = self.addOutPort()
  149. class Controller(CoupledDEVS):
  150. def __init__(self, name):
  151. CoupledDEVS.__init__(self, name)
  152. self.in_ui = self.addInPort("ui")
  153. Ports.addInputPort("ui")
  154. self.out_ui = self.addOutPort("ui")
  155. Ports.addOutputPort("ui")
  156. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  157. self.atomics = []
  158. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  159. self.atomics.append(self.addSubModel(A("A")))
  160. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  161. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  162. self.connectPorts(self.atomics[0].outputs["linkA"], self.atomics[1].input)
  163. self.connectPorts(self.atomics[1].obj_manager_out, self.objectmanager.input)
  164. self.connectPorts(self.objectmanager.output["A"], self.atomics[1].obj_manager_in)
  165. self.connectPorts(self.atomics[0].glob_outputs["ui"], self.out_ui)
  166. self.connectPorts(self.in_ui, self.atomics[0].input)
  167. self.connectPorts(self.atomics[1].glob_outputs["ui"], self.out_ui)
  168. self.connectPorts(self.in_ui, self.atomics[1].input)