target.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 3: Check if a script works by updating parameters.
  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. self.value = 0
  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"].setExit(self._state1_exit)
  35. # state /state2
  36. self.states["/state2"] = State(2, "/state2", self)
  37. # add children
  38. self.states[""].addChild(self.states["/state1"])
  39. self.states[""].addChild(self.states["/state2"])
  40. self.states[""].fixTree()
  41. self.states[""].default_state = self.states["/state1"]
  42. # transition /state1
  43. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state1"]])
  44. _state1_0.setTrigger(None)
  45. _state1_0.setGuard(self._state1_0_guard)
  46. self.states["/state1"].addTransition(_state1_0)
  47. _state1_1 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  48. _state1_1.setTrigger(None)
  49. _state1_1.setGuard(self._state1_1_guard)
  50. self.states["/state1"].addTransition(_state1_1)
  51. def _state1_exit(self):
  52. self.value += 1
  53. def _state1_0_guard(self, parameters):
  54. return self.value < 5
  55. def _state1_1_guard(self, parameters):
  56. return self.value == 5
  57. def initializeStatechart(self):
  58. # enter default state
  59. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  60. RuntimeClassBase.initializeStatechart(self)
  61. class MainApp(ClassBase):
  62. def __init__(self, name):
  63. ClassBase.__init__(self, name)
  64. self.input = self.addInPort("input")
  65. new_instance = self.constructObject(0, 0, [])
  66. self.state.instances[new_instance.instance_id] = new_instance
  67. self.state.next_instance = self.state.next_instance + 1
  68. def constructObject(self, id, start_port_id, parameters):
  69. new_instance = MainAppInstance(self, id, start_port_id)
  70. return new_instance
  71. def instantiate(self, class_name, construct_params):
  72. instance = {}
  73. instance["name"] = class_name
  74. if class_name == "MainApp":
  75. self.narrow_cast_id = self.narrow_cast_id + 0
  76. instance["associations"] = {}
  77. else:
  78. raise Exception("Cannot instantiate class " + class_name)
  79. return instance
  80. ObjectManagerState.instantiate = instantiate
  81. class ObjectManager(ObjectManagerBase):
  82. def __init__(self, name):
  83. ObjectManagerBase.__init__(self, name)
  84. self.state = ObjectManagerState()
  85. self.input = self.addInPort("input")
  86. self.output["MainApp"] = self.addOutPort()
  87. self.state.createInstance("MainApp", [])
  88. self.state.to_send.append((("MainApp", 0), ("MainApp", 0), Event("start_instance", None, ["MainApp[0]"])))
  89. class Controller(CoupledDEVS):
  90. def __init__(self, name):
  91. CoupledDEVS.__init__(self, name)
  92. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  93. self.atomics = []
  94. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  95. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  96. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)