code wiki / _hdl_build / nx_product_decompose.nx
nx_product_decompose.nx source
↩ module page · 145 lines · 7038 B
1// nx_product_decompose.nx v2 -- PRODUCT decomposition (name-an-outcome -> required capabilities -> gaps) over
2// the real design-to-market NEEDS + CAPABILITIES planes, WITH persistence: writes the coverage/gap result to a
3// decompose- plane (the atlas-out compounding loop -- results bank back for the F208 opportunity feeder). Each
4// need matched to a fulfilling capability by name-in-note substring; needs with none = unproven-opportunity GAPS.
5// usage: nx_product_decompose [needs-prefix] [caps-prefix] [out-prefix] license_tier: ORIGINAL expect_exit:0
6import "nx_store_seed_lib.nx"
7import "nx_seg_store.nx"
8import "nx_syscalls.nx"
9const PD_MAGIC_1024: i64 = 1024
10
11const PD_CAP: i64 = 262144
12const PD_NL: i64 = 10
13const PD_TAB: i64 = 9
14const PD_MAXCOL: i64 = 16
15const PD_MAXROW: i64 = 64
16const PD_STDERR: i64 = 2
17const CT_NOTE: i64 = 6
18const PD_ERR_CAPACITY: i64 = 0 - 1
19
20func pd_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
21func pd_werr(s: *u8) -> i64 { sys_write(PD_STDERR, s, pd_slen(s)); return 0 }
22func pd_w(s: *u8) -> i64 { sys_write(1, s, pd_slen(s)); return 0 }
23func pd_wn(v: i64) -> i64 { let b: *u8 = sys_mmap(24); var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } let t: *u8 = sys_mmap(24); 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 j: i64 = 0; while j < k { b[j] = t[k-1-j]; j = j + 1 } sys_write(1, b, k); return 0 }
24func pd_wslice(q: *u8, a: i64, b: i64) -> i64 { let buf: *u8 = sys_mmap(PD_MAGIC_1024); var n: i64 = 0; var i: i64 = a; while i < b { if n < PD_MAGIC_1024 { buf[n] = q[i]; n = n + 1 } i = i + 1 } if n > 0 { sys_write(1, buf, n) } return 0 }
25func pd_cat(dst: *u8, o: i64, src: *u8, a: i64, b: i64) -> i64 { var oo: i64 = o; var i: i64 = a; while i < b { dst[oo] = src[i]; oo = oo + 1; i = i + 1 } return oo }
26func pd_cols(q: *u8, ls: i64, le: i64, sp: *i64) -> i64 {
27 var c: i64 = 0
28 var p: i64 = ls
29 while c < PD_MAXCOL {
30 var e: i64 = p
31 var s: i64 = 1
32 while s == 1 { if e >= le { s = 0 } else { if q[e] == (PD_TAB as u8) { s = 0 } else { e = e + 1 } } }
33 sp[c*2] = p
34 sp[c*2+1] = e
35 c = c + 1
36 if e >= le { return c }
37 p = e + 1
38 }
39 return c
40}
41func pd_has_sub(q: *u8, a: i64, b: i64, w: *u8, wl: i64) -> i64 {
42 if wl < 3 { return 0 }
43 var i: i64 = a
44 while i + wl <= b {
45 var m: i64 = 1
46 var j: i64 = 0
47 while j < wl { if q[i+j] != w[j] { m = 0; j = wl } else { j = j + 1 } }
48 if m == 1 { return 1 }
49 i = i + 1
50 }
51 return 0
52}
53func pd_parse_capacity(buf: *u8, n: i64, na: *i64, nb: *i64, ta: *i64, tb: *i64, row_capacity: i64) -> i64 {
54 if n < 0 || row_capacity < 0 { return PD_ERR_CAPACITY }
55 let sp: *i64 = sys_mmap(PD_MAXCOL*2*8) as *i64
56 var rc: i64 = 0
57 var i: i64 = 0
58 while i < n {
59 var le: i64 = i
60 while le < n { if buf[le] == (PD_NL as u8) { break }; le = le + 1 }
61 if le > i {
62 if rc >= row_capacity { sys_munmap(sp as *u8,PD_MAXCOL*2*8); return PD_ERR_CAPACITY }
63 let nc: i64 = pd_cols(buf,i,le,sp)
64 if nc < 1 { sys_munmap(sp as *u8,PD_MAXCOL*2*8); return PD_ERR_CAPACITY }
65 na[rc]=sp[0];nb[rc]=sp[1]
66 if nc > CT_NOTE { ta[rc]=sp[CT_NOTE*2];tb[rc]=sp[CT_NOTE*2+1] }
67 else { ta[rc]=sp[0];tb[rc]=sp[1] }
68 rc=rc+1
69 }
70 i=le+1
71 }
72 sys_munmap(sp as *u8,PD_MAXCOL*2*8)
73 return rc
74}
75func pd_parse(buf: *u8,n: i64,na: *i64,nb: *i64,ta: *i64,tb: *i64) -> i64 {
76 return pd_parse_capacity(buf,n,na,nb,ta,tb,PD_MAXROW)
77}
78
79func main(argc: i64, argv: *i64) -> i64 {
80 var needsp: *u8 = "knowledge/store/dmktneeds-" as *u8
81 var capsp: *u8 = "knowledge/store/dmktcap-" as *u8
82 var outp: *u8 = "knowledge/store/decompose-" as *u8
83 if argc > 1 { needsp = argv[1] as *u8 }
84 if argc > 2 { capsp = argv[2] as *u8 }
85 if argc > 3 { outp = argv[3] as *u8 }
86 let nbuf: *u8 = sys_mmap(PD_CAP)
87 let nn: i64 = sts_load(needsp, nbuf, PD_CAP)
88 if nn <= 0 { pd_werr("needs plane EMPTY (fail-closed)\n" as *u8); sys_exit(1); return 1 }
89 let cbuf: *u8 = sys_mmap(PD_CAP)
90 let cnn: i64 = sts_load(capsp, cbuf, PD_CAP)
91 if cnn <= 0 { pd_werr("caps plane EMPTY (fail-closed)\n" as *u8); sys_exit(1); return 1 }
92 let nna: *i64 = sys_mmap(PD_MAXROW*8) as *i64
93 let nnb: *i64 = sys_mmap(PD_MAXROW*8) as *i64
94 let nta: *i64 = sys_mmap(PD_MAXROW*8) as *i64
95 let ntb: *i64 = sys_mmap(PD_MAXROW*8) as *i64
96 let nrows: i64 = pd_parse(nbuf, nn, nna, nnb, nta, ntb)
97 if nrows < 0 { pd_werr("needs row capacity exceeded; no coverage published\n" as *u8); return 2 }
98 let cna: *i64 = sys_mmap(PD_MAXROW*8) as *i64
99 let cnb: *i64 = sys_mmap(PD_MAXROW*8) as *i64
100 let cta: *i64 = sys_mmap(PD_MAXROW*8) as *i64
101 let ctb: *i64 = sys_mmap(PD_MAXROW*8) as *i64
102 let crows: i64 = pd_parse(cbuf, cnn, cna, cnb, cta, ctb)
103 if crows < 0 { pd_werr("capabilities row capacity exceeded; no coverage published\n" as *u8); return 2 }
104 pd_w("=== nx_product_decompose v2: name-an-outcome -> required capabilities -> gaps (persisted) ===\n" as *u8)
105 pd_w("needs=" as *u8); pd_wn(nrows); pd_w(" capabilities=" as *u8); pd_wn(crows); pd_w("\n" as *u8)
106 let obuf: *u8 = sys_mmap(PD_CAP)
107 var oo: i64 = 0
108 let nmb: *u8 = sys_mmap(256)
109 var covered: i64 = 0
110 var gaps: i64 = 0
111 var i: i64 = 0
112 while i < nrows {
113 var nml: i64 = 0
114 var kk: i64 = nna[i]
115 while kk < nnb[i] { if nml < 255 { nmb[nml] = nbuf[kk]; nml = nml + 1 } kk = kk + 1 }
116 var hit: i64 = 0 - 1
117 var j: i64 = 0
118 while j < crows {
119 if hit < 0 { if pd_has_sub(cbuf, cta[j], ctb[j], nmb, nml) == 1 { hit = j } }
120 j = j + 1
121 }
122 oo = ss_cat(obuf, oo, "need-" as *u8)
123 oo = pd_cat(obuf, oo, nbuf, nna[i], nnb[i])
124 obuf[oo] = PD_TAB as u8; oo = oo + 1
125 oo = pd_cat(obuf, oo, nbuf, nna[i], nnb[i])
126 obuf[oo] = PD_TAB as u8; oo = oo + 1
127 pd_w("NEED " as *u8); pd_wslice(nbuf, nna[i], nnb[i])
128 if hit >= 0 {
129 pd_w(" -> FULFILLED by " as *u8); pd_wslice(cbuf, cna[hit], cnb[hit]); pd_w("\n" as *u8)
130 oo = ss_cat(obuf, oo, "FULFILLED\tproduct\t" as *u8)
131 oo = pd_cat(obuf, oo, cbuf, cna[hit], cnb[hit])
132 covered = covered + 1
133 } else {
134 pd_w(" -> GAP: no capability yet (unproven-opportunity to build)\n" as *u8)
135 oo = ss_cat(obuf, oo, "GAP\tproduct\tunproven-opportunity-build-next" as *u8)
136 gaps = gaps + 1
137 }
138 obuf[oo] = PD_NL as u8; oo = oo + 1
139 i = i + 1
140 }
141 if sts_seed(outp, obuf, oo) < 0 { pd_werr("decompose plane commit error\n" as *u8) }
142 pd_w("COVERAGE covered=" as *u8); pd_wn(covered); pd_w("/" as *u8); pd_wn(nrows); pd_w(" gaps=" as *u8); pd_wn(gaps); pd_w(" -> persisted to the decompose- plane (atlas-out loop)\n" as *u8)
143 sys_exit(0)
144 return 0
145}