02_take_one_queue.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 09 09:35:55 2016
  4. Model author: Herr Joeri Exelmans
  5. Model name: take_one_queue
  6. Model description:
  7. Internal event lifeline - Queue-semantics: Internal events are treated just like external events: They are added to the object's event queue and will be sensed in another big step. This way, a raised internal event will always be sensed at some point later in time, but it is possible that other (external) events in the object's event queue are treated first.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "take_one_queue"
  11. class c(RuntimeClassBase):
  12. def __init__(self, controller):
  13. RuntimeClassBase.__init__(self, controller)
  14. self.semantics.big_step_maximality = StatechartSemantics.TakeOne
  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. c.user_defined_constructor(self)
  23. def user_defined_constructor(self):
  24. pass
  25. def user_defined_destructor(self):
  26. pass
  27. # builds Statechart structure
  28. def build_statechart_structure(self):
  29. # state <root>
  30. self.states[""] = State(0, self)
  31. # state /a
  32. self.states["/a"] = State(1, self)
  33. self.states["/a"].setEnter(self._a_enter)
  34. # state /b
  35. self.states["/b"] = State(2, self)
  36. self.states["/b"].setEnter(self._b_enter)
  37. # state /c
  38. self.states["/c"] = State(3, self)
  39. self.states["/c"].setEnter(self._c_enter)
  40. # add children
  41. self.states[""].addChild(self.states["/a"])
  42. self.states[""].addChild(self.states["/b"])
  43. self.states[""].addChild(self.states["/c"])
  44. self.states[""].fixTree()
  45. self.states[""].default_state = self.states["/a"]
  46. # transition /a
  47. _a_0 = Transition(self, self.states["/a"], [self.states["/b"]])
  48. _a_0.setAction(self._a_0_exec)
  49. _a_0.setTrigger(Event("e", "in"))
  50. self.states["/a"].addTransition(_a_0)
  51. # transition /b
  52. _b_0 = Transition(self, self.states["/b"], [self.states["/c"]])
  53. _b_0.setTrigger(Event("f", None))
  54. self.states["/b"].addTransition(_b_0)
  55. def _a_enter(self):
  56. self.big_step.outputEvent(Event("entered_a", "out", []))
  57. def _b_enter(self):
  58. self.big_step.outputEvent(Event("entered_b", "out", []))
  59. def _c_enter(self):
  60. self.big_step.outputEvent(Event("entered_c", "out", []))
  61. def _a_0_exec(self, parameters):
  62. self.raiseInternalEvent(Event("f", None, []))
  63. def initializeStatechart(self):
  64. # enter default state
  65. self.default_targets = self.states["/a"].getEffectiveTargetStates()
  66. RuntimeClassBase.initializeStatechart(self)
  67. class ObjectManager(ObjectManagerBase):
  68. def __init__(self, controller):
  69. ObjectManagerBase.__init__(self, controller)
  70. def instantiate(self, class_name, construct_params):
  71. if class_name == "c":
  72. instance = c(self.controller)
  73. instance.associations = {}
  74. else:
  75. raise Exception("Cannot instantiate class " + class_name)
  76. return instance
  77. class Controller(ThreadsControllerBase):
  78. def __init__(self, keep_running = None, behind_schedule_callback = None):
  79. if keep_running == None: keep_running = True
  80. if behind_schedule_callback == None: behind_schedule_callback = None
  81. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  82. self.addInputPort("in")
  83. self.addOutputPort("out")
  84. self.object_manager.createInstance("c", [])
  85. class InputEvent:
  86. def __init__(self, name, port, parameters, time_offset):
  87. self.name = name
  88. self.port = port
  89. self.parameters = parameters
  90. self.time_offset = time_offset
  91. class Test:
  92. def __init__(self):
  93. pass
  94. input_events = [InputEvent("e", "in", [], 0.0)]
  95. expected_events = [[Event("entered_a", "out", [])], [Event("entered_b", "out", [])], [Event("entered_c", "out", [])]]