ps_simulator.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. include "primitives.alh"
  2. include "services.alh"
  3. include "modelling.alh"
  4. include "object_operations.alh"
  5. include "io.alh"
  6. include "json.alh"
  7. Boolean function main(model : Element):
  8. String pdevs_client_taskname
  9. String the_output
  10. String the_input
  11. String devs_model
  12. String simulation_state
  13. String command
  14. Element input_for_devs_sim
  15. Element params
  16. Element parsed_value
  17. Element curr_output
  18. Element constructed_output
  19. Element to_output
  20. Element new_items
  21. Element curr_param
  22. Element keys
  23. Element curr_key
  24. Element devs_instances
  25. Element curr_devs_instance
  26. Element generators
  27. Element curr_convbelt
  28. Element splitted_key
  29. Element connected_items
  30. Element curr_item
  31. Element curr_value
  32. String curr_id
  33. Element temp_dict
  34. Boolean item_found
  35. String devs_name
  36. pdevs_client_taskname = input()
  37. log("(PS) Got task name from console!")
  38. log("(PS) " + pdevs_client_taskname)
  39. simulation_state = "paused"
  40. while (True):
  41. if (has_input()):
  42. the_input = input()
  43. log("(PS) Got input from console!")
  44. log("(PS) " + cast_value(the_input))
  45. if (bool_not(is_physical_string(the_input))):
  46. the_input = cast_value(the_input)
  47. parsed_value = json_deserialize(the_input)
  48. log("(PS) Parsed input!")
  49. log("(PS) " + list_to_string(parsed_value))
  50. // TODO: look at simulation state: figure out what to send to simulator (either nothing or big_step). Also save the end condition probably.
  51. command = list_read(parsed_value, 0)
  52. if (command == "big_step"):
  53. if (simulation_state == "paused"):
  54. simulation_state = "big_step"
  55. input_for_devs_sim = list_create()
  56. list_append(input_for_devs_sim, "big_step")
  57. list_append(input_for_devs_sim, list_read(parsed_value, 1))
  58. give_input_to_other(pdevs_client_taskname, json_serialize(input_for_devs_sim))
  59. if (other_has_output(pdevs_client_taskname)):
  60. the_output = get_output_from_other(pdevs_client_taskname)
  61. log("(PS) Got output from pdevs simulator!")
  62. log("(PS) " + the_output)
  63. parsed_value = json_deserialize(the_output)
  64. log("(PS) Parsed output!")
  65. // log("(PS) " + dict_to_string(parsed_value))
  66. constructed_output = list_create()
  67. if (bool_or((cast_string(parsed_value['name']) == 'new_states'), (cast_string(parsed_value['name']) == 'all_states'))):
  68. // curr_output = detect_convbelt_new_item(parsed_value, model)
  69. log("(PS) ENTER detect_convbelt_new_item")
  70. params = parsed_value['params']
  71. curr_output = dict_create()
  72. keys = dict_keys(params[1])
  73. while (read_nr_out(keys) > 0):
  74. curr_key = set_pop(keys)
  75. log("(PS) curr_key = " + cast_string(curr_key))
  76. if (cast_string(parsed_value['name']) == 'new_states'):
  77. curr_value = params[1][curr_key]
  78. else:
  79. curr_value = params[1][curr_key][1]
  80. log("(PS) curr_value = " + dict_to_string(curr_value))
  81. log(cast_value(dict_in(curr_value, 'product')))
  82. if (bool_and(dict_in(curr_value, 'product'), bool_not(is_physical_none(curr_value['product'])))):
  83. devs_instances = allInstances(model, 'ParallelDEVS/DEVSInstance')
  84. while (read_nr_out(devs_instances) > 0):
  85. curr_devs_instance = set_pop(devs_instances)
  86. splitted_key = string_split(curr_key, ".")
  87. log("(PS) checking key = " + cast_string(list_read(splitted_key, list_len(splitted_key) - 1)))
  88. log("(PS) checking name = " + cast_string(read_attribute(model, curr_devs_instance, "name")))
  89. if (cast_string(list_read(splitted_key, list_len(splitted_key) - 1)) == cast_string(read_attribute(model, curr_devs_instance, "name"))):
  90. break!
  91. if (read_nr_out(devs_instances) == 0):
  92. log("(PS) We shouldn't reach this point..")
  93. log("(PS) found: " + cast_string(curr_devs_instance))
  94. curr_convbelt = set_pop(allAssociationOrigins(model, curr_devs_instance, "PS2DEVS_instancelink"))
  95. if (read_type(model, curr_convbelt) == "ProductionSystemRuntime/ConveyorBelt"):
  96. connected_items = allAssociationDestinations(model, curr_convbelt, "ProductionSystemRuntime/Conv_To_I")
  97. curr_id = cast_string(curr_value['product']['id'])
  98. item_found = False
  99. while (read_nr_out(connected_items) > 0):
  100. if (cast_value(set_pop(connected_items)) == curr_id):
  101. item_found = True
  102. log("(PS) Item " + curr_id + " found...")
  103. break!
  104. if (bool_not(item_found)):
  105. log("(PS) Item not found, creating...")
  106. instantiate_node(model, ("ProductionSystemRuntime/" + cast_string(curr_value['product']['type'])), curr_id)
  107. instantiate_attribute(model, curr_id, "broken", curr_value['product']['broken'])
  108. instantiate_link(model, "ProductionSystemRuntime/Conv_To_I", "", curr_convbelt, curr_id)
  109. temp_dict = dict_create()
  110. dict_add_fast(temp_dict, 'type', ("ProductionSystemRuntime/" + cast_string(curr_value['product']['type'])))
  111. dict_add_fast(temp_dict, 'name', curr_id)
  112. dict_add_fast(temp_dict, 'broken', curr_value['product']['broken'])
  113. dict_add_fast(curr_output, curr_convbelt, temp_dict)
  114. if (read_nr_out(dict_keys(curr_output)) > 0):
  115. new_items = dict_create()
  116. dict_add_fast(new_items, "new_items", curr_output)
  117. list_append(constructed_output, new_items)
  118. if (list_len(constructed_output) > 0):
  119. to_output = dict_create()
  120. dict_add_fast(to_output, 'name', 'state_update')
  121. dict_add_fast(to_output, 'parameters', constructed_output)
  122. output(json_serialize(to_output))
  123. else:
  124. // TODO temporary
  125. output(the_output)
  126. sleep(0.05)
  127. Element function detect_convbelt_new_item(state_update: Element, model: Element):
  128. log("(PS) ENTER detect_convbelt_new_item")
  129. Element params
  130. Element curr_param
  131. Element keys
  132. Element curr_key
  133. Element devs_instances
  134. Element curr_devs_instance
  135. Element generators
  136. Element curr_convbelt
  137. Element splitted_key
  138. Element connected_items
  139. Element curr_item
  140. Element curr_value
  141. String curr_id
  142. Element to_output
  143. Element temp_dict
  144. Boolean item_found
  145. String devs_name
  146. params = state_update['params']
  147. to_output = dict_create()
  148. keys = dict_keys(params[1])
  149. while (read_nr_out(keys) > 0):
  150. curr_key = set_pop(keys)
  151. log("(PS) curr_key = " + cast_string(curr_key))
  152. curr_value = curr_param[curr_key]
  153. if (dict_in(curr_value, 'product')):
  154. devs_instances = allInstances(model, 'ParallelDEVS/DEVSInstance')
  155. while (read_nr_out(devs_instances) > 0):
  156. curr_devs_instance = set_pop(devs_instances)
  157. splitted_key = string_split(curr_key, ".")
  158. log("(PS) checking key = " + cast_string(list_read(splitted_key, list_len(splitted_key) - 1)))
  159. log("(PS) checking name = " + cast_string(read_attribute(model, curr_devs_instance, "name")))
  160. if (cast_string(list_read(splitted_key, list_len(splitted_key) - 1)) == cast_string(read_attribute(model, curr_devs_instance, "name"))):
  161. break!
  162. if (read_nr_out(devs_instances) == 0):
  163. log("(PS) We shouldn't reach this point..")
  164. curr_convbelt = set_pop(allAssociationOrigins(model, curr_devs_instance, "PS2DEVS_instancelink"))
  165. if (read_type(model, curr_convbelt) == "ProductionSystemRuntime/ConveyorBelt"):
  166. connected_items = allAssociationDestinations(model, curr_convbelt, "ProductionSystemRuntime/Conv_To_I")
  167. curr_id = cast_string(curr_value['product']['id'])
  168. item_found = False
  169. while (read_nr_out(connected_items) > 0):
  170. if (cast_value(set_pop(connected_items)) == curr_id):
  171. item_found = True
  172. log("(PS) Item " + curr_id + " found...")
  173. break!
  174. if (bool_not(item_found)):
  175. log("(PS) Item not found, creating...")
  176. instantiate_node(model, ("ProductionSystemRuntime/" + cast_string(curr_value['product']['type'])), curr_id)
  177. instantiate_attribute(model, curr_id, "broken", curr_value['product']['broken'])
  178. instantiate_link(model, "ProductionSystemRuntime/Conv_To_I", "", curr_convbelt, curr_id)
  179. temp_dict = dict_create()
  180. dict_add_fast(temp_dict, 'type', ("ProductionSystemRuntime/" + cast_string(curr_value['product']['type'])))
  181. dict_add_fast(temp_dict, 'name', curr_id)
  182. dict_add_fast(temp_dict, 'broken', curr_value['product']['broken'])
  183. dict_add_fast(to_output, curr_convbelt, temp_dict)
  184. return to_output!
  185. Boolean function is_big_step_done(state_update: Element):
  186. return True!
  187. Boolean function is_small_step_done(state_update: Element):
  188. return True!