target.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: GlobalData
  5. Model description:
  6. Test X: check if global data can be changed
  7. """
  8. from sccd.runtime.DEVS_statecharts_core import *
  9. GLOBAL_INT = 0
  10. # package "GlobalData"
  11. class MainAppInstance(RuntimeClassBase):
  12. def __init__(self, atomdevs, id, start_port_id):
  13. RuntimeClassBase.__init__(self, atomdevs, id)
  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("ui", start_port_id, True)
  24. atomdevs.state.port_mappings[port_name] = id
  25. port_name = addInputPort("<narrow_cast>", start_port_id)
  26. atomdevs.state.port_mappings[port_name] = id
  27. def user_defined_constructor(self):
  28. pass
  29. def user_defined_destructor(self):
  30. pass
  31. # builds Statechart structure
  32. def build_statechart_structure(self):
  33. # state <root>
  34. self.states[""] = State(0, "", self)
  35. # state /state1
  36. self.states["/state1"] = State(1, "/state1", self)
  37. self.states["/state1"].setEnter(self._state1_enter)
  38. # add children
  39. self.states[""].addChild(self.states["/state1"])
  40. self.states[""].fixTree()
  41. self.states[""].default_state = self.states["/state1"]
  42. def _state1_enter(self):
  43. print(GLOBAL_INT)
  44. def initializeStatechart(self):
  45. # enter default state
  46. self.default_targets = self.states["/state1"].getEffectiveTargetStates()
  47. RuntimeClassBase.initializeStatechart(self)
  48. class MainApp(ClassBase):
  49. def __init__(self, name):
  50. ClassBase.__init__(self, name)
  51. self.input = self.addInPort("input")
  52. self.glob_outputs["ui"] = self.addOutPort("ui")
  53. new_instance = self.constructObject(0, 0, [])
  54. self.state.instances[new_instance.instance_id] = new_instance
  55. self.state.next_instance = self.state.next_instance + 1
  56. def constructObject(self, id, start_port_id, parameters):
  57. new_instance = MainAppInstance(self, id, start_port_id)
  58. return new_instance
  59. def instantiate(self, class_name, construct_params):
  60. instance = {}
  61. instance["name"] = class_name
  62. if class_name == "MainApp":
  63. self.narrow_cast_id = self.narrow_cast_id + 0
  64. instance["associations"] = {}
  65. else:
  66. raise Exception("Cannot instantiate class " + class_name)
  67. return instance
  68. ObjectManagerState.instantiate = instantiate
  69. class ObjectManager(ObjectManagerBase):
  70. def __init__(self, name):
  71. ObjectManagerBase.__init__(self, name)
  72. self.state = ObjectManagerState()
  73. self.input = self.addInPort("input")
  74. self.output["MainApp"] = self.addOutPort()
  75. self.state.createInstance("MainApp", [])
  76. self.state.to_send.append((("MainApp", 0), ("MainApp", 0), Event("start_instance", None, ["MainApp[0]"])))
  77. class Controller(CoupledDEVS):
  78. def __init__(self, name):
  79. CoupledDEVS.__init__(self, name)
  80. self.in_ui = self.addInPort("ui")
  81. self.out_ui = self.addOutPort("ui")
  82. self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
  83. self.atomics = []
  84. self.atomics.append(self.addSubModel(MainApp("MainApp")))
  85. self.connectPorts(self.atomics[0].obj_manager_out, self.objectmanager.input)
  86. self.connectPorts(self.objectmanager.output["MainApp"], self.atomics[0].obj_manager_in)
  87. self.connectPorts(self.atomics[0].glob_outputs["ui"], self.out_ui)
  88. self.connectPorts(self.in_ui, self.atomics[0].input)