cbd_semantics.alc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. while (read_nr_out(incoming) > 0):
  158. selected = readAssociationSource(model, set_pop(incoming))
  159. signal = float_neg(cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  160. elif (blocktype == "InverseBlock"):
  161. incoming = allIncomingAssociationInstances(model, block, "Link")
  162. while (read_nr_out(incoming) > 0):
  163. selected = readAssociationSource(model, set_pop(incoming))
  164. signal = float_division(1.0, cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  165. elif (blocktype == "DelayBlock"):
  166. if (element_eq(read_attribute(model, block, "memory"), read_root())):
  167. // No memory yet, so use initial condition
  168. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  169. while (read_nr_out(incoming) > 0):
  170. selected = readAssociationSource(model, set_pop(incoming))
  171. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  172. else:
  173. signal = read_attribute(model, block, "memory")
  174. unset_attribute(model, block, "memory")
  175. set_add(delay_blocks, block)
  176. unset_attribute(model, block, "signal")
  177. instantiate_attribute(model, block, "signal", signal)
  178. output((("SIM_PROBE " + cast_v2s(block)) + " ") + cast_v2s(signal))
  179. output("SIM_END")
  180. while (read_nr_out(delay_blocks) > 0):
  181. block = set_pop(delay_blocks)
  182. // Update memory
  183. incoming = allIncomingAssociationInstances(model, block, "Link")
  184. while (read_nr_out(incoming) > 0):
  185. selected = readAssociationSource(model, set_pop(incoming))
  186. instantiate_attribute(model, block, "memory", cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  187. // Increase simulation time
  188. Integer new_time
  189. new_time = cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) + 1
  190. unset_attribute(model, time, "current_time")
  191. instantiate_attribute(model, time, "current_time", new_time)
  192. return !
  193. Void function execute_cbd(design_model : Element):
  194. String verify_result
  195. Element runtime_model
  196. Element old_runtime_model
  197. String cmd
  198. Boolean running
  199. Element schedule_init
  200. Element schedule_run
  201. Element schedule
  202. String conforming
  203. old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  204. runtime_model = retype_to_runtime(design_model)
  205. runtime_model = sanitize(runtime_model, old_runtime_model)
  206. running = False
  207. conforming = conformance_scd(design_model)
  208. schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
  209. schedule_run = create_schedule(runtime_model, -1)
  210. while (True):
  211. // If we are running, we just don't block for input and automatically do a step if there is no input
  212. if (running):
  213. if (has_input()):
  214. cmd = input()
  215. else:
  216. cmd = "step"
  217. else:
  218. cmd = input()
  219. // Process input
  220. if (cmd == "simulate"):
  221. // Simulation should toggle running to True, but only if the model is conforming
  222. if (conforming == "OK"):
  223. running = True
  224. else:
  225. output("FAIL_CONFORMANCE " + conforming)
  226. elif (cmd == "step"):
  227. // Stepping should make a single step, but first need to pick the correct schedule to use
  228. if (conforming == "OK"):
  229. if (read_attribute(runtime_model, "time", "start_time") == read_attribute(runtime_model, "time", "current_time")):
  230. schedule = schedule_init
  231. else:
  232. schedule = schedule_run
  233. step_simulation(runtime_model, schedule)
  234. else:
  235. output("FAIL_CONFORMANCE " + conforming)
  236. elif (cmd == "pause"):
  237. // Pausing merely stops a running simulation
  238. running = False
  239. elif (cmd == "read_available_attributes"):
  240. // Returns a list of all available attributes
  241. Element attr_list
  242. attr_list = dict_keys(getAttributeList(design_model, cmd))
  243. while (0 < read_nr_out(attr_list)):
  244. output("AVAILABLE_ATTR_VALUE " + cast_v2s(set_pop(attr_list)))
  245. output("AVAILABLE_ATTR_END")
  246. elif (cmd == "read_attribute"):
  247. // Returns the value of an attribute
  248. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  249. elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), cmd == "instantiate_association")):
  250. // Modify the structure
  251. if (cmd == "set_attribute"):
  252. // Setting an attribute
  253. String element_name
  254. String attribute_name
  255. element_name = input()
  256. attribute_name = input()
  257. // Delete it if it exists already
  258. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  259. unset_attribute(design_model, element_name, attribute_name)
  260. // And finally set it
  261. instantiate_attribute(design_model, element_name, attribute_name, input())
  262. elif (cmd == "instantiate_node"):
  263. // Instantiate a node
  264. instantiate_node(design_model, input(), input())
  265. elif (cmd == "instantiate_association"):
  266. // Instantiate an association
  267. instantiate_link(design_model, input(), input(), input(), input())
  268. // After changes, we check whether or not the design model conforms
  269. conforming = conformance_scd(design_model)
  270. if (conforming == "OK"):
  271. // Conforming, so do the retyping and sanitization step
  272. runtime_model = retype_to_runtime(design_model)
  273. runtime_model = sanitize(runtime_model, old_runtime_model)
  274. schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
  275. schedule_run = create_schedule(runtime_model, -1)
  276. old_runtime_model = runtime_model
  277. else:
  278. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  279. running = False
  280. output("FAIL_CONFORMANCE " + conforming)
  281. else:
  282. log("Did not understand command: " + cmd)