target.py 4.5 KB

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