code wiki / (root) / nx_idea_tree.nx

nx_idea_tree.nx source

↩ module page · 160 lines · 6686 B

1// nx_idea_tree.nx -- the ANCESTRY view of the idea genealogy (genealogist criterion 5). 2// 3// Renders, for every idea in idea_genealogy.tsv, its LOGIC-GENEALOGY chain walked UP the 4// descends_from edges to the root insight, plus who/when and the organ it is realized_in. This is 5// the "this idea produced this produced this" view the operator asked for, as the human-readable 6// face of the tree. Iterative (no recursion), cycle-guarded, MEASURED from the real registry only. 7// Output to stdout + append-only knowledge/status/idea_tree.txt. Sovereign syscalls only. 8// 9// module: nishi-core.genealogy.idea_tree 10// depends: nishi-core.genealogy.idea_genealogist 11// capability: GENEALOGY 12// license_tier: ORIGINAL 13import "nx_syscalls.nx" 14const IT_MAGIC_1048576: i64 = 1048576 15const IT_MAGIC_1048575: i64 = 1048575 16 17const IT_IDEA: *u8 = "knowledge/registry/idea_genealogy.tsv" 18const IT_OUT: *u8 = "knowledge/status/idea_tree.txt" 19const IT_CAP: i64 = 256 20const IT_MAX: i64 = 256 21 22func it_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 23func it_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48 as u8;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 } 24 25func it_streq(a: *u8, b: *u8) -> i64 { 26 var i: i64 = 0 27 while i < IT_CAP { if a[i] != b[i] { return 0 } if a[i] == (0 as u8) { return 1 } i = i + 1 } 28 return 1 29} 30 31func it_field_tab(buf: *u8, ls: i64, n: i64, fidx: i64, out: *u8, outcap: i64) -> i64 { 32 var p: i64 = ls 33 var f: i64 = 0 34 var bad: i64 = 0 35 while f < fidx { 36 if p >= n { bad = 1; f = fidx } else { 37 if buf[p] == (10 as u8) { bad = 1; f = fidx } else { 38 if buf[p] == (9 as u8) { f = f + 1 } 39 p = p + 1 40 } 41 } 42 } 43 if bad == 1 { out[0] = 0 as u8; return 0 } 44 var k: i64 = 0 45 var go: i64 = 1 46 while go == 1 { 47 if p >= n { go = 0 } else { 48 if buf[p] == (9 as u8) { go = 0 } else { if buf[p] == (10 as u8) { go = 0 } else { 49 if k < outcap - 1 { out[k] = buf[p]; k = k + 1 } 50 p = p + 1 51 } } 52 } 53 } 54 out[k] = 0 as u8 55 return k 56} 57 58// load 5 fields (id=0, who=2, when=4, descends_from=7, realized_in=8) per non-comment row. 59func it_load(path: *u8, aid: i64, awho: i64, awhen: i64, adesc: i64, areal: i64, cap: i64, maxc: i64) -> i64 { 60 let buf: *u8 = sys_mmap(IT_MAGIC_1048576) 61 let fd: i64 = sys_openat_rd(path) 62 if fd < 0 { return 0 } 63 var n: i64 = 0 64 var r: i64 = sys_read(fd, buf, IT_MAGIC_1048575) 65 while r > 0 { n = n + r; r = sys_read(fd, buf + n, IT_MAGIC_1048575 - n) } 66 sys_close(fd) 67 var cnt: i64 = 0 68 var i: i64 = 0 69 while i < n { 70 if buf[i] == (35 as u8) { 71 var sk: i64 = 1 72 while sk == 1 { if i >= n { sk = 0 } else { if buf[i] == (10 as u8) { sk = 0 } i = i + 1 } } 73 } else { 74 if cnt < maxc { 75 it_field_tab(buf, i, n, 0, (aid + cnt * cap) as *u8, cap) 76 it_field_tab(buf, i, n, 2, (awho + cnt * cap) as *u8, cap) 77 it_field_tab(buf, i, n, 4, (awhen + cnt * cap) as *u8, cap) 78 it_field_tab(buf, i, n, 7, (adesc + cnt * cap) as *u8, cap) 79 it_field_tab(buf, i, n, 8, (areal + cnt * cap) as *u8, cap) 80 let d0: *u8 = (aid + cnt * cap) as *u8 81 if d0[0] != (0 as u8) { cnt = cnt + 1 } 82 } 83 var le: i64 = i 84 var s2: i64 = 1 85 while s2 == 1 { if le >= n { s2 = 0 } else { if buf[le] == (10 as u8) { s2 = 0 } else { le = le + 1 } } } 86 i = le + 1 87 } 88 } 89 return cnt 90} 91 92func it_find(aid: i64, cnt: i64, cap: i64, name: *u8) -> i64 { 93 var i: i64 = 0 94 while i < cnt { if it_streq((aid + i * cap) as *u8, name) == 1 { return i } i = i + 1 } 95 return 0 - 1 96} 97 98func it_isdash(s: *u8) -> i64 { 99 if s[0] == (45 as u8) { if s[1] == (0 as u8) { return 1 } } 100 if s[0] == (0 as u8) { return 1 } 101 return 0 102} 103 104// render one idea's ancestry chain to root, to fd. 105func it_render(fd: i64, i: i64, aid: i64, awho: i64, awhen: i64, adesc: i64, areal: i64, cnt: i64, cap: i64) -> i64 { 106 it_w(fd, (aid + i * cap) as *u8) 107 let d0: *u8 = (adesc + i * cap) as *u8 108 if it_isdash(d0) == 1 { it_w(fd, " [ROOT IDEA]" as *u8) } 109 else { 110 var p: *u8 = d0 111 var iter: i64 = 0 112 var go: i64 = 1 113 while go == 1 { 114 if iter >= 32 { go = 0 } else { 115 it_w(fd, " <- " as *u8); it_w(fd, p) 116 let j: i64 = it_find(aid, cnt, cap, p) 117 if j < 0 { it_w(fd, "(?dangling)" as *u8); go = 0 } else { 118 let pd: *u8 = (adesc + j * cap) as *u8 119 if it_isdash(pd) == 1 { it_w(fd, " [root]" as *u8); go = 0 } else { p = pd } 120 } 121 iter = iter + 1 122 } 123 } 124 } 125 it_w(fd, " (" as *u8); it_w(fd, (awho + i * cap) as *u8) 126 it_w(fd, ", " as *u8); it_w(fd, (awhen + i * cap) as *u8); it_w(fd, ")" as *u8) 127 let rl: *u8 = (areal + i * cap) as *u8 128 if it_isdash(rl) == 0 { it_w(fd, " ->realized_in=" as *u8); it_w(fd, rl) } 129 it_w(fd, "\n" as *u8) 130 return 0 131} 132 133func main() -> i64 { 134 let aid: i64 = sys_mmap(IT_MAX * IT_CAP) as i64 135 let awho: i64 = sys_mmap(IT_MAX * IT_CAP) as i64 136 let awhen: i64 = sys_mmap(IT_MAX * IT_CAP) as i64 137 let adesc: i64 = sys_mmap(IT_MAX * IT_CAP) as i64 138 let areal: i64 = sys_mmap(IT_MAX * IT_CAP) as i64 139 let cnt: i64 = it_load(IT_IDEA, aid, awho, awhen, adesc, areal, IT_CAP, IT_MAX) 140 141 let ofd: i64 = sys_openat_append(IT_OUT, 420) 142 let epoch: i64 = sys_now_realtime_sec() 143 144 it_w(1, "IDEA-TREE ancestry view (logic genealogy: this <- ... <- root), ideas=" as *u8); it_wn(1, cnt) 145 it_w(1, " epoch=" as *u8); it_wn(1, epoch); it_w(1, "\n" as *u8) 146 if ofd >= 0 { it_w(ofd, "IDEA-TREE ancestry view ideas=" as *u8); it_wn(ofd, cnt); it_w(ofd, " epoch=" as *u8); it_wn(ofd, epoch); it_w(ofd, "\n" as *u8) } 147 148 var i: i64 = 0 149 while i < cnt { 150 it_render(1, i, aid, awho, awhen, adesc, areal, cnt, IT_CAP) 151 if ofd >= 0 { it_render(ofd, i, aid, awho, awhen, adesc, areal, cnt, IT_CAP) } 152 i = i + 1 153 } 154 if ofd >= 0 { sys_close(ofd) } 155 156 var ok: i64 = 1 157 if cnt == 0 { ok = 0 } 158 if ok == 1 { it_w(1, "IDEA-TREE verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 159 it_w(1, "IDEA-TREE verdict=RED\n" as *u8); sys_exit(1); return 1 160}