01_source_child.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Mon Aug 08 09:49:26 2016
  4. Model author: Herr Joeri Exelmans
  5. Model name: source_child
  6. Model description:
  7. 'Source Child' priority-semantics: If 2 transitions are enabled, and the source state of the first is an ancestor of the second, only the second is executed.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "source_child"
  11. class c(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.SourceChild
  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 /parent
  32. self.states["/parent"] = State(1, self)
  33. self.states["/parent"].setEnter(self._parent_enter)
  34. # state /parent/a
  35. self.states["/parent/a"] = State(2, self)
  36. self.states["/parent/a"].setEnter(self._parent_a_enter)
  37. # state /parent/b
  38. self.states["/parent/b"] = State(3, self)
  39. self.states["/parent/b"].setEnter(self._parent_b_enter)
  40. # state /c
  41. self.states["/c"] = State(4, self)
  42. self.states["/c"].setEnter(self._c_enter)
  43. # add children
  44. self.states[""].addChild(self.states["/parent"])
  45. self.states[""].addChild(self.states["/c"])
  46. self.states["/parent"].addChild(self.states["/parent/a"])
  47. self.states["/parent"].addChild(self.states["/parent/b"])
  48. self.states[""].fixTree()
  49. self.states[""].default_state = self.states["/parent"]
  50. self.states["/parent"].default_state = self.states["/parent/a"]
  51. # transition /parent/a
  52. _parent_a_0 = Transition(self, self.states["/parent/a"], [self.states["/parent/b"]])
  53. _parent_a_0.setTrigger(None)
  54. self.states["/parent/a"].addTransition(_parent_a_0)
  55. # transition /parent
  56. _parent_0 = Transition(self, self.states["/parent"], [self.states["/c"]])
  57. _parent_0.setTrigger(None)
  58. self.states["/parent"].addTransition(_parent_0)
  59. def _parent_enter(self):
  60. self.big_step.outputEvent(Event("entered_parent", "out", []))
  61. def _parent_a_enter(self):
  62. self.big_step.outputEvent(Event("entered_a", "out", []))
  63. def _parent_b_enter(self):
  64. self.big_step.outputEvent(Event("entered_b", "out", []))
  65. def _c_enter(self):
  66. self.big_step.outputEvent(Event("entered_c", "out", []))
  67. def initializeStatechart(self):
  68. # enter default state
  69. states = self.states["/parent"].getEffectiveTargetStates()
  70. self.updateConfiguration(states)
  71. for state in states:
  72. if state.enter:
  73. state.enter()
  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 == "c":
  79. instance = c(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):
  86. if keep_running == None: keep_running = True
  87. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  88. self.addInputPort("in")
  89. self.addOutputPort("out")
  90. self.object_manager.createInstance("c", [])
  91. class InputEvent:
  92. def __init__(self, name, port, parameters, time_offset):
  93. self.name = name
  94. self.port = port
  95. self.parameters = parameters
  96. self.time_offset = time_offset
  97. class Test:
  98. def __init__(self):
  99. pass
  100. input_events = []
  101. expected_events = [[Event("entered_parent", "out", []), Event("entered_a", "out", [])], [Event("entered_b", "out", []), Event("entered_c", "out", [])]]