@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.
Files changed (109) hide show
  1. package/README.md +1 -1
  2. package/dist/check.d.ts +43 -0
  3. package/dist/check.js +102 -0
  4. package/dist/checks.d.ts +1 -1
  5. package/dist/checks.js +27 -4
  6. package/dist/cli.js +11 -705
  7. package/dist/clidocs.d.ts +0 -2
  8. package/dist/clidocs.js +5 -27
  9. package/dist/clock.js +1 -1
  10. package/dist/command-table.d.ts +144 -0
  11. package/dist/command-table.js +85 -0
  12. package/dist/commands/documents.d.ts +3 -0
  13. package/dist/commands/documents.js +250 -0
  14. package/dist/commands/getting-started.d.ts +3 -0
  15. package/dist/commands/getting-started.js +135 -0
  16. package/dist/commands/housekeeping.d.ts +3 -0
  17. package/dist/commands/housekeeping.js +203 -0
  18. package/dist/commands/modeling.d.ts +3 -0
  19. package/dist/commands/modeling.js +467 -0
  20. package/dist/commands/planning.d.ts +3 -0
  21. package/dist/commands/planning.js +251 -0
  22. package/dist/commands/reviewing.d.ts +3 -0
  23. package/dist/commands/reviewing.js +234 -0
  24. package/dist/commands/skills.d.ts +3 -0
  25. package/dist/commands/skills.js +53 -0
  26. package/dist/commands.d.ts +10 -22
  27. package/dist/commands.js +15 -622
  28. package/dist/cqanswer.d.ts +51 -0
  29. package/dist/cqanswer.js +76 -0
  30. package/dist/diff.d.ts +85 -0
  31. package/dist/diff.js +323 -0
  32. package/dist/export-changes.d.ts +40 -0
  33. package/dist/export-changes.js +115 -0
  34. package/dist/export-csv.d.ts +17 -0
  35. package/dist/export-csv.js +188 -0
  36. package/dist/export-eval-cases.d.ts +38 -0
  37. package/dist/export-eval-cases.js +40 -0
  38. package/dist/export.d.ts +34 -3
  39. package/dist/export.js +70 -12
  40. package/dist/families.d.ts +1 -1
  41. package/dist/families.js +1 -1
  42. package/dist/help.d.ts +11 -2
  43. package/dist/help.js +51 -57
  44. package/dist/kbdocs.d.ts +18 -0
  45. package/dist/kbdocs.js +393 -0
  46. package/dist/metakb.d.ts +1 -1
  47. package/dist/metakb.js +1 -1
  48. package/dist/naming.d.ts +2 -2
  49. package/dist/naming.js +2 -2
  50. package/dist/ontology-ref.d.ts +20 -0
  51. package/dist/ontology-ref.js +108 -0
  52. package/dist/ops/admin.d.ts +2 -2
  53. package/dist/ops/admin.js +26 -8
  54. package/dist/ops/classes.d.ts +1 -1
  55. package/dist/ops/classes.js +15 -7
  56. package/dist/ops/common.d.ts +12 -2
  57. package/dist/ops/common.js +3 -0
  58. package/dist/ops/instances.d.ts +2 -1
  59. package/dist/ops/instances.js +9 -3
  60. package/dist/ops/links.d.ts +2 -2
  61. package/dist/ops/links.js +3 -2
  62. package/dist/ops/naming.d.ts +1 -1
  63. package/dist/ops/naming.js +3 -1
  64. package/dist/ops/scope.d.ts +14 -3
  65. package/dist/ops/scope.js +38 -4
  66. package/dist/ops/slots.d.ts +1 -1
  67. package/dist/ops/slots.js +21 -8
  68. package/dist/ops/values.d.ts +1 -1
  69. package/dist/ops/values.js +5 -1
  70. package/dist/provenance.d.ts +5 -3
  71. package/dist/provenance.js +61 -19
  72. package/dist/release.d.ts +52 -0
  73. package/dist/release.js +337 -0
  74. package/dist/render.d.ts +88 -1
  75. package/dist/render.js +265 -3
  76. package/dist/resync.d.ts +98 -0
  77. package/dist/resync.js +234 -0
  78. package/dist/review.d.ts +1 -1
  79. package/dist/review.js +1 -1
  80. package/dist/reviewdocs.d.ts +42 -0
  81. package/dist/reviewdocs.js +263 -0
  82. package/dist/runner.d.ts +3 -0
  83. package/dist/runner.js +84 -0
  84. package/dist/skills.d.ts +19 -3
  85. package/dist/skills.js +130 -14
  86. package/dist/status.d.ts +1 -1
  87. package/dist/status.js +1 -1
  88. package/dist/types.d.ts +1 -1
  89. package/dist/types.js +1 -1
  90. package/dist/verifynote.d.ts +19 -0
  91. package/dist/verifynote.js +93 -0
  92. package/dist/workflows.d.ts +283 -0
  93. package/dist/workflows.js +270 -0
  94. package/meta-kb/data/edges.json +25 -0
  95. package/meta-kb/data/manifest.json +5 -5
  96. package/meta-kb/data/nodes.json +46 -9
  97. package/package.json +9 -4
  98. package/schema/edge.schema.json +68 -0
  99. package/schema/edges.schema.json +9 -0
  100. package/schema/node.schema.json +914 -0
  101. package/schema/nodes.schema.json +9 -0
  102. package/schema/okb.schema.json +41 -0
  103. package/skills/build-domain-kb-from-docs/SKILL.md +21 -3
  104. package/skills/ontology-coach/SKILL.md +8 -4
  105. package/skills/ontology-maintainer/SKILL.md +152 -0
  106. package/skills/ontology-review/SKILL.md +4 -2
  107. package/skills/ontology-signoff/SKILL.md +182 -0
  108. package/templates/okb-check.yml +148 -0
  109. package/templates/pull_request_template.md +16 -0
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Load an ontology from a folder or from a git ref (`main`, `HEAD~3`, a tag, a sha) without touching
3
+ * the working tree: the ref's okb files are read with `git show` into a temp folder, loaded, and deleted.
4
+ * Read-only: it never takes the write lock.
5
+ */
6
+ import { execFileSync } from "node:child_process";
7
+ import { existsSync, mkdtempSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
8
+ import { tmpdir } from "node:os";
9
+ import { dirname, isAbsolute, join, relative, resolve } from "node:path";
10
+ import { OkbError, Ontology, findRoot, MANIFEST } from "@ontology-kit/reader";
11
+ const FILES = [MANIFEST, "nodes.jsonl", "edges.jsonl", "nodes.json", "edges.json"];
12
+ function git(cwd, args) {
13
+ try {
14
+ return execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 1 << 30 });
15
+ }
16
+ catch (e) {
17
+ const err = e;
18
+ if (err.code === "ENOENT")
19
+ throw new OkbError("okb diff with a git ref needs git, and it isn't installed.");
20
+ throw new OkbError(`git ${args[0]} failed: ${(err.stderr ?? String(e)).trim()}`);
21
+ }
22
+ }
23
+ /** Where the working ontology sits inside its git repository, and the repository's top folder. */
24
+ export function locateInRepo(folder) {
25
+ const root = findRoot(folder);
26
+ if (!root)
27
+ throw new OkbError(`No ontology found in ${resolve(folder)} or its parents, so there is nothing to read from a git ref. Run okb diff from an ontology folder, or pass -C <folder>.`);
28
+ let top;
29
+ try {
30
+ top = git(root, ["rev-parse", "--show-toplevel"]).trim();
31
+ }
32
+ catch {
33
+ throw new OkbError(`${root} isn't inside a git repository, so '${folder}' can't be read as a git ref. Give two folders instead.`);
34
+ }
35
+ // realpath-safe: compare through git's own view of the folder
36
+ const prefix = git(root, ["rev-parse", "--show-prefix"]).trim().replace(/\/$/, "");
37
+ return { top, path: prefix || relative(top, root) || "." };
38
+ }
39
+ /** Load the ontology as it was at `ref`. `where` is from `locateInRepo`. */
40
+ export function loadAtRef(ref, where) {
41
+ if (ref.startsWith("-"))
42
+ throw new OkbError(`'${ref}' isn't a folder or a git ref.`);
43
+ try {
44
+ git(where.top, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]);
45
+ }
46
+ catch {
47
+ throw new OkbError(`'${ref}' isn't a folder or a git ref in ${where.top}.`);
48
+ }
49
+ const at = where.path === "." ? "" : where.path + "/";
50
+ const tmp = mkdtempSync(join(tmpdir(), "okb-diff-"));
51
+ try {
52
+ let found = false;
53
+ for (const f of FILES) {
54
+ const spec = `${ref}:${at}${f}`;
55
+ try {
56
+ git(where.top, ["cat-file", "-e", spec]);
57
+ }
58
+ catch {
59
+ continue;
60
+ }
61
+ const body = execFileSync("git", ["show", spec], { cwd: where.top, maxBuffer: 1 << 30, stdio: ["ignore", "pipe", "pipe"] });
62
+ mkdirSync(dirname(join(tmp, f)), { recursive: true });
63
+ writeFileSync(join(tmp, f), body);
64
+ if (f === MANIFEST)
65
+ found = true;
66
+ }
67
+ if (!found)
68
+ throw new OkbError(`There is no ontology at ${ref}:${at || "."} (no ${MANIFEST} there). Check the ref, or pass the folder it was in.`);
69
+ return Ontology.load(tmp);
70
+ }
71
+ finally {
72
+ rmSync(tmp, { recursive: true, force: true });
73
+ }
74
+ }
75
+ /** The unambiguous spelling of a ref (`refs/heads/main`, `refs/tags/v1`, or the full sha), or null if `name` isn't a ref there. */
76
+ function fullRef(top, name) {
77
+ if (name.startsWith("-"))
78
+ return null;
79
+ try {
80
+ const sha = git(top, ["rev-parse", "--verify", "--quiet", `${name}^{commit}`]).trim();
81
+ return git(top, ["rev-parse", "--symbolic-full-name", name]).trim().split("\n")[0] || sha;
82
+ }
83
+ catch {
84
+ return null;
85
+ }
86
+ }
87
+ /**
88
+ * Resolve one side of `okb diff`: an existing folder is read as it is; anything else is a git ref,
89
+ * looked up in the repository around `context` (the working ontology folder). A name that is both
90
+ * (like `git diff`'s "both revision and filename") is an error that gives both spellings.
91
+ */
92
+ export function loadSide(arg, context) {
93
+ if (existsSync(arg) && statSync(arg).isDirectory()) {
94
+ let ref = null;
95
+ try {
96
+ ref = fullRef(locateInRepo(context).top, arg);
97
+ }
98
+ catch {
99
+ // not in a repository (or no ontology around `context`): it can only be the folder
100
+ }
101
+ if (ref) {
102
+ const folder = isAbsolute(arg) || arg.startsWith("./") || arg.startsWith("../") ? arg : `./${arg}`;
103
+ throw new OkbError(`'${arg}' is both a folder and a git ref. Say which you mean:\n the folder: ${folder}\n the ref: ${ref}`);
104
+ }
105
+ return { ont: Ontology.load(arg), name: arg };
106
+ }
107
+ return { ont: loadAtRef(arg, locateInRepo(context)), name: arg };
108
+ }
@@ -1,5 +1,5 @@
1
- import { Ontology, type NodeType } from "@ontology-kit/format";
2
- import type { Notes } from "./common.js";
1
+ import { Ontology, type NodeType } from "@ontology-kit/reader";
2
+ import { type Notes } from "./common.js";
3
3
  /** Start an empty ontology folder: the manifest, the one Ontology node, no edges. Saved before it returns. */
4
4
  export declare function createOntology(root: string, name: string): Ontology;
5
5
  export declare function addDecision(ont: Ontology, o: {
package/dist/ops/admin.js CHANGED
@@ -1,11 +1,12 @@
1
1
  /** Creating an ontology, design decisions, renaming and removing. */
2
- import { existsSync, mkdirSync } from "node:fs";
2
+ import { existsSync, mkdirSync, writeFileSync } from "node:fs";
3
3
  import { join, resolve } from "node:path";
4
4
  import * as naming from "../naming.js";
5
5
  import { runChecks } from "../checks.js";
6
6
  import { today } from "../clock.js";
7
7
  import { MetaKB } from "../metakb.js";
8
- import { FORMAT, MANIFEST, OkbError, Ontology, nameOrText } from "@ontology-kit/format";
8
+ import { FORMAT, MANIFEST, OkbError, Ontology, nameOrText } from "@ontology-kit/reader";
9
+ import { tip } from "./common.js";
9
10
  import { checkName } from "./naming.js";
10
11
  /** Start an empty ontology folder: the manifest, the one Ontology node, no edges. Saved before it returns. */
11
12
  export function createOntology(root, name) {
@@ -13,12 +14,16 @@ export function createOntology(root, name) {
13
14
  if (existsSync(join(root, MANIFEST)))
14
15
  throw new OkbError(`${root} already contains an ontology (${MANIFEST}).`);
15
16
  const manifest = { format: FORMAT, name, currentStep: 1, created: today() };
16
- const nodes = [{
17
- type: "Ontology", id: "ontology", name, domain: "", purpose: "", users: [], maintainers: [],
18
- outOfScope: [], kind: "application", reuseReviewed: false,
19
- }];
20
- const ont = new Ontology(resolve(root), manifest, nodes, []);
17
+ const ont = new Ontology(resolve(root), manifest, [], []);
18
+ ont.addNode({
19
+ type: "Ontology", id: "ontology", name, domain: "", purpose: "", users: [], maintainers: [],
20
+ outOfScope: [], kind: "application", reuseReviewed: false,
21
+ });
21
22
  ont.save();
23
+ // Keep the lock file and half-written temp files out of git (a team keeps the folder in a repository).
24
+ const ignore = join(root, ".gitignore");
25
+ if (!existsSync(ignore))
26
+ writeFileSync(ignore, ".okb.lock\n*.tmp\n");
22
27
  return ont;
23
28
  }
24
29
  export function addDecision(ont, o) {
@@ -46,7 +51,7 @@ export function addDecision(ont, o) {
46
51
  : `Note: it doesn't match any current finding from ${o.waives.join(", ")}. Check the --about target (a decision about a class also covers its subclasses).`);
47
52
  }
48
53
  if (!o.rationale)
49
- notes.push("Tip: add --why so future readers know the reason, not just the choice.");
54
+ notes.push(tip("Tip: add --why so future readers know the reason, not just the choice."));
50
55
  return notes;
51
56
  }
52
57
  export function rename(ont, ref, next, force = false) {
@@ -60,14 +65,26 @@ export function rename(ont, ref, next, force = false) {
60
65
  }
61
66
  const old = n.name;
62
67
  if (n.type === "Slot" && n.valueType === "Instance") {
68
+ const oldType = n.relType;
63
69
  const retyped = ont.rel.rename(n, next);
64
70
  if (retyped)
65
71
  notes.push(`Its links are now stored as ${retyped.to} (were ${retyped.from}).`);
72
+ if (oldType && n.relType && n.relType !== oldType)
73
+ n.formerRelTypes = remember(n.formerRelTypes, oldType, n.relType);
66
74
  }
75
+ if (n.type !== "ReusedOntology" && old !== next)
76
+ n.formerNames = remember(n.formerNames, old, next);
67
77
  n.name = next;
78
+ ont.touch(n);
68
79
  notes.unshift(`Renamed ${n.type} '${old}' → '${next}' (id ${n.id} unchanged).`);
80
+ if (n.type !== "ReusedOntology" && old !== next)
81
+ notes.push(`Kept '${old}' as a former name, so a release's change log can map old to new.`);
69
82
  return notes;
70
83
  }
84
+ /** The former names after a rename: `old` goes on the end (oldest first), and a name that comes back is no longer former. */
85
+ function remember(former, old, next) {
86
+ return [...(former ?? []).filter((x) => x !== old && x !== next), old];
87
+ }
71
88
  export function remove(ont, ref, types) {
72
89
  const n = ont.find(ref, types);
73
90
  if (n.id === "ontology")
@@ -101,6 +118,7 @@ export function remove(ont, ref, types) {
101
118
  for (const t of ont.ofType("Term")) {
102
119
  if (ont.targets(t.id, "BECAME").includes(n.id)) {
103
120
  t.disposition = "undecided";
121
+ ont.touch(t);
104
122
  notes.push(`Term '${t.text}' is undecided again.`);
105
123
  }
106
124
  }
@@ -1,4 +1,4 @@
1
- import { type Ontology } from "@ontology-kit/format";
1
+ import { type Ontology } from "@ontology-kit/reader";
2
2
  import { type Notes } from "./common.js";
3
3
  export declare function addClass(ont: Ontology, name: string, o?: {
4
4
  parents?: string[];
@@ -1,7 +1,7 @@
1
1
  /** The class hierarchy: adding and changing classes, parents and disjointness. */
2
2
  import * as naming from "../naming.js";
3
- import { OkbError } from "@ontology-kit/format";
4
- import { an, splitList as list } from "./common.js";
3
+ import { OkbError } from "@ontology-kit/reader";
4
+ import { an, splitList as list, tip } from "./common.js";
5
5
  import { checkName } from "./naming.js";
6
6
  import { linkTermIfAny } from "./scope.js";
7
7
  export function addClass(ont, name, o = {}) {
@@ -40,7 +40,7 @@ export function addClass(ont, name, o = {}) {
40
40
  if (asValue)
41
41
  notes.push(`Careful: '${name}' is also an allowed value of the slot ${asValue.name}. Model it as a class OR a value, not both (okb explain decision.class-or-value).`);
42
42
  if (!o.description)
43
- notes.push(`Add a one-line description: okb class set "${name}" --desc "..."`);
43
+ notes.push(tip(`Add a one-line description: okb class set "${name}" --desc "..."`));
44
44
  linkTermIfAny(ont, name, id, "class");
45
45
  return notes;
46
46
  }
@@ -62,8 +62,10 @@ export function checkParents(ont, parents, name, notes) {
62
62
  export function setClass(ont, ref, o) {
63
63
  const cl = ont.find(ref, "Class");
64
64
  const notes = [];
65
- if (o.description !== undefined)
65
+ if (o.description !== undefined) {
66
66
  cl.description = o.description;
67
+ ont.touch(cl);
68
+ }
67
69
  for (const p of o.addParents ?? []) {
68
70
  const pn = ont.find(p, "Class");
69
71
  if (pn.id === cl.id || ont.ancestors(pn.id).has(cl.id))
@@ -77,12 +79,18 @@ export function setClass(ont, ref, o) {
77
79
  ont.removeEdges((e) => e.from === cl.id && e.type === "IS_A" && e.to === pn.id);
78
80
  notes.push(`${cl.name} is no longer under ${pn.name}.`);
79
81
  }
80
- if (o.synonyms?.length)
82
+ if (o.synonyms?.length) {
81
83
  cl.synonyms = [...new Set([...(cl.synonyms ?? []), ...list(o.synonyms)])];
82
- if (o.abstract !== undefined)
84
+ ont.touch(cl);
85
+ }
86
+ if (o.abstract !== undefined) {
83
87
  cl.abstract = o.abstract || undefined;
84
- if (o.terminological !== undefined)
88
+ ont.touch(cl);
89
+ }
90
+ if (o.terminological !== undefined) {
85
91
  cl.terminological = o.terminological || undefined;
92
+ ont.touch(cl);
93
+ }
86
94
  return notes.length ? notes : [`Updated ${cl.name}.`];
87
95
  }
88
96
  export function disjoint(ont, refs) {
@@ -1,6 +1,16 @@
1
1
  /** Shared by the ops modules. */
2
- /** Human-readable notes about what an operation did, including gentle warnings, so beginners learn as they go. */
3
- export type Notes = string[];
2
+ /** A teaching tip: advice about what to do next, shown dimmed. It carries its own `kind`, so rendering never guesses from the text. */
3
+ export interface Tip {
4
+ kind: "tip";
5
+ text: string;
6
+ }
7
+ /** One line about what an operation did: a plain string (a result or a warning) or a {@link Tip}. */
8
+ export type Note = string | Tip;
9
+ /** What an operation tells the user, including gentle warnings and tips, so beginners learn as they go. */
10
+ export type Notes = Note[];
11
+ export declare const tip: (text: string) => Tip;
12
+ /** The text of a note, for tests and plain output. */
13
+ export declare const noteText: (n: Note) => string;
4
14
  export { an } from "../naming.js";
5
15
  /** Command-line list values: repeated flags and/or comma-separated ("a,b" "c" -> [a, b, c]). */
6
16
  export declare const splitList: (s: string | string[] | undefined) => string[];
@@ -1,4 +1,7 @@
1
1
  /** Shared by the ops modules. */
2
+ export const tip = (text) => ({ kind: "tip", text });
3
+ /** The text of a note, for tests and plain output. */
4
+ export const noteText = (n) => (typeof n === "string" ? n : n.text);
2
5
  export { an } from "../naming.js";
3
6
  /** Command-line list values: repeated flags and/or comma-separated ("a,b" "c" -> [a, b, c]). */
4
7
  export const splitList = (s) => (Array.isArray(s) ? s : s ? [s] : []).flatMap((x) => x.split(",")).map((x) => x.trim()).filter(Boolean);
@@ -1,4 +1,4 @@
1
- import { type Ontology } from "@ontology-kit/format";
1
+ import { type Ontology } from "@ontology-kit/reader";
2
2
  import { type Notes } from "./common.js";
3
3
  export declare function addInstance(ont: Ontology, name: string, o: {
4
4
  of: string[];
@@ -6,3 +6,4 @@ export declare function addInstance(ont: Ontology, name: string, o: {
6
6
  assignments?: string[];
7
7
  }): Notes;
8
8
  export declare function setInstanceClasses(ont: Ontology, ref: string, classes: string[]): Notes;
9
+ export declare function setInstanceDescription(ont: Ontology, ref: string, description: string): Notes;
@@ -1,7 +1,7 @@
1
1
  /** Instances and the classes they belong to. */
2
2
  import * as naming from "../naming.js";
3
- import { OkbError, literalValues } from "@ontology-kit/format";
4
- import { an } from "./common.js";
3
+ import { OkbError, literalValues } from "@ontology-kit/reader";
4
+ import { an, tip } from "./common.js";
5
5
  import { assign } from "./values.js";
6
6
  import { checkParents } from "./classes.js";
7
7
  export function addInstance(ont, name, o) {
@@ -16,7 +16,7 @@ export function addInstance(ont, name, o) {
16
16
  if (cl.abstract)
17
17
  throw new OkbError(`${cl.name} is abstract, so it can't have direct instances. Pick one of its subclasses: ${ont.children(cl.id).map((k) => ont.label(k)).join(", ")}.`);
18
18
  if (ont.children(cl.id).length)
19
- notes.push(`Tip: ${cl.name} has subclasses (${ont.children(cl.id).map((k) => ont.label(k)).join(", ")}). Use the most specific class that fits.`);
19
+ notes.push(tip(`Tip: ${cl.name} has subclasses (${ont.children(cl.id).map((k) => ont.label(k)).join(", ")}). Use the most specific class that fits.`));
20
20
  }
21
21
  const id = ont.newId("Instance", name);
22
22
  const inst = ont.addNode({ type: "Instance", id, name, ...(o.description ? { description: o.description } : {}), values: {} });
@@ -68,3 +68,9 @@ export function setInstanceClasses(ont, ref, classes) {
68
68
  notes.push(`Warning: its values for ${[...new Set(lost)].map((s) => ont.label(s)).join(", ")} don't apply to the new class. Remove them (okb instance set "${inst.name}" <slot>=) or pick another class.`);
69
69
  return notes;
70
70
  }
71
+ export function setInstanceDescription(ont, ref, description) {
72
+ const inst = ont.find(ref, "Instance");
73
+ inst.description = description;
74
+ ont.touch(inst);
75
+ return ["Updated description."];
76
+ }
@@ -1,5 +1,5 @@
1
- import { type Ontology } from "@ontology-kit/format";
2
- import type { Notes } from "./common.js";
1
+ import { type Ontology } from "@ontology-kit/reader";
2
+ import { type Notes } from "./common.js";
3
3
  /** Create (or update) one relationship value, with edge properties. */
4
4
  export declare function link(ont: Ontology, fromRef: string, relRef: string, toRef: string, o?: {
5
5
  props?: string[];
package/dist/ops/links.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /** One relationship value at a time, with edge properties (okb link / unlink). */
2
2
  import * as naming from "../naming.js";
3
- import { OkbError, edgeProps } from "@ontology-kit/format";
3
+ import { OkbError, edgeProps } from "@ontology-kit/reader";
4
+ import { tip } from "./common.js";
4
5
  import { assign, findSlotFor, parseAssignments } from "./values.js";
5
6
  function coerceEdgeValue(decl, raw, relName) {
6
7
  switch (decl.valueType) {
@@ -71,7 +72,7 @@ export function link(ont, fromRef, relRef, toRef, o = {}) {
71
72
  if (missing.length)
72
73
  notes.push(`Still required on this edge: ${missing.join(", ")}.`);
73
74
  if (!props.rule && !edge.rule && ont.ofType("Rule").length)
74
- notes.push(`Tip: if a documented rule justifies this link, add --rule <rule id> so the edge shares its quote and verification.`);
75
+ notes.push(tip(`Tip: if a documented rule justifies this link, add --rule <rule id> so the edge shares its quote and verification.`));
75
76
  return notes;
76
77
  }
77
78
  export function unlink(ont, fromRef, relRef, toRef) {
@@ -1,4 +1,4 @@
1
- import { type Ontology, type Conventions } from "@ontology-kit/format";
1
+ import { type Ontology, type Conventions } from "@ontology-kit/reader";
2
2
  import type { Notes } from "./common.js";
3
3
  export declare function setConventions(ont: Ontology, o: Partial<Conventions>): Notes;
4
4
  export declare function checkName(ont: Ontology, name: string, kind: "Class" | "Slot", force: boolean | undefined, notes: Notes, exclude?: string): void;
@@ -1,6 +1,6 @@
1
1
  /** Naming conventions, and the checks every new class or slot name goes through. */
2
2
  import * as naming from "../naming.js";
3
- import { OkbError } from "@ontology-kit/format";
3
+ import { OkbError } from "@ontology-kit/reader";
4
4
  export function setConventions(ont, o) {
5
5
  const conv = { ...naming.DEFAULT_CONVENTIONS, ...(ont.conventions ?? {}) };
6
6
  if (o.classCase) {
@@ -27,6 +27,7 @@ export function setConventions(ont, o) {
27
27
  }
28
28
  conv.source = "chosen";
29
29
  ont.meta.conventions = conv;
30
+ ont.touch(ont.meta);
30
31
  const notes = [];
31
32
  const bad = [...ont.ofType("Class"), ...ont.ofType("Slot")].filter((n) => n.name && naming.conventionalName(n.name, n.type, conv) !== n.name);
32
33
  if (bad.length)
@@ -36,6 +37,7 @@ export function setConventions(ont, o) {
36
37
  function ensureConventions(ont, notes) {
37
38
  if (!ont.conventions) {
38
39
  ont.meta.conventions = { ...naming.DEFAULT_CONVENTIONS, source: "default" };
40
+ ont.touch(ont.meta);
39
41
  notes.push("No naming convention was set, so okb is using its defaults: classes PascalCase and singular, slots camelCase. Confirm them with `okb convention` (or pick your own).");
40
42
  }
41
43
  return ont.conventions;
@@ -1,5 +1,6 @@
1
- import { type Ontology, type Disposition } from "@ontology-kit/format";
2
- import { type Notes } from "./common.js";
1
+ import { type Ontology, type Disposition } from "@ontology-kit/reader";
2
+ import { type Notes, type Tip } from "./common.js";
3
+ import { type MatchMode } from "../cqanswer.js";
3
4
  export declare function setScope(ont: Ontology, o: {
4
5
  name?: string;
5
6
  domain?: string;
@@ -10,13 +11,23 @@ export declare function setScope(ont: Ontology, o: {
10
11
  kind?: string;
11
12
  }): Notes;
12
13
  export declare function addCQ(ont: Ontology, text: string): Notes;
14
+ export declare function setCQ(ont: Ontology, ref: string, o: {
15
+ text?: string;
16
+ status?: string;
17
+ }): Notes;
18
+ /** Store the expected answer on a question. The query must run now, so a misspelled name fails here, not in CI. */
19
+ export declare function answerCQ(ont: Ontology, ref: string, o: {
20
+ query: string;
21
+ expect: string[];
22
+ match: MatchMode;
23
+ }): Notes;
13
24
  export declare function linkCQ(ont: Ontology, cqRef: string, refs: string[], unlink?: boolean): Notes;
14
25
  /**
15
26
  * Once a question is answered, suggest its family (rule scope-cq-families): the same
16
27
  * question about similar things, and more about the same subject. One line, and
17
28
  * only after something is built, so nobody has to list every question up front.
18
29
  */
19
- export declare function familyTip(ont: Ontology, cqId: string): string;
30
+ export declare function familyTip(ont: Ontology, cqId: string): Tip;
20
31
  export declare function addTerms(ont: Ontology, terms: string[], note?: string): Notes;
21
32
  export declare function setTerm(ont: Ontology, ref: string, as: string, nodeRef?: string, note?: string): Notes;
22
33
  export declare function addReuse(ont: Ontology, o: {
package/dist/ops/scope.js CHANGED
@@ -1,10 +1,11 @@
1
1
  /** Step 1 to 3: scope, competency questions, reuse, and the brainstormed term list. */
2
2
  import * as naming from "../naming.js";
3
3
  import { questionFamilies } from "../families.js";
4
- import { OkbError, DISPOSITIONS } from "@ontology-kit/format";
4
+ import { OkbError, DISPOSITIONS } from "@ontology-kit/reader";
5
5
  import { isOneOf } from "../types.js";
6
- import { splitList as list } from "./common.js";
6
+ import { splitList as list, tip } from "./common.js";
7
7
  import { addDecision } from "./admin.js";
8
+ import { compare, describeResult, evaluate, parseQuery } from "../cqanswer.js";
8
9
  export function setScope(ont, o) {
9
10
  const m = ont.meta;
10
11
  if (o.name) {
@@ -26,6 +27,8 @@ export function setScope(ont, o) {
26
27
  throw new OkbError("--kind must be 'application' or 'terminological'.");
27
28
  m.kind = o.kind;
28
29
  }
30
+ if (Object.values(o).some((v) => v !== undefined && (!Array.isArray(v) || v.length)))
31
+ ont.touch(m);
29
32
  return [];
30
33
  }
31
34
  export function addCQ(ont, text) {
@@ -33,9 +36,36 @@ export function addCQ(ont, text) {
33
36
  ont.addNode({ type: "CompetencyQuestion", id, text, status: "draft" });
34
37
  const notes = [`Added ${id}.`];
35
38
  if (!text.trim().endsWith("?"))
36
- notes.push("Tip: phrase competency questions as actual questions.");
39
+ notes.push(tip("Tip: phrase competency questions as actual questions."));
37
40
  return notes;
38
41
  }
42
+ export function setCQ(ont, ref, o) {
43
+ const q = ont.find(ref, "CompetencyQuestion");
44
+ if (o.status && !["draft", "answerable"].includes(o.status))
45
+ throw new OkbError("--status must be draft or answerable.");
46
+ if (o.text)
47
+ q.text = o.text;
48
+ if (o.status)
49
+ q.status = o.status;
50
+ if (o.text || o.status)
51
+ ont.touch(q);
52
+ return o.status === "answerable" ? [`Updated ${q.id}.`, familyTip(ont, q.id)] : [`Updated ${q.id}.`];
53
+ }
54
+ /** Store the expected answer on a question. The query must run now, so a misspelled name fails here, not in CI. */
55
+ export function answerCQ(ont, ref, o) {
56
+ const q = ont.find(ref, "CompetencyQuestion");
57
+ const query = parseQuery(o.query);
58
+ const expect = list(o.expect);
59
+ if (!expect.length)
60
+ throw new OkbError("--expect needs at least one name. Use --expect A,B,C.");
61
+ const actual = evaluate(ont, query);
62
+ q.answer = { query, expect, match: o.match };
63
+ ont.touch(q);
64
+ const r = compare(actual, q.answer);
65
+ const n = ont.displayNumber(q);
66
+ const now = { number: n, id: q.id, text: q.text, ...r };
67
+ return [`Stored the expected answer on ${q.id}.`, r.status === "pass" ? "The ontology answers it today." : `Heads up, the ontology doesn't answer it yet: ${describeResult(now)}`];
68
+ }
39
69
  export function linkCQ(ont, cqRef, refs, unlink = false) {
40
70
  const q = ont.find(cqRef, "CompetencyQuestion");
41
71
  const notes = [];
@@ -64,7 +94,7 @@ export function familyTip(ont, cqId) {
64
94
  return [same, more].filter(Boolean).join(", or ");
65
95
  });
66
96
  const ideas = parts.length ? ` Would you also ask ${parts.join("; ")}?` : "";
67
- return `Tip: ${cqId} is one example of a family of questions.${ideas} Add the ones you want (okb cq add "..."), note the rest as out of scope (okb scope --out-of-scope "..."), or keep it narrow.`;
97
+ return tip(`Tip: ${cqId} is one example of a family of questions.${ideas} Add the ones you want (okb cq add "..."), note the rest as out of scope (okb scope --out-of-scope "..."), or keep it narrow.`);
68
98
  }
69
99
  export function addTerms(ont, terms, note) {
70
100
  const notes = [];
@@ -85,6 +115,7 @@ export function setTerm(ont, ref, as, nodeRef, note) {
85
115
  t.disposition = as;
86
116
  if (note)
87
117
  t.note = note;
118
+ ont.touch(t);
88
119
  ont.removeEdges((e) => e.from === t.id && e.type === "BECAME");
89
120
  const notes = [`'${t.text}' → ${as}.`];
90
121
  const typeFor = { class: ["Class"], slot: ["Slot"], instance: ["Instance"], value: ["Slot"], synonym: ["Class"] };
@@ -108,10 +139,12 @@ export function addReuse(ont, o) {
108
139
  throw new OkbError(`--decision must be one of: ${decisions.join(", ")}.`);
109
140
  ont.addNode({ type: "ReusedOntology", id: ont.newId("ReusedOntology", o.name), name: o.name, url: o.url, decision: o.decision, notes: o.notes });
110
141
  ont.meta.reuseReviewed = true;
142
+ ont.touch(ont.meta);
111
143
  return [`Recorded '${o.name}' (${o.decision}).`];
112
144
  }
113
145
  export function reuseNone(ont, why) {
114
146
  ont.meta.reuseReviewed = true;
147
+ ont.touch(ont.meta);
115
148
  addDecision(ont, { title: "No existing ontology reused", decision: "Build from scratch.", rationale: why, about: ["ontology"] });
116
149
  return ["Recorded that no existing ontology was reused, with your reason as a design decision."];
117
150
  }
@@ -119,6 +152,7 @@ export function linkTermIfAny(ont, name, nodeId, as) {
119
152
  const t = ont.ofType("Term").find((x) => naming.key(naming.withHead(x.text, naming.singularize)) === naming.key(naming.withHead(name, naming.singularize)));
120
153
  if (t && (!t.disposition || t.disposition === "undecided")) {
121
154
  t.disposition = as;
155
+ ont.touch(t);
122
156
  ont.addEdge(t.id, "BECAME", nodeId);
123
157
  }
124
158
  }
@@ -1,4 +1,4 @@
1
- import { type Ontology, type LiteralType } from "@ontology-kit/format";
1
+ import { type Ontology, type LiteralType } from "@ontology-kit/reader";
2
2
  import { type Notes } from "./common.js";
3
3
  export interface SlotOpts {
4
4
  on?: string[];
package/dist/ops/slots.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /** Slots (properties and relationships), their facets, inverses and edge-property declarations. */
2
2
  import * as naming from "../naming.js";
3
- import { OkbError, isRelationship, CARDINALITIES, VALUE_TYPES } from "@ontology-kit/format";
3
+ import { OkbError, isRelationship, CARDINALITIES, VALUE_TYPES } from "@ontology-kit/reader";
4
4
  import { isOneOf } from "../types.js";
5
- import { splitList as list } from "./common.js";
5
+ import { splitList as list, tip } from "./common.js";
6
6
  import { checkName } from "./naming.js";
7
7
  import { linkTermIfAny } from "./scope.js";
8
8
  import { coerce } from "./values.js";
@@ -25,11 +25,11 @@ export function addSlot(ont, name, o) {
25
25
  ? `Added relationship ${name}: (${o.on?.join("|") || "?"})-[:${added.relType}]->(${ont.range(id).map((r) => ont.label(r)).join("|") || "?"}).`
26
26
  : `Added property ${name}${o.on?.length ? ` on ${o.on.join(", ")}` : ""} (${added.valueType}).`);
27
27
  if (!o.on?.length && !added.appliesToRules)
28
- notes.push(`Attach it to a class: okb slot set ${name} --on <Class> (or to every rule: --on-rules)`);
28
+ notes.push(tip(`Attach it to a class: okb slot set ${name} --on <Class> (or to every rule: --on-rules)`));
29
29
  if (!o.card)
30
- notes.push(`Decide how many values it holds: --card single or --card multiple (Step 6).`);
30
+ notes.push(tip(`Decide how many values it holds: --card single or --card multiple (Step 6).`));
31
31
  if (!o.description)
32
- notes.push(`Add a one-line description: okb slot set ${name} --desc "..."`);
32
+ notes.push(tip(`Add a one-line description: okb slot set ${name} --desc "..."`));
33
33
  linkTermIfAny(ont, name, id, "slot");
34
34
  return notes;
35
35
  }
@@ -43,6 +43,7 @@ export function updateSlot(ont, ref, o, creating = false) {
43
43
  throw new OkbError(`--type must be one of ${VALUE_TYPES.join(", ")}.`);
44
44
  const wasRel = s.valueType === "Instance";
45
45
  s.valueType = t;
46
+ ont.touch(s);
46
47
  if (t !== "Enumerated")
47
48
  delete s.allowedValues;
48
49
  if (t !== "Instance") {
@@ -64,6 +65,7 @@ export function updateSlot(ont, ref, o, creating = false) {
64
65
  if (s.valueType !== "Enumerated")
65
66
  throw new OkbError("--values only applies to Enumerated slots (use --type Enumerated).");
66
67
  s.allowedValues = list(o.values);
68
+ ont.touch(s);
67
69
  }
68
70
  const setRange = (refs, replace) => {
69
71
  if (s.valueType !== "Instance")
@@ -72,6 +74,7 @@ export function updateSlot(ont, ref, o, creating = false) {
72
74
  ont.removeEdges((e) => e.from === s.id && e.type === "RANGE");
73
75
  for (const r of refs)
74
76
  ont.addEdge(s.id, "RANGE", ont.find(r, "Class").id);
77
+ ont.touch(s); // a slot's range is a facet, so it counts even though it's stored as edges
75
78
  };
76
79
  if (o.range?.length)
77
80
  setRange(o.range, true);
@@ -80,12 +83,16 @@ export function updateSlot(ont, ref, o, creating = false) {
80
83
  for (const r of o.removeRange ?? []) {
81
84
  const rc = ont.find(r, "Class");
82
85
  ont.removeEdges((e) => e.from === s.id && e.type === "RANGE" && e.to === rc.id);
86
+ ont.touch(s);
83
87
  }
84
88
  if (o.card) {
85
89
  if (!isOneOf(CARDINALITIES, o.card))
86
90
  throw new OkbError("--card must be single or multiple.");
87
91
  s.cardinality = o.card;
92
+ ont.touch(s);
88
93
  }
94
+ if (o.min !== undefined || o.max !== undefined || o.description !== undefined)
95
+ ont.touch(s);
89
96
  if (o.min !== undefined)
90
97
  s.minCardinality = o.min;
91
98
  if (o.max !== undefined)
@@ -113,10 +120,14 @@ export function updateSlot(ont, ref, o, creating = false) {
113
120
  if (s.valueType === "Instance")
114
121
  throw new OkbError(`${s.name} is a relationship. A rule's qualifiers must be properties; link rules with okb rule refines.`);
115
122
  s.appliesToRules = true;
123
+ ont.touch(s);
116
124
  }
117
- if (o.offRules)
125
+ if (o.offRules) {
118
126
  delete s.appliesToRules;
127
+ ont.touch(s);
128
+ }
119
129
  if (o.default !== undefined) {
130
+ ont.touch(s);
120
131
  if (o.default === "")
121
132
  delete s.default;
122
133
  else
@@ -128,9 +139,9 @@ export function updateSlot(ont, ref, o, creating = false) {
128
139
  throw new OkbError(`${s.name}: minimum ${mn} is more than maximum ${mx}${s.cardinality === "single" ? " (single cardinality means at most 1)" : ""}. Nothing could ever satisfy that.`);
129
140
  }
130
141
  if (s.valueType === "Enumerated" && !s.allowedValues?.length)
131
- notes.push(`List the allowed values: okb slot set ${s.name} --values a,b,c`);
142
+ notes.push(tip(`List the allowed values: okb slot set ${s.name} --values a,b,c`));
132
143
  if (s.valueType === "Instance" && ont.range(s.id).length === 0)
133
- notes.push(`Say which class its values come from: okb slot set ${s.name} --range <Class>`);
144
+ notes.push(tip(`Say which class its values come from: okb slot set ${s.name} --range <Class>`));
134
145
  if (!creating && !notes.length)
135
146
  notes.push(`Updated ${s.name}.`);
136
147
  return notes;
@@ -164,6 +175,7 @@ export function declareEdgeProperty(ont, relRef, name, o) {
164
175
  const decls = (rel.edgeProperties ??= []);
165
176
  if (o.remove) {
166
177
  rel.edgeProperties = decls.filter((d) => d.name !== name);
178
+ ont.touch(rel);
167
179
  return [`Removed edge property ${name} from ${rel.name}. Existing values stay on the edges until you remove them.`];
168
180
  }
169
181
  if (RESERVED_EDGE_KEYS.has(name))
@@ -182,5 +194,6 @@ export function declareEdgeProperty(ont, relRef, name, o) {
182
194
  decls[i] = decl;
183
195
  else
184
196
  decls.push(decl);
197
+ ont.touch(rel);
185
198
  return [`${rel.name} edges can now carry ${name} (${t}${decl.required ? ", required" : ""}): (…)-[:${rel.relType} {${name}: …}]->(…)`];
186
199
  }
@@ -1,4 +1,4 @@
1
- import { type Ontology, type Literal, type SlotNode } from "@ontology-kit/format";
1
+ import { type Ontology, type Literal, type SlotNode } from "@ontology-kit/reader";
2
2
  import { type Notes } from "./common.js";
3
3
  export declare function parseAssignments(args: string[]): {
4
4
  slot: string;