cbd_semantics.alc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 = 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. log("Generating matrix for " + blocktype)
  208. log("Block: " + block)
  209. incoming = allIncomingAssociationInstances(model, block, "Link")
  210. Integer index_to_write_constant
  211. index_to_write_constant = -1
  212. log("Iterating over incoming")
  213. while (read_nr_out(incoming) > 0):
  214. log("Iteration")
  215. selected = readAssociationSource(model, set_pop(incoming))
  216. if (set_in(scc, selected)):
  217. // Part of the loop, so in the index of selected in scc
  218. // Five options:
  219. if (blocktype == "AdditionBlock"):
  220. // 1) AdditionBlock
  221. // Add the negative of this signal, which is as of yet unknown
  222. // x = y + z --> x - y - z = 0
  223. dict_overwrite(m[i], list_index_of(scc, selected), -1.0)
  224. elif (blocktype == "MultiplyBlock"):
  225. // 2) MultiplyBlock
  226. if (index_to_write_constant != -1):
  227. return False!
  228. index_to_write_constant = list_index_of(scc, selected)
  229. elif (blocktype == "NegatorBlock"):
  230. // 3) NegatorBlock
  231. // Add the positive of the signal, which is as of yet unknown
  232. dict_overwrite(m[i], list_index_of(scc, selected), 1.0)
  233. elif (blocktype == "DelayBlock"):
  234. // 5) DelayBlock
  235. // Just copies a single value
  236. dict_overwrite(m[i], list_index_of(scc, selected), -1.0)
  237. else:
  238. // Block that cannot be handled
  239. return False!
  240. else:
  241. // A constant, which we can assume is already computed and thus usable
  242. if (blocktype == "AdditionBlock"):
  243. constant = constant + v2f(read_attribute(model, selected, "signal"))
  244. dict_overwrite(m[i], read_nr_out(scc), constant)
  245. elif (blocktype == "MultiplyBlock"):
  246. constant = constant * v2f(read_attribute(model, selected, "signal"))
  247. // Not written to constant part, as multiplies a variable
  248. // Any other block is impossible:
  249. // * Constant would never be part of a SCC
  250. // * Delay would never get an incoming constant
  251. // * Negation and Inverse only get 1 input, which is a variable in a loop
  252. // * Integrator and Derivator never get an incoming constant
  253. if (index_to_write_constant != -1):
  254. dict_overwrite(m[i], index_to_write_constant, -constant)
  255. i = i + 1
  256. // Constructed a complete matrix, so we can start!
  257. log("Constructed matrix to solve:")
  258. log(matrix2string(m))
  259. // Solve matrix now
  260. eliminateGaussJordan(m)
  261. // Now go over m and set signals for each element
  262. // Assume that everything worked out...
  263. i = 0
  264. while (i < read_nr_out(m)):
  265. block = scc[i]
  266. unset_attribute(model, block, "signal")
  267. instantiate_attribute(model, block, "signal", m[i][read_nr_out(scc)])
  268. log((("Solved " + block) + " to ") + cast_v2s(m[i][read_nr_out(scc)]))
  269. output((("SIM_PROBE " + cast_v2s(block)) + " ") + cast_v2s(m[i][read_nr_out(scc)]))
  270. i = i + 1
  271. return True!
  272. Integer function list_index_of(lst : Element, elem : Element):
  273. Integer i
  274. i = 0
  275. while (i < read_nr_out(lst)):
  276. if (value_eq(list_read(lst, i), elem)):
  277. return i!
  278. else:
  279. i = i + 1
  280. return -1!
  281. Void function step_simulation(model : Element, schedule : Element):
  282. String time
  283. Float signal
  284. Element incoming
  285. String selected
  286. String block
  287. String elem
  288. String blocktype
  289. Element memory_blocks
  290. Integer i
  291. Float delta_t
  292. Element scc
  293. time = "time"
  294. delta_t = 0.1
  295. memory_blocks = create_node()
  296. output("SIM_TIME " + cast_v2s(read_attribute(model, time, "current_time")))
  297. i = 0
  298. while (i < read_nr_out(schedule)):
  299. scc = list_read(schedule, i)
  300. i = i + 1
  301. if (list_len(scc) > 1):
  302. log("Solving algebraic loop!")
  303. if (bool_not(solve_scc(model, scc))):
  304. output("ALGEBRAIC_LOOP")
  305. return !
  306. else:
  307. block = set_pop(scc)
  308. // Execute "block"
  309. blocktype = readType(model, block)
  310. if (blocktype == "ConstantBlock"):
  311. signal = read_attribute(model, block, "value")
  312. elif (blocktype == "AdditionBlock"):
  313. signal = 0.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 == "MultiplyBlock"):
  319. signal = 1.0
  320. incoming = allIncomingAssociationInstances(model, block, "Link")
  321. while (read_nr_out(incoming) > 0):
  322. selected = readAssociationSource(model, set_pop(incoming))
  323. signal = signal * cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  324. elif (blocktype == "NegatorBlock"):
  325. incoming = allIncomingAssociationInstances(model, block, "Link")
  326. signal = 0.0
  327. while (read_nr_out(incoming) > 0):
  328. selected = readAssociationSource(model, set_pop(incoming))
  329. signal = float_neg(cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  330. elif (blocktype == "InverseBlock"):
  331. signal = 0.0
  332. incoming = allIncomingAssociationInstances(model, block, "Link")
  333. while (read_nr_out(incoming) > 0):
  334. selected = readAssociationSource(model, set_pop(incoming))
  335. signal = float_division(1.0, cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  336. elif (blocktype == "DelayBlock"):
  337. signal = 0.0
  338. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  339. // No memory yet, so use initial condition
  340. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  341. while (read_nr_out(incoming) > 0):
  342. selected = readAssociationSource(model, set_pop(incoming))
  343. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  344. else:
  345. signal = read_attribute(model, block, "last_in")
  346. unset_attribute(model, block, "last_in")
  347. set_add(memory_blocks, block)
  348. elif (blocktype == "IntegratorBlock"):
  349. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  350. // No history yet, so use initial values
  351. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  352. while (read_nr_out(incoming) > 0):
  353. selected = readAssociationSource(model, set_pop(incoming))
  354. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  355. else:
  356. signal = cast_s2f(cast_v2s(read_attribute(model, block, "last_in"))) + (delta_t * cast_s2f(cast_v2s(read_attribute(model, block, "last_out"))))
  357. unset_attribute(model, block, "last_in")
  358. unset_attribute(model, block, "last_out")
  359. instantiate_attribute(model, block, "last_out", signal)
  360. set_add(memory_blocks, block)
  361. elif (blocktype == "DerivatorBlock"):
  362. if (element_eq(read_attribute(model, block, "last_in"), read_root())):
  363. // No history yet, so use initial values
  364. incoming = allIncomingAssociationInstances(model, block, "InitialCondition")
  365. while (read_nr_out(incoming) > 0):
  366. selected = readAssociationSource(model, set_pop(incoming))
  367. signal = cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
  368. else:
  369. incoming = allIncomingAssociationInstances(model, block, "Link")
  370. while (read_nr_out(incoming) > 0):
  371. selected = readAssociationSource(model, set_pop(incoming))
  372. signal = (cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))) - cast_s2f(cast_v2s(read_attribute(model, block, "last_in")))) / delta_t
  373. unset_attribute(model, block, "last_in")
  374. set_add(memory_blocks, block)
  375. unset_attribute(model, block, "signal")
  376. instantiate_attribute(model, block, "signal", signal)
  377. output((("SIM_PROBE " + cast_v2s(block)) + " ") + cast_v2s(signal))
  378. output("SIM_END")
  379. while (read_nr_out(memory_blocks) > 0):
  380. block = set_pop(memory_blocks)
  381. // Update memory
  382. incoming = allIncomingAssociationInstances(model, block, "Link")
  383. while (read_nr_out(incoming) > 0):
  384. selected = readAssociationSource(model, set_pop(incoming))
  385. instantiate_attribute(model, block, "last_in", cast_s2f(cast_v2s(read_attribute(model, selected, "signal"))))
  386. // Increase simulation time
  387. Float new_time
  388. new_time = cast_s2f(cast_v2s(read_attribute(model, time, "current_time"))) + delta_t
  389. unset_attribute(model, time, "current_time")
  390. instantiate_attribute(model, time, "current_time", new_time)
  391. return !
  392. Void function execute_cbd(design_model : Element):
  393. String verify_result
  394. Element runtime_model
  395. Element old_runtime_model
  396. String cmd
  397. Boolean running
  398. Element schedule_init
  399. Element schedule_run
  400. Element schedule
  401. String conforming
  402. old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
  403. runtime_model = retype_to_runtime(design_model)
  404. runtime_model = sanitize(runtime_model, old_runtime_model)
  405. running = False
  406. conforming = conformance_scd(design_model)
  407. if (conforming == "OK"):
  408. output("CONFORMANCE_OK")
  409. else:
  410. output("CONFORMANCE_FAIL")
  411. schedule_init = create_schedule(runtime_model)
  412. schedule_run = read_root()
  413. while (True):
  414. // If we are running, we just don't block for input and automatically do a step if there is no input
  415. if (running):
  416. if (has_input()):
  417. cmd = input()
  418. else:
  419. cmd = "step"
  420. else:
  421. cmd = input()
  422. // Process input
  423. if (cmd == "simulate"):
  424. // Simulation should toggle running to True, but only if the model is conforming
  425. if (conforming == "OK"):
  426. running = True
  427. else:
  428. output("CONFORMANCE_FAIL " + conforming)
  429. elif (cmd == "step"):
  430. // Stepping should make a single step, but first need to pick the correct schedule to use
  431. if (conforming == "OK"):
  432. if (read_attribute(runtime_model, "time", "start_time") == read_attribute(runtime_model, "time", "current_time")):
  433. schedule = schedule_init
  434. else:
  435. if (element_eq(schedule_run, read_root())):
  436. schedule_run = create_schedule(runtime_model)
  437. schedule = schedule_run
  438. // TODO remove
  439. schedule = create_schedule(runtime_model)
  440. step_simulation(runtime_model, schedule)
  441. else:
  442. output("CONFORMANCE_FAIL " + conforming)
  443. elif (cmd == "pause"):
  444. // Pausing merely stops a running simulation
  445. running = False
  446. elif (cmd == "read_available_attributes"):
  447. // Returns a list of all available attributes
  448. Element attr_list
  449. Element attrs
  450. Element attr
  451. attr_list = getAttributeList(design_model, input())
  452. attrs = dict_keys(attr_list)
  453. while (0 < read_nr_out(attrs)):
  454. attr = set_pop(attrs)
  455. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  456. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  457. output("AVAILABLE_ATTR_END")
  458. elif (cmd == "read_attribute"):
  459. // Returns the value of an attribute
  460. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  461. elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association"))):
  462. // Modify the structure
  463. if (cmd == "set_attribute"):
  464. // Setting an attribute
  465. String element_name
  466. String attribute_name
  467. element_name = input()
  468. attribute_name = input()
  469. // Delete it if it exists already
  470. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  471. unset_attribute(design_model, element_name, attribute_name)
  472. // And finally set it
  473. instantiate_attribute(design_model, element_name, attribute_name, input())
  474. elif (cmd == "instantiate_node"):
  475. // Instantiate a node
  476. instantiate_node(design_model, input(), input())
  477. elif (cmd == "instantiate_association"):
  478. // Instantiate an association
  479. instantiate_link(design_model, input(), input(), input(), input())
  480. elif (cmd == "delete_element"):
  481. // Delete the provided element
  482. model_delete_element(design_model, input())
  483. // After changes, we check whether or not the design model conforms
  484. conforming = conformance_scd(design_model)
  485. if (conforming == "OK"):
  486. // Conforming, so do the retyping and sanitization step
  487. runtime_model = retype_to_runtime(design_model)
  488. runtime_model = sanitize(runtime_model, old_runtime_model)
  489. schedule_init = create_schedule(runtime_model)
  490. schedule_run = read_root()
  491. old_runtime_model = runtime_model
  492. output("CONFORMANCE_OK")
  493. else:
  494. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  495. running = False
  496. output("CONFORMANCE_FAIL " + conforming)
  497. else:
  498. log("Did not understand command: " + cmd)
  499. Float function v2f(i : Element):
  500. return cast_s2f(cast_v2s(i))!
  501. Void function eliminateGaussJordan(m : Element):
  502. Integer i
  503. Integer j
  504. Integer f
  505. Integer g
  506. Boolean searching
  507. Element t
  508. Float divisor
  509. i = 0
  510. j = 0
  511. while (i < read_nr_out(m)):
  512. // Make sure pivot m[i][j] != 0, swapping if necessary
  513. while (v2f(m[i][j]) == 0.0):
  514. // Is zero, so find row which is not zero
  515. f = i + 1
  516. searching = True
  517. while (searching):
  518. if (f >= read_nr_out(m)):
  519. // No longer any rows left, so just increase column counter
  520. searching = False
  521. j = j + 1
  522. else:
  523. if (v2f(m[f][j]) == 0.0):
  524. // Also zero, so continue
  525. f = f + 1
  526. else:
  527. // Found non-zero, so swap row
  528. t = v2f(m[f])
  529. dict_overwrite(m, f, v2f(m[i]))
  530. dict_overwrite(m, i, t)
  531. searching = False
  532. // If we have increased j, we will just start the loop again (possibly), as m[i][j] might be zero again
  533. // Pivot in m[i][j] guaranteed to not be 0
  534. // Now divide complete row by value of m[i][j] to make it equal 1
  535. f = j
  536. divisor = v2f(m[i][j])
  537. while (f < read_nr_out(m[i])):
  538. dict_overwrite(m[i], f, float_division(v2f(m[i][f]), divisor))
  539. f = f + 1
  540. // Eliminate all rows in the j-th column, except the i-th row
  541. f = 0
  542. while (f < read_nr_out(m)):
  543. if (bool_not(f == i)):
  544. g = j
  545. divisor = v2f(m[f][j])
  546. while (g < read_nr_out(m[f])):
  547. dict_overwrite(m[f], g, v2f(m[f][g]) - (divisor * v2f(m[i][g])))
  548. g = g + 1
  549. f = f + 1
  550. // Increase row and column
  551. i = i + 1
  552. j = j + 1
  553. return !
  554. String function matrix2string(m : Element):
  555. Integer i
  556. Integer j
  557. String result
  558. result = ""
  559. i = 0
  560. while (i < read_nr_out(m)):
  561. j = 0
  562. while (j < read_nr_out(m[i])):
  563. result = result + cast_v2s(m[i][j])
  564. result = result + ", "
  565. j = j + 1
  566. i = i + 1
  567. result = result + "\n"
  568. return result!
  569. Void function main():
  570. Element model
  571. String verify_result
  572. while (True):
  573. execute_cbd(instantiate_model(import_node("models/CausalBlockDiagrams_Design")))