pdevs_client.alc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. log("Translating DEVS to string representation!")
  10. String model_rep
  11. Element all_atomics
  12. Element curr_atomic
  13. Element all_coupleds
  14. Element curr_coupled
  15. Element string_element
  16. Element all_ports
  17. Element all_submodels
  18. Element curr_port
  19. Element curr_submodel
  20. Element out_channels
  21. Element curr_channel
  22. Element possible_parent
  23. Element parent
  24. String curr_port_name
  25. String curr_port_type
  26. String curr_submodel_name
  27. String curr_submodel_type
  28. model_rep = "\nfrom DEVS import *\nfrom infinity import INFINITY\n\n"
  29. all_atomics = allInstances(model, "ParallelDEVS/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=") + cast_value(read_attribute(model, curr_atomic, "name"))) + "):\n"
  34. model_rep = model_rep + "\t\tAtomicDEVS.__init__(self, name)\n"
  35. model_rep = model_rep + "\t\tself.state = self.initialState()\n"
  36. model_rep = model_rep + "\t\tself.my_ports = {"
  37. all_ports = allAssociationDestinations(model, curr_atomic, "ParallelDEVS/DEVSBlockToPort")
  38. while (read_nr_out(all_ports) > 0):
  39. curr_port = set_pop(all_ports)
  40. curr_port_name = cast_value(read_attribute(model, curr_port, "name"))
  41. curr_port_type = read_type(model, curr_port)
  42. if (curr_port_type == "ParallelDEVS/InputPort"):
  43. model_rep = (((model_rep + curr_port_name) + ": self.addInPort(") + curr_port_name) + ")"
  44. if (curr_port_type == "ParallelDEVS/OutputPort"):
  45. model_rep = (((model_rep + curr_port_name) + ": self.addOutPort(") + curr_port_name) + ")"
  46. if (read_nr_out(all_ports) > 0):
  47. model_rep = model_rep + ", "
  48. model_rep = model_rep + "}\n"
  49. model_rep = model_rep + "\tdef initialState(self):\n" + cast_string(read_attribute(model, curr_atomic, "initialState")) + "\n"
  50. model_rep = ((model_rep + "\tdef timeAdvance(self):\n") + cast_string(read_attribute(model, curr_atomic, "timeAdvance"))) + "\n"
  51. model_rep = ((model_rep + "\tdef outputFnc(self):\n") + cast_string(read_attribute(model, curr_atomic, "outputFnc"))) + "\n"
  52. model_rep = ((model_rep + "\tdef intTransition(self):\n") + cast_string(read_attribute(model, curr_atomic, "intTransition"))) + "\n"
  53. model_rep = ((model_rep + "\tdef extTransition(self, my_inputs):\n") + cast_string(read_attribute(model, curr_atomic, "extTransition"))) + "\n"
  54. model_rep = ((model_rep + "\tdef confTransition(self, my_inputs):\n") + cast_string(read_attribute(model, curr_atomic, "confTransition"))) + "\n"
  55. all_coupleds = allInstances(model, "ParallelDEVS/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=") + cast_value(read_attribute(model, curr_coupled, "name"))) + "):\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, "ParallelDEVS/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. log(curr_port_type)
  68. if (curr_port_type == "ParallelDEVS/InputPort"):
  69. model_rep = (((model_rep + curr_port_name) + ": self.addInPort(") + curr_port_name) + ")"
  70. if (curr_port_type == "ParallelDEVS/OutputPort"):
  71. model_rep = (((model_rep + curr_port_name) + ": self.addOutPort(") + curr_port_name) + ")"
  72. if (read_nr_out(all_ports) > 0):
  73. model_rep = model_rep + ", "
  74. model_rep = model_rep + "}\n"
  75. model_rep = model_rep + "\t\tself.submodels = {"
  76. all_submodels = allAssociationDestinations(model, curr_coupled, "ParallelDEVS/SubModel")
  77. while (read_nr_out(all_submodels) > 0):
  78. curr_submodel = set_pop(all_submodels)
  79. curr_submodel_name = cast_value(read_attribute(model, curr_submodel, "name"))
  80. curr_submodel_type = cast_string(read_attribute(model, curr_submodel, "type"))
  81. model_rep = ((((((model_rep + curr_submodel_name) + ": self.addSubModel(") + curr_submodel_type) + "(name=") + curr_submodel_name) + ")") + ")"
  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, "ParallelDEVS/DEVSBlockToPort")
  86. while (read_nr_out(all_ports) > 0):
  87. curr_port = set_pop(all_ports)
  88. out_channels = allAssociationDestinations(model, curr_port, "ParallelDEVS/Channel")
  89. while (read_nr_out(out_channels) > 0):
  90. curr_channel = set_pop(out_channels)
  91. parent = set_pop(allAssociationOrigins(model, curr_channel, "ParallelDEVS/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, "ParallelDEVS/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. log(curr_submodel_name)
  98. all_ports = allAssociationDestinations(model, curr_submodel, "ParallelDEVS/DEVSInstanceToPort")
  99. while (read_nr_out(all_ports) > 0):
  100. curr_port = set_pop(all_ports)
  101. out_channels = allAssociationDestinations(model, curr_port, "ParallelDEVS/Channel")
  102. log(cast_value(read_nr_out(out_channels)))
  103. while (read_nr_out(out_channels) > 0):
  104. curr_channel = set_pop(out_channels)
  105. possible_parent = allAssociationOrigins(model, curr_channel, "ParallelDEVS/DEVSInstanceToPort")
  106. if (read_nr_out(possible_parent) == 0):
  107. 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"
  108. else:
  109. parent = set_pop(possible_parent)
  110. 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"
  111. model_rep = model_rep + "\n"
  112. log("Printing model_rep...")
  113. log("\n" + model_rep)
  114. String port
  115. String the_input
  116. String devs_model
  117. port = comm_connect("pypdevs_simulator")
  118. log("(PDEVS) task name = " + get_taskname())
  119. log("(PDEVS) Sending model...")
  120. comm_set(port, model_rep)
  121. log("(PDEVS) " + port)
  122. while (True):
  123. if (comm_hasInput(port)):
  124. log(cast_value(has_input()))
  125. the_input = comm_get(port)
  126. log("(PDEVS) Got input from simulator!")
  127. log("(PDEVS) " + the_input)
  128. output(the_input)
  129. if (has_input()):
  130. the_input = input()
  131. log("(PDEVS) Got input from console/ps simulator!")
  132. log("(PDEVS) " + cast_value(the_input))
  133. if (is_physical_string(the_input)):
  134. comm_set(port, the_input)
  135. if (the_input == "[\"exit\"]"):
  136. break!
  137. else:
  138. comm_set(port, cast_value(the_input))
  139. sleep(0.05)
  140. return True!