cbd_semantics.alc 13 KB

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