target.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: Global Input Test
  5. Model description:
  6. Test 2.0: Check if the model receives input.
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. # package "Global Input Test"
  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("ui", start_port_id, True)
  23. atomdevs.state.port_mappings[port_name] = id
  24. port_name = addInputPort("<narrow_cast>", start_port_id)
  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"] = State(1, "/state1", self)
  36. # state /state2
  37. self.states["/state2"] = State(2, "/state2", self)
  38. # add children
  39. self.states[""].addChild(self.states["/state1"])
  40. self.states[""].addChild(self.states["/state2"])
  41. self.states[""].fixTree()
  42. self.states[""].default_state = self.states["/state1"]
  43. # transition /state1
  44. _state1_0 = Transition(self, self.states["/state1"], [self.states["/state2"]])
  45. _state1_0.setTrigger(Event("input_event", None))
  46. self.states["/state1"].addTransition(_state1_0)
  47. def initializeStatechart(self):
  48. # enter default state
  49. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  50. RuntimeClassBase.initializeStatechart(self)
  51. class MainApp(ClassBase):
  52. def __init__(self, name):
  53. ClassBase.__init__(self, name)
  54. self.input = self.addInPort("input")
  55. new_instance = self.constructObject(0, 0, [])
  56. self.state.instances[new_instance.instance_id] = new_instance
  57. new_instance.start()
  58. self.state.next_time = 0
  59. def constructObject(self, id, start_port_id, parameters):
  60. new_instance = MainAppInstance(self, id, start_port_id)
  61. return new_instance
  62. def instantiate(self, class_name, construct_params):
  63. instance = {}
  64. instance["name"] = class_name
  65. if class_name == "MainApp":
  66. self.narrow_cast_id = self.narrow_cast_id + 0
  67. instance["associations"] = {}
  68. else:
  69. raise Exception("Cannot instantiate class " + class_name)
  70. return instance
  71. ObjectManagerState.instantiate = instantiate
  72. class ObjectManager(ObjectManagerBase):
  73. def __init__(self, name):
  74. ObjectManagerBase.__init__(self, name)
  75. self.state = ObjectManagerState()
  76. self.input = self.addInPort("input")
  77. self.output["MainApp"] = self.addOutPort()
  78. self.state.createInstance("MainApp", [])
  79. class Controller(CoupledDEVS):
  80. def __init__(self, name):
  81. CoupledDEVS.__init__(self, name)
  82. self.in_ui = self.addInPort("ui")
  83. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  84. self.atomics = []
  85. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  86. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  87. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  88. self.connectPorts(self.in_ui, self.atomics[0].input)