associate_event.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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: 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. states = self.states["/start"].getEffectiveTargetStates()
  62. self.updateConfiguration(states)
  63. for state in states:
  64. if state.enter:
  65. state.enter()
  66. class Class2(RuntimeClassBase):
  67. def __init__(self, controller):
  68. RuntimeClassBase.__init__(self, controller)
  69. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  70. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  71. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  72. self.semantics.priority = StatechartSemantics.SourceParent
  73. self.semantics.concurrency = StatechartSemantics.Single
  74. # build Statechart structure
  75. self.build_statechart_structure()
  76. # call user defined constructor
  77. Class2.user_defined_constructor(self)
  78. def user_defined_constructor(self):
  79. pass
  80. def user_defined_destructor(self):
  81. pass
  82. # builds Statechart structure
  83. def build_statechart_structure(self):
  84. # state <root>
  85. self.states[""] = State(0, self)
  86. # state /start
  87. self.states["/start"] = State(1, self)
  88. # add children
  89. self.states[""].addChild(self.states["/start"])
  90. self.states[""].fixTree()
  91. self.states[""].default_state = self.states["/start"]
  92. # transition /start
  93. _start_0 = Transition(self, self.states["/start"], [self.states["/start"]])
  94. _start_0.setAction(self._start_0_exec)
  95. _start_0.setTrigger(Event("hello", None))
  96. self.states["/start"].addTransition(_start_0)
  97. def _start_0_exec(self, parameters):
  98. self.big_step.outputEvent(Event("second_working", "test_output", []))
  99. def initializeStatechart(self):
  100. # enter default state
  101. states = self.states["/start"].getEffectiveTargetStates()
  102. self.updateConfiguration(states)
  103. for state in states:
  104. if state.enter:
  105. state.enter()
  106. class ObjectManager(ObjectManagerBase):
  107. def __init__(self, controller):
  108. ObjectManagerBase.__init__(self, controller)
  109. def instantiate(self, class_name, construct_params):
  110. if class_name == "Class1":
  111. instance = Class1(self.controller)
  112. instance.associations = {}
  113. instance.associations["test2_association"] = Association("Class2", 0, -1)
  114. instance.associations["test3_association"] = Association("Class2", 0, -1)
  115. elif class_name == "Class2":
  116. instance = Class2(self.controller)
  117. instance.associations = {}
  118. else:
  119. raise Exception("Cannot instantiate class " + class_name)
  120. return instance
  121. class Controller(ThreadsControllerBase):
  122. def __init__(self, keep_running = None):
  123. if keep_running == None: keep_running = True
  124. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
  125. self.addInputPort("test_input")
  126. self.addOutputPort("test_output")
  127. self.object_manager.createInstance("Class1", [])
  128. class InputEvent:
  129. def __init__(self, name, port, parameters, time_offset):
  130. self.name = name
  131. self.port = port
  132. self.parameters = parameters
  133. self.time_offset = time_offset
  134. class Test:
  135. def __init__(self):
  136. pass
  137. input_events = [InputEvent("create", "test_input", [], 0.0)]
  138. expected_events = [[Event("request_send", "test_output", [])], [Event("instance_created", "test_output", [])], [Event("second_working", "test_output", [])]]