target.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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: Dissasociate an instance
  5. Model description:
  6. Test 12: Dissasociate an instance
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "Dissasociate an 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("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.association_name = None
  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. self.states["/state3"].setEnter(self._state3_enter)
  46. # state /state4
  47. self.states["/state4"] = State(4, "/state4", self)
  48. self.states["/state4"].setEnter(self._state4_enter)
  49. # add children
  50. self.states[""].addChild(self.states["/state1"])
  51. self.states[""].addChild(self.states["/state2"])
  52. self.states[""].addChild(self.states["/state3"])
  53. self.states[""].addChild(self.states["/state4"])
  54. self.states[""].fixTree()
  55. self.states[""].default_state = self.states["/state1"]
  56. # transition /state1
  57. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  58. _state1_0.setAction(self._state1_0_exec)
  59. _state1_0.setTrigger(Event("instance_created", None))
  60. self.states["/state1"].addTransition(_state1_0)
  61. # transition /state2
  62. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state3"]])
  63. _state2_0.setAction(self._state2_0_exec)
  64. _state2_0.setTrigger(Event("instance_started", None))
  65. self.states["/state2"].addTransition(_state2_0)
  66. # transition /state3
  67. _state3_0 = Transition(self, self.states["/state3"], [self.states["/state4"]])
  68. _state3_0.setAction(self._state3_0_exec)
  69. _state3_0.setTrigger(Event("instance_disassociated", None))
  70. self.states["/state3"].addTransition(_state3_0)
  71. def _state1_enter(self):
  72. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  73. def _state3_enter(self):
  74. self.big_step.outputEventOM(Event("disassociate_instance", None, [self, "linkA"]))
  75. def _state4_enter(self):
  76. self.big_step.outputEventOM(Event("narrow_cast", None, [self, self.association_name, Event("sanity_check", None, [])]))
  77. def _state1_0_exec(self, parameters):
  78. association_name = parameters[0]
  79. self.association_name = association_name
  80. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  81. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  82. def _state2_0_exec(self, parameters):
  83. association_name = parameters[0]
  84. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  85. self.big_step.outputEventOM(Event("narrow_cast", None, [self, association_name, Event("link_check", None, [association_name])]))
  86. def _state3_0_exec(self, parameters):
  87. deleted_links = parameters[0]
  88. self.big_step.outputEvent(Event("instance_disassociated_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), deleted_links]))
  89. def initializeStatechart(self):
  90. # enter default state
  91. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  92. RuntimeClassBase.initializeStatechart(self)
  93. class MainApp(ClassBase):
  94. def __init__(self, name):
  95. ClassBase.__init__(self, name)
  96. self.input = self.addInPort("input")
  97. self.glob_outputs["ui"] = self.addOutPort("ui")
  98. self.outputs["linkA"] = self.addOutPort("linkA")
  99. new_instance = self.constructObject(0, 0, [])
  100. self.state.instances[new_instance.instance_id] = new_instance
  101. self.state.next_instance = self.state.next_instance + 1
  102. def constructObject(self, id, start_port_id, parameters):
  103. new_instance = MainAppInstance(self, id, start_port_id)
  104. return new_instance
  105. class AInstance(RuntimeClassBase):
  106. def __init__(self, atomdevs, id, start_port_id):
  107. RuntimeClassBase.__init__(self, atomdevs, id)
  108. self.associations = {}
  109. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  110. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  111. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  112. self.semantics.priority = StatechartSemantics.SourceParent
  113. self.semantics.concurrency = StatechartSemantics.Single
  114. # build Statechart structure
  115. self.build_statechart_structure()
  116. # call user defined constructor
  117. AInstance.user_defined_constructor(self)
  118. port_name = addInputPort("ui", start_port_id, True)
  119. atomdevs.addInPort(port_name)
  120. atomdevs.state.port_mappings[port_name] = id
  121. port_name = addInputPort("<narrow_cast>", start_port_id)
  122. atomdevs.addInPort(port_name)
  123. atomdevs.state.port_mappings[port_name] = id
  124. def user_defined_constructor(self):
  125. pass
  126. def user_defined_destructor(self):
  127. pass
  128. # builds Statechart structure
  129. def build_statechart_structure(self):
  130. # state <root>
  131. self.states[""] = State(0, "", self)
  132. # state /state1
  133. self.states["/state1"] = State(1, "/state1", self)
  134. self.states["/state1"].setEnter(self._state1_enter)
  135. # state /state2
  136. self.states["/state2"] = State(2, "/state2", self)
  137. # add children
  138. self.states[""].addChild(self.states["/state1"])
  139. self.states[""].addChild(self.states["/state2"])
  140. self.states[""].fixTree()
  141. self.states[""].default_state = self.states["/state1"]
  142. # transition /state1
  143. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  144. _state1_0.setTrigger(None)
  145. self.states["/state1"].addTransition(_state1_0)
  146. # transition /state2
  147. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  148. _state2_0.setAction(self._state2_0_exec)
  149. _state2_0.setTrigger(Event("link_check", None))
  150. self.states["/state2"].addTransition(_state2_0)
  151. _state2_1 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  152. _state2_1.setAction(self._state2_1_exec)
  153. _state2_1.setTrigger(Event("sanity_check", None))
  154. self.states["/state2"].addTransition(_state2_1)
  155. def _state1_enter(self):
  156. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  157. def _state2_0_exec(self, parameters):
  158. link_name = parameters[0]
  159. self.big_step.outputEvent(Event("instance_linked_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), link_name]))
  160. def _state2_1_exec(self, parameters):
  161. self.big_step.outputEvent(Event("not_possible", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  162. def initializeStatechart(self):
  163. # enter default state
  164. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  165. RuntimeClassBase.initializeStatechart(self)
  166. class A(ClassBase):
  167. def __init__(self, name):
  168. ClassBase.__init__(self, name)
  169. self.input = self.addInPort("input")
  170. self.glob_outputs["ui"] = self.addOutPort("ui")
  171. def constructObject(self, id, start_port_id, parameters):
  172. new_instance = AInstance(self, id, start_port_id)
  173. return new_instance
  174. class Dummy(ObjectManagerState):
  175. def __init__(self):
  176. ObjectManagerState.__init__(self)
  177. def instantiate(self, class_name, construct_params):
  178. instance = {}
  179. instance["name"] = class_name
  180. if class_name == "MainApp":
  181. self.narrow_cast_id = self.narrow_cast_id + 0
  182. instance["associations"] = {}
  183. instance["associations"]["linkA"] = Association("A", 0, -1)
  184. elif class_name == "A":
  185. self.narrow_cast_id = self.narrow_cast_id + 0
  186. instance["associations"] = {}
  187. else:
  188. raise Exception("Cannot instantiate class " + class_name)
  189. return instance
  190. class ObjectManager(ObjectManagerBase):
  191. def __init__(self, name):
  192. ObjectManagerBase.__init__(self, name)
  193. self.state = Dummy()
  194. self.input = self.addInPort("input")
  195. self.output["MainApp"] = self.addOutPort()
  196. self.output["A"] = self.addOutPort()
  197. self.state.createInstance("MainApp", [])
  198. self.state.to_send.append((("MainApp", 0), ("MainApp", 0), Event("start_instance", None, ["MainApp[0]"])))
  199. class Controller(CoupledDEVS):
  200. def __init__(self, name):
  201. CoupledDEVS.__init__(self, name)
  202. self.in_ui = self.addInPort("ui")
  203. self.out_ui = self.addOutPort("ui")
  204. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  205. self.atomics = []
  206. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  207. self.atomics.append(self.addSubModel(A("A")))
  208. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  209. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  210. self.connectPorts(self.atomics[0].outputs["linkA"], self.atomics[1].input)
  211. self.connectPorts(self.atomics[1].obj_manager_out, self.objectmanager.input)
  212. self.connectPorts(self.objectmanager.output["A"], self.atomics[1].obj_manager_in)
  213. self.connectPorts(self.atomics[0].glob_outputs["ui"], self.out_ui)
  214. self.connectPorts(self.in_ui, self.atomics[0].input)
  215. self.connectPorts(self.atomics[1].glob_outputs["ui"], self.out_ui)
  216. self.connectPorts(self.in_ui, self.atomics[1].input)