cbd_semantics.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. log("Translating to runtime model!")
  37. log("Creating empty runtime model")
  38. runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  39. log("Converting blocks")
  40. all_blocks = allInstances(design_model, "Block")
  41. while (list_len(all_blocks) > 0):
  42. element_name = set_pop(all_blocks)
  43. mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(design_model["type_mapping"], design_model["model"][element_name]))
  44. element_name = instantiate_node(runtime_model, mm_type_name, element_name)
  45. if (mm_type_name == "ConstantBlock"):
  46. instantiate_attribute(runtime_model, element_name, "value", read_attribute(design_model, element_name, "value"))
  47. elif (mm_type_name == "DelayBlock"):
  48. instantiate_attribute(runtime_model, element_name, "memory", 0.0)
  49. instantiate_attribute(runtime_model, element_name, "signal", 0.0)
  50. log(" Converted block " + element_name)
  51. // Don't merge this together with the block conversion, as the destination block might not exist yet!
  52. log("Relinking blocks")
  53. all_links = allInstances(design_model, "Link")
  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, "Link", element_name, src, dst)
  59. log(((" Relinked blocks " + src) + " and ") + dst)
  60. log("Resetting initial conditions")
  61. all_links = allInstances(design_model, "InitialCondition")
  62. while (read_nr_out(all_links) > 0):
  63. element_name = set_pop(all_links)
  64. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  65. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  66. instantiate_link(runtime_model, "InitialCondition", element_name, src, dst)
  67. log(" Reset IC of " + dst)
  68. log("Creating schedule at time = 0")
  69. create_schedule(runtime_model, True)
  70. log("Creating schedule at time > 0")
  71. create_schedule(runtime_model, False)
  72. log("Solving loops (TODO)")
  73. log("Creating simulation time")
  74. time = instantiate_node(runtime_model, "Time", "time")
  75. instantiate_attribute(runtime_model, time, "current_time", 0)
  76. instantiate_attribute(runtime_model, time, "termination_time", 100)
  77. log("DONE!")
  78. return runtime_model!
  79. Void function create_schedule(model : Element, is_time_zero : Boolean):
  80. Element all_blocks
  81. Element visited
  82. Element to_visit
  83. Element incoming_links
  84. String schedule
  85. String element_name
  86. String link
  87. String source
  88. String new_schedule
  89. Boolean ready
  90. all_blocks = allInstances(model, "Block")
  91. visited = create_node()
  92. to_visit = create_node()
  93. if (is_time_zero):
  94. schedule = instantiate_node(model, "Schedule", "schedule_init")
  95. else:
  96. schedule = instantiate_node(model, "Schedule", "schedule_run")
  97. while (read_nr_out(all_blocks) > 0):
  98. element_name = set_pop(all_blocks)
  99. if (bool_not(set_in(visited, element_name))):
  100. list_append(to_visit, element_name)
  101. while (list_len(to_visit) > 0):
  102. element_name = list_read(to_visit, list_len(to_visit) - 1)
  103. if (reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][element_name])) == "DelayBlock"):
  104. if (is_time_zero):
  105. incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
  106. else:
  107. incoming_links = create_node()
  108. else:
  109. incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
  110. ready = True
  111. while (list_len(incoming_links) > 0):
  112. link = set_pop(incoming_links)
  113. source = readAssociationSource(model, link)
  114. if (bool_not(set_in(visited, source))):
  115. list_append(to_visit, source)
  116. ready = False
  117. if (ready):
  118. new_schedule = instantiate_node(model, "Schedule", "")
  119. instantiate_link(model, "LinkedBlock", "", schedule, element_name)
  120. instantiate_link(model, "NextSchedule", "", schedule, new_schedule)
  121. schedule = new_schedule
  122. list_delete(to_visit, list_len(to_visit) - 1)
  123. set_add(visited, element_name)
  124. return !
  125. String function readType(model : Element, name : String):
  126. return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
  127. Void function list_CBD(model : Element):
  128. Element all_elements
  129. String elem
  130. String block
  131. output("Blocks:")
  132. all_elements = allInstances(model, "Block")
  133. while (read_nr_out(all_elements) > 0):
  134. elem = set_pop(all_elements)
  135. output(((" " + elem) + ": ") + readType(model, elem))
  136. output("Links:")
  137. all_elements = allInstances(model, "Link")
  138. while (read_nr_out(all_elements) > 0):
  139. elem = set_pop(all_elements)
  140. output(((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][elem]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][elem])))
  141. output("Initial conditions:")
  142. all_elements = allInstances(model, "InitialCondition")
  143. while (read_nr_out(all_elements) > 0):
  144. elem = set_pop(all_elements)
  145. output(((" " + reverseKeyLookup(model["model"], read_edge_src(model["model"][elem]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][elem])))
  146. output("Schedule (== 0):")
  147. elem = "schedule_init"
  148. while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
  149. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
  150. output(" " + block)
  151. elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
  152. output("Schedule (> 0):")
  153. elem = "schedule_run"
  154. while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
  155. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
  156. output(" " + block)
  157. elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
  158. return !
  159. Void function step_simulation(model : Element):
  160. String time
  161. String schedule
  162. Float signal
  163. Element incoming
  164. String selected
  165. String block
  166. String elem
  167. String blocktype
  168. time = "time"
  169. if (cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) == 0):
  170. schedule = "schedule_init"
  171. else:
  172. schedule = "schedule_run"
  173. output("==== START ====")
  174. while (read_nr_out(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")) > 0):
  175. block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")))
  176. schedule = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "NextSchedule")))
  177. // Execute "block"
  178. blocktype = readType(model, block)
  179. unset_attribute(model, block, "signal")
  180. if (blocktype == "ConstantBlock"):
  181. signal = read_attribute(model, block, "value")
  182. log("Value: " + cast_v2s(read_attribute(model, block, "value")))
  183. elif (blocktype == "AdditionBlock"):
  184. signal = 0.0
  185. incoming = allIncomingAssociationInstances(model, block, "Link")
  186. while (read_nr_out(incoming) > 0):
  187. selected = readAssociationSource(model, set_pop(incoming))
  188. log("V += " + cast_v2s(read_attribute(model, selected, "signal")))
  189. signal = signal + cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  190. instantiate_attribute(model, block, "signal", signal)
  191. output(((" " + block) + " = ") + cast_v2s(read_attribute(model, block, "signal")))
  192. // Increase simulation time
  193. Integer new_time
  194. new_time = cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) + 1
  195. unset_attribute(model, time, "current_time")
  196. instantiate_attribute(model, time, "current_time", new_time)
  197. output("==== END ====")
  198. return !
  199. Void function set_termination_time(model : Element):
  200. unset_attribute(model, "time", "termination_time")
  201. instantiate_attribute(model, "time", "termination_time", input())
  202. return !
  203. Void function execute_cbd(model : Element):
  204. model = translate_to_runtime(model)
  205. String cmd
  206. Boolean running
  207. running = False
  208. while (True):
  209. output("Which operation do you want to execute?")
  210. if (running):
  211. if (has_input()):
  212. cmd = input()
  213. else:
  214. cmd = "start"
  215. else:
  216. cmd = input()
  217. if (cmd == "help"):
  218. output("Supported operations:")
  219. if (bool_not(running)):
  220. output(" step -- do one simulation step")
  221. output(" start -- start simulation")
  222. //output(" modify -- live modelling: modify model structure")
  223. output(" list -- list blocks and connections")
  224. output(" exit -- select another model")
  225. else:
  226. output(" pause -- pause simulation")
  227. output(" help -- this information")
  228. output(" term -- set termination time")
  229. elif (cmd == "step"):
  230. // Do a simulation step
  231. step_simulation(model)
  232. elif (cmd == "term"):
  233. set_termination_time(model)
  234. elif (running):
  235. if (cmd == "pause"):
  236. running = False
  237. else:
  238. output("Did not understand command!")
  239. output("Use 'help' for a list of available options")
  240. else:
  241. // Not running
  242. if (cmd == "list"):
  243. list_CBD(model)
  244. elif (cmd == "start"):
  245. running = True
  246. elif (cmd == "exit"):
  247. return!
  248. else:
  249. output("Did not understand command!")
  250. output("Use 'help' for a list of available options")
  251. if (cast_s2i(cast_v2s(read_attribute(model, "time", "current_time"))) > cast_s2i(cast_v2s(read_attribute(model, "time", "termination_time")))):
  252. running = False