@ontology-kit/okb 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +26 -2
- package/bin/okb.js +8 -0
- package/dist/checks.d.ts +33 -0
- package/dist/checks.js +966 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +718 -0
- package/dist/clidocs.d.ts +6 -0
- package/dist/clidocs.js +90 -0
- package/dist/clock.d.ts +2 -0
- package/dist/clock.js +16 -0
- package/dist/commands.d.ts +28 -0
- package/dist/commands.js +622 -0
- package/dist/export.d.ts +54 -0
- package/dist/export.js +290 -0
- package/dist/families.d.ts +20 -0
- package/dist/families.js +51 -0
- package/dist/help.d.ts +2 -0
- package/dist/help.js +57 -0
- package/dist/markdown.d.ts +60 -0
- package/dist/markdown.js +310 -0
- package/dist/metakb.d.ts +164 -0
- package/dist/metakb.js +130 -0
- package/dist/naming.d.ts +20 -0
- package/dist/naming.js +140 -0
- package/dist/ops/admin.d.ts +16 -0
- package/dist/ops/admin.js +119 -0
- package/dist/ops/classes.d.ts +21 -0
- package/dist/ops/classes.js +105 -0
- package/dist/ops/common.d.ts +6 -0
- package/dist/ops/common.js +4 -0
- package/dist/ops/instances.d.ts +8 -0
- package/dist/ops/instances.js +70 -0
- package/dist/ops/links.d.ts +9 -0
- package/dist/ops/links.js +85 -0
- package/dist/ops/naming.d.ts +4 -0
- package/dist/ops/naming.js +70 -0
- package/dist/ops/scope.d.ts +29 -0
- package/dist/ops/scope.js +124 -0
- package/dist/ops/slots.d.ts +31 -0
- package/dist/ops/slots.js +186 -0
- package/dist/ops/values.d.ts +20 -0
- package/dist/ops/values.js +151 -0
- package/dist/ops.d.ts +14 -0
- package/dist/ops.js +14 -0
- package/dist/provenance.d.ts +53 -0
- package/dist/provenance.js +296 -0
- package/dist/quotes.d.ts +14 -0
- package/dist/quotes.js +104 -0
- package/dist/render.d.ts +30 -0
- package/dist/render.js +413 -0
- package/dist/review.d.ts +10 -0
- package/dist/review.js +145 -0
- package/dist/site.d.ts +6 -0
- package/dist/site.js +6 -0
- package/dist/skills.d.ts +52 -0
- package/dist/skills.js +120 -0
- package/dist/status.d.ts +18 -0
- package/dist/status.js +59 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +5 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +7 -0
- package/meta-kb/data/edges.json +3172 -0
- package/meta-kb/data/manifest.json +19 -0
- package/meta-kb/data/nodes.json +3546 -0
- package/package.json +71 -3
- package/skills/build-domain-kb-from-docs/SKILL.md +87 -0
- package/skills/ontology-coach/SKILL.md +100 -0
- package/skills/ontology-review/SKILL.md +66 -0
package/dist/export.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `okb export`: writes the ontology's DATA the way a graph database expects it.
|
|
3
|
+
*
|
|
4
|
+
* In the okb file, classes are nodes (so the schema itself can be queried and
|
|
5
|
+
* validated). In a graph database you usually want the opposite: each instance is a
|
|
6
|
+
* node labeled with its class and every ancestor class (labels are flat, so
|
|
7
|
+
* multiple labels are how is-a is expressed), property values are node properties,
|
|
8
|
+
* and relationships are typed edges.
|
|
9
|
+
*
|
|
10
|
+
* (:Beaujolais:RedWine:Wine {name: "Chateau Morgon Beaujolais", body: "light", color: "red"})
|
|
11
|
+
* -[:MAKER]->(:Winery {name: "Chateau Morgon"})
|
|
12
|
+
*
|
|
13
|
+
* Domain rules come along by default, so whatever reads the export gets the guidance
|
|
14
|
+
* with the data: (:OkbRule) nodes that GOVERN what they're about, and CITE the quote
|
|
15
|
+
* (:OkbSourceLocation) that's PART_OF its document (:OkbSource). Only verified rules
|
|
16
|
+
* are exported: a rule nobody has checked against its quote shouldn't reach an app
|
|
17
|
+
* as if it were fact. Links whose `rule` property points at a held-back rule are held
|
|
18
|
+
* back too, since that rule is their only justification.
|
|
19
|
+
*
|
|
20
|
+
* Options:
|
|
21
|
+
* inherited (default true) copy fixed property values from classes onto their instances
|
|
22
|
+
* schema also export the class graph: (:OkbClass) nodes, IS_A, and INSTANCE_OF from each instance
|
|
23
|
+
* rules (default true) export verified domain rules with their quotes and sources
|
|
24
|
+
* unverified also export rules that aren't verified yet, each with its verification status
|
|
25
|
+
*/
|
|
26
|
+
import { type Ontology } from "@ontology-kit/format";
|
|
27
|
+
export interface ExportNode {
|
|
28
|
+
id: string;
|
|
29
|
+
labels: string[];
|
|
30
|
+
properties: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
export interface ExportRel {
|
|
33
|
+
type: string;
|
|
34
|
+
from: string;
|
|
35
|
+
to: string;
|
|
36
|
+
properties: Record<string, unknown>;
|
|
37
|
+
}
|
|
38
|
+
export interface ExportGraph {
|
|
39
|
+
format: "okb-export/2";
|
|
40
|
+
ontology: string;
|
|
41
|
+
metaKbVersion: string;
|
|
42
|
+
nodes: ExportNode[];
|
|
43
|
+
relationships: ExportRel[];
|
|
44
|
+
notes: string[];
|
|
45
|
+
}
|
|
46
|
+
export interface ExportOptions {
|
|
47
|
+
inherited?: boolean;
|
|
48
|
+
schema?: boolean;
|
|
49
|
+
rules?: boolean;
|
|
50
|
+
unverified?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export declare function exportGraph(ont: Ontology, opts?: ExportOptions): ExportGraph;
|
|
53
|
+
/** openCypher script (Neo4j, Memgraph, Amazon Neptune). Run against an empty database, or delete the previous import first. */
|
|
54
|
+
export declare function toCypher(g: ExportGraph): string;
|
package/dist/export.js
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `okb export`: writes the ontology's DATA the way a graph database expects it.
|
|
3
|
+
*
|
|
4
|
+
* In the okb file, classes are nodes (so the schema itself can be queried and
|
|
5
|
+
* validated). In a graph database you usually want the opposite: each instance is a
|
|
6
|
+
* node labeled with its class and every ancestor class (labels are flat, so
|
|
7
|
+
* multiple labels are how is-a is expressed), property values are node properties,
|
|
8
|
+
* and relationships are typed edges.
|
|
9
|
+
*
|
|
10
|
+
* (:Beaujolais:RedWine:Wine {name: "Chateau Morgon Beaujolais", body: "light", color: "red"})
|
|
11
|
+
* -[:MAKER]->(:Winery {name: "Chateau Morgon"})
|
|
12
|
+
*
|
|
13
|
+
* Domain rules come along by default, so whatever reads the export gets the guidance
|
|
14
|
+
* with the data: (:OkbRule) nodes that GOVERN what they're about, and CITE the quote
|
|
15
|
+
* (:OkbSourceLocation) that's PART_OF its document (:OkbSource). Only verified rules
|
|
16
|
+
* are exported: a rule nobody has checked against its quote shouldn't reach an app
|
|
17
|
+
* as if it were fact. Links whose `rule` property points at a held-back rule are held
|
|
18
|
+
* back too, since that rule is their only justification.
|
|
19
|
+
*
|
|
20
|
+
* Options:
|
|
21
|
+
* inherited (default true) copy fixed property values from classes onto their instances
|
|
22
|
+
* schema also export the class graph: (:OkbClass) nodes, IS_A, and INSTANCE_OF from each instance
|
|
23
|
+
* rules (default true) export verified domain rules with their quotes and sources
|
|
24
|
+
* unverified also export rules that aren't verified yet, each with its verification status
|
|
25
|
+
*/
|
|
26
|
+
import { edgeProps } from "@ontology-kit/format";
|
|
27
|
+
import { MetaKB } from "./metakb.js";
|
|
28
|
+
export function exportGraph(ont, opts = {}) {
|
|
29
|
+
const inherited = opts.inherited ?? true;
|
|
30
|
+
const withRules = opts.rules ?? true;
|
|
31
|
+
const nodes = [];
|
|
32
|
+
const rels = [];
|
|
33
|
+
const notes = [];
|
|
34
|
+
const inheritedRels = [];
|
|
35
|
+
// Most specific class first, then ancestors (nearest first).
|
|
36
|
+
const labelsFor = (iid) => {
|
|
37
|
+
const out = [];
|
|
38
|
+
let frontier = ont.targets(iid, "INSTANCE_OF");
|
|
39
|
+
while (frontier.length) {
|
|
40
|
+
const next = [];
|
|
41
|
+
for (const c of frontier) {
|
|
42
|
+
const name = ont.label(c);
|
|
43
|
+
if (!out.includes(name))
|
|
44
|
+
out.push(name);
|
|
45
|
+
next.push(...ont.parents(c));
|
|
46
|
+
}
|
|
47
|
+
frontier = next;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
};
|
|
51
|
+
for (const inst of ont.ofType("Instance")) {
|
|
52
|
+
const properties = { okbId: inst.id, name: inst.name };
|
|
53
|
+
if (inst.description)
|
|
54
|
+
properties.description = inst.description;
|
|
55
|
+
const ctx = ont.instanceClasses(inst.id);
|
|
56
|
+
for (const sid of ont.applicableSlots(inst.id)) {
|
|
57
|
+
const s = ont.get(sid);
|
|
58
|
+
if (s?.type !== "Slot")
|
|
59
|
+
continue; // a HAS_SLOT to a non-slot: struct-well-formed reports it
|
|
60
|
+
if (s.valueType === "Instance") {
|
|
61
|
+
// A class-level link to an instance (e.g. every Merlot has grape = Merlot grape) is inherited by each member.
|
|
62
|
+
if (inherited && ont.rel.values(inst.id, sid).length === 0) {
|
|
63
|
+
const fixed = ont.inheritedFixed(sid, ctx);
|
|
64
|
+
const spec = ont.rel.spec(sid);
|
|
65
|
+
for (const t of fixed?.values ?? []) {
|
|
66
|
+
if (ont.get(String(t))?.type !== "Instance" || !spec)
|
|
67
|
+
continue;
|
|
68
|
+
const [from, to] = spec.reverse ? [String(t), inst.id] : [inst.id, String(t)];
|
|
69
|
+
const classEdge = ont.rel.edge(fixed.cls, sid, String(t));
|
|
70
|
+
inheritedRels.push({ type: spec.type, from, to, properties: { ...(classEdge ? edgeProps(classEdge) : {}), inheritedFrom: ont.label(fixed.cls) } });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
let v = inst.values?.[sid];
|
|
76
|
+
if (v === undefined && inherited) {
|
|
77
|
+
const fixed = ont.inheritedFixed(sid, ctx);
|
|
78
|
+
if (fixed)
|
|
79
|
+
v = s.cardinality === "multiple" ? fixed.values : fixed.values[0];
|
|
80
|
+
}
|
|
81
|
+
if (v !== undefined)
|
|
82
|
+
properties[s.name] = v;
|
|
83
|
+
}
|
|
84
|
+
nodes.push({ id: inst.id, labels: labelsFor(inst.id), properties });
|
|
85
|
+
}
|
|
86
|
+
rels.push(...inheritedRels);
|
|
87
|
+
const relTypes = ont.rel.edgeTypes();
|
|
88
|
+
let classLevel = 0;
|
|
89
|
+
for (const e of ont.edges) {
|
|
90
|
+
if (!relTypes.has(e.type))
|
|
91
|
+
continue;
|
|
92
|
+
const a = ont.get(e.from);
|
|
93
|
+
const b = ont.get(e.to);
|
|
94
|
+
if (a?.type === "Instance" && b?.type === "Instance")
|
|
95
|
+
rels.push({ type: e.type, from: e.from, to: e.to, properties: edgeProps(e) });
|
|
96
|
+
else if (b?.type === "Class")
|
|
97
|
+
classLevel++;
|
|
98
|
+
}
|
|
99
|
+
if (opts.schema) {
|
|
100
|
+
for (const c of ont.ofType("Class")) {
|
|
101
|
+
nodes.push({
|
|
102
|
+
id: c.id, labels: ["OkbClass"],
|
|
103
|
+
properties: { okbId: c.id, name: c.name, ...(c.description ? { description: c.description } : {}), ...(c.abstract ? { abstract: true } : {}) },
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
for (const e of ont.edgesOf("IS_A"))
|
|
107
|
+
rels.push({ type: "IS_A", from: e.from, to: e.to, properties: {} });
|
|
108
|
+
for (const e of ont.edgesOf("INSTANCE_OF"))
|
|
109
|
+
rels.push({ type: "INSTANCE_OF", from: e.from, to: e.to, properties: {} });
|
|
110
|
+
for (const e of ont.edges) {
|
|
111
|
+
if (relTypes.has(e.type) && ont.get(e.from)?.type === "Class")
|
|
112
|
+
rels.push({ type: e.type, from: e.from, to: e.to, properties: { ...edgeProps(e), classLevel: true } });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else if (classLevel) {
|
|
116
|
+
notes.push(`${classLevel} class-level relationship value(s) point at classes (e.g. RedWine GOES_WELL_WITH RedMeat), not instances, so they're only exported with --with-schema.`);
|
|
117
|
+
}
|
|
118
|
+
if (withRules)
|
|
119
|
+
exportRules(ont, { schema: !!opts.schema, unverified: !!opts.unverified }, nodes, rels, notes);
|
|
120
|
+
const kept = settleRuleReferences(ont, rels, new Set(nodes.filter((n) => n.labels.includes("OkbRule")).map((n) => n.id)), { withRules, unverified: !!opts.unverified }, notes);
|
|
121
|
+
const inverses = ont.edgesOf("INVERSE_OF").map((e) => {
|
|
122
|
+
const from = ont.get(e.from);
|
|
123
|
+
return `${ont.label(e.to)} = ${from?.type === "Slot" ? from.relType : undefined} followed backwards`;
|
|
124
|
+
});
|
|
125
|
+
if (inverses.length)
|
|
126
|
+
notes.push(`Inverse relationships are stored once: ${inverses.join("; ")}.`);
|
|
127
|
+
return { format: "okb-export/2", ontology: ont.meta.name, metaKbVersion: MetaKB.get().version, nodes, relationships: kept, notes };
|
|
128
|
+
}
|
|
129
|
+
// Rules
|
|
130
|
+
const isVerified = (r) => r.verification?.status === "SUPPORTED";
|
|
131
|
+
/** (:OkbRule)-[:CITES]->(:OkbSourceLocation)-[:PART_OF]->(:OkbSource), plus GOVERNS. */
|
|
132
|
+
function exportRules(ont, o, nodes, rels, notes) {
|
|
133
|
+
// Rules can share a quote, and quotes share a source: add each node and PART_OF edge once.
|
|
134
|
+
const seen = new Set();
|
|
135
|
+
const push = (n) => {
|
|
136
|
+
if (seen.has(n.id))
|
|
137
|
+
return false;
|
|
138
|
+
seen.add(n.id);
|
|
139
|
+
nodes.push(n);
|
|
140
|
+
return true;
|
|
141
|
+
};
|
|
142
|
+
let held = 0;
|
|
143
|
+
const ruleIds = new Set();
|
|
144
|
+
for (const r of ont.ofType("Rule")) {
|
|
145
|
+
if (!isVerified(r) && !o.unverified) {
|
|
146
|
+
held++;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
// Names, so an app can match a rule to the labels on instance nodes even without --with-schema.
|
|
150
|
+
const governs = { governsClasses: [], governsSlots: [], governsInstances: [] };
|
|
151
|
+
for (const t of ont.targets(r.id, "GOVERNS")) {
|
|
152
|
+
const n = ont.get(t);
|
|
153
|
+
if (n?.type === "Class") {
|
|
154
|
+
governs.governsClasses.push(n.name);
|
|
155
|
+
if (o.schema)
|
|
156
|
+
rels.push({ type: "GOVERNS", from: r.id, to: n.id, properties: {} });
|
|
157
|
+
}
|
|
158
|
+
else if (n?.type === "Slot")
|
|
159
|
+
governs.governsSlots.push(n.name);
|
|
160
|
+
else if (n?.type === "Instance") {
|
|
161
|
+
governs.governsInstances.push(n.name);
|
|
162
|
+
rels.push({ type: "GOVERNS", from: r.id, to: n.id, properties: {} });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const fixed = {
|
|
166
|
+
okbId: r.id, name: r.name, statement: r.statement, modality: r.modality,
|
|
167
|
+
authored: r.authored === true,
|
|
168
|
+
verification: r.verification?.status ?? "UNVERIFIED",
|
|
169
|
+
approvedAfter: r.verification?.approvedAfter,
|
|
170
|
+
extractionConfidence: r.extractionConfidence,
|
|
171
|
+
...governs,
|
|
172
|
+
};
|
|
173
|
+
const qualifiers = ruleQualifiers(ont, r);
|
|
174
|
+
const clash = Object.keys(qualifiers).filter((k) => k in fixed);
|
|
175
|
+
if (clash.length)
|
|
176
|
+
notes.push(`${r.id}: qualifier(s) ${clash.join(", ")} share a name with a fixed OkbRule property, so the fixed one is exported. Rename the slot.`);
|
|
177
|
+
push({ id: r.id, labels: ["OkbRule"], properties: compact({ ...qualifiers, ...fixed }) });
|
|
178
|
+
ruleIds.add(r.id);
|
|
179
|
+
for (const lid of ont.targets(r.id, "CITES")) {
|
|
180
|
+
const loc = ont.get(lid);
|
|
181
|
+
if (loc?.type !== "SourceLocation")
|
|
182
|
+
continue;
|
|
183
|
+
rels.push({ type: "CITES", from: r.id, to: loc.id, properties: {} });
|
|
184
|
+
const added = push({
|
|
185
|
+
id: loc.id,
|
|
186
|
+
labels: ["OkbSourceLocation"],
|
|
187
|
+
properties: compact({ okbId: loc.id, quote: loc.quote, locator: loc.locator, lines: loc.lines, page: loc.page, sourceLink: loc.sourceLink, pageLink: loc.pageLink }),
|
|
188
|
+
});
|
|
189
|
+
if (!added)
|
|
190
|
+
continue;
|
|
191
|
+
for (const sid of ont.targets(loc.id, "PART_OF")) {
|
|
192
|
+
const src = ont.get(sid);
|
|
193
|
+
if (src?.type !== "Source")
|
|
194
|
+
continue;
|
|
195
|
+
push({ id: src.id, labels: ["OkbSource"], properties: compact({ okbId: src.id, title: src.title, format: src.format, url: src.url, repoUrl: src.repoUrl }) });
|
|
196
|
+
rels.push({ type: "PART_OF", from: loc.id, to: src.id, properties: {} });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
// Links between exported rules, and from the descriptive facts that fulfill them.
|
|
201
|
+
for (const e of ont.edgesOf("REFINES"))
|
|
202
|
+
if (ruleIds.has(e.from) && ruleIds.has(e.to))
|
|
203
|
+
rels.push({ type: "REFINES", from: e.from, to: e.to, properties: compact({ adds: e.adds }) });
|
|
204
|
+
for (const e of ont.edgesOf("FULFILLS"))
|
|
205
|
+
if (ruleIds.has(e.to))
|
|
206
|
+
rels.push({ type: "FULFILLS", from: e.from, to: e.to, properties: {} });
|
|
207
|
+
if (held)
|
|
208
|
+
notes.push(`${held} rule(s) haven't passed verification, so they're left out (okb validate lists them). --include-unverified exports them with their status.`);
|
|
209
|
+
}
|
|
210
|
+
/** A rule's qualifier values (slots declared with --on-rules), keyed by slot name. */
|
|
211
|
+
function ruleQualifiers(ont, r) {
|
|
212
|
+
const out = {};
|
|
213
|
+
for (const sid of ont.applicableSlots(r.id)) {
|
|
214
|
+
const s = ont.get(sid);
|
|
215
|
+
if (s?.type === "Slot" && r.values?.[sid] !== undefined)
|
|
216
|
+
out[s.name] = r.values[sid];
|
|
217
|
+
}
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Every exported `rule` property must point at an exported OkbRule node. A link that
|
|
222
|
+
* cites a held-back (unverified) rule is dropped with it; with --no-rules the property is
|
|
223
|
+
* removed, since the id would point at nothing.
|
|
224
|
+
*/
|
|
225
|
+
function settleRuleReferences(ont, rels, exportedRules, o, notes) {
|
|
226
|
+
let dropped = 0;
|
|
227
|
+
let stripped = 0;
|
|
228
|
+
const out = [];
|
|
229
|
+
for (const r of rels) {
|
|
230
|
+
const ref = r.properties.rule;
|
|
231
|
+
if (ref === undefined || (typeof ref === "string" && exportedRules.has(ref))) {
|
|
232
|
+
out.push(r);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const rule = typeof ref === "string" ? ont.get(ref) : undefined;
|
|
236
|
+
if (rule?.type === "Rule" && !isVerified(rule) && !o.unverified) {
|
|
237
|
+
dropped++;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const { rule: _r, ...rest } = r.properties;
|
|
241
|
+
out.push({ ...r, properties: rest });
|
|
242
|
+
stripped++;
|
|
243
|
+
}
|
|
244
|
+
if (dropped)
|
|
245
|
+
notes.push(`${dropped} link(s) cite a rule that hasn't passed verification, so they're left out too.`);
|
|
246
|
+
if (stripped) {
|
|
247
|
+
notes.push(o.withRules
|
|
248
|
+
? `${stripped} link(s) had a rule property that doesn't point at a Rule node (okb validate reports it as slot-edge-properties), so the property was removed.`
|
|
249
|
+
: `Rules weren't exported (--no-rules), so the rule property was removed from ${stripped} link(s).`);
|
|
250
|
+
}
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
253
|
+
function compact(o) {
|
|
254
|
+
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined && !(Array.isArray(v) && v.length === 0)));
|
|
255
|
+
}
|
|
256
|
+
// Cypher
|
|
257
|
+
const ident = (s) => (/^[A-Za-z_][A-Za-z0-9_]*$/.test(s) ? s : "`" + s.replace(/`/g, "``") + "`");
|
|
258
|
+
function literal(v) {
|
|
259
|
+
if (v === null || v === undefined)
|
|
260
|
+
return "null";
|
|
261
|
+
if (Array.isArray(v))
|
|
262
|
+
return "[" + v.map(literal).join(", ") + "]";
|
|
263
|
+
if (typeof v === "string")
|
|
264
|
+
return JSON.stringify(v);
|
|
265
|
+
if (typeof v === "number" || typeof v === "boolean")
|
|
266
|
+
return String(v);
|
|
267
|
+
return JSON.stringify(JSON.stringify(v));
|
|
268
|
+
}
|
|
269
|
+
function mapLiteral(props) {
|
|
270
|
+
return "{" + Object.entries(props).map(([k, v]) => `${ident(k)}: ${literal(v)}`).join(", ") + "}";
|
|
271
|
+
}
|
|
272
|
+
/** openCypher script (Neo4j, Memgraph, Amazon Neptune). Run against an empty database, or delete the previous import first. */
|
|
273
|
+
export function toCypher(g) {
|
|
274
|
+
const out = [
|
|
275
|
+
`// ${g.ontology}: exported by okb (meta-KB ${g.metaKbVersion})`,
|
|
276
|
+
"// Every node carries okbId. To re-import, first run: MATCH (n) WHERE n.okbId IS NOT NULL DETACH DELETE n;",
|
|
277
|
+
...g.notes.map((n) => `// ${n}`),
|
|
278
|
+
"",
|
|
279
|
+
];
|
|
280
|
+
for (const n of g.nodes) {
|
|
281
|
+
const labels = n.labels.length ? ":" + n.labels.map(ident).join(":") : "";
|
|
282
|
+
out.push(`CREATE (${labels} ${mapLiteral(n.properties)});`);
|
|
283
|
+
}
|
|
284
|
+
out.push("");
|
|
285
|
+
for (const r of g.relationships) {
|
|
286
|
+
const props = Object.keys(r.properties).length ? " " + mapLiteral(r.properties) : "";
|
|
287
|
+
out.push(`MATCH (a {okbId: ${literal(r.from)}}), (b {okbId: ${literal(r.to)}}) CREATE (a)-[:${ident(r.type)}${props}]->(b);`);
|
|
288
|
+
}
|
|
289
|
+
return out.join("\n") + "\n";
|
|
290
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A competency question is one example of a type of question (Ontology 101 calls
|
|
3
|
+
* them "just a sketch"). This finds its family in the current ontology, for the
|
|
4
|
+
* `okb cq link` hint and the scope-cq-families review:
|
|
5
|
+
* - the same question about similar things: sibling classes, subclasses, or
|
|
6
|
+
* other instances of the same class
|
|
7
|
+
* - more questions about the same subject: its slots that no question uses yet
|
|
8
|
+
* Things another question already needs are left out (they're covered), and each
|
|
9
|
+
* suggestion appears once, under the first subject it came up for.
|
|
10
|
+
*/
|
|
11
|
+
import { type Ontology } from "@ontology-kit/format";
|
|
12
|
+
export interface QuestionFamily {
|
|
13
|
+
/** What the question is about (a class, instance, or a slot's class). */
|
|
14
|
+
subject: string;
|
|
15
|
+
/** Similar things the same question could be asked about. */
|
|
16
|
+
sameQuestionAbout: string[];
|
|
17
|
+
/** The subject's slots no question uses yet. */
|
|
18
|
+
moreAbout: string[];
|
|
19
|
+
}
|
|
20
|
+
export declare function questionFamilies(ont: Ontology, cqId: string): QuestionFamily[];
|
package/dist/families.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A competency question is one example of a type of question (Ontology 101 calls
|
|
3
|
+
* them "just a sketch"). This finds its family in the current ontology, for the
|
|
4
|
+
* `okb cq link` hint and the scope-cq-families review:
|
|
5
|
+
* - the same question about similar things: sibling classes, subclasses, or
|
|
6
|
+
* other instances of the same class
|
|
7
|
+
* - more questions about the same subject: its slots that no question uses yet
|
|
8
|
+
* Things another question already needs are left out (they're covered), and each
|
|
9
|
+
* suggestion appears once, under the first subject it came up for.
|
|
10
|
+
*/
|
|
11
|
+
import {} from "@ontology-kit/format";
|
|
12
|
+
/** At most this many suggestions per list, so a hint stays one line. */
|
|
13
|
+
const MAX_SUGGESTIONS = 4;
|
|
14
|
+
export function questionFamilies(ont, cqId) {
|
|
15
|
+
const needed = new Set(ont.edgesOf("NEEDS").map((e) => e.to));
|
|
16
|
+
const suggested = new Set();
|
|
17
|
+
const fresh = (ids) => {
|
|
18
|
+
const out = [...new Set(ids)].filter((id) => !needed.has(id) && !suggested.has(id));
|
|
19
|
+
for (const id of out)
|
|
20
|
+
suggested.add(id);
|
|
21
|
+
return out;
|
|
22
|
+
};
|
|
23
|
+
const labels = (ids) => ids.slice(0, MAX_SUGGESTIONS).map((id) => ont.label(id));
|
|
24
|
+
const families = [];
|
|
25
|
+
const seen = new Set();
|
|
26
|
+
for (const id of ont.targets(cqId, "NEEDS")) {
|
|
27
|
+
const n = ont.get(id);
|
|
28
|
+
if (!n)
|
|
29
|
+
continue;
|
|
30
|
+
let subject = id;
|
|
31
|
+
let same = [];
|
|
32
|
+
if (n.type === "Class") {
|
|
33
|
+
const siblings = ont.parents(id).flatMap((p) => ont.children(p)).filter((c) => c !== id);
|
|
34
|
+
same = fresh([...siblings, ...ont.children(id)]);
|
|
35
|
+
}
|
|
36
|
+
else if (n.type === "Instance") {
|
|
37
|
+
same = fresh(ont.targets(id, "INSTANCE_OF").flatMap((c) => ont.sources(c, "INSTANCE_OF")).filter((i) => i !== id));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// A slot: the question is about the class that has it.
|
|
41
|
+
subject = ont.domain(id)[0] ?? id;
|
|
42
|
+
}
|
|
43
|
+
if (seen.has(subject))
|
|
44
|
+
continue;
|
|
45
|
+
seen.add(subject);
|
|
46
|
+
const more = fresh(ont.get(subject)?.type === "Slot" ? [] : ont.applicableSlots(subject));
|
|
47
|
+
if (same.length || more.length)
|
|
48
|
+
families.push({ subject: ont.label(subject), sameQuestionAbout: labels(same), moreAbout: labels(more) });
|
|
49
|
+
}
|
|
50
|
+
return families;
|
|
51
|
+
}
|
package/dist/help.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** The text of `okb help`. Its own module so the docs generator can read it without running the CLI. */
|
|
2
|
+
export declare const HELP = "okb: build an ontology step by step (Ontology 101 method)\n\nGetting started\n okb init <folder> --name \"Wine and Food\" create a new ontology\n okb status where you are and what to do next\n okb step [n|next|prev] move between steps and show the guide\n okb explain [topic] learn a concept, rule, step (e.g. okb explain 5) or decision guide\n okb validate [--all] [--why] [--json] check your ontology against the rules\n\nStep 1: scope okb scope --domain \"...\" --purpose \"...\" --users a,b --maintainers x --out-of-scope \"...\"\n okb cq add \"Which wines go with fish?\" \u00B7 okb cq list\nStep 2: reuse okb reuse add --name \"...\" --url ... --decision reuse|adapt|reference|rejected\n okb reuse none --why \"...\"\nStep 3: terms okb term add wine grape winery color ... \u00B7 okb term list\nStep 4: classes okb convention [--class-case PascalCase] [--slot-case camelCase] [--number singular]\n okb class add RedWine --parent Wine [--desc \"...\"] [--synonym ...] [--abstract]\n okb class set RedWine [--desc] [--add-parent X] [--remove-parent X] [--synonym S]\n okb class disjoint RedWine WhiteWine RoseWine\n okb term set wine --as class|slot|instance|value|synonym|out-of-scope [--node Name]\n okb tree [--instances]\nStep 5: slots Two kinds (Ontology 101 calls both \"slots\"):\n okb property add body --on Wine --type Enumerated --values light,medium,full --card single\n okb relationship add maker --from Wine --to Winery --card single (stored as edges :MAKER)\n okb slot set body [--on X] [--off X] [--desc] ... \u00B7 okb show Wine\nStep 6: facets okb slot set maker --card single --min 1 \u00B7 okb slot set producer --range Winery\n okb relationship inverse maker produces (one edge, readable from both ends)\n okb relationship property preferredOver condition --type String [--required] [--values a,b]\n okb link Radio preferredOver SegmentedControl condition=\"2 to 7 options\" [--rule <rule id>]\n okb unlink Radio preferredOver SegmentedControl\n okb class fix DessertWine sugar=sweet (a value every member has)\n okb class default Wine body=full (a starting value, overridable)\n okb class restrict SingleVarietal grape --max 1\nStep 7: instances okb instance add \"Chateau Morgon Beaujolais\" --of Beaujolais body=light maker=\"Chateau Morgon\"\n okb instance set \"Chateau Morgon Beaujolais\" grape+=Gamay (+= adds, -= removes, slot= clears)\n okb instance set \"Chateau Morgon Beaujolais\" --of Beaujolais (move to another class)\nStep 8: review okb cq link 1 Wine Food goesWellWith \u00B7 okb validate --all \u00B7 okb review\n okb decision add --title \"...\" --decision \"...\" --why \"...\" [--about X] [--waives rule-id]\n okb diagram [--out diagram.md]\nExport okb export [--format json|cypher] [--with-schema] [--no-inherited] [--no-rules] [--include-unverified] [--out file]\n (instances as nodes labeled with their classes, ready for Neo4j / Neptune / Memgraph)\n\nDocuments okb source add docs/button.md [--repo-url https://github.com/.../button.md] [--url https://site/button]\n(extraction) okb source outline <source> [--quotable] [--json] every block, with heading path and lines\n okb quote add <source> \"verbatim text\" [--cites Node] [--line N] located automatically\n okb rule add --statement \"...\" --modality SHOULD --governs Button --cites <quote id> [--authored] [--value slot=value] [--refines <rule> --adds \"...\"]\n okb rule refines <rule> <broader rule> [--adds \"...\"] okb fulfills <instance> <rule>\n okb cite <node> <quote id> attach an existing quote to a node\n okb verify <node> --status SUPPORTED|OVERREACH|UNSUPPORTED [--corrected \"...\"] \u00B7 okb verify <node> --approve\n okb source refresh <source> after the document changes\n\nSkills okb skills install [--copy] [--dir <folder>] [--force] link (or copy) the packaged skills into ~/.claude/skills\n okb skills status [--dir <folder>] which are installed, where, and whether they match this okb\n\nAlso: okb show <name> \u00B7 okb rename <old> <new> \u00B7 okb remove <name> \u00B7 okb drift <folder>...\nNames: when a class and a slot share a name, say which: class:Genre / slot:genre\nGlobal options: -C <folder> (default: current folder) \u00B7 --json (machine-readable output where supported)";
|
package/dist/help.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** The text of `okb help`. Its own module so the docs generator can read it without running the CLI. */
|
|
2
|
+
export const HELP = `okb: build an ontology step by step (Ontology 101 method)
|
|
3
|
+
|
|
4
|
+
Getting started
|
|
5
|
+
okb init <folder> --name "Wine and Food" create a new ontology
|
|
6
|
+
okb status where you are and what to do next
|
|
7
|
+
okb step [n|next|prev] move between steps and show the guide
|
|
8
|
+
okb explain [topic] learn a concept, rule, step (e.g. okb explain 5) or decision guide
|
|
9
|
+
okb validate [--all] [--why] [--json] check your ontology against the rules
|
|
10
|
+
|
|
11
|
+
Step 1: scope okb scope --domain "..." --purpose "..." --users a,b --maintainers x --out-of-scope "..."
|
|
12
|
+
okb cq add "Which wines go with fish?" · okb cq list
|
|
13
|
+
Step 2: reuse okb reuse add --name "..." --url ... --decision reuse|adapt|reference|rejected
|
|
14
|
+
okb reuse none --why "..."
|
|
15
|
+
Step 3: terms okb term add wine grape winery color ... · okb term list
|
|
16
|
+
Step 4: classes okb convention [--class-case PascalCase] [--slot-case camelCase] [--number singular]
|
|
17
|
+
okb class add RedWine --parent Wine [--desc "..."] [--synonym ...] [--abstract]
|
|
18
|
+
okb class set RedWine [--desc] [--add-parent X] [--remove-parent X] [--synonym S]
|
|
19
|
+
okb class disjoint RedWine WhiteWine RoseWine
|
|
20
|
+
okb term set wine --as class|slot|instance|value|synonym|out-of-scope [--node Name]
|
|
21
|
+
okb tree [--instances]
|
|
22
|
+
Step 5: slots Two kinds (Ontology 101 calls both "slots"):
|
|
23
|
+
okb property add body --on Wine --type Enumerated --values light,medium,full --card single
|
|
24
|
+
okb relationship add maker --from Wine --to Winery --card single (stored as edges :MAKER)
|
|
25
|
+
okb slot set body [--on X] [--off X] [--desc] ... · okb show Wine
|
|
26
|
+
Step 6: facets okb slot set maker --card single --min 1 · okb slot set producer --range Winery
|
|
27
|
+
okb relationship inverse maker produces (one edge, readable from both ends)
|
|
28
|
+
okb relationship property preferredOver condition --type String [--required] [--values a,b]
|
|
29
|
+
okb link Radio preferredOver SegmentedControl condition="2 to 7 options" [--rule <rule id>]
|
|
30
|
+
okb unlink Radio preferredOver SegmentedControl
|
|
31
|
+
okb class fix DessertWine sugar=sweet (a value every member has)
|
|
32
|
+
okb class default Wine body=full (a starting value, overridable)
|
|
33
|
+
okb class restrict SingleVarietal grape --max 1
|
|
34
|
+
Step 7: instances okb instance add "Chateau Morgon Beaujolais" --of Beaujolais body=light maker="Chateau Morgon"
|
|
35
|
+
okb instance set "Chateau Morgon Beaujolais" grape+=Gamay (+= adds, -= removes, slot= clears)
|
|
36
|
+
okb instance set "Chateau Morgon Beaujolais" --of Beaujolais (move to another class)
|
|
37
|
+
Step 8: review okb cq link 1 Wine Food goesWellWith · okb validate --all · okb review
|
|
38
|
+
okb decision add --title "..." --decision "..." --why "..." [--about X] [--waives rule-id]
|
|
39
|
+
okb diagram [--out diagram.md]
|
|
40
|
+
Export okb export [--format json|cypher] [--with-schema] [--no-inherited] [--no-rules] [--include-unverified] [--out file]
|
|
41
|
+
(instances as nodes labeled with their classes, ready for Neo4j / Neptune / Memgraph)
|
|
42
|
+
|
|
43
|
+
Documents okb source add docs/button.md [--repo-url https://github.com/.../button.md] [--url https://site/button]
|
|
44
|
+
(extraction) okb source outline <source> [--quotable] [--json] every block, with heading path and lines
|
|
45
|
+
okb quote add <source> "verbatim text" [--cites Node] [--line N] located automatically
|
|
46
|
+
okb rule add --statement "..." --modality SHOULD --governs Button --cites <quote id> [--authored] [--value slot=value] [--refines <rule> --adds "..."]
|
|
47
|
+
okb rule refines <rule> <broader rule> [--adds "..."] okb fulfills <instance> <rule>
|
|
48
|
+
okb cite <node> <quote id> attach an existing quote to a node
|
|
49
|
+
okb verify <node> --status SUPPORTED|OVERREACH|UNSUPPORTED [--corrected "..."] · okb verify <node> --approve
|
|
50
|
+
okb source refresh <source> after the document changes
|
|
51
|
+
|
|
52
|
+
Skills okb skills install [--copy] [--dir <folder>] [--force] link (or copy) the packaged skills into ~/.claude/skills
|
|
53
|
+
okb skills status [--dir <folder>] which are installed, where, and whether they match this okb
|
|
54
|
+
|
|
55
|
+
Also: okb show <name> · okb rename <old> <new> · okb remove <name> · okb drift <folder>...
|
|
56
|
+
Names: when a class and a slot share a name, say which: class:Genre / slot:genre
|
|
57
|
+
Global options: -C <folder> (default: current folder) · --json (machine-readable output where supported)`;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export type BlockKind = "frontmatter" | "paragraph" | "list-item" | "callout" | "table-row" | "code" | "comment" | "image" | "html";
|
|
2
|
+
export interface MdBlock {
|
|
3
|
+
index: number;
|
|
4
|
+
kind: BlockKind;
|
|
5
|
+
headingPath: string[];
|
|
6
|
+
startLine: number;
|
|
7
|
+
endLine: number;
|
|
8
|
+
raw: string;
|
|
9
|
+
text: string;
|
|
10
|
+
quotable: boolean;
|
|
11
|
+
note?: string;
|
|
12
|
+
calloutType?: string;
|
|
13
|
+
lang?: string;
|
|
14
|
+
row?: {
|
|
15
|
+
columns: string[];
|
|
16
|
+
cells: string[];
|
|
17
|
+
key: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface MdDoc {
|
|
21
|
+
frontmatter: Record<string, unknown> | null;
|
|
22
|
+
title?: string;
|
|
23
|
+
headings: {
|
|
24
|
+
level: number;
|
|
25
|
+
text: string;
|
|
26
|
+
line: number;
|
|
27
|
+
path: string[];
|
|
28
|
+
}[];
|
|
29
|
+
blocks: MdBlock[];
|
|
30
|
+
lines: string[];
|
|
31
|
+
problems: {
|
|
32
|
+
line: number;
|
|
33
|
+
message: string;
|
|
34
|
+
}[];
|
|
35
|
+
}
|
|
36
|
+
export declare function inlineToText(s: string): string;
|
|
37
|
+
/** Split a GFM table row into cells, ignoring pipes inside code spans or escaped. */
|
|
38
|
+
export declare function splitRow(line: string): string[];
|
|
39
|
+
export declare function slugHeading(text: string): string;
|
|
40
|
+
export declare function parseMarkdown(src: string): MdDoc;
|
|
41
|
+
export interface QuoteHit {
|
|
42
|
+
blocks: MdBlock[];
|
|
43
|
+
headingPath: string[];
|
|
44
|
+
startLine: number;
|
|
45
|
+
endLine: number;
|
|
46
|
+
quotable: boolean;
|
|
47
|
+
locator: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Find a quote in a parsed doc. Matching is on normalized plain text (letters and
|
|
51
|
+
* digits only), so it tolerates markdown syntax, line wrapping and punctuation, but
|
|
52
|
+
* not changed words. "..." marks a gap. A quote may span consecutive blocks.
|
|
53
|
+
* Returns every place it occurs.
|
|
54
|
+
*/
|
|
55
|
+
export declare function locate(doc: MdDoc, quote: string): QuoteHit[];
|
|
56
|
+
/** GitHub-style links for a quote: source lines (?plain=1#Lx-Ly) and rendered heading anchor. */
|
|
57
|
+
export declare function deepLinks(hit: QuoteHit, repoUrl?: string, pageUrl?: string): {
|
|
58
|
+
sourceLink?: string;
|
|
59
|
+
pageLink?: string;
|
|
60
|
+
};
|