history.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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:24 2016
  4. Model author: Glenn De Jonghe
  5. Model name: TestHistory
  6. Model description:
  7. Testing the History state.
  8. """
  9. from sccd.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.setTrigger(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. _state_3_0.setTrigger(None)
  60. self.states["/state_3"].addTransition(_state_3_0)
  61. # transition /composite_1
  62. _composite_1_0 = Transition(self, self.states["/composite_1"], [self.states["/state_3"]])
  63. _composite_1_0.setTrigger(Event("to_state_3", "test_input"))
  64. self.states["/composite_1"].addTransition(_composite_1_0)
  65. def _composite_1_state_1_enter(self):
  66. self.big_step.outputEvent(Event("in_state_1", "test_output", []))
  67. def _composite_1_state_2_enter(self):
  68. self.big_step.outputEvent(Event("in_state_2", "test_output", []))
  69. def _state_3_enter(self):
  70. self.big_step.outputEvent(Event("in_state_3", "test_output", []))
  71. def initializeStatechart(self):
  72. # enter default state
  73. states = self.states["/composite_1"].getEffectiveTargetStates()
  74. self.updateConfiguration(states)
  75. for state in states:
  76. if state.enter:
  77. state.enter()
  78. class ObjectManager(ObjectManagerBase):
  79. def __init__(self, controller):
  80. ObjectManagerBase.__init__(self, controller)
  81. def instantiate(self, class_name, construct_params):
  82. if class_name == "Class1":
  83. instance = Class1(self.controller)
  84. instance.associations = {}
  85. else:
  86. raise Exception("Cannot instantiate class " + class_name)
  87. return instance
  88. class Controller(ThreadsControllerBase):
  89. def __init__(self, keep_running = None):
  90. if keep_running == None: keep_running = True
  91. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  92. self.addInputPort("test_input")
  93. self.addOutputPort("test_output")
  94. self.object_manager.createInstance("Class1", [])
  95. class InputEvent:
  96. def __init__(self, name, port, parameters, time_offset):
  97. self.name = name
  98. self.port = port
  99. self.parameters = parameters
  100. self.time_offset = time_offset
  101. class Test:
  102. def __init__(self):
  103. pass
  104. input_events = [InputEvent("to_state_2", "test_input", [], 0.0), InputEvent("to_state_3", "test_input", [], 0.0)]
  105. 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", [])]]