@wazootech/reasoner 0.0.0-stage → 0.4.4
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 +21 -0
- package/README.md +225 -2
- package/dist/index.d.ts +432 -0
- package/dist/index.js +2915 -0
- package/package.json +67 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wazoo Technologies
|
|
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,226 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @wazootech/reasoner
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An OWL 2 RL reasoner over RDF/JS quad stores, built alongside
|
|
4
|
+
[`@wazootech/sparql-engine`](https://github.com/wazootech/sparql-engine).
|
|
5
|
+
|
|
6
|
+
**Status.** The OWL 2 RL ruleset, the semi-naive engine, `owl:sameAs` as a
|
|
7
|
+
union-find, incremental additions (`extend`) and retraction (`retract`) work,
|
|
8
|
+
and `bun run test:differential` finds no difference between `extend` or
|
|
9
|
+
`retract` and a fresh run on 5,000 random graphs. Worlds uses it for opt-in
|
|
10
|
+
inference on the Cloudflare backend: derived quads are stored in their own
|
|
11
|
+
column and skipped by search reindexing
|
|
12
|
+
([wazootech/worlds-cloudflare#50](https://github.com/wazootech/worlds-cloudflare/pull/50)
|
|
13
|
+
to [#53](https://github.com/wazootech/worlds-cloudflare/pull/53),
|
|
14
|
+
[#56](https://github.com/wazootech/worlds-cloudflare/pull/56)). The Wiki
|
|
15
|
+
switched from `rdfjs-inference-engine` to this package in
|
|
16
|
+
[wazootech/wiki#418](https://github.com/wazootech/wiki/pull/418). The plan is
|
|
17
|
+
[wazootech/reasoner#1](https://github.com/wazootech/reasoner/issues/1); the
|
|
18
|
+
evidence for it is in
|
|
19
|
+
[wazootech/wiki#414](https://github.com/wazootech/wiki/issues/414).
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install @wazootech/reasoner # Node.js 22 or newer
|
|
25
|
+
bun add @wazootech/reasoner # Bun
|
|
26
|
+
deno add npm:@wazootech/reasoner # Deno
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Deno waits a day before it installs a new release. To get one sooner, add
|
|
30
|
+
`--minimum-dependency-age=0` to the command.
|
|
31
|
+
|
|
32
|
+
Versions up to 0.4.3 were published on JSR as `@wazoo/reasoner`. Only the name
|
|
33
|
+
and the registry changed: every export is the same. Don't use the two names in
|
|
34
|
+
one app, because they load two copies of `sparql-engine`.
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { Reasoner } from "@wazootech/reasoner";
|
|
40
|
+
|
|
41
|
+
const reasoner = new Reasoner();
|
|
42
|
+
reasoner.add(quads); // RDF/JS quads; the graph term names the graph (see below)
|
|
43
|
+
reasoner.run();
|
|
44
|
+
for (const quad of reasoner.closure("application")) store.add(quad);
|
|
45
|
+
reasoner.inconsistencies(); // [{ rule: "cax-dw", terms: [...] }, ...]
|
|
46
|
+
|
|
47
|
+
// Later: reason about a change against the closure already computed.
|
|
48
|
+
const added = reasoner.extend(moreQuads, "application");
|
|
49
|
+
|
|
50
|
+
// Or take asserted triples back; the result is what is no longer entailed.
|
|
51
|
+
const removed = reasoner.retract(goneQuads, "application");
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`closure("complete")` lists every entailed triple. `"application"` leaves out
|
|
55
|
+
what a graph store has no use for: literal subjects, reflexive `owl:sameAs`,
|
|
56
|
+
complement-class skolems, and types whose class is a blank node.
|
|
57
|
+
|
|
58
|
+
## Named graphs
|
|
59
|
+
|
|
60
|
+
`new Reasoner()` reasons per graph by default;
|
|
61
|
+
`new Reasoner({ graphs: "union" })` merges every graph into one closure listed
|
|
62
|
+
in the default graph, as it did when the graph term was ignored. A caller that
|
|
63
|
+
uses only the default graph sees no difference.
|
|
64
|
+
|
|
65
|
+
In per-graph mode entailment still sees every graph, so a rule can join a
|
|
66
|
+
subclass axiom in one graph with a typed instance in another. The graphs decide
|
|
67
|
+
where a triple is _listed_: each entailed triple is in every graph that asserts
|
|
68
|
+
it or whose own closure entails it, in the default graph when it is an axiomatic
|
|
69
|
+
triple, and in `DERIVED_GRAPH` (`https://wazoo.dev/ns/reasoner#derived`) when
|
|
70
|
+
only a combination of graphs entails it. `has(s, p, o, graph?)` takes the graph
|
|
71
|
+
too, `inconsistencies()` names the graph (or the derived graph) whose closure
|
|
72
|
+
violates each rule, and `extend` and `retract` return quads with their graphs. A
|
|
73
|
+
triple asserted in two graphs stays asserted until it is retracted from both.
|
|
74
|
+
|
|
75
|
+
With more than one graph, per-graph mode also computes the closure of each graph
|
|
76
|
+
alone (lazily, once per change) and `extend`/`retract` diff the listings, so
|
|
77
|
+
they cost a pass over the closure instead of just the change. Use one graph, or
|
|
78
|
+
`"union"`, where that matters.
|
|
79
|
+
|
|
80
|
+
## Custom rules
|
|
81
|
+
|
|
82
|
+
Register Datalog-style rules over triple patterns, in the constructor or with
|
|
83
|
+
`addRule`. A pattern is `[subject, predicate, object]`; each position is an
|
|
84
|
+
RDF/JS term or a variable from `variables()`.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { Reasoner, variables } from "@wazootech/reasoner";
|
|
88
|
+
|
|
89
|
+
const { x, y, z } = variables();
|
|
90
|
+
const reasoner = new Reasoner({
|
|
91
|
+
rules: [
|
|
92
|
+
{
|
|
93
|
+
name: "grandparent",
|
|
94
|
+
body: [
|
|
95
|
+
[x, parent, y],
|
|
96
|
+
[y, parent, z],
|
|
97
|
+
],
|
|
98
|
+
head: [[x, grandparent, z]],
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: "no-self-parent", // reported as Inconsistency.rule
|
|
102
|
+
body: [[x, parent, x]],
|
|
103
|
+
inconsistent: [x], // up to five terms
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
reasoner.addRule(anotherRule); // also fine after run(); recomputes once
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
- Custom rules run in the same semi-naive fixpoint as the OWL 2 RL rules, so
|
|
111
|
+
they see derived triples and `owl:sameAs` classes, and their heads feed the
|
|
112
|
+
OWL rules. A head may derive `owl:sameAs`.
|
|
113
|
+
- Derived triples are ordinary triples of the closure. `closure("complete")`
|
|
114
|
+
lists them; `closure("application")` lists them under the same filters as any
|
|
115
|
+
other triple (literal subjects, reflexive `owl:sameAs`, skolems and blank-node
|
|
116
|
+
classes are omitted).
|
|
117
|
+
- `extend` stays incremental. `retract` does not: with any custom rule
|
|
118
|
+
registered it recomputes the whole closure (the result still equals a fresh
|
|
119
|
+
run), because a custom rule can relate any triples.
|
|
120
|
+
- An invalid rule throws an `Error`: unbound head variable, empty body, no head
|
|
121
|
+
and no `inconsistent`, a literal or blank-node head predicate, a duplicate
|
|
122
|
+
name, or a name that is an OWL 2 RL rule name.
|
|
123
|
+
|
|
124
|
+
## Testing
|
|
125
|
+
|
|
126
|
+
- `bun run ci`: format, lint, type-check, unit tests.
|
|
127
|
+
- `bun run build && bun run check:package`: builds the npm package, installs it
|
|
128
|
+
in an empty project, and uses it on Node.js, Bun and Deno, which must all be on
|
|
129
|
+
your PATH.
|
|
130
|
+
- `bun run test:parity`: the closure against the Wiki's current engine
|
|
131
|
+
(`rdfjs-inference-engine`), triple for triple, on the corpora in
|
|
132
|
+
`test/parity/corpora/`. Every difference must be a documented divergence.
|
|
133
|
+
- `bun run test:w3c`: the W3C OWL 2 RL test cases (approved and proposed),
|
|
134
|
+
fetched from w3.org into `.cache/`. A test in `test/w3c/expected-pass.json`
|
|
135
|
+
that fails is a regression; `bun run test:w3c:update` records new passes.
|
|
136
|
+
- `bun run test:differential [--seeds N]`: `Reasoner.extend` and `retract`
|
|
137
|
+
against a fresh run on random graphs (modes extend, retract, retract-instance,
|
|
138
|
+
retract-full, extend-custom, retract-custom, and, with triples spread over
|
|
139
|
+
four named graphs, union-extend, union-retract, graphs-extend, graphs-retract,
|
|
140
|
+
graphs-retract-partial; the graph modes run on every third seed unless
|
|
141
|
+
`--graph-stride 1`); exits 1 on a new failure class. CI runs 1000 seeds (about
|
|
142
|
+
7 s); a nightly workflow runs 5000.
|
|
143
|
+
- `bun run bench`: closure time and memory on synthetic graphs
|
|
144
|
+
(`--sizes 1e3,1e4,1e5 --profile plain|equality`).
|
|
145
|
+
|
|
146
|
+
## Conformance
|
|
147
|
+
|
|
148
|
+
- **W3C OWL 2 RL suite** (`bun run test:w3c`): 88 of 90 pass. The harness
|
|
149
|
+
reads tests given in OWL functional syntax (a subset, `functional-syntax.ts`),
|
|
150
|
+
follows `owl:imports`, and ignores annotations in the conclusion of
|
|
151
|
+
Direct-Semantics tests. Two conclusions are not entailed, each with its reason
|
|
152
|
+
in `test/w3c/known-gaps.json`: `WebOnt-I5.26-010` and `WebOnt-I5.5-005` (bare
|
|
153
|
+
anonymous class expressions, which the rules never create).
|
|
154
|
+
`New-Feature-ObjectPropertyChain-BJP-002` passes through a rule outside the RL
|
|
155
|
+
table, `prp-trp-chain`: a transitive property gets a skolem `(p p)` chain
|
|
156
|
+
axiom, which the `"application"` view hides.
|
|
157
|
+
- **Datatype value equality (dt-eq), numeric and boolean only.** Literals that
|
|
158
|
+
are the same value are put in one `owl:sameAs` class, so functional-property,
|
|
159
|
+
inverse-functional and `owl:hasKey` merges, `owl:differentFrom` and
|
|
160
|
+
`owl:AllDifferent` see them as one value. The spaces follow OWL 2's datatype
|
|
161
|
+
map: `xsd:decimal` and the whole integer family are one space
|
|
162
|
+
(`"1"^^xsd:integer`, `"01"^^xsd:int` and `"1.0"^^xsd:decimal` agree, and
|
|
163
|
+
`"1.0"^^xsd:decimal` is a value of `xsd:integer`), `xsd:float` and
|
|
164
|
+
`xsd:double` are one space each (never equal to each other or to a decimal;
|
|
165
|
+
`+0` and `-0` stay distinct; `NaN` equals nothing), and `xsd:boolean` values
|
|
166
|
+
`"true"`/`"1"` and `"false"`/`"0"` agree. Literals are grouped by value, so
|
|
167
|
+
the cost is linear in the graph's literals. Behavior changes: a subject now
|
|
168
|
+
also holds every equal spelling a literal of the graph has
|
|
169
|
+
(`ex:a ex:p "1"^^xsd:integer` with `"1.0"^^xsd:decimal` used anywhere gives
|
|
170
|
+
`ex:a ex:p "1.0"^^xsd:decimal`, the eq-rep-o copy, in both the `"complete"`
|
|
171
|
+
and `"application"` views), and `isInstanceOf` for the decimal family tests
|
|
172
|
+
the value space rather than the spelling. Unchanged deviations: strings,
|
|
173
|
+
dateTime, binary, anyURI and other datatypes still have one term per spelling,
|
|
174
|
+
no `owl:differentFrom` is materialized between distinct literals, and `dt-eq`
|
|
175
|
+
is never applied to unrelated literals.
|
|
176
|
+
- **Parity with `rdfjs-inference-engine`** (`bun run test:parity`): identical
|
|
177
|
+
closures on `bench-1e3`, `docs`, `micro`, and `owl-nosame`. On `owl` this
|
|
178
|
+
package derives two extra triples, a documented divergence (`eq-rep` applied
|
|
179
|
+
to an ontology triple).
|
|
180
|
+
|
|
181
|
+
## Why
|
|
182
|
+
|
|
183
|
+
The Wiki CLI and Worlds need OWL 2 RL materialization that scales with the
|
|
184
|
+
graph. The engine the Wiki used before, `rdfjs-inference-engine` over Eyeling,
|
|
185
|
+
grew quadratically even on graphs that derive nothing: Eyeling evaluates
|
|
186
|
+
`owl:sameAs` as a builtin that scans every fact, and the equality rules call it
|
|
187
|
+
once per triple. It also re-reasoned over all of its input on every call, while
|
|
188
|
+
Worlds needs to reason about a change (Δ) against an existing closure.
|
|
189
|
+
|
|
190
|
+
## Goals
|
|
191
|
+
|
|
192
|
+
- **Linear in the data where OWL 2 RL allows it.** The rules are compiled to
|
|
193
|
+
indexed joins, not interpreted as N3 text. Evaluation is semi-naive, and
|
|
194
|
+
`owl:sameAs` is handled with a union-find of representatives instead of
|
|
195
|
+
copying triples.
|
|
196
|
+
- **Incremental.** Given a closure and a Δ, derive only what the Δ adds (and
|
|
197
|
+
retract what a deletion removes). `retract` re-derives only the connected
|
|
198
|
+
component of deleted triples about individuals (an index of components is kept
|
|
199
|
+
as triples are asserted). Deleting a schema triple, such as a subclass or a
|
|
200
|
+
restriction, or a component past `maxScope` recomputes the whole closure. This
|
|
201
|
+
shares its "what does this change touch" question with the scoped SHACL
|
|
202
|
+
validation planned for Worlds
|
|
203
|
+
([worlds-sdk-ts#151](https://github.com/wazootech/worlds-sdk-ts/issues/151)).
|
|
204
|
+
- **Gated like `sparql-engine`.** Done means the W3C OWL 2 RL conformance tests
|
|
205
|
+
pass in CI, and the closure matches the Wiki's current engine triple for
|
|
206
|
+
triple on its parity corpora before the Wiki switches over.
|
|
207
|
+
|
|
208
|
+
## Relationship to `sparql-engine`
|
|
209
|
+
|
|
210
|
+
A separate package that depends on `@wazootech/sparql-engine`'s term model.
|
|
211
|
+
Querying and reasoning have different specifications, test suites, and release
|
|
212
|
+
cadences, so neither forces a release of the other.
|
|
213
|
+
|
|
214
|
+
## Releases
|
|
215
|
+
|
|
216
|
+
Every merge to `main` runs the Publish workflow. It checks the packed file on
|
|
217
|
+
Node.js, Bun and Deno, and publishes that same file to npm **only when
|
|
218
|
+
`package.json`'s `version` is not on npm yet**. To release, bump `version` (a
|
|
219
|
+
patch, unless the change breaks callers) in the pull request that should publish.
|
|
220
|
+
A version with a suffix, such as `0.5.0-rc.0`, goes to the `next` tag instead of
|
|
221
|
+
`latest`.
|
|
222
|
+
|
|
223
|
+
npm accepts the publish because this repository's `publish.yml` is the package's
|
|
224
|
+
trusted publisher, so there is no npm token to store or rotate. After publishing,
|
|
225
|
+
a second job installs the new version from npm and checks it. If only that job
|
|
226
|
+
fails, re-run it on its own.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* An OWL 2 RL reasoner over RDF/JS terms.
|
|
4
|
+
*
|
|
5
|
+
* {@link Reasoner} materializes the OWL 2 RL/RDF closure of a set of triples
|
|
6
|
+
* semi-naively, through hash-indexed joins, with `owl:sameAs` kept as a
|
|
7
|
+
* union-find rather than as copied triples. {@link Reasoner.extend} reasons
|
|
8
|
+
* about added triples against a closure that is already computed.
|
|
9
|
+
*/
|
|
10
|
+
import { NamedNode, Quad, Term } from "@rdfjs/types";
|
|
11
|
+
/** Variable is a named rule variable. */
|
|
12
|
+
export declare class Variable {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
constructor(name: string);
|
|
15
|
+
}
|
|
16
|
+
/** RuleArgument is a constant term or a rule variable. */
|
|
17
|
+
type RuleArgument = Term | Variable;
|
|
18
|
+
/** AtomPattern matches or produces facts of one relation. */
|
|
19
|
+
interface AtomPattern {
|
|
20
|
+
readonly kind: "atom";
|
|
21
|
+
readonly relation: string;
|
|
22
|
+
readonly args: readonly RuleArgument[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* BuiltinContext is what a builtin may read: the terms behind ids, and the
|
|
26
|
+
* current equality classes.
|
|
27
|
+
*/
|
|
28
|
+
interface BuiltinContext {
|
|
29
|
+
term(id: number): Term;
|
|
30
|
+
id(term: Term): number;
|
|
31
|
+
find(id: number): number;
|
|
32
|
+
members(root: number): readonly number[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* BuiltinPattern computes bindings instead of matching stored facts. It runs
|
|
36
|
+
* once all of its `inputs` are bound and yields one tuple of `outputs` values
|
|
37
|
+
* per solution; a filter has no outputs and yields one empty tuple to pass.
|
|
38
|
+
*/
|
|
39
|
+
interface BuiltinPattern {
|
|
40
|
+
readonly kind: "builtin";
|
|
41
|
+
readonly name: string;
|
|
42
|
+
readonly inputs: readonly Variable[];
|
|
43
|
+
readonly outputs: readonly Variable[];
|
|
44
|
+
readonly run: (inputs: readonly number[], context: BuiltinContext) => Iterable<readonly number[]>;
|
|
45
|
+
}
|
|
46
|
+
/** BodyPattern is one premise of a rule. */
|
|
47
|
+
type BodyPattern = AtomPattern | BuiltinPattern;
|
|
48
|
+
/** Rule derives every head atom for each solution of its body. */
|
|
49
|
+
interface Rule {
|
|
50
|
+
readonly name: string;
|
|
51
|
+
readonly body: readonly BodyPattern[];
|
|
52
|
+
readonly head: readonly AtomPattern[];
|
|
53
|
+
}
|
|
54
|
+
/** atom builds an {@link AtomPattern}. */
|
|
55
|
+
export declare function atom(relation: string, ...args: readonly RuleArgument[]): AtomPattern;
|
|
56
|
+
/** filter builds a builtin that keeps a solution when `test` holds. */
|
|
57
|
+
export declare function filter(name: string, inputs: readonly Variable[], test: (inputs: readonly number[], context: BuiltinContext) => boolean): BuiltinPattern;
|
|
58
|
+
/** bind builds a builtin that binds `outputs` from `inputs`. */
|
|
59
|
+
export declare function bind(name: string, inputs: readonly Variable[], outputs: readonly Variable[], run: BuiltinPattern["run"]): BuiltinPattern;
|
|
60
|
+
/**
|
|
61
|
+
* variables returns a proxy whose every property is the variable of that
|
|
62
|
+
* name, so rules read like their tables: `const { x, p, y } = variables()`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function variables(): Record<string, Variable>;
|
|
65
|
+
/**
|
|
66
|
+
* TriplePattern is one `[subject, predicate, object]` pattern of a custom
|
|
67
|
+
* rule. Each position is an RDF/JS term (a constant) or a {@link Variable}.
|
|
68
|
+
*/
|
|
69
|
+
type TriplePattern = readonly [subject: RuleArgument, predicate: RuleArgument, object: RuleArgument];
|
|
70
|
+
/**
|
|
71
|
+
* CustomRule is a Datalog-style rule over triples: for every way the `body`
|
|
72
|
+
* patterns match the closure together, each `head` pattern is derived with the
|
|
73
|
+
* same variable bindings, and `inconsistent` (if given) reports an
|
|
74
|
+
* inconsistency.
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* const { x, y, z } = variables();
|
|
78
|
+
* const grandparent: CustomRule = {
|
|
79
|
+
* name: "grandparent",
|
|
80
|
+
* body: [[x, parent, y], [y, parent, z]],
|
|
81
|
+
* head: [[x, grandparent, z]],
|
|
82
|
+
* };
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* A rule is valid when its `name` is non-empty and unique, `body` has at least
|
|
86
|
+
* one pattern, it has a `head` pattern or an `inconsistent` list, every
|
|
87
|
+
* variable in `head` or `inconsistent` occurs in `body`, and every constant
|
|
88
|
+
* predicate in `head` is a named node.
|
|
89
|
+
*/
|
|
90
|
+
interface CustomRule {
|
|
91
|
+
/**
|
|
92
|
+
* name identifies the rule. It is reported as `Inconsistency.rule` and must
|
|
93
|
+
* differ from every OWL 2 RL rule name and every other custom rule name.
|
|
94
|
+
*/
|
|
95
|
+
readonly name: string;
|
|
96
|
+
/** body is the premises; all must match for the rule to fire. */
|
|
97
|
+
readonly body: readonly TriplePattern[];
|
|
98
|
+
/** head is the triples derived for each match. May be empty. */
|
|
99
|
+
readonly head?: readonly TriplePattern[];
|
|
100
|
+
/**
|
|
101
|
+
* inconsistent, when present, reports an {@link Inconsistency} named after
|
|
102
|
+
* the rule for each match, with these (at most five) terms as its `terms`.
|
|
103
|
+
*/
|
|
104
|
+
readonly inconsistent?: readonly RuleArgument[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Equality keeps `owl:sameAs` as a union-find over term ids instead of as
|
|
108
|
+
* copied triples, the way RDFox does: every stored fact names only class
|
|
109
|
+
* representatives, and the materialized closure is the product of each
|
|
110
|
+
* fact's term classes.
|
|
111
|
+
*
|
|
112
|
+
* Union is by size and finds do not compress paths, so the tree depth stays
|
|
113
|
+
* logarithmic and every link keeps the clock tick it was made at. That makes
|
|
114
|
+
* the state at any earlier tick recoverable ({@link Equality.findAt}), which
|
|
115
|
+
* is how an incremental run tells a triple it newly entails from one that was
|
|
116
|
+
* already in the closure before a merge.
|
|
117
|
+
*/
|
|
118
|
+
declare class Equality {
|
|
119
|
+
#private;
|
|
120
|
+
/** find returns the representative of the class holding `id`. */
|
|
121
|
+
find(id: number): number;
|
|
122
|
+
/** findAt returns the representative `id` had before clock tick `tick`. */
|
|
123
|
+
findAt(id: number, tick: number): number;
|
|
124
|
+
/**
|
|
125
|
+
* union merges the classes of `a` and `b` at clock tick `tick`, and returns
|
|
126
|
+
* the representative that was absorbed, or `undefined` if they were already
|
|
127
|
+
* one class.
|
|
128
|
+
*/
|
|
129
|
+
union(a: number, b: number, tick: number): number | undefined;
|
|
130
|
+
/** members returns every id in the class whose representative is `root`. */
|
|
131
|
+
members(root: number): readonly number[];
|
|
132
|
+
/** isTrivial is `true` when `root` is alone in its class. */
|
|
133
|
+
isTrivial(root: number): boolean;
|
|
134
|
+
/** rootsChangedSince returns the roots of every class merged at or after `tick`. */
|
|
135
|
+
rootsChangedSince(tick: number): Set<number>;
|
|
136
|
+
/**
|
|
137
|
+
* reset makes every id in `ids` a class of its own again. `ids` must be
|
|
138
|
+
* closed under the classes it touches, or the survivors keep dangling links.
|
|
139
|
+
*/
|
|
140
|
+
reset(ids: Iterable<number>): void;
|
|
141
|
+
/** classes yields the representative of every class with two or more members. */
|
|
142
|
+
classes(): IterableIterator<number>;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Fact is one stored tuple of term ids. A fact is never mutated; when an
|
|
146
|
+
* `owl:sameAs` merge renames one of its terms it is retired (`diedAt`) and
|
|
147
|
+
* the renamed tuple is stored as a new fact.
|
|
148
|
+
*/
|
|
149
|
+
interface Fact {
|
|
150
|
+
readonly args: readonly number[];
|
|
151
|
+
readonly key: string;
|
|
152
|
+
/** bornAt is the clock tick the fact was stored at. */
|
|
153
|
+
readonly bornAt: number;
|
|
154
|
+
/** diedAt is the tick the fact was retired at, or `Infinity` while live. */
|
|
155
|
+
diedAt: number;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Relation stores the facts of one predicate of fixed arity, with a hash index
|
|
159
|
+
* per combination of bound columns. Single-column indexes exist from the
|
|
160
|
+
* start, because a merge looks up every fact that mentions a renamed term;
|
|
161
|
+
* wider indexes are built on first use and maintained from then on.
|
|
162
|
+
*
|
|
163
|
+
* Retired facts stay in {@link Relation.get} (so an incremental run can ask
|
|
164
|
+
* what was live at an earlier tick) and are skipped lazily by lookups.
|
|
165
|
+
*/
|
|
166
|
+
declare class Relation {
|
|
167
|
+
#private;
|
|
168
|
+
readonly name: string;
|
|
169
|
+
readonly arity: number;
|
|
170
|
+
constructor(name: string, arity: number);
|
|
171
|
+
/** size is the number of live facts. */
|
|
172
|
+
get size(): number;
|
|
173
|
+
/** get returns the fact stored under `key`, live or retired. */
|
|
174
|
+
get(key: string): Fact | undefined;
|
|
175
|
+
/** has is `true` when a live fact has exactly these args. */
|
|
176
|
+
has(args: readonly number[]): boolean;
|
|
177
|
+
/**
|
|
178
|
+
* insert stores `args` at tick `tick` and returns the new fact, or
|
|
179
|
+
* `undefined` when a live fact already has these args.
|
|
180
|
+
*/
|
|
181
|
+
insert(args: readonly number[], tick: number): Fact | undefined;
|
|
182
|
+
/** retire marks a live fact dead at tick `tick`. */
|
|
183
|
+
retire(fact: Fact, tick: number): void;
|
|
184
|
+
/**
|
|
185
|
+
* lookup returns the facts whose columns in `mask` equal `values` (one value
|
|
186
|
+
* per set bit, lowest column first). Retired facts may be included; callers
|
|
187
|
+
* skip any fact whose `diedAt` is finite.
|
|
188
|
+
*/
|
|
189
|
+
lookup(mask: number, values: readonly number[]): readonly Fact[];
|
|
190
|
+
/** column returns the facts whose column `column` equals `id`. */
|
|
191
|
+
column(column: number, id: number): readonly Fact[];
|
|
192
|
+
/**
|
|
193
|
+
* forget drops the single-column buckets of `id` once a merge has renamed
|
|
194
|
+
* every fact in them, so the dead facts do not pin memory.
|
|
195
|
+
*/
|
|
196
|
+
forget(id: number): void;
|
|
197
|
+
/** facts yields every live fact. */
|
|
198
|
+
facts(): IterableIterator<Fact>;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* TermDictionary interns RDF/JS terms as small integers, so facts are arrays
|
|
202
|
+
* of numbers and every index key is a short string of digits. Ids are dense
|
|
203
|
+
* and never reused; an id stays valid for the dictionary's lifetime.
|
|
204
|
+
*/
|
|
205
|
+
declare class TermDictionary {
|
|
206
|
+
#private;
|
|
207
|
+
/** id returns the id of `term`, interning it first if it is new. */
|
|
208
|
+
id(term: Term): number;
|
|
209
|
+
/** lookup returns the id of `term`, or `undefined` if it was never interned. */
|
|
210
|
+
lookup(term: Term): number | undefined;
|
|
211
|
+
/** term returns the term interned as `id`. */
|
|
212
|
+
term(id: number): Term;
|
|
213
|
+
/** size is the number of interned terms. */
|
|
214
|
+
get size(): number;
|
|
215
|
+
}
|
|
216
|
+
/** EngineOptions configure the relation that carries `owl:sameAs`. */
|
|
217
|
+
interface EngineOptions {
|
|
218
|
+
/** equalityRelation is the arity-3 relation whose sameAs facts merge terms. */
|
|
219
|
+
readonly equalityRelation: string;
|
|
220
|
+
/** sameAs is the predicate that states two terms are equal. */
|
|
221
|
+
readonly sameAs: Term;
|
|
222
|
+
/** relations declares each relation the rules use, with its arity. */
|
|
223
|
+
readonly relations: Readonly<Record<string, number>>;
|
|
224
|
+
/**
|
|
225
|
+
* contentTerm marks terms (literals, say) whose joining an equality class
|
|
226
|
+
* changes what rules read from it. When a class absorbs one, the facts of
|
|
227
|
+
* the surviving representative are re-seeded, as renamed facts are.
|
|
228
|
+
*/
|
|
229
|
+
readonly contentTerm?: (term: Term) => boolean;
|
|
230
|
+
}
|
|
231
|
+
/** EngineStatistics summarize the work of the runs so far. */
|
|
232
|
+
interface EngineStatistics {
|
|
233
|
+
rounds: number;
|
|
234
|
+
derivations: number;
|
|
235
|
+
merges: number;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Engine evaluates a set of rules to a fixpoint, semi-naively: each round
|
|
239
|
+
* joins only the facts the previous round added against everything stored,
|
|
240
|
+
* through hash indexes chosen per join step. Facts whose relation is the
|
|
241
|
+
* equality relation and whose predicate is `sameAs` merge their subject and
|
|
242
|
+
* object instead of being stored as-is (see {@link Equality}).
|
|
243
|
+
*/
|
|
244
|
+
export declare class Engine {
|
|
245
|
+
#private;
|
|
246
|
+
readonly terms: TermDictionary;
|
|
247
|
+
readonly equality: Equality;
|
|
248
|
+
readonly statistics: EngineStatistics;
|
|
249
|
+
constructor(rules: readonly Rule[], options: EngineOptions);
|
|
250
|
+
/** clock is the tick the next stored fact will carry. */
|
|
251
|
+
get clock(): number;
|
|
252
|
+
/**
|
|
253
|
+
* tick advances the clock and returns the new tick, so every fact stored
|
|
254
|
+
* from now on is younger than every fact stored before.
|
|
255
|
+
*/
|
|
256
|
+
tick(): number;
|
|
257
|
+
/** relation returns the declared relation called `name`. */
|
|
258
|
+
relation(name: string): Relation;
|
|
259
|
+
/** relations yields every declared relation. */
|
|
260
|
+
relations(): IterableIterator<Relation>;
|
|
261
|
+
/**
|
|
262
|
+
* add stores a fact of `relation` for the next {@link Engine.run} and
|
|
263
|
+
* returns `true` if it was new.
|
|
264
|
+
*/
|
|
265
|
+
add(relation: string, args: readonly number[]): boolean;
|
|
266
|
+
/** record starts collecting every fact stored from now on. */
|
|
267
|
+
record(): void;
|
|
268
|
+
/** stopRecording returns the facts stored since {@link Engine.record}. */
|
|
269
|
+
stopRecording(): Fact[];
|
|
270
|
+
/**
|
|
271
|
+
* mentioning returns every live fact, per relation, that has one of `ids`
|
|
272
|
+
* in a column. Facts name class representatives, so `ids` should hold them.
|
|
273
|
+
*/
|
|
274
|
+
mentioning(ids: ReadonlySet<number>): Map<Relation, Set<Fact>>;
|
|
275
|
+
/**
|
|
276
|
+
* purge retires `facts` (from {@link Engine.mentioning}) and splits the
|
|
277
|
+
* equality classes of `ids` back into single terms, leaving the engine as
|
|
278
|
+
* if those terms had never been mentioned. Assert their facts again and
|
|
279
|
+
* {@link Engine.run} to rebuild what remains true of them.
|
|
280
|
+
*/
|
|
281
|
+
purge(ids: ReadonlySet<number>, facts: Map<Relation, Set<Fact>>): void;
|
|
282
|
+
/** run evaluates the rules until no round derives a new fact. */
|
|
283
|
+
run(): void;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Triple is the part of a quad the reasoner reads. The optional `graph` names
|
|
287
|
+
* the graph the triple is asserted in (the default graph when absent); see
|
|
288
|
+
* {@link GraphMode}.
|
|
289
|
+
*/
|
|
290
|
+
type Triple = Pick<Quad, "subject" | "predicate" | "object"> & {
|
|
291
|
+
readonly graph?: Term;
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* ClosureView chooses which entailed triples a closure lists.
|
|
295
|
+
*
|
|
296
|
+
* - `"complete"` lists every entailed triple.
|
|
297
|
+
* - `"application"` leaves out the triples a graph store has no use for, as
|
|
298
|
+
* the Wiki's previous engine did: triples with a literal subject, reflexive
|
|
299
|
+
* `owl:sameAs`, triples naming a complement-class skolem, and `rdf:type`
|
|
300
|
+
* triples whose class is a blank node (an anonymous restriction). The
|
|
301
|
+
* reasoning behind them still happens; only the listing omits them.
|
|
302
|
+
*/
|
|
303
|
+
type ClosureView = "complete" | "application";
|
|
304
|
+
/** RetractOptions tune {@link Core.retract}. */
|
|
305
|
+
interface RetractOptions {
|
|
306
|
+
/**
|
|
307
|
+
* maxScope is how many asserted triples the connected component of a
|
|
308
|
+
* retraction may hold before `retract` recomputes the whole closure instead
|
|
309
|
+
* of just that component. Defaults to 50,000.
|
|
310
|
+
*/
|
|
311
|
+
readonly maxScope?: number;
|
|
312
|
+
}
|
|
313
|
+
/** Inconsistency is one violated OWL 2 RL `false` rule. */
|
|
314
|
+
interface Inconsistency {
|
|
315
|
+
/** rule is the OWL 2 RL rule name, such as `cax-dw`. */
|
|
316
|
+
readonly rule: string;
|
|
317
|
+
/** terms are the terms the rule matched, in the rule's order. */
|
|
318
|
+
readonly terms: readonly Term[];
|
|
319
|
+
/**
|
|
320
|
+
* graph is the graph whose own closure violates the rule, or
|
|
321
|
+
* {@link DERIVED_GRAPH} when only a combination of graphs does. It is the
|
|
322
|
+
* default graph in `"union"` mode and when only the default graph is used.
|
|
323
|
+
*/
|
|
324
|
+
readonly graph: Term;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* DERIVED_GRAPH is the graph a triple lands in when it is entailed only by a
|
|
328
|
+
* combination of graphs: no single graph entails it on its own.
|
|
329
|
+
*/
|
|
330
|
+
export declare const DERIVED_GRAPH: NamedNode;
|
|
331
|
+
/**
|
|
332
|
+
* GraphMode chooses how a {@link Reasoner} treats the graph term of a quad.
|
|
333
|
+
*
|
|
334
|
+
* - `"per-graph"` (the default) keeps graphs apart. The closure lists each
|
|
335
|
+
* entailed triple in the graphs whose own closure entails it. A triple that
|
|
336
|
+
* is entailed only by premises from several graphs goes in
|
|
337
|
+
* {@link DERIVED_GRAPH}. Triples entailed by no premise at all (the axiomatic
|
|
338
|
+
* triples) are in the default graph. Entailment itself still sees every
|
|
339
|
+
* graph, so cross-graph consequences are found and nothing is lost; the
|
|
340
|
+
* graphs only decide where each triple is listed.
|
|
341
|
+
* - `"union"` merges every graph into one closure, as if the graph term were
|
|
342
|
+
* ignored, and lists it all in the default graph.
|
|
343
|
+
*
|
|
344
|
+
* Callers that use only the default graph get the same closure in both modes.
|
|
345
|
+
*/
|
|
346
|
+
type GraphMode = "per-graph" | "union";
|
|
347
|
+
/** ReasonerOptions configure a {@link Reasoner}. */
|
|
348
|
+
interface ReasonerOptions {
|
|
349
|
+
/** graphs is the {@link GraphMode}. Defaults to `"per-graph"`. */
|
|
350
|
+
readonly graphs?: GraphMode;
|
|
351
|
+
/** rules are custom rules to evaluate alongside the OWL 2 RL rules. */
|
|
352
|
+
readonly rules?: readonly CustomRule[];
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Reasoner materializes the OWL 2 RL closure of the quads it is given, with
|
|
356
|
+
* named graphs (see {@link GraphMode}).
|
|
357
|
+
*
|
|
358
|
+
* ```ts
|
|
359
|
+
* const reasoner = new Reasoner();
|
|
360
|
+
* reasoner.add(quads);
|
|
361
|
+
* reasoner.run();
|
|
362
|
+
* for (const quad of reasoner.closure()) store.add(quad);
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* {@link Reasoner.extend} adds more quads to a closure that is already
|
|
366
|
+
* computed and returns only what they entail that was not entailed before,
|
|
367
|
+
* evaluating only the rules the new facts can reach. {@link Reasoner.retract}
|
|
368
|
+
* removes asserted quads and what only they entailed. A triple asserted in two
|
|
369
|
+
* graphs stays asserted until it is retracted from both.
|
|
370
|
+
*
|
|
371
|
+
* Internally one incremental closure holds the union of all graphs. With more
|
|
372
|
+
* than one graph in `"per-graph"` mode, the closure of each graph alone is also
|
|
373
|
+
* computed, lazily, to decide which graphs list a triple; `extend` and
|
|
374
|
+
* `retract` then diff the listings, which costs a pass over the closure.
|
|
375
|
+
*/
|
|
376
|
+
export declare class Reasoner {
|
|
377
|
+
#private;
|
|
378
|
+
constructor(options?: ReasonerOptions);
|
|
379
|
+
/**
|
|
380
|
+
* addRule registers a custom rule. The closure is recomputed from the
|
|
381
|
+
* asserted quads on the next read. See {@link CustomRule}.
|
|
382
|
+
*/
|
|
383
|
+
addRule(rule: CustomRule): void;
|
|
384
|
+
/** rules lists the custom rules registered so far. */
|
|
385
|
+
get rules(): readonly CustomRule[];
|
|
386
|
+
/** statistics count rounds, derivations, and merges across every run. */
|
|
387
|
+
get statistics(): Readonly<EngineStatistics>;
|
|
388
|
+
/** size is the number of stored facts, before equality classes expand. */
|
|
389
|
+
get size(): number;
|
|
390
|
+
/** add queues quads for the next {@link Reasoner.run}. */
|
|
391
|
+
add(quads: Iterable<Triple>): void;
|
|
392
|
+
/** run computes the closure of everything added so far. */
|
|
393
|
+
run(): void;
|
|
394
|
+
/**
|
|
395
|
+
* extend adds `quads` to the computed closure and returns the quads of the
|
|
396
|
+
* closure that are new: the added ones when they were not already entailed in
|
|
397
|
+
* their graph, and everything they entail.
|
|
398
|
+
*/
|
|
399
|
+
extend(quads: Iterable<Triple>, view?: ClosureView): Quad[];
|
|
400
|
+
/**
|
|
401
|
+
* retract removes asserted `quads` and returns the quads of the closure that
|
|
402
|
+
* are no longer entailed. A quad that was not asserted in its graph is
|
|
403
|
+
* ignored, even if it is entailed. The result equals a fresh run on
|
|
404
|
+
* everything asserted but `quads`.
|
|
405
|
+
*/
|
|
406
|
+
retract(quads: Iterable<Triple>, view?: ClosureView, options?: RetractOptions): Quad[];
|
|
407
|
+
/**
|
|
408
|
+
* closure yields every quad of the closure, the axiomatic triples included,
|
|
409
|
+
* with each `owl:sameAs` class expanded to its members. See
|
|
410
|
+
* {@link GraphMode} for which graph lists each triple.
|
|
411
|
+
*/
|
|
412
|
+
closure(view?: ClosureView): Generator<Quad>;
|
|
413
|
+
/**
|
|
414
|
+
* has is `true` when the closure entails the triple. With a `graph`, it is
|
|
415
|
+
* `true` only when that graph lists the triple. In `"union"` mode the closure
|
|
416
|
+
* is all in the default graph, so any other `graph` gives `false`.
|
|
417
|
+
*/
|
|
418
|
+
has(subject: Term, predicate: Term, object: Term, graph?: Term): boolean;
|
|
419
|
+
/**
|
|
420
|
+
* inconsistencies lists every OWL 2 RL `false` rule the closure violates.
|
|
421
|
+
* Each names the graph whose own closure violates the rule, or
|
|
422
|
+
* {@link DERIVED_GRAPH} for a rule violated only by combining graphs.
|
|
423
|
+
*/
|
|
424
|
+
inconsistencies(): Inconsistency[];
|
|
425
|
+
}
|
|
426
|
+
/** COMPLEMENT_PREFIX starts the skolem IRI naming the complement of a class. */
|
|
427
|
+
export declare const COMPLEMENT_PREFIX = "https://wazoo.dev/.well-known/genid/reasoner/complement/";
|
|
428
|
+
/** The axiomatic triples: the old ruleset's vocabulary sections 0-2 and 6. */
|
|
429
|
+
export declare const AXIOMS: readonly (readonly [Term, Term, Term])[];
|
|
430
|
+
/** OWL2RL_RULES is the full ruleset, in evaluation order. */
|
|
431
|
+
export declare const OWL2RL_RULES: readonly Rule[];
|
|
432
|
+
export type { AtomPattern, BodyPattern, BuiltinContext, BuiltinPattern, ClosureView, CustomRule, EngineOptions, EngineStatistics, GraphMode, Inconsistency, ReasonerOptions, RetractOptions, Rule, RuleArgument, Triple, TriplePattern };
|