cbd_semantics.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "library.alh"
  5. include "conformance_scd.alh"
  6. include "io.alh"
  7. include "metamodels.alh"
  8. include "compilation_manager.alh"
  9. Void function main():
  10. Element model
  11. String verify_result
  12. while (True):
  13. execute_cbd(instantiate_model(import_node("models/CausalBlockDiagrams_Design")))
  14. Element function retype_to_runtime(design_model : Element):
  15. Element runtime_model
  16. Element all_blocks
  17. Element all_links
  18. String mm_type_name
  19. String element_name
  20. String attr_name
  21. String attr_value
  22. String attribute
  23. String src
  24. String dst
  25. String time
  26. Element all_attributes
  27. runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  28. all_blocks = allInstances(design_model, "Block")
  29. while (list_len(all_blocks) > 0):
  30. element_name = set_pop(all_blocks)
  31. mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(design_model["type_mapping"], design_model["model"][element_name]))
  32. element_name = instantiate_node(runtime_model, mm_type_name, element_name)
  33. if (is_nominal_instance(design_model, element_name, "ConstantBlock")):
  34. instantiate_attribute(runtime_model, element_name, "value", read_attribute(design_model, element_name, "value"))
  35. // Don't merge this together with the block conversion, as the destination block might not exist yet!
  36. all_links = allInstances(design_model, "Link")
  37. while (read_nr_out(all_links) > 0):
  38. element_name = set_pop(all_links)
  39. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  40. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  41. instantiate_link(runtime_model, "Link", element_name, src, dst)
  42. all_links = allInstances(design_model, "InitialCondition")
  43. while (read_nr_out(all_links) > 0):
  44. element_name = set_pop(all_links)
  45. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  46. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  47. instantiate_link(runtime_model, "InitialCondition", element_name, src, dst)
  48. return runtime_model!
  49. Element function sanitize(new_runtime_model : Element, old_runtime_model : Element):
  50. Element all_blocks
  51. Element all_links
  52. String element_name
  53. String attr_name
  54. String attr_value
  55. String attribute
  56. String time
  57. Element all_attributes
  58. Float current_time
  59. all_blocks = allInstances(new_runtime_model, "Block")
  60. while (list_len(all_blocks) > 0):
  61. element_name = set_pop(all_blocks)
  62. if (dict_in(old_runtime_model["model"], element_name)):
  63. if (is_nominal_instance(new_runtime_model, element_name, "ICBlock")):
  64. instantiate_attribute(new_runtime_model, element_name, "last_in", read_attribute(old_runtime_model, element_name, "last_in"))
  65. if (is_nominal_instance(new_runtime_model, element_name, "IntegratorBlock")):
  66. instantiate_attribute(new_runtime_model, element_name, "last_out", read_attribute(old_runtime_model, element_name, "last_out"))
  67. instantiate_attribute(new_runtime_model, element_name, "signal", read_attribute(old_runtime_model, element_name, "signal"))
  68. else:
  69. instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
  70. if (dict_in(old_runtime_model["model"], "time")):
  71. current_time = read_attribute(old_runtime_model, "time", "current_time")
  72. else:
  73. current_time = 0
  74. time = instantiate_node(new_runtime_model, "Time", "time")
  75. instantiate_attribute(new_runtime_model, time, "start_time", current_time)
  76. instantiate_attribute(new_runtime_model, time, "current_time", current_time)
  77. return new_runtime_model!
  78. Element function create_schedule(model : Element, start_time : Float):
  79. Element all_blocks
  80. Element visited
  81. Element to_visit
  82. Element incoming_links
  83. String element_name
  84. String link
  85. String source
  86. String new_schedule
  87. Boolean ready
  88. Element schedule
  89. // TODO add algebraic loop detection (not solution...)
  90. schedule = create_node()
  91. all_blocks = allInstances(model, "Block")
  92. visited = create_node()
  93. to_visit = create_node()
  94. while (read_nr_out(all_blocks) > 0):
  95. element_name = set_pop(all_blocks)
  96. if (bool_not(set_in(visited, element_name))):
  97. list_append(to_visit, element_name)
  98. while (list_len(to_visit) > 0):
  99. element_name = list_read(to_visit, list_len(to_visit) - 1)
  100. if (is_nominal_instance(model, element_name, "ICBlock")):
  101. if (element_eq(read_attribute(model, element_name, "last_in"), read_root())):
  102. incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
  103. else:
  104. incoming_links = create_node()
  105. else:
  106. incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
  107. ready = True
  108. while (list_len(incoming_links) > 0):
  109. link = set_pop(incoming_links)
  110. source = readAssociationSource(model, link)
  111. if (bool_not(set_in(visited, source))):
  112. list_append(to_visit, source)
  113. ready = False
  114. if (ready):
  115. list_append(schedule, element_name)
  116. list_delete(to_visit, list_len(to_visit) - 1)
  117. set_add(visited, element_name)
  118. log("Schedule length: " + cast_v2s(read_nr_out(schedule)))
  119. return schedule!
  120. String function readType(model : Element, name : String):
  121. return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
  122. Void function step_simulation(model : Element, schedule : Element):
  123. String time
  124. Float signal
  125. Element incoming
  126. String selected
  127. String block
  128. String elem
  129. String blocktype
  130. Element memory_blocks
  131. Integer i
  132. Float delta_t
  133. time = "time"
  134. delta_t = 0.1
  135. memory_blocks = create_node()
  136. output("SIM_TIME " + cast_v2s(read_attribute(model, time, "current_time")))
  137. i = 0
  138. while (i < read_nr_out(schedule)):
  139. block = list_read(schedule, i)
  140. i = i + 1
  141. // Execute "block"
  142. blocktype = readType(model, block)
  143. if (blocktype == "ConstantBlock"):
  144. signal = read_attribute(model, block, "value")
  145. elif (blocktype == "AdditionBlock"):
  146. signal = 0.0
  147. incoming = allIncomingAssociationInstances(model, block, "Link")
  148. while (read_nr_out(incoming) > 0):
  149. selected = readAssociationSource(model, set_pop(incoming))
  150. signal = signal + cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  151. elif (blocktype == "MultiplyBlock"):
  152. signal = 1.0
  153. incoming = allIncomingAssociationInstances(model, block, "Link")
  154. while (read_nr_out(incoming) > 0):
  155. selected = readAssociationSource(model, set_pop(incoming))
  156. signal = signal * cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  157. elif (blocktype == "NegatorBlock"):
  158. incoming = allIncomingAssociationInstances(model, block, "Link")
  159. signal = 0.0
  160. while (read_nr_out(incoming) > 0):
  161. selected = readAssociationSource(model, set_pop(incoming))
  162. signal = float_neg(cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  163. elif (blocktype == "InverseBlock"):
  164. signal = 0.0
  165. incoming = allIncomingAssociationInstances(model, block, "Link")
  166. while (read_nr_out(incoming) > 0):
  167. selected = readAssociationSource(model, set_pop(incoming))
  168. signal = float_division(1.0, cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  169. elif (blocktype == "DelayBlock"):
  170. signal = 0.0
  171. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  172. // No memory yet, so use initial condition
  173. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  174. while (read_nr_out(incoming) > 0):
  175. selected = readAssociationSource(model, set_pop(incoming))
  176. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  177. else:
  178. signal = read_attribute(model, block, "last_in")
  179. unset_attribute(model, block, "last_in")
  180. set_add(memory_blocks, block)
  181. elif (blocktype == "IntegratorBlock"):
  182. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  183. // No history yet, so use initial values
  184. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  185. while (read_nr_out(incoming) > 0):
  186. selected = readAssociationSource(model, set_pop(incoming))
  187. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  188. else:
  189. signal = cast_s2f(cast_v2s(read_attribute(model, block, "last_in"))) + (delta_t * cast_s2f(cast_v2s(read_attribute(model, block, "last_out"))))
  190. unset_attribute(model, block, "last_in")
  191. unset_attribute(model, block, "last_out")
  192. instantiate_attribute(model, block, "last_out", signal)
  193. set_add(memory_blocks, block)
  194. elif (blocktype == "DerivatorBlock"):
  195. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  196. // No history yet, so use initial values
  197. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  198. while (read_nr_out(incoming) > 0):
  199. selected = readAssociationSource(model, set_pop(incoming))
  200. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  201. else:
  202. incoming = allIncomingAssociationInstances(model, block, "Link")
  203. while (read_nr_out(incoming) > 0):
  204. selected = readAssociationSource(model, set_pop(incoming))
  205. signal = (cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))) - cast_s2f(cast_v2s(read_attribute(model, block, "last_in")))) / delta_t
  206. unset_attribute(model, block, "last_in")
  207. set_add(memory_blocks, block)
  208. unset_attribute(model, block, "signal")
  209. instantiate_attribute(model, block, "signal", signal)
  210. output((("SIM_PROBE " + cast_v2s(block)) + " ") + cast_v2s(signal))
  211. output("SIM_END")
  212. while (read_nr_out(memory_blocks) > 0):
  213. block = set_pop(memory_blocks)
  214. // Update memory
  215. incoming = allIncomingAssociationInstances(model, block, "Link")
  216. while (read_nr_out(incoming) > 0):
  217. selected = readAssociationSource(model, set_pop(incoming))
  218. instantiate_attribute(model, block, "last_in", cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  219. // Increase simulation time
  220. Float new_time
  221. new_time = cast_s2f(cast_v2s(read_attribute(model, time, "current_time"))) + delta_t
  222. unset_attribute(model, time, "current_time")
  223. instantiate_attribute(model, time, "current_time", new_time)
  224. return !
  225. Void function execute_cbd(design_model : Element):
  226. String verify_result
  227. Element runtime_model
  228. Element old_runtime_model
  229. String cmd
  230. Boolean running
  231. Element schedule_init
  232. Element schedule_run
  233. Element schedule
  234. String conforming
  235. old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  236. runtime_model = retype_to_runtime(design_model)
  237. runtime_model = sanitize(runtime_model, old_runtime_model)
  238. running = False
  239. conforming = conformance_scd(design_model)
  240. if (conforming == "OK"):
  241. output("CONFORMANCE_OK")
  242. else:
  243. output("CONFORMANCE_FAIL")
  244. schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
  245. schedule_run = create_schedule(runtime_model, -1)
  246. while (True):
  247. // If we are running, we just don't block for input and automatically do a step if there is no input
  248. if (running):
  249. if (has_input()):
  250. cmd = input()
  251. else:
  252. cmd = "step"
  253. else:
  254. cmd = input()
  255. // Process input
  256. if (cmd == "simulate"):
  257. // Simulation should toggle running to True, but only if the model is conforming
  258. if (conforming == "OK"):
  259. running = True
  260. else:
  261. output("CONFORMANCE_FAIL " + conforming)
  262. elif (cmd == "step"):
  263. // Stepping should make a single step, but first need to pick the correct schedule to use
  264. if (conforming == "OK"):
  265. if (read_attribute(runtime_model, "time", "start_time") == read_attribute(runtime_model, "time", "current_time")):
  266. schedule = schedule_init
  267. else:
  268. schedule = schedule_run
  269. step_simulation(runtime_model, schedule)
  270. else:
  271. output("CONFORMANCE_FAIL " + conforming)
  272. elif (cmd == "pause"):
  273. // Pausing merely stops a running simulation
  274. running = False
  275. elif (cmd == "read_available_attributes"):
  276. // Returns a list of all available attributes
  277. Element attr_list
  278. Element attrs
  279. Element attr
  280. attr_list = getAttributeList(design_model, input())
  281. attrs = dict_keys(attr_list)
  282. while (0 < read_nr_out(attrs)):
  283. attr = set_pop(attrs)
  284. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  285. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  286. output("AVAILABLE_ATTR_END")
  287. elif (cmd == "read_attribute"):
  288. // Returns the value of an attribute
  289. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  290. elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association"))):
  291. // Modify the structure
  292. if (cmd == "set_attribute"):
  293. // Setting an attribute
  294. String element_name
  295. String attribute_name
  296. element_name = input()
  297. attribute_name = input()
  298. // Delete it if it exists already
  299. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  300. unset_attribute(design_model, element_name, attribute_name)
  301. // And finally set it
  302. instantiate_attribute(design_model, element_name, attribute_name, input())
  303. elif (cmd == "instantiate_node"):
  304. // Instantiate a node
  305. instantiate_node(design_model, input(), input())
  306. elif (cmd == "instantiate_association"):
  307. // Instantiate an association
  308. instantiate_link(design_model, input(), input(), input(), input())
  309. elif (cmd == "delete_element"):
  310. // Delete the provided element
  311. model_delete_element(design_model, input())
  312. // After changes, we check whether or not the design model conforms
  313. conforming = conformance_scd(design_model)
  314. if (conforming == "OK"):
  315. // Conforming, so do the retyping and sanitization step
  316. runtime_model = retype_to_runtime(design_model)
  317. runtime_model = sanitize(runtime_model, old_runtime_model)
  318. schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
  319. schedule_run = create_schedule(runtime_model, -1)
  320. old_runtime_model = runtime_model
  321. output("CONFORMANCE_OK")
  322. else:
  323. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  324. running = False
  325. output("CONFORMANCE_FAIL " + conforming)
  326. else:
  327. log("Did not understand command: " + cmd)