target.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Delete A Running Instance (other than the MainApp)
  5. Model description:
  6. Test 14: Check if multiple running instances can be deleted after the creation.
  7. """
  8. from sccd.runtime.statecharts_core import *
  9. # package "Create and Delete A Running Instance (other than the MainApp)"
  10. class MainApp(RuntimeClassBase):
  11. def __init__(self, controller):
  12. RuntimeClassBase.__init__(self, controller)
  13. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  14. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  15. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  16. self.semantics.priority = StatechartSemantics.SourceParent
  17. self.semantics.concurrency = StatechartSemantics.Single
  18. # build Statechart structure
  19. self.build_statechart_structure()
  20. # call user defined constructor
  21. MainApp.user_defined_constructor(self)
  22. def user_defined_constructor(self):
  23. pass
  24. def user_defined_destructor(self):
  25. pass
  26. # builds Statechart structure
  27. def build_statechart_structure(self):
  28. # state <root>
  29. self.states[""] = State(0, "", self)
  30. # state /state1
  31. self.states["/state1"] = State(1, "/state1", self)
  32. self.states["/state1"].setEnter(self._state1_enter)
  33. # state /state2
  34. self.states["/state2"] = State(2, "/state2", self)
  35. # state /state3
  36. self.states["/state3"] = State(3, "/state3", self)
  37. self.states["/state3"].setEnter(self._state3_enter)
  38. # state /state4
  39. self.states["/state4"] = State(4, "/state4", self)
  40. self.states["/state4"].setEnter(self._state4_enter)
  41. # add children
  42. self.states[""].addChild(self.states["/state1"])
  43. self.states[""].addChild(self.states["/state2"])
  44. self.states[""].addChild(self.states["/state3"])
  45. self.states[""].addChild(self.states["/state4"])
  46. self.states[""].fixTree()
  47. self.states[""].default_state = self.states["/state1"]
  48. # transition /state1
  49. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  50. _state1_0.setAction(self._state1_0_exec)
  51. _state1_0.setTrigger(Event("instance_created", None))
  52. self.states["/state1"].addTransition(_state1_0)
  53. # transition /state2
  54. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state3"]])
  55. _state2_0.setAction(self._state2_0_exec)
  56. _state2_0.setTrigger(Event("instance_started", None))
  57. self.states["/state2"].addTransition(_state2_0)
  58. # transition /state3
  59. _state3_0 = Transition(self, self.states["/state3"], [self.states["/state4"]])
  60. _state3_0.setAction(self._state3_0_exec)
  61. _state3_0.setTrigger(Event("instance_deleted", None))
  62. self.states["/state3"].addTransition(_state3_0)
  63. def _state1_enter(self):
  64. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  65. def _state3_enter(self):
  66. self.big_step.outputEventOM(Event("delete_instance", None, [self, 'linkA[0]']))
  67. def _state4_enter(self):
  68. self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'linkA[0]', Event("sanity_check", None, [])]))
  69. def _state1_0_exec(self, parameters):
  70. association_name = parameters[0]
  71. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  72. self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
  73. def _state2_0_exec(self, parameters):
  74. association_name = parameters[0]
  75. self.big_step.outputEvent(Event("instance_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  76. def _state3_0_exec(self, parameters):
  77. deleted_links = parameters[0]
  78. self.big_step.outputEvent(Event("instance_deleted_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), deleted_links]))
  79. def initializeStatechart(self):
  80. # enter default state
  81. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  82. RuntimeClassBase.initializeStatechart(self)
  83. class A(RuntimeClassBase):
  84. def __init__(self, controller):
  85. RuntimeClassBase.__init__(self, controller)
  86. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  87. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  88. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  89. self.semantics.priority = StatechartSemantics.SourceParent
  90. self.semantics.concurrency = StatechartSemantics.Single
  91. # build Statechart structure
  92. self.build_statechart_structure()
  93. # call user defined constructor
  94. A.user_defined_constructor(self)
  95. def user_defined_constructor(self):
  96. pass
  97. def user_defined_destructor(self):
  98. pass
  99. # builds Statechart structure
  100. def build_statechart_structure(self):
  101. # state <root>
  102. self.states[""] = State(0, "", self)
  103. # state /state1
  104. self.states["/state1"] = State(1, "/state1", self)
  105. self.states["/state1"].setEnter(self._state1_enter)
  106. # add children
  107. self.states[""].addChild(self.states["/state1"])
  108. self.states[""].fixTree()
  109. self.states[""].default_state = self.states["/state1"]
  110. # transition /state1
  111. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state1"]])
  112. _state1_0.setAction(self._state1_0_exec)
  113. _state1_0.setTrigger(Event("sanity_check", None))
  114. self.states["/state1"].addTransition(_state1_0)
  115. def _state1_enter(self):
  116. self.big_step.outputEvent(Event("statechart_started_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  117. def _state1_0_exec(self, parameters):
  118. self.big_step.outputEvent(Event("not_possible", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  119. def initializeStatechart(self):
  120. # enter default state
  121. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  122. RuntimeClassBase.initializeStatechart(self)
  123. class ObjectManager(ObjectManagerBase):
  124. def __init__(self, controller):
  125. ObjectManagerBase.__init__(self, controller)
  126. def instantiate(self, class_name, construct_params):
  127. if class_name == "MainApp":
  128. instance = MainApp(self.controller)
  129. instance.associations = {}
  130. instance.associations["linkA"] = Association("A", 0, -1)
  131. elif class_name == "A":
  132. instance = A(self.controller)
  133. instance.associations = {}
  134. else:
  135. raise Exception("Cannot instantiate class " + class_name)
  136. return instance
  137. class Controller(ThreadsControllerBase):
  138. def __init__(self, keep_running = None, behind_schedule_callback = None):
  139. if keep_running == None: keep_running = True
  140. if behind_schedule_callback == None: behind_schedule_callback = None
  141. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  142. self.addInputPort("ui")
  143. self.addOutputPort("ui")
  144. self.object_manager.createInstance("MainApp", [])