create_and_start.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
  3. Model name: sourcechildbug
  4. """
  5. from sccd.runtime.statecharts_core import *
  6. # package "sourcechildbug"
  7. class A(RuntimeClassBase):
  8. def __init__(self, controller):
  9. RuntimeClassBase.__init__(self, controller)
  10. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  11. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  12. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  13. self.semantics.priority = StatechartSemantics.SourceParent
  14. self.semantics.concurrency = StatechartSemantics.Single
  15. # build Statechart structure
  16. self.build_statechart_structure()
  17. # call user defined constructor
  18. A.user_defined_constructor(self)
  19. def user_defined_constructor(self):
  20. pass
  21. def user_defined_destructor(self):
  22. pass
  23. # builds Statechart structure
  24. def build_statechart_structure(self):
  25. # state <root>
  26. self.states[""] = State(0, "", self)
  27. # state /running
  28. self.states["/running"] = State(1, "/running", self)
  29. self.states["/running"].setEnter(self._running_enter)
  30. # state /created
  31. self.states["/created"] = State(2, "/created", self)
  32. # state /stopped
  33. self.states["/stopped"] = State(3, "/stopped", self)
  34. # add children
  35. self.states[""].addChild(self.states["/running"])
  36. self.states[""].addChild(self.states["/created"])
  37. self.states[""].addChild(self.states["/stopped"])
  38. self.states[""].fixTree()
  39. self.states[""].default_state = self.states["/running"]
  40. # transition /running
  41. _running_0 = Transition(self, self.states["/running"], [self.states["/created"]])
  42. _running_0.setAction(self._running_0_exec)
  43. _running_0.setTrigger(Event("instance_created", None))
  44. self.states["/running"].addTransition(_running_0)
  45. # transition /created
  46. _created_0 = Transition(self, self.states["/created"], [self.states["/stopped"]])
  47. _created_0.setAction(self._created_0_exec)
  48. _created_0.setTrigger(Event("instance_started", None))
  49. self.states["/created"].addTransition(_created_0)
  50. def _running_enter(self):
  51. self.big_step.outputEventOM(Event("create_and_start_instance", None, [self, 'to_B']))
  52. def _running_0_exec(self, parameters):
  53. association_name = parameters[0]
  54. print 'A got instance_created [%s]' % association_name
  55. def _created_0_exec(self, parameters):
  56. association_name = parameters[0]
  57. print 'A got instance_started [%s]' % association_name
  58. def initializeStatechart(self):
  59. # enter default state
  60. self.default_targets = self.states["/running"].getEffectiveTargetStates()
  61. RuntimeClassBase.initializeStatechart(self)
  62. class B(RuntimeClassBase):
  63. def __init__(self, controller):
  64. RuntimeClassBase.__init__(self, controller)
  65. self.semantics.big_step_maximality = StatechartSemantics.TakeMany
  66. self.semantics.internal_event_lifeline = StatechartSemantics.Queue
  67. self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
  68. self.semantics.priority = StatechartSemantics.SourceParent
  69. self.semantics.concurrency = StatechartSemantics.Single
  70. # build Statechart structure
  71. self.build_statechart_structure()
  72. # call user defined constructor
  73. B.user_defined_constructor(self)
  74. def user_defined_constructor(self):
  75. pass
  76. def user_defined_destructor(self):
  77. pass
  78. # builds Statechart structure
  79. def build_statechart_structure(self):
  80. # state <root>
  81. self.states[""] = State(0, "", self)
  82. # state /running
  83. self.states["/running"] = State(1, "/running", self)
  84. self.states["/running"].setEnter(self._running_enter)
  85. # add children
  86. self.states[""].addChild(self.states["/running"])
  87. self.states[""].fixTree()
  88. self.states[""].default_state = self.states["/running"]
  89. def _running_enter(self):
  90. print 'B instance created!'
  91. def initializeStatechart(self):
  92. # enter default state
  93. self.default_targets = self.states["/running"].getEffectiveTargetStates()
  94. RuntimeClassBase.initializeStatechart(self)
  95. class ObjectManager(ObjectManagerBase):
  96. def __init__(self, controller):
  97. ObjectManagerBase.__init__(self, controller)
  98. def instantiate(self, class_name, construct_params):
  99. if class_name == "A":
  100. instance = A(self.controller)
  101. instance.associations = {}
  102. instance.associations["to_B"] = Association("B", 0, 1)
  103. elif class_name == "B":
  104. instance = B(self.controller)
  105. instance.associations = {}
  106. else:
  107. raise Exception("Cannot instantiate class " + class_name)
  108. return instance
  109. class Controller(ThreadsControllerBase):
  110. def __init__(self, keep_running = None, behind_schedule_callback = None):
  111. if keep_running == None: keep_running = True
  112. if behind_schedule_callback == None: behind_schedule_callback = None
  113. ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
  114. self.addInputPort("input")
  115. self.object_manager.createInstance("A", [])