cbd_semantics.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "library.alh"
  5. include "conformance_scd.alh"
  6. include "mini_modify.alh"
  7. Void function main():
  8. Element model
  9. String verify_result
  10. while (True):
  11. output("Which model do you want to execute with CBD semantics?")
  12. model = import_node(input())
  13. if (element_eq(model, read_root())):
  14. output("Could not find model; aborting")
  15. elif (element_neq(model["metamodel"], import_node("models/CausalBlockDiagrams_Design"))):
  16. output("Not a CBD design model; aborting")
  17. else:
  18. verify_result = conformance_scd(model)
  19. if (verify_result == "OK"):
  20. output("Model OK!")
  21. log("Model conforms!")
  22. execute_cbd(model)
  23. else:
  24. output("Non-conforming model: " + verify_result)
  25. Element function retype_to_runtime(design_model : Element):
  26. Element runtime_model
  27. Element all_blocks
  28. Element all_links
  29. String mm_type_name
  30. String element_name
  31. String attr_name
  32. String attr_value
  33. String attribute
  34. String src
  35. String dst
  36. String time
  37. Element all_attributes
  38. runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  39. all_blocks = allInstances(design_model, "Block")
  40. while (list_len(all_blocks) > 0):
  41. element_name = set_pop(all_blocks)
  42. mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(design_model["type_mapping"], design_model["model"][element_name]))
  43. element_name = instantiate_node(runtime_model, mm_type_name, element_name)
  44. if (mm_type_name == "ConstantBlock"):
  45. instantiate_attribute(runtime_model, element_name, "value", read_attribute(design_model, element_name, "value"))
  46. // Don't merge this together with the block conversion, as the destination block might not exist yet!
  47. all_links = allInstances(design_model, "Link")
  48. while (read_nr_out(all_links) > 0):
  49. element_name = set_pop(all_links)
  50. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  51. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  52. instantiate_link(runtime_model, "Link", element_name, src, dst)
  53. all_links = allInstances(design_model, "InitialCondition")
  54. while (read_nr_out(all_links) > 0):
  55. element_name = set_pop(all_links)
  56. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  57. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  58. instantiate_link(runtime_model, "InitialCondition", element_name, src, dst)
  59. return runtime_model!
  60. Element function sanitize(new_runtime_model : Element, old_runtime_model : Element):
  61. Element all_blocks
  62. Element all_links
  63. String mm_type_name
  64. String element_name
  65. String attr_name
  66. String attr_value
  67. String attribute
  68. String time
  69. Element all_attributes
  70. Integer current_time
  71. Integer termination_time
  72. all_blocks = allInstances(new_runtime_model, "Block")
  73. while (list_len(all_blocks) > 0):
  74. element_name = set_pop(all_blocks)
  75. mm_type_name = reverseKeyLookup(new_runtime_model["metamodel"]["model"], dict_read_node(new_runtime_model["type_mapping"], new_runtime_model["model"][element_name]))
  76. if (dict_in(old_runtime_model["model"], element_name)):
  77. if (mm_type_name == "DelayBlock"):
  78. instantiate_attribute(new_runtime_model, element_name, "memory", read_attribute(old_runtime_model, element_name, "memory"))
  79. instantiate_attribute(new_runtime_model, element_name, "signal", read_attribute(old_runtime_model, element_name, "signal"))
  80. else:
  81. if (mm_type_name == "DelayBlock"):
  82. instantiate_attribute(new_runtime_model, element_name, "memory", 0.0)
  83. instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
  84. if (dict_in(old_runtime_model["model"], "time")):
  85. current_time = read_attribute(old_runtime_model, "time", "current_time")
  86. termination_time = read_attribute(old_runtime_model, "time", "termination_time")
  87. else:
  88. current_time = 0
  89. termination_time = 10
  90. create_schedule(new_runtime_model, old_runtime_model, current_time)
  91. create_schedule(new_runtime_model, old_runtime_model, -1)
  92. time = instantiate_node(new_runtime_model, "Time", "time")
  93. instantiate_attribute(new_runtime_model, time, "start_time", current_time)
  94. instantiate_attribute(new_runtime_model, time, "current_time", current_time)
  95. instantiate_attribute(new_runtime_model, time, "termination_time", termination_time)
  96. return new_runtime_model!
  97. Void function create_schedule(model : Element, old_model : Element, start_time : Integer):
  98. Element all_blocks
  99. Element visited
  100. Element to_visit
  101. Element incoming_links
  102. String schedule
  103. String element_name
  104. String link
  105. String source
  106. String new_schedule
  107. Boolean ready
  108. all_blocks = allInstances(model, "Block")
  109. visited = create_node()
  110. to_visit = create_node()
  111. if (start_time != -1):
  112. schedule = instantiate_node(model, "Schedule", "schedule_init")
  113. else:
  114. schedule = instantiate_node(model, "Schedule", "schedule_run")
  115. while (read_nr_out(all_blocks) > 0):
  116. element_name = set_pop(all_blocks)
  117. if (bool_not(set_in(visited, element_name))):
  118. list_append(to_visit, element_name)
  119. while (list_len(to_visit) > 0):
  120. element_name = list_read(to_visit, list_len(to_visit) - 1)
  121. if (reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][element_name])) == "DelayBlock"):
  122. if (dict_in(old_model["model"], element_name)):
  123. incoming_links = create_node()
  124. else:
  125. incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
  126. else:
  127. incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
  128. ready = True
  129. while (list_len(incoming_links) > 0):
  130. link = set_pop(incoming_links)
  131. source = readAssociationSource(model, link)
  132. if (bool_not(set_in(visited, source))):
  133. list_append(to_visit, source)
  134. ready = False
  135. if (ready):
  136. new_schedule = instantiate_node(model, "Schedule", "")
  137. instantiate_link(model, "LinkedBlock", "", schedule, element_name)
  138. instantiate_link(model, "NextSchedule", "", schedule, new_schedule)
  139. schedule = new_schedule
  140. list_delete(to_visit, list_len(to_visit) - 1)
  141. set_add(visited, element_name)
  142. return !
  143. String function readType(model : Element, name : String):
  144. return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
  145. Void function list_CBD(model : Element):
  146. Element all_elements
  147. String elem
  148. String block
  149. output("Blocks:")
  150. all_elements = allInstances(model, "Block")
  151. while (read_nr_out(all_elements) > 0):
  152. elem = set_pop(all_elements)
  153. output(((" " + elem) + ": ") + readType(model, elem))
  154. output("Links:")
  155. all_elements = allInstances(model, "Link")
  156. while (read_nr_out(all_elements) > 0):
  157. elem = set_pop(all_elements)
  158. output(((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][elem]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][elem])))
  159. output("Initial conditions:")
  160. all_elements = allInstances(model, "InitialCondition")
  161. while (read_nr_out(all_elements) > 0):
  162. elem = set_pop(all_elements)
  163. output(((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][elem]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][elem])))
  164. output(("Schedule (t = " + cast_v2s(read_attribute(model, "time", "start_time"))) + "):")
  165. elem = "schedule_init"
  166. while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
  167. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
  168. output(" " + block)
  169. elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
  170. output(("Schedule (t > " + cast_v2s(read_attribute(model, "time", "start_time"))) + "):")
  171. elem = "schedule_run"
  172. while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
  173. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
  174. output(" " + block)
  175. elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
  176. return !
  177. Void function step_simulation(model : Element):
  178. String time
  179. String schedule
  180. Float signal
  181. Element incoming
  182. String selected
  183. String block
  184. String elem
  185. String blocktype
  186. String output_string
  187. Element delay_blocks
  188. time = "time"
  189. if (read_attribute(model, time, "current_time") == read_attribute(model, time, "start_time")):
  190. schedule = "schedule_init"
  191. else:
  192. schedule = "schedule_run"
  193. delay_blocks = create_node()
  194. output_string = "("
  195. while (read_nr_out(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")) > 0):
  196. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")))
  197. schedule = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "NextSchedule")))
  198. // Execute "block"
  199. blocktype = readType(model, block)
  200. if (blocktype == "ConstantBlock"):
  201. signal = read_attribute(model, block, "value")
  202. elif (blocktype == "AdditionBlock"):
  203. signal = 0.0
  204. incoming = allIncomingAssociationInstances(model, block, "Link")
  205. while (read_nr_out(incoming) > 0):
  206. selected = readAssociationSource(model, set_pop(incoming))
  207. signal = signal + cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  208. elif (blocktype == "MultiplyBlock"):
  209. signal = 1.0
  210. incoming = allIncomingAssociationInstances(model, block, "Link")
  211. while (read_nr_out(incoming) > 0):
  212. selected = readAssociationSource(model, set_pop(incoming))
  213. signal = signal * cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  214. elif (blocktype == "NegatorBlock"):
  215. incoming = allIncomingAssociationInstances(model, block, "Link")
  216. while (read_nr_out(incoming) > 0):
  217. selected = readAssociationSource(model, set_pop(incoming))
  218. signal = float_neg(cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  219. elif (blocktype == "InverseBlock"):
  220. incoming = allIncomingAssociationInstances(model, block, "Link")
  221. while (read_nr_out(incoming) > 0):
  222. selected = readAssociationSource(model, set_pop(incoming))
  223. signal = float_division(1.0, cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  224. elif (blocktype == "DelayBlock"):
  225. if (cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) == 0):
  226. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  227. while (read_nr_out(incoming) > 0):
  228. selected = readAssociationSource(model, set_pop(incoming))
  229. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  230. else:
  231. signal = read_attribute(model, block, "memory")
  232. set_add(delay_blocks, block)
  233. unset_attribute(model, block, "signal")
  234. instantiate_attribute(model, block, "signal", signal)
  235. output_string = output_string + (((block + " = ") + cast_v2s(signal)) + "; ")
  236. output_string = output_string + ")"
  237. while (read_nr_out(delay_blocks) > 0):
  238. block = set_pop(delay_blocks)
  239. // Update memory
  240. incoming = allIncomingAssociationInstances(model, block, "Link")
  241. while (read_nr_out(incoming) > 0):
  242. selected = readAssociationSource(model, set_pop(incoming))
  243. unset_attribute(model, block, "memory")
  244. instantiate_attribute(model, block, "memory", cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  245. // Increase simulation time
  246. Integer new_time
  247. new_time = cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) + 1
  248. unset_attribute(model, time, "current_time")
  249. instantiate_attribute(model, time, "current_time", new_time)
  250. output(output_string)
  251. return !
  252. Void function set_termination_time(model : Element):
  253. unset_attribute(model, "time", "termination_time")
  254. instantiate_attribute(model, "time", "termination_time", input())
  255. return !
  256. Void function execute_cbd(design_model : Element):
  257. String verify_result
  258. Element runtime_model
  259. Element old_runtime_model
  260. String cmd
  261. Boolean running
  262. log("New model")
  263. old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  264. log("Retype")
  265. runtime_model = retype_to_runtime(design_model)
  266. log("Sanitize")
  267. runtime_model = sanitize(runtime_model, old_runtime_model)
  268. log("Finished")
  269. running = False
  270. while (True):
  271. if (running):
  272. if (has_input()):
  273. cmd = input()
  274. else:
  275. cmd = "step"
  276. else:
  277. output("Which operation do you want to execute?")
  278. cmd = input()
  279. if (cmd == "help"):
  280. output("Supported operations:")
  281. if (bool_not(running)):
  282. output(" step -- do one simulation step")
  283. output(" start -- start simulation")
  284. output(" modify -- live modelling: modify model structure")
  285. output(" list -- list blocks and connections")
  286. output(" exit -- select another model")
  287. output(" verify -- verify the runtime model")
  288. else:
  289. output(" pause -- pause simulation")
  290. output(" help -- this information")
  291. output(" term -- set termination time")
  292. elif (cmd == "step"):
  293. // Do a simulation step
  294. step_simulation(runtime_model)
  295. elif (cmd == "term"):
  296. set_termination_time(runtime_model)
  297. elif (running):
  298. if (cmd == "pause"):
  299. running = False
  300. else:
  301. output("Did not understand command!")
  302. output("Use 'help' for a list of available options")
  303. else:
  304. // Not running
  305. if (cmd == "list"):
  306. list_CBD(runtime_model)
  307. elif (cmd == "start"):
  308. running = True
  309. elif (cmd == "exit"):
  310. return!
  311. elif (cmd == "modify"):
  312. verify_result = "init"
  313. while (verify_result != "OK"):
  314. modify(design_model)
  315. verify_result = conformance_scd(design_model)
  316. if (verify_result != "OK"):
  317. output("Error in design model: " + verify_result)
  318. output("Successfully made modifications to design model!")
  319. old_runtime_model = runtime_model
  320. runtime_model = retype_to_runtime(design_model)
  321. runtime_model = sanitize(runtime_model, old_runtime_model)
  322. elif (cmd == "verify"):
  323. verify_result = conformance_scd(runtime_model)
  324. if (verify_result != "OK"):
  325. output("Error in runtime model: " + verify_result)
  326. else:
  327. output("Runtime model OK!")
  328. else:
  329. output("Did not understand command!")
  330. output("Use 'help' for a list of available options")
  331. if (cast_s2i(cast_v2s(read_attribute(runtime_model, "time", "current_time"))) > cast_s2i(cast_v2s(read_attribute(runtime_model, "time", "termination_time")))):
  332. running = False