parallel.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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:25 2016
  4. Model author: Glenn De Jonghe
  5. Model name: TestParallel
  6. Model description:
  7. Testing parallelism.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "TestParallel"
  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 /parallel_1
  32. self.states["/parallel_1"] = ParallelState(1, self)
  33. # state /parallel_1/orthogonal_1
  34. self.states["/parallel_1/orthogonal_1"] = State(2, self)
  35. # state /parallel_1/orthogonal_1/state_1
  36. self.states["/parallel_1/orthogonal_1/state_1"] = State(3, self)
  37. self.states["/parallel_1/orthogonal_1/state_1"].setEnter(self._parallel_1_orthogonal_1_state_1_enter)
  38. # state /parallel_1/orthogonal_1/state_2
  39. self.states["/parallel_1/orthogonal_1/state_2"] = State(4, self)
  40. self.states["/parallel_1/orthogonal_1/state_2"].setEnter(self._parallel_1_orthogonal_1_state_2_enter)
  41. # state /parallel_1/orthogonal_2
  42. self.states["/parallel_1/orthogonal_2"] = State(5, self)
  43. # state /parallel_1/orthogonal_2/state_3
  44. self.states["/parallel_1/orthogonal_2/state_3"] = State(6, self)
  45. self.states["/parallel_1/orthogonal_2/state_3"].setEnter(self._parallel_1_orthogonal_2_state_3_enter)
  46. # state /parallel_1/orthogonal_2/state_4
  47. self.states["/parallel_1/orthogonal_2/state_4"] = State(7, self)
  48. self.states["/parallel_1/orthogonal_2/state_4"].setEnter(self._parallel_1_orthogonal_2_state_4_enter)
  49. # add children
  50. self.states[""].addChild(self.states["/parallel_1"])
  51. self.states["/parallel_1"].addChild(self.states["/parallel_1/orthogonal_1"])
  52. self.states["/parallel_1"].addChild(self.states["/parallel_1/orthogonal_2"])
  53. self.states["/parallel_1/orthogonal_1"].addChild(self.states["/parallel_1/orthogonal_1/state_1"])
  54. self.states["/parallel_1/orthogonal_1"].addChild(self.states["/parallel_1/orthogonal_1/state_2"])
  55. self.states["/parallel_1/orthogonal_2"].addChild(self.states["/parallel_1/orthogonal_2/state_3"])
  56. self.states["/parallel_1/orthogonal_2"].addChild(self.states["/parallel_1/orthogonal_2/state_4"])
  57. self.states[""].fixTree()
  58. self.states[""].default_state = self.states["/parallel_1"]
  59. self.states["/parallel_1/orthogonal_1"].default_state = self.states["/parallel_1/orthogonal_1/state_1"]
  60. self.states["/parallel_1/orthogonal_2"].default_state = self.states["/parallel_1/orthogonal_2/state_3"]
  61. # transition /parallel_1/orthogonal_1/state_1
  62. _parallel_1_orthogonal_1_state_1_0 = Transition(self, self.states["/parallel_1/orthogonal_1/state_1"], [self.states["/parallel_1/orthogonal_1/state_2"]])
  63. _parallel_1_orthogonal_1_state_1_0.setTrigger(Event("to_state_2", "test_input"))
  64. self.states["/parallel_1/orthogonal_1/state_1"].addTransition(_parallel_1_orthogonal_1_state_1_0)
  65. # transition /parallel_1/orthogonal_1/state_2
  66. _parallel_1_orthogonal_1_state_2_0 = Transition(self, self.states["/parallel_1/orthogonal_1/state_2"], [self.states["/parallel_1/orthogonal_1/state_1"]])
  67. _parallel_1_orthogonal_1_state_2_0.setTrigger(Event("to_state_1", "test_input"))
  68. self.states["/parallel_1/orthogonal_1/state_2"].addTransition(_parallel_1_orthogonal_1_state_2_0)
  69. # transition /parallel_1/orthogonal_2/state_3
  70. _parallel_1_orthogonal_2_state_3_0 = Transition(self, self.states["/parallel_1/orthogonal_2/state_3"], [self.states["/parallel_1/orthogonal_2/state_4"]])
  71. _parallel_1_orthogonal_2_state_3_0.setTrigger(Event("to_state_4", "test_input"))
  72. self.states["/parallel_1/orthogonal_2/state_3"].addTransition(_parallel_1_orthogonal_2_state_3_0)
  73. # transition /parallel_1/orthogonal_2/state_4
  74. _parallel_1_orthogonal_2_state_4_0 = Transition(self, self.states["/parallel_1/orthogonal_2/state_4"], [self.states["/parallel_1/orthogonal_2/state_3"]])
  75. _parallel_1_orthogonal_2_state_4_0.setTrigger(Event("to_state_3", "test_input"))
  76. self.states["/parallel_1/orthogonal_2/state_4"].addTransition(_parallel_1_orthogonal_2_state_4_0)
  77. def _parallel_1_orthogonal_1_state_1_enter(self):
  78. self.big_step.outputEvent(Event("in_state_1", "test_output", []))
  79. def _parallel_1_orthogonal_1_state_2_enter(self):
  80. self.big_step.outputEvent(Event("in_state_2", "test_output", []))
  81. def _parallel_1_orthogonal_2_state_3_enter(self):
  82. self.big_step.outputEvent(Event("in_state_3", "test_output", []))
  83. def _parallel_1_orthogonal_2_state_4_enter(self):
  84. self.big_step.outputEvent(Event("in_state_4", "test_output", []))
  85. def initializeStatechart(self):
  86. # enter default state
  87. states = self.states["/parallel_1"].getEffectiveTargetStates()
  88. self.updateConfiguration(states)
  89. for state in states:
  90. if state.enter:
  91. state.enter()
  92. class ObjectManager(ObjectManagerBase):
  93. def __init__(self, controller):
  94. ObjectManagerBase.__init__(self, controller)
  95. def instantiate(self, class_name, construct_params):
  96. if class_name == "Class1":
  97. instance = Class1(self.controller)
  98. instance.associations = {}
  99. else:
  100. raise Exception("Cannot instantiate class " + class_name)
  101. return instance
  102. class Controller(ThreadsControllerBase):
  103. def __init__(self, keep_running = None):
  104. if keep_running == None: keep_running = True
  105. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  106. self.addInputPort("test_input")
  107. self.addOutputPort("test_output")
  108. self.object_manager.createInstance("Class1", [])
  109. class InputEvent:
  110. def __init__(self, name, port, parameters, time_offset):
  111. self.name = name
  112. self.port = port
  113. self.parameters = parameters
  114. self.time_offset = time_offset
  115. class Test:
  116. def __init__(self):
  117. pass
  118. input_events = [InputEvent("to_state_2", "test_input", [], 0.0), InputEvent("to_state_4", "test_input", [], 0.0), InputEvent("to_state_1", "test_input", [], 0.0), InputEvent("to_state_2", "test_input", [], 0.0), InputEvent("to_state_3", "test_input", [], 0.0)]
  119. expected_events = [[Event("in_state_1", "test_output", []), Event("in_state_3", "test_output", [])], [Event("in_state_2", "test_output", [])], [Event("in_state_4", "test_output", [])], [Event("in_state_1", "test_output", [])], [Event("in_state_2", "test_output", [])], [Event("in_state_3", "test_output", [])]]