target.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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
  5. Model description:
  6. Test 8: Check if an instance is created and started successfully with constructor parameters (other than the main app)
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "Create and Start Instance"
  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("Inport", 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. pass
  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. # add children
  44. self.states[""].addChild(self.states["/state1"])
  45. self.states[""].addChild(self.states["/state2"])
  46. self.states[""].fixTree()
  47. self.states[""].default_state = self.states["/state1"]
  48. # transition /state1
  49. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  50. _state1_0.setAction(self._state1_0_exec)
  51. _state1_0.setTrigger(Event("instance_created", None))
  52. self.states["/state1"].addTransition(_state1_0)
  53. # transition /state2
  54. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  55. _state2_0.setAction(self._state2_0_exec)
  56. _state2_0.setTrigger(Event("instance_started", None))
  57. self.states["/state2"].addTransition(_state2_0)
  58. def _state1_enter(self):
  59. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A", 1, 3.14, "test", [1, 2, 3], {"1": 1, "2": 2, "3": 3}]))
  60. def _state1_0_exec(self, parameters):
  61. association_name = parameters[0]
  62. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("Outport"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  63. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  64. def _state2_0_exec(self, parameters):
  65. association_name = parameters[0]
  66. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("Outport"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  67. def initializeStatechart(self):
  68. # enter default state
  69. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  70. RuntimeClassBase.initializeStatechart(self)
  71. class MainApp(ClassBase):
  72. def __init__(self, name):
  73. ClassBase.__init__(self, name)
  74. self.input = self.addInPort("input")
  75. self.glob_outputs["Outport"] = self.addOutPort("Outport")
  76. self.outputs["linkA"] = self.addOutPort("linkA")
  77. new_instance = self.constructObject(0, 0, [])
  78. self.state.instances[new_instance.instance_id] = new_instance
  79. self.state.next_instance = self.state.next_instance + 1
  80. def constructObject(self, id, start_port_id, parameters):
  81. new_instance = MainAppInstance(self, id, start_port_id)
  82. return new_instance
  83. class AInstance(RuntimeClassBase):
  84. def __init__(self, atomdevs, id, start_port_id, integer, floating_point, astring, alist, adict):
  85. RuntimeClassBase.__init__(self, atomdevs, id)
  86. self.associations = {}
  87. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  88. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  89. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  90. self.semantics.priority = StatechartSemantics.SourceParent
  91. self.semantics.concurrency = StatechartSemantics.Single
  92. # build Statechart structure
  93. self.build_statechart_structure()
  94. # call user defined constructor
  95. AInstance.user_defined_constructor(self, integer, floating_point, astring, alist, adict)
  96. port_name = addInputPort("Inport", start_port_id, True)
  97. atomdevs.addInPort(port_name)
  98. atomdevs.state.port_mappings[port_name] = id
  99. port_name = addInputPort("<narrow_cast>", start_port_id)
  100. atomdevs.addInPort(port_name)
  101. atomdevs.state.port_mappings[port_name] = id
  102. def user_defined_constructor(self, integer, floating_point, astring, alist, adict):
  103. self.integer = integer
  104. self.floating_point = floating_point
  105. self.astring = astring
  106. self.alist = alist
  107. self.adict = adict
  108. def user_defined_destructor(self):
  109. pass
  110. # builds Statechart structure
  111. def build_statechart_structure(self):
  112. # state <root>
  113. self.states[""] = State(0, "", self)
  114. # state /state1
  115. self.states["/state1"] = State(1, "/state1", self)
  116. self.states["/state1"].setEnter(self._state1_enter)
  117. # add children
  118. self.states[""].addChild(self.states["/state1"])
  119. self.states[""].fixTree()
  120. self.states[""].default_state = self.states["/state1"]
  121. def _state1_enter(self):
  122. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("Outport"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  123. self.big_step.outputEvent(Event("constructor_initialized_succesfully", self.getOutPortName("Outport"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), self.integer, self.floating_point, self.astring, self.alist, self.adict]))
  124. def initializeStatechart(self):
  125. # enter default state
  126. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  127. RuntimeClassBase.initializeStatechart(self)
  128. class A(ClassBase):
  129. def __init__(self, name):
  130. ClassBase.__init__(self, name)
  131. self.input = self.addInPort("input")
  132. self.glob_outputs["Outport"] = self.addOutPort("Outport")
  133. def constructObject(self, id, start_port_id, parameters):
  134. new_instance = AInstance(self, id, start_port_id, parameters[1], parameters[2], parameters[3], parameters[4], parameters[5])
  135. return new_instance
  136. class Dummy(ObjectManagerState):
  137. def __init__(self):
  138. ObjectManagerState.__init__(self)
  139. def instantiate(self, class_name, construct_params):
  140. instance = {}
  141. instance["name"] = class_name
  142. if class_name == "MainApp":
  143. self.narrow_cast_id = self.narrow_cast_id + 0
  144. instance["associations"] = {}
  145. instance["associations"]["linkA"] = Association("A", 0, -1)
  146. elif class_name == "A":
  147. self.narrow_cast_id = self.narrow_cast_id + 0
  148. instance["associations"] = {}
  149. else:
  150. raise Exception("Cannot instantiate class " + class_name)
  151. return instance
  152. class ObjectManager(ObjectManagerBase):
  153. def __init__(self, name):
  154. ObjectManagerBase.__init__(self, name)
  155. self.state = Dummy()
  156. self.input = self.addInPort("input")
  157. self.output["MainApp"] = self.addOutPort()
  158. self.output["A"] = self.addOutPort()
  159. self.state.createInstance("MainApp", [])
  160. self.state.to_send.append((("MainApp", 0), ("MainApp", 0), Event("start_instance", None, ["MainApp[0]"])))
  161. class Controller(CoupledDEVS):
  162. def __init__(self, name):
  163. CoupledDEVS.__init__(self, name)
  164. self.in_Inport = self.addInPort("Inport")
  165. self.out_Outport = self.addOutPort("Outport")
  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["Outport"], self.out_Outport)
  176. self.connectPorts(self.in_Inport, self.atomics[0].input)
  177. self.connectPorts(self.atomics[1].glob_outputs["Outport"], self.out_Outport)
  178. self.connectPorts(self.in_Inport, self.atomics[1].input)