object_manager.py 6.3 KB

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