cbd_semantics.alc 12 KB

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