simulate.alc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. model_rep = model_rep + "from pypdevs.DEVS import AtomicDEVS, CoupledDEVS\n"
  30. model_rep = model_rep + "from pypdevs.infinity import INFINITY\n"
  31. all_atomics = allInstances(model, "DEVS/AtomicDEVSBlock")
  32. while (read_nr_out(all_atomics) > 0):
  33. curr_atomic = set_pop(all_atomics)
  34. model_rep = model_rep + "class " + cast_string(read_attribute(model, curr_atomic, "name")) + "(AtomicDEVS):\n"
  35. model_rep = model_rep + "\tdef __init__(self, name, parameters):\n"
  36. model_rep = model_rep + "\t\tself.parameters = parameters\n"
  37. model_rep = model_rep + "\t\tAtomicDEVS.__init__(self, name)\n"
  38. model_rep = model_rep + "\t\tself.initialState()\n"
  39. model_rep = model_rep + "\t\tself.my_ports = {"
  40. all_ports = allAssociationDestinations(model, curr_atomic, "DEVS/DEVSBlockToPort")
  41. while (read_nr_out(all_ports) > 0):
  42. curr_port = set_pop(all_ports)
  43. curr_port_name = cast_value(read_attribute(model, curr_port, "name"))
  44. curr_port_type = read_type(model, curr_port)
  45. if (curr_port_type == "DEVS/InputPort"):
  46. model_rep = model_rep + curr_port_name + ": self.addInPort(" + curr_port_name + ")"
  47. if (curr_port_type == "DEVS/OutputPort"):
  48. model_rep = model_rep + curr_port_name + ": self.addOutPort(" + curr_port_name + ")"
  49. if (read_nr_out(all_ports) > 0):
  50. model_rep = model_rep + ", "
  51. model_rep = model_rep + "}\n"
  52. model_rep = model_rep + "\tdef initialState(self):\n" + cast_string(read_attribute(model, curr_atomic, "initialState")) + "\n"
  53. model_rep = model_rep + "\tdef timeAdvance(self):\n" + cast_string(read_attribute(model, curr_atomic, "timeAdvance")) + "\n"
  54. model_rep = model_rep + "\tdef outputFnc(self):\n" + cast_string(read_attribute(model, curr_atomic, "outputFnc")) + "\n"
  55. model_rep = model_rep + "\tdef intTransition(self):\n" + cast_string(read_attribute(model, curr_atomic, "intTransition")) + "\n"
  56. model_rep = model_rep + "\tdef extTransition(self, inputs):\n" + cast_string(read_attribute(model, curr_atomic, "extTransition")) + "\n"
  57. all_coupleds = allInstances(model, "DEVS/CoupledDEVSBlock")
  58. while (read_nr_out(all_coupleds) > 0):
  59. curr_coupled = set_pop(all_coupleds)
  60. model_rep = model_rep + "class " + cast_string(read_attribute(model, curr_coupled, "name")) + "(CoupledDEVS):\n"
  61. model_rep = model_rep + "\tdef __init__(self, name, parameters):\n"
  62. model_rep = model_rep + "\t\tCoupledDEVS.__init__(self, name)\n"
  63. model_rep = model_rep + "\t\tself.parameters = parameters\n"
  64. model_rep = model_rep + "\t\tself.my_ports = {"
  65. all_ports = allAssociationDestinations(model, curr_coupled, "DEVS/DEVSBlockToPort")
  66. while (read_nr_out(all_ports) > 0):
  67. curr_port = set_pop(all_ports)
  68. curr_port_name = cast_value(read_attribute(model, curr_port, "name"))
  69. curr_port_type = read_type(model, curr_port)
  70. if (curr_port_type == "DEVS/InputPort"):
  71. model_rep = model_rep + curr_port_name + ": self.addInPort(" + curr_port_name + ")"
  72. if (curr_port_type == "DEVS/OutputPort"):
  73. model_rep = model_rep + curr_port_name + ": self.addOutPort(" + curr_port_name + ")"
  74. if (read_nr_out(all_ports) > 0):
  75. model_rep = model_rep + ", "
  76. model_rep = model_rep + "}\n"
  77. model_rep = model_rep + "\t\tself.submodels = {"
  78. all_submodels = allAssociationDestinations(model, curr_coupled, "DEVS/SubModel")
  79. while (read_nr_out(all_submodels) > 0):
  80. curr_submodel = set_pop(all_submodels)
  81. curr_submodel_name = cast_value(read_attribute(model, curr_submodel, "name"))
  82. curr_submodel_type = cast_string(read_attribute(model, curr_submodel, "type"))
  83. curr_submodel_parameters = cast_string(read_attribute(model, curr_submodel, "parameters"))
  84. model_rep = model_rep + curr_submodel_name + ": self.addSubModel(" + curr_submodel_type + "(" + curr_submodel_name + ", " + curr_submodel_parameters + ")" + ")"
  85. if (read_nr_out(all_submodels) > 0):
  86. model_rep = model_rep + ", "
  87. model_rep = model_rep + "}\n"
  88. all_ports = allAssociationDestinations(model, curr_coupled, "DEVS/DEVSBlockToPort")
  89. while (read_nr_out(all_ports) > 0):
  90. curr_port = set_pop(all_ports)
  91. out_channels = allAssociationDestinations(model, curr_port, "DEVS/Channel")
  92. while (read_nr_out(out_channels) > 0):
  93. curr_channel = set_pop(out_channels)
  94. parent = set_pop(allAssociationOrigins(model, curr_channel, "DEVS/DEVSInstanceToPort"))
  95. 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"
  96. all_submodels = allAssociationDestinations(model, curr_coupled, "DEVS/SubModel")
  97. while (read_nr_out(all_submodels) > 0):
  98. curr_submodel = set_pop(all_submodels)
  99. curr_submodel_name = cast_value(read_attribute(model, curr_submodel, "name"))
  100. all_ports = allAssociationDestinations(model, curr_submodel, "DEVS/DEVSInstanceToPort")
  101. while (read_nr_out(all_ports) > 0):
  102. curr_port = set_pop(all_ports)
  103. out_channels = allAssociationDestinations(model, curr_port, "DEVS/Channel")
  104. while (read_nr_out(out_channels) > 0):
  105. curr_channel = set_pop(out_channels)
  106. possible_parent = allAssociationOrigins(model, curr_channel, "DEVS/DEVSInstanceToPort")
  107. if (read_nr_out(possible_parent) == 0):
  108. 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"
  109. else:
  110. parent = set_pop(possible_parent)
  111. 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"
  112. model_rep = model_rep + "\n"
  113. log("Printing model_rep...")
  114. log("\n" + model_rep)
  115. String port
  116. String the_input
  117. String devs_model
  118. port = comm_connect("pypdevs_batch_simulator")
  119. comm_set(port, model_rep)
  120. String experiment
  121. experiment = read_attribute(model, set_pop(allInstances(model, "Experiment/Experiment")), "code")
  122. comm_set(port, experiment)
  123. the_input = comm_get(port)
  124. log(the_input)
  125. return True!