history.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Wed Aug 03 16:10:23 2016
  4. Model author: Glenn De Jonghe
  5. Model name: TestHistory
  6. Model description:
  7. Testing the History state.
  8. """
  9. from python_runtime.statecharts_core import *
  10. # package "TestHistory"
  11. class Class1(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. Class1.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 /composite_1
  32. self.states["/composite_1"] = State(1, self)
  33. # state /composite_1/state_1
  34. self.states["/composite_1/state_1"] = State(2, self)
  35. self.states["/composite_1/state_1"].setEnter(self._composite_1_state_1_enter)
  36. # state /composite_1/state_2
  37. self.states["/composite_1/state_2"] = State(3, self)
  38. self.states["/composite_1/state_2"].setEnter(self._composite_1_state_2_enter)
  39. # state /composite_1/composite_history
  40. self.states["/composite_1/composite_history"] = ShallowHistoryState(4, self)
  41. # state /state_3
  42. self.states["/state_3"] = State(5, self)
  43. self.states["/state_3"].setEnter(self._state_3_enter)
  44. # add children
  45. self.states[""].addChild(self.states["/composite_1"])
  46. self.states[""].addChild(self.states["/state_3"])
  47. self.states["/composite_1"].addChild(self.states["/composite_1/state_1"])
  48. self.states["/composite_1"].addChild(self.states["/composite_1/state_2"])
  49. self.states["/composite_1"].addChild(self.states["/composite_1/composite_history"])
  50. self.states[""].fixTree()
  51. self.states[""].default_state = self.states["/composite_1"]
  52. self.states["/composite_1"].default_state = self.states["/composite_1/state_1"]
  53. # transition /composite_1/state_1
  54. _composite_1_state_1_0 = Transition(self, self.states["/composite_1/state_1"], [self.states["/composite_1/state_2"]])
  55. _composite_1_state_1_0.trigger = Event("to_state_2", "test_input")
  56. self.states["/composite_1/state_1"].addTransition(_composite_1_state_1_0)
  57. # transition /state_3
  58. _state_3_0 = Transition(self, self.states["/state_3"], [self.states["/composite_1/composite_history"]])
  59. self.states["/state_3"].addTransition(_state_3_0)
  60. # transition /composite_1
  61. _composite_1_0 = Transition(self, self.states["/composite_1"], [self.states["/state_3"]])
  62. _composite_1_0.trigger = Event("to_state_3", "test_input")
  63. self.states["/composite_1"].addTransition(_composite_1_0)
  64. def _composite_1_state_1_enter(self):
  65. self.big_step.outputEvent(Event("in_state_1", "test_output", []))
  66. def _composite_1_state_2_enter(self):
  67. self.big_step.outputEvent(Event("in_state_2", "test_output", []))
  68. def _state_3_enter(self):
  69. self.big_step.outputEvent(Event("in_state_3", "test_output", []))
  70. def initializeStatechart(self):
  71. # enter default state
  72. states = self.states["/composite_1"].getEffectiveTargetStates()
  73. self.updateConfiguration(states)
  74. for state in states:
  75. if state.enter:
  76. state.enter()
  77. class ObjectManager(ObjectManagerBase):
  78. def __init__(self, controller):
  79. ObjectManagerBase.__init__(self, controller)
  80. def instantiate(self, class_name, construct_params):
  81. if class_name == "Class1":
  82. instance = Class1(self.controller)
  83. instance.associations = {}
  84. return instance
  85. class Controller(ThreadsControllerBase):
  86. def __init__(self, keep_running = None):
  87. if keep_running == None: keep_running = True
  88. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  89. self.addInputPort("test_input")
  90. self.addOutputPort("test_output")
  91. self.object_manager.createInstance("Class1", [])
  92. class InputEvent:
  93. def __init__(self, name, port, parameters, time_offset):
  94. self.name = name
  95. self.port = port
  96. self.parameters = parameters
  97. self.time_offset = time_offset
  98. class Test:
  99. def __init__(self):
  100. pass
  101. input_events = [InputEvent("to_state_2", "test_input", [], 0.0), InputEvent("to_state_3", "test_input", [], 0.0)]
  102. expected_events = [[Event("in_state_1", "test_output", [])], [Event("in_state_2", "test_output", [])], [Event("in_state_3", "test_output", [])], [Event("in_state_2", "test_output", [])]]