target.py 7.5 KB

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