@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/checks.js
ADDED
|
@@ -0,0 +1,966 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The validator. Every exported check implements exactly one meta-KB rule whose
|
|
3
|
+
* `check` is mechanical or heuristic; meta-kb/build.ts fails if the two drift apart.
|
|
4
|
+
*
|
|
5
|
+
* A check returns raw hits ({message, nodes, severity?}). runChecks() attaches the
|
|
6
|
+
* rule's severity (from its modality), hides rules not yet relevant at the current
|
|
7
|
+
* step (counting them in hiddenLater), and marks warnings/info as "explained" when
|
|
8
|
+
* a DesignDecision waives them.
|
|
9
|
+
*/
|
|
10
|
+
import { existsSync } from "node:fs";
|
|
11
|
+
import { isAbsolute, join } from "node:path";
|
|
12
|
+
import { MetaKB } from "./metakb.js";
|
|
13
|
+
import { Ontology, VALUE_TYPES, DISPOSITIONS } from "@ontology-kit/format";
|
|
14
|
+
import { isOneOf } from "./types.js";
|
|
15
|
+
import * as naming from "./naming.js";
|
|
16
|
+
import { findQuote, loadPages, normalize } from "./quotes.js";
|
|
17
|
+
import { locate, parseMarkdown } from "./markdown.js";
|
|
18
|
+
import { readFileSync } from "node:fs";
|
|
19
|
+
/** Ontology 101 §4.2: "more than a dozen subclasses" suggests missing intermediate categories. */
|
|
20
|
+
const MAX_DIRECT_SUBCLASSES = 12;
|
|
21
|
+
/** Step 1 asks for at least this many competency questions (also a step 1 doneWhen item). */
|
|
22
|
+
export const MIN_COMPETENCY_QUESTIONS = 3;
|
|
23
|
+
/** Quotes from different sources at least this similar (bigram Dice) are reported as possible copy-paste. */
|
|
24
|
+
const DUPLICATE_QUOTE_SIMILARITY = 0.9;
|
|
25
|
+
const L = (ont, id) => `'${ont.label(id)}'`;
|
|
26
|
+
const { an } = naming;
|
|
27
|
+
const list = (ont, ids) => [...ids].map((i) => L(ont, i)).join(", ");
|
|
28
|
+
const classes = (ont) => ont.ofType("Class");
|
|
29
|
+
const slots = (ont) => ont.ofType("Slot");
|
|
30
|
+
function literalProblem(v, f) {
|
|
31
|
+
switch (f.valueType) {
|
|
32
|
+
case "String":
|
|
33
|
+
return typeof v === "string" ? null : "should be text";
|
|
34
|
+
case "Integer":
|
|
35
|
+
return Number.isInteger(v) ? null : "should be a whole number";
|
|
36
|
+
case "Float":
|
|
37
|
+
case "Number":
|
|
38
|
+
return typeof v === "number" ? null : "should be a number";
|
|
39
|
+
case "Boolean":
|
|
40
|
+
return typeof v === "boolean" ? null : "should be true or false";
|
|
41
|
+
case "Enumerated":
|
|
42
|
+
if (typeof v !== "string")
|
|
43
|
+
return "should be one of the allowed values";
|
|
44
|
+
return !f.allowedValues || f.allowedValues.includes(v) ? null : `is not one of the allowed values (${f.allowedValues.join(", ")})`;
|
|
45
|
+
case "Instance":
|
|
46
|
+
return "should be a link to another instance, not a plain value";
|
|
47
|
+
default:
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Is `target` an acceptable Instance-type value given the range? */
|
|
52
|
+
function inRange(ont, target, range, fromClass) {
|
|
53
|
+
const t = ont.get(target);
|
|
54
|
+
if (!t)
|
|
55
|
+
return false;
|
|
56
|
+
if (range.length === 0)
|
|
57
|
+
return true;
|
|
58
|
+
if (t.type === "Instance") {
|
|
59
|
+
const cs = ont.instanceClasses(target);
|
|
60
|
+
return range.some((r) => cs.has(r));
|
|
61
|
+
}
|
|
62
|
+
if (t.type === "Class" && fromClass)
|
|
63
|
+
return range.some((r) => ont.isSub(target, r));
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
/** The extraction-pipeline fields of any node: a hand edit can put them on any type. */
|
|
67
|
+
const pipeline = (n) => n;
|
|
68
|
+
function isTerminological(ont, c) {
|
|
69
|
+
return Boolean(c.terminological) || ont.meta.kind === "terminological";
|
|
70
|
+
}
|
|
71
|
+
export const CHECKS = {
|
|
72
|
+
// Structure
|
|
73
|
+
"struct-well-formed": (ont) => {
|
|
74
|
+
const hits = [];
|
|
75
|
+
const { nodeTypes, edgeTypes } = MetaKB.get().formatRegistry();
|
|
76
|
+
const seen = new Set();
|
|
77
|
+
for (const n of ont.nodes) {
|
|
78
|
+
if (!n.id) {
|
|
79
|
+
hits.push({ message: `A ${n.type ?? "node"} has no id.`, nodes: [] });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (seen.has(n.id))
|
|
83
|
+
hits.push({ message: `Two nodes share the id '${n.id}'.`, nodes: [n.id] });
|
|
84
|
+
seen.add(n.id);
|
|
85
|
+
const nt = nodeTypes.get(n.type);
|
|
86
|
+
if (!nt) {
|
|
87
|
+
hits.push({ message: `Node '${n.id}' has unknown type '${n.type}'. Allowed: ${[...nodeTypes.keys()].join(", ")}.`, nodes: [n.id] });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
// Read as a plain record: the point is to find what a hand edit left out.
|
|
91
|
+
const raw = n;
|
|
92
|
+
const missing = nt.required.filter((p) => raw[p] === undefined || raw[p] === "");
|
|
93
|
+
if (missing.length)
|
|
94
|
+
hits.push({ message: `${n.type} '${n.id}' is missing ${missing.join(", ")}.`, nodes: [n.id] });
|
|
95
|
+
if (n.type === "Slot" && n.cardinality !== undefined && !["single", "multiple"].includes(n.cardinality)) {
|
|
96
|
+
hits.push({ message: `Slot ${L(ont, n.id)} has cardinality '${n.cardinality}'; use 'single' or 'multiple'.`, nodes: [n.id] });
|
|
97
|
+
}
|
|
98
|
+
if (n.type === "Term" && n.disposition !== undefined && !DISPOSITIONS.includes(n.disposition)) {
|
|
99
|
+
hits.push({ message: `Term ${L(ont, n.id)} has disposition '${n.disposition}'; use one of ${DISPOSITIONS.join(", ")}.`, nodes: [n.id] });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const onto = ont.ofType("Ontology");
|
|
103
|
+
if (onto.length !== 1 || onto[0].id !== "ontology") {
|
|
104
|
+
hits.push({ message: `There must be exactly one Ontology node with id 'ontology' (found ${onto.length}).`, nodes: onto.map((o) => o.id) });
|
|
105
|
+
}
|
|
106
|
+
const relTypes = ont.rel.edgeTypes();
|
|
107
|
+
for (const e of ont.edges) {
|
|
108
|
+
const et = edgeTypes.get(e.type);
|
|
109
|
+
const tag = `${e.from} -${e.type}-> ${e.to}`;
|
|
110
|
+
if (!et) {
|
|
111
|
+
if (relTypes.has(e.type)) {
|
|
112
|
+
// A relationship value: must join two Instances, or start at a Class (a fixed class-level value).
|
|
113
|
+
const a = ont.get(e.from);
|
|
114
|
+
const b = ont.get(e.to);
|
|
115
|
+
if (!a || !b)
|
|
116
|
+
hits.push({ message: `Edge ${tag} points at a node that doesn't exist (${!a ? e.from : e.to}).`, nodes: [a ? e.from : e.to] });
|
|
117
|
+
else if (!["Instance", "Class"].includes(a.type) || !["Instance", "Class"].includes(b.type) || (a.type === "Instance" && b.type === "Class")) {
|
|
118
|
+
hits.push({ message: `Relationship ${tag} connects ${a.type} → ${b.type}; relationships link instances (or state a fixed value on a class).`, nodes: [e.from, e.to] });
|
|
119
|
+
}
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
hits.push({ message: `Edge ${tag} has unknown type. It's neither an okb structural edge (${[...edgeTypes.keys()].join(", ")}) nor the relationship type of any relationship slot (${[...relTypes.keys()].join(", ") || "none yet"}).`, nodes: [e.from] });
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const a = ont.get(e.from);
|
|
126
|
+
const b = ont.get(e.to);
|
|
127
|
+
if (!a || !b) {
|
|
128
|
+
hits.push({ message: `Edge ${tag} points at a node that doesn't exist (${!a ? e.from : e.to}).`, nodes: [a ? e.from : e.to] });
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
// Instance misuse in the hierarchy gets its own friendlier rule (inst-are-leaves).
|
|
132
|
+
const instanceMisuse = (e.type === "IS_A" && (a.type === "Instance" || b.type === "Instance")) ||
|
|
133
|
+
(e.type === "INSTANCE_OF" && b.type === "Instance");
|
|
134
|
+
if (!instanceMisuse && (!et.fromTypes.includes(a.type) || !et.toTypes.includes(b.type))) {
|
|
135
|
+
hits.push({ message: `Edge ${tag} connects ${a.type} → ${b.type}; ${e.type} must go ${et.fromTypes.join("/")} → ${et.toTypes.join("/")}.`, nodes: [e.from, e.to] });
|
|
136
|
+
}
|
|
137
|
+
for (const p of et.props) {
|
|
138
|
+
if (e[p] === undefined)
|
|
139
|
+
hits.push({ message: `Edge ${tag} is missing property '${p}'.`, nodes: [e.from] });
|
|
140
|
+
}
|
|
141
|
+
for (const p of et.optionalProps) {
|
|
142
|
+
if (e[p] !== undefined && typeof e[p] !== "string")
|
|
143
|
+
hits.push({ message: `Edge ${tag} has property '${p}' = ${JSON.stringify(e[p])}; it must be text.`, nodes: [e.from] });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return hits;
|
|
147
|
+
},
|
|
148
|
+
// Hierarchy
|
|
149
|
+
"hier-no-cycles": (ont) => {
|
|
150
|
+
const hits = [];
|
|
151
|
+
const reported = new Set();
|
|
152
|
+
const color = new Map();
|
|
153
|
+
const visit = (u, path) => {
|
|
154
|
+
color.set(u, 1);
|
|
155
|
+
path.push(u);
|
|
156
|
+
for (const v of ont.parents(u)) {
|
|
157
|
+
if (color.get(v) === 1) {
|
|
158
|
+
const cycle = path.slice(path.indexOf(v));
|
|
159
|
+
const k = [...cycle].sort().join("|");
|
|
160
|
+
if (!reported.has(k)) {
|
|
161
|
+
reported.add(k);
|
|
162
|
+
hits.push({ message: `Cycle in the hierarchy: ${[...cycle, v].map((c) => ont.label(c)).join(" → ")}. These classes would all be the same class.`, nodes: cycle });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (!color.get(v))
|
|
166
|
+
visit(v, path);
|
|
167
|
+
}
|
|
168
|
+
path.pop();
|
|
169
|
+
color.set(u, 2);
|
|
170
|
+
};
|
|
171
|
+
for (const c of classes(ont))
|
|
172
|
+
if (!color.get(c.id))
|
|
173
|
+
visit(c.id, []);
|
|
174
|
+
return hits;
|
|
175
|
+
},
|
|
176
|
+
"hier-no-singular-plural-pair": (ont) => {
|
|
177
|
+
const hits = [];
|
|
178
|
+
const cs = classes(ont);
|
|
179
|
+
for (const a of cs) {
|
|
180
|
+
for (const b of cs) {
|
|
181
|
+
if (a.id >= b.id || !a.name || !b.name)
|
|
182
|
+
continue;
|
|
183
|
+
const sa = naming.key(naming.withHead(a.name, naming.singularize));
|
|
184
|
+
const sb = naming.key(naming.withHead(b.name, naming.singularize));
|
|
185
|
+
if (sa !== sb || naming.key(a.name) === naming.key(b.name))
|
|
186
|
+
continue;
|
|
187
|
+
const linked = ont.parents(a.id).includes(b.id) || ont.parents(b.id).includes(a.id);
|
|
188
|
+
hits.push({
|
|
189
|
+
message: linked
|
|
190
|
+
? `${L(ont, a.id)} and ${L(ont, b.id)} are singular/plural forms of the same concept, and one is a subclass of the other.`
|
|
191
|
+
: `${L(ont, a.id)} and ${L(ont, b.id)} look like singular/plural forms of the same concept.`,
|
|
192
|
+
nodes: [a.id, b.id],
|
|
193
|
+
severity: linked ? "error" : "warning",
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return hits;
|
|
198
|
+
},
|
|
199
|
+
"hier-no-synonym-classes": (ont) => {
|
|
200
|
+
const hits = [];
|
|
201
|
+
const cs = classes(ont);
|
|
202
|
+
for (const a of cs) {
|
|
203
|
+
for (const syn of a.synonyms ?? []) {
|
|
204
|
+
for (const b of cs) {
|
|
205
|
+
if (b.id !== a.id && naming.key(b.name ?? "") === naming.key(syn)) {
|
|
206
|
+
hits.push({ message: `${L(ont, b.id)} is listed as a synonym of ${L(ont, a.id)} but is also its own class.`, nodes: [a.id, b.id] });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return hits;
|
|
212
|
+
},
|
|
213
|
+
"hier-no-redundant-isa": (ont) => {
|
|
214
|
+
const hits = [];
|
|
215
|
+
for (const c of classes(ont)) {
|
|
216
|
+
const ps = ont.parents(c.id);
|
|
217
|
+
for (const p of ps) {
|
|
218
|
+
const via = ps.find((q) => q !== p && ont.ancestors(q).has(p));
|
|
219
|
+
if (via) {
|
|
220
|
+
hits.push({ message: `${L(ont, c.id)} is linked directly to ${L(ont, p)}, but it already is one through ${L(ont, via)}.`, nodes: [c.id, p] });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return hits;
|
|
225
|
+
},
|
|
226
|
+
"hier-single-child": (ont) => classes(ont)
|
|
227
|
+
.filter((c) => ont.children(c.id).length === 1)
|
|
228
|
+
.map((c) => ({
|
|
229
|
+
message: `${L(ont, c.id)} has only one direct subclass (${L(ont, ont.children(c.id)[0])}). Is a sibling missing, or is the subclass really the same as its parent?`,
|
|
230
|
+
nodes: [c.id],
|
|
231
|
+
})),
|
|
232
|
+
"hier-too-many-children": (ont) => classes(ont)
|
|
233
|
+
.filter((c) => ont.children(c.id).length > MAX_DIRECT_SUBCLASSES)
|
|
234
|
+
.map((c) => ({
|
|
235
|
+
message: `${L(ont, c.id)} has ${ont.children(c.id).length} direct subclasses. Are there natural groups among them?`,
|
|
236
|
+
nodes: [c.id],
|
|
237
|
+
})),
|
|
238
|
+
"hier-subclass-adds-something": (ont) => {
|
|
239
|
+
const hits = [];
|
|
240
|
+
const rangeTargets = new Set(ont.edgesOf("RANGE").map((e) => e.to));
|
|
241
|
+
const relTypes = ont.rel.edgeTypes();
|
|
242
|
+
const valueTargets = new Set(ont.edges.filter((e) => relTypes.has(e.type)).map((e) => e.to));
|
|
243
|
+
const disjoint = new Set(ont.edgesOf("DISJOINT_WITH").flatMap((e) => [e.from, e.to]));
|
|
244
|
+
for (const c of classes(ont)) {
|
|
245
|
+
if (ont.parents(c.id).length === 0 || isTerminological(ont, c))
|
|
246
|
+
continue;
|
|
247
|
+
const nonEmpty = (o) => o && typeof o === "object" && Object.keys(o).length > 0;
|
|
248
|
+
const addsSomething = ont.ownSlots(c.id).length > 0 || nonEmpty(c.fixedValues) || nonEmpty(c.defaults) || nonEmpty(c.facetOverrides) ||
|
|
249
|
+
ont.rel.slotsWithValues(c.id).length > 0 || rangeTargets.has(c.id) || valueTargets.has(c.id) ||
|
|
250
|
+
disjoint.has(c.id) || ont.sources(c.id, "ABOUT").length > 0 || ont.parents(c.id).length > 1;
|
|
251
|
+
if (!addsSomething) {
|
|
252
|
+
hits.push({ message: `${L(ont, c.id)} doesn't add anything to ${list(ont, ont.parents(c.id))}: no own slots, values, restrictions or relationships.`, nodes: [c.id] });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return hits;
|
|
256
|
+
},
|
|
257
|
+
// Instances
|
|
258
|
+
"inst-are-leaves": (ont) => {
|
|
259
|
+
const hits = [];
|
|
260
|
+
for (const e of ont.edges) {
|
|
261
|
+
const a = ont.get(e.from);
|
|
262
|
+
const b = ont.get(e.to);
|
|
263
|
+
if (!a || !b)
|
|
264
|
+
continue;
|
|
265
|
+
if (e.type === "IS_A" && (a.type === "Instance" || b.type === "Instance")) {
|
|
266
|
+
hits.push({ message: `${L(ont, e.from)} IS_A ${L(ont, e.to)}, but instances can't be in the class hierarchy.`, nodes: [e.from, e.to] });
|
|
267
|
+
}
|
|
268
|
+
if (e.type === "INSTANCE_OF" && b.type === "Instance") {
|
|
269
|
+
hits.push({ message: `${L(ont, e.from)} is an instance of ${L(ont, e.to)}, which is itself an instance. Nothing can be an instance of an instance.`, nodes: [e.from, e.to] });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return hits;
|
|
273
|
+
},
|
|
274
|
+
"inst-not-of-abstract": (ont) => ont.edgesOf("INSTANCE_OF")
|
|
275
|
+
.filter((e) => {
|
|
276
|
+
const c = ont.get(e.to);
|
|
277
|
+
return c?.type === "Class" && c.abstract;
|
|
278
|
+
})
|
|
279
|
+
.map((e) => ({ message: `${L(ont, e.from)} is a direct instance of ${L(ont, e.to)}, which is abstract.`, nodes: [e.from, e.to] })),
|
|
280
|
+
// Slots
|
|
281
|
+
"slot-attach-most-general": (ont) => {
|
|
282
|
+
const hits = [];
|
|
283
|
+
for (const s of slots(ont)) {
|
|
284
|
+
const dom = ont.domain(s.id);
|
|
285
|
+
for (const c of dom) {
|
|
286
|
+
const anc = dom.find((d) => d !== c && ont.ancestors(c).has(d));
|
|
287
|
+
if (anc)
|
|
288
|
+
hits.push({ message: `${L(ont, s.id)} is attached to ${L(ont, c)} and to its ancestor ${L(ont, anc)}. ${L(ont, c)} inherits it already.`, nodes: [s.id, c] });
|
|
289
|
+
}
|
|
290
|
+
for (const p of classes(ont)) {
|
|
291
|
+
const kids = ont.children(p.id);
|
|
292
|
+
if (kids.length < 2 || dom.includes(p.id) || dom.some((d) => ont.ancestors(p.id).has(d)))
|
|
293
|
+
continue;
|
|
294
|
+
if (kids.every((k) => dom.includes(k))) {
|
|
295
|
+
hits.push({ message: `${L(ont, s.id)} is attached to every subclass of ${L(ont, p.id)} (${list(ont, kids)}). Attach it to ${L(ont, p.id)} instead.`, nodes: [s.id, p.id] });
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return hits;
|
|
300
|
+
},
|
|
301
|
+
"slot-value-type-declared": (ont) => slots(ont)
|
|
302
|
+
.filter((s) => !isOneOf(VALUE_TYPES, s.valueType))
|
|
303
|
+
.map((s) => ({
|
|
304
|
+
message: s.valueType
|
|
305
|
+
? `Slot ${L(ont, s.id)} has value type '${s.valueType}'. Use one of ${VALUE_TYPES.join(", ")}.`
|
|
306
|
+
: `Slot ${L(ont, s.id)} has no value type.`,
|
|
307
|
+
nodes: [s.id],
|
|
308
|
+
})),
|
|
309
|
+
"slot-instance-needs-range": (ont) => slots(ont)
|
|
310
|
+
.filter((s) => s.valueType === "Instance" && ont.range(s.id).length === 0)
|
|
311
|
+
.map((s) => ({ message: `Slot ${L(ont, s.id)} links to other things but doesn't say which class they must belong to (no range).`, nodes: [s.id] })),
|
|
312
|
+
"slot-enum-needs-values": (ont) => slots(ont)
|
|
313
|
+
.filter((s) => s.valueType === "Enumerated" && !(Array.isArray(s.allowedValues) && s.allowedValues.length))
|
|
314
|
+
.map((s) => ({ message: `Slot ${L(ont, s.id)} is Enumerated but lists no allowed values.`, nodes: [s.id] })),
|
|
315
|
+
"slot-cardinality-declared": (ont) => {
|
|
316
|
+
const missing = slots(ont).filter((s) => !s.cardinality).map((s) => s.id);
|
|
317
|
+
return missing.length ? [{ message: `No cardinality (single or multiple) on: ${list(ont, missing)}.`, nodes: missing }] : [];
|
|
318
|
+
},
|
|
319
|
+
"slot-cardinality-coherent": (ont) => {
|
|
320
|
+
const hits = [];
|
|
321
|
+
const bad = (label, id, min, max, single) => {
|
|
322
|
+
const probs = [];
|
|
323
|
+
if (typeof min === "number" && min < 0)
|
|
324
|
+
probs.push(`minimum ${min} is negative`);
|
|
325
|
+
if (typeof max === "number" && max < 0)
|
|
326
|
+
probs.push(`maximum ${max} is negative`);
|
|
327
|
+
if (typeof min === "number" && typeof max === "number" && min > max)
|
|
328
|
+
probs.push(`minimum ${min} is more than maximum ${max}`);
|
|
329
|
+
if (single && typeof max === "number" && max > 1)
|
|
330
|
+
probs.push(`it is 'single' but maximum is ${max}`);
|
|
331
|
+
if (single && typeof min === "number" && min > 1)
|
|
332
|
+
probs.push(`it is 'single' but minimum is ${min}`);
|
|
333
|
+
if (probs.length)
|
|
334
|
+
hits.push({ message: `${label}: ${probs.join("; ")}.`, nodes: [id] });
|
|
335
|
+
};
|
|
336
|
+
for (const s of slots(ont))
|
|
337
|
+
bad(`Slot ${L(ont, s.id)}`, s.id, s.minCardinality, s.maxCardinality, s.cardinality === "single");
|
|
338
|
+
for (const c of classes(ont)) {
|
|
339
|
+
for (const [sid, ov] of Object.entries(c.facetOverrides ?? {})) {
|
|
340
|
+
const s = ont.get(sid);
|
|
341
|
+
if (s?.type !== "Slot")
|
|
342
|
+
continue;
|
|
343
|
+
const f = ont.effectiveFacets(sid, ont.up(c.id));
|
|
344
|
+
bad(`${L(ont, c.id)}'s restriction on ${L(ont, sid)}`, c.id, f.min, f.max, s.cardinality === "single");
|
|
345
|
+
const slotAllowed = s.allowedValues;
|
|
346
|
+
if (Array.isArray(ov.allowedValues) && Array.isArray(slotAllowed)) {
|
|
347
|
+
const extra = ov.allowedValues.filter((v) => !slotAllowed.includes(String(v)));
|
|
348
|
+
if (extra.length)
|
|
349
|
+
hits.push({ message: `${L(ont, c.id)} allows ${extra.join(", ")} for ${L(ont, sid)}, which the slot itself doesn't allow.`, nodes: [c.id, sid] });
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return hits;
|
|
354
|
+
},
|
|
355
|
+
"slot-range-remove-subclass": (ont) => {
|
|
356
|
+
const hits = [];
|
|
357
|
+
for (const s of slots(ont)) {
|
|
358
|
+
const r = ont.range(s.id);
|
|
359
|
+
for (const b of r) {
|
|
360
|
+
const a = r.find((x) => x !== b && ont.ancestors(b).has(x));
|
|
361
|
+
if (a)
|
|
362
|
+
hits.push({ message: `${L(ont, s.id)}'s range lists ${L(ont, a)} and its subclass ${L(ont, b)}. Remove ${L(ont, b)}.`, nodes: [s.id, b] });
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return hits;
|
|
366
|
+
},
|
|
367
|
+
"slot-range-collapse-subclasses": (ont) => {
|
|
368
|
+
const hits = [];
|
|
369
|
+
for (const s of slots(ont)) {
|
|
370
|
+
const r = ont.range(s.id);
|
|
371
|
+
for (const p of classes(ont)) {
|
|
372
|
+
const kids = ont.children(p.id);
|
|
373
|
+
if (kids.length >= 2 && !r.includes(p.id) && kids.every((k) => r.includes(k))) {
|
|
374
|
+
hits.push({ message: `${L(ont, s.id)}'s range lists every subclass of ${L(ont, p.id)}. Use ${L(ont, p.id)} instead.`, nodes: [s.id, p.id] });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return hits;
|
|
379
|
+
},
|
|
380
|
+
"slot-range-all-but-few": (ont) => {
|
|
381
|
+
const hits = [];
|
|
382
|
+
for (const s of slots(ont)) {
|
|
383
|
+
const r = ont.range(s.id);
|
|
384
|
+
for (const p of classes(ont)) {
|
|
385
|
+
const kids = ont.children(p.id);
|
|
386
|
+
const listed = kids.filter((k) => r.includes(k));
|
|
387
|
+
const missing = kids.length - listed.length;
|
|
388
|
+
if (kids.length >= 4 && !r.includes(p.id) && missing >= 1 && missing <= 2 && listed.length >= kids.length / 2) {
|
|
389
|
+
hits.push({ message: `${L(ont, s.id)}'s range lists ${listed.length} of ${kids.length} subclasses of ${L(ont, p.id)}. Would ${L(ont, p.id)} be the better range?`, nodes: [s.id, p.id] });
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return hits;
|
|
394
|
+
},
|
|
395
|
+
"slot-range-not-too-general": (ont) => {
|
|
396
|
+
const hits = [];
|
|
397
|
+
const generic = new Set(["thing", "entity", "object", "item", "anything", "node", "resource"]);
|
|
398
|
+
const all = classes(ont);
|
|
399
|
+
const roots = ont.roots();
|
|
400
|
+
for (const s of slots(ont)) {
|
|
401
|
+
for (const r of ont.range(s.id)) {
|
|
402
|
+
const n = ont.get(r);
|
|
403
|
+
const topOfEverything = roots.length === 1 && roots[0] === r && all.length >= 5;
|
|
404
|
+
if (n && (generic.has(naming.key(("name" in n && n.name) || "")) || topOfEverything)) {
|
|
405
|
+
hits.push({ message: `${L(ont, s.id)}'s range is ${L(ont, r)}, which covers almost everything. What kind of thing actually fills it?`, nodes: [s.id, r] });
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return hits;
|
|
410
|
+
},
|
|
411
|
+
"slot-values-respect-facets": (ont) => {
|
|
412
|
+
const hits = [];
|
|
413
|
+
const owners = [...ont.ofType("Instance"), ...ont.ofType("Rule"), ...classes(ont)];
|
|
414
|
+
for (const n of owners) {
|
|
415
|
+
// A rule's qualifier values are checked like an instance's, against the slots that apply to Rules.
|
|
416
|
+
const holdsValues = n.type !== "Class";
|
|
417
|
+
const ctx = ont.classContext(n.id);
|
|
418
|
+
const applicable = ont.applicableSlots(n.id);
|
|
419
|
+
for (const sid of applicable) {
|
|
420
|
+
const s = ont.get(sid);
|
|
421
|
+
if (s?.type !== "Slot")
|
|
422
|
+
continue; // a HAS_SLOT to a non-slot: struct-well-formed reports it
|
|
423
|
+
const f = ont.effectiveFacets(sid, ctx);
|
|
424
|
+
let vals = ont.statedValues(n.id, sid);
|
|
425
|
+
// Values inherited from a class's fixed value were checked on that class; only count them here.
|
|
426
|
+
const inherited = holdsValues && vals.length === 0;
|
|
427
|
+
if (inherited)
|
|
428
|
+
vals = ont.inheritedFixed(sid, ctx)?.values ?? [];
|
|
429
|
+
for (const v of inherited ? [] : vals) {
|
|
430
|
+
if (f.valueType === "Instance") {
|
|
431
|
+
if (typeof v !== "string" || !ont.get(v)) {
|
|
432
|
+
hits.push({ message: `${L(ont, n.id)}.${s.name} = ${JSON.stringify(v)} should be a link to another instance.`, nodes: [n.id, sid] });
|
|
433
|
+
}
|
|
434
|
+
else if (!inRange(ont, v, f.range, !holdsValues)) {
|
|
435
|
+
hits.push({ message: `${L(ont, n.id)}.${s.name} points at ${L(ont, v)}, which isn't ${f.range.map((r) => an(ont.label(r))).join(" or ")}.`, nodes: [n.id, sid, v] });
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
const p = literalProblem(v, f);
|
|
440
|
+
if (p)
|
|
441
|
+
hits.push({ message: `${L(ont, n.id)}.${s.name} = ${JSON.stringify(v)} ${p}.`, nodes: [n.id, sid] });
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (f.max !== null && vals.length > f.max) {
|
|
445
|
+
hits.push({ message: `${L(ont, n.id)} has ${vals.length} values for ${s.name}; at most ${f.max} allowed.`, nodes: [n.id, sid] });
|
|
446
|
+
}
|
|
447
|
+
if (holdsValues && vals.length < f.min) {
|
|
448
|
+
hits.push({ message: `${L(ont, n.id)} has ${vals.length} value(s) for ${s.name}; at least ${f.min} required.`, nodes: [n.id, sid] });
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return hits;
|
|
453
|
+
},
|
|
454
|
+
"slot-value-slot-applies": (ont) => {
|
|
455
|
+
const hits = [];
|
|
456
|
+
for (const n of [...ont.ofType("Instance"), ...ont.ofType("Rule"), ...classes(ont)]) {
|
|
457
|
+
const applicable = new Set(ont.applicableSlots(n.id));
|
|
458
|
+
const used = new Set();
|
|
459
|
+
const fields = n.type === "Class" ? [n.fixedValues, n.defaults, n.facetOverrides] : [n.values];
|
|
460
|
+
for (const field of fields)
|
|
461
|
+
for (const sid of Object.keys(field ?? {}))
|
|
462
|
+
used.add(sid);
|
|
463
|
+
for (const sid of ont.rel.slotsWithValues(n.id))
|
|
464
|
+
used.add(sid);
|
|
465
|
+
for (const sid of used) {
|
|
466
|
+
if (!ont.get(sid))
|
|
467
|
+
hits.push({ message: `${L(ont, n.id)} has a value for '${sid}', which isn't a slot.`, nodes: [n.id] });
|
|
468
|
+
else if (!applicable.has(sid)) {
|
|
469
|
+
hits.push({ message: `${L(ont, n.id)} has a value for ${L(ont, sid)}, but that slot isn't attached to ${n.type === "Instance" ? "its class" : "it"} or any ancestor.`, nodes: [n.id, sid] });
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return hits;
|
|
474
|
+
},
|
|
475
|
+
"slot-default-allowed": (ont) => {
|
|
476
|
+
const hits = [];
|
|
477
|
+
const check = (owner, sid, v, ctx) => {
|
|
478
|
+
const s = ont.get(sid);
|
|
479
|
+
if (!s)
|
|
480
|
+
return;
|
|
481
|
+
const f = ont.effectiveFacets(sid, ctx);
|
|
482
|
+
const p = f.valueType === "Instance"
|
|
483
|
+
? (typeof v === "string" && ont.get(v) && inRange(ont, v, f.range, false) ? null : "isn't an instance in the slot's range")
|
|
484
|
+
: literalProblem(v, f);
|
|
485
|
+
if (p)
|
|
486
|
+
hits.push({ message: `Default for ${L(ont, sid)}${owner !== sid ? ` on ${L(ont, owner)}` : ""} (${JSON.stringify(v)}) ${p}.`, nodes: [owner, sid] });
|
|
487
|
+
};
|
|
488
|
+
for (const s of slots(ont))
|
|
489
|
+
if (s.default !== undefined)
|
|
490
|
+
check(s.id, s.id, s.default, []);
|
|
491
|
+
for (const c of classes(ont))
|
|
492
|
+
for (const [sid, v] of Object.entries(c.defaults ?? {}))
|
|
493
|
+
check(c.id, sid, v, ont.up(c.id));
|
|
494
|
+
return hits;
|
|
495
|
+
},
|
|
496
|
+
"slot-fixed-not-overridden": (ont) => {
|
|
497
|
+
const hits = [];
|
|
498
|
+
const same = (a, b) => JSON.stringify([...a].map(String).sort()) === JSON.stringify([...b].map(String).sort());
|
|
499
|
+
for (const c of classes(ont)) {
|
|
500
|
+
const fixedSlots = new Set([...Object.keys(c.fixedValues ?? {}), ...ont.rel.slotsWithValues(c.id)]);
|
|
501
|
+
for (const sid of fixedSlots) {
|
|
502
|
+
const fixed = ont.statedValues(c.id, sid);
|
|
503
|
+
for (const d of ont.descendants(c.id)) {
|
|
504
|
+
const dn = ont.get(d);
|
|
505
|
+
const theirs = ont.statedValues(d, sid);
|
|
506
|
+
if (theirs.length && !same(theirs, fixed)) {
|
|
507
|
+
hits.push({ message: `${L(ont, c.id)} fixes ${ont.label(sid)} = ${fixed.map((v) => ont.label(String(v))).join(", ")}, but its subclass ${L(ont, d)} says ${theirs.map((v) => ont.label(String(v))).join(", ")}.`, nodes: [d, c.id, sid] });
|
|
508
|
+
}
|
|
509
|
+
const dd = dn?.type === "Class" ? dn.defaults?.[sid] : undefined;
|
|
510
|
+
if (dd !== undefined && !same([dd], fixed)) {
|
|
511
|
+
hits.push({ message: `${L(ont, d)} sets a default for ${ont.label(sid)}, but ${L(ont, c.id)} already fixes it to ${fixed.join(", ")}.`, nodes: [d, c.id, sid] });
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
for (const inst of ont.ofType("Instance")) {
|
|
515
|
+
if (!ont.instanceClasses(inst.id).has(c.id))
|
|
516
|
+
continue;
|
|
517
|
+
const theirs = ont.statedValues(inst.id, sid);
|
|
518
|
+
if (theirs.length && !same(theirs, fixed)) {
|
|
519
|
+
hits.push({ message: `${L(ont, inst.id)} says ${ont.label(sid)} = ${theirs.map((v) => ont.label(String(v))).join(", ")}, but every ${ont.label(c.id)} has ${fixed.map((v) => ont.label(String(v))).join(", ")}.`, nodes: [inst.id, c.id, sid] });
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return hits;
|
|
525
|
+
},
|
|
526
|
+
"slot-inverse-consistent": (ont) => {
|
|
527
|
+
const hits = [];
|
|
528
|
+
const related = (a, b) => a.length === 0 || b.length === 0 || a.some((x) => b.some((y) => ont.isSub(x, y) || ont.isSub(y, x)));
|
|
529
|
+
for (const e of ont.edgesOf("INVERSE_OF")) {
|
|
530
|
+
const [s1, s2] = [e.from, e.to];
|
|
531
|
+
for (const [x, y] of [[s1, s2], [s2, s1]]) {
|
|
532
|
+
if (!related(ont.range(x), ont.domain(y))) {
|
|
533
|
+
hits.push({ message: `${L(ont, x)} and ${L(ont, y)} are inverses, but ${L(ont, x)}'s range (${list(ont, ont.range(x))}) doesn't match ${L(ont, y)}'s domain (${list(ont, ont.domain(y))}).`, nodes: [x, y] });
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return hits;
|
|
538
|
+
},
|
|
539
|
+
"slot-edge-properties": (ont) => {
|
|
540
|
+
const hits = [];
|
|
541
|
+
const rel = ont.rel.edgeTypes();
|
|
542
|
+
for (const e of ont.edges) {
|
|
543
|
+
const sid = rel.get(e.type);
|
|
544
|
+
if (!sid)
|
|
545
|
+
continue;
|
|
546
|
+
const s = ont.require(sid, "Slot");
|
|
547
|
+
const decls = s.edgeProperties ?? [];
|
|
548
|
+
const tag = `(${ont.label(e.from)})-[:${e.type}]->(${ont.label(e.to)})`;
|
|
549
|
+
for (const [k, v] of Object.entries(e)) {
|
|
550
|
+
if (["from", "to", "type"].includes(k))
|
|
551
|
+
continue;
|
|
552
|
+
if (k === "rule") {
|
|
553
|
+
if (ont.get(String(v))?.type !== "Rule")
|
|
554
|
+
hits.push({ message: `${tag} points at rule '${v}', which isn't a Rule node.`, nodes: [e.from, e.to] });
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
const d = decls.find((x) => x.name === k);
|
|
558
|
+
if (!d) {
|
|
559
|
+
hits.push({ message: `${tag} has an undeclared edge property '${k}'. Declare it: okb relationship property ${s.name} ${k} --type ...`, nodes: [e.from, sid], severity: "warning" });
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
const f = { valueType: d.valueType, allowedValues: d.allowedValues ?? null, min: 0, max: null, range: [], overriddenBy: [] };
|
|
563
|
+
const p = literalProblem(v, f);
|
|
564
|
+
if (p)
|
|
565
|
+
hits.push({ message: `${tag}.${k} = ${JSON.stringify(v)} ${p}.`, nodes: [e.from, sid] });
|
|
566
|
+
}
|
|
567
|
+
for (const d of decls) {
|
|
568
|
+
if (d.required && e[d.name] === undefined)
|
|
569
|
+
hits.push({ message: `${tag} is missing required edge property '${d.name}'.`, nodes: [e.from, sid] });
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return hits;
|
|
573
|
+
},
|
|
574
|
+
// Disjointness
|
|
575
|
+
"disjoint-no-shared-members": (ont) => {
|
|
576
|
+
const hits = [];
|
|
577
|
+
const pairs = ont.edgesOf("DISJOINT_WITH").map((e) => [e.from, e.to]);
|
|
578
|
+
for (const [a, b] of pairs) {
|
|
579
|
+
if (ont.isSub(a, b) || ont.isSub(b, a)) {
|
|
580
|
+
const [sub, sup] = ont.isSub(a, b) ? [a, b] : [b, a];
|
|
581
|
+
hits.push({ message: `${L(ont, sub)} is declared disjoint with its own ancestor ${L(ont, sup)}, so it could never have members.`, nodes: [a, b] });
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
const violates = (set) => pairs.filter(([a, b]) => set.has(a) && set.has(b) && !(a === b));
|
|
585
|
+
for (const c of classes(ont)) {
|
|
586
|
+
const v = violates(ont.up(c.id));
|
|
587
|
+
// Report only where the conflict starts, not on every descendant.
|
|
588
|
+
const fresh = v.filter(([a, b]) => !ont.parents(c.id).some((p) => ont.up(p).has(a) && ont.up(p).has(b)));
|
|
589
|
+
for (const [a, b] of fresh) {
|
|
590
|
+
if (c.id === a || c.id === b)
|
|
591
|
+
continue;
|
|
592
|
+
hits.push({ message: `${L(ont, c.id)} is under both ${L(ont, a)} and ${L(ont, b)}, which are declared disjoint.`, nodes: [c.id, a, b] });
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
for (const i of ont.ofType("Instance")) {
|
|
596
|
+
const direct = ont.targets(i.id, "INSTANCE_OF");
|
|
597
|
+
const v = violates(ont.instanceClasses(i.id)).filter(([a, b]) => !direct.some((d) => ont.up(d).has(a) && ont.up(d).has(b)));
|
|
598
|
+
for (const [a, b] of v)
|
|
599
|
+
hits.push({ message: `Instance ${L(ont, i.id)} is both ${an(ont.label(a))} and ${an(ont.label(b))}, which are declared disjoint.`, nodes: [i.id, a, b] });
|
|
600
|
+
}
|
|
601
|
+
return hits;
|
|
602
|
+
},
|
|
603
|
+
"disjoint-consider-siblings": (ont) => {
|
|
604
|
+
const hits = [];
|
|
605
|
+
const pairs = new Set(ont.edgesOf("DISJOINT_WITH").map((e) => [e.from, e.to].sort().join("|")));
|
|
606
|
+
for (const p of classes(ont)) {
|
|
607
|
+
const kids = ont.children(p.id);
|
|
608
|
+
if (kids.length < 2)
|
|
609
|
+
continue;
|
|
610
|
+
const any = kids.some((a, i) => kids.slice(i + 1).some((b) => pairs.has([a, b].sort().join("|")) || ont.disjointImplied(a, b)));
|
|
611
|
+
if (!any)
|
|
612
|
+
hits.push({ message: `The subclasses of ${L(ont, p.id)} (${list(ont, kids)}) have no disjointness declared. Can anything be more than one of them?`, nodes: [p.id] });
|
|
613
|
+
}
|
|
614
|
+
return hits;
|
|
615
|
+
},
|
|
616
|
+
// Naming
|
|
617
|
+
"naming-convention-defined": (ont) => {
|
|
618
|
+
if (ont.conventions?.source !== "default" && ont.conventions)
|
|
619
|
+
return [];
|
|
620
|
+
if (!ont.ofType("Class").length && !ont.ofType("Slot").length)
|
|
621
|
+
return [];
|
|
622
|
+
return [{
|
|
623
|
+
message: ont.conventions
|
|
624
|
+
? "okb is using its default naming convention because none was chosen. Confirm it (or pick your own) with `okb convention`."
|
|
625
|
+
: "No naming convention is set yet. Run `okb convention` to pick one.",
|
|
626
|
+
nodes: ["ontology"],
|
|
627
|
+
}];
|
|
628
|
+
},
|
|
629
|
+
"naming-follow-convention": (ont) => {
|
|
630
|
+
const conv = ont.conventions;
|
|
631
|
+
if (!conv)
|
|
632
|
+
return [];
|
|
633
|
+
const hits = [];
|
|
634
|
+
for (const c of classes(ont)) {
|
|
635
|
+
if (c.name && !naming.matches(c.name, conv.classCase)) {
|
|
636
|
+
hits.push({ message: `Class ${L(ont, c.id)} isn't ${conv.classCase}. Try '${naming.convert(c.name, conv.classCase)}'.`, nodes: [c.id] });
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
for (const s of slots(ont)) {
|
|
640
|
+
if (!s.name)
|
|
641
|
+
continue;
|
|
642
|
+
const ws = naming.tokens(s.name);
|
|
643
|
+
const affixOk = conv.slotAffix === "has-prefix" ? ws[0] === "has" : conv.slotAffix === "of-suffix" ? ws[ws.length - 1] === "of" : true;
|
|
644
|
+
if (!naming.matches(s.name, conv.slotCase) || !affixOk) {
|
|
645
|
+
hits.push({ message: `Slot ${L(ont, s.id)} doesn't follow the slot convention (${conv.slotCase}${conv.slotAffix !== "none" ? `, ${conv.slotAffix}` : ""}). Try '${naming.applySlotAffix(s.name, conv)}'.`, nodes: [s.id] });
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
if (conv.instanceCase) {
|
|
649
|
+
for (const i of ont.ofType("Instance")) {
|
|
650
|
+
if (i.name && !naming.matches(i.name, conv.instanceCase)) {
|
|
651
|
+
hits.push({ message: `Instance ${L(ont, i.id)} isn't ${conv.instanceCase}.`, nodes: [i.id] });
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return hits;
|
|
656
|
+
},
|
|
657
|
+
"naming-singular-plural-consistent": (ont) => {
|
|
658
|
+
const conv = ont.conventions;
|
|
659
|
+
if (!conv)
|
|
660
|
+
return [];
|
|
661
|
+
const wantPlural = conv.classNumber === "plural";
|
|
662
|
+
return classes(ont)
|
|
663
|
+
.filter((c) => c.name && naming.isPlural(naming.headWord(c.name)) !== wantPlural)
|
|
664
|
+
.map((c) => ({
|
|
665
|
+
message: `Class ${L(ont, c.id)} looks ${wantPlural ? "singular" : "plural"}, but the convention is ${conv.classNumber}. Try '${naming.withHead(c.name, wantPlural ? naming.pluralize : naming.singularize)}'.`,
|
|
666
|
+
nodes: [c.id],
|
|
667
|
+
}));
|
|
668
|
+
},
|
|
669
|
+
"naming-no-type-words": (ont) => {
|
|
670
|
+
const banned = new Set(["class", "slot", "property", "prop", "attribute", "attr", "concept", "instance"]);
|
|
671
|
+
return [...classes(ont), ...slots(ont)]
|
|
672
|
+
.filter((n) => n.name && naming.tokens(n.name).length > 1 && banned.has(naming.headWord(n.name)))
|
|
673
|
+
.map((n) => ({ message: `${n.type} ${L(ont, n.id)} ends in '${naming.headWord(n.name)}'. Leave type words out of names.`, nodes: [n.id] }));
|
|
674
|
+
},
|
|
675
|
+
"naming-no-abbreviations": (ont) => {
|
|
676
|
+
const hits = [];
|
|
677
|
+
for (const n of [...classes(ont), ...slots(ont)]) {
|
|
678
|
+
if (!n.name)
|
|
679
|
+
continue;
|
|
680
|
+
const suspicious = naming.rawTokens(n.name).filter((t) => /^[A-Z]{2,5}$/.test(t) || /\.$/.test(t));
|
|
681
|
+
if (suspicious.length)
|
|
682
|
+
hits.push({ message: `${n.type} ${L(ont, n.id)} may contain an abbreviation (${suspicious.join(", ")}).`, nodes: [n.id] });
|
|
683
|
+
}
|
|
684
|
+
return hits;
|
|
685
|
+
},
|
|
686
|
+
"naming-subclass-names-consistent": (ont) => {
|
|
687
|
+
const hits = [];
|
|
688
|
+
for (const p of classes(ont)) {
|
|
689
|
+
const kids = ont.children(p.id);
|
|
690
|
+
if (kids.length < 2 || !p.name)
|
|
691
|
+
continue;
|
|
692
|
+
const head = naming.headWord(p.name);
|
|
693
|
+
const incl = kids.filter((k) => naming.tokens(ont.label(k)).includes(head) || naming.tokens(ont.label(k)).includes(naming.pluralize(head)));
|
|
694
|
+
if (incl.length && incl.length < kids.length) {
|
|
695
|
+
const excl = kids.filter((k) => !incl.includes(k));
|
|
696
|
+
hits.push({ message: `Subclasses of ${L(ont, p.id)} are named inconsistently: ${list(ont, incl)} ${incl.length === 1 ? "includes" : "include"} '${head}' but ${list(ont, excl)} ${excl.length === 1 ? "doesn't" : "don't"}.`, nodes: [p.id, ...excl] });
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return hits;
|
|
700
|
+
},
|
|
701
|
+
"naming-unique": (ont) => {
|
|
702
|
+
const hits = [];
|
|
703
|
+
for (const t of ["Class", "Slot", "Instance"]) {
|
|
704
|
+
const seen = new Map();
|
|
705
|
+
for (const n of ont.ofType(t)) {
|
|
706
|
+
const k = naming.key(n.name ?? "");
|
|
707
|
+
if (!k)
|
|
708
|
+
continue;
|
|
709
|
+
if (seen.has(k))
|
|
710
|
+
hits.push({ message: `Two ${t === "Class" ? "classes" : t.toLowerCase() + "s"} have the same name: ${L(ont, seen.get(k))} and ${L(ont, n.id)}.`, nodes: [seen.get(k), n.id] });
|
|
711
|
+
else
|
|
712
|
+
seen.set(k, n.id);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return hits;
|
|
716
|
+
},
|
|
717
|
+
// Scope & docs
|
|
718
|
+
"scope-defined": (ont) => {
|
|
719
|
+
const m = ont.meta;
|
|
720
|
+
const missing = ["domain", "purpose"].filter((k) => !m[k]);
|
|
721
|
+
if (!(m.users ?? []).length)
|
|
722
|
+
missing.push("users");
|
|
723
|
+
return missing.length ? [{ message: `Scope is missing: ${missing.join(", ")}. Run \`okb scope\`.`, nodes: ["ontology"] }] : [];
|
|
724
|
+
},
|
|
725
|
+
"scope-competency-questions": (ont) => {
|
|
726
|
+
const n = ont.ofType("CompetencyQuestion").length;
|
|
727
|
+
return n >= MIN_COMPETENCY_QUESTIONS ? [] : [{ message: `There ${n === 1 ? "is 1 competency question" : `are ${n} competency questions`}; write at least ${MIN_COMPETENCY_QUESTIONS} (\`okb cq add\`).`, nodes: ["ontology"] }];
|
|
728
|
+
},
|
|
729
|
+
"scope-cq-coverage": (ont) => {
|
|
730
|
+
const loose = ont.ofType("CompetencyQuestion").filter((q) => ont.targets(q.id, "NEEDS").length === 0);
|
|
731
|
+
return loose.map((q) => ({ message: `Competency question ${q.id} ("${q.text}") isn't linked to anything that answers it.`, nodes: [q.id] }));
|
|
732
|
+
},
|
|
733
|
+
"scope-no-unneeded": (ont) => {
|
|
734
|
+
const needs = ont.edgesOf("NEEDS").map((e) => e.to);
|
|
735
|
+
if (needs.length === 0)
|
|
736
|
+
return [];
|
|
737
|
+
const neededClasses = new Set();
|
|
738
|
+
const neededSlots = new Set();
|
|
739
|
+
for (const t of needs) {
|
|
740
|
+
const n = ont.get(t);
|
|
741
|
+
if (!n)
|
|
742
|
+
continue;
|
|
743
|
+
if (n.type === "Class") {
|
|
744
|
+
for (const x of ont.up(t))
|
|
745
|
+
neededClasses.add(x);
|
|
746
|
+
for (const k of ont.children(t))
|
|
747
|
+
neededClasses.add(k);
|
|
748
|
+
}
|
|
749
|
+
else if (n.type === "Slot") {
|
|
750
|
+
neededSlots.add(t);
|
|
751
|
+
for (const i of ont.inverses(t))
|
|
752
|
+
neededSlots.add(i);
|
|
753
|
+
for (const d of ont.domain(t))
|
|
754
|
+
for (const x of ont.up(d))
|
|
755
|
+
neededClasses.add(x);
|
|
756
|
+
for (const r of ont.range(t))
|
|
757
|
+
for (const x of ont.up(r))
|
|
758
|
+
neededClasses.add(x);
|
|
759
|
+
}
|
|
760
|
+
else if (n.type === "Instance") {
|
|
761
|
+
for (const c of ont.targets(t, "INSTANCE_OF"))
|
|
762
|
+
for (const x of ont.up(c))
|
|
763
|
+
neededClasses.add(x);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
// One extra level of specialization is allowed (Ontology 101 §4.7).
|
|
767
|
+
for (const c of [...neededClasses])
|
|
768
|
+
for (const k of ont.children(c))
|
|
769
|
+
neededClasses.add(k);
|
|
770
|
+
const extraC = classes(ont).filter((c) => !neededClasses.has(c.id)).map((c) => c.id);
|
|
771
|
+
const extraS = slots(ont).filter((s) => !neededSlots.has(s.id)).map((s) => s.id);
|
|
772
|
+
const hits = [];
|
|
773
|
+
if (extraC.length)
|
|
774
|
+
hits.push({ message: `No competency question needs these classes (or their parent/child): ${list(ont, extraC)}.`, nodes: extraC });
|
|
775
|
+
if (extraS.length)
|
|
776
|
+
hits.push({ message: `No competency question needs these slots: ${list(ont, extraS)}.`, nodes: extraS });
|
|
777
|
+
return hits;
|
|
778
|
+
},
|
|
779
|
+
"doc-descriptions": (ont) => {
|
|
780
|
+
const missing = [...classes(ont), ...slots(ont)].filter((n) => !n.description).map((n) => n.id);
|
|
781
|
+
return missing.length ? [{ message: `No description on ${missing.length} class(es)/slot(s): ${list(ont, missing)}.`, nodes: missing }] : [];
|
|
782
|
+
},
|
|
783
|
+
"reuse-recorded": (ont) => ont.meta.reuseReviewed || ont.ofType("ReusedOntology").length
|
|
784
|
+
? []
|
|
785
|
+
: [{ message: "Reuse hasn't been considered yet (Step 2). Record what you found, or `okb reuse none --why ...`.", nodes: ["ontology"] }],
|
|
786
|
+
"terms-dispositioned": (ont) => {
|
|
787
|
+
const open = ont.ofType("Term").filter((t) => !t.disposition || t.disposition === "undecided").map((t) => t.id);
|
|
788
|
+
return open.length ? [{ message: `${open.length} term(s) from Step 3 haven't been sorted yet: ${list(ont, open)}.`, nodes: open }] : [];
|
|
789
|
+
},
|
|
790
|
+
// Provenance
|
|
791
|
+
"prov-verified": (ont) => {
|
|
792
|
+
const hits = [];
|
|
793
|
+
for (const n of ont.nodes) {
|
|
794
|
+
const { extracted, verification } = pipeline(n);
|
|
795
|
+
if ((n.type === "Rule" || extracted) && !verification) {
|
|
796
|
+
hits.push({ message: `${n.type} ${L(ont, n.id)} ${n.type === "Rule" && n.authored ? "was written by the owner" : "was extracted from a document"} but hasn't been verified against its quote yet (Step 3 of the extraction pipeline): okb verify ${n.id} --status ...`, nodes: [n.id] });
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
for (const n of ont.nodes) {
|
|
800
|
+
const { verification } = pipeline(n);
|
|
801
|
+
if (!verification || verification.status === "SUPPORTED")
|
|
802
|
+
continue;
|
|
803
|
+
hits.push({
|
|
804
|
+
message: verification.status === "OVERREACH"
|
|
805
|
+
? `${n.type} ${L(ont, n.id)} was corrected after claiming more than its quote supports. A person must check it: okb verify ${n.id} --approve`
|
|
806
|
+
: `${n.type} ${L(ont, n.id)} has verification status ${verification.status ?? "(none)"}. A person must resolve it.`,
|
|
807
|
+
nodes: [n.id],
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
return hits;
|
|
811
|
+
},
|
|
812
|
+
"prov-cites-quote": (ont) => {
|
|
813
|
+
const hits = [];
|
|
814
|
+
for (const n of ont.nodes) {
|
|
815
|
+
const { extracted, verification } = pipeline(n);
|
|
816
|
+
if (!(n.type === "Rule" || extracted || verification))
|
|
817
|
+
continue;
|
|
818
|
+
const locs = ont.targets(n.id, "CITES").map((i) => ont.get(i)).filter((l) => l?.type === "SourceLocation");
|
|
819
|
+
const good = locs.filter((l) => l.quote && ont.targets(l.id, "PART_OF").some((s) => ont.get(s)?.type === "Source"));
|
|
820
|
+
if (good.length === 0)
|
|
821
|
+
hits.push({ message: `${n.type} ${L(ont, n.id)} doesn't cite a verbatim quote from a Source.`, nodes: [n.id] });
|
|
822
|
+
}
|
|
823
|
+
return hits;
|
|
824
|
+
},
|
|
825
|
+
"prov-quote-current": (ont, ctx) => {
|
|
826
|
+
const hits = [];
|
|
827
|
+
for (const src of ont.ofType("Source")) {
|
|
828
|
+
if (!src.localPath)
|
|
829
|
+
continue;
|
|
830
|
+
const pages = ctx.pages.get(src.id);
|
|
831
|
+
if (!pages)
|
|
832
|
+
continue;
|
|
833
|
+
if (pages instanceof Error) {
|
|
834
|
+
hits.push({ message: `Can't read ${src.localPath} for Source ${L(ont, src.id)}: ${pages.message}`, nodes: [src.id], severity: "warning" });
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
for (const loc of ont.sources(src.id, "PART_OF").map((i) => ont.get(i))) {
|
|
838
|
+
if (loc?.type !== "SourceLocation" || !loc.quote)
|
|
839
|
+
continue;
|
|
840
|
+
if (Array.isArray(pages)) {
|
|
841
|
+
if (!findQuote(loc.quote, pages).found)
|
|
842
|
+
hits.push({ message: `Quote ${loc.id} no longer appears in ${src.localPath}: "${String(loc.quote).slice(0, 70)}…"`, nodes: [loc.id] });
|
|
843
|
+
continue;
|
|
844
|
+
}
|
|
845
|
+
const found = locate(pages, loc.quote);
|
|
846
|
+
if (!found.length) {
|
|
847
|
+
hits.push({ message: `Quote ${loc.id} no longer appears in ${src.localPath} (was ${loc.locator}, line ${loc.lines}): "${String(loc.quote).slice(0, 70)}…"`, nodes: [loc.id] });
|
|
848
|
+
}
|
|
849
|
+
else if (loc.startLine && !found.some((h) => h.startLine === loc.startLine)) {
|
|
850
|
+
hits.push({ message: `Quote ${loc.id} moved (was line ${loc.lines}, now line ${found[0].startLine}). Update links with: okb source refresh ${src.id}`, nodes: [loc.id], severity: "info" });
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return hits;
|
|
855
|
+
},
|
|
856
|
+
"prov-duplicate-quotes": (ont) => duplicateQuotes(ont),
|
|
857
|
+
};
|
|
858
|
+
/** Near-identical quotes attributed to different sources (also used by `okb drift`). */
|
|
859
|
+
export function duplicateQuotes(ont) {
|
|
860
|
+
const hits = [];
|
|
861
|
+
const locs = ont.ofType("SourceLocation").filter((l) => l.quote);
|
|
862
|
+
const src = (l) => ont.targets(l.id, "PART_OF")[0];
|
|
863
|
+
const buckets = new Map();
|
|
864
|
+
for (const l of locs) {
|
|
865
|
+
const k = String(l.quote).toLowerCase().split(/[^a-z0-9]+/).filter(Boolean).slice(0, 6).join(" ");
|
|
866
|
+
if (!buckets.has(k))
|
|
867
|
+
buckets.set(k, []);
|
|
868
|
+
buckets.get(k).push(l);
|
|
869
|
+
}
|
|
870
|
+
for (const group of buckets.values()) {
|
|
871
|
+
for (let i = 0; i < group.length; i++) {
|
|
872
|
+
for (let j = i + 1; j < group.length; j++) {
|
|
873
|
+
const [a, b] = [group[i], group[j]];
|
|
874
|
+
if (!src(a) || !src(b) || src(a) === src(b))
|
|
875
|
+
continue;
|
|
876
|
+
const sim = similarity(normalize(a.quote), normalize(b.quote));
|
|
877
|
+
if (sim >= DUPLICATE_QUOTE_SIMILARITY) {
|
|
878
|
+
hits.push({ message: `${a.id} (${ont.label(src(a))}) and ${b.id} (${ont.label(src(b))}) are ${sim === 1 ? "identical" : Math.round(sim * 100) + "% similar"}. Possibly copy-pasted; check each is right for its own document.`, nodes: [a.id, b.id] });
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return hits;
|
|
884
|
+
}
|
|
885
|
+
function similarity(a, b) {
|
|
886
|
+
if (a === b)
|
|
887
|
+
return 1;
|
|
888
|
+
const bigrams = (s) => {
|
|
889
|
+
const m = new Map();
|
|
890
|
+
for (let i = 0; i < s.length - 1; i++)
|
|
891
|
+
m.set(s.slice(i, i + 2), (m.get(s.slice(i, i + 2)) ?? 0) + 1);
|
|
892
|
+
return m;
|
|
893
|
+
};
|
|
894
|
+
const A = bigrams(a);
|
|
895
|
+
const B = bigrams(b);
|
|
896
|
+
let inter = 0;
|
|
897
|
+
for (const [k, v] of A)
|
|
898
|
+
inter += Math.min(v, B.get(k) ?? 0);
|
|
899
|
+
return (2 * inter) / Math.max(1, a.length - 1 + b.length - 1);
|
|
900
|
+
}
|
|
901
|
+
export async function loadSourcePages(ont) {
|
|
902
|
+
const pages = new Map();
|
|
903
|
+
for (const s of ont.ofType("Source")) {
|
|
904
|
+
if (!s.localPath)
|
|
905
|
+
continue;
|
|
906
|
+
const p = isAbsolute(s.localPath) ? s.localPath : join(ont.root, s.localPath);
|
|
907
|
+
try {
|
|
908
|
+
if (!existsSync(p))
|
|
909
|
+
pages.set(s.id, new Error("file not found"));
|
|
910
|
+
else if (s.format === "markdown" || /\.(md|markdown)$/i.test(p))
|
|
911
|
+
pages.set(s.id, parseMarkdown(readFileSync(p, "utf8")));
|
|
912
|
+
else
|
|
913
|
+
pages.set(s.id, await loadPages(p));
|
|
914
|
+
}
|
|
915
|
+
catch (e) {
|
|
916
|
+
pages.set(s.id, e);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
return pages;
|
|
920
|
+
}
|
|
921
|
+
export function runChecks(ont, opts = {}, ctx = { pages: new Map() }) {
|
|
922
|
+
const meta = MetaKB.get();
|
|
923
|
+
// A decision about a class also covers that class's subclasses.
|
|
924
|
+
const waivers = ont.ofType("DesignDecision").flatMap((d) => (d.metaRules ?? []).map((r) => {
|
|
925
|
+
const about = new Set();
|
|
926
|
+
for (const a of ont.targets(d.id, "ABOUT")) {
|
|
927
|
+
about.add(a);
|
|
928
|
+
if (ont.get(a)?.type === "Class")
|
|
929
|
+
for (const x of ont.descendants(a))
|
|
930
|
+
about.add(x);
|
|
931
|
+
}
|
|
932
|
+
return { rule: r, decision: d.id, about, any: ont.targets(d.id, "ABOUT").length === 0 };
|
|
933
|
+
}));
|
|
934
|
+
let hidden = 0;
|
|
935
|
+
const findings = [];
|
|
936
|
+
for (const [key, check] of Object.entries(CHECKS)) {
|
|
937
|
+
if (opts.only && !opts.only.includes(key))
|
|
938
|
+
continue;
|
|
939
|
+
const rule = meta.rule(key);
|
|
940
|
+
// Rules for later steps are hidden until you get there, except errors: those always show.
|
|
941
|
+
const later = !opts.all && (rule.fromStep ?? 1) > ont.step;
|
|
942
|
+
let hits;
|
|
943
|
+
try {
|
|
944
|
+
hits = check(ont, ctx);
|
|
945
|
+
}
|
|
946
|
+
catch (e) {
|
|
947
|
+
hits = [{ message: `(internal) check crashed: ${e.message}. Run okb validate after fixing struct-well-formed errors.`, nodes: [], severity: "warning" }];
|
|
948
|
+
}
|
|
949
|
+
for (const h of hits) {
|
|
950
|
+
const severity = h.severity ?? rule.severity;
|
|
951
|
+
if (later && severity !== "error") {
|
|
952
|
+
hidden++;
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
const f = { rule: key, severity, message: h.message, nodes: h.nodes };
|
|
956
|
+
if (severity !== "error") {
|
|
957
|
+
const w = waivers.find((w) => w.rule === key && (w.any || h.nodes.some((n) => w.about.has(n))));
|
|
958
|
+
if (w)
|
|
959
|
+
f.explainedBy = w.decision;
|
|
960
|
+
}
|
|
961
|
+
findings.push(f);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
const order = { error: 0, warning: 1, info: 2 };
|
|
965
|
+
return { findings: findings.sort((a, b) => order[a.severity] - order[b.severity]), hiddenLater: hidden };
|
|
966
|
+
}
|