@ontology-kit/okb 0.1.0 → 0.3.0-alpha.133
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/README.md +1 -1
- package/dist/check.d.ts +43 -0
- package/dist/check.js +102 -0
- package/dist/checks.d.ts +1 -1
- package/dist/checks.js +27 -4
- package/dist/cli.js +11 -705
- package/dist/clidocs.d.ts +0 -2
- package/dist/clidocs.js +5 -27
- package/dist/clock.js +1 -1
- package/dist/command-table.d.ts +144 -0
- package/dist/command-table.js +85 -0
- package/dist/commands/documents.d.ts +3 -0
- package/dist/commands/documents.js +250 -0
- package/dist/commands/getting-started.d.ts +3 -0
- package/dist/commands/getting-started.js +135 -0
- package/dist/commands/housekeeping.d.ts +3 -0
- package/dist/commands/housekeeping.js +203 -0
- package/dist/commands/modeling.d.ts +3 -0
- package/dist/commands/modeling.js +467 -0
- package/dist/commands/planning.d.ts +3 -0
- package/dist/commands/planning.js +251 -0
- package/dist/commands/reviewing.d.ts +3 -0
- package/dist/commands/reviewing.js +234 -0
- package/dist/commands/skills.d.ts +3 -0
- package/dist/commands/skills.js +53 -0
- package/dist/commands.d.ts +10 -22
- package/dist/commands.js +15 -622
- package/dist/cqanswer.d.ts +51 -0
- package/dist/cqanswer.js +76 -0
- package/dist/diff.d.ts +85 -0
- package/dist/diff.js +323 -0
- package/dist/export-changes.d.ts +40 -0
- package/dist/export-changes.js +115 -0
- package/dist/export-csv.d.ts +17 -0
- package/dist/export-csv.js +188 -0
- package/dist/export-eval-cases.d.ts +38 -0
- package/dist/export-eval-cases.js +40 -0
- package/dist/export.d.ts +34 -3
- package/dist/export.js +70 -12
- package/dist/families.d.ts +1 -1
- package/dist/families.js +1 -1
- package/dist/help.d.ts +11 -2
- package/dist/help.js +51 -57
- package/dist/kbdocs.d.ts +18 -0
- package/dist/kbdocs.js +393 -0
- package/dist/metakb.d.ts +1 -1
- package/dist/metakb.js +1 -1
- package/dist/naming.d.ts +2 -2
- package/dist/naming.js +2 -2
- package/dist/ontology-ref.d.ts +20 -0
- package/dist/ontology-ref.js +108 -0
- package/dist/ops/admin.d.ts +2 -2
- package/dist/ops/admin.js +26 -8
- package/dist/ops/classes.d.ts +1 -1
- package/dist/ops/classes.js +15 -7
- package/dist/ops/common.d.ts +12 -2
- package/dist/ops/common.js +3 -0
- package/dist/ops/instances.d.ts +2 -1
- package/dist/ops/instances.js +9 -3
- package/dist/ops/links.d.ts +2 -2
- package/dist/ops/links.js +3 -2
- package/dist/ops/naming.d.ts +1 -1
- package/dist/ops/naming.js +3 -1
- package/dist/ops/scope.d.ts +14 -3
- package/dist/ops/scope.js +38 -4
- package/dist/ops/slots.d.ts +1 -1
- package/dist/ops/slots.js +21 -8
- package/dist/ops/values.d.ts +1 -1
- package/dist/ops/values.js +5 -1
- package/dist/provenance.d.ts +5 -3
- package/dist/provenance.js +61 -19
- package/dist/release.d.ts +52 -0
- package/dist/release.js +337 -0
- package/dist/render.d.ts +88 -1
- package/dist/render.js +265 -3
- package/dist/resync.d.ts +98 -0
- package/dist/resync.js +234 -0
- package/dist/review.d.ts +1 -1
- package/dist/review.js +1 -1
- package/dist/reviewdocs.d.ts +42 -0
- package/dist/reviewdocs.js +263 -0
- package/dist/runner.d.ts +3 -0
- package/dist/runner.js +84 -0
- package/dist/skills.d.ts +19 -3
- package/dist/skills.js +130 -14
- package/dist/status.d.ts +1 -1
- package/dist/status.js +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -1
- package/dist/verifynote.d.ts +19 -0
- package/dist/verifynote.js +93 -0
- package/dist/workflows.d.ts +283 -0
- package/dist/workflows.js +270 -0
- package/meta-kb/data/edges.json +25 -0
- package/meta-kb/data/manifest.json +5 -5
- package/meta-kb/data/nodes.json +46 -9
- package/package.json +9 -4
- package/schema/edge.schema.json +68 -0
- package/schema/edges.schema.json +9 -0
- package/schema/node.schema.json +914 -0
- package/schema/nodes.schema.json +9 -0
- package/schema/okb.schema.json +41 -0
- package/skills/build-domain-kb-from-docs/SKILL.md +21 -3
- package/skills/ontology-coach/SKILL.md +8 -4
- package/skills/ontology-maintainer/SKILL.md +152 -0
- package/skills/ontology-review/SKILL.md +4 -2
- package/skills/ontology-signoff/SKILL.md +182 -0
- package/templates/okb-check.yml +148 -0
- package/templates/pull_request_template.md +16 -0
package/dist/help.js
CHANGED
|
@@ -1,57 +1,51 @@
|
|
|
1
|
-
/** The
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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)`;
|
|
1
|
+
/** The first sentence of a description. */
|
|
2
|
+
export function summary(text) {
|
|
3
|
+
return text.split(/(?<=\.)\s+(?=[A-Z`])/)[0].replace(/`/g, "").replace(/\.$/, "");
|
|
4
|
+
}
|
|
5
|
+
const MAX_FLAGS = 4;
|
|
6
|
+
/** A usage wider than this puts its summary on the next line. */
|
|
7
|
+
const MAX_USAGE = 48;
|
|
8
|
+
/** `okb init <folder> [--name]`: a single-form command shows its arguments, from the record; several forms show their subcommands. */
|
|
9
|
+
function usageOf(cmd) {
|
|
10
|
+
const forms = cmd.forms.filter((f) => !f.sub?.startsWith("--") && !f.hidden);
|
|
11
|
+
const subs = forms.filter((f) => f.sub).map((f) => f.sub);
|
|
12
|
+
const bare = forms.find((f) => !f.sub);
|
|
13
|
+
const rows = (bare?.args ?? []).filter((a) => a[0] !== "--json");
|
|
14
|
+
const words = rows.filter((a) => !a[0].startsWith("--")).map((a) => a[0]);
|
|
15
|
+
if (subs.length) {
|
|
16
|
+
// A bare form that needs an argument (`okb release <version>`) is not optional: show it as one of the alternatives.
|
|
17
|
+
if (bare && (bare.min ?? 0) > 0)
|
|
18
|
+
return `${cmd.name} ${[words.join(" "), ...subs].join("|")}`;
|
|
19
|
+
return `${cmd.name} ${bare ? `[${subs.join("|")}]` : subs.join("|")}`;
|
|
20
|
+
}
|
|
21
|
+
const flags = rows.filter((a) => a[0].startsWith("--"));
|
|
22
|
+
const shown = flags.length > MAX_FLAGS ? ["[options]"] : flags.map((a) => optionUsage(a, bare?.require));
|
|
23
|
+
return [cmd.name, ...words, ...shown].join(" ");
|
|
24
|
+
}
|
|
25
|
+
/** A required option shows its operand without brackets (`--out <folder>`); an optional one stays in brackets. */
|
|
26
|
+
export function optionUsage([name, type], required = []) {
|
|
27
|
+
if (!required.includes(name.replace(/^--/, "")))
|
|
28
|
+
return `[${name}]`;
|
|
29
|
+
return `${name} <${/^[\w-]+$/.test(type) ? type : "value"}>`;
|
|
30
|
+
}
|
|
31
|
+
/** One line per command, grouped by what you are trying to do: its name, its subcommands, and what it does. */
|
|
32
|
+
export function renderHelp(commands) {
|
|
33
|
+
const rows = [];
|
|
34
|
+
for (const cmd of commands) {
|
|
35
|
+
if (cmd.hidden)
|
|
36
|
+
continue;
|
|
37
|
+
rows.push({ group: cmd.group, usage: `okb ${usageOf(cmd)}`, what: cmd.summary ?? summary(cmd.description) });
|
|
38
|
+
}
|
|
39
|
+
const width = Math.max(...rows.filter((r) => r.usage.length <= MAX_USAGE).map((r) => r.usage.length)) + 3;
|
|
40
|
+
const lines = ["okb: build an ontology step by step (Ontology 101 method)"];
|
|
41
|
+
let group = "";
|
|
42
|
+
for (const r of rows) {
|
|
43
|
+
if (r.group !== group) {
|
|
44
|
+
lines.push("", r.group);
|
|
45
|
+
group = r.group;
|
|
46
|
+
}
|
|
47
|
+
lines.push(r.usage.length > MAX_USAGE ? ` ${r.usage}\n ${"".padEnd(width)}${r.what}` : ` ${r.usage.padEnd(width)}${r.what}`);
|
|
48
|
+
}
|
|
49
|
+
lines.push("", "Start with: okb init <folder> --name \"My Ontology\", then okb status and okb step.", "Learn a concept or rule: okb explain <topic>.", "Every command, with examples and options: docs/CLI.md, or https://ontology-kit.dev/reference/cli-reference/", "", "Names: when a class and a slot share a name, say which: class:Genre / slot:genre", "Global options: -C <folder> (default: current folder) · --json (machine-readable output where supported)");
|
|
50
|
+
return lines.join("\n");
|
|
51
|
+
}
|
package/dist/kbdocs.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Ontology } from "@ontology-kit/reader";
|
|
2
|
+
export type DocFiles = Record<string, string>;
|
|
3
|
+
export declare const BANNER = "<!-- Generated by okb docs. Do not edit: change the knowledge base and run okb docs again. -->";
|
|
4
|
+
/** Every page for the ontology: file name (relative to the docs folder) → Markdown. */
|
|
5
|
+
export declare function kbDocs(ont: Ontology): DocFiles;
|
|
6
|
+
/** Write the pages into `dir`, removing pages an earlier run wrote (they carry the banner) that no longer exist. Other files stay. Returns the file names. */
|
|
7
|
+
export declare function writeKbDocs(ont: Ontology, dir: string): string[];
|
|
8
|
+
/**
|
|
9
|
+
* Write pages into `dir` (creating folders) and leave every other file alone. A page never replaces a file
|
|
10
|
+
* okb didn't write (no banner): that file is named in the error and nothing is written. Returns the file names.
|
|
11
|
+
*/
|
|
12
|
+
export declare function writePages(dir: string, files: DocFiles): string[];
|
|
13
|
+
/**
|
|
14
|
+
* Write a changed-only view into `dir`, removing pages an earlier run wrote there (they carry the banner)
|
|
15
|
+
* that this view doesn't include, so a folder reused for a second `--changed` doesn't keep the first one's pages.
|
|
16
|
+
* `rules-in-context.md` is a separate view of the same folder and stays.
|
|
17
|
+
*/
|
|
18
|
+
export declare function writeChangedPages(dir: string, files: DocFiles): string[];
|
package/dist/kbdocs.js
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `okb docs`: reference pages for a domain ontology, so people can read its structure and rules
|
|
3
|
+
* without the CLI. `kbDocs` is a pure function from a loaded `Ontology` to a map of file name to
|
|
4
|
+
* Markdown (no disk, no clock), so the same ontology always gives the same pages and they can be
|
|
5
|
+
* committed and checked for drift. `writeKbDocs` writes them.
|
|
6
|
+
*
|
|
7
|
+
* Layout: `index.md` (scope, questions, class tree, decisions), `classes/<anchor>.md` (one per
|
|
8
|
+
* class), `rules.md`, `sources.md`. Every item gets an `<a id>` from its node id with `.` replaced
|
|
9
|
+
* by `-` (`rule.breve-uses` → `rule-breve-uses`), the scheme gendocs.ts uses for the meta-KB docs.
|
|
10
|
+
* Links are relative, so they work on GitHub and in a content collection that keeps the folder.
|
|
11
|
+
*/
|
|
12
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { dirname, join } from "node:path";
|
|
14
|
+
import { OkbError, nameOrText } from "@ontology-kit/reader";
|
|
15
|
+
import { anchorId } from "./site.js";
|
|
16
|
+
export const BANNER = "<!-- Generated by okb docs. Do not edit: change the knowledge base and run okb docs again. -->";
|
|
17
|
+
const byText = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
18
|
+
const byName = (a, b) => byText((nameOrText(a) ?? a.id).toLowerCase(), (nameOrText(b) ?? b.id).toLowerCase()) || byText(a.id, b.id);
|
|
19
|
+
const anchor = (id) => `<a id="${anchorId(id)}"></a>`;
|
|
20
|
+
const cell = (s) => s.replace(/\|/g, "\\|").replace(/\s*\n\s*/g, " ");
|
|
21
|
+
const oneLine = (s) => s.replace(/\s*\n\s*/g, " ").trim();
|
|
22
|
+
const plural = (n, w) => `${n} ${w}${n === 1 ? "" : "s"}`;
|
|
23
|
+
/** Where a page lives, relative to the docs root. */
|
|
24
|
+
const classFile = (id) => `classes/${anchorId(id)}.md`;
|
|
25
|
+
const HERE = { index: "", class: "../", rules: "", sources: "" };
|
|
26
|
+
class Links {
|
|
27
|
+
ont;
|
|
28
|
+
page;
|
|
29
|
+
root;
|
|
30
|
+
constructor(ont, page) {
|
|
31
|
+
this.ont = ont;
|
|
32
|
+
this.page = page;
|
|
33
|
+
this.root = HERE[page];
|
|
34
|
+
}
|
|
35
|
+
index = () => `${this.root}index.md`;
|
|
36
|
+
rules = (id) => `${this.root}rules.md${id ? `#${anchorId(id)}` : ""}`;
|
|
37
|
+
sources = (id) => `${this.root}sources.md${id ? `#${anchorId(id)}` : ""}`;
|
|
38
|
+
/** A class page, from this page: siblings in classes/ link without the folder. */
|
|
39
|
+
cls = (id) => (this.page === "class" ? `${anchorId(id)}.md` : classFile(id));
|
|
40
|
+
/** A link to a node's page, or its plain name when it has none. */
|
|
41
|
+
node(id) {
|
|
42
|
+
const n = this.ont.get(id);
|
|
43
|
+
if (!n)
|
|
44
|
+
return `\`${id}\``;
|
|
45
|
+
const name = nameOrText(n) ?? id;
|
|
46
|
+
if (n.type === "Class")
|
|
47
|
+
return `[${name}](${this.cls(id)})`;
|
|
48
|
+
if (n.type === "Instance") {
|
|
49
|
+
const home = [...this.ont.targets(id, "INSTANCE_OF")].sort(byText)[0];
|
|
50
|
+
return home && this.ont.get(home)?.type === "Class" ? `[${name}](${this.cls(home)}#${anchorId(id)})` : name;
|
|
51
|
+
}
|
|
52
|
+
if (n.type === "Rule")
|
|
53
|
+
return `[${name}](${this.rules(id)})`;
|
|
54
|
+
return name;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const slotName = (ont, id) => nameOrText(ont.require(id)) ?? id;
|
|
58
|
+
function valueText(ont, l, v) {
|
|
59
|
+
if (typeof v === "string" && ont.get(v))
|
|
60
|
+
return l.node(v);
|
|
61
|
+
return String(v);
|
|
62
|
+
}
|
|
63
|
+
function listValues(ont, l, vs) {
|
|
64
|
+
return vs.map((v) => valueText(ont, l, v)).join(", ");
|
|
65
|
+
}
|
|
66
|
+
/** The quote behind a rule or other node, as links to the sources page (and the source's own link when it has one). */
|
|
67
|
+
function quoteLines(ont, l, nodeId) {
|
|
68
|
+
const locs = ont.targets(nodeId, "CITES").map((id) => ont.get(id)).filter((n) => n?.type === "SourceLocation");
|
|
69
|
+
if (!locs.length)
|
|
70
|
+
return ["- Source: none cited."];
|
|
71
|
+
return locs.flatMap((loc) => {
|
|
72
|
+
const where = loc.locator ?? loc.id;
|
|
73
|
+
const ext = loc.sourceLink ?? loc.pageLink;
|
|
74
|
+
return [`- Source: [${where}${loc.lines ? `, line ${loc.lines}` : loc.page !== undefined ? `, page ${loc.page}` : ""}](${l.sources(loc.id)})${ext ? ` ([original](${ext}))` : ""}`, ` > ${oneLine(loc.quote)}`];
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function ruleBlock(ont, l, rule, level, opts = {}) {
|
|
78
|
+
const out = [anchor(rule.id), `${"#".repeat(level)} ${rule.name}`, "", `**${rule.modality.replace("_", " ")}.** ${rule.statement}`, ""];
|
|
79
|
+
const bullets = [];
|
|
80
|
+
const governs = ont.targets(rule.id, "GOVERNS");
|
|
81
|
+
if (opts.governs && governs.length)
|
|
82
|
+
bullets.push(`- Governs: ${governs.map((g) => l.node(g)).join(", ")}`);
|
|
83
|
+
const v = rule.verification?.status;
|
|
84
|
+
bullets.push(`- Verification: ${v ? v.toLowerCase() : "not verified"}${rule.authored ? " (written by the owner)" : ""}`);
|
|
85
|
+
for (const [slotId, val] of Object.entries(rule.values ?? {}))
|
|
86
|
+
bullets.push(`- ${slotName(ont, slotId)}: ${[val].flat().join(", ")}`);
|
|
87
|
+
const refines = ont.targets(rule.id, "REFINES");
|
|
88
|
+
if (refines.length)
|
|
89
|
+
bullets.push(`- Refines: ${refines.map((r) => l.node(r)).join(", ")}`);
|
|
90
|
+
bullets.push(...quoteLines(ont, l, rule.id));
|
|
91
|
+
return [...out, ...bullets, ""];
|
|
92
|
+
}
|
|
93
|
+
function rulesGoverning(ont, classId) {
|
|
94
|
+
return ont.ofType("Rule").filter((r) => ont.targets(r.id, "GOVERNS").includes(classId));
|
|
95
|
+
}
|
|
96
|
+
function decisionsAbout(ont, id) {
|
|
97
|
+
return ont.ofType("DesignDecision").filter((d) => ont.targets(d.id, "ABOUT").includes(id)).sort(byName);
|
|
98
|
+
}
|
|
99
|
+
function decisionBlock(d, level) {
|
|
100
|
+
const out = [anchor(d.id), `${"#".repeat(level)} ${d.title}`, "", `**Decision.** ${d.decision}`, ""];
|
|
101
|
+
if (d.question)
|
|
102
|
+
out.push(`**Question.** ${d.question}`, "");
|
|
103
|
+
if (d.rationale)
|
|
104
|
+
out.push(`**Why.** ${d.rationale}`, "");
|
|
105
|
+
if (d.alternatives?.length)
|
|
106
|
+
out.push(`**Alternatives considered.** ${d.alternatives.join("; ")}`, "");
|
|
107
|
+
if (d.metaRules?.length)
|
|
108
|
+
out.push(`**Explains the rules:** ${d.metaRules.map((r) => `\`${r}\``).join(", ")}`, "");
|
|
109
|
+
out.push(`Date: ${d.date}`, "");
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
function classTree(ont, l, ids, seen, depth = 0) {
|
|
113
|
+
const out = [];
|
|
114
|
+
for (const id of ids.map((i) => ont.require(i, "Class")).sort(byName).map((c) => c.id)) {
|
|
115
|
+
const again = seen.has(id);
|
|
116
|
+
out.push(`${" ".repeat(depth)}- ${l.node(id)}${again ? " (see above)" : ""}`);
|
|
117
|
+
if (again)
|
|
118
|
+
continue;
|
|
119
|
+
seen.add(id);
|
|
120
|
+
out.push(...classTree(ont, l, ont.children(id), seen, depth + 1));
|
|
121
|
+
}
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
function indexPage(ont) {
|
|
125
|
+
const l = new Links(ont, "index");
|
|
126
|
+
const m = ont.meta;
|
|
127
|
+
const out = [BANNER, "", `# ${m.name}`, ""];
|
|
128
|
+
if (m.purpose)
|
|
129
|
+
out.push(m.purpose, "");
|
|
130
|
+
out.push("## Scope", "");
|
|
131
|
+
const row = (k, v) => {
|
|
132
|
+
const text = Array.isArray(v) ? v.join(", ") : v;
|
|
133
|
+
if (text)
|
|
134
|
+
out.push(`- **${k}:** ${text}`);
|
|
135
|
+
};
|
|
136
|
+
row("Domain", m.domain);
|
|
137
|
+
row("Users", m.users);
|
|
138
|
+
row("Maintainers", m.maintainers);
|
|
139
|
+
row("Out of scope", m.outOfScope);
|
|
140
|
+
row("Kind", m.kind);
|
|
141
|
+
if (ont.manifest.version)
|
|
142
|
+
row("Version", ont.manifest.version);
|
|
143
|
+
out.push("");
|
|
144
|
+
const cqs = ont.numbered("CompetencyQuestion");
|
|
145
|
+
if (cqs.length) {
|
|
146
|
+
out.push("## Competency questions", "");
|
|
147
|
+
cqs.forEach((q, i) => out.push(`${i + 1}. ${nameOrText(q)}`));
|
|
148
|
+
out.push("");
|
|
149
|
+
}
|
|
150
|
+
const rules = ont.ofType("Rule").length;
|
|
151
|
+
const sources = ont.ofType("Source").length;
|
|
152
|
+
out.push("## Classes", "");
|
|
153
|
+
const seen = new Set();
|
|
154
|
+
out.push(...classTree(ont, l, ont.roots(), seen));
|
|
155
|
+
const orphans = ont.ofType("Class").filter((c) => !seen.has(c.id)).map((c) => c.id);
|
|
156
|
+
if (orphans.length)
|
|
157
|
+
out.push(...classTree(ont, l, orphans, seen));
|
|
158
|
+
if (!ont.ofType("Class").length)
|
|
159
|
+
out.push("No classes yet.");
|
|
160
|
+
out.push("");
|
|
161
|
+
const decisions = ont.numbered("DesignDecision");
|
|
162
|
+
if (decisions.length) {
|
|
163
|
+
out.push("## Design decisions", "");
|
|
164
|
+
for (const d of decisions) {
|
|
165
|
+
const about = ont.targets(d.id, "ABOUT").filter((a) => ont.get(a)?.type === "Class");
|
|
166
|
+
out.push(`- ${d.title}: ${d.decision}${about.length ? ` (about ${about.map((a) => l.node(a)).join(", ")})` : ""}`);
|
|
167
|
+
}
|
|
168
|
+
out.push("");
|
|
169
|
+
}
|
|
170
|
+
out.push("## More", "", `- [Rules](rules.md): ${plural(rules, "rule")}`, `- [Sources](sources.md): ${plural(sources, "source")}`, "");
|
|
171
|
+
return out.join("\n");
|
|
172
|
+
}
|
|
173
|
+
function classPage(ont, c) {
|
|
174
|
+
const l = new Links(ont, "class");
|
|
175
|
+
const out = [BANNER, "", anchor(c.id), `# ${c.name}`, ""];
|
|
176
|
+
out.push(`[Index](${l.index()}) · [Rules](${l.rules()}) · [Sources](${l.sources()})`, "");
|
|
177
|
+
if (c.description)
|
|
178
|
+
out.push(c.description, "");
|
|
179
|
+
const bullets = [];
|
|
180
|
+
if (c.synonyms?.length)
|
|
181
|
+
bullets.push(`- Also called: ${c.synonyms.join(", ")}`);
|
|
182
|
+
if (c.formerNames?.length)
|
|
183
|
+
bullets.push(`- Formerly: ${c.formerNames.join(", ")}`);
|
|
184
|
+
if (c.abstract)
|
|
185
|
+
bullets.push("- Abstract: it has no members of its own, only its subclasses do.");
|
|
186
|
+
const parents = ont.parents(c.id).sort(byText);
|
|
187
|
+
if (parents.length)
|
|
188
|
+
bullets.push(`- Is a: ${parents.map((p) => l.node(p)).join(", ")}`);
|
|
189
|
+
const kids = ont.children(c.id).sort(byText);
|
|
190
|
+
if (kids.length)
|
|
191
|
+
bullets.push(`- Subclasses: ${kids.map((k) => l.node(k)).join(", ")}`);
|
|
192
|
+
const disjoint = [...ont.targets(c.id, "DISJOINT_WITH"), ...ont.sources(c.id, "DISJOINT_WITH")].sort(byText);
|
|
193
|
+
if (disjoint.length)
|
|
194
|
+
bullets.push(`- Disjoint with: ${disjoint.map((d) => l.node(d)).join(", ")}`);
|
|
195
|
+
if (bullets.length)
|
|
196
|
+
out.push(...bullets, "");
|
|
197
|
+
// Slots: own first, then inherited from the nearest ancestors.
|
|
198
|
+
const nearest = [...ont.ancestors(c.id)]; // nearest first: it decides which class's value applies
|
|
199
|
+
const ancestors = [...nearest].sort(byText);
|
|
200
|
+
const context = [c.id, ...nearest];
|
|
201
|
+
const slotIds = ont.applicableSlots(c.id);
|
|
202
|
+
if (slotIds.length) {
|
|
203
|
+
out.push("## Slots", "", "| Slot | Kind | From | Type | Allowed values | Count | Class value |", "| --- | --- | --- | --- | --- | --- | --- |");
|
|
204
|
+
const rows = slotIds.map((sid) => ({ sid, s: ont.require(sid, "Slot") })).sort((a, b) => Number(ont.ownSlots(c.id).includes(b.sid)) - Number(ont.ownSlots(c.id).includes(a.sid)) || byName(a.s, b.s));
|
|
205
|
+
for (const { sid, s } of rows) {
|
|
206
|
+
const own = ont.ownSlots(c.id).includes(sid);
|
|
207
|
+
const from = own ? "own" : "inherited from " + ont.domain(sid).filter((d) => ancestors.includes(d)).sort(byText).map((d) => l.node(d)).join(", ");
|
|
208
|
+
const f = ont.effectiveFacets(sid, context);
|
|
209
|
+
const rel = f.valueType === "Instance";
|
|
210
|
+
const type = rel ? `link to ${f.range.length ? f.range.map((r) => l.node(r)).join(" or ") : "anything"}` : (f.valueType ?? "unset");
|
|
211
|
+
const count = `${f.min}..${f.max ?? "*"}`;
|
|
212
|
+
const note = f.overriddenBy.length ? ` (narrowed by ${f.overriddenBy.map((o) => l.node(o)).join(", ")})` : "";
|
|
213
|
+
const fixed = ont.statedValues(c.id, sid);
|
|
214
|
+
const inheritedFixed = fixed.length ? null : ont.inheritedFixed(sid, ancestors);
|
|
215
|
+
const classValue = fixed.length ? listValues(ont, l, fixed) : inheritedFixed ? `${listValues(ont, l, inheritedFixed.values)} (from ${l.node(inheritedFixed.cls)})` : "";
|
|
216
|
+
out.push(`| ${cell(`${anchor(`${c.id}.${sid}`)}${s.name}`)} | ${rel ? "relationship" : "property"} | ${cell(from)} | ${cell(type)} | ${cell(f.allowedValues ? f.allowedValues.join(", ") + note : note.trim())} | ${count} | ${cell(classValue)} |`);
|
|
217
|
+
}
|
|
218
|
+
out.push("");
|
|
219
|
+
const described = rows.filter(({ s }) => s.description);
|
|
220
|
+
if (described.length) {
|
|
221
|
+
for (const { s } of described)
|
|
222
|
+
out.push(`- **${s.name}:** ${s.description}`);
|
|
223
|
+
out.push("");
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// Rules: this class's own, then those inherited from ancestors.
|
|
227
|
+
const own = rulesGoverning(ont, c.id).sort(byName);
|
|
228
|
+
const inherited = ancestors.flatMap((a) => rulesGoverning(ont, a).map((r) => ({ r, a }))).sort((x, y) => byName(x.r, y.r));
|
|
229
|
+
if (own.length || inherited.length) {
|
|
230
|
+
out.push("## Rules", "");
|
|
231
|
+
for (const r of own)
|
|
232
|
+
out.push(...ruleSummary(ont, l, r));
|
|
233
|
+
for (const { r, a } of inherited)
|
|
234
|
+
out.push(...ruleSummary(ont, l, r, a));
|
|
235
|
+
}
|
|
236
|
+
const members = ont.ofType("Instance").filter((i) => ont.targets(i.id, "INSTANCE_OF").includes(c.id)).sort(byName);
|
|
237
|
+
if (members.length) {
|
|
238
|
+
out.push("## Instances", "");
|
|
239
|
+
for (const i of members) {
|
|
240
|
+
const parts = [];
|
|
241
|
+
for (const sid of ont.applicableSlots(i.id)) {
|
|
242
|
+
const vals = ont.statedValues(i.id, sid);
|
|
243
|
+
if (vals.length)
|
|
244
|
+
parts.push(`${slotName(ont, sid)}: ${listValues(ont, l, vals)}`);
|
|
245
|
+
}
|
|
246
|
+
out.push(`- ${anchor(i.id)}**${i.name}**${i.description ? `. ${i.description}` : ""}${parts.length ? ` (${parts.join("; ")})` : ""}`);
|
|
247
|
+
}
|
|
248
|
+
out.push("");
|
|
249
|
+
}
|
|
250
|
+
const decisions = decisionsAbout(ont, c.id);
|
|
251
|
+
if (decisions.length) {
|
|
252
|
+
out.push("## Decisions", "");
|
|
253
|
+
for (const d of decisions)
|
|
254
|
+
out.push(...decisionBlock(d, 3));
|
|
255
|
+
}
|
|
256
|
+
return out.join("\n").replace(/\n{3,}/g, "\n\n").trimEnd() + "\n";
|
|
257
|
+
}
|
|
258
|
+
/** A rule on a class page: modality, statement and the link to its quote. */
|
|
259
|
+
function ruleSummary(ont, l, rule, from) {
|
|
260
|
+
const loc = ont.targets(rule.id, "CITES").map((id) => ont.get(id)).find((n) => n?.type === "SourceLocation");
|
|
261
|
+
const quote = loc ? ` Source: [${loc.locator ?? loc.id}${loc.lines ? `, line ${loc.lines}` : ""}](${l.sources(loc.id)}).` : " Source: none cited.";
|
|
262
|
+
return [`- [${rule.name}](${l.rules(rule.id)}) (${from ? `inherited from ${l.node(from)}, ` : ""}${rule.modality.replace("_", " ")}): ${rule.statement}${quote}`, ...(loc ? [` > ${oneLine(loc.quote)}`] : []), ""];
|
|
263
|
+
}
|
|
264
|
+
function rulesPage(ont) {
|
|
265
|
+
const l = new Links(ont, "rules");
|
|
266
|
+
const rules = ont.ofType("Rule").sort(byName);
|
|
267
|
+
const out = [BANNER, "", "# Rules", "", `[Index](index.md) · [Sources](sources.md)`, ""];
|
|
268
|
+
if (!rules.length)
|
|
269
|
+
return [...out, "This knowledge base has no rules yet.", ""].join("\n");
|
|
270
|
+
out.push(`${plural(rules.length, "rule")}. Each one links to the quote it rests on.`, "");
|
|
271
|
+
for (const r of rules)
|
|
272
|
+
out.push(...ruleBlock(ont, l, r, 2, { governs: true }));
|
|
273
|
+
return out.join("\n").trimEnd() + "\n";
|
|
274
|
+
}
|
|
275
|
+
function sourcesPage(ont) {
|
|
276
|
+
const l = new Links(ont, "sources");
|
|
277
|
+
const out = [BANNER, "", "# Sources", "", `[Index](index.md) · [Rules](rules.md)`, ""];
|
|
278
|
+
const sources = ont.ofType("Source").sort(byName);
|
|
279
|
+
if (!sources.length)
|
|
280
|
+
return [...out, "This knowledge base has no registered sources yet.", ""].join("\n");
|
|
281
|
+
const locsOf = (s) => ont.sources(s.id, "PART_OF").map((id) => ont.get(id)).filter((n) => n?.type === "SourceLocation")
|
|
282
|
+
.sort((a, b) => (a.startLine ?? Infinity) - (b.startLine ?? Infinity) || byText(a.id, b.id));
|
|
283
|
+
for (const s of sources) {
|
|
284
|
+
out.push(anchor(s.id), `## ${s.title}`, "");
|
|
285
|
+
const meta = [];
|
|
286
|
+
if (s.localPath)
|
|
287
|
+
meta.push(`- File: \`${s.localPath}\``);
|
|
288
|
+
if (s.format)
|
|
289
|
+
meta.push(`- Format: ${s.format}`);
|
|
290
|
+
if (s.url)
|
|
291
|
+
meta.push(`- URL: <${s.url}>`);
|
|
292
|
+
if (s.repoUrl)
|
|
293
|
+
meta.push(`- Repository: <${s.repoUrl}>`);
|
|
294
|
+
if (meta.length)
|
|
295
|
+
out.push(...meta, "");
|
|
296
|
+
for (const loc of locsOf(s)) {
|
|
297
|
+
out.push(anchor(loc.id), `### ${loc.locator ?? loc.id}`, "");
|
|
298
|
+
const ext = loc.sourceLink ?? loc.pageLink;
|
|
299
|
+
out.push(`> ${oneLine(loc.quote)}`, "");
|
|
300
|
+
const facts = [loc.lines ? `line ${loc.lines}` : null, loc.page !== undefined ? `page ${loc.page}` : null, loc.blockKind ?? null].filter(Boolean).join(", ");
|
|
301
|
+
if (facts || ext)
|
|
302
|
+
out.push(`${facts}${ext ? `${facts ? " · " : ""}[original](${ext})` : ""}`, "");
|
|
303
|
+
const citing = ont.sources(loc.id, "CITES").map((id) => ont.require(id)).sort(byName);
|
|
304
|
+
if (citing.length)
|
|
305
|
+
out.push(`Cited by: ${citing.map((n) => (n.type === "Rule" ? `rule ${l.node(n.id)}` : `${n.type.toLowerCase()} ${l.node(n.id)}`)).join(", ")}`, "");
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// Quotes whose source is missing from the file still need a landing place for rule links.
|
|
309
|
+
const loose = ont.ofType("SourceLocation").filter((loc) => !ont.targets(loc.id, "PART_OF").some((id) => ont.get(id)?.type === "Source"));
|
|
310
|
+
if (loose.length) {
|
|
311
|
+
out.push("## Quotes without a source", "");
|
|
312
|
+
for (const loc of loose.sort(byName))
|
|
313
|
+
out.push(anchor(loc.id), `### ${loc.locator ?? loc.id}`, "", `> ${oneLine(loc.quote)}`, "");
|
|
314
|
+
}
|
|
315
|
+
return out.join("\n").trimEnd() + "\n";
|
|
316
|
+
}
|
|
317
|
+
/** Every page for the ontology: file name (relative to the docs folder) → Markdown. */
|
|
318
|
+
export function kbDocs(ont) {
|
|
319
|
+
const files = {
|
|
320
|
+
"index.md": indexPage(ont),
|
|
321
|
+
"rules.md": rulesPage(ont),
|
|
322
|
+
"sources.md": sourcesPage(ont),
|
|
323
|
+
};
|
|
324
|
+
for (const c of ont.ofType("Class"))
|
|
325
|
+
files[classFile(c.id)] = classPage(ont, c);
|
|
326
|
+
return files;
|
|
327
|
+
}
|
|
328
|
+
/** Write the pages into `dir`, removing pages an earlier run wrote (they carry the banner) that no longer exist. Other files stay. Returns the file names. */
|
|
329
|
+
export function writeKbDocs(ont, dir) {
|
|
330
|
+
const files = kbDocs(ont);
|
|
331
|
+
return replacePages(dir, files, (old) => !(old in files));
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Write pages into `dir` (creating folders) and leave every other file alone. A page never replaces a file
|
|
335
|
+
* okb didn't write (no banner): that file is named in the error and nothing is written. Returns the file names.
|
|
336
|
+
*/
|
|
337
|
+
export function writePages(dir, files) {
|
|
338
|
+
return replacePages(dir, files, () => false);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Check that no page would replace a file okb didn't write, then remove the earlier pages (banner) that `drop`
|
|
342
|
+
* picks, then write. The check comes first, so a refused run removes nothing.
|
|
343
|
+
*/
|
|
344
|
+
function replacePages(dir, files, drop) {
|
|
345
|
+
const yours = Object.keys(files).filter((name) => {
|
|
346
|
+
try {
|
|
347
|
+
return !readFileSync(join(dir, name), "utf8").startsWith(BANNER);
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
return false; // not there yet
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
if (yours.length) {
|
|
354
|
+
throw new OkbError(`${dir} already has ${yours.join(", ")}, and okb didn't write ${yours.length === 1 ? "it" : "them"}, so okb docs won't replace ${yours.length === 1 ? "it" : "them"}. Write the pages to an empty or new folder (--out <folder>).`);
|
|
355
|
+
}
|
|
356
|
+
for (const old of listMarkdown(dir))
|
|
357
|
+
if (!(old in files) && drop(old) && readFileSync(join(dir, old), "utf8").startsWith(BANNER))
|
|
358
|
+
rmSync(join(dir, old));
|
|
359
|
+
for (const [name, text] of Object.entries(files)) {
|
|
360
|
+
mkdirSync(dirname(join(dir, name)), { recursive: true });
|
|
361
|
+
writeFileSync(join(dir, name), text);
|
|
362
|
+
}
|
|
363
|
+
return Object.keys(files).sort();
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Write a changed-only view into `dir`, removing pages an earlier run wrote there (they carry the banner)
|
|
367
|
+
* that this view doesn't include, so a folder reused for a second `--changed` doesn't keep the first one's pages.
|
|
368
|
+
* `rules-in-context.md` is a separate view of the same folder and stays.
|
|
369
|
+
*/
|
|
370
|
+
export function writeChangedPages(dir, files) {
|
|
371
|
+
return replacePages(dir, files, (old) => old !== "rules-in-context.md");
|
|
372
|
+
}
|
|
373
|
+
function listMarkdown(dir) {
|
|
374
|
+
const out = [];
|
|
375
|
+
const walk = (rel) => {
|
|
376
|
+
let entries;
|
|
377
|
+
try {
|
|
378
|
+
entries = readdirSync(join(dir, rel), { withFileTypes: true });
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
for (const e of entries) {
|
|
384
|
+
const p = rel ? `${rel}/${e.name}` : e.name;
|
|
385
|
+
if (e.isDirectory())
|
|
386
|
+
walk(p);
|
|
387
|
+
else if (e.name.endsWith(".md"))
|
|
388
|
+
out.push(p);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
walk("");
|
|
392
|
+
return out;
|
|
393
|
+
}
|
package/dist/metakb.d.ts
CHANGED
package/dist/metakb.js
CHANGED
|
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
|
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import * as naming from "./naming.js";
|
|
6
|
-
import {} from "@ontology-kit/
|
|
6
|
+
import {} from "@ontology-kit/reader";
|
|
7
7
|
import {} from "./types.js";
|
|
8
8
|
export const META_DIR = join(dirname(dirname(fileURLToPath(import.meta.url))), "meta-kb", "data");
|
|
9
9
|
const EXPLAINABLE = new Set(["Concept", "Principle", "Decision", "Step", "Rule"]);
|
package/dist/naming.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Name styles, conversion, and singular/plural guessing. */
|
|
2
|
-
import { type Conventions } from "@ontology-kit/
|
|
3
|
-
export { fold, key, rawTokens, relType, slug, stripAccents, tokens } from "@ontology-kit/
|
|
2
|
+
import { type Conventions } from "@ontology-kit/reader";
|
|
3
|
+
export { fold, key, rawTokens, relType, slug, stripAccents, tokens } from "@ontology-kit/reader";
|
|
4
4
|
export declare const CLASS_STYLES: string[];
|
|
5
5
|
export declare const SLOT_STYLES: string[];
|
|
6
6
|
export declare const DEFAULT_CONVENTIONS: Conventions;
|
package/dist/naming.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Name styles, conversion, and singular/plural guessing. */
|
|
2
|
-
import { rawTokens, stripAccents, tokens } from "@ontology-kit/
|
|
3
|
-
export { fold, key, rawTokens, relType, slug, stripAccents, tokens } from "@ontology-kit/
|
|
2
|
+
import { rawTokens, stripAccents, tokens } from "@ontology-kit/reader";
|
|
3
|
+
export { fold, key, rawTokens, relType, slug, stripAccents, tokens } from "@ontology-kit/reader";
|
|
4
4
|
export const CLASS_STYLES = ["PascalCase", "Title Case", "snake_case", "kebab-case", "Sentence case"];
|
|
5
5
|
export const SLOT_STYLES = ["camelCase", "snake_case", "kebab-case", "lower case"];
|
|
6
6
|
export const DEFAULT_CONVENTIONS = {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Ontology } from "@ontology-kit/reader";
|
|
2
|
+
/** Where the working ontology sits inside its git repository, and the repository's top folder. */
|
|
3
|
+
export declare function locateInRepo(folder: string): {
|
|
4
|
+
top: string;
|
|
5
|
+
path: string;
|
|
6
|
+
};
|
|
7
|
+
/** Load the ontology as it was at `ref`. `where` is from `locateInRepo`. */
|
|
8
|
+
export declare function loadAtRef(ref: string, where: {
|
|
9
|
+
top: string;
|
|
10
|
+
path: string;
|
|
11
|
+
}): Ontology;
|
|
12
|
+
/**
|
|
13
|
+
* Resolve one side of `okb diff`: an existing folder is read as it is; anything else is a git ref,
|
|
14
|
+
* looked up in the repository around `context` (the working ontology folder). A name that is both
|
|
15
|
+
* (like `git diff`'s "both revision and filename") is an error that gives both spellings.
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadSide(arg: string, context: string): {
|
|
18
|
+
ont: Ontology;
|
|
19
|
+
name: string;
|
|
20
|
+
};
|