target.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.associations = {}
  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. MainAppInstance.user_defined_constructor(self)
  23. port_name = addInputPort("<narrow_cast>", start_port_id)
  24. atomdevs.addInPort(port_name)
  25. atomdevs.state.port_mappings[port_name] = id
  26. def user_defined_constructor(self):
  27. pass
  28. def user_defined_destructor(self):
  29. pass
  30. # builds Statechart structure
  31. def build_statechart_structure(self):
  32. # state <root>
  33. self.states[""] = State(0, "", self)
  34. # state /state1
  35. self.states["/state1"] = ParallelState(1, "/state1", self)
  36. # state /state1/state11
  37. self.states["/state1/state11"] = State(2, "/state1/state11", self)
  38. # state /state1/state11/state11
  39. self.states["/state1/state11/state11"] = State(3, "/state1/state11/state11", self)
  40. # state /state1/state12
  41. self.states["/state1/state12"] = State(4, "/state1/state12", self)
  42. # state /state1/state12/state12
  43. self.states["/state1/state12/state12"] = State(5, "/state1/state12/state12", self)
  44. # add children
  45. self.states[""].addChild(self.states["/state1"])
  46. self.states["/state1"].addChild(self.states["/state1/state11"])
  47. self.states["/state1"].addChild(self.states["/state1/state12"])
  48. self.states["/state1/state11"].addChild(self.states["/state1/state11/state11"])
  49. self.states["/state1/state12"].addChild(self.states["/state1/state12/state12"])
  50. self.states[""].fixTree()
  51. self.states[""].default_state = self.states["/state1"]
  52. self.states["/state1/state11"].default_state = self.states["/state1/state11/state11"]
  53. self.states["/state1/state12"].default_state = self.states["/state1/state12/state12"]
  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. class Dummy(ObjectManagerState):
  69. def __init__(self):
  70. ObjectManagerState.__init__(self)
  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. class ObjectManager(ObjectManagerBase):
  81. def __init__(self, name):
  82. ObjectManagerBase.__init__(self, name)
  83. self.state = Dummy()
  84. self.input = self.addInPort("input")
  85. self.output["MainApp"] = self.addOutPort()
  86. self.state.createInstance("MainApp", [])
  87. self.state.to_send.append((("MainApp", 0), ("MainApp", 0), Event("start_instance", None, ["MainApp[0]"])))
  88. class Controller(CoupledDEVS):
  89. def __init__(self, name):
  90. CoupledDEVS.__init__(self, name)
  91. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  92. self.atomics = []
  93. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  94. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  95. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)