target.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: Simon Van Mierlo
  4. Model name: Timer (Threaded Version)
  5. """
  6. from sccd.runtime.DEVS_statecharts_core import *
  7. import time
  8. # package "Timer (Threaded Version)"
  9. class MainAppInstance(RuntimeClassBase):
  10. def __init__(self, atomdevs):
  11. RuntimeClassBase.__init__(self, atomdevs)
  12. self.associations = {}
  13. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  14. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  15. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  16. self.semantics.priority = StatechartSemantics.SourceParent
  17. self.semantics.concurrency = StatechartSemantics.Single
  18. # build Statechart structure
  19. self.build_statechart_structure()
  20. # call user defined constructor
  21. MainAppInstance.user_defined_constructor(self)
  22. def user_defined_constructor(self):
  23. self.starting_time = time.time()
  24. def user_defined_destructor(self):
  25. pass
  26. # builds Statechart structure
  27. def build_statechart_structure(self):
  28. # state <root>
  29. self.states[""] = State(0, "", self)
  30. # state /running
  31. self.states["/running"] = State(1, "/running", self)
  32. self.states["/running"].setEnter(self._running_enter)
  33. self.states["/running"].setExit(self._running_exit)
  34. # state /interrupted
  35. self.states["/interrupted"] = State(2, "/interrupted", self)
  36. # add children
  37. self.states[""].addChild(self.states["/running"])
  38. self.states[""].addChild(self.states["/interrupted"])
  39. self.states[""].fixTree()
  40. self.states[""].default_state = self.states["/running"]
  41. # transition /running
  42. _running_0 = Transition(self, self.states["/running"], [self.states["/running"]])
  43. _running_0.setAction(self._running_0_exec)
  44. _running_0.setTrigger(Event("_0after"))
  45. self.states["/running"].addTransition(_running_0)
  46. _running_1 = Transition(self, self.states["/running"], [self.states["/interrupted"]])
  47. _running_1.setAction(self._running_1_exec)
  48. _running_1.setTrigger(Event("interrupt", self.getInPortName("input")))
  49. self.states["/running"].addTransition(_running_1)
  50. # transition /interrupted
  51. _interrupted_0 = Transition(self, self.states["/interrupted"], [self.states["/interrupted"]])
  52. _interrupted_0.setAction(self._interrupted_0_exec)
  53. _interrupted_0.setTrigger(Event("interrupt", self.getInPortName("input")))
  54. self.states["/interrupted"].addTransition(_interrupted_0)
  55. _interrupted_1 = Transition(self, self.states["/interrupted"], [self.states["/running"]])
  56. _interrupted_1.setAction(self._interrupted_1_exec)
  57. _interrupted_1.setTrigger(Event("continue", self.getInPortName("input")))
  58. self.states["/interrupted"].addTransition(_interrupted_1)
  59. def _running_enter(self):
  60. self.addTimer(0, 0.05)
  61. def _running_exit(self):
  62. self.removeTimer(0)
  63. def _running_0_exec(self, parameters):
  64. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  65. def _running_1_exec(self, parameters):
  66. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  67. def _interrupted_0_exec(self, parameters):
  68. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  69. def _interrupted_1_exec(self, parameters):
  70. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  71. def initializeStatechart(self):
  72. # enter default state
  73. self.default_targets = self.states["/running"].getEffectiveTargetStates()
  74. RuntimeClassBase.initializeStatechart(self)
  75. class MainApp(ObjectManagerBase):
  76. def __init__(self, name):
  77. ObjectManagerBase.__init__(self, name)
  78. self.input = self.addInPort("input")
  79. self.instances[self.next_instance] = MainAppInstance(self)
  80. self.next_instance = self.next_instance + 1
  81. def constructObject(self, parameters):
  82. new_instance = MainAppInstance(self)
  83. return new_instance
  84. class ObjectManagerState:
  85. def __init__(self):
  86. self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]
  87. class ObjectManager(TheObjectManager):
  88. def __init__(self, name):
  89. TheObjectManager.__init__(self, name)
  90. self.State = ObjectManagerState()
  91. self.input = self.addInPort("input")
  92. self.output["MainApp"] = self.addOutPort()
  93. class Controller(CoupledDEVS):
  94. def __init__(self, name):
  95. CoupledDEVS.__init__(self, name)
  96. self.input = self.addInPort("input")
  97. self.addOutPort("output")
  98. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  99. self.atomic0 = self.addSubModel(MainApp("MainApp"))
  100. self.connectPorts(self.atomic0.obj_manager_out, self.objectmanager.input)
  101. self.connectPorts(self.objectmanager.output["MainApp"], self.atomic0.obj_manager_in)