target.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Model author: Sam Pieters
  4. Model name: Create and Start Instance Of Multiple Classes
  5. Model description:
  6. TODO
  7. Test 10: Check if an instance of multiple classes are created and started successfully
  8. """
  9. from sccd.runtime.statecharts_core import *
  10. # package "Create and Start Instance Of Multiple Classes"
  11. class MainApp(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. MainApp.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 /state1
  32. self.states["/state1"] = State(1, "/state1", self)
  33. self.states["/state1"].setEnter(self._state1_enter)
  34. # state /state2
  35. self.states["/state2"] = State(2, "/state2", self)
  36. # add children
  37. self.states[""].addChild(self.states["/state1"])
  38. self.states[""].addChild(self.states["/state2"])
  39. self.states[""].fixTree()
  40. self.states[""].default_state = self.states["/state1"]
  41. # transition /state1
  42. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  43. _state1_0.setAction(self._state1_0_exec)
  44. _state1_0.setTrigger(Event("instance_created", None))
  45. self.states["/state1"].addTransition(_state1_0)
  46. # transition /state2
  47. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  48. _state2_0.setAction(self._state2_0_exec)
  49. _state2_0.setTrigger(Event("instance_started", None))
  50. self.states["/state2"].addTransition(_state2_0)
  51. def _state1_enter(self):
  52. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  53. def _state1_0_exec(self, parameters):
  54. association_name = parameters[0]
  55. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  56. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  57. def _state2_0_exec(self, parameters):
  58. association_name = parameters[0]
  59. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  60. def initializeStatechart(self):
  61. # enter default state
  62. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  63. RuntimeClassBase.initializeStatechart(self)
  64. class A(RuntimeClassBase):
  65. def __init__(self, controller):
  66. RuntimeClassBase.__init__(self, controller)
  67. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  68. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  69. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  70. self.semantics.priority = StatechartSemantics.SourceParent
  71. self.semantics.concurrency = StatechartSemantics.Single
  72. # build Statechart structure
  73. self.build_statechart_structure()
  74. # call user defined constructor
  75. A.user_defined_constructor(self)
  76. def user_defined_constructor(self):
  77. pass
  78. def user_defined_destructor(self):
  79. pass
  80. # builds Statechart structure
  81. def build_statechart_structure(self):
  82. # state <root>
  83. self.states[""] = State(0, "", self)
  84. # state /state1
  85. self.states["/state1"] = State(1, "/state1", self)
  86. self.states["/state1"].setEnter(self._state1_enter)
  87. # add children
  88. self.states[""].addChild(self.states["/state1"])
  89. self.states[""].fixTree()
  90. self.states[""].default_state = self.states["/state1"]
  91. def _state1_enter(self):
  92. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  93. self.big_step.outputEvent(Event("constructor_initialized_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  94. def initializeStatechart(self):
  95. # enter default state
  96. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  97. RuntimeClassBase.initializeStatechart(self)
  98. class B(RuntimeClassBase):
  99. def __init__(self, controller):
  100. RuntimeClassBase.__init__(self, controller)
  101. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  102. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  103. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  104. self.semantics.priority = StatechartSemantics.SourceParent
  105. self.semantics.concurrency = StatechartSemantics.Single
  106. # build Statechart structure
  107. self.build_statechart_structure()
  108. # call user defined constructor
  109. B.user_defined_constructor(self)
  110. def user_defined_constructor(self):
  111. pass
  112. def user_defined_destructor(self):
  113. pass
  114. # builds Statechart structure
  115. def build_statechart_structure(self):
  116. # state <root>
  117. self.states[""] = State(0, "", self)
  118. # state /state1
  119. self.states["/state1"] = State(1, "/state1", self)
  120. self.states["/state1"].setEnter(self._state1_enter)
  121. # add children
  122. self.states[""].addChild(self.states["/state1"])
  123. self.states[""].fixTree()
  124. self.states[""].default_state = self.states["/state1"]
  125. def _state1_enter(self):
  126. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  127. self.big_step.outputEvent(Event("constructor_initialized_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  128. def initializeStatechart(self):
  129. # enter default state
  130. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  131. RuntimeClassBase.initializeStatechart(self)
  132. class ObjectManager(ObjectManagerBase):
  133. def __init__(self, controller):
  134. ObjectManagerBase.__init__(self, controller)
  135. def instantiate(self, class_name, construct_params):
  136. if class_name == "MainApp":
  137. instance = MainApp(self.controller)
  138. instance.associations = {}
  139. instance.associations["linkA"] = Association("A", 0, -1)
  140. instance.associations["linkB"] = Association("B", 0, -1)
  141. elif class_name == "A":
  142. instance = A(self.controller)
  143. instance.associations = {}
  144. elif class_name == "B":
  145. instance = B(self.controller)
  146. instance.associations = {}
  147. else:
  148. raise Exception("Cannot instantiate class " + class_name)
  149. return instance
  150. class Controller(ThreadsControllerBase):
  151. def __init__(self, keep_running = None, behind_schedule_callback = None):
  152. if keep_running == None: keep_running = True
  153. if behind_schedule_callback == None: behind_schedule_callback = None
  154. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  155. self.addInputPort("ui")
  156. self.addOutputPort("ui")
  157. self.object_manager.createInstance("MainApp", [])