@ontology-kit/okb 0.3.0-alpha.23 → 0.3.0-alpha.68
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/dist/check.js +21 -2
- package/dist/checks.js +23 -0
- package/dist/cli.js +2 -2
- package/dist/command-table.d.ts +21 -11
- package/dist/command-table.js +24 -21
- package/dist/commands/documents.js +2 -2
- package/dist/commands/modeling.js +68 -34
- package/dist/commands/planning.js +40 -1
- package/dist/commands/reviewing.js +52 -10
- package/dist/cqanswer.d.ts +51 -0
- package/dist/cqanswer.js +76 -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.d.ts +33 -2
- package/dist/export.js +69 -11
- package/dist/ops/admin.d.ts +1 -1
- package/dist/ops/admin.js +2 -1
- package/dist/ops/classes.js +2 -2
- package/dist/ops/common.d.ts +12 -2
- package/dist/ops/common.js +3 -0
- package/dist/ops/instances.js +2 -2
- package/dist/ops/links.d.ts +1 -1
- package/dist/ops/links.js +2 -1
- package/dist/ops/scope.d.ts +9 -2
- package/dist/ops/scope.js +19 -3
- package/dist/ops/slots.js +6 -6
- package/dist/provenance.d.ts +1 -1
- package/dist/provenance.js +3 -2
- package/dist/render.d.ts +15 -2
- package/dist/render.js +25 -5
- package/dist/runner.js +22 -17
- package/dist/verifynote.d.ts +4 -3
- package/dist/verifynote.js +3 -2
- package/dist/workflows.d.ts +31 -7
- package/dist/workflows.js +56 -9
- package/meta-kb/data/edges.json +25 -0
- package/meta-kb/data/manifest.json +4 -4
- package/meta-kb/data/nodes.json +39 -2
- package/package.json +2 -2
- package/schema/node.schema.json +22 -1
- package/skills/build-domain-kb-from-docs/SKILL.md +1 -1
- package/skills/ontology-coach/SKILL.md +2 -2
- package/skills/ontology-maintainer/SKILL.md +1 -1
- package/skills/ontology-review/SKILL.md +1 -1
- package/skills/ontology-signoff/SKILL.md +1 -1
package/dist/check.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { loadSourcePages, runChecks } from "./checks.js";
|
|
2
|
+
import { checkQuestions, describeResult } from "./cqanswer.js";
|
|
2
3
|
import { compareVersions as compareSemver } from "./release.js";
|
|
3
4
|
const open = (fs) => fs.filter((f) => !f.explainedBy);
|
|
4
5
|
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
@@ -8,6 +9,7 @@ export function compareVersions(a, b) {
|
|
|
8
9
|
const x = core(a), y = core(b);
|
|
9
10
|
return x && y ? compareSemver(x, y) : null;
|
|
10
11
|
}
|
|
12
|
+
const SAVE_LEFTOVERS_RULE = "struct-no-save-leftovers";
|
|
11
13
|
const QUOTE_RULES = ["prov-quote-current"];
|
|
12
14
|
const VERIFY_RULES = ["prov-verified", "prov-cites-quote"];
|
|
13
15
|
/** The checks, in the order they're reported. */
|
|
@@ -21,9 +23,13 @@ export const CHECK_STEPS = [
|
|
|
21
23
|
const errors = mine.filter((f) => f.severity === "error");
|
|
22
24
|
const warnings = mine.filter((f) => f.severity === "warning").length;
|
|
23
25
|
const tail = warnings ? ` (${plural(warnings, "warning")} ${warnings === 1 ? "doesn't" : "don't"} block a merge; okb validate --all lists them)` : "";
|
|
26
|
+
// Leftovers of an interrupted save say the files may not belong together, which the other warnings don't, so show them here.
|
|
27
|
+
const torn = mine.filter((f) => f.rule === SAVE_LEFTOVERS_RULE);
|
|
24
28
|
return errors.length
|
|
25
|
-
? { status: "fail", summary: `${plural(errors.length, "error")}${tail}`, problems: errors.map((f) => `${f.rule}: ${f.message}`) }
|
|
26
|
-
:
|
|
29
|
+
? { status: "fail", summary: `${plural(errors.length, "error")}${tail}`, problems: [...errors, ...torn].map((f) => `${f.rule}: ${f.message}`) }
|
|
30
|
+
: torn.length
|
|
31
|
+
? { status: "warn", summary: `no errors, but a save was cut short${tail}`, problems: torn.map((f) => `${f.rule}: ${f.message}`) }
|
|
32
|
+
: { status: "pass", summary: `no errors${tail}`, problems: [] };
|
|
27
33
|
},
|
|
28
34
|
},
|
|
29
35
|
{
|
|
@@ -58,6 +64,19 @@ export const CHECK_STEPS = [
|
|
|
58
64
|
: { status: "pass", summary: "nothing is unverified, unsupported or waiting for approval", problems: [] };
|
|
59
65
|
},
|
|
60
66
|
},
|
|
67
|
+
{
|
|
68
|
+
id: "competency-questions",
|
|
69
|
+
title: "Competency questions are still answered (okb cq check)",
|
|
70
|
+
run({ ont }) {
|
|
71
|
+
const results = checkQuestions(ont);
|
|
72
|
+
const failed = results.filter((r) => r.status === "fail");
|
|
73
|
+
const none = results.filter((r) => r.status === "none").length;
|
|
74
|
+
const tail = none ? ` (${plural(none, "question")} with no answer stored)` : "";
|
|
75
|
+
return failed.length
|
|
76
|
+
? { status: "fail", summary: `${plural(failed.length, "question")} no longer get the expected answer${tail}`, problems: failed.map(describeResult) }
|
|
77
|
+
: { status: "pass", summary: `${results.length - none} of ${plural(results.length, "question")} get the expected answer${tail}`, problems: [] };
|
|
78
|
+
},
|
|
79
|
+
},
|
|
61
80
|
{
|
|
62
81
|
id: "okb-version",
|
|
63
82
|
title: "okb version",
|
package/dist/checks.js
CHANGED
|
@@ -68,8 +68,27 @@ const pipeline = (n) => n;
|
|
|
68
68
|
function isTerminological(ont, c) {
|
|
69
69
|
return Boolean(c.terminological) || ont.meta.kind === "terminological";
|
|
70
70
|
}
|
|
71
|
+
/** The files a save leaves behind when it is cut short (see decision 0033). */
|
|
72
|
+
const SAVE_LEFTOVERS = [".okb.saving.tmp", "okb.json.tmp", "nodes.jsonl.tmp", "edges.jsonl.tmp"];
|
|
71
73
|
export const CHECKS = {
|
|
72
74
|
// Structure
|
|
75
|
+
// Reads the folder, not the graph. A load finishes a marked save, so this sees a marker only when a save began after
|
|
76
|
+
// the load (a command running now, which finishes it) and temp files with no marker (an older okb's save, or a
|
|
77
|
+
// save killed before its marker). An in-memory ontology has no okb.json on disk, so it has no leftovers.
|
|
78
|
+
"struct-no-save-leftovers": (ont) => {
|
|
79
|
+
if (!existsSync(join(ont.root, "okb.json")))
|
|
80
|
+
return [];
|
|
81
|
+
const found = SAVE_LEFTOVERS.filter((f) => existsSync(join(ont.root, f)));
|
|
82
|
+
if (!found.length)
|
|
83
|
+
return [];
|
|
84
|
+
const marked = found.includes(".okb.saving.tmp");
|
|
85
|
+
return [{
|
|
86
|
+
message: marked
|
|
87
|
+
? `A save of ${ont.root} didn't finish: ${found.join(", ")} ${found.length > 1 ? "are" : "is"} left over. If no okb command is running, run any okb command that changes this folder; it finishes the save.`
|
|
88
|
+
: `${found.join(", ")} ${found.length > 1 ? "are" : "is"} left over from a save that was cut short, so a file of this ontology may be older than the others. Compare ${found.length > 1 ? "each with the file it names" : "it with the file it names"} (git diff shows the real one), restore the folder from git or a backup if they differ, then delete the leftover${found.length > 1 ? "s" : ""}. If a command is running now, wait for it.`,
|
|
89
|
+
nodes: [],
|
|
90
|
+
}];
|
|
91
|
+
},
|
|
73
92
|
"struct-well-formed": (ont) => {
|
|
74
93
|
const hits = [];
|
|
75
94
|
const { nodeTypes, edgeTypes } = MetaKB.get().formatRegistry();
|
|
@@ -730,6 +749,10 @@ export const CHECKS = {
|
|
|
730
749
|
const loose = ont.ofType("CompetencyQuestion").filter((q) => ont.targets(q.id, "NEEDS").length === 0);
|
|
731
750
|
return loose.map((q) => ({ message: `Competency question ${q.id} ("${q.text}") isn't linked to anything that answers it.`, nodes: [q.id] }));
|
|
732
751
|
},
|
|
752
|
+
"scope-cq-expected-answer": (ont) => {
|
|
753
|
+
const loose = ont.ofType("CompetencyQuestion").filter((q) => !q.answer);
|
|
754
|
+
return loose.map((q) => ({ message: `Competency question ${q.id} ("${q.text}") has no expected answer, so nothing checks that the ontology still answers it.`, nodes: [q.id] }));
|
|
755
|
+
},
|
|
733
756
|
"scope-no-unneeded": (ont) => {
|
|
734
757
|
const needs = ont.edgesOf("NEEDS").map((e) => e.to);
|
|
735
758
|
if (needs.length === 0)
|
package/dist/cli.js
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
/** okb: command-line interface. Run `okb help` for usage. */
|
|
3
3
|
import { OkbError } from "@ontology-kit/reader";
|
|
4
4
|
import { COMMANDS } from "./commands.js";
|
|
5
|
-
import { isParseError, leadingGlobals } from "./command-table.js";
|
|
5
|
+
import { asksForHelp, isParseError, leadingGlobals } from "./command-table.js";
|
|
6
6
|
import { runCommand } from "./runner.js";
|
|
7
7
|
import { SYMBOL } from "./render.js";
|
|
8
8
|
async function main(argv) {
|
|
9
9
|
// Global options (-C, --json) may come before the command; they also work after it.
|
|
10
10
|
const { globals, rest } = leadingGlobals(argv);
|
|
11
|
-
const words =
|
|
11
|
+
const words = asksForHelp(rest) ? ["help"] : rest;
|
|
12
12
|
await runCommand(COMMANDS, words, globals);
|
|
13
13
|
}
|
|
14
14
|
try {
|
package/dist/command-table.d.ts
CHANGED
|
@@ -42,7 +42,6 @@ export interface Ctx {
|
|
|
42
42
|
mutateAsync(fn: (ont: Ontology) => Promise<Notes>): Promise<void>;
|
|
43
43
|
/** Hold the folder's lock while `fn` runs; `fn` saves what it changed. */
|
|
44
44
|
edit<T>(fn: (ont: Ontology) => T): T;
|
|
45
|
-
editAsync<T>(fn: (ont: Ontology) => Promise<T>): Promise<T>;
|
|
46
45
|
/** The ontology folder as given by `-C` (default `.`). */
|
|
47
46
|
readonly dir: string;
|
|
48
47
|
/** Print `data` as JSON under --json, otherwise the lines the printer builds. Nothing prints for no lines. */
|
|
@@ -60,7 +59,20 @@ export interface Ctx {
|
|
|
60
59
|
/** Set the process exit code. */
|
|
61
60
|
exit(code: number): void;
|
|
62
61
|
}
|
|
63
|
-
|
|
62
|
+
/** What a form says about its arguments: any of `min`, `max` or `require` needs the `usage` message that wrong arguments print. */
|
|
63
|
+
type ArgumentRules = {
|
|
64
|
+
usage: string;
|
|
65
|
+
min?: number;
|
|
66
|
+
max?: number;
|
|
67
|
+
require?: string[];
|
|
68
|
+
} | {
|
|
69
|
+
usage?: string;
|
|
70
|
+
min?: undefined;
|
|
71
|
+
max?: undefined;
|
|
72
|
+
require?: undefined;
|
|
73
|
+
};
|
|
74
|
+
export type Form = FormBase & ArgumentRules;
|
|
75
|
+
interface FormBase {
|
|
64
76
|
/** The subcommand word, if any: `add` for `okb cq add`. A form without one is the bare command. */
|
|
65
77
|
sub?: string;
|
|
66
78
|
description: string;
|
|
@@ -69,13 +81,6 @@ export interface Form {
|
|
|
69
81
|
args: Arg[];
|
|
70
82
|
/** The options this form parses (global `-C` and `--json` are always accepted). */
|
|
71
83
|
options?: OptionSpecs;
|
|
72
|
-
/** The message when the arguments are wrong. Required wherever `min`, `max` or `require` is set. */
|
|
73
|
-
usage?: string;
|
|
74
|
-
/** Fewest and most positional arguments. */
|
|
75
|
-
min?: number;
|
|
76
|
-
max?: number;
|
|
77
|
-
/** Options that must be given. */
|
|
78
|
-
require?: string[];
|
|
79
84
|
/** Left out of the help text and docs/CLI.md (an internal or deprecated form). */
|
|
80
85
|
hidden?: boolean;
|
|
81
86
|
/** One card on docs/CLI-CHEAT-SHEET.md. */
|
|
@@ -100,8 +105,6 @@ export interface Command {
|
|
|
100
105
|
bare?: string;
|
|
101
106
|
/** Overrides the "Usage: okb x add|set ..." message for an unknown subcommand. */
|
|
102
107
|
usage?: string;
|
|
103
|
-
/** Rewrites the command line to another command's (everyday names for slot kinds). */
|
|
104
|
-
rewrite?: (rest: string[]) => string[];
|
|
105
108
|
/** Left out of the help text and docs/CLI.md. */
|
|
106
109
|
hidden?: boolean;
|
|
107
110
|
}
|
|
@@ -115,6 +118,12 @@ export declare function leadingGlobals(argv: readonly string[]): {
|
|
|
115
118
|
globals: Partial<Globals>;
|
|
116
119
|
rest: string[];
|
|
117
120
|
};
|
|
121
|
+
/**
|
|
122
|
+
* Whether the words ask for the help text: none at all, or `--help` / `-h` before a `--`. After `--` every word is an
|
|
123
|
+
* operand (`okb show -- --help` looks up a thing called "--help"). An option's value can't be `--help` or `-h` here,
|
|
124
|
+
* because parseArgs refuses a value that starts with a dash, so the other places the token can sit still mean help.
|
|
125
|
+
*/
|
|
126
|
+
export declare function asksForHelp(words: readonly string[]): boolean;
|
|
118
127
|
export interface Resolved {
|
|
119
128
|
cmd: Command;
|
|
120
129
|
form: Form;
|
|
@@ -132,3 +141,4 @@ export declare function parseForm(form: Form, rest: readonly string[]): {
|
|
|
132
141
|
export declare const isParseError: (e: unknown) => boolean;
|
|
133
142
|
/** "true", "yes" or "1" (any case) is true; anything else is false; absent stays undefined. */
|
|
134
143
|
export declare const tri: (x: string | undefined) => boolean | undefined;
|
|
144
|
+
export {};
|
package/dist/command-table.js
CHANGED
|
@@ -39,30 +39,33 @@ export function leadingGlobals(argv) {
|
|
|
39
39
|
}
|
|
40
40
|
return { globals, rest };
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Whether the words ask for the help text: none at all, or `--help` / `-h` before a `--`. After `--` every word is an
|
|
44
|
+
* operand (`okb show -- --help` looks up a thing called "--help"). An option's value can't be `--help` or `-h` here,
|
|
45
|
+
* because parseArgs refuses a value that starts with a dash, so the other places the token can sit still mean help.
|
|
46
|
+
*/
|
|
47
|
+
export function asksForHelp(words) {
|
|
48
|
+
if (words.length === 0)
|
|
49
|
+
return true;
|
|
50
|
+
const end = words.indexOf("--");
|
|
51
|
+
return (end < 0 ? words : words.slice(0, end)).some((w) => w === "--help" || w === "-h");
|
|
52
|
+
}
|
|
42
53
|
const runnable = (cmd) => cmd.forms.filter((f) => f.run);
|
|
43
54
|
const subWords = (cmd) => runnable(cmd).filter((f) => f.sub && !f.hidden).map((f) => f.sub);
|
|
44
55
|
/** Find the command and form for the words after global options; throws a usage error if there is none. */
|
|
45
56
|
export function resolve(commands, argv) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (named)
|
|
59
|
-
return { cmd, form: named, rest: rest.slice(1) };
|
|
60
|
-
const bare = forms.find((f) => !f.sub) ?? forms.find((f) => f.sub === cmd.bare);
|
|
61
|
-
if (!bare)
|
|
62
|
-
throw new OkbError(cmd.usage ?? `Usage: okb ${cmd.name} ${subWords(cmd).join("|")} ... (okb help)`);
|
|
63
|
-
return { cmd, form: bare, rest };
|
|
64
|
-
}
|
|
65
|
-
throw new OkbError("Command rewrite loop.");
|
|
57
|
+
const [name, ...rest] = argv;
|
|
58
|
+
const cmd = commands.find((c) => c.name === name || c.aliases?.includes(name));
|
|
59
|
+
if (!cmd)
|
|
60
|
+
throw new OkbError(`Unknown command '${name}'. Run okb help.`);
|
|
61
|
+
const forms = runnable(cmd);
|
|
62
|
+
const named = rest[0] === undefined ? undefined : forms.find((f) => f.sub === rest[0]);
|
|
63
|
+
if (named)
|
|
64
|
+
return { cmd, form: named, rest: rest.slice(1) };
|
|
65
|
+
const bare = forms.find((f) => !f.sub) ?? forms.find((f) => f.sub === cmd.bare);
|
|
66
|
+
if (!bare)
|
|
67
|
+
throw new OkbError(cmd.usage ?? `Usage: okb ${cmd.name} ${subWords(cmd).join("|")} ... (okb help)`);
|
|
68
|
+
return { cmd, form: bare, rest };
|
|
66
69
|
}
|
|
67
70
|
const GLOBAL_OPTIONS = { C: { type: "string", short: "C" }, json: bool };
|
|
68
71
|
/** Parse a form's words with its options; wrong arguments throw the form's usage message. */
|
|
@@ -73,7 +76,7 @@ export function parseForm(form, rest) {
|
|
|
73
76
|
(form.max !== undefined && positionals.length > form.max) ||
|
|
74
77
|
(form.require ?? []).some((k) => !opts[k]);
|
|
75
78
|
if (bad)
|
|
76
|
-
throw new OkbError(form.usage);
|
|
79
|
+
throw new OkbError(form.usage ?? "");
|
|
77
80
|
return { opts, args: positionals };
|
|
78
81
|
}
|
|
79
82
|
/** True for a parse error from `parseArgs` (an unknown option, a missing value). */
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { opt } from "../command-table.js";
|
|
3
3
|
import { OkbError } from "@ontology-kit/reader";
|
|
4
4
|
import * as prov from "../provenance.js";
|
|
5
|
-
import {
|
|
5
|
+
import { renderOutline, renderQuotes, renderRules, renderSources } from "../render.js";
|
|
6
6
|
import { renderResync } from "../resync.js";
|
|
7
7
|
import { outline, quotes, refreshAll } from "../workflows.js";
|
|
8
8
|
const { str, strs, bool } = opt;
|
|
@@ -68,7 +68,7 @@ export const documents = [
|
|
|
68
68
|
if (ctx.args.length)
|
|
69
69
|
throw new OkbError("--all re-checks every source; leave out the source name (or drop --all).");
|
|
70
70
|
const { report, write } = await refreshAll(ctx.dir, { dryRun: o["dry-run"], record: o.record });
|
|
71
|
-
ctx.emit(() => report, () =>
|
|
71
|
+
ctx.emit(() => report, () => renderResync(report, { dryRun: !write }));
|
|
72
72
|
// 1 when the KB no longer matches its documents (a quote is gone or a source can't be read); --strict also when blocks need work.
|
|
73
73
|
if (!report.ok || (o.strict && report.worklist.length))
|
|
74
74
|
ctx.exit(1);
|
|
@@ -12,13 +12,19 @@ const slotOpts = (ctx) => {
|
|
|
12
12
|
card: o.card, min: ctx.int("min"), max: ctx.int("max"), default: o.default, description: o.desc, force: o.force, onRules: o["on-rules"], offRules: o["off-rules"],
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
|
-
/**
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
/** Every option `slot add`, `slot set` and their friendlier names (`property`, `relationship`) parse. */
|
|
16
|
+
const slotOptions = { on: strs, off: strs, type: str, values: strs, range: strs, "add-range": strs, "remove-range": strs, card: str, min: str, max: str, default: str, desc: str, force: bool, "on-rules": bool, "off-rules": bool };
|
|
17
|
+
/** `relationship` also takes `--from` and `--to`, which are the slot's `--on` and `--range`. */
|
|
18
|
+
const relationshipOptions = { ...slotOptions, from: strs, to: strs };
|
|
19
|
+
/** The values of two options that name the same thing, or undefined when neither was given. */
|
|
20
|
+
const either = (a, b) => (a || b ? [...(a ?? []), ...(b ?? [])] : undefined);
|
|
21
|
+
/** The slot options of a `relationship` form: `--from` is `--on`, `--to` is `--range`, and `add` always makes an Instance slot. */
|
|
22
|
+
const relationshipSlotOpts = (ctx, add) => ({
|
|
23
|
+
...slotOpts(ctx),
|
|
24
|
+
on: either(ctx.opts.on, ctx.opts.from),
|
|
25
|
+
range: either(ctx.opts.range, ctx.opts.to),
|
|
26
|
+
...(add ? { type: "Instance" } : {}),
|
|
27
|
+
});
|
|
22
28
|
export const modeling = [
|
|
23
29
|
{
|
|
24
30
|
name: "convention",
|
|
@@ -175,7 +181,7 @@ export const modeling = [
|
|
|
175
181
|
group: "Modeling",
|
|
176
182
|
summary: "add a slot that holds a plain value",
|
|
177
183
|
description: "A slot that holds a plain value, such as a number, text or one of a fixed list. It takes the same subcommands as `okb slot`.",
|
|
178
|
-
|
|
184
|
+
usage: "Usage: okb property add|set|inverse ... (okb help)",
|
|
179
185
|
forms: [
|
|
180
186
|
{
|
|
181
187
|
sub: "add",
|
|
@@ -189,7 +195,32 @@ export const modeling = [
|
|
|
189
195
|
["--card", "single or multiple", "Whether it holds one value or several."],
|
|
190
196
|
["--desc", "text", "A one-line description."],
|
|
191
197
|
],
|
|
198
|
+
options: slotOptions,
|
|
199
|
+
usage: "Usage: okb slot add <name> --on <Class> --type <Type> [--card single|multiple] ...",
|
|
200
|
+
min: 1,
|
|
192
201
|
cheat: { example: "okb property add body --on Wine --type Enumerated --values light,full", description: "Add a plain-value slot" },
|
|
202
|
+
run: (ctx) => ctx.mutate((ont) => ops.addSlot(ont, ctx.args[0], slotOpts(ctx))),
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
sub: "set",
|
|
206
|
+
description: "Changes a property. It takes the flags `okb slot set` takes.",
|
|
207
|
+
examples: ["okb property set body --desc \"How heavy the wine feels\""],
|
|
208
|
+
args: [["<property>", "slot", "The property to change."]],
|
|
209
|
+
options: slotOptions,
|
|
210
|
+
usage: "Usage: okb slot set <slot> [--on C] [--type T] [--values a,b] [--range C] [--card single|multiple] ...",
|
|
211
|
+
min: 1,
|
|
212
|
+
run: (ctx) => ctx.mutate((ont) => ops.updateSlot(ont, ctx.args[0], slotOpts(ctx))),
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
sub: "inverse",
|
|
216
|
+
description: "Same as `okb slot inverse`: only relationships have inverses.",
|
|
217
|
+
examples: [],
|
|
218
|
+
args: [],
|
|
219
|
+
usage: "Usage: okb slot inverse <slotA> <slotB>",
|
|
220
|
+
min: 2,
|
|
221
|
+
max: 2,
|
|
222
|
+
hidden: true,
|
|
223
|
+
run: (ctx) => ctx.mutate((ont) => ops.inverse(ont, ctx.args[0], ctx.args[1])),
|
|
193
224
|
},
|
|
194
225
|
],
|
|
195
226
|
},
|
|
@@ -198,7 +229,7 @@ export const modeling = [
|
|
|
198
229
|
group: "Modeling",
|
|
199
230
|
summary: "add a slot that links things",
|
|
200
231
|
description: "A slot that links one thing to another, such as a wine to its winery. Okb stores it as edges, so `maker` becomes `:MAKER`.",
|
|
201
|
-
|
|
232
|
+
usage: "Usage: okb relationship add|set|inverse|property ... (okb help)",
|
|
202
233
|
forms: [
|
|
203
234
|
{
|
|
204
235
|
sub: "add",
|
|
@@ -210,7 +241,21 @@ export const modeling = [
|
|
|
210
241
|
["--to", "class", "The class it points to."],
|
|
211
242
|
["--card", "single or multiple", "Whether it holds one target or several."],
|
|
212
243
|
],
|
|
244
|
+
options: relationshipOptions,
|
|
245
|
+
usage: "Usage: okb slot add <name> --on <Class> --type <Type> [--card single|multiple] ...",
|
|
246
|
+
min: 1,
|
|
213
247
|
cheat: { example: "okb relationship add maker --from Wine --to Winery", description: "Add a link between classes" },
|
|
248
|
+
run: (ctx) => ctx.mutate((ont) => ops.addSlot(ont, ctx.args[0], relationshipSlotOpts(ctx, true))),
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
sub: "set",
|
|
252
|
+
description: "Changes a relationship. It takes the flags `okb slot set` takes, and `--from` and `--to` name the classes.",
|
|
253
|
+
examples: ["okb relationship set maker --card single --min 1"],
|
|
254
|
+
args: [["<relationship>", "slot", "The relationship to change."]],
|
|
255
|
+
options: relationshipOptions,
|
|
256
|
+
usage: "Usage: okb slot set <slot> [--on C] [--type T] [--values a,b] [--range C] [--card single|multiple] ...",
|
|
257
|
+
min: 1,
|
|
258
|
+
run: (ctx) => ctx.mutate((ont) => ops.updateSlot(ont, ctx.args[0], relationshipSlotOpts(ctx, false))),
|
|
214
259
|
},
|
|
215
260
|
{
|
|
216
261
|
sub: "inverse",
|
|
@@ -220,6 +265,10 @@ export const modeling = [
|
|
|
220
265
|
["<slotA>", "slot", "The relationship that is stored."],
|
|
221
266
|
["<slotB>", "slot", "Its inverse."],
|
|
222
267
|
],
|
|
268
|
+
usage: "Usage: okb slot inverse <slotA> <slotB>",
|
|
269
|
+
min: 2,
|
|
270
|
+
max: 2,
|
|
271
|
+
run: (ctx) => ctx.mutate((ont) => ops.inverse(ont, ctx.args[0], ctx.args[1])),
|
|
223
272
|
},
|
|
224
273
|
{
|
|
225
274
|
sub: "property",
|
|
@@ -234,6 +283,14 @@ export const modeling = [
|
|
|
234
283
|
["--desc", "text", "A one-line description."],
|
|
235
284
|
["--remove", "flag", "Remove the property."],
|
|
236
285
|
],
|
|
286
|
+
options: { type: str, values: strs, required: bool, desc: str, remove: bool },
|
|
287
|
+
usage: "Usage: okb relationship property <relationship> <name> --type String|Integer|Float|Boolean|Enumerated [--values a,b] [--required] [--desc ...] [--remove]",
|
|
288
|
+
min: 2,
|
|
289
|
+
max: 2,
|
|
290
|
+
run: (ctx) => {
|
|
291
|
+
const o = ctx.opts;
|
|
292
|
+
ctx.mutate((ont) => ops.declareEdgeProperty(ont, ctx.args[0], ctx.args[1], { type: o.type, values: o.values, required: o.required, description: o.desc, remove: o.remove }));
|
|
293
|
+
},
|
|
237
294
|
},
|
|
238
295
|
],
|
|
239
296
|
},
|
|
@@ -260,7 +317,7 @@ export const modeling = [
|
|
|
260
317
|
["--desc", "text", "A one-line description."],
|
|
261
318
|
["--force", "flag", "Add it even if the name breaks your convention."],
|
|
262
319
|
],
|
|
263
|
-
options:
|
|
320
|
+
options: slotOptions,
|
|
264
321
|
usage: "Usage: okb slot add <name> --on <Class> --type <Type> [--card single|multiple] ...",
|
|
265
322
|
min: 1,
|
|
266
323
|
run: (ctx) => ctx.mutate((ont) => ops.addSlot(ont, ctx.args[0], slotOpts(ctx))),
|
|
@@ -277,7 +334,7 @@ export const modeling = [
|
|
|
277
334
|
["--on-rules", "flag", "Let rules set this slot."],
|
|
278
335
|
["--off-rules", "flag", "Stop rules setting this slot."],
|
|
279
336
|
],
|
|
280
|
-
options:
|
|
337
|
+
options: slotOptions,
|
|
281
338
|
usage: "Usage: okb slot set <slot> [--on C] [--type T] [--values a,b] [--range C] [--card single|multiple] ...",
|
|
282
339
|
min: 1,
|
|
283
340
|
cheat: { example: "okb slot set maker --card single --min 1", description: "Tighten a slot" },
|
|
@@ -407,27 +464,4 @@ export const modeling = [
|
|
|
407
464
|
},
|
|
408
465
|
],
|
|
409
466
|
},
|
|
410
|
-
{
|
|
411
|
-
name: "edgeprop",
|
|
412
|
-
group: "Modeling",
|
|
413
|
-
description: "Declares a property on a relationship. Reached through okb relationship property.",
|
|
414
|
-
hidden: true,
|
|
415
|
-
forms: [
|
|
416
|
-
{
|
|
417
|
-
sub: "property",
|
|
418
|
-
description: "",
|
|
419
|
-
examples: [],
|
|
420
|
-
args: [],
|
|
421
|
-
options: { type: str, values: strs, required: bool, desc: str, remove: bool },
|
|
422
|
-
usage: "Usage: okb relationship property <relationship> <name> --type String|Integer|Float|Boolean|Enumerated [--values a,b] [--required] [--desc ...] [--remove]",
|
|
423
|
-
min: 2,
|
|
424
|
-
max: 2,
|
|
425
|
-
hidden: true,
|
|
426
|
-
run: (ctx) => {
|
|
427
|
-
const o = ctx.opts;
|
|
428
|
-
ctx.mutate((ont) => ops.declareEdgeProperty(ont, ctx.args[0], ctx.args[1], { type: o.type, values: o.values, required: o.required, description: o.desc, remove: o.remove }));
|
|
429
|
-
},
|
|
430
|
-
},
|
|
431
|
-
],
|
|
432
|
-
},
|
|
433
467
|
];
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/** Planning: scope, competency questions, reuse and terms (steps 1 to 3). */
|
|
2
2
|
import { opt } from "../command-table.js";
|
|
3
|
-
import {} from "@ontology-kit/reader";
|
|
3
|
+
import { OkbError } from "@ontology-kit/reader";
|
|
4
4
|
import * as ops from "../ops.js";
|
|
5
|
+
import { checkQuestions, describeResult } from "../cqanswer.js";
|
|
5
6
|
import { renderQuestions, renderReuse, renderScope, renderTerms } from "../render.js";
|
|
6
7
|
const { str, strs } = opt;
|
|
7
8
|
const SCOPE_OPTIONS = { name: str, domain: str, purpose: str, users: strs, maintainers: strs, "out-of-scope": strs, kind: str };
|
|
@@ -95,6 +96,44 @@ export const planning = [
|
|
|
95
96
|
options: { text: str, status: str },
|
|
96
97
|
run: (ctx) => ctx.mutate((ont) => ops.setCQ(ont, ctx.args[0], { text: ctx.opts.text, status: ctx.opts.status })),
|
|
97
98
|
},
|
|
99
|
+
{
|
|
100
|
+
sub: "answer",
|
|
101
|
+
description: "Stores the answer a question should get: a query (one JSON object) and the names it must return. The query is `{\"find\": {\"class\": ..., \"where\": {slot: value}}}` (instances of a class), `{\"traverse\": {\"from\": ..., \"slot\": ...}}` (what a slot gives for one thing) or `{\"slots\": {\"of\": ...}}` (a thing's slots). `okb cq check` runs it later.",
|
|
102
|
+
examples: ["okb cq answer 6 --query '{\"traverse\":{\"from\":\"Chateau Morgon\",\"slot\":\"produces\"}}' --expect \"Chateau Morgon Beaujolais\""],
|
|
103
|
+
args: [
|
|
104
|
+
["<number>", "number", "The question's number."],
|
|
105
|
+
["--query", "JSON", "The query to run."],
|
|
106
|
+
["--expect", "list", "The names it must return, separated by commas."],
|
|
107
|
+
["--exact", "flag", "Fail on a name that isn't listed (the default)."],
|
|
108
|
+
["--includes", "flag", "Pass as long as the listed names are there."],
|
|
109
|
+
],
|
|
110
|
+
options: { query: str, expect: strs, exact: opt.bool, includes: opt.bool },
|
|
111
|
+
usage: "Usage: okb cq answer <question number> --query '<JSON>' --expect A,B,C [--exact|--includes]",
|
|
112
|
+
min: 1,
|
|
113
|
+
max: 1,
|
|
114
|
+
cheat: { example: "okb cq answer 1 --query '{...}' --expect A,B", description: "Store a question's expected answer" },
|
|
115
|
+
run: (ctx) => {
|
|
116
|
+
const o = ctx.opts;
|
|
117
|
+
if (o.query === undefined || o.expect === undefined)
|
|
118
|
+
throw new OkbError("cq answer needs --query and --expect.");
|
|
119
|
+
if (o.exact && o.includes)
|
|
120
|
+
throw new OkbError("Use --exact or --includes, not both.");
|
|
121
|
+
ctx.mutate((ont) => ops.answerCQ(ont, ctx.args[0], { query: o.query, expect: o.expect, match: o.includes ? "includes" : "exact" }));
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
sub: "check",
|
|
126
|
+
description: "Runs each question's stored query and compares the names with the expected answer. Prints pass, fail (with the missing and extra names) or no answer stored for each question, and exits non-zero when any fails. `okb check` includes it.",
|
|
127
|
+
examples: ["okb cq check", "okb cq check --json"],
|
|
128
|
+
args: [],
|
|
129
|
+
usage: "Usage: okb cq check [--json]",
|
|
130
|
+
max: 0,
|
|
131
|
+
run: (ctx) => {
|
|
132
|
+
const results = checkQuestions(ctx.load());
|
|
133
|
+
ctx.emit(() => results, () => (results.length ? results.map(describeResult) : ["No competency questions yet."]));
|
|
134
|
+
ctx.exit(results.some((r) => r.status === "fail") ? 1 : 0);
|
|
135
|
+
},
|
|
136
|
+
},
|
|
98
137
|
{
|
|
99
138
|
sub: "remove",
|
|
100
139
|
description: "Deletes a question.",
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/** Reviewing and sharing: review questions, decisions, diagrams, exports and reference pages (step 8). */
|
|
2
2
|
import { opt } from "../command-table.js";
|
|
3
|
-
import {} from "@ontology-kit/reader";
|
|
3
|
+
import { OkbError, query } from "@ontology-kit/reader";
|
|
4
4
|
import * as ops from "../ops.js";
|
|
5
5
|
import { buildReview } from "../review.js";
|
|
6
|
-
import { mermaid, renderDecisions, renderDiagramWritten, renderExportWritten, renderReview, renderWrote } from "../render.js";
|
|
6
|
+
import { mermaid, renderDecisions, renderQueryResult, renderChangeSetWritten, renderDiagramWritten, renderExportWritten, renderReview, renderWrote } from "../render.js";
|
|
7
7
|
import { docs, exportOntology, reviewData } from "../workflows.js";
|
|
8
8
|
const { str, strs, bool } = opt;
|
|
9
9
|
export const reviewing = [
|
|
@@ -102,26 +102,37 @@ export const reviewing = [
|
|
|
102
102
|
{
|
|
103
103
|
name: "export",
|
|
104
104
|
group: "Reviewing and sharing",
|
|
105
|
-
summary: "write JSON or
|
|
106
|
-
description: "Writes the ontology as JSON
|
|
105
|
+
summary: "write JSON, openCypher or bulk-load CSV",
|
|
106
|
+
description: "Writes the ontology as JSON, as openCypher, or as CSV files for the Neo4j and Neptune bulk loaders (okb writes files and never connects to a database). Instances become nodes labeled with their classes.",
|
|
107
107
|
forms: [
|
|
108
108
|
{
|
|
109
109
|
description: "Prints the export, or writes it to a file.",
|
|
110
|
-
examples: ["okb export --format cypher --out wine.cypher"],
|
|
110
|
+
examples: ["okb export --format cypher --out wine.cypher", "okb export --format neo4j-csv --out wine-neo4j", "okb export --format cypher --since 1.0.0 --out wine-1.1.0.cypher"],
|
|
111
111
|
args: [
|
|
112
|
-
["--format", "json or
|
|
113
|
-
["--out", "path", "Write to this file instead of printing."],
|
|
112
|
+
["--format", "json, cypher, neo4j-csv or neptune-csv", "The output format. Defaults to json. The two CSV formats are bulk-loader files (`neo4j-admin database import`, Neptune bulk loader) and write several files into the folder --out names."],
|
|
113
|
+
["--out", "path", "Write to this file instead of printing. For neo4j-csv and neptune-csv it is a folder, which is required."],
|
|
114
|
+
["--style", "batched or simple", "For cypher: batched (default) is a constraint plus UNWIND/MERGE batches you can load again; simple is one readable CREATE per node and edge, for an empty database."],
|
|
115
|
+
["--since", "release or git ref", "For cypher: write a change set instead of a full load. It moves a database loaded from that earlier release to this folder's release: MERGE for what was added or changed, removed properties, DETACH DELETE for removed nodes and deletes for removed relationships, behind a guard that stops before changing anything unless the database's (:OkbRelease) node holds that version. The folder must be at its latest release (`okb release`). Use the same flags as the full export."],
|
|
114
116
|
["--with-schema", "flag", "Include classes and slots, not only instances."],
|
|
115
117
|
["--no-inherited", "flag", "Leave out values an instance inherits from its class."],
|
|
116
118
|
["--no-rules", "flag", "Leave out extracted rules."],
|
|
117
119
|
["--include-unverified", "flag", "Include rules and quotes that haven't been verified."],
|
|
118
120
|
],
|
|
119
|
-
options: { format: str, out: str, "with-schema": bool, "no-inherited": bool, "no-rules": bool, "include-unverified": bool },
|
|
121
|
+
options: { format: str, out: str, style: str, since: str, "with-schema": bool, "no-inherited": bool, "no-rules": bool, "include-unverified": bool },
|
|
120
122
|
cheat: { example: "okb export --format cypher --out wine.cypher", description: "Export for a graph database" },
|
|
121
123
|
run: (ctx) => {
|
|
122
124
|
const o = ctx.opts;
|
|
123
|
-
const { fmt, graph, text } = exportOntology(ctx.load(), { format: o.format, out: o.out, schema: o["with-schema"], noInherited: o["no-inherited"], noRules: o["no-rules"], includeUnverified: o["include-unverified"] });
|
|
124
|
-
if (o.out) {
|
|
125
|
+
const { fmt, graph, text, files, changes } = exportOntology(ctx.load(), { format: o.format, out: o.out, style: o.style, since: o.since, schema: o["with-schema"], noInherited: o["no-inherited"], noRules: o["no-rules"], includeUnverified: o["include-unverified"] });
|
|
126
|
+
if (o.out && files) {
|
|
127
|
+
for (const [name, body] of Object.entries(files))
|
|
128
|
+
ctx.write(`${o.out}/${name}`, body);
|
|
129
|
+
ctx.print(renderExportWritten(o.out, graph, fmt, Object.keys(files)));
|
|
130
|
+
}
|
|
131
|
+
else if (o.out && changes) {
|
|
132
|
+
ctx.write(o.out, text);
|
|
133
|
+
ctx.print(renderChangeSetWritten(o.out, changes));
|
|
134
|
+
}
|
|
135
|
+
else if (o.out) {
|
|
125
136
|
ctx.write(o.out, text);
|
|
126
137
|
ctx.print(renderExportWritten(o.out, graph, fmt));
|
|
127
138
|
}
|
|
@@ -131,6 +142,37 @@ export const reviewing = [
|
|
|
131
142
|
},
|
|
132
143
|
],
|
|
133
144
|
},
|
|
145
|
+
{
|
|
146
|
+
name: "query",
|
|
147
|
+
group: "Reviewing and sharing",
|
|
148
|
+
summary: "run one read-only query and print what it returns",
|
|
149
|
+
description: "Runs one query against the ontology and prints what it returns, the same every time, for a person or a script. The query is the one-key JSON object `okb cq answer` stores, so a query that works here can be stored with `okb cq answer` unchanged. It never changes the ontology. To look at one thing, use `okb show`.",
|
|
150
|
+
forms: [
|
|
151
|
+
{
|
|
152
|
+
description: "Runs the query. `{\"find\": {\"class\": ..., \"where\": {slot: value}}}` lists the instances of a class (and its subclasses) whose values match, `{\"traverse\": {\"from\": ..., \"slot\": ...}}` lists what a slot gives for one thing (or a list of things), and `{\"slots\": {\"of\": ...}}` lists a thing's slots. Quote the JSON so your shell passes it as one argument. With `--json` it prints `{kind, hits}`.",
|
|
153
|
+
examples: ["okb query '{\"find\":{\"class\":\"Drink\"}}'", "okb query '{\"slots\":{\"of\":\"Drink\"}}' --json"],
|
|
154
|
+
args: [
|
|
155
|
+
["<json>", "JSON", "The query: an object with one key, `find`, `traverse` or `slots`."],
|
|
156
|
+
],
|
|
157
|
+
usage: "Usage: okb query '<JSON>' [--json]",
|
|
158
|
+
min: 1,
|
|
159
|
+
max: 1,
|
|
160
|
+
cheat: { example: "okb query '{\"find\":{\"class\":\"Drink\"}}'", description: "Run a read-only query" },
|
|
161
|
+
run: (ctx) => {
|
|
162
|
+
let q;
|
|
163
|
+
try {
|
|
164
|
+
q = JSON.parse(ctx.args[0]);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
throw new OkbError(`The query isn't valid JSON. Quote it as one argument. Example: okb query '{"find":{"class":"Wine","where":{"color":"red"}}}'`);
|
|
168
|
+
}
|
|
169
|
+
const ont = ctx.load();
|
|
170
|
+
const result = query(ont, q);
|
|
171
|
+
ctx.emit(() => result, () => renderQueryResult(ont, result));
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
},
|
|
134
176
|
{
|
|
135
177
|
name: "docs",
|
|
136
178
|
group: "Reviewing and sharing",
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expected answers for competency questions (`okb cq answer`, `okb cq check`).
|
|
3
|
+
*
|
|
4
|
+
* A question stores a declarative query and the names it should return. The query is one of three
|
|
5
|
+
* shapes, each a JSON object with one key, modeled on the read tools an agent uses:
|
|
6
|
+
*
|
|
7
|
+
* {"find": {"class": "Wine", "where": {"color": "red"}}} instances of a class (and its subclasses) whose slot values match
|
|
8
|
+
* {"traverse": {"from": "Chateau Morgon", "slot": "produces"}} what a slot gives for one class or instance
|
|
9
|
+
* {"slots": {"of": "RedWine"}} the slots a class or instance has
|
|
10
|
+
*
|
|
11
|
+
* Evaluation is the reader's query layer (`query` and `resultNames`, see docs/FORMAT.md); this file only
|
|
12
|
+
* stores answers and compares names. It reads the ontology and never writes it.
|
|
13
|
+
*/
|
|
14
|
+
import { type CompetencyQuestionNode, type Ontology } from "@ontology-kit/reader";
|
|
15
|
+
export type MatchMode = "exact" | "includes";
|
|
16
|
+
export type AnswerQuery = Record<string, unknown>;
|
|
17
|
+
export interface Answer {
|
|
18
|
+
query: AnswerQuery;
|
|
19
|
+
expect: string[];
|
|
20
|
+
match?: MatchMode;
|
|
21
|
+
}
|
|
22
|
+
export type CqStatus = "pass" | "fail" | "none";
|
|
23
|
+
export interface CqResult {
|
|
24
|
+
number: number;
|
|
25
|
+
id: string;
|
|
26
|
+
text: string;
|
|
27
|
+
status: CqStatus;
|
|
28
|
+
/** Expected names the query didn't return. */
|
|
29
|
+
missing: string[];
|
|
30
|
+
/** Names the query returned that weren't expected (a failure only for `exact`). */
|
|
31
|
+
extra: string[];
|
|
32
|
+
/** What the query returned. */
|
|
33
|
+
actual: string[];
|
|
34
|
+
/** Why the query couldn't run, for example a name that no longer exists. */
|
|
35
|
+
error?: string;
|
|
36
|
+
/** Set when the query has a `*` in `where`, which the reader reads as "has any value". */
|
|
37
|
+
note?: string;
|
|
38
|
+
match?: MatchMode;
|
|
39
|
+
}
|
|
40
|
+
/** Parse a stored query's JSON and check its shape with the reader, or throw an `OkbError` saying what is wrong. Names aren't resolved here. */
|
|
41
|
+
export declare function parseQuery(text: string): AnswerQuery;
|
|
42
|
+
/** Run a query through the reader. Throws an `OkbError` when a name in it doesn't exist. Names come back sorted. */
|
|
43
|
+
export declare const evaluate: (ont: Ontology, q: AnswerQuery) => string[];
|
|
44
|
+
/** Compare what a query returned with what was expected, ignoring case. */
|
|
45
|
+
export declare function compare(actual: string[], answer: Answer): Pick<CqResult, "status" | "missing" | "extra" | "actual" | "match">;
|
|
46
|
+
/** Evaluate one question's stored answer. */
|
|
47
|
+
export declare function checkQuestion(ont: Ontology, q: CompetencyQuestionNode): CqResult;
|
|
48
|
+
/** Evaluate every question, in number order. */
|
|
49
|
+
export declare function checkQuestions(ont: Ontology): CqResult[];
|
|
50
|
+
/** One line per result, for the terminal and for `okb check`'s problem list. */
|
|
51
|
+
export declare function describeResult(r: CqResult): string;
|