code wiki / (root) / nx_ontology_test.nx

nx_ontology_test.nx source

↩ module page · 71 lines · 3211 B

1// nx_ontology_test.nx -- smoke for the substrate ontology primitive. 2// 3// Builds a synthetic ontology at /tmp/nx_ontology_t1.txt with known 4// triples covering each predicate type, then verifies count + lookup 5// + domain queries. 6 7import "nx_syscalls.nx" 8import "nx_tier.nx" 9import "nx_str.nx" 10import "nx_ontology.nx" 11 12const NX_ONT_T_MODE: i64 = 420 // 0o644 13 14func nx_ont_t_write(fd: i64, s: *u8) -> nx_int { 15 var n: i64 = 0 16 while s[n] != 0 { n = n + 1 } 17 let w: i64 = sys_write(fd, s, n) 18 if w != n { return 1 } 19 return 0 20} 21 22func nx_ont_t_fixture(path: *u8) -> nx_int { 23 let fd: i64 = sys_openat_wr(path, NX_ONT_T_MODE) 24 if fd < 0 { return 1 } 25 26 if nx_ont_t_write(fd, "# ontology fixture\n" as *u8) != 0 { return 2 } 27 // 6 triples 28 if nx_ont_t_write(fd, "quicksort\tis_a\tsorting_algorithm\n" as *u8) != 0 { return 3 } 29 if nx_ont_t_write(fd, "quicksort\tin_domain\tcompetitive_programming\n" as *u8) != 0 { return 4 } 30 if nx_ont_t_write(fd, "dijkstra\tis_a\tsssp_algorithm\n" as *u8) != 0 { return 5 } 31 if nx_ont_t_write(fd, "dijkstra\tin_domain\tcompetitive_programming\n" as *u8) != 0 { return 6 } 32 if nx_ont_t_write(fd, "sssp_algorithm\tsubclass_of\tgraph_algorithm\n" as *u8) != 0 { return 7 } 33 if nx_ont_t_write(fd, "fft\tin_domain\tnumerical_analysis\n" as *u8) != 0 { return 8 } 34 if nx_ont_t_write(fd, "\n" as *u8) != 0 { return 9 } 35 36 sys_close(fd) 37 return 0 38} 39 40func main() -> nx_exit { 41 let path: *u8 = "/tmp/nx_ontology_t1.txt" as *u8 42 let prep: nx_int = nx_ont_t_fixture(path) 43 if prep != 0 { return 10 + prep } 44 45 let o: *NxOntology = nx_ontology_load(path) 46 if (o as nx_size) == 0 { return 20 } 47 if nx_ontology_n_triples(o) != 6 { return 21 } 48 49 // count_by_predicate 50 if nx_ontology_count_by_predicate(o, "is_a" as *u8) != 2 { return 30 } 51 if nx_ontology_count_by_predicate(o, "in_domain" as *u8) != 3 { return 31 } 52 if nx_ontology_count_by_predicate(o, "subclass_of" as *u8) != 1 { return 32 } 53 if nx_ontology_count_by_predicate(o, "nonexistent" as *u8) != 0 { return 33 } 54 55 // count_with_po 56 if nx_ontology_count_with_po(o, "in_domain" as *u8, "competitive_programming" as *u8) != 2 { return 40 } 57 if nx_ontology_count_with_po(o, "in_domain" as *u8, "numerical_analysis" as *u8) != 1 { return 41 } 58 if nx_ontology_count_with_po(o, "in_domain" as *u8, "missing" as *u8) != 0 { return 42 } 59 60 // exact triple lookup 61 if nx_ontology_has_triple(o, "quicksort" as *u8, "is_a" as *u8, "sorting_algorithm" as *u8) != 1 { return 50 } 62 if nx_ontology_has_triple(o, "quicksort" as *u8, "is_a" as *u8, "graph_algorithm" as *u8) != 0 { return 51 } 63 if nx_ontology_has_triple(o, "missing" as *u8, "is_a" as *u8, "sorting_algorithm" as *u8) != 0 { return 52 } 64 65 // domain shortcut 66 if nx_ontology_in_domain(o, "quicksort" as *u8, "competitive_programming" as *u8) != 1 { return 60 } 67 if nx_ontology_in_domain(o, "quicksort" as *u8, "machine_learning" as *u8) != 0 { return 61 } 68 if nx_ontology_in_domain(o, "fft" as *u8, "numerical_analysis" as *u8) != 1 { return 62 } 69 70 return 0 71}