cbd_semantics.alc 12 KB

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