cbd_semantics.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
  82. if (dict_in(old_runtime_model["model"], "time")):
  83. current_time = read_attribute(old_runtime_model, "time", "current_time")
  84. termination_time = read_attribute(old_runtime_model, "time", "termination_time")
  85. else:
  86. current_time = 0
  87. termination_time = 10
  88. create_schedule(new_runtime_model, old_runtime_model, current_time)
  89. create_schedule(new_runtime_model, old_runtime_model, -1)
  90. time = instantiate_node(new_runtime_model, "Time", "time")
  91. instantiate_attribute(new_runtime_model, time, "start_time", current_time)
  92. instantiate_attribute(new_runtime_model, time, "current_time", current_time)
  93. instantiate_attribute(new_runtime_model, time, "termination_time", termination_time)
  94. return new_runtime_model!
  95. Void function create_schedule(model : Element, old_model : Element, start_time : Integer):
  96. Element all_blocks
  97. Element visited
  98. Element to_visit
  99. Element incoming_links
  100. String schedule
  101. String element_name
  102. String link
  103. String source
  104. String new_schedule
  105. Boolean ready
  106. // TODO add algebraic loop detection (not solution...)
  107. all_blocks = allInstances(model, "Block")
  108. visited = create_node()
  109. to_visit = create_node()
  110. if (start_time != -1):
  111. schedule = instantiate_node(model, "Schedule", "schedule_init")
  112. else:
  113. schedule = instantiate_node(model, "Schedule", "schedule_run")
  114. while (read_nr_out(all_blocks) > 0):
  115. element_name = set_pop(all_blocks)
  116. if (bool_not(set_in(visited, element_name))):
  117. list_append(to_visit, element_name)
  118. while (list_len(to_visit) > 0):
  119. element_name = list_read(to_visit, list_len(to_visit) - 1)
  120. if (reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][element_name])) == "DelayBlock"):
  121. if (dict_in(old_model["model"], element_name)):
  122. incoming_links = create_node()
  123. else:
  124. incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
  125. else:
  126. incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
  127. ready = True
  128. while (list_len(incoming_links) > 0):
  129. link = set_pop(incoming_links)
  130. source = readAssociationSource(model, link)
  131. if (bool_not(set_in(visited, source))):
  132. list_append(to_visit, source)
  133. ready = False
  134. if (ready):
  135. new_schedule = instantiate_node(model, "Schedule", "")
  136. instantiate_link(model, "LinkedBlock", "", schedule, element_name)
  137. instantiate_link(model, "NextSchedule", "", schedule, new_schedule)
  138. schedule = new_schedule
  139. list_delete(to_visit, list_len(to_visit) - 1)
  140. set_add(visited, element_name)
  141. log("Scheduled " + element_name)
  142. else:
  143. log("Delay " + element_name)
  144. return !
  145. String function readType(model : Element, name : String):
  146. return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
  147. Void function list_CBD(model : Element):
  148. Element all_elements
  149. String elem
  150. String block
  151. output("Blocks:")
  152. all_elements = allInstances(model, "Block")
  153. while (read_nr_out(all_elements) > 0):
  154. elem = set_pop(all_elements)
  155. output(((" " + elem) + ": ") + readType(model, elem))
  156. output("Links:")
  157. all_elements = allInstances(model, "Link")
  158. while (read_nr_out(all_elements) > 0):
  159. elem = set_pop(all_elements)
  160. output(((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][elem]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][elem])))
  161. output("Initial conditions:")
  162. all_elements = allInstances(model, "InitialCondition")
  163. while (read_nr_out(all_elements) > 0):
  164. elem = set_pop(all_elements)
  165. output(((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][elem]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][elem])))
  166. output(("Schedule (t = " + cast_v2s(read_attribute(model, "time", "start_time"))) + "):")
  167. elem = "schedule_init"
  168. while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
  169. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
  170. output(" " + block)
  171. elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
  172. output(("Schedule (t > " + cast_v2s(read_attribute(model, "time", "start_time"))) + "):")
  173. elem = "schedule_run"
  174. while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
  175. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
  176. output(" " + block)
  177. elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
  178. return !
  179. Void function step_simulation(model : Element):
  180. String time
  181. String schedule
  182. Float signal
  183. Element incoming
  184. String selected
  185. String block
  186. String elem
  187. String blocktype
  188. String output_string
  189. Element delay_blocks
  190. time = "time"
  191. if (read_attribute(model, time, "current_time") == read_attribute(model, time, "start_time")):
  192. schedule = "schedule_init"
  193. else:
  194. schedule = "schedule_run"
  195. delay_blocks = create_node()
  196. output_string = "("
  197. while (read_nr_out(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")) > 0):
  198. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")))
  199. schedule = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "NextSchedule")))
  200. // Execute "block"
  201. blocktype = readType(model, block)
  202. if (blocktype == "ConstantBlock"):
  203. signal = read_attribute(model, block, "value")
  204. elif (blocktype == "AdditionBlock"):
  205. signal = 0.0
  206. incoming = allIncomingAssociationInstances(model, block, "Link")
  207. while (read_nr_out(incoming) > 0):
  208. selected = readAssociationSource(model, set_pop(incoming))
  209. signal = signal + cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  210. elif (blocktype == "MultiplyBlock"):
  211. signal = 1.0
  212. incoming = allIncomingAssociationInstances(model, block, "Link")
  213. while (read_nr_out(incoming) > 0):
  214. selected = readAssociationSource(model, set_pop(incoming))
  215. signal = signal * cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  216. elif (blocktype == "NegatorBlock"):
  217. incoming = allIncomingAssociationInstances(model, block, "Link")
  218. while (read_nr_out(incoming) > 0):
  219. selected = readAssociationSource(model, set_pop(incoming))
  220. signal = float_neg(cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  221. elif (blocktype == "InverseBlock"):
  222. incoming = allIncomingAssociationInstances(model, block, "Link")
  223. while (read_nr_out(incoming) > 0):
  224. selected = readAssociationSource(model, set_pop(incoming))
  225. signal = float_division(1.0, cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  226. elif (blocktype == "DelayBlock"):
  227. if (element_eq(read_attribute(model, block, "memory"), read_root())):
  228. // No memory yet, so use initial condition
  229. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  230. while (read_nr_out(incoming) > 0):
  231. selected = readAssociationSource(model, set_pop(incoming))
  232. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  233. else:
  234. signal = read_attribute(model, block, "memory")
  235. unset_attribute(model, block, "memory")
  236. set_add(delay_blocks, block)
  237. unset_attribute(model, block, "signal")
  238. instantiate_attribute(model, block, "signal", signal)
  239. output_string = output_string + (((block + " = ") + cast_v2s(signal)) + "; ")
  240. output_string = output_string + ")"
  241. while (read_nr_out(delay_blocks) > 0):
  242. block = set_pop(delay_blocks)
  243. // Update memory
  244. incoming = allIncomingAssociationInstances(model, block, "Link")
  245. while (read_nr_out(incoming) > 0):
  246. selected = readAssociationSource(model, set_pop(incoming))
  247. instantiate_attribute(model, block, "memory", cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  248. // Increase simulation time
  249. Integer new_time
  250. new_time = cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) + 1
  251. unset_attribute(model, time, "current_time")
  252. instantiate_attribute(model, time, "current_time", new_time)
  253. output(output_string)
  254. return !
  255. Void function set_termination_time(model : Element):
  256. unset_attribute(model, "time", "termination_time")
  257. instantiate_attribute(model, "time", "termination_time", input())
  258. return !
  259. Void function execute_cbd(design_model : Element):
  260. String verify_result
  261. Element runtime_model
  262. Element old_runtime_model
  263. String cmd
  264. Boolean running
  265. old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  266. runtime_model = retype_to_runtime(design_model)
  267. runtime_model = sanitize(runtime_model, old_runtime_model)
  268. running = False
  269. while (True):
  270. if (running):
  271. if (has_input()):
  272. cmd = input()
  273. else:
  274. cmd = "step"
  275. else:
  276. output("Which operation do you want to execute?")
  277. cmd = input()
  278. if (cmd == "help"):
  279. output("Supported operations:")
  280. if (bool_not(running)):
  281. output(" step -- do one simulation step")
  282. output(" start -- start simulation")
  283. output(" modify -- live modelling: modify model structure")
  284. output(" list -- list blocks and connections")
  285. output(" exit -- select another model")
  286. output(" verify -- verify the runtime model")
  287. else:
  288. output(" pause -- pause simulation")
  289. output(" help -- this information")
  290. output(" term -- set termination time")
  291. elif (cmd == "step"):
  292. // Do a simulation step
  293. step_simulation(runtime_model)
  294. elif (cmd == "term"):
  295. set_termination_time(runtime_model)
  296. elif (running):
  297. if (cmd == "pause"):
  298. running = False
  299. else:
  300. output("Did not understand command!")
  301. output("Use 'help' for a list of available options")
  302. else:
  303. // Not running
  304. if (cmd == "list"):
  305. list_CBD(runtime_model)
  306. elif (cmd == "start"):
  307. running = True
  308. elif (cmd == "exit"):
  309. return!
  310. elif (cmd == "modify"):
  311. verify_result = "init"
  312. while (verify_result != "OK"):
  313. modify(design_model)
  314. verify_result = conformance_scd(design_model)
  315. if (verify_result != "OK"):
  316. output("Error in design model: " + verify_result)
  317. output("Successfully made modifications to design model!")
  318. old_runtime_model = runtime_model
  319. log("Retype")
  320. runtime_model = retype_to_runtime(design_model)
  321. log("Sanitize")
  322. runtime_model = sanitize(runtime_model, old_runtime_model)
  323. log("All done!")
  324. elif (cmd == "verify"):
  325. verify_result = conformance_scd(runtime_model)
  326. if (verify_result != "OK"):
  327. output("Error in runtime model: " + verify_result)
  328. else:
  329. output("Runtime model OK!")
  330. else:
  331. output("Did not understand command!")
  332. output("Use 'help' for a list of available options")
  333. if (cast_s2i(cast_v2s(read_attribute(runtime_model, "time", "current_time"))) > cast_s2i(cast_v2s(read_attribute(runtime_model, "time", "termination_time")))):
  334. running = False