target.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 (Eventloop Version)
  5. """
  6. from sccd.runtime.statecharts_core import *
  7. from sccd.runtime.libs.ui import ui
  8. from time import time
  9. CANVAS_WIDTH = 500
  10. CANVAS_HEIGHT = 500
  11. FONT_SIZE = 50
  12. # package "Timer (Eventloop Version)"
  13. class MainApp(RuntimeClassBase):
  14. def __init__(self, controller):
  15. RuntimeClassBase.__init__(self, controller)
  16. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  17. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  18. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  19. self.semantics.priority = StatechartSemantics.SourceParent
  20. self.semantics.concurrency = StatechartSemantics.Single
  21. # build Statechart structure
  22. self.build_statechart_structure()
  23. # call user defined constructor
  24. MainApp.user_defined_constructor(self)
  25. def user_defined_constructor(self):
  26. self.canvas = ui.append_canvas(ui.window,500,500,{'background':'#222222'})
  27. self.clock_text = self.canvas.element.create_text(
  28. CANVAS_WIDTH / 2,
  29. CANVAS_HEIGHT / 2,
  30. text='0.0',
  31. anchor='center',
  32. font=("TkDefaultFont", FONT_SIZE)
  33. )
  34. self.actual_clock_text = self.canvas.element.create_text(
  35. CANVAS_WIDTH / 2,
  36. (CANVAS_HEIGHT / 2) + FONT_SIZE,
  37. text='0.0',
  38. anchor='center',
  39. font=("TkDefaultFont", FONT_SIZE)
  40. )
  41. interrupt_button = ui.append_button(ui.window, 'INTERRUPT');
  42. continue_button = ui.append_button(ui.window, 'CONTINUE');
  43. ui.bind_event(interrupt_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'interrupt_clicked');
  44. ui.bind_event(continue_button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'continue_clicked');
  45. def user_defined_destructor(self):
  46. pass
  47. # user defined method
  48. def update_timers(self):
  49. self.canvas.element.itemconfigure(self.clock_text, text=str('%.2f' % (self.getSimulatedTime() / 1000.0)))
  50. self.canvas.element.itemconfigure(self.actual_clock_text, text='%.2f' % (time() / 1000.0))
  51. # builds Statechart structure
  52. def build_statechart_structure(self):
  53. # state <root>
  54. self.states[""] = State(0, "", self)
  55. # state /running
  56. self.states["/running"] = State(1, "/running", self)
  57. self.states["/running"].setEnter(self._running_enter)
  58. self.states["/running"].setExit(self._running_exit)
  59. # state /interrupted
  60. self.states["/interrupted"] = State(2, "/interrupted", self)
  61. # add children
  62. self.states[""].addChild(self.states["/running"])
  63. self.states[""].addChild(self.states["/interrupted"])
  64. self.states[""].fixTree()
  65. self.states[""].default_state = self.states["/running"]
  66. # transition /running
  67. _running_0 = Transition(self, self.states["/running"], [self.states["/running"]])
  68. _running_0.setAction(self._running_0_exec)
  69. _running_0.setTrigger(Event("_0after"))
  70. self.states["/running"].addTransition(_running_0)
  71. _running_1 = Transition(self, self.states["/running"], [self.states["/interrupted"]])
  72. _running_1.setAction(self._running_1_exec)
  73. _running_1.setTrigger(Event("interrupt_clicked", self.getInPortName("ui")))
  74. self.states["/running"].addTransition(_running_1)
  75. # transition /interrupted
  76. _interrupted_0 = Transition(self, self.states["/interrupted"], [self.states["/interrupted"]])
  77. _interrupted_0.setAction(self._interrupted_0_exec)
  78. _interrupted_0.setTrigger(Event("interrupt_clicked", self.getInPortName("ui")))
  79. self.states["/interrupted"].addTransition(_interrupted_0)
  80. _interrupted_1 = Transition(self, self.states["/interrupted"], [self.states["/running"]])
  81. _interrupted_1.setAction(self._interrupted_1_exec)
  82. _interrupted_1.setTrigger(Event("continue_clicked", self.getInPortName("ui")))
  83. self.states["/interrupted"].addTransition(_interrupted_1)
  84. def _running_enter(self):
  85. self.addTimer(0, 0.05)
  86. def _running_exit(self):
  87. self.removeTimer(0)
  88. def _running_0_exec(self, parameters):
  89. self.update_timers()
  90. def _running_1_exec(self, parameters):
  91. self.update_timers()
  92. def _interrupted_0_exec(self, parameters):
  93. self.update_timers()
  94. def _interrupted_1_exec(self, parameters):
  95. self.update_timers()
  96. def initializeStatechart(self):
  97. # enter default state
  98. self.default_targets = self.states["/running"].getEffectiveTargetStates()
  99. RuntimeClassBase.initializeStatechart(self)
  100. class ObjectManager(ObjectManagerBase):
  101. def __init__(self, controller):
  102. ObjectManagerBase.__init__(self, controller)
  103. def instantiate(self, class_name, construct_params):
  104. if class_name == "MainApp":
  105. instance = MainApp(self.controller)
  106. instance.associations = {}
  107. else:
  108. raise Exception("Cannot instantiate class " + class_name)
  109. return instance
  110. class Controller(EventLoopControllerBase):
  111. def __init__(self, event_loop_callbacks, finished_callback = None, behind_schedule_callback = None):
  112. if finished_callback == None: finished_callback = None
  113. if behind_schedule_callback == None: behind_schedule_callback = None
  114. EventLoopControllerBase.__init__(self, ObjectManager(self), event_loop_callbacks, finished_callback, behind_schedule_callback)
  115. self.addInputPort("ui")
  116. self.object_manager.createInstance("MainApp", [])