DEVS_simulate.alc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. include "primitives.alh"
  2. include "services.alh"
  3. include "modelling.alh"
  4. include "object_operations.alh"
  5. include "utils.alh"
  6. include "io.alh"
  7. include "typing.alh"
  8. Boolean function main(model : Element):
  9. String model_rep
  10. Element all_atomics
  11. Element curr_atomic
  12. Element all_coupleds
  13. Element curr_coupled
  14. Element string_element
  15. Element all_ports
  16. Element all_submodels
  17. Element curr_port
  18. Element curr_submodel
  19. Element out_channels
  20. Element curr_channel
  21. Element possible_parent
  22. Element parent
  23. String curr_port_name
  24. String curr_port_type
  25. String curr_submodel_name
  26. String curr_submodel_type
  27. String curr_submodel_parameters
  28. model_rep = ""
  29. all_atomics = allInstances(model, "DEVS/AtomicDEVSBlock")
  30. while (read_nr_out(all_atomics) > 0):
  31. curr_atomic = set_pop(all_atomics)
  32. model_rep = model_rep + "class " + cast_string(read_attribute(model, curr_atomic, "name")) + "(AtomicDEVS):\n"
  33. model_rep = model_rep + "\tdef __init__(self, name, parameters):\n"
  34. model_rep = model_rep + "\t\tself.parameters = parameters\n"
  35. model_rep = model_rep + "\t\tAtomicDEVS.__init__(self, name)\n"
  36. model_rep = model_rep + "\t\tself.initialState()\n"
  37. model_rep = model_rep + "\t\tself.my_ports = {"
  38. all_ports = allAssociationDestinations(model, curr_atomic, "DEVS/DEVSBlockToPort")
  39. while (read_nr_out(all_ports) > 0):
  40. curr_port = set_pop(all_ports)
  41. curr_port_name = cast_value(read_attribute(model, curr_port, "name"))
  42. curr_port_type = read_type(model, curr_port)
  43. if (curr_port_type == "DEVS/InputPort"):
  44. model_rep = model_rep + curr_port_name + ": self.addInPort(" + curr_port_name + ")"
  45. if (curr_port_type == "DEVS/OutputPort"):
  46. model_rep = model_rep + curr_port_name + ": self.addOutPort(" + curr_port_name + ")"
  47. if (read_nr_out(all_ports) > 0):
  48. model_rep = model_rep + ", "
  49. model_rep = model_rep + "}\n"
  50. model_rep = model_rep + "\tdef initialState(self):\n" + cast_string(read_attribute(model, curr_atomic, "initialState")) + "\n"
  51. model_rep = model_rep + "\tdef timeAdvance(self):\n" + cast_string(read_attribute(model, curr_atomic, "timeAdvance")) + "\n"
  52. model_rep = model_rep + "\tdef outputFnc(self):\n" + cast_string(read_attribute(model, curr_atomic, "outputFnc")) + "\n"
  53. model_rep = model_rep + "\tdef intTransition(self):\n" + cast_string(read_attribute(model, curr_atomic, "intTransition")) + "\n"
  54. model_rep = model_rep + "\tdef extTransition(self, my_inputs):\n" + cast_string(read_attribute(model, curr_atomic, "extTransition")) + "\n"
  55. all_coupleds = allInstances(model, "DEVS/CoupledDEVSBlock")
  56. while (read_nr_out(all_coupleds) > 0):
  57. curr_coupled = set_pop(all_coupleds)
  58. model_rep = model_rep + "class " + cast_string(read_attribute(model, curr_coupled, "name")) + "(CoupledDEVS):\n"
  59. model_rep = model_rep + "\tdef __init__(self, name, parameters):\n"
  60. model_rep = model_rep + "\t\tCoupledDEVS.__init__(self, name)\n"
  61. model_rep = model_rep + "\t\tself.my_ports = {"
  62. all_ports = allAssociationDestinations(model, curr_coupled, "DEVS/DEVSBlockToPort")
  63. while (read_nr_out(all_ports) > 0):
  64. curr_port = set_pop(all_ports)
  65. curr_port_name = cast_value(read_attribute(model, curr_port, "name"))
  66. curr_port_type = read_type(model, curr_port)
  67. if (curr_port_type == "DEVS/InputPort"):
  68. model_rep = model_rep + curr_port_name + ": self.addInPort(" + curr_port_name + ")"
  69. if (curr_port_type == "DEVS/OutputPort"):
  70. model_rep = model_rep + curr_port_name + ": self.addOutPort(" + curr_port_name + ")"
  71. if (read_nr_out(all_ports) > 0):
  72. model_rep = model_rep + ", "
  73. model_rep = model_rep + "}\n"
  74. model_rep = model_rep + "\t\tself.submodels = {"
  75. all_submodels = allAssociationDestinations(model, curr_coupled, "DEVS/SubModel")
  76. while (read_nr_out(all_submodels) > 0):
  77. curr_submodel = set_pop(all_submodels)
  78. curr_submodel_name = cast_value(read_attribute(model, curr_submodel, "name"))
  79. curr_submodel_type = cast_string(read_attribute(model, curr_submodel, "type"))
  80. curr_submodel_parameters = cast_string(read_attribute(model, curr_submodel, "parameters"))
  81. model_rep = model_rep + curr_submodel_name + ": self.addSubModel(" + curr_submodel_type + "(" + curr_submodel_name + ", " + curr_submodel_parameters + ")" + ")"
  82. if (read_nr_out(all_submodels) > 0):
  83. model_rep = model_rep + ", "
  84. model_rep = model_rep + "}\n"
  85. all_ports = allAssociationDestinations(model, curr_coupled, "DEVS/DEVSBlockToPort")
  86. while (read_nr_out(all_ports) > 0):
  87. curr_port = set_pop(all_ports)
  88. out_channels = allAssociationDestinations(model, curr_port, "DEVS/Channel")
  89. while (read_nr_out(out_channels) > 0):
  90. curr_channel = set_pop(out_channels)
  91. parent = set_pop(allAssociationOrigins(model, curr_channel, "DEVS/DEVSInstanceToPort"))
  92. model_rep = model_rep + "\t\tself.connectPorts(self.my_ports[" + cast_value(read_attribute(model, curr_port, "name")) + "], self.submodels[" + cast_value(read_attribute(model, parent, "name")) + "].my_ports[" + cast_value(read_attribute(model, curr_channel, "name")) + "])\n"
  93. all_submodels = allAssociationDestinations(model, curr_coupled, "DEVS/SubModel")
  94. while (read_nr_out(all_submodels) > 0):
  95. curr_submodel = set_pop(all_submodels)
  96. curr_submodel_name = cast_value(read_attribute(model, curr_submodel, "name"))
  97. all_ports = allAssociationDestinations(model, curr_submodel, "DEVS/DEVSInstanceToPort")
  98. while (read_nr_out(all_ports) > 0):
  99. curr_port = set_pop(all_ports)
  100. out_channels = allAssociationDestinations(model, curr_port, "DEVS/Channel")
  101. while (read_nr_out(out_channels) > 0):
  102. curr_channel = set_pop(out_channels)
  103. possible_parent = allAssociationOrigins(model, curr_channel, "DEVS/DEVSInstanceToPort")
  104. if (read_nr_out(possible_parent) == 0):
  105. model_rep = model_rep + "\t\tself.connectPorts(self.submodels[" + curr_submodel_name + "].my_ports[" + cast_value(read_attribute(model, curr_port, "name")) + "], self.my_ports[" + cast_value(read_attribute(model, curr_channel, "name")) + "])\n"
  106. else:
  107. parent = set_pop(possible_parent)
  108. model_rep = model_rep + "\t\tself.connectPorts(self.submodels[" + curr_submodel_name + "].my_ports[" + cast_value(read_attribute(model, curr_port, "name")) + "], self.submodels[" + cast_value(read_attribute(model, parent, "name")) + "].my_ports[" + cast_value(read_attribute(model, curr_channel, "name")) + "])\n"
  109. model_rep = model_rep + "\n"
  110. log("Printing model_rep...")
  111. log("\n" + model_rep)
  112. String port
  113. String the_input
  114. String devs_model
  115. port = comm_connect("pypdevs_simulator")
  116. comm_set(port, model_rep)
  117. comm_set(port, "[\"simulate\"]")
  118. while (True):
  119. if (comm_hasInput(port)):
  120. the_input = comm_get(port)
  121. log("(PDEVS) Got input from simulator!")
  122. log("(PDEVS) " + the_input)
  123. sleep(0.05)
  124. return True!