target.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Multiple Instances of the same type (other than the MainApp)
  5. Model description:
  6. Test 13: Check if multiple instances can be deleted after the creation, the instance is not started yet.
  7. """
  8. from sccd.runtime.statecharts_core import *
  9. # package "Create and Delete Multiple Instances of the same type (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. self.states["/state2"].setEnter(self._state2_enter)
  36. # state /state3
  37. self.states["/state3"] = State(3, "/state3", self)
  38. self.states["/state3"].setEnter(self._state3_enter)
  39. # state /state4
  40. self.states["/state4"] = State(4, "/state4", self)
  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_created", 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.setTrigger(None)
  61. self.states["/state3"].addTransition(_state3_0)
  62. # transition /state4
  63. _state4_0 = Transition(self, self.states["/state4"], [self.states["/state4"]])
  64. _state4_0.setAction(self._state4_0_exec)
  65. _state4_0.setTrigger(Event("instance_deleted", None))
  66. self.states["/state4"].addTransition(_state4_0)
  67. def _state1_enter(self):
  68. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  69. def _state2_enter(self):
  70. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  71. def _state3_enter(self):
  72. self.big_step.outputEventOM(Event("delete_instance", None, [self, 'linkA[1]']))
  73. self.big_step.outputEventOM(Event("delete_instance", None, [self, 'linkA[0]']))
  74. def _state1_0_exec(self, parameters):
  75. association_name = parameters[0]
  76. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  77. def _state2_0_exec(self, parameters):
  78. association_name = parameters[0]
  79. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  80. def _state4_0_exec(self, parameters):
  81. deleted_links = parameters[0]
  82. self.big_step.outputEvent(Event("instance_deleted_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), deleted_links]))
  83. def initializeStatechart(self):
  84. # enter default state
  85. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  86. RuntimeClassBase.initializeStatechart(self)
  87. class A(RuntimeClassBase):
  88. def __init__(self, controller):
  89. RuntimeClassBase.__init__(self, controller)
  90. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  91. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  92. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  93. self.semantics.priority = StatechartSemantics.SourceParent
  94. self.semantics.concurrency = StatechartSemantics.Single
  95. # build Statechart structure
  96. self.build_statechart_structure()
  97. # call user defined constructor
  98. A.user_defined_constructor(self)
  99. def user_defined_constructor(self):
  100. pass
  101. def user_defined_destructor(self):
  102. pass
  103. # builds Statechart structure
  104. def build_statechart_structure(self):
  105. # state <root>
  106. self.states[""] = State(0, "", self)
  107. # state /state1
  108. self.states["/state1"] = State(1, "/state1", self)
  109. self.states["/state1"].setEnter(self._state1_enter)
  110. # add children
  111. self.states[""].addChild(self.states["/state1"])
  112. self.states[""].fixTree()
  113. self.states[""].default_state = self.states["/state1"]
  114. def _state1_enter(self):
  115. self.big_step.outputEvent(Event("not_possible", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  116. def initializeStatechart(self):
  117. # enter default state
  118. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  119. RuntimeClassBase.initializeStatechart(self)
  120. class ObjectManager(ObjectManagerBase):
  121. def __init__(self, controller):
  122. ObjectManagerBase.__init__(self, controller)
  123. def instantiate(self, class_name, construct_params):
  124. if class_name == "MainApp":
  125. instance = MainApp(self.controller)
  126. instance.associations = {}
  127. instance.associations["linkA"] = Association("A", 0, -1)
  128. elif class_name == "A":
  129. instance = A(self.controller)
  130. instance.associations = {}
  131. else:
  132. raise Exception("Cannot instantiate class " + class_name)
  133. return instance
  134. class Controller(ThreadsControllerBase):
  135. def __init__(self, keep_running = None, behind_schedule_callback = None):
  136. if keep_running == None: keep_running = True
  137. if behind_schedule_callback == None: behind_schedule_callback = None
  138. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  139. self.addInputPort("ui")
  140. self.addOutputPort("ui")
  141. self.object_manager.createInstance("MainApp", [])