target.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Model author: Simon Van Mierlo
  4. Model name: Timer (Threaded Version)
  5. """
  6. from sccd.runtime.statecharts_core import *
  7. import time
  8. # package "Timer (Threaded Version)"
  9. class MainApp(RuntimeClassBase):
  10. def __init__(self, controller):
  11. RuntimeClassBase.__init__(self, controller)
  12. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  13. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  14. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  15. self.semantics.priority = StatechartSemantics.SourceParent
  16. self.semantics.concurrency = StatechartSemantics.Single
  17. # build Statechart structure
  18. self.build_statechart_structure()
  19. # call user defined constructor
  20. MainApp.user_defined_constructor(self)
  21. def user_defined_constructor(self):
  22. self.starting_time = time.time()
  23. def user_defined_destructor(self):
  24. pass
  25. # builds Statechart structure
  26. def build_statechart_structure(self):
  27. # state <root>
  28. self.states[""] = State(0, "", self)
  29. # state /running
  30. self.states["/running"] = State(1, "/running", self)
  31. self.states["/running"].setEnter(self._running_enter)
  32. self.states["/running"].setExit(self._running_exit)
  33. # state /interrupted
  34. self.states["/interrupted"] = State(2, "/interrupted", self)
  35. # add children
  36. self.states[""].addChild(self.states["/running"])
  37. self.states[""].addChild(self.states["/interrupted"])
  38. self.states[""].fixTree()
  39. self.states[""].default_state = self.states["/running"]
  40. # transition /running
  41. _running_0 = Transition(self, self.states["/running"], [self.states["/running"]])
  42. _running_0.setAction(self._running_0_exec)
  43. _running_0.setTrigger(Event("_0after"))
  44. self.states["/running"].addTransition(_running_0)
  45. _running_1 = Transition(self, self.states["/running"], [self.states["/interrupted"]])
  46. _running_1.setAction(self._running_1_exec)
  47. _running_1.setTrigger(Event("interrupt", self.getInPortName("input")))
  48. self.states["/running"].addTransition(_running_1)
  49. # transition /interrupted
  50. _interrupted_0 = Transition(self, self.states["/interrupted"], [self.states["/interrupted"]])
  51. _interrupted_0.setAction(self._interrupted_0_exec)
  52. _interrupted_0.setTrigger(Event("interrupt", self.getInPortName("input")))
  53. self.states["/interrupted"].addTransition(_interrupted_0)
  54. _interrupted_1 = Transition(self, self.states["/interrupted"], [self.states["/running"]])
  55. _interrupted_1.setAction(self._interrupted_1_exec)
  56. _interrupted_1.setTrigger(Event("continue", self.getInPortName("input")))
  57. self.states["/interrupted"].addTransition(_interrupted_1)
  58. def _running_enter(self):
  59. self.addTimer(0, 0.05)
  60. def _running_exit(self):
  61. self.removeTimer(0)
  62. def _running_0_exec(self, parameters):
  63. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  64. def _running_1_exec(self, parameters):
  65. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  66. def _interrupted_0_exec(self, parameters):
  67. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  68. def _interrupted_1_exec(self, parameters):
  69. self.big_step.outputEvent(Event("time_update", self.getOutPortName("output"), [self.getSimulatedTime(), time.time() - self.starting_time]))
  70. def initializeStatechart(self):
  71. # enter default state
  72. self.default_targets = self.states["/running"].getEffectiveTargetStates()
  73. RuntimeClassBase.initializeStatechart(self)
  74. class ObjectManager(ObjectManagerBase):
  75. def __init__(self, controller):
  76. ObjectManagerBase.__init__(self, controller)
  77. def instantiate(self, class_name, construct_params):
  78. if class_name == "MainApp":
  79. instance = MainApp(self.controller)
  80. instance.associations = {}
  81. else:
  82. raise Exception("Cannot instantiate class " + class_name)
  83. return instance
  84. class Controller(ThreadsControllerBase):
  85. def __init__(self, keep_running = None, behind_schedule_callback = None):
  86. if keep_running == None: keep_running = True
  87. if behind_schedule_callback == None: behind_schedule_callback = None
  88. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  89. self.addInputPort("input")
  90. self.addOutputPort("output")
  91. self.object_manager.createInstance("MainApp", [])