cbd_semantics.alc 22 KB

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