cbd_semantics.alc 12 KB

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