cbd_semantics.alc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. Element function retype_to_runtime(design_model : Element):
  10. Element runtime_model
  11. Element all_blocks
  12. Element all_links
  13. String mm_type_name
  14. String element_name
  15. String attr_name
  16. String attr_value
  17. String attribute
  18. String src
  19. String dst
  20. String time
  21. Element all_attributes
  22. runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  23. all_blocks = allInstances(design_model, "Block")
  24. while (list_len(all_blocks) > 0):
  25. element_name = set_pop(all_blocks)
  26. mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(design_model["type_mapping"], design_model["model"][element_name]))
  27. element_name = instantiate_node(runtime_model, mm_type_name, element_name)
  28. if (is_nominal_instance(design_model, element_name, "ConstantBlock")):
  29. instantiate_attribute(runtime_model, element_name, "value", read_attribute(design_model, element_name, "value"))
  30. // Don't merge this together with the block conversion, as the destination block might not exist yet!
  31. all_links = allInstances(design_model, "Link")
  32. while (read_nr_out(all_links) > 0):
  33. element_name = set_pop(all_links)
  34. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  35. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  36. instantiate_link(runtime_model, "Link", element_name, src, dst)
  37. all_links = allInstances(design_model, "InitialCondition")
  38. while (read_nr_out(all_links) > 0):
  39. element_name = set_pop(all_links)
  40. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  41. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  42. instantiate_link(runtime_model, "InitialCondition", element_name, src, dst)
  43. return runtime_model!
  44. Element function sanitize(new_runtime_model : Element, old_runtime_model : Element):
  45. Element all_blocks
  46. Element all_links
  47. String element_name
  48. String attr_name
  49. String attr_value
  50. String attribute
  51. String time
  52. Element all_attributes
  53. Float current_time
  54. all_blocks = allInstances(new_runtime_model, "Block")
  55. while (list_len(all_blocks) > 0):
  56. element_name = set_pop(all_blocks)
  57. if (dict_in(old_runtime_model["model"], element_name)):
  58. if (is_nominal_instance(new_runtime_model, element_name, "ICBlock")):
  59. instantiate_attribute(new_runtime_model, element_name, "last_in", read_attribute(old_runtime_model, element_name, "last_in"))
  60. if (is_nominal_instance(new_runtime_model, element_name, "IntegratorBlock")):
  61. instantiate_attribute(new_runtime_model, element_name, "last_out", read_attribute(old_runtime_model, element_name, "last_out"))
  62. instantiate_attribute(new_runtime_model, element_name, "signal", read_attribute(old_runtime_model, element_name, "signal"))
  63. else:
  64. instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
  65. if (dict_in(old_runtime_model["model"], "time")):
  66. current_time = read_attribute(old_runtime_model, "time", "current_time")
  67. else:
  68. current_time = 0
  69. time = instantiate_node(new_runtime_model, "Time", "time")
  70. instantiate_attribute(new_runtime_model, time, "start_time", current_time)
  71. instantiate_attribute(new_runtime_model, time, "current_time", current_time)
  72. return new_runtime_model!
  73. Element function create_schedule(model : Element):
  74. // Create nice graph first
  75. Element nodes
  76. Element successors
  77. String element_name
  78. Element incoming_links
  79. Element all_blocks
  80. nodes = allInstances(model, "Block")
  81. successors = create_node()
  82. while (read_nr_out(nodes) > 0):
  83. element_name = set_pop(nodes)
  84. if (bool_not(dict_in(successors, element_name))):
  85. dict_add(successors, element_name, create_node())
  86. if (is_nominal_instance(model, element_name, "ICBlock")):
  87. if (element_eq(read_attribute(model, element_name, "last_in"), read_root())):
  88. incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
  89. else:
  90. incoming_links = create_node()
  91. if (is_nominal_instance(model, element_name, "DerivatorBlock")):
  92. Element new_incoming_links
  93. new_incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
  94. while (read_nr_out(new_incoming_links) > 0):
  95. list_append(incoming_links, set_pop(new_incoming_links))
  96. else:
  97. incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
  98. while (read_nr_out(incoming_links) > 0):
  99. String source
  100. source = readAssociationSource(model, set_pop(incoming_links))
  101. if (bool_not(dict_in(successors, source))):
  102. dict_add(successors, source, create_node())
  103. set_add(successors[source], element_name)
  104. Element values
  105. values = create_node()
  106. dict_add(values, "S", create_node())
  107. dict_add(values, "index", 0)
  108. dict_add(values, "indices", create_node())
  109. dict_add(values, "lowlink", create_node())
  110. dict_add(values, "onStack", create_node())
  111. dict_add(values, "successors", successors)
  112. dict_add(values, "SCC", create_node())
  113. nodes = allInstances(model, "Block")
  114. while (read_nr_out(nodes) > 0):
  115. strongconnect(set_pop(nodes), values)
  116. return values["SCC"]!
  117. Void function dict_overwrite(d : Element, key : Element, value : Element):
  118. if (dict_in(d, key)):
  119. dict_delete(d, key)
  120. if (dict_in_node(d, key)):
  121. dict_delete_node(d, key)
  122. dict_add(d, key, value)
  123. return !
  124. Integer function min(a : Integer, b : Integer):
  125. if (a < b):
  126. return a!
  127. else:
  128. return b!
  129. Void function strongconnect(v : String, values : Element):
  130. if (dict_in(values["indices"], v)):
  131. return!
  132. dict_overwrite(values["indices"], v, values["index"])
  133. dict_overwrite(values["lowlink"], v, values["index"])
  134. dict_overwrite(values, "index", cast_s2i(cast_v2s(values["index"])) + 1)
  135. list_append(values["S"], v)
  136. dict_overwrite(values["onStack"], v, True)
  137. Element successors
  138. String w
  139. successors = values["successors"][v]
  140. while (read_nr_out(successors) > 0):
  141. w = set_pop(successors)
  142. if (bool_not(dict_in(values["indices"], w))):
  143. strongconnect(w, values)
  144. dict_overwrite(values["lowlink"], v, min(values["lowlink"][v], values["lowlink"][w]))
  145. elif (dict_in(values["onStack"], w)):
  146. if (values["onStack"][w]):
  147. dict_overwrite(values["lowlink"], v, min(values["lowlink"][v], values["indices"][w]))
  148. if (value_eq(values["lowlink"][v], values["indices"][v])):
  149. Element scc
  150. scc = create_node()
  151. // It will always differ now
  152. w = list_pop(values["S"])
  153. list_append(scc, w)
  154. dict_overwrite(values["onStack"], w, False)
  155. while (w != v):
  156. w = list_pop(values["S"])
  157. list_append(scc, w)
  158. dict_overwrite(values["onStack"], w, False)
  159. list_insert(values["SCC"], scc, 0)
  160. return!
  161. Element function list_pop(list : Element):
  162. Integer top
  163. Element t
  164. top = list_len(list) - 1
  165. t = list_read(list, top)
  166. list_delete(list, top)
  167. return t!
  168. String function readType(model : Element, name : String):
  169. return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
  170. Boolean function solve_scc(model : Element, scc : Element):
  171. Element m
  172. Integer i
  173. Integer j
  174. String block
  175. String blocktype
  176. Element incoming
  177. String selected
  178. Float constant
  179. Element t
  180. // Construct the matrix first, with as many rows as there are variables
  181. // Number of columns is 1 higher
  182. i = 0
  183. m = create_node()
  184. while (i < read_nr_out(scc)):
  185. j = 0
  186. t = create_node()
  187. while (j < (read_nr_out(scc) + 1)):
  188. list_append(t, 0.0)
  189. j = j + 1
  190. list_append(m, t)
  191. i = i + 1
  192. log("Matrix ready!")
  193. // Matrix initialized to 0.0
  194. i = 0
  195. while (i < read_nr_out(scc)):
  196. log("Creating matrix row")
  197. // First element of scc
  198. block = cast_v2s(list_read(scc, i))
  199. blocktype = readType(model, block)
  200. // First write 1 in the current block
  201. dict_overwrite(m[i], i, 1.0)
  202. // Now check all blocks that are incoming
  203. if (blocktype == "AdditionBlock"):
  204. constant = 0.0
  205. elif (blocktype == "MultiplyBlock"):
  206. constant = 1.0
  207. incoming = allIncomingAssociationInstances(model, block, "Link")
  208. Integer index_to_write_constant
  209. index_to_write_constant = -1
  210. log("Iterating over incoming")
  211. while (read_nr_out(incoming) > 0):
  212. log("Iteration")
  213. selected = readAssociationSource(model, set_pop(incoming))
  214. if (set_in(scc, selected)):
  215. // Part of the loop, so in the index of selected in scc
  216. // Five options:
  217. if (blocktype == "AdditionBlock"):
  218. // 1) AdditionBlock
  219. // Add the negative of this signal, which is as of yet unknown
  220. // x = y + z --> x - y - z = 0
  221. dict_overwrite(m[i], list_index_of(scc, selected), -1.0)
  222. elif (blocktype == "MultiplyBlock"):
  223. // 2) MultiplyBlock
  224. if (index_to_write_constant != -1):
  225. return False!
  226. index_to_write_constant = list_index_of(scc, selected)
  227. elif (blocktype == "NegatorBlock"):
  228. // 3) NegatorBlock
  229. // Add the positive of the signal, which is as of yet unknown
  230. dict_overwrite(m[i], list_index_of(scc, selected), 1.0)
  231. elif (blocktype == "DelayBlock"):
  232. // 5) DelayBlock
  233. // Just copies a single value
  234. dict_overwrite(m[i], list_index_of(scc, selected), -1.0)
  235. else:
  236. // Block that cannot be handled
  237. return False!
  238. else:
  239. // A constant, which we can assume is already computed and thus usable
  240. if (blocktype == "AdditionBlock"):
  241. constant = constant + v2f(read_attribute(model, selected, "signal"))
  242. dict_overwrite(m[i], read_nr_out(scc), -constant)
  243. elif (blocktype == "MultiplyBlock"):
  244. constant = constant * v2f(read_attribute(model, selected, "signal"))
  245. // Not written to constant part, as multiplies a variable
  246. // Any other block is impossible:
  247. // * Constant would never be part of a SCC
  248. // * Delay would never get an incoming constant
  249. // * Negation and Inverse only get 1 input, which is a variable in a loop
  250. // * Integrator and Derivator never get an incoming constant
  251. if (index_to_write_constant != -1):
  252. dict_overwrite(m[i], index_to_write_constant, -constant)
  253. i = i + 1
  254. // Constructed a complete matrix, so we can start!
  255. log("Constructed matrix to solve:")
  256. log(matrix2string(m))
  257. // Solve matrix now
  258. eliminateGaussJordan(m)
  259. // Now go over m and set signals for each element
  260. // Assume that everything worked out...
  261. i = 0
  262. while (i < read_nr_out(m)):
  263. block = scc[i]
  264. unset_attribute(model, block, "signal")
  265. instantiate_attribute(model, block, "signal", m[i][read_nr_out(scc)])
  266. log((("Solved " + block) + " to ") + cast_v2s(m[i][read_nr_out(scc)]))
  267. output((("SIM_PROBE " + cast_v2s(block)) + " ") + cast_v2s(m[i][read_nr_out(scc)]))
  268. return True!
  269. Integer function list_index_of(lst : Element, elem : Element):
  270. Integer i
  271. i = 0
  272. while (i < read_nr_out(lst)):
  273. if (value_eq(list_read(lst, i), elem)):
  274. return i!
  275. else:
  276. i = i + 1
  277. return -1!
  278. Void function step_simulation(model : Element, schedule : Element):
  279. String time
  280. Float signal
  281. Element incoming
  282. String selected
  283. String block
  284. String elem
  285. String blocktype
  286. Element memory_blocks
  287. Integer i
  288. Float delta_t
  289. Element scc
  290. time = "time"
  291. delta_t = 0.1
  292. memory_blocks = create_node()
  293. output("SIM_TIME " + cast_v2s(read_attribute(model, time, "current_time")))
  294. i = 0
  295. while (i < read_nr_out(schedule)):
  296. scc = list_read(schedule, i)
  297. i = i + 1
  298. if (list_len(scc) > 1):
  299. log("Solving algebraic loop!")
  300. if (bool_not(solve_scc(model, scc))):
  301. output("ALGEBRAIC_LOOP")
  302. return !
  303. else:
  304. block = set_pop(scc)
  305. // Execute "block"
  306. blocktype = readType(model, block)
  307. if (blocktype == "ConstantBlock"):
  308. signal = read_attribute(model, block, "value")
  309. elif (blocktype == "AdditionBlock"):
  310. signal = 0.0
  311. incoming = allIncomingAssociationInstances(model, block, "Link")
  312. while (read_nr_out(incoming) > 0):
  313. selected = readAssociationSource(model, set_pop(incoming))
  314. signal = signal + cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  315. elif (blocktype == "MultiplyBlock"):
  316. signal = 1.0
  317. incoming = allIncomingAssociationInstances(model, block, "Link")
  318. while (read_nr_out(incoming) > 0):
  319. selected = readAssociationSource(model, set_pop(incoming))
  320. signal = signal * cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  321. elif (blocktype == "NegatorBlock"):
  322. incoming = allIncomingAssociationInstances(model, block, "Link")
  323. signal = 0.0
  324. while (read_nr_out(incoming) > 0):
  325. selected = readAssociationSource(model, set_pop(incoming))
  326. signal = float_neg(cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  327. elif (blocktype == "InverseBlock"):
  328. signal = 0.0
  329. incoming = allIncomingAssociationInstances(model, block, "Link")
  330. while (read_nr_out(incoming) > 0):
  331. selected = readAssociationSource(model, set_pop(incoming))
  332. signal = float_division(1.0, cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  333. elif (blocktype == "DelayBlock"):
  334. signal = 0.0
  335. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  336. // No memory yet, so use initial condition
  337. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  338. while (read_nr_out(incoming) > 0):
  339. selected = readAssociationSource(model, set_pop(incoming))
  340. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  341. else:
  342. signal = read_attribute(model, block, "last_in")
  343. unset_attribute(model, block, "last_in")
  344. set_add(memory_blocks, block)
  345. elif (blocktype == "IntegratorBlock"):
  346. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  347. // No history yet, so use initial values
  348. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  349. while (read_nr_out(incoming) > 0):
  350. selected = readAssociationSource(model, set_pop(incoming))
  351. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  352. else:
  353. signal = cast_s2f(cast_v2s(read_attribute(model, block, "last_in"))) + (delta_t * cast_s2f(cast_v2s(read_attribute(model, block, "last_out"))))
  354. unset_attribute(model, block, "last_in")
  355. unset_attribute(model, block, "last_out")
  356. instantiate_attribute(model, block, "last_out", signal)
  357. set_add(memory_blocks, block)
  358. elif (blocktype == "DerivatorBlock"):
  359. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  360. // No history yet, so use initial values
  361. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  362. while (read_nr_out(incoming) > 0):
  363. selected = readAssociationSource(model, set_pop(incoming))
  364. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  365. else:
  366. incoming = allIncomingAssociationInstances(model, block, "Link")
  367. while (read_nr_out(incoming) > 0):
  368. selected = readAssociationSource(model, set_pop(incoming))
  369. signal = (cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))) - cast_s2f(cast_v2s(read_attribute(model, block, "last_in")))) / delta_t
  370. unset_attribute(model, block, "last_in")
  371. set_add(memory_blocks, block)
  372. unset_attribute(model, block, "signal")
  373. instantiate_attribute(model, block, "signal", signal)
  374. output((("SIM_PROBE " + cast_v2s(block)) + " ") + cast_v2s(signal))
  375. output("SIM_END")
  376. while (read_nr_out(memory_blocks) > 0):
  377. block = set_pop(memory_blocks)
  378. // Update memory
  379. incoming = allIncomingAssociationInstances(model, block, "Link")
  380. while (read_nr_out(incoming) > 0):
  381. selected = readAssociationSource(model, set_pop(incoming))
  382. instantiate_attribute(model, block, "last_in", cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  383. // Increase simulation time
  384. Float new_time
  385. new_time = cast_s2f(cast_v2s(read_attribute(model, time, "current_time"))) + delta_t
  386. unset_attribute(model, time, "current_time")
  387. instantiate_attribute(model, time, "current_time", new_time)
  388. return !
  389. Void function execute_cbd(design_model : Element):
  390. String verify_result
  391. Element runtime_model
  392. Element old_runtime_model
  393. String cmd
  394. Boolean running
  395. Element schedule_init
  396. Element schedule_run
  397. Element schedule
  398. String conforming
  399. old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  400. runtime_model = retype_to_runtime(design_model)
  401. runtime_model = sanitize(runtime_model, old_runtime_model)
  402. running = False
  403. conforming = conformance_scd(design_model)
  404. if (conforming == "OK"):
  405. output("CONFORMANCE_OK")
  406. else:
  407. output("CONFORMANCE_FAIL")
  408. schedule_init = create_schedule(runtime_model)
  409. schedule_run = read_root()
  410. while (True):
  411. // If we are running, we just don't block for input and automatically do a step if there is no input
  412. if (running):
  413. if (has_input()):
  414. cmd = input()
  415. else:
  416. cmd = "step"
  417. else:
  418. cmd = input()
  419. // Process input
  420. if (cmd == "simulate"):
  421. // Simulation should toggle running to True, but only if the model is conforming
  422. if (conforming == "OK"):
  423. running = True
  424. else:
  425. output("CONFORMANCE_FAIL " + conforming)
  426. elif (cmd == "step"):
  427. // Stepping should make a single step, but first need to pick the correct schedule to use
  428. if (conforming == "OK"):
  429. if (read_attribute(runtime_model, "time", "start_time") == read_attribute(runtime_model, "time", "current_time")):
  430. schedule = schedule_init
  431. else:
  432. if (element_eq(schedule_run, read_root())):
  433. schedule_run = create_schedule(runtime_model)
  434. schedule = schedule_run
  435. // TODO remove
  436. schedule = create_schedule(runtime_model)
  437. step_simulation(runtime_model, schedule)
  438. else:
  439. output("CONFORMANCE_FAIL " + conforming)
  440. elif (cmd == "pause"):
  441. // Pausing merely stops a running simulation
  442. running = False
  443. elif (cmd == "read_available_attributes"):
  444. // Returns a list of all available attributes
  445. Element attr_list
  446. Element attrs
  447. Element attr
  448. attr_list = getAttributeList(design_model, input())
  449. attrs = dict_keys(attr_list)
  450. while (0 < read_nr_out(attrs)):
  451. attr = set_pop(attrs)
  452. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  453. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  454. output("AVAILABLE_ATTR_END")
  455. elif (cmd == "read_attribute"):
  456. // Returns the value of an attribute
  457. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  458. elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association"))):
  459. // Modify the structure
  460. if (cmd == "set_attribute"):
  461. // Setting an attribute
  462. String element_name
  463. String attribute_name
  464. element_name = input()
  465. attribute_name = input()
  466. // Delete it if it exists already
  467. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  468. unset_attribute(design_model, element_name, attribute_name)
  469. // And finally set it
  470. instantiate_attribute(design_model, element_name, attribute_name, input())
  471. elif (cmd == "instantiate_node"):
  472. // Instantiate a node
  473. instantiate_node(design_model, input(), input())
  474. elif (cmd == "instantiate_association"):
  475. // Instantiate an association
  476. instantiate_link(design_model, input(), input(), input(), input())
  477. elif (cmd == "delete_element"):
  478. // Delete the provided element
  479. model_delete_element(design_model, input())
  480. // After changes, we check whether or not the design model conforms
  481. conforming = conformance_scd(design_model)
  482. if (conforming == "OK"):
  483. // Conforming, so do the retyping and sanitization step
  484. runtime_model = retype_to_runtime(design_model)
  485. runtime_model = sanitize(runtime_model, old_runtime_model)
  486. schedule_init = create_schedule(runtime_model)
  487. schedule_run = read_root()
  488. old_runtime_model = runtime_model
  489. output("CONFORMANCE_OK")
  490. else:
  491. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  492. running = False
  493. output("CONFORMANCE_FAIL " + conforming)
  494. else:
  495. log("Did not understand command: " + cmd)
  496. Float function v2f(i : Element):
  497. return cast_s2f(cast_v2s(i))!
  498. Void function eliminateGaussJordan(m : Element):
  499. Integer i
  500. Integer j
  501. Integer f
  502. Integer g
  503. Boolean searching
  504. Element t
  505. Float divisor
  506. i = 0
  507. j = 0
  508. while (i < read_nr_out(m)):
  509. // Make sure pivot m[i][j] != 0, swapping if necessary
  510. while (v2f(m[i][j]) == 0.0):
  511. // Is zero, so find row which is not zero
  512. f = i + 1
  513. searching = True
  514. while (searching):
  515. if (f >= read_nr_out(m)):
  516. // No longer any rows left, so just increase column counter
  517. searching = False
  518. j = j + 1
  519. else:
  520. if (v2f(m[f][j]) == 0.0):
  521. // Also zero, so continue
  522. f = f + 1
  523. else:
  524. // Found non-zero, so swap row
  525. t = v2f(m[f])
  526. dict_overwrite(m, f, v2f(m[i]))
  527. dict_overwrite(m, i, t)
  528. searching = False
  529. // If we have increased j, we will just start the loop again (possibly), as m[i][j] might be zero again
  530. // Pivot in m[i][j] guaranteed to not be 0
  531. // Now divide complete row by value of m[i][j] to make it equal 1
  532. f = j
  533. divisor = v2f(m[i][j])
  534. while (f < read_nr_out(m[i])):
  535. dict_overwrite(m[i], f, float_division(v2f(m[i][f]), divisor))
  536. f = f + 1
  537. // Eliminate all rows in the j-th column, except the i-th row
  538. f = 0
  539. while (f < read_nr_out(m)):
  540. if (bool_not(f == i)):
  541. g = j
  542. divisor = v2f(m[f][j])
  543. while (g < read_nr_out(m[f])):
  544. dict_overwrite(m[f], g, v2f(m[f][g]) - (divisor * v2f(m[i][g])))
  545. g = g + 1
  546. f = f + 1
  547. // Increase row and column
  548. i = i + 1
  549. j = j + 1
  550. return !
  551. String function matrix2string(m : Element):
  552. Integer i
  553. Integer j
  554. String result
  555. result = ""
  556. i = 0
  557. while (i < read_nr_out(m)):
  558. j = 0
  559. while (j < read_nr_out(m[i])):
  560. result = result + cast_v2s(m[i][j])
  561. result = result + ", "
  562. j = j + 1
  563. i = i + 1
  564. result = result + "\n"
  565. return result!
  566. Void function main():
  567. Element model
  568. String verify_result
  569. while (True):
  570. execute_cbd(instantiate_model(import_node("models/CausalBlockDiagrams_Design")))