target.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 Instance
  5. Model description:
  6. Test 6: Check if an instance in created successfully (other than the main app, without parameters)
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "Create Instance"
  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. pass
  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. # add children
  40. self.states[""].addChild(self.states["/state1"])
  41. self.states[""].addChild(self.states["/state2"])
  42. self.states[""].fixTree()
  43. self.states[""].default_state = self.states["/state1"]
  44. # transition /state1
  45. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  46. _state1_0.setAction(self._state1_0_exec)
  47. _state1_0.setTrigger(Event("instance_created", None))
  48. self.states["/state1"].addTransition(_state1_0)
  49. def _state1_enter(self):
  50. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  51. def _state1_0_exec(self, parameters):
  52. association_name = parameters[0]
  53. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  54. def initializeStatechart(self):
  55. # enter default state
  56. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  57. RuntimeClassBase.initializeStatechart(self)
  58. class MainApp(ClassBase):
  59. def __init__(self, name):
  60. ClassBase.__init__(self, name)
  61. self.input = self.addInPort("input")
  62. self.glob_outputs["ui"] = self.addOutPort("ui")
  63. self.outputs["linkA"] = self.addOutPort("linkA")
  64. self.state.instances[self.state.next_instance] = MainAppInstance(self)
  65. self.state.next_instance = self.state.next_instance + 1
  66. def constructObject(self, parameters):
  67. new_instance = MainAppInstance(self)
  68. return new_instance
  69. class AInstance(RuntimeClassBase):
  70. def __init__(self, atomdevs):
  71. RuntimeClassBase.__init__(self, atomdevs)
  72. self.associations = {}
  73. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  74. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  75. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  76. self.semantics.priority = StatechartSemantics.SourceParent
  77. self.semantics.concurrency = StatechartSemantics.Single
  78. # build Statechart structure
  79. self.build_statechart_structure()
  80. # call user defined constructor
  81. AInstance.user_defined_constructor(self)
  82. port_name = Ports.addInputPort("<narrow_cast>", self)
  83. atomdevs.addInPort(port_name)
  84. def user_defined_constructor(self):
  85. pass
  86. def user_defined_destructor(self):
  87. pass
  88. # builds Statechart structure
  89. def build_statechart_structure(self):
  90. # state <root>
  91. self.states[""] = State(0, "", self)
  92. # state /state1
  93. self.states["/state1"] = State(1, "/state1", self)
  94. self.states["/state1"].setEnter(self._state1_enter)
  95. # add children
  96. self.states[""].addChild(self.states["/state1"])
  97. self.states[""].fixTree()
  98. self.states[""].default_state = self.states["/state1"]
  99. def _state1_enter(self):
  100. self.big_step.outputEvent(Event("not_possible", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  101. def initializeStatechart(self):
  102. # enter default state
  103. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  104. RuntimeClassBase.initializeStatechart(self)
  105. class A(ClassBase):
  106. def __init__(self, name):
  107. ClassBase.__init__(self, name)
  108. self.input = self.addInPort("input")
  109. self.glob_outputs["ui"] = self.addOutPort("ui")
  110. def constructObject(self, parameters):
  111. new_instance = AInstance(self)
  112. return new_instance
  113. class ObjectManagerState:
  114. def __init__(self):
  115. self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]
  116. class ObjectManager(ObjectManagerBase):
  117. def __init__(self, name):
  118. ObjectManagerBase.__init__(self, name)
  119. self.state = ObjectManagerState()
  120. self.input = self.addInPort("input")
  121. self.output["MainApp"] = self.addOutPort()
  122. self.output["A"] = self.addOutPort()
  123. class Controller(CoupledDEVS):
  124. def __init__(self, name):
  125. CoupledDEVS.__init__(self, name)
  126. self.in_ui = self.addInPort("ui")
  127. Ports.addInputPort("ui")
  128. self.out_ui = self.addOutPort("ui")
  129. Ports.addOutputPort("ui")
  130. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  131. self.atomics = []
  132. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  133. self.atomics.append(self.addSubModel(A("A")))
  134. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  135. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  136. self.connectPorts(self.atomics[0].outputs["linkA"], self.atomics[1].input)
  137. self.connectPorts(self.atomics[1].obj_manager_out, self.objectmanager.input)
  138. self.connectPorts(self.objectmanager.output["A"], self.atomics[1].obj_manager_in)
  139. self.connectPorts(self.atomics[0].glob_outputs["ui"], self.out_ui)
  140. self.connectPorts(self.in_ui, self.atomics[0].input)
  141. self.connectPorts(self.atomics[1].glob_outputs["ui"], self.out_ui)
  142. self.connectPorts(self.in_ui, self.atomics[1].input)