@ontology-kit/reader 0.0.0-stage → 0.2.0-bootstrap.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 +35 -2
- package/dist/edge-types.d.ts +18 -0
- package/dist/edge-types.js +26 -0
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +3 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +16 -0
- package/dist/layout.d.ts +18 -0
- package/dist/layout.js +116 -0
- package/dist/lock.d.ts +7 -0
- package/dist/lock.js +144 -0
- package/dist/model.d.ts +104 -0
- package/dist/model.js +648 -0
- package/dist/naming.d.ts +25 -0
- package/dist/naming.js +50 -0
- package/dist/registry.d.ts +4 -0
- package/dist/registry.js +39 -0
- package/dist/relationships.d.ts +30 -0
- package/dist/relationships.js +214 -0
- package/dist/types.d.ts +264 -0
- package/dist/types.js +13 -0
- package/package.json +49 -3
package/dist/naming.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How names turn into identities: the folding that makes lookups case- and delimiter-insensitive,
|
|
3
|
+
* the slug an id is built from, and the edge type a relationship slot's name becomes. These are the
|
|
4
|
+
* parts of naming the file format itself depends on. okb keeps the style rules (PascalCase, plurals).
|
|
5
|
+
*/
|
|
6
|
+
/** 'Rosé' -> 'Rose': decompose, then drop the combining marks. */
|
|
7
|
+
export function stripAccents(s) {
|
|
8
|
+
return s.normalize("NFKD").replace(/\p{M}/gu, "");
|
|
9
|
+
}
|
|
10
|
+
function splitWords(name, keepDots = false) {
|
|
11
|
+
const s = name.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2");
|
|
12
|
+
return s.split(keepDots ? /[^A-Za-z0-9.]+/ : /[^A-Za-z0-9]+/).filter(Boolean);
|
|
13
|
+
}
|
|
14
|
+
/** 'RedWine' / 'red_wine' / 'Red wine' -> ['red', 'wine'] */
|
|
15
|
+
export function tokens(name) {
|
|
16
|
+
return splitWords(name).map((t) => t.toLowerCase());
|
|
17
|
+
}
|
|
18
|
+
/** Words with original capitalization (and trailing dots kept, for abbreviation checks). */
|
|
19
|
+
export function rawTokens(name) {
|
|
20
|
+
return splitWords(name, true);
|
|
21
|
+
}
|
|
22
|
+
/** Lowercase letters and digits only, without accents: 'Rosé Wine!' -> 'rosewine'. */
|
|
23
|
+
export function fold(text) {
|
|
24
|
+
return stripAccents(text).toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
25
|
+
}
|
|
26
|
+
const keyCache = new Map();
|
|
27
|
+
/**
|
|
28
|
+
* Case- and delimiter-insensitive identity for uniqueness checks (the fold of a
|
|
29
|
+
* name). Memoized: name lookups compare against every node's name, so the same
|
|
30
|
+
* short strings recur constantly.
|
|
31
|
+
*/
|
|
32
|
+
export function key(name) {
|
|
33
|
+
let k = keyCache.get(name);
|
|
34
|
+
if (k === undefined) {
|
|
35
|
+
k = fold(name);
|
|
36
|
+
keyCache.set(name, k);
|
|
37
|
+
}
|
|
38
|
+
return k;
|
|
39
|
+
}
|
|
40
|
+
/** The part of an id built from a name: 'Red Wine' -> 'red-wine'. */
|
|
41
|
+
export function slug(name) {
|
|
42
|
+
const ts = tokens(stripAccents(name));
|
|
43
|
+
return ts.length ? ts.join("-") : "x";
|
|
44
|
+
}
|
|
45
|
+
/** "a Winery" / "an Author" */
|
|
46
|
+
export const an = (w) => (/^[aeiou]/i.test(w) ? "an " : "a ") + w;
|
|
47
|
+
/** Relationship (edge) type for a slot name: 'goesWellWith' -> 'GOES_WELL_WITH' (the openCypher convention). */
|
|
48
|
+
export function relType(slotName) {
|
|
49
|
+
return tokens(stripAccents(slotName)).join("_").toUpperCase();
|
|
50
|
+
}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Generated by packages/okb/meta-kb/build.ts (pnpm build:meta) from packages/okb/meta-kb/src/format.yaml.
|
|
2
|
+
// Don't edit by hand: change the YAML and rebuild. A test checks this file against the compiled meta-KB.
|
|
3
|
+
export const FORMAT_VERSION = "okb-lpg/3";
|
|
4
|
+
export const NODE_TYPE_NAMES = [
|
|
5
|
+
"Ontology",
|
|
6
|
+
"CompetencyQuestion",
|
|
7
|
+
"Term",
|
|
8
|
+
"ReusedOntology",
|
|
9
|
+
"Class",
|
|
10
|
+
"Slot",
|
|
11
|
+
"Instance",
|
|
12
|
+
"DesignDecision",
|
|
13
|
+
"Source",
|
|
14
|
+
"SourceLocation",
|
|
15
|
+
"Rule",
|
|
16
|
+
];
|
|
17
|
+
export const EDGE_TYPE_NAMES = [
|
|
18
|
+
"IS_A",
|
|
19
|
+
"INSTANCE_OF",
|
|
20
|
+
"HAS_SLOT",
|
|
21
|
+
"RANGE",
|
|
22
|
+
"INVERSE_OF",
|
|
23
|
+
"DISJOINT_WITH",
|
|
24
|
+
"NEEDS",
|
|
25
|
+
"BECAME",
|
|
26
|
+
"ABOUT",
|
|
27
|
+
"CITES",
|
|
28
|
+
"PART_OF",
|
|
29
|
+
"GOVERNS",
|
|
30
|
+
"REFINES",
|
|
31
|
+
"FULFILLS",
|
|
32
|
+
];
|
|
33
|
+
export const PROVENANCE_EDGE_TYPE_NAMES = [
|
|
34
|
+
"CITES",
|
|
35
|
+
"PART_OF",
|
|
36
|
+
"GOVERNS",
|
|
37
|
+
"REFINES",
|
|
38
|
+
"FULFILLS",
|
|
39
|
+
];
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Ontology } from "./model.js";
|
|
2
|
+
import type { EdgePropertyDecl, GraphEdge, RelationshipSlot } from "./types.js";
|
|
3
|
+
/** How one slot's values are stored: the edge type, and whether this slot reads it backwards. */
|
|
4
|
+
export interface LinkSpec {
|
|
5
|
+
type: string;
|
|
6
|
+
reverse: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** An edge's own properties (everything except from/type/to). */
|
|
9
|
+
export declare function edgeProps(e: GraphEdge): Record<string, unknown>;
|
|
10
|
+
export declare class Relationships {
|
|
11
|
+
private readonly ont;
|
|
12
|
+
constructor(ont: Ontology);
|
|
13
|
+
/** The slot that stores a relationship's edges: the slot itself, or the one it's the inverse of. */
|
|
14
|
+
primary(slotId: string): string;
|
|
15
|
+
isPrimary(slotId: string): boolean;
|
|
16
|
+
spec(slotId: string): LinkSpec | null;
|
|
17
|
+
/** Edge types that hold relationship values, mapped to the slot that stores each. */
|
|
18
|
+
edgeTypes(): Map<string, string>;
|
|
19
|
+
/** The nodes `id` is linked to through a relationship slot (either direction). */
|
|
20
|
+
values(id: string, slotId: string): string[];
|
|
21
|
+
/** The stored edge behind one relationship value, whichever end it's read from. */
|
|
22
|
+
edge(id: string, slotId: string, target: string): GraphEdge | undefined;
|
|
23
|
+
/** Relationship slots `id` has any values for (either direction). */
|
|
24
|
+
slotsWithValues(id: string): string[];
|
|
25
|
+
/** The edge properties a relationship's edges can carry (declared on its primary slot). */
|
|
26
|
+
edgeProperties(slotId: string): EdgePropertyDecl[];
|
|
27
|
+
}
|
|
28
|
+
export declare function isRelationship(s: {
|
|
29
|
+
type: string;
|
|
30
|
+
} | undefined): s is RelationshipSlot;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How relationship values are stored. This is the one module that knows it.
|
|
3
|
+
*
|
|
4
|
+
* A slot with valueType "Instance" is a relationship. Its values are edges whose type
|
|
5
|
+
* is the slot's relType, the slot name in UPPER_SNAKE case: (wine)-[:MAKER]->(winery).
|
|
6
|
+
* An edge from a class is a fixed class-level value its members inherit.
|
|
7
|
+
*
|
|
8
|
+
* Inverse slots are one relationship read from both ends, stored once. The slot on the
|
|
9
|
+
* `from` side of INVERSE_OF is the primary (stored) direction; its inverse reads the
|
|
10
|
+
* same edges backwards, so `produces` = incoming MAKER edges. Edge-property
|
|
11
|
+
* declarations live on the primary slot.
|
|
12
|
+
*
|
|
13
|
+
* Everything that creates, retypes, moves or deletes those edges goes through here,
|
|
14
|
+
* so a change to the storage scheme is a change to this file.
|
|
15
|
+
*/
|
|
16
|
+
import { OkbError } from "./errors.js";
|
|
17
|
+
import * as naming from "./naming.js";
|
|
18
|
+
import { isStructuralEdgeType } from "./edge-types.js";
|
|
19
|
+
/** An edge's own properties (everything except from/type/to). */
|
|
20
|
+
export function edgeProps(e) {
|
|
21
|
+
const { from: _f, type: _t, to: _to, ...rest } = e;
|
|
22
|
+
return rest;
|
|
23
|
+
}
|
|
24
|
+
export class Relationships {
|
|
25
|
+
ont;
|
|
26
|
+
constructor(ont) {
|
|
27
|
+
this.ont = ont;
|
|
28
|
+
}
|
|
29
|
+
/** The slot that stores a relationship's edges: the slot itself, or the one it's the inverse of. */
|
|
30
|
+
primary(slotId) {
|
|
31
|
+
return this.ont.sources(slotId, "INVERSE_OF")[0] ?? slotId;
|
|
32
|
+
}
|
|
33
|
+
isPrimary(slotId) {
|
|
34
|
+
return this.primary(slotId) === slotId;
|
|
35
|
+
}
|
|
36
|
+
spec(slotId) {
|
|
37
|
+
const s = this.ont.get(slotId);
|
|
38
|
+
if (s?.type !== "Slot" || s.valueType !== "Instance")
|
|
39
|
+
return null;
|
|
40
|
+
const primary = this.primary(slotId);
|
|
41
|
+
const p = this.ont.get(primary);
|
|
42
|
+
if (p?.type !== "Slot" || !p.relType)
|
|
43
|
+
return null;
|
|
44
|
+
return { type: p.relType, reverse: primary !== slotId };
|
|
45
|
+
}
|
|
46
|
+
/** Edge types that hold relationship values, mapped to the slot that stores each. */
|
|
47
|
+
edgeTypes() {
|
|
48
|
+
const m = new Map();
|
|
49
|
+
for (const s of this.ont.ofType("Slot"))
|
|
50
|
+
if (isRelationship(s) && this.isPrimary(s.id))
|
|
51
|
+
m.set(s.relType, s.id);
|
|
52
|
+
return m;
|
|
53
|
+
}
|
|
54
|
+
/** The nodes `id` is linked to through a relationship slot (either direction). */
|
|
55
|
+
values(id, slotId) {
|
|
56
|
+
const spec = this.spec(slotId);
|
|
57
|
+
if (!spec)
|
|
58
|
+
return [];
|
|
59
|
+
return spec.reverse ? this.ont.sources(id, spec.type) : this.ont.targets(id, spec.type);
|
|
60
|
+
}
|
|
61
|
+
/** The stored edge behind one relationship value, whichever end it's read from. */
|
|
62
|
+
edge(id, slotId, target) {
|
|
63
|
+
const spec = this.spec(slotId);
|
|
64
|
+
if (!spec)
|
|
65
|
+
return undefined;
|
|
66
|
+
const [from, to] = spec.reverse ? [target, id] : [id, target];
|
|
67
|
+
return this.ont.outEdges(from, spec.type).find((e) => e.to === to);
|
|
68
|
+
}
|
|
69
|
+
/** Relationship slots `id` has any values for (either direction). */
|
|
70
|
+
slotsWithValues(id) {
|
|
71
|
+
return this.ont.ofType("Slot").filter((s) => this.values(id, s.id).length > 0).map((s) => s.id);
|
|
72
|
+
}
|
|
73
|
+
/** The edge properties a relationship's edges can carry (declared on its primary slot). */
|
|
74
|
+
edgeProperties(slotId) {
|
|
75
|
+
const p = this.ont.get(this.primary(slotId));
|
|
76
|
+
return (p?.type === "Slot" && p.edgeProperties) || [];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Link `id` to `target` through a relationship slot. There's at most one edge per
|
|
80
|
+
* (from, relationship, to); `props` (edge properties, or `rule`) are merged into it.
|
|
81
|
+
* Returns whether a new edge was created.
|
|
82
|
+
*/
|
|
83
|
+
/** @internal */
|
|
84
|
+
link(id, slotId, target, props = {}) {
|
|
85
|
+
const existing = this.edge(id, slotId, target);
|
|
86
|
+
if (existing) {
|
|
87
|
+
Object.assign(existing, props);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
const spec = this.spec(slotId);
|
|
91
|
+
if (!spec)
|
|
92
|
+
throw new OkbError(`${this.ont.label(slotId)} isn't a relationship.`);
|
|
93
|
+
const [from, to] = spec.reverse ? [target, id] : [id, target];
|
|
94
|
+
this.ont.addEdge(from, spec.type, to, props);
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
/** @internal */
|
|
98
|
+
unlink(id, slotId, target) {
|
|
99
|
+
const spec = this.spec(slotId);
|
|
100
|
+
if (!spec)
|
|
101
|
+
return;
|
|
102
|
+
const [from, to] = spec.reverse ? [target, id] : [id, target];
|
|
103
|
+
this.ont.removeEdges((e) => e.type === spec.type && e.from === from && e.to === to);
|
|
104
|
+
}
|
|
105
|
+
/** The edge type a relationship called `name` would be stored as; refuses structural and taken types. */
|
|
106
|
+
/** @internal */
|
|
107
|
+
typeFor(name, slotId) {
|
|
108
|
+
const t = naming.relType(name);
|
|
109
|
+
if (isStructuralEdgeType(t))
|
|
110
|
+
throw new OkbError(`A relationship called '${name}' would be stored as ${t}, which okb uses for its own structure. Pick another name.`);
|
|
111
|
+
const other = this.ont.ofType("Slot").find((x) => x.id !== slotId && x.relType === t);
|
|
112
|
+
if (other)
|
|
113
|
+
throw new OkbError(`'${name}' would be stored as ${t}, which ${other.name} already uses. Pick another name.`);
|
|
114
|
+
return t;
|
|
115
|
+
}
|
|
116
|
+
/** A slot's value type became Instance: give it an edge type, unless it already has one. */
|
|
117
|
+
/** @internal */
|
|
118
|
+
becomeRelationship(slot) {
|
|
119
|
+
if (slot.relType)
|
|
120
|
+
return;
|
|
121
|
+
slot.relType = this.typeFor(slot.name, slot.id);
|
|
122
|
+
this.ont.touch(slot);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* A relationship's value type changed to a literal type: its stored links and its
|
|
126
|
+
* inverse pairing go. Returns the edge type and how many edges of it there were.
|
|
127
|
+
*/
|
|
128
|
+
/** @internal */
|
|
129
|
+
becomeProperty(slot) {
|
|
130
|
+
const type = slot.relType;
|
|
131
|
+
const links = type ? this.ont.edgesOf(type).length : 0;
|
|
132
|
+
if (type && this.isPrimary(slot.id))
|
|
133
|
+
this.ont.removeEdges((e) => e.type === type);
|
|
134
|
+
this.ont.removeEdges((e) => e.type === "INVERSE_OF" && (e.from === slot.id || e.to === slot.id));
|
|
135
|
+
delete slot.relType;
|
|
136
|
+
this.ont.touch(slot);
|
|
137
|
+
return { type, links };
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Make `b` the inverse of `a`: `a` stays the stored direction, so any edges stored
|
|
141
|
+
* under b's own type move onto a's type, reversed (merging properties where the
|
|
142
|
+
* same link already exists). Returns how many edges moved.
|
|
143
|
+
*/
|
|
144
|
+
/** @internal */
|
|
145
|
+
declareInverse(a, b) {
|
|
146
|
+
const moved = this.ont.edgesOf(b.relType);
|
|
147
|
+
this.ont.removeEdges((e) => e.type === b.relType);
|
|
148
|
+
for (const e of moved) {
|
|
149
|
+
const existing = this.ont.outEdges(e.to, a.relType).find((x) => x.to === e.from);
|
|
150
|
+
if (existing)
|
|
151
|
+
Object.assign(existing, edgeProps(e));
|
|
152
|
+
else
|
|
153
|
+
this.ont.addEdge(e.to, a.relType, e.from, edgeProps(e));
|
|
154
|
+
}
|
|
155
|
+
const declared = a.edgeProperties ?? [];
|
|
156
|
+
const copied = (b.edgeProperties ?? []).filter((p) => !declared.some((q) => q.name === p.name));
|
|
157
|
+
if (copied.length) {
|
|
158
|
+
a.edgeProperties = [...declared, ...copied];
|
|
159
|
+
this.ont.touch(a); // only a stamp for content that changed on the node; the INVERSE_OF edge below is not
|
|
160
|
+
}
|
|
161
|
+
this.ont.addEdge(a.id, "INVERSE_OF", b.id);
|
|
162
|
+
return moved.length;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* A relationship is being renamed: claim the new edge type and, if this slot stores
|
|
166
|
+
* the edges, retype them. Returns the change when stored edges were retyped.
|
|
167
|
+
*/
|
|
168
|
+
/** @internal */
|
|
169
|
+
rename(slot, name) {
|
|
170
|
+
const t = this.typeFor(name, slot.id);
|
|
171
|
+
if (t === slot.relType)
|
|
172
|
+
return null;
|
|
173
|
+
const from = slot.relType;
|
|
174
|
+
slot.relType = t;
|
|
175
|
+
this.ont.touch(slot);
|
|
176
|
+
if (!this.isPrimary(slot.id))
|
|
177
|
+
return null;
|
|
178
|
+
if (from)
|
|
179
|
+
this.ont.retypeEdges(from, t);
|
|
180
|
+
return { from, to: t };
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* A relationship slot is about to be removed. If it stores the edges and has an
|
|
184
|
+
* inverse, the inverse becomes the stored direction and keeps every link;
|
|
185
|
+
* otherwise its stored edges are deleted. Returns what was kept, if anything.
|
|
186
|
+
*/
|
|
187
|
+
/** @internal */
|
|
188
|
+
drop(slot) {
|
|
189
|
+
const type = slot.relType;
|
|
190
|
+
if (!type || !this.isPrimary(slot.id))
|
|
191
|
+
return null;
|
|
192
|
+
const inv = isRelationship(slot) ? this.ont.targets(slot.id, "INVERSE_OF")[0] : undefined;
|
|
193
|
+
if (!inv) {
|
|
194
|
+
this.ont.removeEdges((e) => e.type === type);
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
const invNode = this.ont.require(inv, "Slot");
|
|
198
|
+
if (!isRelationship(invNode))
|
|
199
|
+
throw new OkbError(`${slot.name}'s inverse ${invNode.name} isn't a relationship. Fix it (okb slot set ${invNode.name} --type Instance) or remove the inverse first.`);
|
|
200
|
+
const moved = this.ont.edgesOf(type);
|
|
201
|
+
this.ont.removeEdges((e) => e.type === type || (e.type === "INVERSE_OF" && e.from === slot.id));
|
|
202
|
+
for (const e of moved)
|
|
203
|
+
this.ont.addEdge(e.to, invNode.relType, e.from, edgeProps(e));
|
|
204
|
+
if (slot.edgeProperties?.length && !invNode.edgeProperties?.length) {
|
|
205
|
+
invNode.edgeProperties = slot.edgeProperties;
|
|
206
|
+
this.ont.touch(invNode);
|
|
207
|
+
}
|
|
208
|
+
return { links: moved.length, type: invNode.relType, name: invNode.name };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
export function isRelationship(s) {
|
|
212
|
+
const slot = s;
|
|
213
|
+
return slot?.type === "Slot" && slot.valueType === "Instance" && Boolean(slot.relType);
|
|
214
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for the okb file format (okb-lpg/3). The node and edge registry
|
|
3
|
+
* is meta-kb/src/format.yaml; docs/FORMAT.md describes each property.
|
|
4
|
+
*
|
|
5
|
+
* Files can be edited by hand, so what's loaded is only *expected* to match these
|
|
6
|
+
* types. The struct-well-formed rule reports what doesn't, and code that walks
|
|
7
|
+
* edges should still expect ids that point nowhere (use Ontology.require).
|
|
8
|
+
*/
|
|
9
|
+
/** An untyped graph node: the meta-KB, and okb files before they're checked. */
|
|
10
|
+
export interface GraphNode {
|
|
11
|
+
id: string;
|
|
12
|
+
type: string;
|
|
13
|
+
[prop: string]: any;
|
|
14
|
+
}
|
|
15
|
+
/** An edge. Anything besides from/type/to is an edge property (see OkbEdgeProps). */
|
|
16
|
+
export interface GraphEdge {
|
|
17
|
+
from: string;
|
|
18
|
+
type: string;
|
|
19
|
+
to: string;
|
|
20
|
+
[prop: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
export interface Manifest {
|
|
23
|
+
format: string;
|
|
24
|
+
currentStep: number;
|
|
25
|
+
name?: string;
|
|
26
|
+
created?: string;
|
|
27
|
+
metaKbVersion?: string;
|
|
28
|
+
/** The knowledge base's own release version (`okb release`), semver; absent until the first release. */
|
|
29
|
+
version?: string;
|
|
30
|
+
/** The okb version the KB was built with; `okb check` warns when the installed okb is older, and the CI workflow installs it. */
|
|
31
|
+
okbVersion?: string;
|
|
32
|
+
}
|
|
33
|
+
export interface Conventions {
|
|
34
|
+
classCase: string;
|
|
35
|
+
slotCase: string;
|
|
36
|
+
instanceCase: string | null;
|
|
37
|
+
classNumber: "singular" | "plural";
|
|
38
|
+
slotAffix: "none" | "has-prefix" | "of-suffix";
|
|
39
|
+
/** "chosen" once the user ran `okb convention`; "default" when okb filled it in. */
|
|
40
|
+
source?: "chosen" | "default";
|
|
41
|
+
}
|
|
42
|
+
export declare const VALUE_TYPES: readonly ["String", "Integer", "Float", "Number", "Boolean", "Enumerated", "Instance"];
|
|
43
|
+
export type ValueType = (typeof VALUE_TYPES)[number];
|
|
44
|
+
/** Value types an edge property can have: everything except links to other things. */
|
|
45
|
+
export type LiteralType = Exclude<ValueType, "Instance">;
|
|
46
|
+
/** A stored property value (Instance.values, Class.fixedValues/defaults, Slot.default). */
|
|
47
|
+
export type Literal = string | number | boolean;
|
|
48
|
+
export type StoredValue = Literal | Literal[];
|
|
49
|
+
export declare const DISPOSITIONS: readonly ["undecided", "class", "slot", "instance", "value", "synonym", "out-of-scope"];
|
|
50
|
+
export type Disposition = (typeof DISPOSITIONS)[number];
|
|
51
|
+
export declare const CARDINALITIES: readonly ["single", "multiple"];
|
|
52
|
+
export type Cardinality = (typeof CARDINALITIES)[number];
|
|
53
|
+
export type VerificationStatus = (typeof VERIFICATION_STATUSES)[number];
|
|
54
|
+
export interface Verification {
|
|
55
|
+
status: VerificationStatus;
|
|
56
|
+
checkedAgainst?: string[];
|
|
57
|
+
note?: string;
|
|
58
|
+
/** A link (http or https) to the PR comment, issue or review thread where the reasoning happened. Never a name or a date. */
|
|
59
|
+
ref?: string;
|
|
60
|
+
/** Set by `okb verify --approve`: the status a person approved it from. */
|
|
61
|
+
approvedAfter?: VerificationStatus;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* When a node was made and when something stored on it last changed: UTC to the second
|
|
65
|
+
* (`2026-11-21T14:03:22Z`). Missing on nodes from files written before they existed;
|
|
66
|
+
* read that as unknown, never as zero. Edges carry no stamps (see docs/FORMAT.md).
|
|
67
|
+
*/
|
|
68
|
+
export interface Stamps {
|
|
69
|
+
createdAt?: string;
|
|
70
|
+
updatedAt?: string;
|
|
71
|
+
}
|
|
72
|
+
/** Fields the extraction pipeline adds to nodes drafted from a document. */
|
|
73
|
+
export interface ExtractedFields {
|
|
74
|
+
extracted?: boolean;
|
|
75
|
+
verification?: Verification;
|
|
76
|
+
extractionConfidence?: "verbatim" | "paraphrased" | "corrected";
|
|
77
|
+
_originalDraft?: string;
|
|
78
|
+
}
|
|
79
|
+
export interface OntologyNode extends Stamps {
|
|
80
|
+
type: "Ontology";
|
|
81
|
+
id: string;
|
|
82
|
+
name: string;
|
|
83
|
+
domain: string;
|
|
84
|
+
purpose: string;
|
|
85
|
+
users: string[];
|
|
86
|
+
maintainers: string[];
|
|
87
|
+
outOfScope: string[];
|
|
88
|
+
kind: "application" | "terminological";
|
|
89
|
+
reuseReviewed: boolean;
|
|
90
|
+
conventions?: Conventions;
|
|
91
|
+
}
|
|
92
|
+
export interface CompetencyQuestionNode extends Stamps {
|
|
93
|
+
type: "CompetencyQuestion";
|
|
94
|
+
id: string;
|
|
95
|
+
text: string;
|
|
96
|
+
status?: "draft" | "answerable";
|
|
97
|
+
}
|
|
98
|
+
export interface TermNode extends Stamps {
|
|
99
|
+
type: "Term";
|
|
100
|
+
id: string;
|
|
101
|
+
text: string;
|
|
102
|
+
disposition?: Disposition;
|
|
103
|
+
note?: string;
|
|
104
|
+
}
|
|
105
|
+
export interface ReusedOntologyNode extends Stamps {
|
|
106
|
+
type: "ReusedOntology";
|
|
107
|
+
id: string;
|
|
108
|
+
name: string;
|
|
109
|
+
url?: string;
|
|
110
|
+
decision: "reuse" | "adapt" | "reference" | "rejected";
|
|
111
|
+
notes?: string;
|
|
112
|
+
}
|
|
113
|
+
/** A class's restriction of an inherited slot (Ontology 101's facet overrides). */
|
|
114
|
+
export interface FacetOverride {
|
|
115
|
+
allowedValues?: Literal[];
|
|
116
|
+
minCardinality?: number;
|
|
117
|
+
maxCardinality?: number;
|
|
118
|
+
range?: string[];
|
|
119
|
+
}
|
|
120
|
+
export interface ClassNode extends ExtractedFields, Stamps {
|
|
121
|
+
type: "Class";
|
|
122
|
+
id: string;
|
|
123
|
+
name: string;
|
|
124
|
+
/** Names this had before `okb rename`, oldest first. Identity is the id; this is for people and for mapping old queries. */
|
|
125
|
+
formerNames?: string[];
|
|
126
|
+
description?: string;
|
|
127
|
+
synonyms?: string[];
|
|
128
|
+
abstract?: boolean;
|
|
129
|
+
terminological?: boolean;
|
|
130
|
+
/** Property values every member has. Relationship values are edges from the class. */
|
|
131
|
+
fixedValues?: Record<string, StoredValue>;
|
|
132
|
+
/** Starting values for new instances; they can change them. */
|
|
133
|
+
defaults?: Record<string, StoredValue>;
|
|
134
|
+
facetOverrides?: Record<string, FacetOverride>;
|
|
135
|
+
}
|
|
136
|
+
export interface EdgePropertyDecl {
|
|
137
|
+
name: string;
|
|
138
|
+
valueType: LiteralType;
|
|
139
|
+
allowedValues?: string[];
|
|
140
|
+
required?: boolean;
|
|
141
|
+
description?: string;
|
|
142
|
+
}
|
|
143
|
+
export interface SlotNode extends ExtractedFields, Stamps {
|
|
144
|
+
type: "Slot";
|
|
145
|
+
id: string;
|
|
146
|
+
name: string;
|
|
147
|
+
/** Names this had before `okb rename`, oldest first. Identity is the id; this is for people and for mapping old queries. */
|
|
148
|
+
formerNames?: string[];
|
|
149
|
+
description?: string;
|
|
150
|
+
/** "Instance" makes the slot a relationship (see RelationshipSlot). Missing until declared. */
|
|
151
|
+
valueType?: ValueType;
|
|
152
|
+
allowedValues?: string[];
|
|
153
|
+
cardinality?: Cardinality;
|
|
154
|
+
minCardinality?: number;
|
|
155
|
+
maxCardinality?: number;
|
|
156
|
+
default?: StoredValue;
|
|
157
|
+
/** Relationships only: the edge type its values are stored as (maker -> MAKER). */
|
|
158
|
+
relType?: string;
|
|
159
|
+
/** Relationships only: edge types this slot's links were stored as before a rename, oldest first. Old Cypher queries use these. */
|
|
160
|
+
formerRelTypes?: string[];
|
|
161
|
+
/** Relationships only: properties its edges can carry. */
|
|
162
|
+
edgeProperties?: EdgePropertyDecl[];
|
|
163
|
+
/** The slot applies to every Rule (qualifier values in Rule.values). Rules have no classes and no inheritance. */
|
|
164
|
+
appliesToRules?: boolean;
|
|
165
|
+
}
|
|
166
|
+
/** A slot whose values link to other things; they're stored as edges of type relType. */
|
|
167
|
+
export type RelationshipSlot = SlotNode & {
|
|
168
|
+
valueType: "Instance";
|
|
169
|
+
relType: string;
|
|
170
|
+
};
|
|
171
|
+
export interface InstanceNode extends ExtractedFields, Stamps {
|
|
172
|
+
type: "Instance";
|
|
173
|
+
id: string;
|
|
174
|
+
name: string;
|
|
175
|
+
/** Names this had before `okb rename`, oldest first. Identity is the id; this is for people and for mapping old queries. */
|
|
176
|
+
formerNames?: string[];
|
|
177
|
+
description?: string;
|
|
178
|
+
/** Property values only; relationship values are edges. */
|
|
179
|
+
values?: Record<string, StoredValue>;
|
|
180
|
+
}
|
|
181
|
+
export interface DesignDecisionNode extends ExtractedFields, Stamps {
|
|
182
|
+
type: "DesignDecision";
|
|
183
|
+
id: string;
|
|
184
|
+
title: string;
|
|
185
|
+
decision: string;
|
|
186
|
+
question?: string;
|
|
187
|
+
rationale?: string;
|
|
188
|
+
alternatives?: string[];
|
|
189
|
+
/** Rule keys this decision explains (waives, for warnings and hints). */
|
|
190
|
+
metaRules?: string[];
|
|
191
|
+
date: string;
|
|
192
|
+
}
|
|
193
|
+
export interface SourceNode extends Stamps {
|
|
194
|
+
type: "Source";
|
|
195
|
+
id: string;
|
|
196
|
+
title: string;
|
|
197
|
+
localPath?: string;
|
|
198
|
+
format?: "markdown" | "pdf" | "text";
|
|
199
|
+
url?: string;
|
|
200
|
+
repoUrl?: string;
|
|
201
|
+
/** Markdown frontmatter. */
|
|
202
|
+
meta?: Record<string, unknown>;
|
|
203
|
+
/**
|
|
204
|
+
* The quotable blocks the KB last saw, one record per block (`okb source add`, `okb source refresh --all --record`).
|
|
205
|
+
* `okb source refresh --all` compares the file with it to list new, changed and removed blocks.
|
|
206
|
+
*/
|
|
207
|
+
blocks?: SourceBlock[];
|
|
208
|
+
}
|
|
209
|
+
/** One quotable block of a markdown source: a hash of its text and enough to find it and pair it with an edited version. */
|
|
210
|
+
export interface SourceBlock {
|
|
211
|
+
/** SHA-256 (hex) of the block's plain text with layout, case and punctuation folded away. */
|
|
212
|
+
hash: string;
|
|
213
|
+
kind: string;
|
|
214
|
+
headingPath: string[];
|
|
215
|
+
/** A table row's first cell. */
|
|
216
|
+
rowKey?: string;
|
|
217
|
+
/** The first 120 characters of the block's text. */
|
|
218
|
+
preview: string;
|
|
219
|
+
}
|
|
220
|
+
export interface SourceLocationNode extends Stamps {
|
|
221
|
+
type: "SourceLocation";
|
|
222
|
+
id: string;
|
|
223
|
+
quote: string;
|
|
224
|
+
locator?: string;
|
|
225
|
+
headingPath?: string[];
|
|
226
|
+
startLine?: number;
|
|
227
|
+
endLine?: number;
|
|
228
|
+
lines?: string;
|
|
229
|
+
blockKind?: string;
|
|
230
|
+
sourceLink?: string;
|
|
231
|
+
pageLink?: string;
|
|
232
|
+
page?: number | string;
|
|
233
|
+
}
|
|
234
|
+
export declare const MODALITIES: readonly ["MUST", "MUST_NOT", "SHOULD", "SHOULD_NOT", "MAY"];
|
|
235
|
+
export declare const VERIFICATION_STATUSES: readonly ["SUPPORTED", "OVERREACH", "UNSUPPORTED"];
|
|
236
|
+
export type Modality = (typeof MODALITIES)[number];
|
|
237
|
+
/**
|
|
238
|
+
* A domain rule: a requirement on any implementation, extracted from documentation
|
|
239
|
+
* (docs/EXTRACTION-PIPELINE.md) or written by the ontology's owner.
|
|
240
|
+
*/
|
|
241
|
+
export interface RuleNode extends ExtractedFields, Stamps {
|
|
242
|
+
type: "Rule";
|
|
243
|
+
id: string;
|
|
244
|
+
name: string;
|
|
245
|
+
modality: Modality;
|
|
246
|
+
statement: string;
|
|
247
|
+
/** Written by the ontology owner, not extracted. It still cites a quote and gets verified. */
|
|
248
|
+
authored?: boolean;
|
|
249
|
+
/** Qualifier values for property slots that apply to Rules (Slot.appliesToRules), e.g. condition. Each rule states its own; nothing is inherited through REFINES. */
|
|
250
|
+
values?: Record<string, StoredValue>;
|
|
251
|
+
}
|
|
252
|
+
export type OkbNode = OntologyNode | CompetencyQuestionNode | TermNode | ReusedOntologyNode | ClassNode | SlotNode | InstanceNode | DesignDecisionNode | SourceNode | SourceLocationNode | RuleNode;
|
|
253
|
+
export type NodeType = OkbNode["type"];
|
|
254
|
+
export type NodeOf<T extends NodeType> = Extract<OkbNode, {
|
|
255
|
+
type: T;
|
|
256
|
+
}>;
|
|
257
|
+
export interface Facets {
|
|
258
|
+
valueType?: ValueType;
|
|
259
|
+
allowedValues: Literal[] | null;
|
|
260
|
+
min: number;
|
|
261
|
+
max: number | null;
|
|
262
|
+
range: string[];
|
|
263
|
+
overriddenBy: string[];
|
|
264
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for the okb file format (okb-lpg/3). The node and edge registry
|
|
3
|
+
* is meta-kb/src/format.yaml; docs/FORMAT.md describes each property.
|
|
4
|
+
*
|
|
5
|
+
* Files can be edited by hand, so what's loaded is only *expected* to match these
|
|
6
|
+
* types. The struct-well-formed rule reports what doesn't, and code that walks
|
|
7
|
+
* edges should still expect ids that point nowhere (use Ontology.require).
|
|
8
|
+
*/
|
|
9
|
+
export const VALUE_TYPES = ["String", "Integer", "Float", "Number", "Boolean", "Enumerated", "Instance"];
|
|
10
|
+
export const DISPOSITIONS = ["undecided", "class", "slot", "instance", "value", "synonym", "out-of-scope"];
|
|
11
|
+
export const CARDINALITIES = ["single", "multiple"];
|
|
12
|
+
export const MODALITIES = ["MUST", "MUST_NOT", "SHOULD", "SHOULD_NOT", "MAY"];
|
|
13
|
+
export const VERIFICATION_STATUSES = ["SUPPORTED", "OVERREACH", "UNSUPPORTED"];
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ontology-kit/reader",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.2.0-bootstrap.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Read the okb file format: an ontology folder's typed nodes and edges, loading with migrations, the reserved edge types and relationship values. No CLI, no meta-KB.",
|
|
6
|
+
"homepage": "https://ontology-kit.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/alanbsmith/ontology-kit.git",
|
|
10
|
+
"directory": "packages/reader"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/alanbsmith/ontology-kit/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"ontology",
|
|
17
|
+
"knowledge-graph",
|
|
18
|
+
"knowledge-base",
|
|
19
|
+
"labeled-property-graph",
|
|
20
|
+
"file-format",
|
|
21
|
+
"okb"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=24"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^26.6.6",
|
|
44
|
+
"typescript": "^5.9.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"typecheck": "tsc",
|
|
48
|
+
"test": "node --test \"tests/*.test.ts\"",
|
|
49
|
+
"build": "rm -rf dist types-internal && tsc -p tsconfig.build.json && node ../../scripts/rewrite-dts.ts && tsc -p tsconfig.internal.json && node ../../scripts/rewrite-dts.ts types-internal"
|
|
50
|
+
},
|
|
51
|
+
"types": "./dist/index.d.ts"
|
|
6
52
|
}
|