ps_simulator.alc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. include "conformance_scd.alh"
  8. Boolean function main(model : Element):
  9. String pdevs_client_taskname
  10. String the_output
  11. String the_input
  12. String devs_model
  13. String simulation_state
  14. String command
  15. Element input_for_devs_sim
  16. Element params
  17. Element parsed_value
  18. Element curr_output
  19. Element constructed_output
  20. Element to_output
  21. Element new_items
  22. Element curr_param
  23. Element keys
  24. Element curr_key
  25. Element devs_instances
  26. Element curr_devs_instance
  27. Element generators
  28. Element curr_convbelt
  29. Element splitted_key
  30. Element connected_items
  31. Element curr_item
  32. Element curr_value
  33. String curr_id
  34. Element temp_dict
  35. Boolean item_found
  36. String devs_name
  37. Element devs_to_ps
  38. Element ps_to_devs
  39. Element memory
  40. Element convbelt_instances
  41. Element ass_instances
  42. Element curr_ass
  43. pdevs_client_taskname = input()
  44. log("(PS) Got task name from console!")
  45. log("(PS) " + pdevs_client_taskname)
  46. simulation_state = "paused"
  47. memory = dict_create()
  48. dict_add_fast(memory, "detect_initialize", dict_create())
  49. dict_add_fast(memory, "detect_convbelt_new_item", dict_create())
  50. dict_add_fast(memory, "detect_convbelt_deleted_item", dict_create())
  51. log("(PS) building DEVS to PS dictionary...")
  52. devs_to_ps = dict_create()
  53. ps_to_devs = dict_create()
  54. devs_instances = allInstances(model, 'ParallelDEVS/DEVSInstance')
  55. while (read_nr_out(devs_instances) > 0):
  56. curr_devs_instance = set_pop(devs_instances)
  57. dict_add_fast(devs_to_ps, cast_string(read_attribute(model, curr_devs_instance, "name")), set_pop(allAssociationOrigins(model, curr_devs_instance, "PS2DEVS_instancelink")))
  58. convbelt_instances = allInstances(model, 'ProductionSystemRuntime/ConveyorBelt')
  59. while (read_nr_out(convbelt_instances) > 0):
  60. curr_convbelt = set_pop(convbelt_instances)
  61. dict_add_fast(ps_to_devs, curr_convbelt, allAssociationDestinations(model, curr_convbelt, "PS2DEVS_instancelink"))
  62. ass_instances = allInstances(model, 'ProductionSystemRuntime/ConveyorBelt')
  63. while (read_nr_out(ass_instances) > 0):
  64. curr_ass = set_pop(ass_instances)
  65. dict_add_fast(ps_to_devs, curr_ass, allAssociationDestinations(model, curr_ass, "PS2DEVS_instancelink"))
  66. log("(PS) ps_to_devs = " + dict_to_string(ps_to_devs))
  67. while (True):
  68. if (has_input()):
  69. the_input = input()
  70. log("(PS) Got input from console!")
  71. log("(PS) " + cast_value(the_input))
  72. if (bool_not(is_physical_string(the_input))):
  73. the_input = cast_value(the_input)
  74. parsed_value = json_deserialize(the_input)
  75. log("(PS) Parsed input!")
  76. log("(PS) " + list_to_string(parsed_value))
  77. if (list_len(parsed_value) > 0):
  78. // TODO: look at simulation state: figure out what to send to simulator (either nothing or big_step). Also save the end condition probably.
  79. command = list_read(parsed_value, 0)
  80. if (command == "big_step"):
  81. if (simulation_state == "paused"):
  82. simulation_state = "big_step"
  83. input_for_devs_sim = list_create()
  84. list_append(input_for_devs_sim, "big_step")
  85. list_append(input_for_devs_sim, list_read(parsed_value, 1))
  86. give_input_to_other(pdevs_client_taskname, json_serialize(input_for_devs_sim))
  87. if (other_has_output(pdevs_client_taskname)):
  88. the_output = get_output_from_other(pdevs_client_taskname)
  89. log("(PS) Got output from pdevs simulator!")
  90. log("(PS) " + the_output)
  91. parsed_value = json_deserialize(the_output)
  92. log("(PS) Parsed output!")
  93. // log("(PS) " + dict_to_string(parsed_value))
  94. constructed_output = list_create()
  95. if (bool_or((cast_string(parsed_value['name']) == 'new_states'), (cast_string(parsed_value['name']) == 'all_states'))):
  96. detect_initialize_SESSION(parsed_value, model, constructed_output, devs_to_ps, ps_to_devs, memory['detect_initialize'])
  97. detect_convbelt_new_item_SESSION(parsed_value, model, constructed_output, devs_to_ps, ps_to_devs, memory['detect_convbelt_new_item'])
  98. detect_convbelt_deleted_item_SESSION(parsed_value, model, constructed_output, devs_to_ps, ps_to_devs, memory['detect_convbelt_deleted_item'])
  99. detect_ass_new_item_SESSION(parsed_value, model, constructed_output, devs_to_ps, ps_to_devs, memory['detect_convbelt_new_item'])
  100. detect_ass_deleted_item_SESSION(parsed_value, model, constructed_output, devs_to_ps, ps_to_devs, memory['detect_convbelt_deleted_item'])
  101. if (list_len(constructed_output) > 0):
  102. to_output = dict_create()
  103. dict_add_fast(to_output, 'name', 'state_update')
  104. dict_add_fast(to_output, 'params', constructed_output)
  105. output(json_serialize(to_output))
  106. else:
  107. // TODO temporary
  108. output(the_output)
  109. sleep(0.05)
  110. Void function detect_initialize_SESSION(state_update: Element, model: Element, output_list: Element, devs_to_ps: Element, ps_to_devs: Element, memory: Element):
  111. log("(PS) ENTER detect_initialize")
  112. Element params
  113. Element keys
  114. Element curr_key
  115. Element splitted_key
  116. Element curr_element
  117. Element to_output
  118. Element state_updates
  119. to_output = dict_create()
  120. if (cast_string(state_update['name']) == 'all_states'):
  121. params = state_update['params']
  122. if (bool_and((cast_float(params[0][0]) == 0.0), (cast_integer(params[0][1]) == 0))):
  123. log("(PS) initializing...")
  124. keys = dict_keys(params[1])
  125. while (read_nr_out(keys) > 0):
  126. curr_key = set_pop(keys)
  127. splitted_key = string_split(curr_key, ".")
  128. curr_element = devs_to_ps[cast_string(list_read(splitted_key, list_len(splitted_key) - 1))]
  129. if (is_nominal_instance(model, curr_element, "ProductionSystemRuntime/Machine")):
  130. log("(PS) found a machine " + cast_string(curr_element))
  131. instantiate_attribute(model, curr_element, "state", cast_string(params[1][curr_key][1]['name']))
  132. dict_add_fast(to_output, curr_element, cast_string(params[1][curr_key][1]['name']))
  133. if (read_nr_out(dict_keys(to_output)) > 0):
  134. state_updates = dict_create()
  135. dict_add_fast(state_updates, "new_states", to_output)
  136. list_append(output_list, state_updates)
  137. return!
  138. Void function detect_convbelt_new_item_SESSION(state_update: Element, model: Element, output_list: Element, devs_to_ps: Element, ps_to_devs: Element, memory: Element):
  139. log("(PS) ENTER detect_convbelt_new_item")
  140. Element params
  141. Element curr_param
  142. Element keys
  143. Element curr_key
  144. Element devs_instances
  145. Element curr_devs_instance
  146. Element generators
  147. Element curr_convbelt
  148. Element splitted_key
  149. Element connected_items
  150. String curr_item
  151. Element curr_value
  152. String curr_id
  153. Element to_output
  154. Element temp_dict
  155. Element new_items
  156. Boolean item_found
  157. String devs_name
  158. params = state_update['params']
  159. to_output = dict_create()
  160. keys = dict_keys(params[1])
  161. while (read_nr_out(keys) > 0):
  162. curr_key = set_pop(keys)
  163. log("(PS) curr_key = " + cast_string(curr_key))
  164. if (cast_string(state_update['name']) == 'new_states'):
  165. curr_value = params[1][curr_key]
  166. else:
  167. curr_value = params[1][curr_key][1]
  168. log("(PS) curr_value = " + dict_to_string(curr_value))
  169. log(cast_value(dict_in(curr_value, 'product')))
  170. if (bool_and(dict_in(curr_value, 'product'), bool_not(is_physical_none(curr_value['product'])))):
  171. splitted_key = string_split(curr_key, ".")
  172. curr_convbelt = devs_to_ps[cast_string(list_read(splitted_key, list_len(splitted_key) - 1))]
  173. if (read_type(model, curr_convbelt) == "ProductionSystemRuntime/ConveyorBelt"):
  174. connected_items = allAssociationDestinations(model, curr_convbelt, "ProductionSystemRuntime/Conv_To_I")
  175. curr_id = cast_string(curr_value['product']['id'])
  176. item_found = False
  177. while (read_nr_out(connected_items) > 0):
  178. curr_item = cast_string(set_pop(connected_items))
  179. if (curr_item == curr_id):
  180. item_found = True
  181. log("(PS) Item " + curr_id + " found...")
  182. break!
  183. if (bool_not(item_found)):
  184. log("(PS) Item " + curr_id + " not found, creating...")
  185. instantiate_node(model, ("ProductionSystemRuntime/" + cast_string(curr_value['product']['type'])), curr_id)
  186. instantiate_attribute(model, curr_id, "broken", curr_value['product']['broken'])
  187. instantiate_link(model, "ProductionSystemRuntime/Conv_To_I", "", curr_convbelt, curr_id)
  188. temp_dict = dict_create()
  189. dict_add_fast(temp_dict, 'type', (cast_string(curr_value['product']['type'])))
  190. dict_add_fast(temp_dict, 'name', curr_id)
  191. dict_add_fast(temp_dict, 'broken', curr_value['product']['broken'])
  192. dict_add_fast(to_output, curr_convbelt, temp_dict)
  193. if (read_nr_out(dict_keys(to_output)) > 0):
  194. new_items = dict_create()
  195. dict_add_fast(new_items, "new_items", to_output)
  196. list_append(output_list, new_items)
  197. return!
  198. Void function detect_convbelt_deleted_item_SESSION(state_update: Element, model: Element, output_list: Element, devs_to_ps: Element, ps_to_devs: Element, memory: Element):
  199. log("(PS) ENTER detect_convbelt_deleted_item")
  200. Element params
  201. Element curr_param
  202. Element keys
  203. Element curr_key
  204. Element devs_instances
  205. Element curr_devs_instance
  206. Element generators
  207. Element curr_convbelt
  208. Element splitted_key
  209. Element connected_items
  210. Element curr_item
  211. Element curr_value
  212. String curr_id
  213. Element to_output
  214. Element temp_dict
  215. Element removed_items
  216. String curr_instance
  217. Boolean item_found
  218. String devs_name
  219. Element items
  220. Element prev_items
  221. Element new_memory
  222. Element conveyorbelts
  223. Element items_keys
  224. Element prev_items_keys
  225. new_memory = dict_copy(memory)
  226. params = state_update['params']
  227. to_output = dict_create()
  228. keys = dict_keys(params[1])
  229. while (read_nr_out(keys) > 0):
  230. curr_key = set_pop(keys)
  231. if (cast_string(state_update['name']) == 'new_states'):
  232. curr_value = params[1][curr_key]
  233. else:
  234. curr_value = params[1][curr_key][1]
  235. if (dict_in(curr_value, 'product')):
  236. splitted_key = string_split(curr_key, ".")
  237. if (bool_and(dict_in(new_memory, cast_string(list_read(splitted_key, list_len(splitted_key) - 1))), is_physical_none(curr_value['product']))):
  238. dict_delete(new_memory, cast_string(list_read(splitted_key, list_len(splitted_key) - 1)))
  239. elif (bool_not(is_physical_none(curr_value['product']))):
  240. dict_overwrite(new_memory, cast_string(list_read(splitted_key, list_len(splitted_key) - 1)), curr_value['product'])
  241. conveyorbelts = allInstances(model, "ProductionSystemRuntime/ConveyorBelt")
  242. while (read_nr_out(conveyorbelts) > 0):
  243. curr_convbelt = set_pop(conveyorbelts)
  244. devs_instances = set_copy(ps_to_devs[curr_convbelt])
  245. prev_items = dict_create()
  246. items = dict_create()
  247. while (read_nr_out(devs_instances) > 0):
  248. curr_instance = cast_string(read_attribute(model, set_pop(devs_instances), "name"))
  249. if (bool_and(dict_in(memory, curr_instance), bool_not(is_physical_none(memory[curr_instance])))):
  250. dict_add_fast(prev_items, cast_string(memory[curr_instance]['id']), memory[curr_instance])
  251. if (bool_and(dict_in(new_memory, curr_instance), bool_not(is_physical_none(new_memory[curr_instance])))):
  252. dict_add_fast(items, cast_string(new_memory[curr_instance]['id']), new_memory[curr_instance])
  253. prev_items_keys = dict_keys(prev_items)
  254. items_keys = dict_keys(items)
  255. while(read_nr_out(prev_items_keys) > 0):
  256. curr_item = set_pop(prev_items_keys)
  257. if (bool_not(set_in(items_keys, curr_item))):
  258. log("(PS) item " + cast_string(curr_item) + " removed")
  259. temp_dict = dict_create()
  260. dict_add_fast(temp_dict, 'name', curr_item)
  261. dict_add_fast(to_output, curr_convbelt, temp_dict)
  262. model_delete_element(model, curr_item)
  263. else:
  264. log("(PS) item " + cast_string(curr_item) + " NOT removed")
  265. keys = dict_keys(new_memory)
  266. while (read_nr_out(keys) > 0):
  267. curr_key = set_pop(keys)
  268. dict_overwrite(memory, curr_key, new_memory[curr_key])
  269. if (read_nr_out(dict_keys(to_output)) > 0):
  270. removed_items = dict_create()
  271. dict_add_fast(removed_items, "removed_items", to_output)
  272. list_append(output_list, removed_items)
  273. return!
  274. Void function detect_ass_new_item_SESSION(state_update: Element, model: Element, output_list: Element, devs_to_ps: Element, ps_to_devs: Element, memory: Element):
  275. log("(PS) ENTER detect_ass_new_item")
  276. Element params
  277. Element curr_param
  278. Element keys
  279. Element curr_key
  280. Element devs_instances
  281. Element curr_devs_instance
  282. Element generators
  283. Element curr_ass
  284. Element splitted_key
  285. Element connected_items
  286. String curr_item
  287. Element curr_value
  288. String curr_id
  289. Element to_output
  290. Element temp_dict
  291. Element new_items
  292. Boolean item_found
  293. String devs_name
  294. Element queue
  295. params = state_update['params']
  296. to_output = dict_create()
  297. keys = dict_keys(params[1])
  298. while (read_nr_out(keys) > 0):
  299. curr_key = set_pop(keys)
  300. log("(PS) curr_key = " + cast_string(curr_key))
  301. if (cast_string(state_update['name']) == 'new_states'):
  302. curr_value = params[1][curr_key]
  303. else:
  304. curr_value = params[1][curr_key][1]
  305. log("(PS) curr_value = " + dict_to_string(curr_value))
  306. log(cast_value(dict_in(curr_value, 'queue')))
  307. if (dict_in(curr_value, 'queue')):
  308. if (list_len(curr_value['queue']) > 0):
  309. queue = curr_value['queue']
  310. splitted_key = string_split(curr_key, ".")
  311. curr_ass = devs_to_ps[cast_string(list_read(splitted_key, list_len(splitted_key) - 1))]
  312. if (read_type(model, curr_ass) == "ProductionSystemRuntime/Assembler"):
  313. connected_items = allAssociationDestinations(model, curr_ass, "ProductionSystemRuntime/Proc_To_I")
  314. while (read_nr_out(queue) > 0):
  315. curr_id = cast_string(dict_read(list_pop_final(queue), 'id'))
  316. item_found = False
  317. while (read_nr_out(connected_items) > 0):
  318. curr_item = cast_string(set_pop(connected_items))
  319. if (curr_item == curr_id):
  320. item_found = True
  321. log("(PS) Item " + curr_id + " found...")
  322. break!
  323. if (bool_not(item_found)):
  324. log("(PS) Item " + curr_id + " not found, creating...")
  325. instantiate_node(model, ("ProductionSystemRuntime/" + cast_string(curr_value['product']['type'])), curr_id)
  326. instantiate_attribute(model, curr_id, "broken", curr_value['product']['broken'])
  327. instantiate_link(model, "ProductionSystemRuntime/P_To_I", "", curr_ass, curr_id)
  328. temp_dict = dict_create()
  329. dict_add_fast(temp_dict, 'type', (cast_string(curr_value['product']['type'])))
  330. dict_add_fast(temp_dict, 'name', curr_id)
  331. dict_add_fast(temp_dict, 'broken', curr_value['product']['broken'])
  332. dict_add_fast(to_output, curr_ass, temp_dict)
  333. if (read_nr_out(dict_keys(to_output)) > 0):
  334. new_items = dict_create()
  335. dict_add_fast(new_items, "new_items", to_output)
  336. list_append(output_list, new_items)
  337. return!
  338. Void function detect_ass_deleted_item_SESSION(state_update: Element, model: Element, output_list: Element, devs_to_ps: Element, ps_to_devs: Element, memory: Element):
  339. log("(PS) ENTER detect_ass_deleted_item")
  340. Element params
  341. Element curr_param
  342. Element keys
  343. Element curr_key
  344. Element devs_instances
  345. Element curr_devs_instance
  346. Element generators
  347. Element curr_ass
  348. Element splitted_key
  349. Element connected_items
  350. String curr_item
  351. Element curr_value
  352. String curr_id
  353. Element to_output
  354. Element temp_dict
  355. Element removed_items
  356. String curr_instance
  357. Boolean item_found
  358. String devs_name
  359. Element curr_queue
  360. Element curr_queue_keys
  361. Element prev_queue
  362. Element new_memory
  363. Element assemblers
  364. Element items_keys
  365. Element prev_items_keys
  366. Element to_remove
  367. new_memory = dict_copy(memory)
  368. params = state_update['params']
  369. to_output = dict_create()
  370. keys = dict_keys(params[1])
  371. while (read_nr_out(keys) > 0):
  372. curr_key = set_pop(keys)
  373. if (cast_string(state_update['name']) == 'new_states'):
  374. curr_value = params[1][curr_key]
  375. else:
  376. curr_value = params[1][curr_key][1]
  377. if (dict_in(curr_value, 'queue')):
  378. splitted_key = string_split(curr_key, ".")
  379. dict_overwrite(new_memory, cast_string(list_read(splitted_key, list_len(splitted_key) - 1)), curr_value['queue'])
  380. assemblers = allInstances(model, "ProductionSystemRuntime/Assembler")
  381. while (read_nr_out(assemblers) > 0):
  382. curr_ass = set_pop(assemblers)
  383. to_remove = list_create()
  384. curr_devs_instance = ps_to_devs[curr_ass]
  385. if (dict_in(memory, curr_devs_instance)):
  386. prev_queue = list_copy(memory[curr_devs_instance])
  387. curr_queue_keys = set_create()
  388. curr_queue = list_copy(new_memory[curr_devs_instance])
  389. while (read_nr_out(curr_queue) > 0):
  390. set_add(curr_queue_keys, cast_string(dict_read(list_pop_final(curr_queue), 'id')))
  391. while(read_nr_out(prev_queue) > 0):
  392. curr_item = cast_string(curr_item['id'])
  393. if (bool_not(set_in(curr_queue_keys, curr_item))):
  394. log("(PS) item " + curr_item + " removed")
  395. temp_dict = dict_create()
  396. dict_add_fast(temp_dict, 'name', curr_item)
  397. list_append(to_remove, temp_dict)
  398. model_delete_element(model, curr_item)
  399. else:
  400. log("(PS) item " + curr_item + " NOT removed")
  401. if (read_nr_out(to_remove) > 0):
  402. dict_add_fast(to_output, curr_ass, to_remove)
  403. keys = dict_keys(new_memory)
  404. while (read_nr_out(keys) > 0):
  405. curr_key = set_pop(keys)
  406. dict_overwrite(memory, curr_key, new_memory[curr_key])
  407. if (read_nr_out(dict_keys(to_output)) > 0):
  408. removed_items = dict_create()
  409. dict_add_fast(removed_items, "removed_items", to_output)
  410. list_append(output_list, removed_items)
  411. return!
  412. Boolean function is_big_step_done(state_update: Element):
  413. return True!
  414. Boolean function is_small_step_done(state_update: Element):
  415. return True!