cbd_semantics.alc 10 KB

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