target.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: Global inport
  5. Model description:
  6. Test X: Test if global inport works
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "Global inport"
  10. class MainAppInstance(RuntimeClassBase):
  11. def __init__(self, atomdevs):
  12. RuntimeClassBase.__init__(self, atomdevs)
  13. self.associations = {}
  14. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  15. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  16. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  17. self.semantics.priority = StatechartSemantics.SourceParent
  18. self.semantics.concurrency = StatechartSemantics.Single
  19. # build Statechart structure
  20. self.build_statechart_structure()
  21. # call user defined constructor
  22. MainAppInstance.user_defined_constructor(self)
  23. port_name = Ports.addInputPort("<narrow_cast>", self)
  24. atomdevs.addInPort(port_name)
  25. def user_defined_constructor(self):
  26. pass
  27. def user_defined_destructor(self):
  28. pass
  29. # builds Statechart structure
  30. def build_statechart_structure(self):
  31. # state <root>
  32. self.states[""] = State(0, "", self)
  33. # state /state1
  34. self.states["/state1"] = State(1, "/state1", self)
  35. self.states["/state1"].setEnter(self._state1_enter)
  36. # state /state2
  37. self.states["/state2"] = State(2, "/state2", self)
  38. # add children
  39. self.states[""].addChild(self.states["/state1"])
  40. self.states[""].addChild(self.states["/state2"])
  41. self.states[""].fixTree()
  42. self.states[""].default_state = self.states["/state1"]
  43. # transition /state1
  44. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  45. _state1_0.setAction(self._state1_0_exec)
  46. _state1_0.setTrigger(Event("test", None))
  47. self.states["/state1"].addTransition(_state1_0)
  48. def _state1_enter(self):
  49. self.big_step.outputEvent(Event("generate_input", self.getOutPortName("Output"), []))
  50. def _state1_0_exec(self, parameters):
  51. self.big_step.outputEvent(Event("received", self.getOutPortName("Output"), []))
  52. def initializeStatechart(self):
  53. # enter default state
  54. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  55. RuntimeClassBase.initializeStatechart(self)
  56. class MainApp(ClassBase):
  57. def __init__(self, name):
  58. ClassBase.__init__(self, name)
  59. self.input = self.addInPort("input")
  60. self.glob_outputs["Output"] = self.addOutPort("Output")
  61. self.state.instances[self.state.next_instance] = MainAppInstance(self)
  62. self.state.next_instance = self.state.next_instance + 1
  63. def constructObject(self, parameters):
  64. new_instance = MainAppInstance(self)
  65. return new_instance
  66. class ObjectManagerState:
  67. def __init__(self):
  68. self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]
  69. class ObjectManager(ObjectManagerBase):
  70. def __init__(self, name):
  71. ObjectManagerBase.__init__(self, name)
  72. self.state = ObjectManagerState()
  73. self.input = self.addInPort("input")
  74. self.output["MainApp"] = self.addOutPort()
  75. class Controller(CoupledDEVS):
  76. def __init__(self, name):
  77. CoupledDEVS.__init__(self, name)
  78. self.in_Input = self.addInPort("Input")
  79. Ports.addInputPort("Input")
  80. self.out_Output = self.addOutPort("Output")
  81. Ports.addOutputPort("Output")
  82. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  83. self.atomics = []
  84. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  85. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  86. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  87. self.connectPorts(self.atomics[0].glob_outputs["Output"], self.out_Output)
  88. self.connectPorts(self.in_Input, self.atomics[0].input)