associate_event.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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:57 2016
  4. Model author: Glenn De Jonghe
  5. Model name: TestAssociateEvent
  6. Model description:
  7. Testing the object manager
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "TestAssociateEvent"
  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 /start
  32. self.states["/start"] = State(1, self)
  33. # state /wait
  34. self.states["/wait"] = State(2, self)
  35. # add children
  36. self.states[""].addChild(self.states["/start"])
  37. self.states[""].addChild(self.states["/wait"])
  38. self.states[""].fixTree()
  39. self.states[""].default_state = self.states["/start"]
  40. # transition /start
  41. _start_0 = Transition(self, self.states["/start"], [self.states["/wait"]])
  42. _start_0.setAction(self._start_0_exec)
  43. _start_0.setTrigger(Event("create", "test_input"))
  44. self.states["/start"].addTransition(_start_0)
  45. # transition /wait
  46. _wait_0 = Transition(self, self.states["/wait"], [self.states["/start"]])
  47. _wait_0.setAction(self._wait_0_exec)
  48. _wait_0.setTrigger(Event("instance_created", None))
  49. self.states["/wait"].addTransition(_wait_0)
  50. def _start_0_exec(self, parameters):
  51. self.big_step.outputEventOM(Event("create_instance", None, [self, "test2_association"]))
  52. self.big_step.outputEvent(Event("request_send", "test_output", []))
  53. def _wait_0_exec(self, parameters):
  54. association_name = parameters[0]
  55. self.big_step.outputEvent(Event("instance_created", "test_output", []))
  56. self.big_step.outputEventOM(Event("start_instance", None, [self, "test2_association"]))
  57. self.big_step.outputEventOM(Event("narrow_cast", None, [self, "test2_association", Event("hello", None, [])]))
  58. self.big_step.outputEventOM(Event("associate_instance", None, [self, "test2_association", "test3_association"]))
  59. def initializeStatechart(self):
  60. # enter default state
  61. self.default_targets = self.states["/start"].getEffectiveTargetStates()
  62. RuntimeClassBase.initializeStatechart(self)
  63. class Class2(RuntimeClassBase):
  64. def __init__(self, controller):
  65. RuntimeClassBase.__init__(self, controller)
  66. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  67. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  68. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  69. self.semantics.priority = StatechartSemantics.SourceParent
  70. self.semantics.concurrency = StatechartSemantics.Single
  71. # build Statechart structure
  72. self.build_statechart_structure()
  73. # call user defined constructor
  74. Class2.user_defined_constructor(self)
  75. def user_defined_constructor(self):
  76. pass
  77. def user_defined_destructor(self):
  78. pass
  79. # builds Statechart structure
  80. def build_statechart_structure(self):
  81. # state <root>
  82. self.states[""] = State(0, self)
  83. # state /start
  84. self.states["/start"] = State(1, self)
  85. # add children
  86. self.states[""].addChild(self.states["/start"])
  87. self.states[""].fixTree()
  88. self.states[""].default_state = self.states["/start"]
  89. # transition /start
  90. _start_0 = Transition(self, self.states["/start"], [self.states["/start"]])
  91. _start_0.setAction(self._start_0_exec)
  92. _start_0.setTrigger(Event("hello", None))
  93. self.states["/start"].addTransition(_start_0)
  94. def _start_0_exec(self, parameters):
  95. self.big_step.outputEvent(Event("second_working", "test_output", []))
  96. def initializeStatechart(self):
  97. # enter default state
  98. self.default_targets = self.states["/start"].getEffectiveTargetStates()
  99. RuntimeClassBase.initializeStatechart(self)
  100. class ObjectManager(ObjectManagerBase):
  101. def __init__(self, controller):
  102. ObjectManagerBase.__init__(self, controller)
  103. def instantiate(self, class_name, construct_params):
  104. if class_name == "Class1":
  105. instance = Class1(self.controller)
  106. instance.associations = {}
  107. instance.associations["test2_association"] = Association("Class2", 0, -1)
  108. instance.associations["test3_association"] = Association("Class2", 0, -1)
  109. elif class_name == "Class2":
  110. instance = Class2(self.controller)
  111. instance.associations = {}
  112. else:
  113. raise Exception("Cannot instantiate class " + class_name)
  114. return instance
  115. class Controller(ThreadsControllerBase):
  116. def __init__(self, keep_running = None, behind_schedule_callback = None):
  117. if keep_running == None: keep_running = True
  118. if behind_schedule_callback == None: behind_schedule_callback = None
  119. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  120. self.addInputPort("test_input")
  121. self.addOutputPort("test_output")
  122. self.object_manager.createInstance("Class1", [])
  123. class InputEvent:
  124. def __init__(self, name, port, parameters, time_offset):
  125. self.name = name
  126. self.port = port
  127. self.parameters = parameters
  128. self.time_offset = time_offset
  129. class Test:
  130. def __init__(self):
  131. pass
  132. input_events = [InputEvent("create", "test_input", [], 0.0)]
  133. expected_events = [[Event("request_send", "test_output", [])], [Event("instance_created", "test_output", [])], [Event("second_working", "test_output", [])]]