target.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 an Instance (other than the MainApp)
  5. Model description:
  6. Test 12: Check if an instance 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 an 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. # add children
  36. self.states[""].addChild(self.states["/state1"])
  37. self.states[""].addChild(self.states["/state2"])
  38. self.states[""].fixTree()
  39. self.states[""].default_state = self.states["/state1"]
  40. # transition /state1
  41. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  42. _state1_0.setAction(self._state1_0_exec)
  43. _state1_0.setTrigger(Event("instance_created", None))
  44. self.states["/state1"].addTransition(_state1_0)
  45. # transition /state2
  46. _state2_0 = Transition(self, self.states["/state2"], [self.states["/state2"]])
  47. _state2_0.setAction(self._state2_0_exec)
  48. _state2_0.setTrigger(Event("instance_deleted", None))
  49. self.states["/state2"].addTransition(_state2_0)
  50. def _state1_enter(self):
  51. self.big_step.outputEventOM(Event("create_instance", None, [self, "linkA", "A"]))
  52. def _state1_0_exec(self, parameters):
  53. association_name = parameters[0]
  54. self.big_step.outputEvent(Event("instance_created_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), association_name]))
  55. self.big_step.outputEventOM(Event("delete_instance", None, [self, association_name]))
  56. def _state2_0_exec(self, parameters):
  57. deleted_links = parameters[0]
  58. self.big_step.outputEvent(Event("instance_deleted_succesfully", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0)), deleted_links]))
  59. def initializeStatechart(self):
  60. # enter default state
  61. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  62. RuntimeClassBase.initializeStatechart(self)
  63. class A(RuntimeClassBase):
  64. def __init__(self, controller):
  65. RuntimeClassBase.__init__(self, controller)
  66. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  67. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  68. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  69. self.semantics.priority = StatechartSemantics.SourceParent
  70. self.semantics.concurrency = StatechartSemantics.Single
  71. # build Statechart structure
  72. self.build_statechart_structure()
  73. # call user defined constructor
  74. A.user_defined_constructor(self)
  75. def user_defined_constructor(self):
  76. pass
  77. def user_defined_destructor(self):
  78. pass
  79. # builds Statechart structure
  80. def build_statechart_structure(self):
  81. # state <root>
  82. self.states[""] = State(0, "", self)
  83. # state /state1
  84. self.states["/state1"] = State(1, "/state1", self)
  85. self.states["/state1"].setEnter(self._state1_enter)
  86. # add children
  87. self.states[""].addChild(self.states["/state1"])
  88. self.states[""].fixTree()
  89. self.states[""].default_state = self.states["/state1"]
  90. def _state1_enter(self):
  91. self.big_step.outputEvent(Event("not_possible", self.getOutPortName("ui"), [str('%.2f' % (self.getSimulatedTime() / 1000.0))]))
  92. def initializeStatechart(self):
  93. # enter default state
  94. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  95. RuntimeClassBase.initializeStatechart(self)
  96. class ObjectManager(ObjectManagerBase):
  97. def __init__(self, controller):
  98. ObjectManagerBase.__init__(self, controller)
  99. def instantiate(self, class_name, construct_params):
  100. if class_name == "MainApp":
  101. instance = MainApp(self.controller)
  102. instance.associations = {}
  103. instance.associations["linkA"] = Association("A", 0, -1)
  104. elif class_name == "A":
  105. instance = A(self.controller)
  106. instance.associations = {}
  107. else:
  108. raise Exception("Cannot instantiate class " + class_name)
  109. return instance
  110. class Controller(ThreadsControllerBase):
  111. def __init__(self, keep_running = None, behind_schedule_callback = None):
  112. if keep_running == None: keep_running = True
  113. if behind_schedule_callback == None: behind_schedule_callback = None
  114. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  115. self.addInputPort("ui")
  116. self.addOutputPort("ui")
  117. self.object_manager.createInstance("MainApp", [])