@mark1russell7/ontology 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mark Russell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,120 @@
1
- # Temporary Holding Version
1
+ # @mark1russell7/ontology
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A grammar of kinds and relations, a registry that checks records, claims under conditions, and the stores of the kinds.
4
+
5
+ - **Kinds.** A kind has one parent, a mode, attributes as `zod` schemas, and facets. A facet is a set of attributes that kinds share with no common parent.
6
+ - **Relation kinds.** A relation kind tells the kinds at its two ends. It can have qualifiers, roles and a key. A relation can narrow a wider relation. Each relation instance has an id from its kind and its key.
7
+ - **Claims under conditions.** A claim has a statement, a verdict, evidence and an amount. Its conditions tell where it holds: versions of a system, an architecture, a provider or a population.
8
+ - **Codegen.** The kinds give the SQLite tables, the DuckDB tables with one view for each kind, and the JSON Schema of each kind.
9
+ - **Drift test.** `staleGenerated` finds each generated file that is not the same as a new generation.
10
+
11
+ The core names no topic. Each topic is an add-on: a set of kinds, relation kinds and facets on the spine.
12
+
13
+ ## Install
14
+
15
+ ```sh
16
+ pnpm add @mark1russell7/ontology zod @mark1russell7/quantity
17
+ ```
18
+
19
+ `zod` and `@mark1russell7/quantity` are peer dependencies. The schemas of your add-on and the registry must use the same copy of each.
20
+
21
+ ## The spine
22
+
23
+ The spine is the general part of each registry:
24
+
25
+ | Part | Names |
26
+ | --- | --- |
27
+ | Kinds | `entity` (the root), `concept`, `claim`, `facet`, `kind`, `relation` |
28
+ | Facets | `evidenced` (a grade, an agreement score, sources), `quantified` (an amount and its statistic) |
29
+ | Relation kinds | `has-facet`, `subkind-of`, `about`, `classified-as`, `instance-of`, `subclass-of` |
30
+
31
+ `instance-of` names no kind of particular. An add-on adds its kinds to it with an extension.
32
+
33
+ The attribute `name` of `entity` is optional. `nameOf(entity)` gives the name, or the id when the entity has no name.
34
+
35
+ ## Make a registry with an add-on
36
+
37
+ ```ts
38
+ import type { RegistryInput } from "@mark1russell7/ontology";
39
+ import { spineRegistry } from "@mark1russell7/ontology";
40
+ import { z } from "zod";
41
+
42
+ const library: RegistryInput = {
43
+ kinds: [
44
+ { name: "book", parent: "entity", mode: "continuant", description: "A book.", attributes: { pages: z.number().int() } },
45
+ ],
46
+ relations: [{ name: "cites", description: "A book cites a claim.", from: ["book"], to: ["claim"] }],
47
+ // The add-on adds its kind to the ends of relation kinds of the spine
48
+ extensions: [
49
+ { relation: "classified-as", from: ["book"] },
50
+ { relation: "instance-of", from: ["book"] },
51
+ { relation: "about", from: ["book"] },
52
+ ],
53
+ };
54
+
55
+ const registry = spineRegistry(library);
56
+
57
+ registry.validateEntity({ id: "lib.book.dune", kind: "book", attrs: { name: "Dune", pages: 412 } });
58
+ ```
59
+
60
+ `spineRegistry(...addOns)` joins the spine and the add-ons with `composeRegistryInput`. The registry refuses a kind, a relation kind or a facet that two parts declare. An extension can only add kinds to the ends of a relation kind. A narrower relation kind must stay inside the ends of its parent, so an add-on extends the parent too.
61
+
62
+ ## Rules across attributes
63
+
64
+ A kind can have requirements: when each attribute of `when` has its value, each attribute of `then` has a value. A subkind gets the requirements of its parent.
65
+
66
+ ```ts
67
+ const task = {
68
+ name: "task",
69
+ parent: "entity",
70
+ mode: "occurrent",
71
+ description: "A task.",
72
+ attributes: { state: z.enum(["planned", "done"]), due: z.string().optional() },
73
+ requirements: [{ description: "a planned task has a due date", when: { state: "planned" }, then: ["due"] }],
74
+ };
75
+ ```
76
+
77
+ The registry refuses a requirement that names an attribute that the kind does not have. The JSON Schema of the kind shows each requirement as `if` and `then` in `allOf`.
78
+
79
+ ## Check entities and edges
80
+
81
+ - `validateEntity(raw)` checks the attributes of the kind, its ancestors and their facets. It gives the default scope of the kind to an entity with no scope.
82
+ - `validateEdge(raw, kindOf)` checks the kinds of the ends and of the role fillers, and the qualifiers. It gives the edge with its id.
83
+ - `relationIdOf(edge)` gives the id of a relation from the key of its kind. A symmetric kind gives one id for the two orders.
84
+ - `toGraph()` gives the registry as data: an entity for each kind and facet, and the edges between them.
85
+
86
+ ## Claims under conditions
87
+
88
+ ```ts
89
+ import { rangeMeets } from "@mark1russell7/ontology";
90
+
91
+ rangeMeets(">=2.6.12", "6.6"); // "all": the range allows each 6.6 release
92
+ rangeMeets("=6.6.87.2", "6.6"); // "some": the range allows one 6.6 release
93
+ rangeMeets(">=6.15", "6.6"); // "none"
94
+ ```
95
+
96
+ A condition is `{ of, versions }`, `{ on }`, `{ architecture }` or `{ population }`. `conditionSchema` refuses a range with no version in it.
97
+
98
+ ## Generate the stores
99
+
100
+ ```ts
101
+ import { generateAll } from "@mark1russell7/ontology";
102
+ import { staleGenerated, writeGenerated } from "@mark1russell7/ontology/node";
103
+
104
+ const files = generateAll(registry, { header: "-- Generated by pnpm generate. Do not edit by hand." });
105
+ await writeGenerated(registry, "generated");
106
+ const stale = await staleGenerated(registry, "generated"); // [] when the files are current
107
+ ```
108
+
109
+ | File | Contents |
110
+ | --- | --- |
111
+ | `duckdb.sql` | The tables `entities` and `edges`, and a view `v_<kind>` with a column for each attribute. A quantity gives a number column and a unit column. The envelope columns are `envelope_scope`, `envelope_valid_from`, `envelope_valid_to` and `envelope_observed_at`. |
112
+ | `sqlite.sql` | The tables `entities` and `edges`, with JSON attributes. |
113
+ | `registry.json` | Each kind and each relation kind, with the JSON Schema of its attributes and qualifiers. |
114
+ | `jsonschema.json` | The JSON Schemas of an entity, an edge and the full attributes of each kind. |
115
+
116
+ Put `staleGenerated` in a test, so that CI fails when a kind changes and the generated files do not.
117
+
118
+ ## License
119
+
120
+ MIT