after_0.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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: Yentl Van Tendeloo
  5. Model name: after_0
  6. Model description:
  7. Used for testing the AFTER(0) event---which should not block the deletion of the B instance.
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "after_0"
  11. class A(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. A.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 /x
  32. self.states["/x"] = State(1, self)
  33. self.states["/x"].setEnter(self._x_enter)
  34. # state /ready
  35. self.states["/ready"] = State(2, self)
  36. self.states["/ready"].setEnter(self._ready_enter)
  37. # state /done
  38. self.states["/done"] = State(3, self)
  39. # add children
  40. self.states[""].addChild(self.states["/x"])
  41. self.states[""].addChild(self.states["/ready"])
  42. self.states[""].addChild(self.states["/done"])
  43. self.states[""].fixTree()
  44. self.states[""].default_state = self.states["/x"]
  45. # transition /x
  46. _x_0 = Transition(self, self.states["/x"], [self.states["/ready"]])
  47. _x_0.setTrigger(Event("instance_created", None))
  48. self.states["/x"].addTransition(_x_0)
  49. # transition /ready
  50. _ready_0 = Transition(self, self.states["/ready"], [self.states["/done"]])
  51. _ready_0.setAction(self._ready_0_exec)
  52. _ready_0.setTrigger(Event("close", None))
  53. self.states["/ready"].addTransition(_ready_0)
  54. def _x_enter(self):
  55. self.big_step.outputEventOM(Event("create_instance", None, [self, 'child', 'B']))
  56. self.big_step.outputEvent(Event("creating_instance", "test_output", []))
  57. def _ready_enter(self):
  58. self.big_step.outputEventOM(Event("start_instance", None, [self, 'child[0]']))
  59. self.big_step.outputEvent(Event("starting_instance", "test_output", []))
  60. def _ready_0_exec(self, parameters):
  61. self.big_step.outputEventOM(Event("delete_instance", None, [self, 'child[0]']))
  62. self.big_step.outputEvent(Event("deleting_instance", "test_output", []))
  63. def initializeStatechart(self):
  64. # enter default state
  65. states = self.states["/x"].getEffectiveTargetStates()
  66. self.updateConfiguration(states)
  67. for state in states:
  68. if state.enter:
  69. state.enter()
  70. class B(RuntimeClassBase):
  71. def __init__(self, controller):
  72. RuntimeClassBase.__init__(self, controller)
  73. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  74. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  75. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  76. self.semantics.priority = StatechartSemantics.SourceParent
  77. self.semantics.concurrency = StatechartSemantics.Single
  78. # build Statechart structure
  79. self.build_statechart_structure()
  80. # call user defined constructor
  81. B.user_defined_constructor(self)
  82. def user_defined_constructor(self):
  83. pass
  84. def user_defined_destructor(self):
  85. pass
  86. # builds Statechart structure
  87. def build_statechart_structure(self):
  88. # state <root>
  89. self.states[""] = State(0, self)
  90. # state /z
  91. self.states["/z"] = State(1, self)
  92. self.states["/z"].setEnter(self._z_enter)
  93. self.states["/z"].setExit(self._z_exit)
  94. # add children
  95. self.states[""].addChild(self.states["/z"])
  96. self.states[""].fixTree()
  97. self.states[""].default_state = self.states["/z"]
  98. # transition /z
  99. _z_0 = Transition(self, self.states["/z"], [self.states["/z"]])
  100. _z_0.setAction(self._z_0_exec)
  101. _z_0.setTrigger(Event("_0after"))
  102. self.states["/z"].addTransition(_z_0)
  103. def _z_enter(self):
  104. self.addTimer(0, 0)
  105. def _z_exit(self):
  106. self.removeTimer(0)
  107. def _z_0_exec(self, parameters):
  108. self.big_step.outputEventOM(Event("broad_cast", None, [Event("close", None, [])]))
  109. self.big_step.outputEvent(Event("after_0", "test_output", []))
  110. def initializeStatechart(self):
  111. # enter default state
  112. states = self.states["/z"].getEffectiveTargetStates()
  113. self.updateConfiguration(states)
  114. for state in states:
  115. if state.enter:
  116. state.enter()
  117. class ObjectManager(ObjectManagerBase):
  118. def __init__(self, controller):
  119. ObjectManagerBase.__init__(self, controller)
  120. def instantiate(self, class_name, construct_params):
  121. if class_name == "A":
  122. instance = A(self.controller)
  123. instance.associations = {}
  124. instance.associations["child"] = Association("B", 0, 1)
  125. elif class_name == "B":
  126. instance = B(self.controller)
  127. instance.associations = {}
  128. instance.associations["parent"] = Association("A", 1, 1)
  129. else:
  130. raise Exception("Cannot instantiate class " + class_name)
  131. return instance
  132. class Controller(ThreadsControllerBase):
  133. def __init__(self, keep_running = None):
  134. if keep_running == None: keep_running = True
  135. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  136. self.addOutputPort("test_output")
  137. self.object_manager.createInstance("A", [])
  138. class InputEvent:
  139. def __init__(self, name, port, parameters, time_offset):
  140. self.name = name
  141. self.port = port
  142. self.parameters = parameters
  143. self.time_offset = time_offset
  144. class Test:
  145. def __init__(self):
  146. pass
  147. input_events = []
  148. expected_events = [[Event("creating_instance", "test_output", [])], [Event("starting_instance", "test_output", [])], [Event("after_0", "test_output", [])], [Event("deleting_instance", "test_output", []), Event("after_0", "test_output", [])]]