after_0.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Date: Tue Aug 09 09:35:56 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. self.default_targets = self.states["/x"].getEffectiveTargetStates()
  66. RuntimeClassBase.initializeStatechart(self)
  67. class B(RuntimeClassBase):
  68. def __init__(self, controller):
  69. RuntimeClassBase.__init__(self, controller)
  70. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  71. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  72. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  73. self.semantics.priority = StatechartSemantics.SourceParent
  74. self.semantics.concurrency = StatechartSemantics.Single
  75. # build Statechart structure
  76. self.build_statechart_structure()
  77. # call user defined constructor
  78. B.user_defined_constructor(self)
  79. def user_defined_constructor(self):
  80. pass
  81. def user_defined_destructor(self):
  82. pass
  83. # builds Statechart structure
  84. def build_statechart_structure(self):
  85. # state <root>
  86. self.states[""] = State(0, self)
  87. # state /z
  88. self.states["/z"] = State(1, self)
  89. self.states["/z"].setEnter(self._z_enter)
  90. self.states["/z"].setExit(self._z_exit)
  91. # add children
  92. self.states[""].addChild(self.states["/z"])
  93. self.states[""].fixTree()
  94. self.states[""].default_state = self.states["/z"]
  95. # transition /z
  96. _z_0 = Transition(self, self.states["/z"], [self.states["/z"]])
  97. _z_0.setAction(self._z_0_exec)
  98. _z_0.setTrigger(Event("_0after"))
  99. self.states["/z"].addTransition(_z_0)
  100. def _z_enter(self):
  101. self.addTimer(0, 0)
  102. def _z_exit(self):
  103. self.removeTimer(0)
  104. def _z_0_exec(self, parameters):
  105. self.big_step.outputEventOM(Event("broad_cast", None, [Event("close", None, [])]))
  106. self.big_step.outputEvent(Event("after_0", "test_output", []))
  107. def initializeStatechart(self):
  108. # enter default state
  109. self.default_targets = self.states["/z"].getEffectiveTargetStates()
  110. RuntimeClassBase.initializeStatechart(self)
  111. class ObjectManager(ObjectManagerBase):
  112. def __init__(self, controller):
  113. ObjectManagerBase.__init__(self, controller)
  114. def instantiate(self, class_name, construct_params):
  115. if class_name == "A":
  116. instance = A(self.controller)
  117. instance.associations = {}
  118. instance.associations["child"] = Association("B", 0, 1)
  119. elif class_name == "B":
  120. instance = B(self.controller)
  121. instance.associations = {}
  122. instance.associations["parent"] = Association("A", 1, 1)
  123. else:
  124. raise Exception("Cannot instantiate class " + class_name)
  125. return instance
  126. class Controller(ThreadsControllerBase):
  127. def __init__(self, keep_running = None, behind_schedule_callback = None):
  128. if keep_running == None: keep_running = True
  129. if behind_schedule_callback == None: behind_schedule_callback = None
  130. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  131. self.addOutputPort("test_output")
  132. self.object_manager.createInstance("A", [])
  133. class InputEvent:
  134. def __init__(self, name, port, parameters, time_offset):
  135. self.name = name
  136. self.port = port
  137. self.parameters = parameters
  138. self.time_offset = time_offset
  139. class Test:
  140. def __init__(self):
  141. pass
  142. input_events = []
  143. 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", [])]]