after.py 4.8 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:24 2016
  4. Model author: Glenn De Jonghe
  5. Model name: TestAfter
  6. Model description:
  7. Used for testing the AFTER event.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "TestAfter"
  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
  32. self.states["/composite"] = State(1, self)
  33. # state /composite/state_1
  34. self.states["/composite/state_1"] = State(2, self)
  35. self.states["/composite/state_1"].setEnter(self._composite_state_1_enter)
  36. self.states["/composite/state_1"].setExit(self._composite_state_1_exit)
  37. # state /composite/state_2
  38. self.states["/composite/state_2"] = State(3, self)
  39. self.states["/composite/state_2"].setEnter(self._composite_state_2_enter)
  40. # state /composite/state_3
  41. self.states["/composite/state_3"] = State(4, self)
  42. self.states["/composite/state_3"].setEnter(self._composite_state_3_enter)
  43. # add children
  44. self.states[""].addChild(self.states["/composite"])
  45. self.states["/composite"].addChild(self.states["/composite/state_1"])
  46. self.states["/composite"].addChild(self.states["/composite/state_2"])
  47. self.states["/composite"].addChild(self.states["/composite/state_3"])
  48. self.states[""].fixTree()
  49. self.states[""].default_state = self.states["/composite"]
  50. self.states["/composite"].default_state = self.states["/composite/state_1"]
  51. # transition /composite/state_1
  52. _composite_state_1_0 = Transition(self, self.states["/composite/state_1"], [self.states["/composite/state_2"]])
  53. _composite_state_1_0.setTrigger(Event("_0after"))
  54. self.states["/composite/state_1"].addTransition(_composite_state_1_0)
  55. _composite_state_1_1 = Transition(self, self.states["/composite/state_1"], [self.states["/composite/state_3"]])
  56. _composite_state_1_1.setTrigger(Event("_1after"))
  57. self.states["/composite/state_1"].addTransition(_composite_state_1_1)
  58. def _composite_state_1_enter(self):
  59. self.addTimer(0, 0.1)
  60. self.addTimer(1, 0.2)
  61. def _composite_state_1_exit(self):
  62. self.removeTimer(0)
  63. self.removeTimer(1)
  64. def _composite_state_2_enter(self):
  65. self.big_step.outputEvent(Event("in_state_2", "test_output", []))
  66. def _composite_state_3_enter(self):
  67. self.big_step.outputEvent(Event("in_state_3", "test_output", []))
  68. def initializeStatechart(self):
  69. # enter default state
  70. states = self.states["/composite"].getEffectiveTargetStates()
  71. self.updateConfiguration(states)
  72. for state in states:
  73. if state.enter:
  74. state.enter()
  75. class ObjectManager(ObjectManagerBase):
  76. def __init__(self, controller):
  77. ObjectManagerBase.__init__(self, controller)
  78. def instantiate(self, class_name, construct_params):
  79. if class_name == "Class1":
  80. instance = Class1(self.controller)
  81. instance.associations = {}
  82. else:
  83. raise Exception("Cannot instantiate class " + class_name)
  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 = []
  102. expected_events = [[Event("in_state_2", "test_output", [])]]