@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/dist/index.js
ADDED
|
@@ -0,0 +1,2915 @@
|
|
|
1
|
+
import { dataFactory, termKey } from "@wazootech/sparql-engine/term";
|
|
2
|
+
// src/owl2rl/vocabulary.ts
|
|
3
|
+
/** RDF_NS is the RDF namespace IRI. */
|
|
4
|
+
const RDF_NS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
|
5
|
+
/** RDFS_NS is the RDF Schema namespace IRI. */
|
|
6
|
+
const RDFS_NS = "http://www.w3.org/2000/01/rdf-schema#";
|
|
7
|
+
/** OWL_NS is the OWL namespace IRI. */
|
|
8
|
+
const OWL_NS = "http://www.w3.org/2002/07/owl#";
|
|
9
|
+
/** XSD_NS is the XML Schema datatypes namespace IRI. */
|
|
10
|
+
const XSD_NS = "http://www.w3.org/2001/XMLSchema#";
|
|
11
|
+
function namespace(base, names) {
|
|
12
|
+
const terms = {};
|
|
13
|
+
for (const name of names) terms[name] = dataFactory.namedNode(base + name);
|
|
14
|
+
return terms;
|
|
15
|
+
}
|
|
16
|
+
/** RDF holds the RDF terms the rules use. */
|
|
17
|
+
const RDF = namespace(RDF_NS, [
|
|
18
|
+
"type",
|
|
19
|
+
"first",
|
|
20
|
+
"rest",
|
|
21
|
+
"nil",
|
|
22
|
+
"langString",
|
|
23
|
+
"PlainLiteral",
|
|
24
|
+
"XMLLiteral"
|
|
25
|
+
]);
|
|
26
|
+
/** RDFS holds the RDF Schema terms the rules use. */
|
|
27
|
+
const RDFS = namespace(RDFS_NS, [
|
|
28
|
+
"subClassOf",
|
|
29
|
+
"subPropertyOf",
|
|
30
|
+
"domain",
|
|
31
|
+
"range",
|
|
32
|
+
"Datatype",
|
|
33
|
+
"Literal",
|
|
34
|
+
"label",
|
|
35
|
+
"comment",
|
|
36
|
+
"seeAlso",
|
|
37
|
+
"isDefinedBy"
|
|
38
|
+
]);
|
|
39
|
+
/** OWL holds the OWL terms the rules use. */
|
|
40
|
+
const OWL = namespace(OWL_NS, [
|
|
41
|
+
"sameAs",
|
|
42
|
+
"differentFrom",
|
|
43
|
+
"AllDifferent",
|
|
44
|
+
"members",
|
|
45
|
+
"distinctMembers",
|
|
46
|
+
"Thing",
|
|
47
|
+
"Nothing",
|
|
48
|
+
"Class",
|
|
49
|
+
"NamedIndividual",
|
|
50
|
+
"ObjectProperty",
|
|
51
|
+
"DatatypeProperty",
|
|
52
|
+
"AnnotationProperty",
|
|
53
|
+
"FunctionalProperty",
|
|
54
|
+
"InverseFunctionalProperty",
|
|
55
|
+
"IrreflexiveProperty",
|
|
56
|
+
"SymmetricProperty",
|
|
57
|
+
"AsymmetricProperty",
|
|
58
|
+
"TransitiveProperty",
|
|
59
|
+
"ReflexiveProperty",
|
|
60
|
+
"equivalentClass",
|
|
61
|
+
"equivalentProperty",
|
|
62
|
+
"propertyDisjointWith",
|
|
63
|
+
"AllDisjointProperties",
|
|
64
|
+
"AllDisjointClasses",
|
|
65
|
+
"inverseOf",
|
|
66
|
+
"hasKey",
|
|
67
|
+
"propertyChainAxiom",
|
|
68
|
+
"sourceIndividual",
|
|
69
|
+
"assertionProperty",
|
|
70
|
+
"targetIndividual",
|
|
71
|
+
"targetValue",
|
|
72
|
+
"intersectionOf",
|
|
73
|
+
"unionOf",
|
|
74
|
+
"complementOf",
|
|
75
|
+
"oneOf",
|
|
76
|
+
"someValuesFrom",
|
|
77
|
+
"allValuesFrom",
|
|
78
|
+
"hasValue",
|
|
79
|
+
"onProperty",
|
|
80
|
+
"onClass",
|
|
81
|
+
"onDataRange",
|
|
82
|
+
"maxCardinality",
|
|
83
|
+
"maxQualifiedCardinality",
|
|
84
|
+
"disjointWith",
|
|
85
|
+
"deprecated",
|
|
86
|
+
"versionInfo",
|
|
87
|
+
"priorVersion",
|
|
88
|
+
"backwardCompatibleWith",
|
|
89
|
+
"incompatibleWith"
|
|
90
|
+
]);
|
|
91
|
+
/** XSD holds the XML Schema datatypes OWL 2 RL supports. */
|
|
92
|
+
const XSD = namespace(XSD_NS, [
|
|
93
|
+
"decimal",
|
|
94
|
+
"integer",
|
|
95
|
+
"nonNegativeInteger",
|
|
96
|
+
"nonPositiveInteger",
|
|
97
|
+
"positiveInteger",
|
|
98
|
+
"negativeInteger",
|
|
99
|
+
"long",
|
|
100
|
+
"int",
|
|
101
|
+
"short",
|
|
102
|
+
"byte",
|
|
103
|
+
"unsignedLong",
|
|
104
|
+
"unsignedInt",
|
|
105
|
+
"unsignedShort",
|
|
106
|
+
"unsignedByte",
|
|
107
|
+
"float",
|
|
108
|
+
"double",
|
|
109
|
+
"string",
|
|
110
|
+
"normalizedString",
|
|
111
|
+
"token",
|
|
112
|
+
"language",
|
|
113
|
+
"Name",
|
|
114
|
+
"NCName",
|
|
115
|
+
"NMTOKEN",
|
|
116
|
+
"boolean",
|
|
117
|
+
"hexBinary",
|
|
118
|
+
"base64Binary",
|
|
119
|
+
"anyURI",
|
|
120
|
+
"dateTime",
|
|
121
|
+
"dateTimeStamp"
|
|
122
|
+
]);
|
|
123
|
+
// src/owl2rl/datatypes.ts
|
|
124
|
+
const RDF_LANGSTRING = RDF_NS + "langString";
|
|
125
|
+
const RDF_PLAIN_LITERAL = RDF_NS + "PlainLiteral";
|
|
126
|
+
const RDF_XML_LITERAL = RDF_NS + "XMLLiteral";
|
|
127
|
+
const RDFS_LITERAL = RDFS_NS + "Literal";
|
|
128
|
+
const XSD_STRING = XSD_NS + "string";
|
|
129
|
+
const XSD_BOOLEAN = XSD_NS + "boolean";
|
|
130
|
+
const XSD_DECIMAL = XSD_NS + "decimal";
|
|
131
|
+
const XSD_INTEGER = XSD_NS + "integer";
|
|
132
|
+
const XSD_FLOAT = XSD_NS + "float";
|
|
133
|
+
const XSD_DOUBLE = XSD_NS + "double";
|
|
134
|
+
const XSD_HEX_BINARY = XSD_NS + "hexBinary";
|
|
135
|
+
const XSD_BASE64_BINARY = XSD_NS + "base64Binary";
|
|
136
|
+
const XSD_ANY_URI = XSD_NS + "anyURI";
|
|
137
|
+
const XSD_DATETIME = XSD_NS + "dateTime";
|
|
138
|
+
const XSD_DATETIME_STAMP = XSD_NS + "dateTimeStamp";
|
|
139
|
+
const STRING_LIKE = /* @__PURE__ */ new Set([
|
|
140
|
+
XSD_STRING,
|
|
141
|
+
XSD_NS + "normalizedString",
|
|
142
|
+
XSD_NS + "token",
|
|
143
|
+
XSD_NS + "language",
|
|
144
|
+
XSD_NS + "Name",
|
|
145
|
+
XSD_NS + "NCName",
|
|
146
|
+
XSD_NS + "NMTOKEN"
|
|
147
|
+
]);
|
|
148
|
+
const INTEGER_BOUNDS = /* @__PURE__ */ new Map([
|
|
149
|
+
[XSD_NS + "nonPositiveInteger", [null, 0n]],
|
|
150
|
+
[XSD_NS + "negativeInteger", [null, -1n]],
|
|
151
|
+
[XSD_NS + "long", [-(2n ** 63n), 2n ** 63n - 1n]],
|
|
152
|
+
[XSD_NS + "int", [-(2n ** 31n), 2n ** 31n - 1n]],
|
|
153
|
+
[XSD_NS + "short", [-(2n ** 15n), 2n ** 15n - 1n]],
|
|
154
|
+
[XSD_NS + "byte", [-(2n ** 7n), 2n ** 7n - 1n]],
|
|
155
|
+
[XSD_NS + "nonNegativeInteger", [0n, null]],
|
|
156
|
+
[XSD_NS + "unsignedLong", [0n, 2n ** 64n - 1n]],
|
|
157
|
+
[XSD_NS + "unsignedInt", [0n, 2n ** 32n - 1n]],
|
|
158
|
+
[XSD_NS + "unsignedShort", [0n, 2n ** 16n - 1n]],
|
|
159
|
+
[XSD_NS + "unsignedByte", [0n, 2n ** 8n - 1n]],
|
|
160
|
+
[XSD_NS + "positiveInteger", [1n, null]]
|
|
161
|
+
]);
|
|
162
|
+
const SUPPORTED = /* @__PURE__ */ new Set([
|
|
163
|
+
RDF_LANGSTRING,
|
|
164
|
+
RDF_PLAIN_LITERAL,
|
|
165
|
+
RDF_XML_LITERAL,
|
|
166
|
+
RDFS_LITERAL,
|
|
167
|
+
...STRING_LIKE,
|
|
168
|
+
XSD_BOOLEAN,
|
|
169
|
+
XSD_DECIMAL,
|
|
170
|
+
XSD_INTEGER,
|
|
171
|
+
XSD_FLOAT,
|
|
172
|
+
XSD_DOUBLE,
|
|
173
|
+
XSD_HEX_BINARY,
|
|
174
|
+
XSD_BASE64_BINARY,
|
|
175
|
+
XSD_ANY_URI,
|
|
176
|
+
XSD_DATETIME,
|
|
177
|
+
XSD_DATETIME_STAMP,
|
|
178
|
+
...INTEGER_BOUNDS.keys()
|
|
179
|
+
]);
|
|
180
|
+
/** literalDatatype is the datatype IRI of `literal`, `rdf:langString` if tagged. */
|
|
181
|
+
function literalDatatype(literal) {
|
|
182
|
+
if (literal.language) return RDF_LANGSTRING;
|
|
183
|
+
return literal.datatype?.value ?? XSD_STRING;
|
|
184
|
+
}
|
|
185
|
+
/** isSupportedDatatype is `true` for a datatype OWL 2 RL's rules evaluate. */
|
|
186
|
+
function isSupportedDatatype(datatype) {
|
|
187
|
+
return SUPPORTED.has(datatype);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* datatypeFamily names the value space a datatype's values live in; values of
|
|
191
|
+
* different families are never equal, and a literal is only ever an instance
|
|
192
|
+
* of datatypes in its own family (and of `rdfs:Literal`).
|
|
193
|
+
*/
|
|
194
|
+
function datatypeFamily(datatype) {
|
|
195
|
+
if (!SUPPORTED.has(datatype)) return void 0;
|
|
196
|
+
if (STRING_LIKE.has(datatype)) return "string";
|
|
197
|
+
if (datatype === XSD_DECIMAL || datatype === XSD_INTEGER) return "decimal";
|
|
198
|
+
if (INTEGER_BOUNDS.has(datatype)) return "decimal";
|
|
199
|
+
if (datatype === XSD_DATETIME_STAMP) return XSD_DATETIME;
|
|
200
|
+
return datatype;
|
|
201
|
+
}
|
|
202
|
+
/** literalValue parses `literal` in its own datatype, or `null` if ill-typed. */
|
|
203
|
+
function literalValue(literal) {
|
|
204
|
+
return valueFor(literal, literalDatatype(literal));
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* isInstanceOf is `true` when `literal` is a value of `datatype`: the two are
|
|
208
|
+
* in one family and the literal's spelling is valid for `datatype`.
|
|
209
|
+
*/
|
|
210
|
+
function isInstanceOf(literal, datatype) {
|
|
211
|
+
if (datatype !== RDFS_LITERAL) {
|
|
212
|
+
const own = datatypeFamily(literalDatatype(literal));
|
|
213
|
+
if (own === void 0 || own !== datatypeFamily(datatype)) return false;
|
|
214
|
+
if (own === "decimal") return inDecimalSpace(literal, datatype);
|
|
215
|
+
}
|
|
216
|
+
return valueFor(literal, datatype) !== null;
|
|
217
|
+
}
|
|
218
|
+
/** inDecimalSpace is `true` when the literal's decimal value is in `datatype`. */
|
|
219
|
+
function inDecimalSpace(literal, datatype) {
|
|
220
|
+
const value = literalValue(literal);
|
|
221
|
+
if (value === null || value.family !== "numeric" || value.kind !== "decimal") return false;
|
|
222
|
+
if (datatype === XSD_DECIMAL) return true;
|
|
223
|
+
if (value.scale !== 0) return false;
|
|
224
|
+
if (datatype === XSD_INTEGER) return true;
|
|
225
|
+
const [min, max] = INTEGER_BOUNDS.get(datatype);
|
|
226
|
+
return (min === null || value.num >= min) && (max === null || value.num <= max);
|
|
227
|
+
}
|
|
228
|
+
/** isInvalidFor is `true` for a supported `datatype` that `literal` is not in. */
|
|
229
|
+
function isInvalidFor(literal, datatype) {
|
|
230
|
+
return isSupportedDatatype(datatype) && !isInstanceOf(literal, datatype);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* valuesDiffer is `true` when two literals have comparable values that are
|
|
234
|
+
* not equal (Eyeling's `dt:differentValueFrom`).
|
|
235
|
+
*/
|
|
236
|
+
function valuesDiffer(left, right) {
|
|
237
|
+
const a = literalValue(left);
|
|
238
|
+
const b = literalValue(right);
|
|
239
|
+
if (!comparable(a, b)) return false;
|
|
240
|
+
return !sameValue(a, b);
|
|
241
|
+
}
|
|
242
|
+
function valueFor(literal, datatype) {
|
|
243
|
+
if (!SUPPORTED.has(datatype)) return null;
|
|
244
|
+
if (datatype === RDFS_LITERAL) {
|
|
245
|
+
const own = literalDatatype(literal);
|
|
246
|
+
if (!SUPPORTED.has(own) || valueFor(literal, own) === null) return null;
|
|
247
|
+
return {
|
|
248
|
+
family: "literal",
|
|
249
|
+
comparable: false
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const lexical = literal.value;
|
|
253
|
+
if (datatype === RDF_LANGSTRING) {
|
|
254
|
+
if (!literal.language || !isValidLanguageTag(literal.language, true)) return null;
|
|
255
|
+
return {
|
|
256
|
+
family: "langString",
|
|
257
|
+
value: lexical,
|
|
258
|
+
lang: literal.language.toLowerCase(),
|
|
259
|
+
comparable: true
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
if (datatype === RDF_PLAIN_LITERAL) return plainLiteralValue(lexical);
|
|
263
|
+
if (datatype === RDF_XML_LITERAL) {
|
|
264
|
+
if (!isWellFormedXmlFragment(lexical)) return null;
|
|
265
|
+
return {
|
|
266
|
+
family: "xmlLiteral",
|
|
267
|
+
value: lexical.trim(),
|
|
268
|
+
comparable: true
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
if (literal.language) return null;
|
|
272
|
+
if (STRING_LIKE.has(datatype)) return stringValue(lexical, datatype);
|
|
273
|
+
if (datatype === XSD_BOOLEAN) return booleanValue(lexical);
|
|
274
|
+
if (datatype === XSD_INTEGER || INTEGER_BOUNDS.has(datatype)) return integerValue(lexical, datatype);
|
|
275
|
+
if (datatype === XSD_DECIMAL) return decimalValue(lexical);
|
|
276
|
+
if (datatype === XSD_FLOAT || datatype === XSD_DOUBLE) return floatingValue(lexical, datatype);
|
|
277
|
+
if (datatype === XSD_HEX_BINARY || datatype === XSD_BASE64_BINARY) return binaryValue(lexical, datatype);
|
|
278
|
+
if (datatype === XSD_ANY_URI) return anyUriValue(lexical);
|
|
279
|
+
if (datatype === XSD_DATETIME || datatype === XSD_DATETIME_STAMP) return dateTimeValue(lexical, datatype);
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
function comparable(a, b) {
|
|
283
|
+
if (!a || !b || !a.comparable || !b.comparable) return false;
|
|
284
|
+
if (a.family === "dateTime" && b.family === "dateTime") return a.hasTimezone === b.hasTimezone;
|
|
285
|
+
return a.family === b.family;
|
|
286
|
+
}
|
|
287
|
+
function sameValue(a, b) {
|
|
288
|
+
switch (a.family) {
|
|
289
|
+
case "langString":
|
|
290
|
+
case "plainLiteral": return b.family === a.family && a.value === b.value && a.lang === b.lang;
|
|
291
|
+
case "string":
|
|
292
|
+
case "xmlLiteral":
|
|
293
|
+
case "anyURI":
|
|
294
|
+
case "boolean": return b.family === a.family && a.value === b.value;
|
|
295
|
+
case "binary":
|
|
296
|
+
if (b.family !== "binary" || a.bytes.length !== b.bytes.length) return false;
|
|
297
|
+
return a.bytes.every((byte, index) => byte === b.bytes[index]);
|
|
298
|
+
case "numeric": {
|
|
299
|
+
if (b.family !== "numeric") return false;
|
|
300
|
+
if (a.kind === "decimal" && b.kind === "decimal") {
|
|
301
|
+
const scale = Math.max(a.scale, b.scale);
|
|
302
|
+
return a.num * 10n ** BigInt(scale - a.scale) === b.num * 10n ** BigInt(scale - b.scale);
|
|
303
|
+
}
|
|
304
|
+
const left = a.kind === "decimal" ? Number(a.num) / 10 ** a.scale : a.value;
|
|
305
|
+
const right = b.kind === "decimal" ? Number(b.num) / 10 ** b.scale : b.value;
|
|
306
|
+
if (Number.isNaN(left) || Number.isNaN(right) || left !== right) return false;
|
|
307
|
+
return a.kind !== "floating" || b.kind !== "floating" || Object.is(left, right);
|
|
308
|
+
}
|
|
309
|
+
case "dateTime": {
|
|
310
|
+
if (b.family !== "dateTime" || a.hasTimezone !== b.hasTimezone) return false;
|
|
311
|
+
if (!a.hasTimezone) return a.localKey === b.localKey;
|
|
312
|
+
if (a.totalSeconds !== b.totalSeconds) return false;
|
|
313
|
+
const scale = Math.max(a.fracScale, b.fracScale);
|
|
314
|
+
return a.fracNum * 10n ** BigInt(scale - a.fracScale) === b.fracNum * 10n ** BigInt(scale - b.fracScale);
|
|
315
|
+
}
|
|
316
|
+
default: return false;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function isValidLanguageTag(tag, allowDirection) {
|
|
320
|
+
return (allowDirection ? /^[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*(?:--(?:ltr|rtl))?$/ : /^[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*$/).test(tag);
|
|
321
|
+
}
|
|
322
|
+
function plainLiteralValue(lexical) {
|
|
323
|
+
const at = lexical.lastIndexOf("@");
|
|
324
|
+
if (at < 0) return null;
|
|
325
|
+
const lang = lexical.slice(at + 1);
|
|
326
|
+
if (lang !== "" && !isValidLanguageTag(lang, false)) return null;
|
|
327
|
+
return {
|
|
328
|
+
family: "plainLiteral",
|
|
329
|
+
value: lexical.slice(0, at),
|
|
330
|
+
lang: lang.toLowerCase(),
|
|
331
|
+
comparable: true
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function stringValue(lexical, datatype) {
|
|
335
|
+
if (datatype === "http://www.w3.org/2001/XMLSchema#normalizedString") {
|
|
336
|
+
if (/[\t\n\r]/.test(lexical)) return null;
|
|
337
|
+
} else if (datatype !== XSD_STRING) {
|
|
338
|
+
if (/[\t\n\r]/.test(lexical) || /^ | $/.test(lexical) || / {2,}/.test(lexical)) return null;
|
|
339
|
+
}
|
|
340
|
+
if (datatype === "http://www.w3.org/2001/XMLSchema#language") {
|
|
341
|
+
if (!isValidLanguageTag(lexical, false)) return null;
|
|
342
|
+
} else if (datatype === "http://www.w3.org/2001/XMLSchema#Name") {
|
|
343
|
+
if (!/^[:_\p{L}][\u0300-\u036F:_\p{L}\p{N}.\-\u00B7\u203F-\u2040]*$/u.test(lexical)) return null;
|
|
344
|
+
} else if (datatype === "http://www.w3.org/2001/XMLSchema#NCName") {
|
|
345
|
+
if (!/^[_\p{L}][\u0300-\u036F_\p{L}\p{N}.\-\u00B7\u203F-\u2040]*$/u.test(lexical)) return null;
|
|
346
|
+
} else if (datatype === "http://www.w3.org/2001/XMLSchema#NMTOKEN") {
|
|
347
|
+
if (!/^[\u0300-\u036F:_\p{L}\p{N}.\-\u00B7\u203F-\u2040]+$/u.test(lexical)) return null;
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
family: "string",
|
|
351
|
+
value: datatype === "http://www.w3.org/2001/XMLSchema#language" ? lexical.toLowerCase() : lexical,
|
|
352
|
+
comparable: true
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function booleanValue(lexical) {
|
|
356
|
+
if (lexical === "true" || lexical === "1") return {
|
|
357
|
+
family: "boolean",
|
|
358
|
+
value: true,
|
|
359
|
+
comparable: true
|
|
360
|
+
};
|
|
361
|
+
if (lexical === "false" || lexical === "0") return {
|
|
362
|
+
family: "boolean",
|
|
363
|
+
value: false,
|
|
364
|
+
comparable: true
|
|
365
|
+
};
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
function integerValue(lexical, datatype) {
|
|
369
|
+
if (!/^[+-]?\d+$/.test(lexical)) return null;
|
|
370
|
+
const value = BigInt(lexical);
|
|
371
|
+
const bounds = INTEGER_BOUNDS.get(datatype);
|
|
372
|
+
if (bounds) {
|
|
373
|
+
const [min, max] = bounds;
|
|
374
|
+
if (min !== null && value < min) return null;
|
|
375
|
+
if (max !== null && value > max) return null;
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
family: "numeric",
|
|
379
|
+
kind: "decimal",
|
|
380
|
+
num: value,
|
|
381
|
+
scale: 0,
|
|
382
|
+
comparable: true
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function decimalValue(lexical) {
|
|
386
|
+
let text = lexical;
|
|
387
|
+
let sign = 1n;
|
|
388
|
+
if (text.startsWith("+")) text = text.slice(1);
|
|
389
|
+
else if (text.startsWith("-")) {
|
|
390
|
+
sign = -1n;
|
|
391
|
+
text = text.slice(1);
|
|
392
|
+
}
|
|
393
|
+
if (!/^(?:\d+(?:\.\d*)?|\.\d+)$/.test(text)) return null;
|
|
394
|
+
let [whole = "0", fraction = ""] = text.split(".");
|
|
395
|
+
if (whole === "") whole = "0";
|
|
396
|
+
whole = whole.replace(/^0+(?=\d)/, "");
|
|
397
|
+
fraction = fraction.replace(/0+$/, "");
|
|
398
|
+
return {
|
|
399
|
+
family: "numeric",
|
|
400
|
+
kind: "decimal",
|
|
401
|
+
num: sign * BigInt(whole + fraction || "0"),
|
|
402
|
+
scale: fraction.length,
|
|
403
|
+
comparable: true
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
const FLOAT_MAX = 34028234663852886e22;
|
|
407
|
+
const FLOAT_MIN_POSITIVE = 1401298464324817e-60;
|
|
408
|
+
function floatingValue(lexical, datatype) {
|
|
409
|
+
const special = lexical === "INF" || lexical === "+INF" ? 1e400 : lexical === "-INF" ? -1e400 : lexical === "NaN" ? NaN : null;
|
|
410
|
+
if (special === null && !/^[+-]?(?:(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|\d+(?:[eE][+-]?\d+))$/.test(lexical)) return null;
|
|
411
|
+
const value = special ?? Number(lexical);
|
|
412
|
+
if (Number.isFinite(value)) {
|
|
413
|
+
const magnitude = Math.abs(value);
|
|
414
|
+
const [max, min] = datatype === XSD_FLOAT ? [FLOAT_MAX, FLOAT_MIN_POSITIVE] : [Number.MAX_VALUE, Number.MIN_VALUE];
|
|
415
|
+
if (magnitude > 0 && (magnitude > max || magnitude < min)) return null;
|
|
416
|
+
}
|
|
417
|
+
return {
|
|
418
|
+
family: "numeric",
|
|
419
|
+
kind: "floating",
|
|
420
|
+
value,
|
|
421
|
+
comparable: !Number.isNaN(value)
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
425
|
+
function binaryValue(lexical, datatype) {
|
|
426
|
+
if (datatype === XSD_HEX_BINARY) {
|
|
427
|
+
if (!/^(?:[0-9A-Fa-f]{2})*$/.test(lexical)) return null;
|
|
428
|
+
const bytes = [];
|
|
429
|
+
for (let index = 0; index < lexical.length; index += 2) bytes.push(parseInt(lexical.slice(index, index + 2), 16));
|
|
430
|
+
return {
|
|
431
|
+
family: "binary",
|
|
432
|
+
bytes,
|
|
433
|
+
comparable: true
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
const text = lexical.replace(/[\t\n\r ]+/g, "");
|
|
437
|
+
if (text.length === 0) return {
|
|
438
|
+
family: "binary",
|
|
439
|
+
bytes: [],
|
|
440
|
+
comparable: true
|
|
441
|
+
};
|
|
442
|
+
if (text.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}$/.test(text)) return null;
|
|
443
|
+
const pad = text.endsWith("==") ? 2 : text.endsWith("=") ? 1 : 0;
|
|
444
|
+
if (pad && text.slice(0, -pad).includes("=")) return null;
|
|
445
|
+
const bytes = [];
|
|
446
|
+
for (let index = 0; index < text.length; index += 4) {
|
|
447
|
+
const codes = [
|
|
448
|
+
0,
|
|
449
|
+
1,
|
|
450
|
+
2,
|
|
451
|
+
3
|
|
452
|
+
].map((offset) => {
|
|
453
|
+
const char = text[index + offset];
|
|
454
|
+
return char === "=" ? 0 : BASE64.indexOf(char);
|
|
455
|
+
});
|
|
456
|
+
if (codes.some((code) => code < 0)) return null;
|
|
457
|
+
const n = codes[0] << 18 | codes[1] << 12 | codes[2] << 6 | codes[3];
|
|
458
|
+
bytes.push(n >> 16 & 255);
|
|
459
|
+
if (text[index + 2] !== "=") bytes.push(n >> 8 & 255);
|
|
460
|
+
if (text[index + 3] !== "=") bytes.push(n & 255);
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
family: "binary",
|
|
464
|
+
bytes,
|
|
465
|
+
comparable: true
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
function anyUriValue(lexical) {
|
|
469
|
+
if (/[^!-~]/.test(lexical) || /[<>"{}|\\^`]/.test(lexical)) return null;
|
|
470
|
+
for (let index = 0; index < lexical.length; index++) if (lexical[index] === "%" && !/^[0-9A-Fa-f]{2}$/.test(lexical.slice(index + 1, index + 3))) return null;
|
|
471
|
+
if (lexical !== "" && !/^[A-Za-z][A-Za-z0-9+.-]*:/.test(lexical) && !/^[/?#]/.test(lexical)) {
|
|
472
|
+
const cut = lexical.search(/[/?#]/);
|
|
473
|
+
if ((cut < 0 ? lexical : lexical.slice(0, cut)).includes(":")) return null;
|
|
474
|
+
}
|
|
475
|
+
return {
|
|
476
|
+
family: "anyURI",
|
|
477
|
+
value: lexical,
|
|
478
|
+
comparable: true
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
function isWellFormedXmlFragment(input) {
|
|
482
|
+
const source = input.trim();
|
|
483
|
+
if (!source) return false;
|
|
484
|
+
const stack = [];
|
|
485
|
+
let seenElement = false;
|
|
486
|
+
let position = 0;
|
|
487
|
+
while (position < source.length) {
|
|
488
|
+
const open = source.indexOf("<", position);
|
|
489
|
+
if (open < 0) return stack.length === 0 && isValidXmlText(source.slice(position)) && seenElement;
|
|
490
|
+
if (!isValidXmlText(source.slice(position, open))) return false;
|
|
491
|
+
const skip = (start, end) => {
|
|
492
|
+
if (!source.startsWith(start, open)) return false;
|
|
493
|
+
const close = source.indexOf(end, open + start.length);
|
|
494
|
+
if (close < 0) throw new SyntaxError("unterminated");
|
|
495
|
+
if (start === "<!--" && source.slice(open + 4, close).includes("--")) throw new SyntaxError("double hyphen");
|
|
496
|
+
position = close + end.length;
|
|
497
|
+
return true;
|
|
498
|
+
};
|
|
499
|
+
try {
|
|
500
|
+
if (skip("<!--", "-->") || skip("<![CDATA[", "]]>") || skip("<?", "?>")) continue;
|
|
501
|
+
} catch {
|
|
502
|
+
return false;
|
|
503
|
+
}
|
|
504
|
+
if (source.startsWith("</", open)) {
|
|
505
|
+
const close = source.indexOf(">", open + 2);
|
|
506
|
+
if (close < 0) return false;
|
|
507
|
+
const name = source.slice(open + 2, close).trim();
|
|
508
|
+
if (!isValidXmlName(name) || stack.pop() !== name) return false;
|
|
509
|
+
position = close + 1;
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
const close = findXmlTagEnd(source, open + 1);
|
|
513
|
+
if (close < 0) return false;
|
|
514
|
+
const raw = source.slice(open + 1, close);
|
|
515
|
+
const selfClosing = /\/\s*$/.test(raw);
|
|
516
|
+
const content = selfClosing ? raw.replace(/\/\s*$/, "").trim() : raw.trim();
|
|
517
|
+
const name = /^([^\s/>]+)/.exec(content);
|
|
518
|
+
if (!name || !isValidXmlName(name[1]) || !areValidXmlAttributes(content.slice(name[0].length))) return false;
|
|
519
|
+
seenElement = true;
|
|
520
|
+
if (!selfClosing) stack.push(name[1]);
|
|
521
|
+
position = close + 1;
|
|
522
|
+
}
|
|
523
|
+
return stack.length === 0 && seenElement;
|
|
524
|
+
}
|
|
525
|
+
function findXmlTagEnd(source, start) {
|
|
526
|
+
let quote = null;
|
|
527
|
+
for (let index = start; index < source.length; index++) {
|
|
528
|
+
const char = source[index];
|
|
529
|
+
if (quote) {
|
|
530
|
+
if (char === quote) quote = null;
|
|
531
|
+
} else if (char === "\"" || char === "'") quote = char;
|
|
532
|
+
else if (char === ">") return index;
|
|
533
|
+
}
|
|
534
|
+
return -1;
|
|
535
|
+
}
|
|
536
|
+
function areValidXmlAttributes(source) {
|
|
537
|
+
let rest = source.trim();
|
|
538
|
+
const seen = /* @__PURE__ */ new Set();
|
|
539
|
+
while (rest) {
|
|
540
|
+
const match = /^([A-Za-z_:\u0080-][A-Za-z0-9_.:\-\u0080-]*)\s*=\s*("[^"]*"|'[^']*')\s*/u.exec(rest);
|
|
541
|
+
if (!match || seen.has(match[1])) return false;
|
|
542
|
+
const value = match[2].slice(1, -1);
|
|
543
|
+
if (/</.test(value) || !isValidXmlText(value)) return false;
|
|
544
|
+
seen.add(match[1]);
|
|
545
|
+
rest = rest.slice(match[0].length).trim();
|
|
546
|
+
}
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
function isValidXmlText(value) {
|
|
550
|
+
return !/[<&]/.test(value.replace(/&(?:amp|lt|gt|quot|apos|#\d+|#x[0-9A-Fa-f]+);/g, ""));
|
|
551
|
+
}
|
|
552
|
+
function isValidXmlName(name) {
|
|
553
|
+
return /^[A-Za-z_:\u0080-][A-Za-z0-9_.:\-\u0080-]*$/u.test(name) && !/^xml$/i.test(name);
|
|
554
|
+
}
|
|
555
|
+
function isLeapYear(year) {
|
|
556
|
+
if (year % 400n === 0n) return true;
|
|
557
|
+
if (year % 100n === 0n) return false;
|
|
558
|
+
return year % 4n === 0n;
|
|
559
|
+
}
|
|
560
|
+
function daysInMonth(year, month) {
|
|
561
|
+
if (month === 2) return isLeapYear(year) ? 29 : 28;
|
|
562
|
+
return [
|
|
563
|
+
4,
|
|
564
|
+
6,
|
|
565
|
+
9,
|
|
566
|
+
11
|
|
567
|
+
].includes(month) ? 30 : 31;
|
|
568
|
+
}
|
|
569
|
+
function floorDiv(a, b) {
|
|
570
|
+
let quotient = a / b;
|
|
571
|
+
const remainder = a % b;
|
|
572
|
+
if (remainder !== 0n && remainder > 0n !== b > 0n) quotient -= 1n;
|
|
573
|
+
return quotient;
|
|
574
|
+
}
|
|
575
|
+
function daysFromCivil(year, month, day) {
|
|
576
|
+
const y = month <= 2 ? year - 1n : year;
|
|
577
|
+
const era = floorDiv(y, 400n);
|
|
578
|
+
const yearOfEra = y - era * 400n;
|
|
579
|
+
const dayOfYear = (153n * BigInt(month + (month > 2 ? -3 : 9)) + 2n) / 5n + BigInt(day) - 1n;
|
|
580
|
+
const dayOfEra = yearOfEra * 365n + yearOfEra / 4n - yearOfEra / 100n + dayOfYear;
|
|
581
|
+
return era * 146097n + dayOfEra - 719468n;
|
|
582
|
+
}
|
|
583
|
+
function civilFromDays(days) {
|
|
584
|
+
const z = days + 719468n;
|
|
585
|
+
const era = floorDiv(z, 146097n);
|
|
586
|
+
const dayOfEra = z - era * 146097n;
|
|
587
|
+
const yearOfEra = (dayOfEra - dayOfEra / 1460n + dayOfEra / 36524n - dayOfEra / 146096n) / 365n;
|
|
588
|
+
let year = yearOfEra + era * 400n;
|
|
589
|
+
const dayOfYear = dayOfEra - (365n * yearOfEra + yearOfEra / 4n - yearOfEra / 100n);
|
|
590
|
+
const shifted = (5n * dayOfYear + 2n) / 153n;
|
|
591
|
+
const day = dayOfYear - (153n * shifted + 2n) / 5n + 1n;
|
|
592
|
+
const month = shifted < 10n ? shifted + 3n : shifted - 9n;
|
|
593
|
+
if (month <= 2n) year += 1n;
|
|
594
|
+
return {
|
|
595
|
+
year,
|
|
596
|
+
month: Number(month),
|
|
597
|
+
day: Number(day)
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
function localKey(year, month, day, hour, minute, second, fraction) {
|
|
601
|
+
const yearText = year < 0n ? `-${(-year).toString().padStart(4, "0")}` : year.toString().padStart(4, "0");
|
|
602
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
603
|
+
return `${yearText}-${pad(month)}-${pad(day)}T${pad(hour)}:${pad(minute)}:${pad(second)}${fraction ? `.${fraction}` : ""}`;
|
|
604
|
+
}
|
|
605
|
+
function dateTimeValue(lexical, datatype) {
|
|
606
|
+
const match = /^(-?\d{4,})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(Z|[+-]\d{2}:\d{2})?$/.exec(lexical);
|
|
607
|
+
if (!match) return null;
|
|
608
|
+
const yearDigits = match[1].replace(/^-/, "");
|
|
609
|
+
if (yearDigits.length !== 4 && yearDigits[0] === "0") return null;
|
|
610
|
+
let year = BigInt(match[1]);
|
|
611
|
+
let month = Number(match[2]);
|
|
612
|
+
let day = Number(match[3]);
|
|
613
|
+
let hour = Number(match[4]);
|
|
614
|
+
const minute = Number(match[5]);
|
|
615
|
+
const second = Number(match[6]);
|
|
616
|
+
let fraction = match[7] ?? "";
|
|
617
|
+
const zone = match[8] ?? null;
|
|
618
|
+
if (datatype === XSD_DATETIME_STAMP && !zone) return null;
|
|
619
|
+
if (month < 1 || month > 12) return null;
|
|
620
|
+
if (day < 1 || day > daysInMonth(year, month)) return null;
|
|
621
|
+
if (minute > 59 || second > 59) return null;
|
|
622
|
+
let dayCount = daysFromCivil(year, month, day);
|
|
623
|
+
if (hour === 24) {
|
|
624
|
+
if (minute !== 0 || second !== 0 || /[1-9]/.test(fraction)) return null;
|
|
625
|
+
hour = 0;
|
|
626
|
+
dayCount += 1n;
|
|
627
|
+
({year, month, day} = civilFromDays(dayCount));
|
|
628
|
+
} else if (hour > 23) return null;
|
|
629
|
+
fraction = fraction.replace(/0+$/g, "");
|
|
630
|
+
let offsetMinutes = null;
|
|
631
|
+
if (zone === "Z") offsetMinutes = 0;
|
|
632
|
+
else if (zone) {
|
|
633
|
+
const hours = Number(zone.slice(1, 3));
|
|
634
|
+
const minutes = Number(zone.slice(4, 6));
|
|
635
|
+
if (hours > 14 || minutes > 59 || hours === 14 && minutes !== 0) return null;
|
|
636
|
+
offsetMinutes = (zone[0] === "-" ? -1 : 1) * (hours * 60 + minutes);
|
|
637
|
+
}
|
|
638
|
+
let totalSeconds = dayCount * 86400n + BigInt(hour * 3600 + minute * 60 + second);
|
|
639
|
+
if (offsetMinutes !== null) totalSeconds -= BigInt(offsetMinutes * 60);
|
|
640
|
+
return {
|
|
641
|
+
family: "dateTime",
|
|
642
|
+
hasTimezone: offsetMinutes !== null,
|
|
643
|
+
totalSeconds,
|
|
644
|
+
fracNum: fraction ? BigInt(fraction) : 0n,
|
|
645
|
+
fracScale: fraction.length,
|
|
646
|
+
localKey: localKey(year, month, day, hour, minute, second, fraction),
|
|
647
|
+
comparable: true
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* valueKey is a string that two literals share exactly when they are the same
|
|
652
|
+
* value of the numeric or boolean value space, or `null` for any other
|
|
653
|
+
* literal (other datatypes, ill-typed or NaN literals). It is what dt-eq
|
|
654
|
+
* groups literals by, in linear time, instead of comparing every pair.
|
|
655
|
+
*
|
|
656
|
+
* The spaces follow OWL 2's datatype map, where the value spaces of
|
|
657
|
+
* `xsd:decimal` (with the whole integer family), `xsd:float` and `xsd:double`
|
|
658
|
+
* are disjoint:
|
|
659
|
+
*
|
|
660
|
+
* - `"1"^^xsd:integer`, `"01"^^xsd:int` and `"1.0"^^xsd:decimal` agree.
|
|
661
|
+
* - Floats agree with floats and doubles with doubles (`"1.0"^^xsd:double` is
|
|
662
|
+
* `"1"^^xsd:double`), never with each other or with a decimal. +0 and -0
|
|
663
|
+
* stay distinct values, as they are in {@link valuesDiffer}.
|
|
664
|
+
* - Booleans key on their value, so `"true"` and `"1"` agree.
|
|
665
|
+
*
|
|
666
|
+
* ({@link valuesDiffer} still treats a decimal and a float of the same number
|
|
667
|
+
* as not different, as Eyeling's `dt:differentValueFrom` did; that is a
|
|
668
|
+
* comparison and merges nothing.)
|
|
669
|
+
*/
|
|
670
|
+
function valueKey(literal) {
|
|
671
|
+
const value = literalValue(literal);
|
|
672
|
+
if (value === null || !value.comparable) return null;
|
|
673
|
+
if (value.family === "boolean") return value.value ? "b:true" : "b:false";
|
|
674
|
+
if (value.family !== "numeric") return null;
|
|
675
|
+
if (value.kind === "decimal") {
|
|
676
|
+
let { num, scale } = value;
|
|
677
|
+
if (num === 0n) return "decimal:0";
|
|
678
|
+
while (scale > 0 && num % 10n === 0n) {
|
|
679
|
+
num /= 10n;
|
|
680
|
+
scale--;
|
|
681
|
+
}
|
|
682
|
+
return `decimal:${num}e-${scale}`;
|
|
683
|
+
}
|
|
684
|
+
return `${literalDatatype(literal) === XSD_FLOAT ? "float" : "double"}:${Object.is(value.value, -0) ? "-0" : String(value.value)}`;
|
|
685
|
+
}
|
|
686
|
+
// src/rule.ts
|
|
687
|
+
/** Variable is a named rule variable. */
|
|
688
|
+
var Variable = class {
|
|
689
|
+
name;
|
|
690
|
+
constructor(name) {
|
|
691
|
+
this.name = name;
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
/** atom builds an {@link AtomPattern}. */
|
|
695
|
+
function atom(relation, ...args) {
|
|
696
|
+
return {
|
|
697
|
+
kind: "atom",
|
|
698
|
+
relation,
|
|
699
|
+
args
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
const PASS = [[]];
|
|
703
|
+
const FAIL = [];
|
|
704
|
+
/** filter builds a builtin that keeps a solution when `test` holds. */
|
|
705
|
+
function filter(name, inputs, test) {
|
|
706
|
+
return {
|
|
707
|
+
kind: "builtin",
|
|
708
|
+
name,
|
|
709
|
+
inputs,
|
|
710
|
+
outputs: [],
|
|
711
|
+
run: (values, context) => test(values, context) ? PASS : FAIL
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
/** bind builds a builtin that binds `outputs` from `inputs`. */
|
|
715
|
+
function bind(name, inputs, outputs, run) {
|
|
716
|
+
return {
|
|
717
|
+
kind: "builtin",
|
|
718
|
+
name,
|
|
719
|
+
inputs,
|
|
720
|
+
outputs,
|
|
721
|
+
run
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* variables returns a proxy whose every property is the variable of that
|
|
726
|
+
* name, so rules read like their tables: `const { x, p, y } = variables()`.
|
|
727
|
+
*/
|
|
728
|
+
function variables() {
|
|
729
|
+
const cache = /* @__PURE__ */ new Map();
|
|
730
|
+
return new Proxy({}, { get(_target, name) {
|
|
731
|
+
if (typeof name !== "string") return void 0;
|
|
732
|
+
let variable = cache.get(name);
|
|
733
|
+
if (variable === void 0) {
|
|
734
|
+
variable = new Variable(name);
|
|
735
|
+
cache.set(name, variable);
|
|
736
|
+
}
|
|
737
|
+
return variable;
|
|
738
|
+
} });
|
|
739
|
+
}
|
|
740
|
+
// src/owl2rl/rules.ts
|
|
741
|
+
/** TRIPLE is the relation holding the graph. */
|
|
742
|
+
const TRIPLE = "triple";
|
|
743
|
+
/** INCONSISTENCY is the relation holding one row per inconsistency found. */
|
|
744
|
+
const INCONSISTENCY = "inconsistency";
|
|
745
|
+
/** RELATIONS declares every relation the ruleset uses, with its arity. */
|
|
746
|
+
const RELATIONS = {
|
|
747
|
+
[TRIPLE]: 3,
|
|
748
|
+
[INCONSISTENCY]: 6,
|
|
749
|
+
list: 1,
|
|
750
|
+
member: 2,
|
|
751
|
+
pair: 3,
|
|
752
|
+
chainList: 1,
|
|
753
|
+
chainPath: 3,
|
|
754
|
+
intersectionList: 1,
|
|
755
|
+
allTypes: 2,
|
|
756
|
+
keyContext: 2,
|
|
757
|
+
sameValues: 4,
|
|
758
|
+
literal: 1,
|
|
759
|
+
literalValue: 2
|
|
760
|
+
};
|
|
761
|
+
/** VALUE_PREFIX starts the internal term naming one numeric or boolean value. */
|
|
762
|
+
const VALUE_PREFIX = "https://wazoo.dev/reasoner/ns#value/";
|
|
763
|
+
/** NONE pads an inconsistency row that names fewer than five terms. */
|
|
764
|
+
const NONE = dataFactory.namedNode("https://wazoo.dev/reasoner/ns#none");
|
|
765
|
+
/** COMPLEMENT_PREFIX starts the skolem IRI naming the complement of a class. */
|
|
766
|
+
const COMPLEMENT_PREFIX = "https://wazoo.dev/.well-known/genid/reasoner/complement/";
|
|
767
|
+
/** CHAIN_PREFIX starts the skolem IRIs naming the list nodes of a derived chain. */
|
|
768
|
+
const CHAIN_PREFIX = "https://wazoo.dev/.well-known/genid/reasoner/chain/";
|
|
769
|
+
/** isSkolemTerm is `true` for a term the rules mint for their own bookkeeping. */
|
|
770
|
+
function isSkolemTerm(term) {
|
|
771
|
+
return term.termType === "NamedNode" && (term.value.startsWith("https://wazoo.dev/.well-known/genid/reasoner/complement/") || term.value.startsWith("https://wazoo.dev/.well-known/genid/reasoner/chain/"));
|
|
772
|
+
}
|
|
773
|
+
const v = variables();
|
|
774
|
+
const ZERO = dataFactory.literal("0", XSD.nonNegativeInteger);
|
|
775
|
+
const ONE = dataFactory.literal("1", XSD.nonNegativeInteger);
|
|
776
|
+
function t(s, p, o) {
|
|
777
|
+
return atom(TRIPLE, s, p, o);
|
|
778
|
+
}
|
|
779
|
+
function rule(name, body, head) {
|
|
780
|
+
return {
|
|
781
|
+
name,
|
|
782
|
+
body,
|
|
783
|
+
head
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
/** inconsistent builds the head row that reports rule `name` over `terms`. */
|
|
787
|
+
function inconsistent(name, ...terms) {
|
|
788
|
+
const padded = [...terms];
|
|
789
|
+
while (padded.length < 5) padded.push(NONE);
|
|
790
|
+
return atom(INCONSISTENCY, dataFactory.literal(name), ...padded);
|
|
791
|
+
}
|
|
792
|
+
/** literalsOf yields the members of `id`'s class that are literals. */
|
|
793
|
+
function* literalsOf(id, context) {
|
|
794
|
+
for (const member of context.members(context.find(id))) {
|
|
795
|
+
const term = context.term(member);
|
|
796
|
+
if (term.termType === "Literal") yield [member, term];
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* isPlainString is `true` for the datatypes the old ruleset typed directly
|
|
801
|
+
* instead of indexing them for value comparison.
|
|
802
|
+
*/
|
|
803
|
+
function isPlainString(literal) {
|
|
804
|
+
const datatype = literalDatatype(literal);
|
|
805
|
+
return datatype === XSD.string.value || datatype === RDF.langString.value;
|
|
806
|
+
}
|
|
807
|
+
const literalMember = bind("literal-member", [v.lt], [v.m], function* (inputs, context) {
|
|
808
|
+
for (const [member, literal] of literalsOf(inputs[0], context)) if (!isPlainString(literal)) yield [member];
|
|
809
|
+
});
|
|
810
|
+
const stringMember = (datatype) => bind(`${datatype.value}-member`, [v.o], [v.lt], function* (inputs, context) {
|
|
811
|
+
for (const [member, literal] of literalsOf(inputs[0], context)) if (literalDatatype(literal) === datatype.value) yield [member];
|
|
812
|
+
});
|
|
813
|
+
/** someValuesDiffer: some non-string literals of the two classes differ. */
|
|
814
|
+
function someValuesDiffer(left, right, context, strings) {
|
|
815
|
+
for (const [, a] of literalsOf(left, context)) {
|
|
816
|
+
if (isPlainString(a) !== strings) continue;
|
|
817
|
+
if (strings && literalDatatype(a) !== XSD.string.value) continue;
|
|
818
|
+
for (const [, b] of literalsOf(right, context)) {
|
|
819
|
+
if (isPlainString(b) !== strings) continue;
|
|
820
|
+
if (strings && literalDatatype(b) !== XSD.string.value) continue;
|
|
821
|
+
if (valuesDiffer(a, b)) return true;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return false;
|
|
825
|
+
}
|
|
826
|
+
/** complementSkolem names the complement class of `named`. */
|
|
827
|
+
function complementSkolem(named) {
|
|
828
|
+
return dataFactory.namedNode(COMPLEMENT_PREFIX + encodeURIComponent(termKey(named)));
|
|
829
|
+
}
|
|
830
|
+
const complementOf = bind("complement", [v.c], [v.notC], (inputs, context) => {
|
|
831
|
+
const named = context.term(context.find(inputs[0]));
|
|
832
|
+
return [[context.id(complementSkolem(named))]];
|
|
833
|
+
});
|
|
834
|
+
/** complementHead declares `?notC` as the complement class of `c`. */
|
|
835
|
+
function complementHead(c) {
|
|
836
|
+
return [t(v.notC, RDF.type, OWL.Class), t(v.notC, OWL.complementOf, c)];
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* transitiveChain mints, for a transitive property `p`, the two list nodes of
|
|
840
|
+
* the chain `(p p)`. They are named after `p`, so every run mints the same ones.
|
|
841
|
+
*/
|
|
842
|
+
const transitiveChain = bind("transitive-chain", [v.p], [v.l1, v.l2], (inputs, context) => {
|
|
843
|
+
const key = encodeURIComponent(termKey(context.term(context.find(inputs[0]))));
|
|
844
|
+
return [[context.id(dataFactory.namedNode(`${CHAIN_PREFIX}${key}/1`)), context.id(dataFactory.namedNode(`${CHAIN_PREFIX}${key}/2`))]];
|
|
845
|
+
});
|
|
846
|
+
const same = filter("same", [v.zi, v.zj], ([a, b], context) => context.find(a) === context.find(b));
|
|
847
|
+
/** The axiomatic triples: the old ruleset's vocabulary sections 0-2 and 6. */
|
|
848
|
+
const AXIOMS = [
|
|
849
|
+
[
|
|
850
|
+
OWL.sameAs,
|
|
851
|
+
RDF.type,
|
|
852
|
+
OWL.ReflexiveProperty
|
|
853
|
+
],
|
|
854
|
+
[
|
|
855
|
+
OWL.differentFrom,
|
|
856
|
+
RDF.type,
|
|
857
|
+
OWL.SymmetricProperty
|
|
858
|
+
],
|
|
859
|
+
[
|
|
860
|
+
OWL.NamedIndividual,
|
|
861
|
+
RDF.type,
|
|
862
|
+
OWL.Class
|
|
863
|
+
],
|
|
864
|
+
[
|
|
865
|
+
OWL.NamedIndividual,
|
|
866
|
+
RDFS.subClassOf,
|
|
867
|
+
OWL.Thing
|
|
868
|
+
],
|
|
869
|
+
...[
|
|
870
|
+
RDF.PlainLiteral,
|
|
871
|
+
RDF.XMLLiteral,
|
|
872
|
+
RDFS.Literal,
|
|
873
|
+
XSD.decimal,
|
|
874
|
+
XSD.integer,
|
|
875
|
+
XSD.nonNegativeInteger,
|
|
876
|
+
XSD.nonPositiveInteger,
|
|
877
|
+
XSD.positiveInteger,
|
|
878
|
+
XSD.negativeInteger,
|
|
879
|
+
XSD.long,
|
|
880
|
+
XSD.int,
|
|
881
|
+
XSD.short,
|
|
882
|
+
XSD.byte,
|
|
883
|
+
XSD.unsignedLong,
|
|
884
|
+
XSD.unsignedInt,
|
|
885
|
+
XSD.unsignedShort,
|
|
886
|
+
XSD.unsignedByte,
|
|
887
|
+
XSD.float,
|
|
888
|
+
XSD.double,
|
|
889
|
+
XSD.string,
|
|
890
|
+
XSD.normalizedString,
|
|
891
|
+
XSD.token,
|
|
892
|
+
XSD.language,
|
|
893
|
+
XSD.Name,
|
|
894
|
+
XSD.NCName,
|
|
895
|
+
XSD.NMTOKEN,
|
|
896
|
+
XSD.boolean,
|
|
897
|
+
XSD.hexBinary,
|
|
898
|
+
XSD.base64Binary,
|
|
899
|
+
XSD.anyURI,
|
|
900
|
+
XSD.dateTime,
|
|
901
|
+
XSD.dateTimeStamp
|
|
902
|
+
].map((datatype) => [
|
|
903
|
+
datatype,
|
|
904
|
+
RDF.type,
|
|
905
|
+
RDFS.Datatype
|
|
906
|
+
]),
|
|
907
|
+
...[
|
|
908
|
+
[XSD.integer, XSD.decimal],
|
|
909
|
+
[XSD.long, XSD.integer],
|
|
910
|
+
[XSD.int, XSD.long],
|
|
911
|
+
[XSD.short, XSD.int],
|
|
912
|
+
[XSD.byte, XSD.short],
|
|
913
|
+
[XSD.nonPositiveInteger, XSD.integer],
|
|
914
|
+
[XSD.negativeInteger, XSD.nonPositiveInteger],
|
|
915
|
+
[XSD.nonNegativeInteger, XSD.integer],
|
|
916
|
+
[XSD.positiveInteger, XSD.nonNegativeInteger],
|
|
917
|
+
[XSD.unsignedLong, XSD.nonNegativeInteger],
|
|
918
|
+
[XSD.unsignedInt, XSD.unsignedLong],
|
|
919
|
+
[XSD.unsignedShort, XSD.unsignedInt],
|
|
920
|
+
[XSD.unsignedByte, XSD.unsignedShort],
|
|
921
|
+
[XSD.dateTimeStamp, XSD.dateTime],
|
|
922
|
+
[XSD.normalizedString, XSD.string],
|
|
923
|
+
[XSD.token, XSD.normalizedString],
|
|
924
|
+
[XSD.language, XSD.token],
|
|
925
|
+
[XSD.Name, XSD.token],
|
|
926
|
+
[XSD.NCName, XSD.Name],
|
|
927
|
+
[XSD.NMTOKEN, XSD.token]
|
|
928
|
+
].map(([sub, sup]) => [
|
|
929
|
+
sub,
|
|
930
|
+
RDFS.subClassOf,
|
|
931
|
+
sup
|
|
932
|
+
]),
|
|
933
|
+
...[
|
|
934
|
+
RDFS.label,
|
|
935
|
+
RDFS.comment,
|
|
936
|
+
RDFS.seeAlso,
|
|
937
|
+
RDFS.isDefinedBy,
|
|
938
|
+
OWL.deprecated,
|
|
939
|
+
OWL.versionInfo,
|
|
940
|
+
OWL.priorVersion,
|
|
941
|
+
OWL.backwardCompatibleWith,
|
|
942
|
+
OWL.incompatibleWith
|
|
943
|
+
].map((property) => [
|
|
944
|
+
property,
|
|
945
|
+
RDF.type,
|
|
946
|
+
OWL.AnnotationProperty
|
|
947
|
+
]),
|
|
948
|
+
[
|
|
949
|
+
OWL.Thing,
|
|
950
|
+
RDF.type,
|
|
951
|
+
OWL.Class
|
|
952
|
+
],
|
|
953
|
+
[
|
|
954
|
+
OWL.Nothing,
|
|
955
|
+
RDF.type,
|
|
956
|
+
OWL.Class
|
|
957
|
+
]
|
|
958
|
+
];
|
|
959
|
+
/** listRules maintain the internal list relations. */
|
|
960
|
+
const listRules = [
|
|
961
|
+
...[
|
|
962
|
+
OWL.intersectionOf,
|
|
963
|
+
OWL.unionOf,
|
|
964
|
+
OWL.oneOf,
|
|
965
|
+
OWL.members,
|
|
966
|
+
OWL.distinctMembers,
|
|
967
|
+
OWL.hasKey,
|
|
968
|
+
OWL.propertyChainAxiom
|
|
969
|
+
].map((property) => rule(`list-root ${property.value}`, [t(v.x, property, v.l)], [atom("list", v.l)])),
|
|
970
|
+
rule("list-tail", [
|
|
971
|
+
atom("list", v.l),
|
|
972
|
+
t(v.l, RDF.rest, v.tail),
|
|
973
|
+
t(v.tail, RDF.first, v.head)
|
|
974
|
+
], [atom("list", v.tail)]),
|
|
975
|
+
rule("list-member-first", [atom("list", v.l), t(v.l, RDF.first, v.x)], [atom("member", v.l, v.x)]),
|
|
976
|
+
rule("list-member-rest", [
|
|
977
|
+
atom("list", v.l),
|
|
978
|
+
t(v.l, RDF.rest, v.tail),
|
|
979
|
+
atom("member", v.tail, v.x)
|
|
980
|
+
], [atom("member", v.l, v.x)]),
|
|
981
|
+
rule("list-pair-first", [
|
|
982
|
+
atom("list", v.l),
|
|
983
|
+
t(v.l, RDF.first, v.left),
|
|
984
|
+
t(v.l, RDF.rest, v.tail),
|
|
985
|
+
atom("member", v.tail, v.right)
|
|
986
|
+
], [atom("pair", v.l, v.left, v.right)]),
|
|
987
|
+
rule("list-pair-rest", [
|
|
988
|
+
atom("list", v.l),
|
|
989
|
+
t(v.l, RDF.rest, v.tail),
|
|
990
|
+
atom("pair", v.tail, v.left, v.right)
|
|
991
|
+
], [atom("pair", v.l, v.left, v.right)])
|
|
992
|
+
];
|
|
993
|
+
const equalityRules = [
|
|
994
|
+
rule("eq-diff1", [t(v.x, OWL.differentFrom, v.x)], [inconsistent("eq-diff1", v.x, v.x)]),
|
|
995
|
+
...[OWL.members, OWL.distinctMembers].map((property, index) => rule(`eq-diff${index + 2}`, [
|
|
996
|
+
t(v.ad, RDF.type, OWL.AllDifferent),
|
|
997
|
+
t(v.ad, property, v.l),
|
|
998
|
+
atom("pair", v.l, v.zi, v.zj),
|
|
999
|
+
same
|
|
1000
|
+
], [inconsistent(`eq-diff${index + 2}`, v.ad, v.zi, v.zj)])),
|
|
1001
|
+
rule("dt-diff-class", [t(v.r, OWL.sameAs, v.r), filter("different-values", [v.r], ([r], context) => someValuesDiffer(r, r, context, false))], [t(v.r, OWL.differentFrom, v.r)]),
|
|
1002
|
+
rule("dt-diff-class-string", [t(v.r, OWL.sameAs, v.r), filter("different-strings", [v.r], ([r], context) => someValuesDiffer(r, r, context, true))], [t(v.r, OWL.differentFrom, v.r)])
|
|
1003
|
+
];
|
|
1004
|
+
const propertyRules = [
|
|
1005
|
+
rule("prp-dom", [t(v.p, RDFS.domain, v.c), t(v.x, v.p, v.y)], [t(v.x, RDF.type, v.c)]),
|
|
1006
|
+
rule("prp-rng", [t(v.p, RDFS.range, v.c), t(v.x, v.p, v.y)], [t(v.y, RDF.type, v.c)]),
|
|
1007
|
+
rule("prp-fp", [
|
|
1008
|
+
t(v.p, RDF.type, OWL.FunctionalProperty),
|
|
1009
|
+
t(v.x, v.p, v.y1),
|
|
1010
|
+
t(v.x, v.p, v.y2)
|
|
1011
|
+
], [t(v.y1, OWL.sameAs, v.y2)]),
|
|
1012
|
+
rule("prp-ifp", [
|
|
1013
|
+
t(v.p, RDF.type, OWL.InverseFunctionalProperty),
|
|
1014
|
+
t(v.x1, v.p, v.y),
|
|
1015
|
+
t(v.x2, v.p, v.y)
|
|
1016
|
+
], [t(v.x1, OWL.sameAs, v.x2)]),
|
|
1017
|
+
rule("prp-irp", [t(v.p, RDF.type, OWL.IrreflexiveProperty), t(v.x, v.p, v.x)], [inconsistent("prp-irp", v.p, v.x)]),
|
|
1018
|
+
rule("prp-symp", [t(v.p, RDF.type, OWL.SymmetricProperty), t(v.x, v.p, v.y)], [t(v.y, v.p, v.x)]),
|
|
1019
|
+
rule("prp-asyp", [
|
|
1020
|
+
t(v.p, RDF.type, OWL.AsymmetricProperty),
|
|
1021
|
+
t(v.x, v.p, v.y),
|
|
1022
|
+
t(v.y, v.p, v.x)
|
|
1023
|
+
], [inconsistent("prp-asyp", v.p, v.x, v.y)]),
|
|
1024
|
+
rule("prp-trp", [
|
|
1025
|
+
t(v.p, RDF.type, OWL.TransitiveProperty),
|
|
1026
|
+
t(v.x, v.p, v.y),
|
|
1027
|
+
t(v.y, v.p, v.z)
|
|
1028
|
+
], [t(v.x, v.p, v.z)]),
|
|
1029
|
+
rule("prp-rp", [t(v.p, RDF.type, OWL.ReflexiveProperty), t(v.x, RDF.type, OWL.NamedIndividual)], [t(v.x, v.p, v.x)]),
|
|
1030
|
+
rule("prp-spo1", [t(v.p1, RDFS.subPropertyOf, v.p2), t(v.x, v.p1, v.y)], [t(v.x, v.p2, v.y)]),
|
|
1031
|
+
rule("chain-root", [t(v.x, OWL.propertyChainAxiom, v.l), filter("not-transitive-chain", [v.l], ([l], context) => {
|
|
1032
|
+
const term = context.term(context.find(l));
|
|
1033
|
+
return !(term.termType === "NamedNode" && term.value.startsWith("https://wazoo.dev/.well-known/genid/reasoner/chain/"));
|
|
1034
|
+
})], [atom("chainList", v.l)]),
|
|
1035
|
+
rule("chain-tail", [
|
|
1036
|
+
atom("chainList", v.l),
|
|
1037
|
+
t(v.l, RDF.rest, v.tail),
|
|
1038
|
+
t(v.tail, RDF.first, v.head)
|
|
1039
|
+
], [atom("chainList", v.tail)]),
|
|
1040
|
+
rule("chain-last", [
|
|
1041
|
+
atom("chainList", v.l),
|
|
1042
|
+
t(v.l, RDF.first, v.p),
|
|
1043
|
+
t(v.l, RDF.rest, RDF.nil),
|
|
1044
|
+
t(v.x, v.p, v.y)
|
|
1045
|
+
], [atom("chainPath", v.l, v.x, v.y)]),
|
|
1046
|
+
rule("chain-step", [
|
|
1047
|
+
atom("chainList", v.l),
|
|
1048
|
+
t(v.l, RDF.first, v.p),
|
|
1049
|
+
t(v.l, RDF.rest, v.tail),
|
|
1050
|
+
atom("chainPath", v.tail, v.z, v.y),
|
|
1051
|
+
t(v.x, v.p, v.z)
|
|
1052
|
+
], [atom("chainPath", v.l, v.x, v.y)]),
|
|
1053
|
+
rule("prp-spo2", [t(v.p, OWL.propertyChainAxiom, v.l), atom("chainPath", v.l, v.x, v.y)], [t(v.x, v.p, v.y)]),
|
|
1054
|
+
rule("prp-trp-chain", [t(v.p, RDF.type, OWL.TransitiveProperty), transitiveChain], [
|
|
1055
|
+
t(v.p, OWL.propertyChainAxiom, v.l1),
|
|
1056
|
+
t(v.l1, RDF.first, v.p),
|
|
1057
|
+
t(v.l1, RDF.rest, v.l2),
|
|
1058
|
+
t(v.l2, RDF.first, v.p),
|
|
1059
|
+
t(v.l2, RDF.rest, RDF.nil)
|
|
1060
|
+
]),
|
|
1061
|
+
rule("prp-spo2-transitive", [
|
|
1062
|
+
t(v.p, OWL.propertyChainAxiom, v.l),
|
|
1063
|
+
t(v.l, RDF.first, v.p),
|
|
1064
|
+
t(v.l, RDF.rest, v.tail),
|
|
1065
|
+
t(v.tail, RDF.first, v.p),
|
|
1066
|
+
t(v.tail, RDF.rest, RDF.nil)
|
|
1067
|
+
], [t(v.p, RDF.type, OWL.TransitiveProperty)]),
|
|
1068
|
+
rule("prp-eqp1", [t(v.p1, OWL.equivalentProperty, v.p2), t(v.x, v.p1, v.y)], [t(v.x, v.p2, v.y)]),
|
|
1069
|
+
rule("prp-eqp2", [t(v.p1, OWL.equivalentProperty, v.p2), t(v.x, v.p2, v.y)], [t(v.x, v.p1, v.y)]),
|
|
1070
|
+
rule("prp-pdw", [
|
|
1071
|
+
t(v.p1, OWL.propertyDisjointWith, v.p2),
|
|
1072
|
+
t(v.x, v.p1, v.y),
|
|
1073
|
+
t(v.x, v.p2, v.y)
|
|
1074
|
+
], [inconsistent("prp-pdw", v.p1, v.p2, v.x, v.y)]),
|
|
1075
|
+
...disjointPropertyDifferences("prp-pdw", [t(v.p1, OWL.propertyDisjointWith, v.p2)]),
|
|
1076
|
+
rule("prp-adp", [
|
|
1077
|
+
t(v.adp, RDF.type, OWL.AllDisjointProperties),
|
|
1078
|
+
t(v.adp, OWL.members, v.l),
|
|
1079
|
+
atom("pair", v.l, v.p1, v.p2),
|
|
1080
|
+
t(v.u, v.p1, v.w),
|
|
1081
|
+
t(v.u, v.p2, v.w)
|
|
1082
|
+
], [inconsistent("prp-adp", v.adp, v.p1, v.p2, v.u, v.w)]),
|
|
1083
|
+
...disjointPropertyDifferences("prp-adp", [
|
|
1084
|
+
t(v.adp, RDF.type, OWL.AllDisjointProperties),
|
|
1085
|
+
t(v.adp, OWL.members, v.l),
|
|
1086
|
+
atom("pair", v.l, v.p1, v.p2)
|
|
1087
|
+
]),
|
|
1088
|
+
rule("prp-inv1", [t(v.p1, OWL.inverseOf, v.p2), t(v.x, v.p1, v.y)], [t(v.y, v.p2, v.x)]),
|
|
1089
|
+
rule("prp-inv2", [t(v.p1, OWL.inverseOf, v.p2), t(v.x, v.p2, v.y)], [t(v.y, v.p1, v.x)]),
|
|
1090
|
+
rule("key-root", [t(v.c, OWL.hasKey, v.l)], [atom("keyContext", v.l, v.c)]),
|
|
1091
|
+
rule("key-tail", [
|
|
1092
|
+
atom("keyContext", v.l, v.c),
|
|
1093
|
+
t(v.l, RDF.rest, v.tail),
|
|
1094
|
+
t(v.tail, RDF.first, v.head)
|
|
1095
|
+
], [atom("keyContext", v.tail, v.c)]),
|
|
1096
|
+
rule("key-last", [
|
|
1097
|
+
atom("keyContext", v.l, v.c),
|
|
1098
|
+
t(v.l, RDF.first, v.p),
|
|
1099
|
+
t(v.l, RDF.rest, RDF.nil),
|
|
1100
|
+
t(v.x, v.p, v.z),
|
|
1101
|
+
t(v.x, RDF.type, v.c),
|
|
1102
|
+
t(v.y, v.p, v.z),
|
|
1103
|
+
t(v.y, RDF.type, v.c)
|
|
1104
|
+
], [atom("sameValues", v.x, v.y, v.l, v.c)]),
|
|
1105
|
+
rule("key-step", [
|
|
1106
|
+
atom("keyContext", v.l, v.c),
|
|
1107
|
+
t(v.l, RDF.first, v.p),
|
|
1108
|
+
t(v.l, RDF.rest, v.tail),
|
|
1109
|
+
atom("sameValues", v.x, v.y, v.tail, v.c),
|
|
1110
|
+
t(v.x, v.p, v.z),
|
|
1111
|
+
t(v.y, v.p, v.z)
|
|
1112
|
+
], [atom("sameValues", v.x, v.y, v.l, v.c)]),
|
|
1113
|
+
rule("prp-key", [t(v.c, OWL.hasKey, v.l), atom("sameValues", v.x, v.y, v.l, v.c)], [t(v.x, OWL.sameAs, v.y)]),
|
|
1114
|
+
rule("prp-fp-different", [
|
|
1115
|
+
t(v.p, RDF.type, OWL.FunctionalProperty),
|
|
1116
|
+
t(v.x1, v.p, v.y1),
|
|
1117
|
+
t(v.y1, OWL.differentFrom, v.y2),
|
|
1118
|
+
t(v.x2, v.p, v.y2)
|
|
1119
|
+
], [t(v.x1, OWL.differentFrom, v.x2)]),
|
|
1120
|
+
rule("prp-ifp-different", [
|
|
1121
|
+
t(v.p, RDF.type, OWL.InverseFunctionalProperty),
|
|
1122
|
+
t(v.x1, v.p, v.y1),
|
|
1123
|
+
t(v.x1, OWL.differentFrom, v.x2),
|
|
1124
|
+
t(v.x2, v.p, v.y2)
|
|
1125
|
+
], [t(v.y1, OWL.differentFrom, v.y2)]),
|
|
1126
|
+
rule("prp-npa1", [
|
|
1127
|
+
t(v.npa, OWL.sourceIndividual, v.i1),
|
|
1128
|
+
t(v.npa, OWL.assertionProperty, v.p),
|
|
1129
|
+
t(v.npa, OWL.targetIndividual, v.i2),
|
|
1130
|
+
t(v.i1, v.p, v.i2)
|
|
1131
|
+
], [inconsistent("prp-npa1", v.npa, v.i1, v.p, v.i2)]),
|
|
1132
|
+
rule("prp-npa2", [
|
|
1133
|
+
t(v.npa, OWL.sourceIndividual, v.i),
|
|
1134
|
+
t(v.npa, OWL.assertionProperty, v.p),
|
|
1135
|
+
t(v.npa, OWL.targetValue, v.lt),
|
|
1136
|
+
t(v.i, v.p, v.lt)
|
|
1137
|
+
], [inconsistent("prp-npa2", v.npa, v.i, v.p, v.lt)])
|
|
1138
|
+
];
|
|
1139
|
+
/**
|
|
1140
|
+
* disjointPropertyDifferences: if disjoint properties relate two subjects to
|
|
1141
|
+
* one value, the subjects differ; if they relate one subject to two values,
|
|
1142
|
+
* the values differ (both directions of each).
|
|
1143
|
+
*/
|
|
1144
|
+
function disjointPropertyDifferences(name, schema) {
|
|
1145
|
+
return [rule(`${name}-subjects`, [
|
|
1146
|
+
...schema,
|
|
1147
|
+
t(v.x1, v.p1, v.y),
|
|
1148
|
+
t(v.x2, v.p2, v.y)
|
|
1149
|
+
], [t(v.x1, OWL.differentFrom, v.x2), t(v.x2, OWL.differentFrom, v.x1)]), rule(`${name}-objects`, [
|
|
1150
|
+
...schema,
|
|
1151
|
+
t(v.x, v.p1, v.y1),
|
|
1152
|
+
t(v.x, v.p2, v.y2)
|
|
1153
|
+
], [t(v.y1, OWL.differentFrom, v.y2), t(v.y2, OWL.differentFrom, v.y1)])];
|
|
1154
|
+
}
|
|
1155
|
+
const classRules = [
|
|
1156
|
+
rule("cls-nothing2", [t(v.x, RDF.type, OWL.Nothing)], [inconsistent("cls-nothing2", v.x)]),
|
|
1157
|
+
rule("intersection-root", [t(v.c, OWL.intersectionOf, v.l)], [atom("intersectionList", v.l)]),
|
|
1158
|
+
rule("intersection-tail", [
|
|
1159
|
+
atom("intersectionList", v.l),
|
|
1160
|
+
t(v.l, RDF.rest, v.tail),
|
|
1161
|
+
t(v.tail, RDF.first, v.head)
|
|
1162
|
+
], [atom("intersectionList", v.tail)]),
|
|
1163
|
+
rule("intersection-last", [
|
|
1164
|
+
atom("intersectionList", v.l),
|
|
1165
|
+
t(v.l, RDF.first, v.c),
|
|
1166
|
+
t(v.l, RDF.rest, RDF.nil),
|
|
1167
|
+
t(v.y, RDF.type, v.c)
|
|
1168
|
+
], [atom("allTypes", v.y, v.l)]),
|
|
1169
|
+
rule("intersection-step", [
|
|
1170
|
+
atom("intersectionList", v.l),
|
|
1171
|
+
t(v.l, RDF.first, v.c),
|
|
1172
|
+
t(v.l, RDF.rest, v.tail),
|
|
1173
|
+
atom("allTypes", v.y, v.tail),
|
|
1174
|
+
t(v.y, RDF.type, v.c)
|
|
1175
|
+
], [atom("allTypes", v.y, v.l)]),
|
|
1176
|
+
rule("cls-int1", [t(v.c, OWL.intersectionOf, v.l), atom("allTypes", v.y, v.l)], [t(v.y, RDF.type, v.c)]),
|
|
1177
|
+
rule("cls-int2", [
|
|
1178
|
+
t(v.c, OWL.intersectionOf, v.l),
|
|
1179
|
+
t(v.y, RDF.type, v.c),
|
|
1180
|
+
atom("member", v.l, v.ci)
|
|
1181
|
+
], [t(v.y, RDF.type, v.ci)]),
|
|
1182
|
+
rule("cls-uni", [
|
|
1183
|
+
t(v.c, OWL.unionOf, v.l),
|
|
1184
|
+
atom("member", v.l, v.ci),
|
|
1185
|
+
t(v.y, RDF.type, v.ci)
|
|
1186
|
+
], [t(v.y, RDF.type, v.c)]),
|
|
1187
|
+
rule("cls-com", [
|
|
1188
|
+
t(v.c1, OWL.complementOf, v.c2),
|
|
1189
|
+
t(v.x, RDF.type, v.c1),
|
|
1190
|
+
t(v.x, RDF.type, v.c2)
|
|
1191
|
+
], [inconsistent("cls-com", v.c1, v.c2, v.x)]),
|
|
1192
|
+
rule("cls-svf1", [
|
|
1193
|
+
t(v.r, OWL.someValuesFrom, v.c),
|
|
1194
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1195
|
+
t(v.u, v.p, v.w),
|
|
1196
|
+
t(v.w, RDF.type, v.c)
|
|
1197
|
+
], [t(v.u, RDF.type, v.r)]),
|
|
1198
|
+
rule("cls-svf2", [
|
|
1199
|
+
t(v.r, OWL.someValuesFrom, OWL.Thing),
|
|
1200
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1201
|
+
t(v.u, v.p, v.w)
|
|
1202
|
+
], [t(v.u, RDF.type, v.r)]),
|
|
1203
|
+
rule("cls-avf", [
|
|
1204
|
+
t(v.r, OWL.allValuesFrom, v.c),
|
|
1205
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1206
|
+
t(v.u, RDF.type, v.r),
|
|
1207
|
+
t(v.u, v.p, v.w)
|
|
1208
|
+
], [t(v.w, RDF.type, v.c)]),
|
|
1209
|
+
rule("cls-hv1", [
|
|
1210
|
+
t(v.r, OWL.hasValue, v.value),
|
|
1211
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1212
|
+
t(v.u, RDF.type, v.r)
|
|
1213
|
+
], [t(v.u, v.p, v.value)]),
|
|
1214
|
+
rule("cls-hv2", [
|
|
1215
|
+
t(v.r, OWL.hasValue, v.value),
|
|
1216
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1217
|
+
t(v.u, v.p, v.value)
|
|
1218
|
+
], [t(v.u, RDF.type, v.r)]),
|
|
1219
|
+
rule("cls-maxc1", [
|
|
1220
|
+
t(v.r, OWL.maxCardinality, ZERO),
|
|
1221
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1222
|
+
t(v.u, RDF.type, v.r),
|
|
1223
|
+
t(v.u, v.p, v.y)
|
|
1224
|
+
], [inconsistent("cls-maxc1", v.r, v.u, v.p, v.y)]),
|
|
1225
|
+
rule("cls-maxc2", [
|
|
1226
|
+
t(v.r, OWL.maxCardinality, ONE),
|
|
1227
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1228
|
+
t(v.u, RDF.type, v.r),
|
|
1229
|
+
t(v.u, v.p, v.y1),
|
|
1230
|
+
t(v.u, v.p, v.y2)
|
|
1231
|
+
], [t(v.y1, OWL.sameAs, v.y2)]),
|
|
1232
|
+
rule("cls-maxqc1", [
|
|
1233
|
+
t(v.r, OWL.maxQualifiedCardinality, ZERO),
|
|
1234
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1235
|
+
t(v.r, OWL.onClass, v.c),
|
|
1236
|
+
t(v.u, RDF.type, v.r),
|
|
1237
|
+
t(v.u, v.p, v.y),
|
|
1238
|
+
t(v.y, RDF.type, v.c)
|
|
1239
|
+
], [inconsistent("cls-maxqc1", v.r, v.u, v.p, v.y, v.c)]),
|
|
1240
|
+
rule("cls-maxqc2", [
|
|
1241
|
+
t(v.r, OWL.maxQualifiedCardinality, ZERO),
|
|
1242
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1243
|
+
t(v.r, OWL.onClass, OWL.Thing),
|
|
1244
|
+
t(v.u, RDF.type, v.r),
|
|
1245
|
+
t(v.u, v.p, v.y)
|
|
1246
|
+
], [inconsistent("cls-maxqc2", v.r, v.u, v.p, v.y)]),
|
|
1247
|
+
rule("cls-maxqc3", [
|
|
1248
|
+
t(v.r, OWL.maxQualifiedCardinality, ONE),
|
|
1249
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1250
|
+
t(v.r, OWL.onClass, v.c),
|
|
1251
|
+
t(v.u, RDF.type, v.r),
|
|
1252
|
+
t(v.u, v.p, v.y1),
|
|
1253
|
+
t(v.y1, RDF.type, v.c),
|
|
1254
|
+
t(v.u, v.p, v.y2),
|
|
1255
|
+
t(v.y2, RDF.type, v.c)
|
|
1256
|
+
], [t(v.y1, OWL.sameAs, v.y2)]),
|
|
1257
|
+
rule("cls-maxqc4", [
|
|
1258
|
+
t(v.r, OWL.maxQualifiedCardinality, ONE),
|
|
1259
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1260
|
+
t(v.r, OWL.onClass, OWL.Thing),
|
|
1261
|
+
t(v.u, RDF.type, v.r),
|
|
1262
|
+
t(v.u, v.p, v.y1),
|
|
1263
|
+
t(v.u, v.p, v.y2)
|
|
1264
|
+
], [t(v.y1, OWL.sameAs, v.y2)]),
|
|
1265
|
+
rule("cls-maxqc-different", [
|
|
1266
|
+
t(v.r, OWL.maxQualifiedCardinality, ONE),
|
|
1267
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1268
|
+
t(v.r, OWL.onClass, v.c),
|
|
1269
|
+
t(v.u, RDF.type, v.r),
|
|
1270
|
+
t(v.u, v.p, v.y1),
|
|
1271
|
+
t(v.y1, RDF.type, v.c),
|
|
1272
|
+
t(v.y1, OWL.differentFrom, v.y2),
|
|
1273
|
+
t(v.u, v.p, v.y2),
|
|
1274
|
+
complementOf
|
|
1275
|
+
], [...complementHead(v.c), t(v.y2, RDF.type, v.notC)]),
|
|
1276
|
+
rule("cls-maxqd1", [
|
|
1277
|
+
t(v.r, OWL.maxQualifiedCardinality, ZERO),
|
|
1278
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1279
|
+
t(v.r, OWL.onDataRange, v.dt),
|
|
1280
|
+
t(v.u, RDF.type, v.r),
|
|
1281
|
+
t(v.u, v.p, v.y),
|
|
1282
|
+
t(v.y, RDF.type, v.dt)
|
|
1283
|
+
], [inconsistent("cls-maxqd1", v.r, v.u, v.p, v.y, v.dt)]),
|
|
1284
|
+
rule("cls-maxqd2", [
|
|
1285
|
+
t(v.r, OWL.maxQualifiedCardinality, ONE),
|
|
1286
|
+
t(v.r, OWL.onProperty, v.p),
|
|
1287
|
+
t(v.r, OWL.onDataRange, v.dt),
|
|
1288
|
+
t(v.u, RDF.type, v.r),
|
|
1289
|
+
t(v.u, v.p, v.y1),
|
|
1290
|
+
t(v.y1, RDF.type, v.dt),
|
|
1291
|
+
t(v.u, v.p, v.y2),
|
|
1292
|
+
t(v.y2, RDF.type, v.dt)
|
|
1293
|
+
], [t(v.y1, OWL.sameAs, v.y2)]),
|
|
1294
|
+
rule("cls-oo", [t(v.c, OWL.oneOf, v.l), atom("member", v.l, v.i)], [t(v.i, RDF.type, v.c)])
|
|
1295
|
+
];
|
|
1296
|
+
const classAxiomRules = [
|
|
1297
|
+
rule("cax-sco", [t(v.c1, RDFS.subClassOf, v.c2), t(v.x, RDF.type, v.c1)], [t(v.x, RDF.type, v.c2)]),
|
|
1298
|
+
rule("cax-eqc1", [t(v.c1, OWL.equivalentClass, v.c2), t(v.x, RDF.type, v.c1)], [t(v.x, RDF.type, v.c2)]),
|
|
1299
|
+
rule("cax-eqc2", [t(v.c1, OWL.equivalentClass, v.c2), t(v.x, RDF.type, v.c2)], [t(v.x, RDF.type, v.c1)]),
|
|
1300
|
+
rule("cax-dw", [
|
|
1301
|
+
t(v.c1, OWL.disjointWith, v.c2),
|
|
1302
|
+
t(v.x, RDF.type, v.c1),
|
|
1303
|
+
t(v.x, RDF.type, v.c2)
|
|
1304
|
+
], [inconsistent("cax-dw", v.c1, v.c2, v.x)]),
|
|
1305
|
+
rule("cax-dw-complement2", [t(v.c1, OWL.disjointWith, v.c), complementOf], [...complementHead(v.c), t(v.c1, RDFS.subClassOf, v.notC)]),
|
|
1306
|
+
rule("cax-dw-complement1", [t(v.c, OWL.disjointWith, v.c2), complementOf], [...complementHead(v.c), t(v.c2, RDFS.subClassOf, v.notC)]),
|
|
1307
|
+
rule("cax-adc", [
|
|
1308
|
+
t(v.adc, RDF.type, OWL.AllDisjointClasses),
|
|
1309
|
+
t(v.adc, OWL.members, v.l),
|
|
1310
|
+
atom("pair", v.l, v.c1, v.c2),
|
|
1311
|
+
t(v.z, RDF.type, v.c1),
|
|
1312
|
+
t(v.z, RDF.type, v.c2)
|
|
1313
|
+
], [inconsistent("cax-adc", v.adc, v.c1, v.c2, v.z)]),
|
|
1314
|
+
rule("cax-adc-disjoint", [
|
|
1315
|
+
t(v.adc, RDF.type, OWL.AllDisjointClasses),
|
|
1316
|
+
t(v.adc, OWL.members, v.l),
|
|
1317
|
+
atom("pair", v.l, v.c1, v.c2)
|
|
1318
|
+
], [t(v.c1, OWL.disjointWith, v.c2), t(v.c2, OWL.disjointWith, v.c1)])
|
|
1319
|
+
];
|
|
1320
|
+
const datatypeRules = [
|
|
1321
|
+
rule("dt-literal", [t(v.s, v.p, v.o), bind("literal", [v.o], [v.lt], function* ([o], context) {
|
|
1322
|
+
for (const [member, literal] of literalsOf(o, context)) if (!isPlainString(literal)) yield [member];
|
|
1323
|
+
})], [atom("literal", v.lt)]),
|
|
1324
|
+
rule("dt-eq-value", [atom("literal", v.lt), bind("value-key", [v.lt], [v.m, v.key], function* (inputs, context) {
|
|
1325
|
+
for (const [member, literal] of literalsOf(inputs[0], context)) {
|
|
1326
|
+
const key = isPlainString(literal) ? null : valueKey(literal);
|
|
1327
|
+
if (key === null) continue;
|
|
1328
|
+
yield [member, context.id(dataFactory.namedNode(VALUE_PREFIX + key))];
|
|
1329
|
+
}
|
|
1330
|
+
})], [atom("literalValue", v.key, v.m)]),
|
|
1331
|
+
rule("dt-eq", [
|
|
1332
|
+
atom("literalValue", v.key, v.a),
|
|
1333
|
+
atom("literalValue", v.key, v.b),
|
|
1334
|
+
filter("distinct-classes", [v.a, v.b], ([a, b], context) => context.find(a) !== context.find(b))
|
|
1335
|
+
], [t(v.a, OWL.sameAs, v.b)]),
|
|
1336
|
+
rule("dt-string", [t(v.s, v.p, v.o), stringMember(XSD.string)], [t(v.lt, RDF.type, XSD.string), t(v.lt, RDF.type, RDFS.Literal)]),
|
|
1337
|
+
rule("dt-langstring", [t(v.s, v.p, v.o), stringMember(RDF.langString)], [t(v.lt, RDF.type, RDF.langString), t(v.lt, RDF.type, RDFS.Literal)]),
|
|
1338
|
+
rule("dt-type2", [
|
|
1339
|
+
atom("literal", v.lt),
|
|
1340
|
+
literalMember,
|
|
1341
|
+
bind("own-datatype", [v.m], [v.dt], ([m], context) => {
|
|
1342
|
+
const literal = context.term(m);
|
|
1343
|
+
const datatype = literalDatatype(literal);
|
|
1344
|
+
return isInstanceOf(literal, datatype) ? [[context.id(dataFactory.namedNode(datatype))]] : [];
|
|
1345
|
+
})
|
|
1346
|
+
], [t(v.m, RDF.type, v.dt)]),
|
|
1347
|
+
rule("dt-type2-declared", [
|
|
1348
|
+
atom("literal", v.lt),
|
|
1349
|
+
t(v.dt, RDF.type, RDFS.Datatype),
|
|
1350
|
+
literalMember,
|
|
1351
|
+
filter("instance-of", [v.m, v.dt], ([m, dt], context) => {
|
|
1352
|
+
const datatype = context.term(dt);
|
|
1353
|
+
return datatype.termType === "NamedNode" && isInstanceOf(context.term(m), datatype.value);
|
|
1354
|
+
})
|
|
1355
|
+
], [t(v.m, RDF.type, v.dt)]),
|
|
1356
|
+
rule("dt-literal-type", [atom("literal", v.lt), literalMember], [t(v.m, RDF.type, RDFS.Literal)]),
|
|
1357
|
+
rule("dt-not-type", [
|
|
1358
|
+
atom("literal", v.lt),
|
|
1359
|
+
t(v.lt, RDF.type, v.dt),
|
|
1360
|
+
literalMember,
|
|
1361
|
+
filter("invalid-for", [v.m, v.dt], ([m, dt], context) => {
|
|
1362
|
+
const datatype = context.term(dt);
|
|
1363
|
+
return datatype.termType === "NamedNode" && isInvalidFor(context.term(m), datatype.value);
|
|
1364
|
+
})
|
|
1365
|
+
], [inconsistent("dt-not-type", v.m, v.dt)]),
|
|
1366
|
+
rule("dt-not-type-string", [
|
|
1367
|
+
t(v.lt, RDF.type, v.dt),
|
|
1368
|
+
bind("string-member", [v.lt], [v.m], function* (inputs, context) {
|
|
1369
|
+
for (const [member, literal] of literalsOf(inputs[0], context)) if (isPlainString(literal)) yield [member];
|
|
1370
|
+
}),
|
|
1371
|
+
filter("invalid-for", [v.m, v.dt], ([m, dt], context) => {
|
|
1372
|
+
const datatype = context.term(dt);
|
|
1373
|
+
return datatype.termType === "NamedNode" && isInvalidFor(context.term(m), datatype.value);
|
|
1374
|
+
})
|
|
1375
|
+
], [inconsistent("dt-not-type", v.m, v.dt)])
|
|
1376
|
+
];
|
|
1377
|
+
const schemaRules = [
|
|
1378
|
+
rule("scm-cls", [t(v.c, RDF.type, OWL.Class)], [
|
|
1379
|
+
t(v.c, RDFS.subClassOf, v.c),
|
|
1380
|
+
t(v.c, OWL.equivalentClass, v.c),
|
|
1381
|
+
t(v.c, RDFS.subClassOf, OWL.Thing),
|
|
1382
|
+
t(OWL.Nothing, RDFS.subClassOf, v.c)
|
|
1383
|
+
]),
|
|
1384
|
+
rule("scm-sco", [t(v.c1, RDFS.subClassOf, v.c2), t(v.c2, RDFS.subClassOf, v.c3)], [t(v.c1, RDFS.subClassOf, v.c3)]),
|
|
1385
|
+
rule("scm-eqc1", [t(v.c1, OWL.equivalentClass, v.c2)], [t(v.c1, RDFS.subClassOf, v.c2), t(v.c2, RDFS.subClassOf, v.c1)]),
|
|
1386
|
+
rule("scm-eqc2", [t(v.c1, RDFS.subClassOf, v.c2), t(v.c2, RDFS.subClassOf, v.c1)], [t(v.c1, OWL.equivalentClass, v.c2)]),
|
|
1387
|
+
...[OWL.ObjectProperty, OWL.DatatypeProperty].map((kind) => rule(kind === OWL.ObjectProperty ? "scm-op" : "scm-dp", [t(v.p, RDF.type, kind)], [t(v.p, RDFS.subPropertyOf, v.p), t(v.p, OWL.equivalentProperty, v.p)])),
|
|
1388
|
+
rule("scm-spo", [t(v.p1, RDFS.subPropertyOf, v.p2), t(v.p2, RDFS.subPropertyOf, v.p3)], [t(v.p1, RDFS.subPropertyOf, v.p3)]),
|
|
1389
|
+
rule("scm-eqp1", [t(v.p1, OWL.equivalentProperty, v.p2)], [t(v.p1, RDFS.subPropertyOf, v.p2), t(v.p2, RDFS.subPropertyOf, v.p1)]),
|
|
1390
|
+
rule("scm-eqp2", [t(v.p1, RDFS.subPropertyOf, v.p2), t(v.p2, RDFS.subPropertyOf, v.p1)], [t(v.p1, OWL.equivalentProperty, v.p2)]),
|
|
1391
|
+
rule("scm-dom1", [t(v.p, RDFS.domain, v.c1), t(v.c1, RDFS.subClassOf, v.c2)], [t(v.p, RDFS.domain, v.c2)]),
|
|
1392
|
+
rule("scm-dom2", [t(v.p2, RDFS.domain, v.c), t(v.p1, RDFS.subPropertyOf, v.p2)], [t(v.p1, RDFS.domain, v.c)]),
|
|
1393
|
+
rule("scm-rng1", [t(v.p, RDFS.range, v.c1), t(v.c1, RDFS.subClassOf, v.c2)], [t(v.p, RDFS.range, v.c2)]),
|
|
1394
|
+
rule("scm-rng2", [t(v.p2, RDFS.range, v.c), t(v.p1, RDFS.subPropertyOf, v.p2)], [t(v.p1, RDFS.range, v.c)]),
|
|
1395
|
+
rule("scm-rng-short-unsignedInt", [t(v.p, RDFS.range, XSD.short), t(v.p, RDFS.range, XSD.unsignedInt)], [t(v.p, RDFS.range, XSD.unsignedShort)]),
|
|
1396
|
+
rule("scm-rng-sign", [t(v.p, RDFS.range, XSD.nonNegativeInteger), t(v.p, RDFS.range, XSD.nonPositiveInteger)], [t(v.p, RDFS.range, XSD.short)]),
|
|
1397
|
+
rule("scm-hv", [
|
|
1398
|
+
t(v.c1, OWL.hasValue, v.i),
|
|
1399
|
+
t(v.c1, OWL.onProperty, v.p1),
|
|
1400
|
+
t(v.c2, OWL.hasValue, v.i),
|
|
1401
|
+
t(v.c2, OWL.onProperty, v.p2),
|
|
1402
|
+
t(v.p1, RDFS.subPropertyOf, v.p2)
|
|
1403
|
+
], [t(v.c1, RDFS.subClassOf, v.c2)]),
|
|
1404
|
+
rule("scm-svf1", [
|
|
1405
|
+
t(v.c1, OWL.someValuesFrom, v.y1),
|
|
1406
|
+
t(v.c1, OWL.onProperty, v.p),
|
|
1407
|
+
t(v.c2, OWL.someValuesFrom, v.y2),
|
|
1408
|
+
t(v.c2, OWL.onProperty, v.p),
|
|
1409
|
+
t(v.y1, RDFS.subClassOf, v.y2)
|
|
1410
|
+
], [t(v.c1, RDFS.subClassOf, v.c2)]),
|
|
1411
|
+
rule("scm-svf2", [
|
|
1412
|
+
t(v.c1, OWL.someValuesFrom, v.y),
|
|
1413
|
+
t(v.c1, OWL.onProperty, v.p1),
|
|
1414
|
+
t(v.c2, OWL.someValuesFrom, v.y),
|
|
1415
|
+
t(v.c2, OWL.onProperty, v.p2),
|
|
1416
|
+
t(v.p1, RDFS.subPropertyOf, v.p2)
|
|
1417
|
+
], [t(v.c1, RDFS.subClassOf, v.c2)]),
|
|
1418
|
+
rule("scm-avf1", [
|
|
1419
|
+
t(v.c1, OWL.allValuesFrom, v.y1),
|
|
1420
|
+
t(v.c1, OWL.onProperty, v.p),
|
|
1421
|
+
t(v.c2, OWL.allValuesFrom, v.y2),
|
|
1422
|
+
t(v.c2, OWL.onProperty, v.p),
|
|
1423
|
+
t(v.y1, RDFS.subClassOf, v.y2)
|
|
1424
|
+
], [t(v.c1, RDFS.subClassOf, v.c2)]),
|
|
1425
|
+
rule("scm-avf2", [
|
|
1426
|
+
t(v.c1, OWL.allValuesFrom, v.y),
|
|
1427
|
+
t(v.c1, OWL.onProperty, v.p1),
|
|
1428
|
+
t(v.c2, OWL.allValuesFrom, v.y),
|
|
1429
|
+
t(v.c2, OWL.onProperty, v.p2),
|
|
1430
|
+
t(v.p1, RDFS.subPropertyOf, v.p2)
|
|
1431
|
+
], [t(v.c2, RDFS.subClassOf, v.c1)]),
|
|
1432
|
+
rule("scm-int", [t(v.c, OWL.intersectionOf, v.l), atom("member", v.l, v.ci)], [t(v.c, RDFS.subClassOf, v.ci)]),
|
|
1433
|
+
rule("scm-uni", [t(v.c, OWL.unionOf, v.l), atom("member", v.l, v.ci)], [t(v.ci, RDFS.subClassOf, v.c)])
|
|
1434
|
+
];
|
|
1435
|
+
/** OWL2RL_RULES is the full ruleset, in evaluation order. */
|
|
1436
|
+
const OWL2RL_RULES = [
|
|
1437
|
+
...listRules,
|
|
1438
|
+
...equalityRules,
|
|
1439
|
+
...propertyRules,
|
|
1440
|
+
...classRules,
|
|
1441
|
+
...classAxiomRules,
|
|
1442
|
+
...datatypeRules,
|
|
1443
|
+
...schemaRules
|
|
1444
|
+
];
|
|
1445
|
+
// src/custom-rule.ts
|
|
1446
|
+
const RESERVED = new Set(OWL2RL_RULES.map((rule) => rule.name));
|
|
1447
|
+
function isArgument(value) {
|
|
1448
|
+
return value instanceof Variable || typeof value === "object" && value !== null && typeof value.termType === "string" && value.termType !== "Quad" && value.termType !== "DefaultGraph";
|
|
1449
|
+
}
|
|
1450
|
+
/**
|
|
1451
|
+
* compileCustomRule validates `rule` and lowers it onto the engine's internal
|
|
1452
|
+
* rule type. It throws an `Error` naming the rule and the problem.
|
|
1453
|
+
*/
|
|
1454
|
+
function compileCustomRule(rule, taken) {
|
|
1455
|
+
const fail = (message) => {
|
|
1456
|
+
throw new Error(`Invalid rule ${JSON.stringify(rule.name)}: ${message}`);
|
|
1457
|
+
};
|
|
1458
|
+
if (typeof rule.name !== "string" || rule.name === "") fail("name must be a non-empty string");
|
|
1459
|
+
if (RESERVED.has(rule.name)) fail("name is an OWL 2 RL rule name");
|
|
1460
|
+
if (taken.has(rule.name)) fail("name is already registered");
|
|
1461
|
+
if (!Array.isArray(rule.body) || rule.body.length === 0) fail("body needs at least one pattern");
|
|
1462
|
+
const head = rule.head ?? [];
|
|
1463
|
+
const inconsistent = rule.inconsistent;
|
|
1464
|
+
if (head.length === 0 && inconsistent === void 0) fail("needs a head pattern or an inconsistent list");
|
|
1465
|
+
const check = (pattern, where) => {
|
|
1466
|
+
if (!Array.isArray(pattern) || pattern.length !== 3) fail(`${where} pattern must be [subject, predicate, object]`);
|
|
1467
|
+
for (const arg of pattern) if (!isArgument(arg)) fail(`${where} pattern has an invalid term`);
|
|
1468
|
+
};
|
|
1469
|
+
const bound = /* @__PURE__ */ new Set();
|
|
1470
|
+
for (const pattern of rule.body) {
|
|
1471
|
+
check(pattern, "body");
|
|
1472
|
+
for (const arg of pattern) if (arg instanceof Variable) bound.add(arg);
|
|
1473
|
+
}
|
|
1474
|
+
const needBound = (arg) => {
|
|
1475
|
+
if (arg instanceof Variable && !bound.has(arg)) fail(`unbound head variable ?${arg.name} (it must occur in the body)`);
|
|
1476
|
+
};
|
|
1477
|
+
for (const pattern of head) {
|
|
1478
|
+
check(pattern, "head");
|
|
1479
|
+
pattern.forEach(needBound);
|
|
1480
|
+
const predicate = pattern[1];
|
|
1481
|
+
if (!(predicate instanceof Variable) && predicate.termType !== "NamedNode") fail("a constant head predicate must be a named node");
|
|
1482
|
+
}
|
|
1483
|
+
if (inconsistent !== void 0) {
|
|
1484
|
+
if (!Array.isArray(inconsistent) || inconsistent.length > 5) fail("inconsistent takes at most five terms");
|
|
1485
|
+
for (const arg of inconsistent) {
|
|
1486
|
+
if (!isArgument(arg)) fail("inconsistent has an invalid term");
|
|
1487
|
+
needBound(arg);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
const heads = head.map(([s, p, o]) => atom(TRIPLE, s, p, o));
|
|
1491
|
+
if (inconsistent !== void 0) {
|
|
1492
|
+
const padded = [...inconsistent];
|
|
1493
|
+
while (padded.length < 5) padded.push(NONE);
|
|
1494
|
+
heads.push(atom(INCONSISTENCY, dataFactory.literal(rule.name), ...padded));
|
|
1495
|
+
}
|
|
1496
|
+
return {
|
|
1497
|
+
name: rule.name,
|
|
1498
|
+
body: rule.body.map(([s, p, o]) => atom(TRIPLE, s, p, o)),
|
|
1499
|
+
head: heads
|
|
1500
|
+
};
|
|
1501
|
+
}
|
|
1502
|
+
// src/generalized-quad.ts
|
|
1503
|
+
/**
|
|
1504
|
+
* generalizedQuad builds a quad without validating the term positions.
|
|
1505
|
+
*
|
|
1506
|
+
* OWL 2 RL entails generalized triples (a literal subject, for example, from
|
|
1507
|
+
* `eq-sym` or a custom rule), and the `"complete"` closure view lists them.
|
|
1508
|
+
* `dataFactory.quad` in `@wazootech/sparql-engine` 0.5 throws on such a position,
|
|
1509
|
+
* so the closure builds its quads here and the `"application"` view filters
|
|
1510
|
+
* them out.
|
|
1511
|
+
*/
|
|
1512
|
+
function generalizedQuad(subject, predicate, object, graph = dataFactory.defaultGraph()) {
|
|
1513
|
+
return {
|
|
1514
|
+
termType: "Quad",
|
|
1515
|
+
value: "",
|
|
1516
|
+
subject,
|
|
1517
|
+
predicate,
|
|
1518
|
+
object,
|
|
1519
|
+
graph,
|
|
1520
|
+
equals: (other) => other?.termType === "Quad" && subject.equals(other.subject) && predicate.equals(other.predicate) && object.equals(other.object) && graph.equals(other.graph)
|
|
1521
|
+
};
|
|
1522
|
+
}
|
|
1523
|
+
// src/equality.ts
|
|
1524
|
+
/**
|
|
1525
|
+
* Equality keeps `owl:sameAs` as a union-find over term ids instead of as
|
|
1526
|
+
* copied triples, the way RDFox does: every stored fact names only class
|
|
1527
|
+
* representatives, and the materialized closure is the product of each
|
|
1528
|
+
* fact's term classes.
|
|
1529
|
+
*
|
|
1530
|
+
* Union is by size and finds do not compress paths, so the tree depth stays
|
|
1531
|
+
* logarithmic and every link keeps the clock tick it was made at. That makes
|
|
1532
|
+
* the state at any earlier tick recoverable ({@link Equality.findAt}), which
|
|
1533
|
+
* is how an incremental run tells a triple it newly entails from one that was
|
|
1534
|
+
* already in the closure before a merge.
|
|
1535
|
+
*/
|
|
1536
|
+
var Equality = class {
|
|
1537
|
+
#parent = [];
|
|
1538
|
+
#size = [];
|
|
1539
|
+
#linkedAt = [];
|
|
1540
|
+
#members = /* @__PURE__ */ new Map();
|
|
1541
|
+
#grow(id) {
|
|
1542
|
+
for (let next = this.#parent.length; next <= id; next++) {
|
|
1543
|
+
this.#parent.push(next);
|
|
1544
|
+
this.#size.push(1);
|
|
1545
|
+
this.#linkedAt.push(1e400);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
/** find returns the representative of the class holding `id`. */
|
|
1549
|
+
find(id) {
|
|
1550
|
+
const parent = this.#parent;
|
|
1551
|
+
if (id >= parent.length) return id;
|
|
1552
|
+
while (parent[id] !== id) id = parent[id];
|
|
1553
|
+
return id;
|
|
1554
|
+
}
|
|
1555
|
+
/** findAt returns the representative `id` had before clock tick `tick`. */
|
|
1556
|
+
findAt(id, tick) {
|
|
1557
|
+
const parent = this.#parent;
|
|
1558
|
+
if (id >= parent.length) return id;
|
|
1559
|
+
while (parent[id] !== id && this.#linkedAt[id] < tick) id = parent[id];
|
|
1560
|
+
return id;
|
|
1561
|
+
}
|
|
1562
|
+
/**
|
|
1563
|
+
* union merges the classes of `a` and `b` at clock tick `tick`, and returns
|
|
1564
|
+
* the representative that was absorbed, or `undefined` if they were already
|
|
1565
|
+
* one class.
|
|
1566
|
+
*/
|
|
1567
|
+
union(a, b, tick) {
|
|
1568
|
+
this.#grow(Math.max(a, b));
|
|
1569
|
+
let root = this.find(a);
|
|
1570
|
+
let absorbed = this.find(b);
|
|
1571
|
+
if (root === absorbed) return void 0;
|
|
1572
|
+
if (this.#size[absorbed] > this.#size[root] || this.#size[absorbed] === this.#size[root] && absorbed < root) [root, absorbed] = [absorbed, root];
|
|
1573
|
+
this.#parent[absorbed] = root;
|
|
1574
|
+
this.#linkedAt[absorbed] = tick;
|
|
1575
|
+
this.#size[root] = this.#size[root] + this.#size[absorbed];
|
|
1576
|
+
const kept = this.#members.get(root) ?? [root];
|
|
1577
|
+
const moved = this.#members.get(absorbed) ?? [absorbed];
|
|
1578
|
+
for (const member of moved) kept.push(member);
|
|
1579
|
+
this.#members.set(root, kept);
|
|
1580
|
+
this.#members.delete(absorbed);
|
|
1581
|
+
return absorbed;
|
|
1582
|
+
}
|
|
1583
|
+
/** members returns every id in the class whose representative is `root`. */
|
|
1584
|
+
members(root) {
|
|
1585
|
+
return this.#members.get(root) ?? [root];
|
|
1586
|
+
}
|
|
1587
|
+
/** isTrivial is `true` when `root` is alone in its class. */
|
|
1588
|
+
isTrivial(root) {
|
|
1589
|
+
return !this.#members.has(root);
|
|
1590
|
+
}
|
|
1591
|
+
/** rootsChangedSince returns the roots of every class merged at or after `tick`. */
|
|
1592
|
+
rootsChangedSince(tick) {
|
|
1593
|
+
const roots = /* @__PURE__ */ new Set();
|
|
1594
|
+
for (let id = 0; id < this.#parent.length; id++) if (this.#linkedAt[id] >= tick && this.#linkedAt[id] !== 1e400) roots.add(this.find(id));
|
|
1595
|
+
return roots;
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* reset makes every id in `ids` a class of its own again. `ids` must be
|
|
1599
|
+
* closed under the classes it touches, or the survivors keep dangling links.
|
|
1600
|
+
*/
|
|
1601
|
+
reset(ids) {
|
|
1602
|
+
for (const id of ids) {
|
|
1603
|
+
if (id >= this.#parent.length) continue;
|
|
1604
|
+
this.#parent[id] = id;
|
|
1605
|
+
this.#size[id] = 1;
|
|
1606
|
+
this.#linkedAt[id] = 1e400;
|
|
1607
|
+
this.#members.delete(id);
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
/** classes yields the representative of every class with two or more members. */
|
|
1611
|
+
classes() {
|
|
1612
|
+
return this.#members.keys();
|
|
1613
|
+
}
|
|
1614
|
+
};
|
|
1615
|
+
// src/relation.ts
|
|
1616
|
+
/** factKey renders the dedup key of a tuple. */
|
|
1617
|
+
function factKey(args) {
|
|
1618
|
+
return args.join(",");
|
|
1619
|
+
}
|
|
1620
|
+
const NO_FACTS = [];
|
|
1621
|
+
/**
|
|
1622
|
+
* Relation stores the facts of one predicate of fixed arity, with a hash index
|
|
1623
|
+
* per combination of bound columns. Single-column indexes exist from the
|
|
1624
|
+
* start, because a merge looks up every fact that mentions a renamed term;
|
|
1625
|
+
* wider indexes are built on first use and maintained from then on.
|
|
1626
|
+
*
|
|
1627
|
+
* Retired facts stay in {@link Relation.get} (so an incremental run can ask
|
|
1628
|
+
* what was live at an earlier tick) and are skipped lazily by lookups.
|
|
1629
|
+
*/
|
|
1630
|
+
var Relation = class {
|
|
1631
|
+
name;
|
|
1632
|
+
arity;
|
|
1633
|
+
#facts = /* @__PURE__ */ new Map();
|
|
1634
|
+
#indexes = /* @__PURE__ */ new Map();
|
|
1635
|
+
#live = 0;
|
|
1636
|
+
constructor(name, arity) {
|
|
1637
|
+
this.name = name;
|
|
1638
|
+
this.arity = arity;
|
|
1639
|
+
for (let column = 0; column < arity; column++) this.#indexes.set(1 << column, /* @__PURE__ */ new Map());
|
|
1640
|
+
}
|
|
1641
|
+
/** size is the number of live facts. */
|
|
1642
|
+
get size() {
|
|
1643
|
+
return this.#live;
|
|
1644
|
+
}
|
|
1645
|
+
/** get returns the fact stored under `key`, live or retired. */
|
|
1646
|
+
get(key) {
|
|
1647
|
+
return this.#facts.get(key);
|
|
1648
|
+
}
|
|
1649
|
+
/** has is `true` when a live fact has exactly these args. */
|
|
1650
|
+
has(args) {
|
|
1651
|
+
const fact = this.#facts.get(factKey(args));
|
|
1652
|
+
return fact !== void 0 && fact.diedAt === 1e400;
|
|
1653
|
+
}
|
|
1654
|
+
/**
|
|
1655
|
+
* insert stores `args` at tick `tick` and returns the new fact, or
|
|
1656
|
+
* `undefined` when a live fact already has these args.
|
|
1657
|
+
*/
|
|
1658
|
+
insert(args, tick) {
|
|
1659
|
+
const key = factKey(args);
|
|
1660
|
+
const existing = this.#facts.get(key);
|
|
1661
|
+
if (existing !== void 0 && existing.diedAt === 1e400) return;
|
|
1662
|
+
const fact = {
|
|
1663
|
+
args,
|
|
1664
|
+
key,
|
|
1665
|
+
bornAt: tick,
|
|
1666
|
+
diedAt: 1e400
|
|
1667
|
+
};
|
|
1668
|
+
this.#facts.set(key, fact);
|
|
1669
|
+
this.#live++;
|
|
1670
|
+
for (const [mask, index] of this.#indexes) {
|
|
1671
|
+
const indexKey = maskKey(mask, args);
|
|
1672
|
+
const bucket = index.get(indexKey);
|
|
1673
|
+
if (bucket === void 0) index.set(indexKey, [fact]);
|
|
1674
|
+
else bucket.push(fact);
|
|
1675
|
+
}
|
|
1676
|
+
return fact;
|
|
1677
|
+
}
|
|
1678
|
+
/** retire marks a live fact dead at tick `tick`. */
|
|
1679
|
+
retire(fact, tick) {
|
|
1680
|
+
if (fact.diedAt !== 1e400) return;
|
|
1681
|
+
fact.diedAt = tick;
|
|
1682
|
+
this.#live--;
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* lookup returns the facts whose columns in `mask` equal `values` (one value
|
|
1686
|
+
* per set bit, lowest column first). Retired facts may be included; callers
|
|
1687
|
+
* skip any fact whose `diedAt` is finite.
|
|
1688
|
+
*/
|
|
1689
|
+
lookup(mask, values) {
|
|
1690
|
+
if (mask === 0) return [...this.#facts.values()];
|
|
1691
|
+
if (values.length > 2) {
|
|
1692
|
+
let narrowed = 0;
|
|
1693
|
+
for (let column = 0, kept = 0; kept < 2; column++) if ((mask & 1 << column) !== 0) {
|
|
1694
|
+
narrowed |= 1 << column;
|
|
1695
|
+
kept++;
|
|
1696
|
+
}
|
|
1697
|
+
mask = narrowed;
|
|
1698
|
+
values = values.slice(0, 2);
|
|
1699
|
+
}
|
|
1700
|
+
let index = this.#indexes.get(mask);
|
|
1701
|
+
if (index === void 0) index = this.#buildIndex(mask);
|
|
1702
|
+
return index.get(valuesKey(values)) ?? NO_FACTS;
|
|
1703
|
+
}
|
|
1704
|
+
/** column returns the facts whose column `column` equals `id`. */
|
|
1705
|
+
column(column, id) {
|
|
1706
|
+
return this.#indexes.get(1 << column).get(id) ?? NO_FACTS;
|
|
1707
|
+
}
|
|
1708
|
+
/**
|
|
1709
|
+
* forget drops the single-column buckets of `id` once a merge has renamed
|
|
1710
|
+
* every fact in them, so the dead facts do not pin memory.
|
|
1711
|
+
*/
|
|
1712
|
+
forget(id) {
|
|
1713
|
+
for (let column = 0; column < this.arity; column++) this.#indexes.get(1 << column).delete(id);
|
|
1714
|
+
}
|
|
1715
|
+
/** facts yields every live fact. */
|
|
1716
|
+
*facts() {
|
|
1717
|
+
for (const fact of this.#facts.values()) if (fact.diedAt === 1e400) yield fact;
|
|
1718
|
+
}
|
|
1719
|
+
#buildIndex(mask) {
|
|
1720
|
+
const index = /* @__PURE__ */ new Map();
|
|
1721
|
+
for (const fact of this.#facts.values()) {
|
|
1722
|
+
if (fact.diedAt !== 1e400) continue;
|
|
1723
|
+
const indexKey = maskKey(mask, fact.args);
|
|
1724
|
+
const bucket = index.get(indexKey);
|
|
1725
|
+
if (bucket === void 0) index.set(indexKey, [fact]);
|
|
1726
|
+
else bucket.push(fact);
|
|
1727
|
+
}
|
|
1728
|
+
this.#indexes.set(mask, index);
|
|
1729
|
+
return index;
|
|
1730
|
+
}
|
|
1731
|
+
};
|
|
1732
|
+
const PACK = 2 ** 26;
|
|
1733
|
+
function valuesKey(values) {
|
|
1734
|
+
if (values.length === 1) return values[0];
|
|
1735
|
+
if (values.length === 2 && values[0] < PACK && values[1] < PACK) return values[0] * PACK + values[1];
|
|
1736
|
+
return values.join(",");
|
|
1737
|
+
}
|
|
1738
|
+
function maskKey(mask, args) {
|
|
1739
|
+
const values = [];
|
|
1740
|
+
for (let column = 0; column < args.length; column++) if ((mask & 1 << column) !== 0) values.push(args[column]);
|
|
1741
|
+
return valuesKey(values);
|
|
1742
|
+
}
|
|
1743
|
+
// src/term-dictionary.ts
|
|
1744
|
+
/**
|
|
1745
|
+
* TermDictionary interns RDF/JS terms as small integers, so facts are arrays
|
|
1746
|
+
* of numbers and every index key is a short string of digits. Ids are dense
|
|
1747
|
+
* and never reused; an id stays valid for the dictionary's lifetime.
|
|
1748
|
+
*/
|
|
1749
|
+
var TermDictionary = class {
|
|
1750
|
+
#ids = /* @__PURE__ */ new Map();
|
|
1751
|
+
#terms = [];
|
|
1752
|
+
/** id returns the id of `term`, interning it first if it is new. */
|
|
1753
|
+
id(term) {
|
|
1754
|
+
const key = termKey(term);
|
|
1755
|
+
let id = this.#ids.get(key);
|
|
1756
|
+
if (id === void 0) {
|
|
1757
|
+
id = this.#terms.length;
|
|
1758
|
+
this.#ids.set(key, id);
|
|
1759
|
+
this.#terms.push(term);
|
|
1760
|
+
}
|
|
1761
|
+
return id;
|
|
1762
|
+
}
|
|
1763
|
+
/** lookup returns the id of `term`, or `undefined` if it was never interned. */
|
|
1764
|
+
lookup(term) {
|
|
1765
|
+
return this.#ids.get(termKey(term));
|
|
1766
|
+
}
|
|
1767
|
+
/** term returns the term interned as `id`. */
|
|
1768
|
+
term(id) {
|
|
1769
|
+
const term = this.#terms[id];
|
|
1770
|
+
if (term === void 0) throw new RangeError(`Unknown term id ${id}`);
|
|
1771
|
+
return term;
|
|
1772
|
+
}
|
|
1773
|
+
/** size is the number of interned terms. */
|
|
1774
|
+
get size() {
|
|
1775
|
+
return this.#terms.length;
|
|
1776
|
+
}
|
|
1777
|
+
};
|
|
1778
|
+
// src/engine.ts
|
|
1779
|
+
const UNBOUND = -1;
|
|
1780
|
+
/**
|
|
1781
|
+
* Engine evaluates a set of rules to a fixpoint, semi-naively: each round
|
|
1782
|
+
* joins only the facts the previous round added against everything stored,
|
|
1783
|
+
* through hash indexes chosen per join step. Facts whose relation is the
|
|
1784
|
+
* equality relation and whose predicate is `sameAs` merge their subject and
|
|
1785
|
+
* object instead of being stored as-is (see {@link Equality}).
|
|
1786
|
+
*/
|
|
1787
|
+
var Engine = class {
|
|
1788
|
+
terms = new TermDictionary();
|
|
1789
|
+
equality = new Equality();
|
|
1790
|
+
statistics = {
|
|
1791
|
+
rounds: 0,
|
|
1792
|
+
derivations: 0,
|
|
1793
|
+
merges: 0
|
|
1794
|
+
};
|
|
1795
|
+
#relations = /* @__PURE__ */ new Map();
|
|
1796
|
+
#rules;
|
|
1797
|
+
#equalityRelation;
|
|
1798
|
+
#sameAs;
|
|
1799
|
+
#clock = 0;
|
|
1800
|
+
#delta = /* @__PURE__ */ new Map();
|
|
1801
|
+
#absorbed = [];
|
|
1802
|
+
#contentTerm;
|
|
1803
|
+
/** #contentful holds the roots whose class has a content term. */
|
|
1804
|
+
#contentful = /* @__PURE__ */ new Set();
|
|
1805
|
+
/** #reseed holds roots whose class gained a content term in a merge. */
|
|
1806
|
+
#reseed = [];
|
|
1807
|
+
/** #constants are the term ids that appear as constants in a rule. */
|
|
1808
|
+
#constants = /* @__PURE__ */ new Set();
|
|
1809
|
+
/** #constantRoots holds the roots whose class has a rule constant. */
|
|
1810
|
+
#constantRoots = /* @__PURE__ */ new Set();
|
|
1811
|
+
#context;
|
|
1812
|
+
#recorded;
|
|
1813
|
+
constructor(rules, options) {
|
|
1814
|
+
for (const [name, arity] of Object.entries(options.relations)) this.#relations.set(name, new Relation(name, arity));
|
|
1815
|
+
this.#equalityRelation = this.relation(options.equalityRelation);
|
|
1816
|
+
this.#sameAs = this.terms.id(options.sameAs);
|
|
1817
|
+
this.#contentTerm = options.contentTerm;
|
|
1818
|
+
this.#context = {
|
|
1819
|
+
term: (id) => this.terms.term(id),
|
|
1820
|
+
id: (term) => this.terms.id(term),
|
|
1821
|
+
find: (id) => this.equality.find(id),
|
|
1822
|
+
members: (root) => this.equality.members(root)
|
|
1823
|
+
};
|
|
1824
|
+
this.#rules = rules.map((rule) => this.#compile(rule));
|
|
1825
|
+
}
|
|
1826
|
+
/** clock is the tick the next stored fact will carry. */
|
|
1827
|
+
get clock() {
|
|
1828
|
+
return this.#clock;
|
|
1829
|
+
}
|
|
1830
|
+
/**
|
|
1831
|
+
* tick advances the clock and returns the new tick, so every fact stored
|
|
1832
|
+
* from now on is younger than every fact stored before.
|
|
1833
|
+
*/
|
|
1834
|
+
tick() {
|
|
1835
|
+
return ++this.#clock;
|
|
1836
|
+
}
|
|
1837
|
+
/** relation returns the declared relation called `name`. */
|
|
1838
|
+
relation(name) {
|
|
1839
|
+
const relation = this.#relations.get(name);
|
|
1840
|
+
if (relation === void 0) throw new Error(`Unknown relation ${name}`);
|
|
1841
|
+
return relation;
|
|
1842
|
+
}
|
|
1843
|
+
/** relations yields every declared relation. */
|
|
1844
|
+
relations() {
|
|
1845
|
+
return this.#relations.values();
|
|
1846
|
+
}
|
|
1847
|
+
/**
|
|
1848
|
+
* add stores a fact of `relation` for the next {@link Engine.run} and
|
|
1849
|
+
* returns `true` if it was new.
|
|
1850
|
+
*/
|
|
1851
|
+
add(relation, args) {
|
|
1852
|
+
return this.#store(this.relation(relation), args);
|
|
1853
|
+
}
|
|
1854
|
+
/** record starts collecting every fact stored from now on. */
|
|
1855
|
+
record() {
|
|
1856
|
+
this.#recorded = [];
|
|
1857
|
+
}
|
|
1858
|
+
/** stopRecording returns the facts stored since {@link Engine.record}. */
|
|
1859
|
+
stopRecording() {
|
|
1860
|
+
const facts = this.#recorded ?? [];
|
|
1861
|
+
this.#recorded = void 0;
|
|
1862
|
+
return facts;
|
|
1863
|
+
}
|
|
1864
|
+
/**
|
|
1865
|
+
* mentioning returns every live fact, per relation, that has one of `ids`
|
|
1866
|
+
* in a column. Facts name class representatives, so `ids` should hold them.
|
|
1867
|
+
*/
|
|
1868
|
+
mentioning(ids) {
|
|
1869
|
+
const found = /* @__PURE__ */ new Map();
|
|
1870
|
+
for (const relation of this.#relations.values()) {
|
|
1871
|
+
const facts = /* @__PURE__ */ new Set();
|
|
1872
|
+
for (const id of ids) for (let column = 0; column < relation.arity; column++) for (const fact of relation.column(column, id)) if (fact.diedAt === 1e400) facts.add(fact);
|
|
1873
|
+
if (facts.size > 0) found.set(relation, facts);
|
|
1874
|
+
}
|
|
1875
|
+
return found;
|
|
1876
|
+
}
|
|
1877
|
+
/**
|
|
1878
|
+
* purge retires `facts` (from {@link Engine.mentioning}) and splits the
|
|
1879
|
+
* equality classes of `ids` back into single terms, leaving the engine as
|
|
1880
|
+
* if those terms had never been mentioned. Assert their facts again and
|
|
1881
|
+
* {@link Engine.run} to rebuild what remains true of them.
|
|
1882
|
+
*/
|
|
1883
|
+
purge(ids, facts) {
|
|
1884
|
+
this.#clock++;
|
|
1885
|
+
for (const [relation, group] of facts) for (const fact of group) relation.retire(fact, this.#clock);
|
|
1886
|
+
for (const relation of this.#relations.values()) for (const id of ids) relation.forget(id);
|
|
1887
|
+
this.equality.reset(ids);
|
|
1888
|
+
for (const id of ids) {
|
|
1889
|
+
this.#contentful.delete(id);
|
|
1890
|
+
this.#constantRoots.delete(id);
|
|
1891
|
+
if (this.#constants.has(id)) this.#constantRoots.add(id);
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
/** run evaluates the rules until no round derives a new fact. */
|
|
1895
|
+
run() {
|
|
1896
|
+
this.#settle();
|
|
1897
|
+
while (this.#delta.size > 0) {
|
|
1898
|
+
this.statistics.rounds++;
|
|
1899
|
+
const delta = this.#delta;
|
|
1900
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
1901
|
+
this.#delta = /* @__PURE__ */ new Map();
|
|
1902
|
+
this.#clock++;
|
|
1903
|
+
for (const rule of this.#rules) {
|
|
1904
|
+
const binding = Array.from({ length: rule.slots }, () => UNBOUND);
|
|
1905
|
+
for (const seed of rule.seeds) {
|
|
1906
|
+
if (!this.#mayFire(seed)) continue;
|
|
1907
|
+
const facts = this.#seedFacts(delta, grouped, seed);
|
|
1908
|
+
if (facts === void 0) continue;
|
|
1909
|
+
for (const fact of facts) {
|
|
1910
|
+
if (fact.diedAt !== 1e400) continue;
|
|
1911
|
+
const bound = this.#unify(seed.atom, fact.args, binding);
|
|
1912
|
+
if (bound === null) continue;
|
|
1913
|
+
this.#join(rule, seed.plan, 0, binding);
|
|
1914
|
+
for (const slot of bound) binding[slot] = UNBOUND;
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
this.#settle();
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
/**
|
|
1922
|
+
* mayFire is `false` when another body atom of `seed` matches no live fact
|
|
1923
|
+
* at the start of the round: its relation is empty, or one of its constant
|
|
1924
|
+
* columns holds no live fact. Skipping is sound under semi-naive evaluation.
|
|
1925
|
+
* A firing's newest fact is in some round's delta, and every older premise
|
|
1926
|
+
* is stored by then, so the seed for that newest fact's atom sees every
|
|
1927
|
+
* guard non-empty and is not skipped. Only the per-column indexes every
|
|
1928
|
+
* relation already keeps are read, so no index is created.
|
|
1929
|
+
*/
|
|
1930
|
+
#mayFire(seed) {
|
|
1931
|
+
for (const atom of seed.guards) {
|
|
1932
|
+
if (atom.relation.size === 0) return false;
|
|
1933
|
+
for (let column = 0; column < atom.args.length; column++) {
|
|
1934
|
+
const arg = atom.args[column];
|
|
1935
|
+
if (arg < 0) continue;
|
|
1936
|
+
if (!atom.relation.column(column, this.equality.find(arg)).some((fact) => fact.diedAt === 1e400)) return false;
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
return true;
|
|
1940
|
+
}
|
|
1941
|
+
/** seedFacts returns the delta facts `seed` can match, grouping on demand. */
|
|
1942
|
+
#seedFacts(delta, grouped, seed) {
|
|
1943
|
+
const relation = seed.atom.relation;
|
|
1944
|
+
const facts = delta.get(relation);
|
|
1945
|
+
if (facts === void 0 || seed.keyColumn < 0) return facts;
|
|
1946
|
+
let columns = grouped.get(relation);
|
|
1947
|
+
if (columns === void 0) {
|
|
1948
|
+
columns = [];
|
|
1949
|
+
grouped.set(relation, columns);
|
|
1950
|
+
}
|
|
1951
|
+
let groups = columns[seed.keyColumn];
|
|
1952
|
+
if (groups === void 0) {
|
|
1953
|
+
groups = /* @__PURE__ */ new Map();
|
|
1954
|
+
for (const fact of facts) {
|
|
1955
|
+
const id = fact.args[seed.keyColumn];
|
|
1956
|
+
const group = groups.get(id);
|
|
1957
|
+
if (group === void 0) groups.set(id, [fact]);
|
|
1958
|
+
else group.push(fact);
|
|
1959
|
+
}
|
|
1960
|
+
columns[seed.keyColumn] = groups;
|
|
1961
|
+
}
|
|
1962
|
+
const constant = this.equality.find(seed.atom.args[seed.keyColumn]);
|
|
1963
|
+
return groups.get(constant);
|
|
1964
|
+
}
|
|
1965
|
+
#store(relation, args) {
|
|
1966
|
+
const equality = this.equality;
|
|
1967
|
+
const canonical = args.map((id) => equality.find(id));
|
|
1968
|
+
if (relation === this.#equalityRelation && canonical[1] === equality.find(this.#sameAs) && canonical[0] !== canonical[2]) {
|
|
1969
|
+
const left = canonical[0];
|
|
1970
|
+
const right = canonical[2];
|
|
1971
|
+
const leftContent = this.#hasContent(left);
|
|
1972
|
+
const rightContent = this.#hasContent(right);
|
|
1973
|
+
const leftConstant = this.#constantRoots.has(left);
|
|
1974
|
+
const rightConstant = this.#constantRoots.has(right);
|
|
1975
|
+
const absorbed = equality.union(left, right, this.#clock);
|
|
1976
|
+
if (absorbed !== void 0) {
|
|
1977
|
+
this.statistics.merges++;
|
|
1978
|
+
this.#absorbed.push(absorbed);
|
|
1979
|
+
const kept = absorbed === left ? right : left;
|
|
1980
|
+
this.#constantRoots.delete(absorbed);
|
|
1981
|
+
if (leftConstant || rightConstant) this.#constantRoots.add(kept);
|
|
1982
|
+
if (absorbed === left ? leftConstant : rightConstant) this.#reseed.push(kept);
|
|
1983
|
+
this.#contentful.delete(absorbed);
|
|
1984
|
+
if (leftContent || rightContent) this.#contentful.add(kept);
|
|
1985
|
+
if (absorbed === left ? leftContent : rightContent) this.#reseed.push(kept);
|
|
1986
|
+
}
|
|
1987
|
+
const root = equality.find(canonical[0]);
|
|
1988
|
+
canonical[0] = root;
|
|
1989
|
+
canonical[2] = root;
|
|
1990
|
+
canonical[1] = equality.find(canonical[1]);
|
|
1991
|
+
}
|
|
1992
|
+
const fact = relation.insert(canonical, this.#clock);
|
|
1993
|
+
if (fact === void 0) return false;
|
|
1994
|
+
this.#recorded?.push(fact);
|
|
1995
|
+
this.statistics.derivations++;
|
|
1996
|
+
const pending = this.#delta.get(relation);
|
|
1997
|
+
if (pending === void 0) this.#delta.set(relation, [fact]);
|
|
1998
|
+
else pending.push(fact);
|
|
1999
|
+
return true;
|
|
2000
|
+
}
|
|
2001
|
+
/** hasContent is `true` when the class of `root` holds a content term. */
|
|
2002
|
+
#hasContent(root) {
|
|
2003
|
+
if (this.#contentTerm === void 0) return false;
|
|
2004
|
+
if (this.equality.isTrivial(root)) return this.#contentTerm(this.terms.term(root));
|
|
2005
|
+
return this.#contentful.has(root);
|
|
2006
|
+
}
|
|
2007
|
+
/**
|
|
2008
|
+
* settle renames every fact that mentions a term absorbed by a merge. The
|
|
2009
|
+
* renamed facts join the next round's delta, so rules see the merged class
|
|
2010
|
+
* as one term.
|
|
2011
|
+
*/
|
|
2012
|
+
#settle() {
|
|
2013
|
+
while (this.#absorbed.length > 0) {
|
|
2014
|
+
const absorbed = this.#absorbed.pop();
|
|
2015
|
+
for (const relation of this.#relations.values()) {
|
|
2016
|
+
for (let column = 0; column < relation.arity; column++) for (const fact of relation.column(column, absorbed)) {
|
|
2017
|
+
if (fact.diedAt !== 1e400) continue;
|
|
2018
|
+
relation.retire(fact, this.#clock);
|
|
2019
|
+
this.#store(relation, fact.args);
|
|
2020
|
+
}
|
|
2021
|
+
relation.forget(absorbed);
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
const reseed = this.#reseed;
|
|
2025
|
+
this.#reseed = [];
|
|
2026
|
+
const queued = /* @__PURE__ */ new Set();
|
|
2027
|
+
for (const stale of reseed) {
|
|
2028
|
+
const root = this.equality.find(stale);
|
|
2029
|
+
for (const relation of this.#relations.values()) for (let column = 0; column < relation.arity; column++) for (const fact of relation.column(column, root)) {
|
|
2030
|
+
if (fact.diedAt !== 1e400 || fact.bornAt >= this.#clock || queued.has(fact)) continue;
|
|
2031
|
+
queued.add(fact);
|
|
2032
|
+
const pending = this.#delta.get(relation);
|
|
2033
|
+
if (pending === void 0) this.#delta.set(relation, [fact]);
|
|
2034
|
+
else pending.push(fact);
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
#join(rule, plan, step, binding) {
|
|
2039
|
+
if (step === plan.length) {
|
|
2040
|
+
for (const head of rule.head) {
|
|
2041
|
+
const args = head.args.map((arg) => arg >= 0 ? arg : binding[-arg - 1]);
|
|
2042
|
+
this.#store(head.relation, args);
|
|
2043
|
+
}
|
|
2044
|
+
return;
|
|
2045
|
+
}
|
|
2046
|
+
const current = plan[step];
|
|
2047
|
+
if (current.kind === "builtin") {
|
|
2048
|
+
const inputs = current.inputs.map((slot) => binding[slot]);
|
|
2049
|
+
for (const outputs of current.run(inputs, this.#context)) {
|
|
2050
|
+
const bound = [];
|
|
2051
|
+
let ok = true;
|
|
2052
|
+
for (let index = 0; index < current.outputs.length; index++) {
|
|
2053
|
+
const slot = current.outputs[index];
|
|
2054
|
+
const value = outputs[index];
|
|
2055
|
+
if (binding[slot] === UNBOUND) {
|
|
2056
|
+
binding[slot] = value;
|
|
2057
|
+
bound.push(slot);
|
|
2058
|
+
} else if (this.equality.find(binding[slot]) !== this.equality.find(value)) {
|
|
2059
|
+
ok = false;
|
|
2060
|
+
break;
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
if (ok) this.#join(rule, plan, step + 1, binding);
|
|
2064
|
+
for (const slot of bound) binding[slot] = UNBOUND;
|
|
2065
|
+
}
|
|
2066
|
+
return;
|
|
2067
|
+
}
|
|
2068
|
+
let mask = 0;
|
|
2069
|
+
const values = [];
|
|
2070
|
+
for (let column = 0; column < current.args.length; column++) {
|
|
2071
|
+
const arg = current.args[column];
|
|
2072
|
+
const value = arg >= 0 ? arg : binding[-arg - 1];
|
|
2073
|
+
if (value === UNBOUND) continue;
|
|
2074
|
+
mask |= 1 << column;
|
|
2075
|
+
values.push(this.equality.find(value));
|
|
2076
|
+
}
|
|
2077
|
+
const facts = current.relation.lookup(mask, values);
|
|
2078
|
+
for (let index = 0; index < facts.length; index++) {
|
|
2079
|
+
const fact = facts[index];
|
|
2080
|
+
if (fact.diedAt !== 1e400) continue;
|
|
2081
|
+
const bound = this.#unify(current, fact.args, binding);
|
|
2082
|
+
if (bound === null) continue;
|
|
2083
|
+
this.#join(rule, plan, step + 1, binding);
|
|
2084
|
+
for (const slot of bound) binding[slot] = UNBOUND;
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
/**
|
|
2088
|
+
* unify binds `atom`'s free variables to `args`, and returns the slots it
|
|
2089
|
+
* bound, or `null` (binding nothing) if the fact does not match.
|
|
2090
|
+
*/
|
|
2091
|
+
#unify(atom, args, binding) {
|
|
2092
|
+
const bound = [];
|
|
2093
|
+
for (let column = 0; column < atom.args.length; column++) {
|
|
2094
|
+
const arg = atom.args[column];
|
|
2095
|
+
const value = args[column];
|
|
2096
|
+
if (arg >= 0) {
|
|
2097
|
+
if (this.equality.find(arg) !== value) return undo(bound, binding);
|
|
2098
|
+
continue;
|
|
2099
|
+
}
|
|
2100
|
+
const slot = -arg - 1;
|
|
2101
|
+
if (binding[slot] === UNBOUND) {
|
|
2102
|
+
binding[slot] = value;
|
|
2103
|
+
bound.push(slot);
|
|
2104
|
+
} else if (this.equality.find(binding[slot]) !== value) return undo(bound, binding);
|
|
2105
|
+
}
|
|
2106
|
+
return bound;
|
|
2107
|
+
}
|
|
2108
|
+
#constant(term) {
|
|
2109
|
+
const id = this.terms.id(term);
|
|
2110
|
+
this.#constants.add(id);
|
|
2111
|
+
this.#constantRoots.add(id);
|
|
2112
|
+
return id;
|
|
2113
|
+
}
|
|
2114
|
+
#compile(rule) {
|
|
2115
|
+
const slots = /* @__PURE__ */ new Map();
|
|
2116
|
+
const slot = (variable) => {
|
|
2117
|
+
let index = slots.get(variable);
|
|
2118
|
+
if (index === void 0) {
|
|
2119
|
+
index = slots.size;
|
|
2120
|
+
slots.set(variable, index);
|
|
2121
|
+
}
|
|
2122
|
+
return index;
|
|
2123
|
+
};
|
|
2124
|
+
const compileAtom = (pattern) => {
|
|
2125
|
+
const relation = this.relation(pattern.relation);
|
|
2126
|
+
if (pattern.args.length !== relation.arity) throw new Error(`Rule ${rule.name}: ${pattern.relation} takes ${relation.arity} arguments`);
|
|
2127
|
+
return {
|
|
2128
|
+
kind: "atom",
|
|
2129
|
+
relation,
|
|
2130
|
+
args: pattern.args.map((arg) => arg instanceof Variable ? -slot(arg) - 1 : this.#constant(arg))
|
|
2131
|
+
};
|
|
2132
|
+
};
|
|
2133
|
+
const body = rule.body.map((pattern) => pattern.kind === "atom" ? compileAtom(pattern) : {
|
|
2134
|
+
kind: "builtin",
|
|
2135
|
+
name: pattern.name,
|
|
2136
|
+
inputs: pattern.inputs.map(slot),
|
|
2137
|
+
outputs: pattern.outputs.map(slot),
|
|
2138
|
+
run: pattern.run
|
|
2139
|
+
});
|
|
2140
|
+
const head = rule.head.map(compileAtom);
|
|
2141
|
+
const atoms = body.filter((step) => step.kind === "atom");
|
|
2142
|
+
if (atoms.length === 0) throw new Error(`Rule ${rule.name} has no body atom to seed from`);
|
|
2143
|
+
const seeds = atoms.map((seed) => {
|
|
2144
|
+
const constants = seed.args.flatMap((arg, column) => arg >= 0 ? [column] : []);
|
|
2145
|
+
return {
|
|
2146
|
+
atom: seed,
|
|
2147
|
+
plan: plan(rule.name, seed, body.filter((step) => step !== seed)),
|
|
2148
|
+
keyColumn: constants.includes(1) ? 1 : constants[0] ?? -1,
|
|
2149
|
+
guards: atoms.filter((atom) => atom !== seed)
|
|
2150
|
+
};
|
|
2151
|
+
});
|
|
2152
|
+
const bodyVariables = /* @__PURE__ */ new Set();
|
|
2153
|
+
for (const step of body) for (const variable of variablesOf(step)) bodyVariables.add(variable);
|
|
2154
|
+
for (const atom of head) for (const arg of atom.args) if (arg < 0 && !bodyVariables.has(-arg - 1)) throw new Error(`Rule ${rule.name}: a head variable is unbound`);
|
|
2155
|
+
return {
|
|
2156
|
+
name: rule.name,
|
|
2157
|
+
slots: slots.size,
|
|
2158
|
+
head,
|
|
2159
|
+
seeds
|
|
2160
|
+
};
|
|
2161
|
+
}
|
|
2162
|
+
};
|
|
2163
|
+
function undo(bound, binding) {
|
|
2164
|
+
for (const slot of bound) binding[slot] = UNBOUND;
|
|
2165
|
+
return null;
|
|
2166
|
+
}
|
|
2167
|
+
function variablesOf(step) {
|
|
2168
|
+
if (step.kind === "builtin") return [...step.inputs, ...step.outputs];
|
|
2169
|
+
return step.args.filter((arg) => arg < 0).map((arg) => -arg - 1);
|
|
2170
|
+
}
|
|
2171
|
+
/**
|
|
2172
|
+
* plan orders the remaining body after a seed: a builtin as soon as its
|
|
2173
|
+
* inputs are bound, otherwise the atom with the most bound columns (ties keep
|
|
2174
|
+
* the rule's written order, so rules list their schema atoms first).
|
|
2175
|
+
*/
|
|
2176
|
+
function plan(name, seed, rest) {
|
|
2177
|
+
const bound = new Set(variablesOf(seed));
|
|
2178
|
+
const ordered = [];
|
|
2179
|
+
const remaining = rest.slice();
|
|
2180
|
+
while (remaining.length > 0) {
|
|
2181
|
+
let chosen = remaining.findIndex((step) => step.kind === "builtin" && step.inputs.every((input) => bound.has(input)));
|
|
2182
|
+
if (chosen === -1) {
|
|
2183
|
+
let best = -1;
|
|
2184
|
+
for (let index = 0; index < remaining.length; index++) {
|
|
2185
|
+
const step = remaining[index];
|
|
2186
|
+
if (step.kind !== "atom") continue;
|
|
2187
|
+
const score = step.args.filter((arg) => arg >= 0 || bound.has(-arg - 1)).length;
|
|
2188
|
+
if (score > best) {
|
|
2189
|
+
best = score;
|
|
2190
|
+
chosen = index;
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
if (chosen === -1) throw new Error(`Rule ${name}: a builtin's inputs are never bound`);
|
|
2195
|
+
const [step] = remaining.splice(chosen, 1);
|
|
2196
|
+
ordered.push(step);
|
|
2197
|
+
for (const variable of variablesOf(step)) bound.add(variable);
|
|
2198
|
+
}
|
|
2199
|
+
return ordered;
|
|
2200
|
+
}
|
|
2201
|
+
// src/reasoner.ts
|
|
2202
|
+
const isSkolem = isSkolemTerm;
|
|
2203
|
+
const HUB_NAMESPACES = [
|
|
2204
|
+
RDF_NS,
|
|
2205
|
+
RDFS_NS,
|
|
2206
|
+
OWL_NS,
|
|
2207
|
+
XSD_NS
|
|
2208
|
+
];
|
|
2209
|
+
/**
|
|
2210
|
+
* isHub is `true` for the vocabulary terms every graph shares. They are left
|
|
2211
|
+
* out when finding which triples a retraction can affect, because two triples
|
|
2212
|
+
* that only share `rdf:type` do not derive anything from each other.
|
|
2213
|
+
*/
|
|
2214
|
+
function isHub(term) {
|
|
2215
|
+
return term.termType === "NamedNode" && HUB_NAMESPACES.some((namespace) => term.value.startsWith(namespace));
|
|
2216
|
+
}
|
|
2217
|
+
/** INSTANCE_VOCABULARY are the vocabulary predicates that relate instances. */
|
|
2218
|
+
const INSTANCE_VOCABULARY = /* @__PURE__ */ new Set([
|
|
2219
|
+
`${RDF_NS}type`,
|
|
2220
|
+
`${OWL_NS}sameAs`,
|
|
2221
|
+
`${OWL_NS}differentFrom`,
|
|
2222
|
+
"http://www.w3.org/2000/01/rdf-schema#label",
|
|
2223
|
+
"http://www.w3.org/2000/01/rdf-schema#comment",
|
|
2224
|
+
"http://www.w3.org/2000/01/rdf-schema#seeAlso",
|
|
2225
|
+
"http://www.w3.org/2000/01/rdf-schema#isDefinedBy"
|
|
2226
|
+
]);
|
|
2227
|
+
/**
|
|
2228
|
+
* isInstance is `true` for a triple about individuals: an ordinary property
|
|
2229
|
+
* between two terms, `rdf:type` to a class that is not OWL or RDFS vocabulary,
|
|
2230
|
+
* `owl:sameAs`, `owl:differentFrom` and annotations. Everything else (the
|
|
2231
|
+
* ontology: subclass, domain and range, property characteristics, restrictions,
|
|
2232
|
+
* lists) is schema, and retracting it recomputes the whole closure, since a
|
|
2233
|
+
* class or property is shared by instances all over the graph.
|
|
2234
|
+
*/
|
|
2235
|
+
function isInstance([, p, o]) {
|
|
2236
|
+
if (p.termType !== "NamedNode") return false;
|
|
2237
|
+
if (!isHub(p)) return true;
|
|
2238
|
+
if (!INSTANCE_VOCABULARY.has(p.value)) return false;
|
|
2239
|
+
return p.value !== `http://www.w3.org/1999/02/22-rdf-syntax-ns#type` || !isHub(o);
|
|
2240
|
+
}
|
|
2241
|
+
/**
|
|
2242
|
+
* connectors are the terms of an instance triple that link it to other
|
|
2243
|
+
* instance triples: its subject and object. Its predicate and the class of an
|
|
2244
|
+
* `rdf:type` triple are schema terms that instances share without depending on
|
|
2245
|
+
* each other, so they do not connect.
|
|
2246
|
+
*/
|
|
2247
|
+
function connectors(triple) {
|
|
2248
|
+
if (!isInstance(triple)) return [];
|
|
2249
|
+
const [s, p, o] = triple;
|
|
2250
|
+
return (p.value === `http://www.w3.org/1999/02/22-rdf-syntax-ns#type` ? [s] : [s, o]).filter((term) => !isHub(term));
|
|
2251
|
+
}
|
|
2252
|
+
const tripleKey = (s, p, o) => `${termKey(s)} ${termKey(p)} ${termKey(o)}`;
|
|
2253
|
+
/** shown is `true` when the closure view lists the triple. */
|
|
2254
|
+
function shown(s, p, o, view) {
|
|
2255
|
+
if (p.termType !== "NamedNode") return false;
|
|
2256
|
+
if (view === "complete") return true;
|
|
2257
|
+
if (s.termType === "Literal") return false;
|
|
2258
|
+
if (s.equals(o) && p.equals(OWL.sameAs)) return false;
|
|
2259
|
+
if (isSkolem(s) || isSkolem(p) || isSkolem(o)) return false;
|
|
2260
|
+
return !(p.equals(RDF.type) && o.termType === "BlankNode");
|
|
2261
|
+
}
|
|
2262
|
+
/**
|
|
2263
|
+
* Core materializes the OWL 2 RL closure of the triples it is given, as one
|
|
2264
|
+
* graph: it ignores the graph term. {@link Reasoner} layers named graphs on top
|
|
2265
|
+
* of it.
|
|
2266
|
+
*
|
|
2267
|
+
* {@link Core.extend} adds more triples to a closure that is already
|
|
2268
|
+
* computed and returns only what they entail that was not entailed before,
|
|
2269
|
+
* evaluating only the rules the new facts can reach. {@link Core.retract}
|
|
2270
|
+
* removes asserted triples and what only they entailed.
|
|
2271
|
+
*
|
|
2272
|
+
* {@link CustomRule}s, given to the constructor or {@link Reasoner.addRule},
|
|
2273
|
+
* run in the same fixpoint as the OWL 2 RL rules. What they derive is stored
|
|
2274
|
+
* as triples of the closure, so `closure("complete")` lists it and
|
|
2275
|
+
* `closure("application")` lists it under the same filters as any other
|
|
2276
|
+
* triple. `extend` stays incremental. `retract` is not: with a custom rule
|
|
2277
|
+
* registered it recomputes the whole closure.
|
|
2278
|
+
*/
|
|
2279
|
+
var Core = class {
|
|
2280
|
+
#engine;
|
|
2281
|
+
#triples;
|
|
2282
|
+
#ran = false;
|
|
2283
|
+
/** #asserted holds the triples given to add or extend, not what they entail. */
|
|
2284
|
+
#asserted = /* @__PURE__ */ new Map();
|
|
2285
|
+
/**
|
|
2286
|
+
* #parent and #owned index the asserted triples by connected component: a
|
|
2287
|
+
* union-find over the keys of their non-vocabulary terms, and, per root, the
|
|
2288
|
+
* keys of the triples in that component. A retraction does not split a
|
|
2289
|
+
* component here, so an index can only be coarser than the truth until
|
|
2290
|
+
* {@link Core.retract} rebuilds the components it touched.
|
|
2291
|
+
*/
|
|
2292
|
+
#parent = /* @__PURE__ */ new Map();
|
|
2293
|
+
#owned = /* @__PURE__ */ new Map();
|
|
2294
|
+
#custom = [];
|
|
2295
|
+
#compiled = [];
|
|
2296
|
+
constructor(options = {}) {
|
|
2297
|
+
for (const rule of options.rules ?? []) this.#register(rule);
|
|
2298
|
+
this.#init();
|
|
2299
|
+
}
|
|
2300
|
+
/**
|
|
2301
|
+
* addRule registers a custom rule. The closure is recomputed from the
|
|
2302
|
+
* asserted triples the next time it is read or extended, so the rule applies
|
|
2303
|
+
* to everything already added. Throws an `Error` for an invalid rule (see
|
|
2304
|
+
* {@link CustomRule}); the reasoner is unchanged then.
|
|
2305
|
+
*/
|
|
2306
|
+
addRule(rule) {
|
|
2307
|
+
this.#register(rule);
|
|
2308
|
+
this.#init();
|
|
2309
|
+
for (const triple of this.#asserted.values()) this.#store(...triple);
|
|
2310
|
+
this.#ran = false;
|
|
2311
|
+
}
|
|
2312
|
+
/** rules lists the custom rules registered so far. */
|
|
2313
|
+
get rules() {
|
|
2314
|
+
return this.#custom;
|
|
2315
|
+
}
|
|
2316
|
+
#register(rule) {
|
|
2317
|
+
const compiled = compileCustomRule(rule, new Set(this.#custom.map((r) => r.name)));
|
|
2318
|
+
this.#custom.push(rule);
|
|
2319
|
+
this.#compiled.push(compiled);
|
|
2320
|
+
}
|
|
2321
|
+
#init() {
|
|
2322
|
+
this.#engine = new Engine([...OWL2RL_RULES, ...this.#compiled], {
|
|
2323
|
+
equalityRelation: TRIPLE,
|
|
2324
|
+
sameAs: OWL.sameAs,
|
|
2325
|
+
relations: RELATIONS,
|
|
2326
|
+
contentTerm: (term) => term.termType === "Literal"
|
|
2327
|
+
});
|
|
2328
|
+
this.#triples = this.#engine.relation(TRIPLE);
|
|
2329
|
+
for (const [subject, predicate, object] of AXIOMS) this.#store(subject, predicate, object);
|
|
2330
|
+
}
|
|
2331
|
+
/** statistics count rounds, derivations, and merges across every run. */
|
|
2332
|
+
get statistics() {
|
|
2333
|
+
return this.#engine.statistics;
|
|
2334
|
+
}
|
|
2335
|
+
/** size is the number of stored facts, before equality classes expand. */
|
|
2336
|
+
get size() {
|
|
2337
|
+
return this.#triples.size;
|
|
2338
|
+
}
|
|
2339
|
+
/** add queues triples for the next {@link Core.run}. */
|
|
2340
|
+
add(triples) {
|
|
2341
|
+
for (const triple of triples) this.#assert(triple.subject, triple.predicate, triple.object);
|
|
2342
|
+
}
|
|
2343
|
+
/** run computes the closure of everything added so far. */
|
|
2344
|
+
run() {
|
|
2345
|
+
this.#engine.run();
|
|
2346
|
+
this.#ran = true;
|
|
2347
|
+
}
|
|
2348
|
+
/**
|
|
2349
|
+
* extend adds `triples` to the computed closure and returns the triples of
|
|
2350
|
+
* the closure that are new: the added triples themselves when they were not
|
|
2351
|
+
* already entailed, and everything they entail.
|
|
2352
|
+
*/
|
|
2353
|
+
extend(triples, view = "complete") {
|
|
2354
|
+
if (!this.#ran) this.run();
|
|
2355
|
+
const since = this.#engine.tick();
|
|
2356
|
+
this.add(triples);
|
|
2357
|
+
this.run();
|
|
2358
|
+
const equality = this.#engine.equality;
|
|
2359
|
+
const facts = /* @__PURE__ */ new Set();
|
|
2360
|
+
for (const fact of this.#triples.facts()) if (fact.bornAt >= since) facts.add(fact);
|
|
2361
|
+
for (const root of equality.rootsChangedSince(since)) for (let column = 0; column < 3; column++) for (const fact of this.#triples.column(column, root)) if (fact.diedAt === 1e400) facts.add(fact);
|
|
2362
|
+
const added = [];
|
|
2363
|
+
for (const fact of facts) for (const [s, p, o] of this.#expand(fact)) {
|
|
2364
|
+
const before = this.#triples.get(factKey([
|
|
2365
|
+
equality.findAt(s, since),
|
|
2366
|
+
equality.findAt(p, since),
|
|
2367
|
+
equality.findAt(o, since)
|
|
2368
|
+
]));
|
|
2369
|
+
if (before !== void 0 && before.bornAt < since && before.diedAt >= since) continue;
|
|
2370
|
+
const quad = this.#quad(s, p, o, view);
|
|
2371
|
+
if (quad !== void 0) added.push(quad);
|
|
2372
|
+
}
|
|
2373
|
+
return added;
|
|
2374
|
+
}
|
|
2375
|
+
/**
|
|
2376
|
+
* retract removes asserted `triples` and returns the triples of the closure
|
|
2377
|
+
* that are no longer entailed. A triple that was never asserted is ignored,
|
|
2378
|
+
* even if it is entailed. The result equals a fresh run on everything
|
|
2379
|
+
* asserted but `triples`.
|
|
2380
|
+
*
|
|
2381
|
+
* Retracting triples about individuals recomputes only their connected
|
|
2382
|
+
* component: the triples reachable from them through shared subjects and
|
|
2383
|
+
* objects. Classes and properties do not connect, because instances share
|
|
2384
|
+
* them without depending on each other. The whole closure is recomputed
|
|
2385
|
+
* instead when any retracted triple is schema (subclass, domain, restriction
|
|
2386
|
+
* and so on), when the component is larger than
|
|
2387
|
+
* {@link RetractOptions.maxScope}, or when it turns out not to be closed
|
|
2388
|
+
* (a fact links it to another individual or to a complement class, or an
|
|
2389
|
+
* asserted triple outside it names one of its terms).
|
|
2390
|
+
*
|
|
2391
|
+
* When any {@link CustomRule} is registered, `retract` is not incremental:
|
|
2392
|
+
* it always recomputes the whole closure, because a custom rule may relate
|
|
2393
|
+
* any triples, so no component is known to be closed. The result is still
|
|
2394
|
+
* the same as a fresh run.
|
|
2395
|
+
*
|
|
2396
|
+
* The result equals a fresh run for every input the differential test in
|
|
2397
|
+
* `test/differential/` generates. Rules that derive schema-level facts from
|
|
2398
|
+
* instance triples, other than the complement case handled above, are the
|
|
2399
|
+
* known place this could be wrong.
|
|
2400
|
+
*/
|
|
2401
|
+
retract(triples, view = "complete", options = {}) {
|
|
2402
|
+
if (!this.#ran) this.run();
|
|
2403
|
+
const gone = /* @__PURE__ */ new Set();
|
|
2404
|
+
for (const t of triples) {
|
|
2405
|
+
const key = tripleKey(t.subject, t.predicate, t.object);
|
|
2406
|
+
if (this.#asserted.has(key)) gone.add(key);
|
|
2407
|
+
}
|
|
2408
|
+
if (gone.size === 0) return [];
|
|
2409
|
+
const scope = this.#custom.length > 0 ? void 0 : this.#component(gone);
|
|
2410
|
+
const goneTriples = new Map([...gone].map((key) => [key, this.#asserted.get(key)]));
|
|
2411
|
+
for (const key of gone) this.#asserted.delete(key);
|
|
2412
|
+
if (scope !== void 0) this.#reindex(scope, goneTriples);
|
|
2413
|
+
if (scope !== void 0 && scope.size <= (options.maxScope ?? 5e4)) {
|
|
2414
|
+
const removed = this.#recompute(scope, gone, [...goneTriples.values()], view);
|
|
2415
|
+
if (removed !== void 0) return removed;
|
|
2416
|
+
}
|
|
2417
|
+
return this.#rebuild(view);
|
|
2418
|
+
}
|
|
2419
|
+
/** nodes are the keys of the terms of a triple that connect components. */
|
|
2420
|
+
#nodes(triple) {
|
|
2421
|
+
return connectors(triple).map(termKey);
|
|
2422
|
+
}
|
|
2423
|
+
#find(key) {
|
|
2424
|
+
let root = key;
|
|
2425
|
+
while (this.#parent.get(root) !== root) root = this.#parent.get(root);
|
|
2426
|
+
while (this.#parent.get(key) !== root) {
|
|
2427
|
+
const next = this.#parent.get(key);
|
|
2428
|
+
this.#parent.set(key, root);
|
|
2429
|
+
key = next;
|
|
2430
|
+
}
|
|
2431
|
+
return root;
|
|
2432
|
+
}
|
|
2433
|
+
/** link files the asserted triple under the component of its terms. */
|
|
2434
|
+
#link(key, triple) {
|
|
2435
|
+
const nodes = this.#nodes(triple);
|
|
2436
|
+
if (nodes.length === 0) return;
|
|
2437
|
+
for (const node of nodes) if (!this.#parent.has(node)) {
|
|
2438
|
+
this.#parent.set(node, node);
|
|
2439
|
+
this.#owned.set(node, /* @__PURE__ */ new Set());
|
|
2440
|
+
}
|
|
2441
|
+
let root = this.#find(nodes[0]);
|
|
2442
|
+
for (let i = 1; i < nodes.length; i++) {
|
|
2443
|
+
let other = this.#find(nodes[i]);
|
|
2444
|
+
if (other === root) continue;
|
|
2445
|
+
if (this.#owned.get(other).size > this.#owned.get(root).size) [root, other] = [other, root];
|
|
2446
|
+
const into = this.#owned.get(root);
|
|
2447
|
+
for (const owned of this.#owned.get(other)) into.add(owned);
|
|
2448
|
+
this.#owned.delete(other);
|
|
2449
|
+
this.#parent.set(other, root);
|
|
2450
|
+
}
|
|
2451
|
+
this.#owned.get(root).add(key);
|
|
2452
|
+
}
|
|
2453
|
+
/**
|
|
2454
|
+
* component returns the keys of the asserted triples connected to `gone`,
|
|
2455
|
+
* or `undefined` when `gone` touches only vocabulary terms.
|
|
2456
|
+
*/
|
|
2457
|
+
#component(gone) {
|
|
2458
|
+
const roots = /* @__PURE__ */ new Set();
|
|
2459
|
+
for (const key of gone) if (!isInstance(this.#asserted.get(key))) return void 0;
|
|
2460
|
+
for (const key of gone) {
|
|
2461
|
+
const [first] = this.#nodes(this.#asserted.get(key));
|
|
2462
|
+
if (first !== void 0) roots.add(this.#find(first));
|
|
2463
|
+
}
|
|
2464
|
+
if (roots.size === 0) return void 0;
|
|
2465
|
+
const scope = /* @__PURE__ */ new Set();
|
|
2466
|
+
for (const root of roots) for (const key of this.#owned.get(root)) scope.add(key);
|
|
2467
|
+
return scope;
|
|
2468
|
+
}
|
|
2469
|
+
/**
|
|
2470
|
+
* reindex splits the components of `scope` again, now that the retracted
|
|
2471
|
+
* triples are gone, so they do not stay merged for good.
|
|
2472
|
+
*/
|
|
2473
|
+
#reindex(scope, gone) {
|
|
2474
|
+
const triple = (key) => this.#asserted.get(key) ?? gone.get(key);
|
|
2475
|
+
const nodes = /* @__PURE__ */ new Set();
|
|
2476
|
+
for (const key of scope) for (const node of this.#nodes(triple(key))) nodes.add(node);
|
|
2477
|
+
for (const node of nodes) this.#owned.delete(this.#find(node));
|
|
2478
|
+
for (const node of nodes) this.#parent.delete(node);
|
|
2479
|
+
for (const key of scope) {
|
|
2480
|
+
const kept = this.#asserted.get(key);
|
|
2481
|
+
if (kept !== void 0) this.#link(key, kept);
|
|
2482
|
+
}
|
|
2483
|
+
}
|
|
2484
|
+
/**
|
|
2485
|
+
* recompute drops every fact that mentions a term of the component, splits
|
|
2486
|
+
* its `owl:sameAs` classes, asserts the component's remaining triples again
|
|
2487
|
+
* and runs to a fixpoint. It returns `undefined`, having changed nothing,
|
|
2488
|
+
* when the component is not closed (a fact or class links it to a term
|
|
2489
|
+
* outside it), so the caller can recompute everything.
|
|
2490
|
+
*/
|
|
2491
|
+
#recompute(scope, gone, goneTriples, view) {
|
|
2492
|
+
const engine = this.#engine;
|
|
2493
|
+
const terms = engine.terms;
|
|
2494
|
+
const equality = engine.equality;
|
|
2495
|
+
const queue = goneTriples.flatMap(connectors);
|
|
2496
|
+
for (const key of scope) if (!gone.has(key)) queue.push(...connectors(this.#asserted.get(key)));
|
|
2497
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2498
|
+
while (queue.length > 0) {
|
|
2499
|
+
const term = queue.pop();
|
|
2500
|
+
const id = terms.lookup(term);
|
|
2501
|
+
if (id !== void 0) ids.add(id);
|
|
2502
|
+
const skolem = terms.lookup(complementSkolem(term));
|
|
2503
|
+
if (skolem !== void 0 && !ids.has(skolem)) queue.push(terms.term(skolem));
|
|
2504
|
+
}
|
|
2505
|
+
for (const id of Array.from(ids)) for (const member of equality.members(equality.find(id))) ids.add(member);
|
|
2506
|
+
for (const id of ids) if (isHub(terms.term(id))) return void 0;
|
|
2507
|
+
const facts = engine.mentioning(ids);
|
|
2508
|
+
for (const [relation, group] of facts) for (const fact of group) for (let column = 0; column < fact.args.length; column++) {
|
|
2509
|
+
if (relation.name === "inconsistency" && column === 0) continue;
|
|
2510
|
+
const arg = fact.args[column];
|
|
2511
|
+
if (ids.has(arg)) continue;
|
|
2512
|
+
const outside = terms.term(arg);
|
|
2513
|
+
if (this.#parent.has(termKey(outside))) return void 0;
|
|
2514
|
+
if (isSkolem(outside)) return void 0;
|
|
2515
|
+
}
|
|
2516
|
+
const before = /* @__PURE__ */ new Map();
|
|
2517
|
+
for (const fact of facts.get(this.#triples) ?? []) for (const [s, p, o] of this.#expand(fact)) {
|
|
2518
|
+
const key = tripleKey(terms.term(s), terms.term(p), terms.term(o));
|
|
2519
|
+
if (this.#asserted.has(key) && !scope.has(key)) return void 0;
|
|
2520
|
+
const quad = this.#quad(s, p, o, "complete");
|
|
2521
|
+
if (quad !== void 0) before.set(tripleKey(quad.subject, quad.predicate, quad.object), quad);
|
|
2522
|
+
}
|
|
2523
|
+
engine.purge(ids, facts);
|
|
2524
|
+
engine.record();
|
|
2525
|
+
for (const key of scope) {
|
|
2526
|
+
const triple = this.#asserted.get(key);
|
|
2527
|
+
if (triple !== void 0) this.#store(...triple);
|
|
2528
|
+
}
|
|
2529
|
+
engine.run();
|
|
2530
|
+
for (const fact of engine.stopRecording()) {
|
|
2531
|
+
if (fact.diedAt !== 1e400) continue;
|
|
2532
|
+
if (this.#triples.get(fact.key) !== fact) continue;
|
|
2533
|
+
for (const [s, p, o] of this.#expand(fact)) before.delete(tripleKey(terms.term(s), terms.term(p), terms.term(o)));
|
|
2534
|
+
}
|
|
2535
|
+
return [...before.values()].filter((quad) => shown(quad.subject, quad.predicate, quad.object, view));
|
|
2536
|
+
}
|
|
2537
|
+
/** rebuild recomputes the whole closure from the asserted triples. */
|
|
2538
|
+
#rebuild(view) {
|
|
2539
|
+
const old = /* @__PURE__ */ new Map();
|
|
2540
|
+
for (const quad of this.closure("complete")) old.set(tripleKey(quad.subject, quad.predicate, quad.object), quad);
|
|
2541
|
+
this.#init();
|
|
2542
|
+
for (const triple of this.#asserted.values()) this.#store(...triple);
|
|
2543
|
+
this.run();
|
|
2544
|
+
for (const quad of this.closure("complete")) old.delete(tripleKey(quad.subject, quad.predicate, quad.object));
|
|
2545
|
+
return [...old.values()].filter((quad) => shown(quad.subject, quad.predicate, quad.object, view));
|
|
2546
|
+
}
|
|
2547
|
+
/**
|
|
2548
|
+
* closure yields every triple of the closure, the axiomatic triples
|
|
2549
|
+
* included, with each `owl:sameAs` class expanded to its members.
|
|
2550
|
+
*/
|
|
2551
|
+
*closure(view = "complete") {
|
|
2552
|
+
if (!this.#ran) this.run();
|
|
2553
|
+
for (const fact of this.#triples.facts()) for (const [s, p, o] of this.#expand(fact)) {
|
|
2554
|
+
const quad = this.#quad(s, p, o, view);
|
|
2555
|
+
if (quad !== void 0) yield quad;
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2558
|
+
/** has is `true` when the closure entails the triple. */
|
|
2559
|
+
has(subject, predicate, object) {
|
|
2560
|
+
if (!this.#ran) this.run();
|
|
2561
|
+
const terms = this.#engine.terms;
|
|
2562
|
+
const ids = [
|
|
2563
|
+
subject,
|
|
2564
|
+
predicate,
|
|
2565
|
+
object
|
|
2566
|
+
].map((term) => terms.lookup(term));
|
|
2567
|
+
if (ids.some((id) => id === void 0)) return false;
|
|
2568
|
+
const equality = this.#engine.equality;
|
|
2569
|
+
return this.#triples.has(ids.map((id) => equality.find(id)));
|
|
2570
|
+
}
|
|
2571
|
+
/** inconsistencies lists every OWL 2 RL `false` rule the closure violates. */
|
|
2572
|
+
inconsistencies() {
|
|
2573
|
+
if (!this.#ran) this.run();
|
|
2574
|
+
const terms = this.#engine.terms;
|
|
2575
|
+
const none = terms.id(NONE);
|
|
2576
|
+
const reports = [];
|
|
2577
|
+
for (const fact of this.#engine.relation(INCONSISTENCY).facts()) {
|
|
2578
|
+
const [rule, ...matched] = fact.args;
|
|
2579
|
+
reports.push({
|
|
2580
|
+
rule: terms.term(rule).value,
|
|
2581
|
+
terms: matched.filter((id) => id !== none).map((id) => terms.term(id))
|
|
2582
|
+
});
|
|
2583
|
+
}
|
|
2584
|
+
return reports;
|
|
2585
|
+
}
|
|
2586
|
+
#assert(subject, predicate, object) {
|
|
2587
|
+
const key = tripleKey(subject, predicate, object);
|
|
2588
|
+
if (!this.#asserted.has(key)) {
|
|
2589
|
+
const triple = [
|
|
2590
|
+
subject,
|
|
2591
|
+
predicate,
|
|
2592
|
+
object
|
|
2593
|
+
];
|
|
2594
|
+
this.#asserted.set(key, triple);
|
|
2595
|
+
this.#link(key, triple);
|
|
2596
|
+
}
|
|
2597
|
+
this.#store(subject, predicate, object);
|
|
2598
|
+
}
|
|
2599
|
+
#store(subject, predicate, object) {
|
|
2600
|
+
const terms = this.#engine.terms;
|
|
2601
|
+
this.#engine.add(TRIPLE, [
|
|
2602
|
+
terms.id(subject),
|
|
2603
|
+
terms.id(predicate),
|
|
2604
|
+
terms.id(object)
|
|
2605
|
+
]);
|
|
2606
|
+
}
|
|
2607
|
+
*#expand(fact) {
|
|
2608
|
+
const equality = this.#engine.equality;
|
|
2609
|
+
const [s, p, o] = fact.args;
|
|
2610
|
+
for (const subject of equality.members(s)) for (const predicate of equality.members(p)) for (const object of equality.members(o)) yield [
|
|
2611
|
+
subject,
|
|
2612
|
+
predicate,
|
|
2613
|
+
object
|
|
2614
|
+
];
|
|
2615
|
+
}
|
|
2616
|
+
#quad(s, p, o, view) {
|
|
2617
|
+
const terms = this.#engine.terms;
|
|
2618
|
+
const predicate = terms.term(p);
|
|
2619
|
+
const subject = terms.term(s);
|
|
2620
|
+
const object = terms.term(o);
|
|
2621
|
+
if (!shown(subject, predicate, object, view)) return void 0;
|
|
2622
|
+
return generalizedQuad(subject, predicate, object);
|
|
2623
|
+
}
|
|
2624
|
+
};
|
|
2625
|
+
/**
|
|
2626
|
+
* DERIVED_GRAPH is the graph a triple lands in when it is entailed only by a
|
|
2627
|
+
* combination of graphs: no single graph entails it on its own.
|
|
2628
|
+
*/
|
|
2629
|
+
const DERIVED_GRAPH = dataFactory.namedNode("https://wazoo.dev/ns/reasoner#derived");
|
|
2630
|
+
const DEFAULT_KEY = termKey(dataFactory.defaultGraph());
|
|
2631
|
+
const DERIVED_KEY = termKey(DERIVED_GRAPH);
|
|
2632
|
+
const graphKey = (graph) => graph === void 0 ? DEFAULT_KEY : termKey(graph);
|
|
2633
|
+
let baseKeys;
|
|
2634
|
+
/** base is the triples entailed by nothing: the axiomatic triples. */
|
|
2635
|
+
function base() {
|
|
2636
|
+
if (baseKeys === void 0) {
|
|
2637
|
+
baseKeys = /* @__PURE__ */ new Set();
|
|
2638
|
+
for (const q of new Core().closure("complete")) baseKeys.add(tripleKey(q.subject, q.predicate, q.object));
|
|
2639
|
+
}
|
|
2640
|
+
return baseKeys;
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* Reasoner materializes the OWL 2 RL closure of the quads it is given, with
|
|
2644
|
+
* named graphs (see {@link GraphMode}).
|
|
2645
|
+
*
|
|
2646
|
+
* ```ts
|
|
2647
|
+
* const reasoner = new Reasoner();
|
|
2648
|
+
* reasoner.add(quads);
|
|
2649
|
+
* reasoner.run();
|
|
2650
|
+
* for (const quad of reasoner.closure()) store.add(quad);
|
|
2651
|
+
* ```
|
|
2652
|
+
*
|
|
2653
|
+
* {@link Reasoner.extend} adds more quads to a closure that is already
|
|
2654
|
+
* computed and returns only what they entail that was not entailed before,
|
|
2655
|
+
* evaluating only the rules the new facts can reach. {@link Reasoner.retract}
|
|
2656
|
+
* removes asserted quads and what only they entailed. A triple asserted in two
|
|
2657
|
+
* graphs stays asserted until it is retracted from both.
|
|
2658
|
+
*
|
|
2659
|
+
* Internally one incremental closure holds the union of all graphs. With more
|
|
2660
|
+
* than one graph in `"per-graph"` mode, the closure of each graph alone is also
|
|
2661
|
+
* computed, lazily, to decide which graphs list a triple; `extend` and
|
|
2662
|
+
* `retract` then diff the listings, which costs a pass over the closure.
|
|
2663
|
+
*/
|
|
2664
|
+
var Reasoner = class {
|
|
2665
|
+
#mode;
|
|
2666
|
+
#union;
|
|
2667
|
+
/** #graphs are the graphs with asserted triples, and those triples. */
|
|
2668
|
+
#graphs = /* @__PURE__ */ new Map();
|
|
2669
|
+
/** #refs counts the graphs each asserted triple is asserted in. */
|
|
2670
|
+
#refs = /* @__PURE__ */ new Map();
|
|
2671
|
+
#per;
|
|
2672
|
+
constructor(options = {}) {
|
|
2673
|
+
this.#mode = options.graphs ?? "per-graph";
|
|
2674
|
+
this.#union = new Core({ rules: options.rules });
|
|
2675
|
+
}
|
|
2676
|
+
/**
|
|
2677
|
+
* addRule registers a custom rule. The closure is recomputed from the
|
|
2678
|
+
* asserted quads on the next read. See {@link CustomRule}.
|
|
2679
|
+
*/
|
|
2680
|
+
addRule(rule) {
|
|
2681
|
+
this.#union.addRule(rule);
|
|
2682
|
+
this.#per = void 0;
|
|
2683
|
+
}
|
|
2684
|
+
/** rules lists the custom rules registered so far. */
|
|
2685
|
+
get rules() {
|
|
2686
|
+
return this.#union.rules;
|
|
2687
|
+
}
|
|
2688
|
+
/** statistics count rounds, derivations, and merges across every run. */
|
|
2689
|
+
get statistics() {
|
|
2690
|
+
return this.#union.statistics;
|
|
2691
|
+
}
|
|
2692
|
+
/** size is the number of stored facts, before equality classes expand. */
|
|
2693
|
+
get size() {
|
|
2694
|
+
return this.#union.size;
|
|
2695
|
+
}
|
|
2696
|
+
/** add queues quads for the next {@link Reasoner.run}. */
|
|
2697
|
+
add(quads) {
|
|
2698
|
+
const all = [...quads];
|
|
2699
|
+
for (const quad of all) this.#register(quad);
|
|
2700
|
+
this.#union.add(all);
|
|
2701
|
+
}
|
|
2702
|
+
/** run computes the closure of everything added so far. */
|
|
2703
|
+
run() {
|
|
2704
|
+
this.#union.run();
|
|
2705
|
+
}
|
|
2706
|
+
/**
|
|
2707
|
+
* extend adds `quads` to the computed closure and returns the quads of the
|
|
2708
|
+
* closure that are new: the added ones when they were not already entailed in
|
|
2709
|
+
* their graph, and everything they entail.
|
|
2710
|
+
*/
|
|
2711
|
+
extend(quads, view = "complete") {
|
|
2712
|
+
const all = [...quads];
|
|
2713
|
+
const after = new Set(this.#graphs.keys());
|
|
2714
|
+
for (const quad of all) after.add(graphKey(quad.graph));
|
|
2715
|
+
const diffing = this.#mode === "per-graph" && after.size > 1;
|
|
2716
|
+
const before = diffing ? this.#listing() : void 0;
|
|
2717
|
+
for (const quad of all) this.#register(quad);
|
|
2718
|
+
const added = this.#union.extend(all, diffing ? "complete" : view);
|
|
2719
|
+
if (before === void 0) return this.#label(added);
|
|
2720
|
+
return this.#difference(this.#listing(), before, view);
|
|
2721
|
+
}
|
|
2722
|
+
/**
|
|
2723
|
+
* retract removes asserted `quads` and returns the quads of the closure that
|
|
2724
|
+
* are no longer entailed. A quad that was not asserted in its graph is
|
|
2725
|
+
* ignored, even if it is entailed. The result equals a fresh run on
|
|
2726
|
+
* everything asserted but `quads`.
|
|
2727
|
+
*/
|
|
2728
|
+
retract(quads, view = "complete", options = {}) {
|
|
2729
|
+
const gone = /* @__PURE__ */ new Map();
|
|
2730
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2731
|
+
for (const quad of quads) {
|
|
2732
|
+
const key = tripleKey(quad.subject, quad.predicate, quad.object);
|
|
2733
|
+
const id = `${graphKey(quad.graph)}\0${key}`;
|
|
2734
|
+
if (gone.has(id)) continue;
|
|
2735
|
+
if (this.#graphs.get(graphKey(quad.graph))?.triples.has(key)) {
|
|
2736
|
+
gone.set(id, quad);
|
|
2737
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
if (gone.size === 0) return [];
|
|
2741
|
+
const diffing = this.#mode === "per-graph" && this.#graphs.size > 1;
|
|
2742
|
+
const before = diffing ? this.#listing() : void 0;
|
|
2743
|
+
const lost = [];
|
|
2744
|
+
for (const quad of gone.values()) {
|
|
2745
|
+
const key = tripleKey(quad.subject, quad.predicate, quad.object);
|
|
2746
|
+
if (this.#refs.get(key) === counts.get(key)) lost.push(quad);
|
|
2747
|
+
}
|
|
2748
|
+
const removed = this.#union.retract(lost, diffing ? "complete" : view, options);
|
|
2749
|
+
const labelled = before === void 0 ? this.#label(removed) : [];
|
|
2750
|
+
for (const quad of gone.values()) this.#unregister(quad);
|
|
2751
|
+
if (before === void 0) return labelled;
|
|
2752
|
+
return this.#difference(before, this.#listing(), view);
|
|
2753
|
+
}
|
|
2754
|
+
/**
|
|
2755
|
+
* closure yields every quad of the closure, the axiomatic triples included,
|
|
2756
|
+
* with each `owl:sameAs` class expanded to its members. See
|
|
2757
|
+
* {@link GraphMode} for which graph lists each triple.
|
|
2758
|
+
*/
|
|
2759
|
+
*closure(view = "complete") {
|
|
2760
|
+
if (this.#plain) {
|
|
2761
|
+
yield* this.#union.closure(view);
|
|
2762
|
+
return;
|
|
2763
|
+
}
|
|
2764
|
+
for (const quad of this.#union.closure(view)) yield* this.#label([quad]);
|
|
2765
|
+
}
|
|
2766
|
+
/**
|
|
2767
|
+
* has is `true` when the closure entails the triple. With a `graph`, it is
|
|
2768
|
+
* `true` only when that graph lists the triple. In `"union"` mode the closure
|
|
2769
|
+
* is all in the default graph, so any other `graph` gives `false`.
|
|
2770
|
+
*/
|
|
2771
|
+
has(subject, predicate, object, graph) {
|
|
2772
|
+
if (!this.#union.has(subject, predicate, object)) return false;
|
|
2773
|
+
if (graph === void 0) return true;
|
|
2774
|
+
const key = graphKey(graph);
|
|
2775
|
+
if (this.#plain) return key === DEFAULT_KEY;
|
|
2776
|
+
return this.#labelsOf(tripleKey(subject, predicate, object)).includes(key);
|
|
2777
|
+
}
|
|
2778
|
+
/**
|
|
2779
|
+
* inconsistencies lists every OWL 2 RL `false` rule the closure violates.
|
|
2780
|
+
* Each names the graph whose own closure violates the rule, or
|
|
2781
|
+
* {@link DERIVED_GRAPH} for a rule violated only by combining graphs.
|
|
2782
|
+
*/
|
|
2783
|
+
inconsistencies() {
|
|
2784
|
+
const all = this.#union.inconsistencies();
|
|
2785
|
+
if (this.#plain) {
|
|
2786
|
+
const graph = dataFactory.defaultGraph();
|
|
2787
|
+
return all.map((report) => ({
|
|
2788
|
+
...report,
|
|
2789
|
+
graph
|
|
2790
|
+
}));
|
|
2791
|
+
}
|
|
2792
|
+
if (!this.#multi) {
|
|
2793
|
+
const [{ graph }] = this.#graphs.values();
|
|
2794
|
+
return all.map((report) => ({
|
|
2795
|
+
...report,
|
|
2796
|
+
graph
|
|
2797
|
+
}));
|
|
2798
|
+
}
|
|
2799
|
+
const reports = [];
|
|
2800
|
+
const known = /* @__PURE__ */ new Set();
|
|
2801
|
+
for (const [key, { core }] of this.#perGraphs()) for (const report of core.inconsistencies()) {
|
|
2802
|
+
known.add(report.rule);
|
|
2803
|
+
reports.push({
|
|
2804
|
+
...report,
|
|
2805
|
+
graph: this.#graphTerm(key)
|
|
2806
|
+
});
|
|
2807
|
+
}
|
|
2808
|
+
for (const report of all) if (!known.has(report.rule)) reports.push({
|
|
2809
|
+
...report,
|
|
2810
|
+
graph: DERIVED_GRAPH
|
|
2811
|
+
});
|
|
2812
|
+
return reports;
|
|
2813
|
+
}
|
|
2814
|
+
/** #plain is `true` when every triple is listed in the default graph. */
|
|
2815
|
+
get #plain() {
|
|
2816
|
+
if (this.#mode === "union" || this.#graphs.size === 0) return true;
|
|
2817
|
+
return this.#graphs.size === 1 && this.#graphs.has(DEFAULT_KEY);
|
|
2818
|
+
}
|
|
2819
|
+
/** #multi is `true` when per-graph closures are needed to label triples. */
|
|
2820
|
+
get #multi() {
|
|
2821
|
+
return this.#mode === "per-graph" && this.#graphs.size > 1;
|
|
2822
|
+
}
|
|
2823
|
+
#register(quad) {
|
|
2824
|
+
const key = tripleKey(quad.subject, quad.predicate, quad.object);
|
|
2825
|
+
const gk = graphKey(quad.graph);
|
|
2826
|
+
let entry = this.#graphs.get(gk);
|
|
2827
|
+
if (entry === void 0) {
|
|
2828
|
+
entry = {
|
|
2829
|
+
graph: quad.graph ?? dataFactory.defaultGraph(),
|
|
2830
|
+
triples: /* @__PURE__ */ new Map()
|
|
2831
|
+
};
|
|
2832
|
+
this.#graphs.set(gk, entry);
|
|
2833
|
+
}
|
|
2834
|
+
if (entry.triples.has(key)) return;
|
|
2835
|
+
entry.triples.set(key, quad);
|
|
2836
|
+
this.#refs.set(key, (this.#refs.get(key) ?? 0) + 1);
|
|
2837
|
+
this.#per = void 0;
|
|
2838
|
+
}
|
|
2839
|
+
#unregister(quad) {
|
|
2840
|
+
const key = tripleKey(quad.subject, quad.predicate, quad.object);
|
|
2841
|
+
const gk = graphKey(quad.graph);
|
|
2842
|
+
const entry = this.#graphs.get(gk);
|
|
2843
|
+
entry.triples.delete(key);
|
|
2844
|
+
if (entry.triples.size === 0) this.#graphs.delete(gk);
|
|
2845
|
+
const refs = this.#refs.get(key) - 1;
|
|
2846
|
+
if (refs === 0) this.#refs.delete(key);
|
|
2847
|
+
else this.#refs.set(key, refs);
|
|
2848
|
+
this.#per = void 0;
|
|
2849
|
+
}
|
|
2850
|
+
/** #perGraphs computes the closure of each graph alone, once per change. */
|
|
2851
|
+
#perGraphs() {
|
|
2852
|
+
if (this.#per !== void 0) return this.#per;
|
|
2853
|
+
const per = /* @__PURE__ */ new Map();
|
|
2854
|
+
for (const [key, { triples }] of this.#graphs) {
|
|
2855
|
+
const core = new Core({ rules: this.#union.rules });
|
|
2856
|
+
core.add(triples.values());
|
|
2857
|
+
core.run();
|
|
2858
|
+
const keys = /* @__PURE__ */ new Set();
|
|
2859
|
+
for (const q of core.closure("complete")) keys.add(tripleKey(q.subject, q.predicate, q.object));
|
|
2860
|
+
per.set(key, {
|
|
2861
|
+
core,
|
|
2862
|
+
keys
|
|
2863
|
+
});
|
|
2864
|
+
}
|
|
2865
|
+
return this.#per = per;
|
|
2866
|
+
}
|
|
2867
|
+
#graphTerm(key) {
|
|
2868
|
+
if (key === DEFAULT_KEY) return dataFactory.defaultGraph();
|
|
2869
|
+
if (key === DERIVED_KEY) return DERIVED_GRAPH;
|
|
2870
|
+
return this.#graphs.get(key).graph;
|
|
2871
|
+
}
|
|
2872
|
+
/**
|
|
2873
|
+
* #labelsOf returns the keys of the graphs that list an entailed triple: the
|
|
2874
|
+
* default graph for an axiom, each graph that asserts it or entails it alone,
|
|
2875
|
+
* else the derived graph.
|
|
2876
|
+
*/
|
|
2877
|
+
#labelsOf(key) {
|
|
2878
|
+
const labels = /* @__PURE__ */ new Set();
|
|
2879
|
+
const axiom = base().has(key);
|
|
2880
|
+
if (axiom) labels.add(DEFAULT_KEY);
|
|
2881
|
+
const per = this.#multi ? this.#perGraphs() : void 0;
|
|
2882
|
+
for (const [gk, { triples }] of this.#graphs) {
|
|
2883
|
+
const alone = per === void 0 || per.get(gk).keys.has(key);
|
|
2884
|
+
if (triples.has(key) || alone && !axiom) labels.add(gk);
|
|
2885
|
+
}
|
|
2886
|
+
if (labels.size === 0) labels.add(DERIVED_KEY);
|
|
2887
|
+
return [...labels];
|
|
2888
|
+
}
|
|
2889
|
+
/** #label places each default-graph quad of the closure in its graphs. */
|
|
2890
|
+
#label(quads) {
|
|
2891
|
+
if (this.#plain) return [...quads];
|
|
2892
|
+
const out = [];
|
|
2893
|
+
for (const { subject, predicate, object } of quads) {
|
|
2894
|
+
const key = tripleKey(subject, predicate, object);
|
|
2895
|
+
for (const gk of this.#labelsOf(key)) out.push(generalizedQuad(subject, predicate, object, this.#graphTerm(gk)));
|
|
2896
|
+
}
|
|
2897
|
+
return out;
|
|
2898
|
+
}
|
|
2899
|
+
/** #listing is the complete closure keyed by graph and triple. */
|
|
2900
|
+
#listing() {
|
|
2901
|
+
const listing = /* @__PURE__ */ new Map();
|
|
2902
|
+
for (const quad of this.closure("complete")) {
|
|
2903
|
+
const key = tripleKey(quad.subject, quad.predicate, quad.object);
|
|
2904
|
+
listing.set(`${graphKey(quad.graph)}\0${key}`, quad);
|
|
2905
|
+
}
|
|
2906
|
+
return listing;
|
|
2907
|
+
}
|
|
2908
|
+
/** #difference lists the quads of `from` that `without` lacks, in `view`. */
|
|
2909
|
+
#difference(from, without, view) {
|
|
2910
|
+
const out = [];
|
|
2911
|
+
for (const [key, quad] of from) if (!without.has(key) && shown(quad.subject, quad.predicate, quad.object, view)) out.push(quad);
|
|
2912
|
+
return out;
|
|
2913
|
+
}
|
|
2914
|
+
};
|
|
2915
|
+
export { AXIOMS, COMPLEMENT_PREFIX, DERIVED_GRAPH, Engine, OWL2RL_RULES, Reasoner, Variable, atom, bind, filter, variables };
|