sccd.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Tue Aug 02 14:25:10 2016
  4. Model author: Simon Van Mierlo
  5. Model name: Timer (Threaded Version)
  6. """
  7. from python_runtime.statecharts_core import *
  8. from python_runtime.libs.ui import ui
  9. from python_runtime.accurate_time import time
  10. # package "Timer (Threaded Version)"
  11. class MainApp(RuntimeClassBase):
  12. def __init__(self, controller):
  13. RuntimeClassBase.__init__(self, controller)
  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. MainApp.user_defined_constructor(self)
  23. def user_defined_constructor(self):
  24. pass
  25. def user_defined_destructor(self):
  26. pass
  27. # user defined method
  28. def update_timers(self):
  29. print 'SIMTIME = %.2f' % get_simulated_time()
  30. print 'ACTTIME = %.2f' % time()
  31. # builds Statechart structure
  32. def build_statechart_structure(self):
  33. # state /running
  34. self.states["/running"] = State(0, self)
  35. self.states["/running"].setEnter(self._running_enter)
  36. self.states["/running"].setExit(self._running_exit)
  37. # state /interrupted
  38. self.states["/interrupted"] = State(1, self)
  39. # state <root>
  40. self.states[""] = State(2, self)
  41. # add children
  42. self.states[""].addChild(self.states["/running"])
  43. self.states[""].addChild(self.states["/interrupted"])
  44. self.states[""].fixTree()
  45. self.states[""].default_state = self.states["/running"]
  46. # transition /running
  47. _running_0 = Transition(self, self.states["/running"], self.states["/running"])
  48. _running_0.setAction(self._running_0_exec)
  49. _running_0.trigger = Event("_0after")
  50. self.states["/running"].addTransition(_running_0)
  51. _running_1 = Transition(self, self.states["/running"], self.states["/interrupted"])
  52. _running_1.setAction(self._running_1_exec)
  53. _running_1.trigger = Event("interrupt", "input")
  54. self.states["/running"].addTransition(_running_1)
  55. # transition /interrupted
  56. _interrupted_0 = Transition(self, self.states["/interrupted"], self.states["/interrupted"])
  57. _interrupted_0.setAction(self._interrupted_0_exec)
  58. _interrupted_0.trigger = Event("interrupt", "input")
  59. self.states["/interrupted"].addTransition(_interrupted_0)
  60. _interrupted_1 = Transition(self, self.states["/interrupted"], self.states["/running"])
  61. _interrupted_1.setAction(self._interrupted_1_exec)
  62. _interrupted_1.trigger = Event("continue", "input")
  63. self.states["/interrupted"].addTransition(_interrupted_1)
  64. def _running_enter(self):
  65. self.addTimer(0, 0.05)
  66. def _running_exit(self):
  67. self.removeTimer(0)
  68. def _running_0_exec(self, parameters):
  69. self.update_timers()
  70. def _running_1_exec(self, parameters):
  71. self.update_timers()
  72. def _interrupted_0_exec(self, parameters):
  73. self.update_timers()
  74. def _interrupted_1_exec(self, parameters):
  75. self.update_timers()
  76. def initializeStatechart(self):
  77. # enter default state
  78. states = self.states["/running"].getEffectiveTargetStates()
  79. self.updateConfiguration(states)
  80. for state in states:
  81. if state.enter:
  82. state.enter()
  83. class ObjectManager(ObjectManagerBase):
  84. def __init__(self, controller):
  85. ObjectManagerBase.__init__(self, controller)
  86. def instantiate(self, class_name, construct_params):
  87. if class_name == "MainApp":
  88. instance = MainApp(self.controller)
  89. instance.associations = {}
  90. return instance
  91. class Controller(ThreadsControllerBase):
  92. def __init__(self, keep_running = None):
  93. if keep_running == None: keep_running = True
  94. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  95. self.addInputPort("input")
  96. self.object_manager.createInstance("MainApp", [])