guard.py 4.4 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: TestGuard
  6. Model description:
  7. Testing the guard.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "TestGuard"
  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. # user defined attributes
  22. self.member1 = 0
  23. # call user defined constructor
  24. Class1.user_defined_constructor(self)
  25. def user_defined_constructor(self):
  26. pass
  27. def user_defined_destructor(self):
  28. pass
  29. # builds Statechart structure
  30. def build_statechart_structure(self):
  31. # state <root>
  32. self.states[""] = State(0, self)
  33. # state /state_1
  34. self.states["/state_1"] = State(1, self)
  35. self.states["/state_1"].setEnter(self._state_1_enter)
  36. # state /state_2
  37. self.states["/state_2"] = State(2, self)
  38. # state /state_3
  39. self.states["/state_3"] = State(3, self)
  40. self.states["/state_3"].setEnter(self._state_3_enter)
  41. # add children
  42. self.states[""].addChild(self.states["/state_1"])
  43. self.states[""].addChild(self.states["/state_2"])
  44. self.states[""].addChild(self.states["/state_3"])
  45. self.states[""].fixTree()
  46. self.states[""].default_state = self.states["/state_1"]
  47. # transition /state_1
  48. _state_1_0 = Transition(self, self.states["/state_1"], [self.states["/state_2"]])
  49. _state_1_0.setTrigger(None)
  50. self.states["/state_1"].addTransition(_state_1_0)
  51. # transition /state_2
  52. _state_2_0 = Transition(self, self.states["/state_2"], [self.states["/state_1"]])
  53. _state_2_0.setTrigger(None)
  54. _state_2_0.setGuard(self._state_2_0_guard)
  55. self.states["/state_2"].addTransition(_state_2_0)
  56. _state_2_1 = Transition(self, self.states["/state_2"], [self.states["/state_3"]])
  57. _state_2_1.setTrigger(None)
  58. _state_2_1.setGuard(self._state_2_1_guard)
  59. self.states["/state_2"].addTransition(_state_2_1)
  60. def _state_1_enter(self):
  61. self.member1 = self.member1 + 1
  62. def _state_3_enter(self):
  63. self.big_step.outputEvent(Event("received", "test_output", [self.member1]))
  64. def _state_2_0_guard(self, parameters):
  65. return self.member1 < 3
  66. def _state_2_1_guard(self, parameters):
  67. return self.member1 >= 3
  68. def initializeStatechart(self):
  69. # enter default state
  70. states = self.states["/state_1"].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.addOutputPort("test_output")
  90. self.object_manager.createInstance("Class1", [])
  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("received", "test_output", [3])]]