cbd_semantics.alc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. Integer 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 : Integer):
  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. // Assume \delta t == 1
  124. String time
  125. Float signal
  126. Element incoming
  127. String selected
  128. String block
  129. String elem
  130. String blocktype
  131. Element memory_blocks
  132. Integer i
  133. time = "time"
  134. memory_blocks = create_node()
  135. output("SIM_TIME " + cast_v2s(read_attribute(model, time, "current_time")))
  136. i = 0
  137. while (i < read_nr_out(schedule)):
  138. block = list_read(schedule, i)
  139. i = i + 1
  140. // Execute "block"
  141. blocktype = readType(model, block)
  142. if (blocktype == "ConstantBlock"):
  143. signal = read_attribute(model, block, "value")
  144. elif (blocktype == "AdditionBlock"):
  145. signal = 0.0
  146. incoming = allIncomingAssociationInstances(model, block, "Link")
  147. while (read_nr_out(incoming) > 0):
  148. selected = readAssociationSource(model, set_pop(incoming))
  149. signal = signal + cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  150. elif (blocktype == "MultiplyBlock"):
  151. signal = 1.0
  152. incoming = allIncomingAssociationInstances(model, block, "Link")
  153. while (read_nr_out(incoming) > 0):
  154. selected = readAssociationSource(model, set_pop(incoming))
  155. signal = signal * cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  156. elif (blocktype == "NegatorBlock"):
  157. incoming = allIncomingAssociationInstances(model, block, "Link")
  158. signal = 0.0
  159. while (read_nr_out(incoming) > 0):
  160. selected = readAssociationSource(model, set_pop(incoming))
  161. signal = float_neg(cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  162. elif (blocktype == "InverseBlock"):
  163. signal = 0.0
  164. incoming = allIncomingAssociationInstances(model, block, "Link")
  165. while (read_nr_out(incoming) > 0):
  166. selected = readAssociationSource(model, set_pop(incoming))
  167. signal = float_division(1.0, cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  168. elif (blocktype == "DelayBlock"):
  169. signal = 0.0
  170. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  171. // No memory yet, so use initial condition
  172. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  173. while (read_nr_out(incoming) > 0):
  174. selected = readAssociationSource(model, set_pop(incoming))
  175. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  176. else:
  177. signal = read_attribute(model, block, "last_in")
  178. unset_attribute(model, block, "last_in")
  179. set_add(memory_blocks, block)
  180. elif (blocktype == "IntegratorBlock"):
  181. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  182. // No history yet, so use initial values
  183. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  184. while (read_nr_out(incoming) > 0):
  185. selected = readAssociationSource(model, set_pop(incoming))
  186. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  187. else:
  188. signal = cast_s2f(cast_v2s(read_attribute(model, block, "last_in"))) + cast_s2f(cast_v2s(read_attribute(model, block, "last_out")))
  189. unset_attribute(model, block, "last_in")
  190. unset_attribute(model, block, "last_out")
  191. instantiate_attribute(model, block, "last_out", signal)
  192. set_add(memory_blocks, block)
  193. elif (blocktype == "DerivatorBlock"):
  194. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  195. // No history yet, so use initial values
  196. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  197. while (read_nr_out(incoming) > 0):
  198. selected = readAssociationSource(model, set_pop(incoming))
  199. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  200. else:
  201. incoming = allIncomingAssociationInstances(model, block, "Link")
  202. while (read_nr_out(incoming) > 0):
  203. selected = readAssociationSource(model, set_pop(incoming))
  204. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))) - cast_s2f(cast_v2s(read_attribute(model, block, "last_in")))
  205. unset_attribute(model, block, "last_in")
  206. set_add(memory_blocks, block)
  207. unset_attribute(model, block, "signal")
  208. instantiate_attribute(model, block, "signal", signal)
  209. output((("SIM_PROBE " + cast_v2s(block)) + " ") + cast_v2s(signal))
  210. output("SIM_END")
  211. while (read_nr_out(memory_blocks) > 0):
  212. block = set_pop(memory_blocks)
  213. // Update memory
  214. incoming = allIncomingAssociationInstances(model, block, "Link")
  215. while (read_nr_out(incoming) > 0):
  216. selected = readAssociationSource(model, set_pop(incoming))
  217. instantiate_attribute(model, block, "last_in", cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  218. // Increase simulation time
  219. Integer new_time
  220. new_time = cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) + 1
  221. unset_attribute(model, time, "current_time")
  222. instantiate_attribute(model, time, "current_time", new_time)
  223. return !
  224. Void function execute_cbd(design_model : Element):
  225. String verify_result
  226. Element runtime_model
  227. Element old_runtime_model
  228. String cmd
  229. Boolean running
  230. Element schedule_init
  231. Element schedule_run
  232. Element schedule
  233. String conforming
  234. old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  235. runtime_model = retype_to_runtime(design_model)
  236. runtime_model = sanitize(runtime_model, old_runtime_model)
  237. running = False
  238. conforming = conformance_scd(design_model)
  239. if (conforming == "OK"):
  240. output("CONFORMANCE_OK")
  241. else:
  242. output("CONFORMANCE_FAIL")
  243. schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
  244. schedule_run = create_schedule(runtime_model, -1)
  245. while (True):
  246. // If we are running, we just don't block for input and automatically do a step if there is no input
  247. if (running):
  248. if (has_input()):
  249. cmd = input()
  250. else:
  251. cmd = "step"
  252. else:
  253. cmd = input()
  254. // Process input
  255. if (cmd == "simulate"):
  256. // Simulation should toggle running to True, but only if the model is conforming
  257. if (conforming == "OK"):
  258. running = True
  259. else:
  260. output("CONFORMANCE_FAIL " + conforming)
  261. elif (cmd == "step"):
  262. // Stepping should make a single step, but first need to pick the correct schedule to use
  263. if (conforming == "OK"):
  264. if (read_attribute(runtime_model, "time", "start_time") == read_attribute(runtime_model, "time", "current_time")):
  265. schedule = schedule_init
  266. else:
  267. schedule = schedule_run
  268. step_simulation(runtime_model, schedule)
  269. else:
  270. output("CONFORMANCE_FAIL " + conforming)
  271. elif (cmd == "pause"):
  272. // Pausing merely stops a running simulation
  273. running = False
  274. elif (cmd == "read_available_attributes"):
  275. // Returns a list of all available attributes
  276. Element attr_list
  277. Element attrs
  278. Element attr
  279. attr_list = getAttributeList(design_model, input())
  280. attrs = dict_keys(attr_list)
  281. while (0 < read_nr_out(attrs)):
  282. attr = set_pop(attrs)
  283. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  284. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  285. output("AVAILABLE_ATTR_END")
  286. elif (cmd == "read_attribute"):
  287. // Returns the value of an attribute
  288. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  289. elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association"))):
  290. // Modify the structure
  291. if (cmd == "set_attribute"):
  292. // Setting an attribute
  293. String element_name
  294. String attribute_name
  295. element_name = input()
  296. attribute_name = input()
  297. // Delete it if it exists already
  298. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  299. unset_attribute(design_model, element_name, attribute_name)
  300. // And finally set it
  301. instantiate_attribute(design_model, element_name, attribute_name, input())
  302. elif (cmd == "instantiate_node"):
  303. // Instantiate a node
  304. instantiate_node(design_model, input(), input())
  305. elif (cmd == "instantiate_association"):
  306. // Instantiate an association
  307. instantiate_link(design_model, input(), input(), input(), input())
  308. elif (cmd == "delete_element"):
  309. // Delete the provided element
  310. model_delete_element(design_model, input())
  311. // After changes, we check whether or not the design model conforms
  312. conforming = conformance_scd(design_model)
  313. if (conforming == "OK"):
  314. // Conforming, so do the retyping and sanitization step
  315. runtime_model = retype_to_runtime(design_model)
  316. runtime_model = sanitize(runtime_model, old_runtime_model)
  317. schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
  318. schedule_run = create_schedule(runtime_model, -1)
  319. old_runtime_model = runtime_model
  320. output("CONFORMANCE_OK")
  321. else:
  322. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  323. running = False
  324. output("CONFORMANCE_FAIL " + conforming)
  325. else:
  326. log("Did not understand command: " + cmd)