00_no_statechart_py.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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: Herr Joeri Exelmans
  5. Model name: no_statechart
  6. Model description:
  7. Test to see what happens when a class with no statechart is defined.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "no_statechart"
  11. class my_struct:
  12. def __init__(self, x, y):
  13. # user defined attributes
  14. self.x = None
  15. self.y = None
  16. # call user defined constructor
  17. my_struct.user_defined_constructor(self, x, y)
  18. def user_defined_constructor(self, x, y):
  19. self.x = x
  20. self.y = y
  21. def user_defined_destructor(self):
  22. pass
  23. class my_class(RuntimeClassBase):
  24. def __init__(self, controller):
  25. RuntimeClassBase.__init__(self, controller)
  26. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  27. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  28. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  29. self.semantics.priority = StatechartSemantics.SourceParent
  30. self.semantics.concurrency = StatechartSemantics.Single
  31. # build Statechart structure
  32. self.build_statechart_structure()
  33. # call user defined constructor
  34. my_class.user_defined_constructor(self)
  35. def user_defined_constructor(self):
  36. self.data = my_struct(0, 0)
  37. def user_defined_destructor(self):
  38. pass
  39. # builds Statechart structure
  40. def build_statechart_structure(self):
  41. # state <root>
  42. self.states[""] = State(0, self)
  43. # state /a
  44. self.states["/a"] = State(1, self)
  45. self.states["/a"].setEnter(self._a_enter)
  46. self.states["/a"].setExit(self._a_exit)
  47. # state /b
  48. self.states["/b"] = State(2, self)
  49. self.states["/b"].setEnter(self._b_enter)
  50. self.states["/b"].setExit(self._b_exit)
  51. # add children
  52. self.states[""].addChild(self.states["/a"])
  53. self.states[""].addChild(self.states["/b"])
  54. self.states[""].fixTree()
  55. self.states[""].default_state = self.states["/a"]
  56. # transition /a
  57. _a_0 = Transition(self, self.states["/a"], [self.states["/b"]])
  58. _a_0.setAction(self._a_0_exec)
  59. _a_0.setTrigger(Event("_0after"))
  60. _a_0.setGuard(self._a_0_guard)
  61. self.states["/a"].addTransition(_a_0)
  62. # transition /b
  63. _b_0 = Transition(self, self.states["/b"], [self.states["/a"]])
  64. _b_0.setAction(self._b_0_exec)
  65. _b_0.setTrigger(Event("_1after"))
  66. _b_0.setGuard(self._b_0_guard)
  67. self.states["/b"].addTransition(_b_0)
  68. def _a_enter(self):
  69. self.addTimer(0, 0.1)
  70. def _a_exit(self):
  71. self.removeTimer(0)
  72. def _b_enter(self):
  73. self.addTimer(1, 0.1)
  74. def _b_exit(self):
  75. self.removeTimer(1)
  76. def _a_0_exec(self, parameters):
  77. self.big_step.outputEvent(Event("to_b", "out", [self.data.x]))
  78. self.data.x += 1
  79. def _a_0_guard(self, parameters):
  80. return self.data.x < 2
  81. def _b_0_exec(self, parameters):
  82. self.data.y += 1
  83. self.big_step.outputEvent(Event("to_a", "out", [self.data.y]))
  84. def _b_0_guard(self, parameters):
  85. return self.data.y < 2
  86. def initializeStatechart(self):
  87. # enter default state
  88. states = self.states["/a"].getEffectiveTargetStates()
  89. self.updateConfiguration(states)
  90. for state in states:
  91. if state.enter:
  92. state.enter()
  93. class ObjectManager(ObjectManagerBase):
  94. def __init__(self, controller):
  95. ObjectManagerBase.__init__(self, controller)
  96. def instantiate(self, class_name, construct_params):
  97. if class_name == "my_struct":
  98. instance = my_struct(self.controller, construct_params[0], construct_params[1])
  99. instance.associations = {}
  100. elif class_name == "my_class":
  101. instance = my_class(self.controller)
  102. instance.associations = {}
  103. else:
  104. raise Exception("Cannot instantiate class " + class_name)
  105. return instance
  106. class Controller(ThreadsControllerBase):
  107. def __init__(self, keep_running = None):
  108. if keep_running == None: keep_running = True
  109. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  110. self.addInputPort("in")
  111. self.addOutputPort("out")
  112. self.object_manager.createInstance("my_class", [])
  113. class InputEvent:
  114. def __init__(self, name, port, parameters, time_offset):
  115. self.name = name
  116. self.port = port
  117. self.parameters = parameters
  118. self.time_offset = time_offset
  119. class Test:
  120. def __init__(self):
  121. pass
  122. input_events = []
  123. expected_events = [[Event("to_b", "out", [0])], [Event("to_a", "out", [1])], [Event("to_b", "out", [1])], [Event("to_a", "out", [2])]]