ps_simulator.alc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. pdevs_client_taskname = input()
  22. log("(PS) Got task name from console!")
  23. log("(PS) " + pdevs_client_taskname)
  24. simulation_state = "paused"
  25. while (True):
  26. if (has_input()):
  27. the_input = input()
  28. log("(PS) Got input from console!")
  29. log("(PS) " + cast_v2s(the_input))
  30. if (bool_not(is_physical_string(the_input))):
  31. the_input = cast_v2s(the_input)
  32. parsed_value = json_deserialize(the_input)
  33. log("(PS) Parsed input!")
  34. log("(PS) " + list_to_string(parsed_value))
  35. // TODO: look at simulation state: figure out what to send to simulator (either nothing or big_step). Also save the end condition probably.
  36. command = list_read(parsed_value, 0)
  37. if (command == "big_step"):
  38. if (simulation_state == "paused"):
  39. simulation_state = "big_step"
  40. input_for_devs_sim = list_create()
  41. list_append(input_for_devs_sim, "big_step")
  42. list_append(input_for_devs_sim, list_read(parsed_value, 1))
  43. give_input_to_other(pdevs_client_taskname, json_serialize(input_for_devs_sim))
  44. if (other_has_output(pdevs_client_taskname)):
  45. the_output = get_output_from_other(pdevs_client_taskname)
  46. log("(PS) Got output from pdevs simulator!")
  47. log("(PS) " + the_output)
  48. parsed_value = json_deserialize(the_output)
  49. log("(PS) Parsed output!")
  50. log("(PS) " + dict_to_string(parsed_value))
  51. if (cast_v2s(parsed_value['name']) == 'new_states'):
  52. constructed_output = list_create()
  53. curr_output = detect_convbelt_new_item(parsed_value, model)
  54. if (read_nr_out(dict_keys(curr_output)) > 0):
  55. new_items = dict_create()
  56. dict_add_fast(new_items, "new_items", curr_output)
  57. list_append(constructed_output, new_items)
  58. if (list_len(constructed_output) > 0):
  59. to_output = dict_create()
  60. dict_add_fast(to_output, 'name', 'state_update')
  61. dict_add_fast(to_output, 'parameters', constructed_output)
  62. output(json_serialize(to_output))
  63. else:
  64. // TODO temporary
  65. output(the_output)
  66. sleep(0.05)
  67. Element function detect_convbelt_new_item(state_update: Element, model: Element):
  68. Element params
  69. Element curr_param
  70. Element keys
  71. Element curr_key
  72. Element devs_instances
  73. Element curr_devs_instance
  74. Element generators
  75. Element curr_convbelt
  76. Element splitted_key
  77. Element connected_items
  78. Element curr_item
  79. Element curr_value
  80. String curr_id
  81. Element to_output
  82. Element temp_dict
  83. Boolean item_found
  84. String devs_name
  85. params = state_update['parameters']
  86. to_output = dict_create()
  87. while (list_len(params) > 0):
  88. curr_param = list_pop(params, 0)
  89. keys = dict_keys(curr_param)
  90. while (read_nr_out(keys) > 0):
  91. curr_key = set_pop(keys)
  92. curr_value = curr_param[curr_key]
  93. if (dict_in(curr_value, 'product')):
  94. devs_instances = allInstances(model, 'ParallelDEVS/DEVSInstance')
  95. while (read_nr_out(devs_instances) > 0):
  96. curr_devs_instance = set_pop(devs_instances)
  97. splitted_key = string_split(curr_key, ".")
  98. if (list_read(splitted_key, list_len(splitted_key) - 1) == read_attribute(model, curr_devs_instance, "name")):
  99. break!
  100. log("(PS) We shouldn't reach this point...")
  101. curr_convbelt = set_pop(allAssociationOrigins(model, curr_devs_instance, "PS2DEVS_instancelink"))
  102. if (read_type(model, curr_convbelt) == "ProductionSystemRuntime/ConveyorBelt"):
  103. connected_items = allAssociationDestinations(model, curr_convbelt, "ProductionSystemRuntime/Conv_To_I")
  104. curr_id = cast_i2s(curr_value['product']['id'])
  105. item_found = False
  106. while (read_nr_out(connected_items) > 0):
  107. if (cast_v2s(set_pop(connected_items)) == curr_id):
  108. item_found = True
  109. log("(PS) Item " + curr_id + " found...")
  110. break!
  111. if (bool_not(item_found)):
  112. log("(PS) Item not found, creating...")
  113. instantiate_node(model, ("ProductionSystemRuntime/" + cast_i2s(curr_value['product']['type'])), curr_id)
  114. instantiate_attribute(model, curr_id, "broken", curr_value['product']['broken'])
  115. instantiate_link(model, "ProductionSystemRuntime/Conv_To_I", "", curr_convbelt, curr_id)
  116. temp_dict = dict_create()
  117. dict_add_fast(temp_dict, 'type', ("ProductionSystemRuntime/" + cast_i2s(curr_value['product']['type'])))
  118. dict_add_fast(temp_dict, 'name', curr_id)
  119. dict_add_fast(temp_dict, 'broken', curr_value['product']['broken'])
  120. dict_add_fast(to_output, curr_convbelt, temp_dict)
  121. return to_output!
  122. Boolean function is_big_step_done(state_update: Element):
  123. return True!
  124. Boolean function is_small_step_done(state_update: Element):
  125. return True!