target.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration) and Sam Pieters (DEVS)
  3. Model author: Sam Pieters
  4. Model name: AfterTransitionTest
  5. Model description:
  6. Test 2: Check if a statechart transitions after a certain time.
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "AfterTransitionTest"
  10. class MainAppInstance(RuntimeClassBase):
  11. def __init__(self, atomdevs, id, start_port_id):
  12. RuntimeClassBase.__init__(self, atomdevs, id)
  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. MainAppInstance.user_defined_constructor(self)
  22. port_name = addInputPort("<narrow_cast>", start_port_id)
  23. atomdevs.state.port_mappings[port_name] = id
  24. def user_defined_constructor(self):
  25. pass
  26. def user_defined_destructor(self):
  27. pass
  28. # builds Statechart structure
  29. def build_statechart_structure(self):
  30. # state <root>
  31. self.states[""] = State(0, "", self)
  32. # state /state1
  33. self.states["/state1"] = State(1, "/state1", self)
  34. self.states["/state1"].setEnter(self._state1_enter)
  35. self.states["/state1"].setExit(self._state1_exit)
  36. # state /state2
  37. self.states["/state2"] = State(2, "/state2", self)
  38. # state /state3
  39. self.states["/state3"] = State(3, "/state3", self)
  40. # add children
  41. self.states[""].addChild(self.states["/state1"])
  42. self.states[""].addChild(self.states["/state2"])
  43. self.states[""].addChild(self.states["/state3"])
  44. self.states[""].fixTree()
  45. self.states[""].default_state = self.states["/state1"]
  46. # transition /state1
  47. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state3"]])
  48. _state1_0.setTrigger(Event("_0after"))
  49. self.states["/state1"].addTransition(_state1_0)
  50. def _state1_enter(self):
  51. self.addTimer(0, 1)
  52. def _state1_exit(self):
  53. self.removeTimer(0)
  54. def initializeStatechart(self):
  55. # enter default state
  56. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  57. RuntimeClassBase.initializeStatechart(self)
  58. class MainApp(ClassBase):
  59. def __init__(self, name):
  60. ClassBase.__init__(self, name)
  61. self.input = self.addInPort("input")
  62. new_instance = self.constructObject(0, 0, [])
  63. self.state.instances[new_instance.instance_id] = new_instance
  64. self.state.next_instance = self.state.next_instance + 1
  65. def constructObject(self, id, start_port_id, parameters):
  66. new_instance = MainAppInstance(self, id, start_port_id)
  67. return new_instance
  68. def instantiate(self, class_name, construct_params):
  69. instance = {}
  70. instance["name"] = class_name
  71. if class_name == "MainApp":
  72. self.narrow_cast_id = self.narrow_cast_id + 0
  73. instance["associations"] = {}
  74. else:
  75. raise Exception("Cannot instantiate class " + class_name)
  76. return instance
  77. ObjectManagerState.instantiate = instantiate
  78. class ObjectManager(ObjectManagerBase):
  79. def __init__(self, name):
  80. ObjectManagerBase.__init__(self, name)
  81. self.state = ObjectManagerState()
  82. self.input = self.addInPort("input")
  83. self.output["MainApp"] = self.addOutPort()
  84. self.state.createInstance("MainApp", [])
  85. self.state.to_send.append((("MainApp", 0), ("MainApp", 0), Event("start_instance", None, ["MainApp[0]"])))
  86. class Controller(CoupledDEVS):
  87. def __init__(self, name):
  88. CoupledDEVS.__init__(self, name)
  89. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  90. self.atomics = []
  91. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  92. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  93. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)