eyemind 0.7.2 → 0.8.1
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/README.md +216 -19
- package/SPEC.md +1 -1
- package/package.json +1 -1
- package/src/cli.js +10 -7
- package/src/formats.js +14 -11
- package/src/owl2rl.js +34 -30
- package/src/qudt.js +109 -0
- package/src/rdf-logic.js +59 -0
- package/src/rdf-values.js +98 -0
- package/src/shacl.js +552 -0
- package/src/skos.js +67 -0
- package/src/version.js +1 -1
package/src/shacl.js
ADDED
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
// SHACL Core shapes compile to separate LS predicates. Each dependency is
|
|
2
|
+
// explicit before LS stratification; a nested sh:not/or/node is not a test
|
|
3
|
+
// against an incompletely materialized generic conformance relation.
|
|
4
|
+
// https://www.w3.org/TR/shacl12-core/
|
|
5
|
+
import { EyemindError, Struct } from './terms.js';
|
|
6
|
+
import { write } from './writer.js';
|
|
7
|
+
import { RDF_DATATYPES } from './rdf-values.js';
|
|
8
|
+
|
|
9
|
+
const SH = 'http://www.w3.org/ns/shacl#';
|
|
10
|
+
const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
11
|
+
const RDFS = 'http://www.w3.org/2000/01/rdf-schema#';
|
|
12
|
+
const XSD = 'http://www.w3.org/2001/XMLSchema#';
|
|
13
|
+
const uri = (u) => new Struct('iri', [u]);
|
|
14
|
+
const iri = (u) => write(uri(u));
|
|
15
|
+
const sh = (n) => iri(SH + n);
|
|
16
|
+
const NONE = "'sh_none'";
|
|
17
|
+
const TARGETS = ['targetNode', 'targetClass', 'targetSubjectsOf', 'targetObjectsOf', 'targetWhere'];
|
|
18
|
+
const CONSTRAINTS = ['class', 'datatype', 'nodeKind', 'minCount', 'maxCount', 'minExclusive', 'minInclusive',
|
|
19
|
+
'maxExclusive', 'maxInclusive', 'minLength', 'maxLength', 'pattern', 'singleLine', 'languageIn', 'uniqueLang',
|
|
20
|
+
'memberShape', 'minListLength', 'maxListLength', 'uniqueMembers', 'equals', 'disjoint', 'subsetOf', 'lessThan',
|
|
21
|
+
'lessThanOrEquals', 'not', 'and', 'or', 'xone', 'node', 'property', 'someValue', 'qualifiedValueShape',
|
|
22
|
+
'qualifiedMinCount', 'qualifiedMaxCount', 'qualifiedValueShapesDisjoint', 'reifierShape', 'reificationRequired',
|
|
23
|
+
'closed', 'hasValue', 'in', 'rootClass', 'uniqueValuesFor'];
|
|
24
|
+
const ANNOTATIONS = ['severity', 'message', 'deactivated', 'name', 'description', 'order', 'group', 'defaultValue', 'unit',
|
|
25
|
+
'intent', 'agentInstruction', 'codeIdentifier', 'codeType'];
|
|
26
|
+
const ALLOWED = new Set([...TARGETS, ...CONSTRAINTS, ...ANNOTATIONS, 'path', 'values', 'flags', 'ignoredProperties']);
|
|
27
|
+
// Shape-expecting parameters, whose values are compiled as shapes and checked for recursion.
|
|
28
|
+
const SHAPE_PARAMETERS = ['node', 'not', 'qualifiedValueShape', 'property', 'memberShape', 'reifierShape', 'someValue', 'targetWhere'];
|
|
29
|
+
const PATH_OPERATORS = ['inversePath', 'alternativePath', 'zeroOrMorePath', 'oneOrMorePath', 'zeroOrOnePath'];
|
|
30
|
+
// The default conformance-disallow set of SHACL 1.2.
|
|
31
|
+
export const SHACL_DISALLOWS = ['Violation', 'Warning', 'Info'].map((n) => SH + n);
|
|
32
|
+
const literal = (t, datatype) => t instanceof Struct && t.name === 'literal' && t.args[1]?.name === 'datatype' && t.args[1].args[0] === datatype;
|
|
33
|
+
const BOOL = (t) => literal(t, XSD + 'boolean') && ['true', 'false', '1', '0'].includes(t.args[0]);
|
|
34
|
+
// Only the boolean true activates a constraint; the suite reads "1" as not true.
|
|
35
|
+
const TRUE = (t) => literal(t, XSD + 'boolean') && t.args[0] === 'true';
|
|
36
|
+
const isIri = (t) => t instanceof Struct && t.name === 'iri';
|
|
37
|
+
const resource = (t) => t instanceof Struct && ['iri', 'bnode', 'triple'].includes(t.name);
|
|
38
|
+
const typeNames = (types) => types.map((t) => t.args?.[0]);
|
|
39
|
+
|
|
40
|
+
export function compileShacl(quads, { disallows = SHACL_DISALLOWS } = {}) {
|
|
41
|
+
const graphs = new Map(), lines = [COMMON], blocked = new Set(disallows);
|
|
42
|
+
for (const severity of disallows) lines.push(`fact(sh_disallowed(${iri(severity)}));`);
|
|
43
|
+
if (disallows.length !== SHACL_DISALLOWS.length || disallows.some((d) => !SHACL_DISALLOWS.includes(d)))
|
|
44
|
+
lines.push('fact(sh_custom_disallows);');
|
|
45
|
+
for (const [s, p, o, g] of quads) {
|
|
46
|
+
const key = write(g);
|
|
47
|
+
if (!graphs.has(key)) graphs.set(key, { graph: g, triples: [] });
|
|
48
|
+
graphs.get(key).triples.push([s, p, o]);
|
|
49
|
+
}
|
|
50
|
+
let nextShape = 0, nextPath = 0, nextConstraint = 0, nextHelper = 0;
|
|
51
|
+
for (const { graph, triples } of graphs.values()) {
|
|
52
|
+
const G = write(graph), index = new Map(), terms = new Map(), reifiers = new Map();
|
|
53
|
+
for (const [s, p, o] of triples) {
|
|
54
|
+
const key = write(s);
|
|
55
|
+
if (!index.has(key)) { index.set(key, new Map()); terms.set(key, s); }
|
|
56
|
+
const properties = index.get(key), name = p.args[0];
|
|
57
|
+
if (!properties.has(name)) properties.set(name, []);
|
|
58
|
+
if (!properties.get(name).some((v) => write(v) === write(o))) properties.get(name).push(o);
|
|
59
|
+
if (name === RDF + 'reifies' && o instanceof Struct && o.name === 'triple') {
|
|
60
|
+
if (!reifiers.has(write(o))) reifiers.set(write(o), []);
|
|
61
|
+
reifiers.get(write(o)).push(s);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const values = (s, p) => index.get(write(s))?.get(p.startsWith('http') ? p : SH + p) ?? [];
|
|
65
|
+
const error = (s, message) => { throw new EyemindError(`--shacl: ${write(s)} ${message}`); };
|
|
66
|
+
const one = (s, p) => {
|
|
67
|
+
const list = values(s, p);
|
|
68
|
+
if (list.length > 1) error(s, `has multiple sh:${p} values`);
|
|
69
|
+
return list[0];
|
|
70
|
+
};
|
|
71
|
+
const rdfList = (root) => {
|
|
72
|
+
const items = [], seen = new Set();
|
|
73
|
+
let at = root;
|
|
74
|
+
while (write(at) !== iri(RDF + 'nil')) {
|
|
75
|
+
if (seen.has(write(at))) error(root, 'contains a cyclic RDF list');
|
|
76
|
+
seen.add(write(at));
|
|
77
|
+
const first = values(at, RDF + 'first'), rest = values(at, RDF + 'rest');
|
|
78
|
+
if (first.length !== 1 || rest.length !== 1) error(root, 'needs a well-formed RDF list');
|
|
79
|
+
items.push(first[0]); at = rest[0];
|
|
80
|
+
}
|
|
81
|
+
return items;
|
|
82
|
+
};
|
|
83
|
+
// sh:class, sh:datatype, sh:nodeKind and sh:rootClass take an IRI or a list of IRIs.
|
|
84
|
+
const iris = (s, property, t) => {
|
|
85
|
+
const items = isIri(t) ? [t] : t.name === 'bnode' ? rdfList(t) : [];
|
|
86
|
+
if (!items.length || !items.every(isIri)) error(s, `needs an IRI or a list of IRIs as sh:${property}`);
|
|
87
|
+
return items;
|
|
88
|
+
};
|
|
89
|
+
const types = (s) => typeNames(values(s, RDF + 'type'));
|
|
90
|
+
const isClass = (s) => types(s).some((t) => t === RDFS + 'Class' || t === SH + 'ShapeClass');
|
|
91
|
+
const isNodeShape = (s) => types(s).some((t) => t === SH + 'NodeShape' || t === SH + 'ShapeClass');
|
|
92
|
+
const shapes = new Map();
|
|
93
|
+
const shape = (s, depth = 0) => {
|
|
94
|
+
if (!resource(s)) error(s, 'is not a shape resource');
|
|
95
|
+
const key = write(s);
|
|
96
|
+
if (shapes.has(key)) return shapes.get(key);
|
|
97
|
+
if (depth >= 128) error(s, 'has shape references nested beyond 128 levels');
|
|
98
|
+
const node = { term: s, id: nextShape++, refs: [], properties: [], lists: new Map(), inactive: TRUE(one(s, 'deactivated')) };
|
|
99
|
+
shapes.set(key, node);
|
|
100
|
+
if (node.inactive) return node;
|
|
101
|
+
for (const p of index.get(key)?.keys() ?? []) if (p.startsWith(SH) && !ALLOWED.has(p.slice(SH.length))) error(s, `uses unsupported SHACL feature <${p}>`);
|
|
102
|
+
for (const p of SHAPE_PARAMETERS) for (const child of values(s, p)) {
|
|
103
|
+
const target = shape(child, depth + 1); node.refs.push(target);
|
|
104
|
+
if (p === 'property') node.properties.push(target);
|
|
105
|
+
}
|
|
106
|
+
for (const p of ['and', 'or', 'xone']) {
|
|
107
|
+
const root = one(s, p);
|
|
108
|
+
if (root) {
|
|
109
|
+
const children = rdfList(root).map((child) => shape(child, depth + 1));
|
|
110
|
+
node.lists.set(p, children); node.refs.push(...children);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return node;
|
|
114
|
+
};
|
|
115
|
+
for (const [s, p, o] of triples) {
|
|
116
|
+
if (p.args[0].startsWith(SH) && [...TARGETS, ...CONSTRAINTS, 'path'].includes(p.args[0].slice(SH.length))) shape(s);
|
|
117
|
+
if (p.args[0] === RDF + 'type' && [SH + 'NodeShape', SH + 'PropertyShape', SH + 'ShapeClass'].includes(o.args?.[0])) shape(s);
|
|
118
|
+
}
|
|
119
|
+
const checked = new Set(), checking = new Set();
|
|
120
|
+
const acyclic = (node) => {
|
|
121
|
+
if (checking.has(node.id)) error(node.term, 'has recursive shape dependencies; recursive SHACL validation is unsupported');
|
|
122
|
+
if (checked.has(node.id)) return;
|
|
123
|
+
checking.add(node.id); node.refs.forEach(acyclic); checking.delete(node.id); checked.add(node.id);
|
|
124
|
+
};
|
|
125
|
+
shapes.forEach(acyclic);
|
|
126
|
+
const paths = new Map(), parsing = new Set();
|
|
127
|
+
const path = (term) => {
|
|
128
|
+
const key = write(term);
|
|
129
|
+
if (parsing.has(key)) error(term, 'has a cyclic SHACL path');
|
|
130
|
+
if (paths.has(key)) return paths.get(key);
|
|
131
|
+
if (parsing.size >= 128) error(term, 'has a SHACL path nested beyond 128 levels');
|
|
132
|
+
parsing.add(key);
|
|
133
|
+
const name = `sh_path_${nextPath++}`;
|
|
134
|
+
if (isIri(term)) lines.push(`implies([rdf(X, ${write(term)}, Y, ${G}), ent_resource(X)], ${name}(X, Y));`);
|
|
135
|
+
else {
|
|
136
|
+
const operators = PATH_OPERATORS.filter((op) => values(term, op).length);
|
|
137
|
+
if (values(term, RDF + 'first').length) {
|
|
138
|
+
if (operators.length) error(term, 'mixes a sequence path with another path operator');
|
|
139
|
+
const steps = rdfList(term);
|
|
140
|
+
if (steps.length < 2) error(term, 'needs at least two members in a sequence path');
|
|
141
|
+
const calls = steps.map((step, i) => `${path(step)}(${i ? 'V' + i : 'X'}, ${i === steps.length - 1 ? 'Y' : 'V' + (i + 1)})`);
|
|
142
|
+
lines.push(`implies([${calls.join(', ')}], ${name}(X, Y));`);
|
|
143
|
+
} else {
|
|
144
|
+
if (operators.length !== 1) error(term, 'needs exactly one supported SHACL path operator');
|
|
145
|
+
const op = operators[0], value = one(term, op);
|
|
146
|
+
if (op === 'alternativePath') {
|
|
147
|
+
const alternatives = rdfList(value);
|
|
148
|
+
if (alternatives.length < 2) error(term, 'needs at least two alternative paths');
|
|
149
|
+
for (const alternative of alternatives) lines.push(`implies(${path(alternative)}(X, Y), ${name}(X, Y));`);
|
|
150
|
+
} else {
|
|
151
|
+
const child = path(value);
|
|
152
|
+
if (op === 'inversePath') lines.push(`implies(${child}(Y, X), ${name}(X, Y));`);
|
|
153
|
+
else {
|
|
154
|
+
if (op !== 'oneOrMorePath') lines.push(`implies(ent_node(X, ${G}), ${name}(X, X));`);
|
|
155
|
+
lines.push(`implies(${child}(X, Y), ${name}(X, Y));`);
|
|
156
|
+
if (op !== 'zeroOrOnePath') lines.push(`implies([${name}(X, M), ${child}(M, Y)], ${name}(X, Y));`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
parsing.delete(key); paths.set(key, name); return name;
|
|
162
|
+
};
|
|
163
|
+
// sh:closed sh:ByTypes permits, for each rdf:type T of a value node, the
|
|
164
|
+
// properties that collectProperties(T) reaches in the shapes graph.
|
|
165
|
+
let byTypes = null;
|
|
166
|
+
const closedByTypes = () => {
|
|
167
|
+
if (byTypes) return byTypes;
|
|
168
|
+
byTypes = 'sh_by_types';
|
|
169
|
+
const targeting = new Map();
|
|
170
|
+
for (const [s, p, o] of triples) if (p.args[0] === SH + 'targetClass') {
|
|
171
|
+
if (!targeting.has(write(o))) targeting.set(write(o), []);
|
|
172
|
+
targeting.get(write(o)).push(s);
|
|
173
|
+
}
|
|
174
|
+
const collect = (s, out, seen) => {
|
|
175
|
+
if (seen.has(write(s))) return;
|
|
176
|
+
seen.add(write(s));
|
|
177
|
+
for (const property of values(s, 'property')) for (const p of values(property, 'path')) if (isIri(p)) out.set(write(p), p);
|
|
178
|
+
if (isClass(s)) {
|
|
179
|
+
for (const o of values(s, RDFS + 'subClassOf')) collect(o, out, seen);
|
|
180
|
+
for (const o of targeting.get(write(s)) ?? []) collect(o, out, seen);
|
|
181
|
+
}
|
|
182
|
+
if (isNodeShape(s)) for (const o of values(s, 'node')) collect(o, out, seen);
|
|
183
|
+
};
|
|
184
|
+
const candidates = new Map(terms);
|
|
185
|
+
for (const [, , o] of triples) if (targeting.has(write(o))) candidates.set(write(o), o);
|
|
186
|
+
for (const [key, s] of candidates) {
|
|
187
|
+
if (!isIri(s)) continue;
|
|
188
|
+
const out = new Map();
|
|
189
|
+
collect(s, out, new Set());
|
|
190
|
+
for (const p of out.keys()) lines.push(`fact(${byTypes}(${key}, ${p}, ${G}));`);
|
|
191
|
+
}
|
|
192
|
+
return byTypes;
|
|
193
|
+
};
|
|
194
|
+
for (const node of shapes.values()) {
|
|
195
|
+
const { term: s, id, inactive } = node, S = write(s), v = `sh_values_${id}`, ok = `sh_ok_${id}`, bad = `sh_bad_${id}`;
|
|
196
|
+
const issue = `sh_issue_${id}`, focus = `sh_focus_${id}`, target = `sh_target_${id}`;
|
|
197
|
+
if (inactive) { lines.push(`fact(${ok}(_));`); continue; }
|
|
198
|
+
const p = one(s, 'path'), P = p ? write(p) : NONE;
|
|
199
|
+
if (p && types(s).includes(SH + 'NodeShape')) error(s, 'is a NodeShape with sh:path');
|
|
200
|
+
if (!p && types(s).includes(SH + 'PropertyShape')) error(s, 'is a PropertyShape without sh:path');
|
|
201
|
+
if (p) lines.push(`implied_by(${v}(F, V), ${path(p)}(F, V));`);
|
|
202
|
+
else lines.push(`implied_by(${v}(F, F), ent_node(F, ${G}));`);
|
|
203
|
+
// Core node expressions are constants: sh:values adds them, and
|
|
204
|
+
// sh:defaultValue supplies them when there is no other value node.
|
|
205
|
+
const constant = (property, t) => {
|
|
206
|
+
if (!isIri(t) && !(t instanceof Struct && t.name === 'literal') && typeof t !== 'bigint') error(s, `needs an IRI or literal sh:${property}`);
|
|
207
|
+
if (!p) error(s, `uses sh:${property} without a property path`);
|
|
208
|
+
return write(t);
|
|
209
|
+
};
|
|
210
|
+
const added = values(s, 'values').map((t) => constant('values', t));
|
|
211
|
+
for (const t of added) lines.push(`implied_by(${v}(F, ${t}), ent_node(F, ${G}));`);
|
|
212
|
+
const fallback = one(s, 'defaultValue');
|
|
213
|
+
if (fallback !== undefined && !added.length)
|
|
214
|
+
lines.push(`implied_by(${v}(F, ${constant('defaultValue', fallback)}), [ent_node(F, ${G}), not(${path(p)}(F, _))]);`);
|
|
215
|
+
// A complex resultPath must remain interpretable in the standalone report.
|
|
216
|
+
const describePath = (term, seen = new Set()) => {
|
|
217
|
+
if (isIri(term) || seen.has(write(term))) return;
|
|
218
|
+
seen.add(write(term));
|
|
219
|
+
for (const [predicate, objects] of index.get(write(term)) ?? []) {
|
|
220
|
+
if (![RDF + 'first', RDF + 'rest', ...PATH_OPERATORS.map((n) => SH + n)].includes(predicate)) continue;
|
|
221
|
+
for (const object of objects) {
|
|
222
|
+
lines.push(`fact(sh_path_statement(${P}, ${write(term)}, ${iri(predicate)}, ${write(object)}, ${G}));`);
|
|
223
|
+
describePath(object, seen);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
if (p) describePath(p);
|
|
228
|
+
const integer = (property, t) => { if (typeof t !== 'bigint' || t < 0n) error(s, `needs a nonnegative integer sh:${property}`); return write(t); };
|
|
229
|
+
const boolean = (property, t) => { if (!BOOL(t)) error(s, `needs a boolean sh:${property}`); return TRUE(t); };
|
|
230
|
+
const requireIri = (property, t) => { if (!isIri(t)) error(s, `needs an IRI sh:${property}`); return write(t); };
|
|
231
|
+
const severity = one(s, 'severity') ?? uri(SH + 'Violation');
|
|
232
|
+
requireIri('severity', severity);
|
|
233
|
+
const messages = values(s, 'message');
|
|
234
|
+
for (const message of messages) if (!(message instanceof Struct && message.name === 'literal')) error(s, 'needs literal sh:message values');
|
|
235
|
+
// A constraint is the set of triples of its parameters. Their reifiers
|
|
236
|
+
// may deactivate it or give it its own severity and messages.
|
|
237
|
+
const constraint = (...pairs) => {
|
|
238
|
+
const annotations = pairs.flatMap(([property, t]) => reifiers.get(write(new Struct('triple', [s, uri(SH + property), t]))) ?? []);
|
|
239
|
+
if (annotations.some((r) => TRUE(one(r, 'deactivated')))) return null;
|
|
240
|
+
const own = annotations.flatMap((r) => values(r, 'severity')), texts = annotations.flatMap((r) => values(r, 'message'));
|
|
241
|
+
if (own.length > 1) error(s, 'has multiple reified sh:severity values for one constraint');
|
|
242
|
+
const level = own[0] ?? severity, cid = `'sh_c_${nextConstraint++}'`;
|
|
243
|
+
requireIri('severity', level);
|
|
244
|
+
lines.push(`fact(sh_constraint(${cid}, ${write(level)}));`);
|
|
245
|
+
for (const message of texts.length ? texts : messages) lines.push(`fact(sh_constraint_message(${cid}, ${write(message)}));`);
|
|
246
|
+
if (blocked.has(level.args[0])) lines.push(`fact(sh_blocking(${cid}));`);
|
|
247
|
+
return cid;
|
|
248
|
+
};
|
|
249
|
+
const emit = (cid, body, component, value = 'V', resultPath = P, key = NONE) => {
|
|
250
|
+
if (cid) lines.push(`implied_by(${issue}(F, ${resultPath}, ${value}, ${sh(component + 'ConstraintComponent')}, ${key}, ${cid}), [${body.join(', ')}]);`);
|
|
251
|
+
};
|
|
252
|
+
const each = (property, fn) => values(s, property).forEach((t) => fn(t, constraint([property, t])));
|
|
253
|
+
const scalar = (property, fn) => { const t = one(s, property); if (t !== undefined) fn(t, constraint([property, t])); };
|
|
254
|
+
const helper = (name) => `sh_${name}_${id}_${nextHelper++}`;
|
|
255
|
+
const deactivated = one(s, 'deactivated');
|
|
256
|
+
if (deactivated !== undefined) boolean('deactivated', deactivated);
|
|
257
|
+
const all = (property, fn) => values(s, property).forEach(fn);
|
|
258
|
+
all('targetNode', (t) => lines.push(`fact(${target}(${write(t)}), ent_node(${write(t)}, ${G}));`));
|
|
259
|
+
all('targetClass', (t) => lines.push(`implies(sh_instance(F, ${requireIri('targetClass', t)}, ${G}), ${target}(F));`));
|
|
260
|
+
all('targetSubjectsOf', (t) => lines.push(`implies([rdf(F, ${requireIri('targetSubjectsOf', t)}, _, ${G}), ent_resource(F)], ${target}(F));`));
|
|
261
|
+
all('targetObjectsOf', (t) => lines.push(`implies([rdf(X, ${requireIri('targetObjectsOf', t)}, F, ${G}), ent_resource(X)], ${target}(F));`));
|
|
262
|
+
all('targetWhere', (t) => lines.push(`implies([sh_graph_node(F, ${G}), sh_ok_${shape(t).id}(F)], ${target}(F));`));
|
|
263
|
+
if (isClass(s)) {
|
|
264
|
+
if (!isIri(s)) error(s, 'is a shape and a class but not an IRI');
|
|
265
|
+
lines.push(`implies(sh_instance(F, ${S}, ${G}), ${target}(F));`);
|
|
266
|
+
}
|
|
267
|
+
if (isIri(s)) lines.push(`implies(rdf(F, ${sh('shape')}, ${S}, ${G}), ${target}(F));`);
|
|
268
|
+
lines.push(`implies(${target}(F), ${focus}(F, 'sh_target'));`);
|
|
269
|
+
each('class', (t, c) => {
|
|
270
|
+
const member = helper('class');
|
|
271
|
+
for (const type of iris(s, 'class', t)) lines.push(`implied_by(${member}(V), sh_instance(V, ${write(type)}, ${G}));`);
|
|
272
|
+
emit(c, [`${v}(F, V)`, `not(${member}(V))`], 'Class', 'V', P, write(t));
|
|
273
|
+
});
|
|
274
|
+
scalar('datatype', (t, c) => {
|
|
275
|
+
const member = helper('datatype');
|
|
276
|
+
for (const type of iris(s, 'datatype', t)) lines.push(RDF_DATATYPES.has(type.args[0]) ?
|
|
277
|
+
`implied_by(${member}(V), rv_valid_as(V, ${write(type)}));` :
|
|
278
|
+
`fact(${member}(literal(_, datatype(${JSON.stringify(type.args[0])}))));`);
|
|
279
|
+
emit(c, [`${v}(F, V)`, `not(${member}(V))`], 'Datatype');
|
|
280
|
+
});
|
|
281
|
+
scalar('nodeKind', (t, c) => {
|
|
282
|
+
const kinds = iris(s, 'nodeKind', t), allowed = isIri(t) ? ['IRI', 'BlankNode', 'Literal', 'BlankNodeOrIRI', 'BlankNodeOrLiteral', 'IRIOrLiteral', 'TripleTerm'] : ['IRI', 'BlankNode', 'Literal', 'TripleTerm'];
|
|
283
|
+
if (!kinds.every((k) => allowed.some((n) => k.args[0] === SH + n))) error(s, 'has an unsupported sh:nodeKind');
|
|
284
|
+
const member = helper('kind');
|
|
285
|
+
for (const kind of kinds) lines.push(`implied_by(${member}(V), sh_kind(V, ${write(kind)}));`);
|
|
286
|
+
emit(c, [`${v}(F, V)`, `not(${member}(V))`], 'NodeKind');
|
|
287
|
+
});
|
|
288
|
+
const collected = [`findall_sorted(V, ${v}(F, V), Values)`, 'ent_count(Values, N)'];
|
|
289
|
+
for (const [name, cmp] of [['minCount', 'lt'], ['maxCount', 'gt']]) scalar(name, (t, c) => {
|
|
290
|
+
if (!p) error(s, `uses sh:${name} without a property path`);
|
|
291
|
+
emit(c, [...collected, `${cmp}(N, ${integer(name, t)})`], name[0].toUpperCase() + name.slice(1), NONE);
|
|
292
|
+
});
|
|
293
|
+
for (const [name, signs] of [['minExclusive', ['>']], ['minInclusive', ['>', '=']], ['maxExclusive', ['<']], ['maxInclusive', ['<', '=']]]) scalar(name, (t, c) => {
|
|
294
|
+
const pass = helper('bound');
|
|
295
|
+
for (const sign of signs) lines.push(`implied_by(${pass}(V), rv_order(V, ${write(t)}, '${sign}'));`);
|
|
296
|
+
emit(c, [`${v}(F, V)`, `not(${pass}(V))`], name[0].toUpperCase() + name.slice(1));
|
|
297
|
+
});
|
|
298
|
+
for (const [name, cmp] of [['minLength', 'lt'], ['maxLength', 'gt']]) scalar(name, (t, c) => {
|
|
299
|
+
const limit = integer(name, t), pass = helper('length');
|
|
300
|
+
lines.push(`implied_by(${pass}(V), [sh_string(V, Text), atom_length(Text, N), not(${cmp}(N, ${limit}))]);`);
|
|
301
|
+
emit(c, [`${v}(F, V)`, `not(${pass}(V))`], name[0].toUpperCase() + name.slice(1));
|
|
302
|
+
});
|
|
303
|
+
scalar('pattern', (t, c) => {
|
|
304
|
+
if (!literal(t, XSD + 'string')) error(s, 'needs an xsd:string sh:pattern');
|
|
305
|
+
const flags = one(s, 'flags');
|
|
306
|
+
if (flags && (!literal(flags, XSD + 'string') || /[^imsx]/.test(flags.args[0]))) error(s, 'has unsupported sh:flags');
|
|
307
|
+
let expression = t.args[0];
|
|
308
|
+
// The LS regex primitive uses ECMAScript Unicode syntax. Translate the
|
|
309
|
+
// portable SHACL flags at compilation, without executing input patterns.
|
|
310
|
+
const f = flags?.args[0] ?? '';
|
|
311
|
+
if (f.includes('x')) expression = extendedPattern(expression);
|
|
312
|
+
if (f.includes('s')) expression = dotAllPattern(expression);
|
|
313
|
+
if (f.includes('m')) error(s, 'uses sh:flags m, which is not supported by the LS regex primitive');
|
|
314
|
+
if (f.includes('i')) expression = caseInsensitivePattern(expression);
|
|
315
|
+
try { new RegExp(expression, 'u'); } catch { error(s, 'has an invalid or unsupported regular expression'); }
|
|
316
|
+
emit(c, [`${v}(F, V)`, `not(sh_pattern(V, ${JSON.stringify(expression)}))`], 'Pattern');
|
|
317
|
+
});
|
|
318
|
+
scalar('singleLine', (t, c) => {
|
|
319
|
+
if (boolean('singleLine', t)) emit(c, [`${v}(F, V)`, 'rv_literal(V)', 'sh_string(V, Text)', `regex_match(${JSON.stringify('[\n\u000b\f\r]')}, Text)`], 'SingleLine');
|
|
320
|
+
});
|
|
321
|
+
scalar('languageIn', (t, c) => {
|
|
322
|
+
const allowed = helper('language');
|
|
323
|
+
for (const tag of rdfList(t)) {
|
|
324
|
+
if (!literal(tag, XSD + 'string')) error(s, 'needs strings in sh:languageIn');
|
|
325
|
+
lines.push(`implied_by(${allowed}(V), [sh_language(V, L), sh_language_matches(L, ${JSON.stringify(tag.args[0].toLowerCase())})]);`);
|
|
326
|
+
}
|
|
327
|
+
emit(c, [`${v}(F, V)`, `not(${allowed}(V))`], 'LanguageIn');
|
|
328
|
+
});
|
|
329
|
+
scalar('uniqueLang', (t, c) => {
|
|
330
|
+
if (!p) error(s, 'uses sh:uniqueLang without a property path');
|
|
331
|
+
if (boolean('uniqueLang', t)) emit(c, [`${v}(F, V)`, `${v}(F, W)`, 'not_identical(V, W)', 'sh_language_key(V, L)', 'sh_language_key(W, L)'], 'UniqueLang', NONE, P, 'L');
|
|
332
|
+
});
|
|
333
|
+
// List constraints read SHACL lists of the data graph; any other value node is a result.
|
|
334
|
+
const lists = (name, c, body) => {
|
|
335
|
+
emit(c, [`${v}(F, V)`, `not(sh_list(V, _, ${G}))`], name);
|
|
336
|
+
emit(c, [`${v}(F, V)`, `sh_list(V, Members, ${G})`, ...body], name);
|
|
337
|
+
};
|
|
338
|
+
each('memberShape', (t, c) => lists('MemberShape', c, ['ent_member(M, Members)', `not(sh_ok_${shape(t).id}(M))`]));
|
|
339
|
+
for (const [name, cmp] of [['minListLength', 'lt'], ['maxListLength', 'gt']]) scalar(name, (t, c) =>
|
|
340
|
+
lists(name[0].toUpperCase() + name.slice(1), c, ['ent_count(Members, N)', `${cmp}(N, ${integer(name, t)})`]));
|
|
341
|
+
scalar('uniqueMembers', (t, c) => { if (boolean('uniqueMembers', t)) lists('UniqueMembers', c, ['sh_duplicate(Members)']); });
|
|
342
|
+
scalar('in', (t, c) => {
|
|
343
|
+
const allowed = helper('in');
|
|
344
|
+
for (const item of rdfList(t)) lines.push(`fact(${allowed}(${write(item)}));`);
|
|
345
|
+
emit(c, [`${v}(F, V)`, `not(${allowed}(V))`], 'In');
|
|
346
|
+
});
|
|
347
|
+
each('hasValue', (t, c) => emit(c, [`ent_node(F, ${G})`, `not(${v}(F, ${write(t)}))`], 'HasValue', NONE, P, write(t)));
|
|
348
|
+
each('rootClass', (t, c) => {
|
|
349
|
+
const member = helper('root');
|
|
350
|
+
for (const root of iris(s, 'rootClass', t)) {
|
|
351
|
+
lines.push(`fact(${member}(${write(root)}));`);
|
|
352
|
+
lines.push(`implied_by(${member}(V), sh_subclass(V, ${write(root)}, ${G}));`);
|
|
353
|
+
}
|
|
354
|
+
emit(c, [`${v}(F, V)`, 'not(unify(V, iri(_)))'], 'RootClass', 'V', P, write(t));
|
|
355
|
+
emit(c, [`${v}(F, V)`, 'unify(V, iri(_))', `not(${member}(V))`], 'RootClass', 'V', P, write(t));
|
|
356
|
+
});
|
|
357
|
+
// Property pairs compare the value nodes with the nodes of another path.
|
|
358
|
+
for (const name of ['equals', 'disjoint', 'subsetOf', 'lessThan', 'lessThanOrEquals']) each(name, (t, c) => {
|
|
359
|
+
const other = path(t), component = name[0].toUpperCase() + name.slice(1);
|
|
360
|
+
if (name.startsWith('lessThan') && !p) error(s, `uses sh:${name} without a property path`);
|
|
361
|
+
if (name === 'equals') {
|
|
362
|
+
emit(c, [`${v}(F, V)`, `not(${other}(F, V))`], component, 'V', P, write(t));
|
|
363
|
+
emit(c, [`${other}(F, V)`, `not(${v}(F, V))`], component, 'V', P, write(t));
|
|
364
|
+
} else if (name === 'disjoint') emit(c, [`${v}(F, V)`, `${other}(F, V)`], component, 'V', P, write(t));
|
|
365
|
+
else if (name === 'subsetOf') emit(c, [`${v}(F, V)`, `not(${other}(F, V))`], component, 'V', P, write(t));
|
|
366
|
+
else {
|
|
367
|
+
// Each value node and other node that do not compare is its own result.
|
|
368
|
+
const ordered = helper('pair');
|
|
369
|
+
lines.push(`implied_by(${ordered}(V, W), rv_order(V, W, '<'));`);
|
|
370
|
+
if (name === 'lessThanOrEquals') lines.push(`implied_by(${ordered}(V, W), rv_order(V, W, '='));`);
|
|
371
|
+
emit(c, [`${v}(F, V)`, `${other}(F, W)`, `not(${ordered}(V, W))`], component, 'V', P, `k(${write(t)}, W)`);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
for (const child of node.properties) {
|
|
375
|
+
if (!child.inactive && !one(child.term, 'path')) error(child.term, 'is referenced by sh:property without sh:path');
|
|
376
|
+
if (!constraint(['property', child.term])) continue;
|
|
377
|
+
lines.push(`implied_by(${bad}(F), [${v}(F, V), not(sh_ok_${child.id}(V))]);`);
|
|
378
|
+
if (!child.inactive) lines.push(`implies([${focus}(F, X), ${v}(F, V)], sh_focus_${child.id}(V, sh_via(${id}, F, X)));`);
|
|
379
|
+
}
|
|
380
|
+
each('node', (t, c) => emit(c, [`${v}(F, V)`, `not(sh_ok_${shape(t).id}(V))`], 'Node', 'V', P, write(t)));
|
|
381
|
+
each('not', (t, c) => emit(c, [`${v}(F, V)`, `sh_ok_${shape(t).id}(V)`], 'Not', 'V', P, write(t)));
|
|
382
|
+
each('someValue', (t, c) => {
|
|
383
|
+
const some = helper('some');
|
|
384
|
+
lines.push(`implied_by(${some}(F), [${v}(F, V), sh_ok_${shape(t).id}(V)]);`);
|
|
385
|
+
emit(c, [`ent_node(F, ${G})`, `not(${some}(F))`], 'SomeValue', NONE, P, write(t));
|
|
386
|
+
});
|
|
387
|
+
for (const [name, children] of node.lists) {
|
|
388
|
+
const c = constraint([name, one(s, name)]);
|
|
389
|
+
if (name === 'and') for (const child of children) emit(c, [`${v}(F, V)`, `not(sh_ok_${child.id}(V))`], 'And');
|
|
390
|
+
if (name === 'or') emit(c, [`${v}(F, V)`, ...children.map((child) => `not(sh_ok_${child.id}(V))`)], 'Or');
|
|
391
|
+
if (name === 'xone') {
|
|
392
|
+
const match = helper('xone');
|
|
393
|
+
children.forEach((child, i) => lines.push(`implied_by(${match}(V, ${i}), sh_ok_${child.id}(V));`));
|
|
394
|
+
emit(c, [`${v}(F, V)`, `findall_sorted(I, ${match}(V, I), Matches)`, 'ent_count(Matches, N)', 'ne(N, 1)'], 'Xone');
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const qualified = one(s, 'qualifiedValueShape');
|
|
398
|
+
if (qualified) {
|
|
399
|
+
const child = shape(qualified), match = helper('qualified'), siblings = new Map();
|
|
400
|
+
const disjoint = one(s, 'qualifiedValueShapesDisjoint');
|
|
401
|
+
if (disjoint && boolean('qualifiedValueShapesDisjoint', disjoint)) for (const parent of shapes.values()) if (parent.properties.includes(node)) {
|
|
402
|
+
for (const sibling of parent.properties) if (sibling !== node) {
|
|
403
|
+
const q = one(sibling.term, 'qualifiedValueShape');
|
|
404
|
+
if (q && write(q) !== write(qualified)) siblings.set(write(q), shape(q));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
lines.push(`implied_by(${match}(F, V), [${v}(F, V), sh_ok_${child.id}(V)${[...siblings.values()].map((sib) => `, not(sh_ok_${sib.id}(V))`).join('')}]);`);
|
|
408
|
+
for (const [name, cmp] of [['qualifiedMinCount', 'lt'], ['qualifiedMaxCount', 'gt']]) {
|
|
409
|
+
const t = one(s, name);
|
|
410
|
+
if (t === undefined) continue;
|
|
411
|
+
if (!p) error(s, `uses sh:${name} without a property path`);
|
|
412
|
+
emit(constraint(['qualifiedValueShape', qualified], [name, t]), [`findall_sorted(V, ${match}(F, V), Matches)`, 'ent_count(Matches, N)', `${cmp}(N, ${integer(name, t)})`], name[0].toUpperCase() + name.slice(1), NONE);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
// Each reifier of a value triple must conform; sh:reificationRequired needs one.
|
|
416
|
+
each('reifierShape', (t, c) => {
|
|
417
|
+
if (!p || !isIri(p)) error(s, 'uses sh:reifierShape without an IRI sh:path');
|
|
418
|
+
emit(c, [`${v}(F, V)`, `rdf(R, ${iri(RDF + 'reifies')}, triple(F, ${P}, V), ${G})`, `not(sh_ok_${shape(t).id}(R))`], 'ReifierShape', 'V', P, 'R');
|
|
419
|
+
const required = one(s, 'reificationRequired');
|
|
420
|
+
if (required !== undefined && boolean('reificationRequired', required))
|
|
421
|
+
emit(constraint(['reifierShape', t], ['reificationRequired', required]), [`${v}(F, V)`, `not(rdf(_, ${iri(RDF + 'reifies')}, triple(F, ${P}, V), ${G}))`], 'ReifierShape');
|
|
422
|
+
});
|
|
423
|
+
each('uniqueValuesFor', (t, c) => {
|
|
424
|
+
const properties = isIri(t) ? [t] : rdfList(t);
|
|
425
|
+
if (!properties.length || !properties.every(isIri)) error(s, 'needs an IRI or a list of IRIs as sh:uniqueValuesFor');
|
|
426
|
+
const key = helper('key'), duplicate = helper('duplicate');
|
|
427
|
+
lines.push(`implied_by(${key}(V, [${properties.map((_, i) => 'L' + i).join(', ')}]), [${properties.map((q, i) => `findall_sorted(X, rdf(V, ${write(q)}, X, ${G}), L${i})`).join(', ')}]);`);
|
|
428
|
+
lines.push(`implied_by(${duplicate}(V), [${key}(V, K), not(unify(K, [${properties.map(() => '[]').join(', ')}])), ${target}(T), not_identical(T, V), ${key}(T, K)]);`);
|
|
429
|
+
emit(c, [`${v}(F, V)`, `${duplicate}(V)`], 'UniqueValuesFor', NONE, P, 'V');
|
|
430
|
+
});
|
|
431
|
+
scalar('closed', (t, c) => {
|
|
432
|
+
const types = isIri(t) && t.args[0] === SH + 'ByTypes';
|
|
433
|
+
if (!types && !boolean('closed', t)) return;
|
|
434
|
+
const allowed = helper('closed'), ignored = one(s, 'ignoredProperties');
|
|
435
|
+
const predicates = [...(types ? [] : node.properties.map((child) => one(child.term, 'path')).filter(isIri)), ...(ignored ? rdfList(ignored) : [])];
|
|
436
|
+
for (const property of predicates) lines.push(`fact(${allowed}(_, ${requireIri('ignoredProperties', property)}));`);
|
|
437
|
+
if (types) {
|
|
438
|
+
lines.push(`fact(${allowed}(_, ${iri(RDF + 'type')}));`);
|
|
439
|
+
lines.push(`implied_by(${allowed}(X, P), [rdf(X, ${iri(RDF + 'type')}, T, ${G}), ${closedByTypes()}(T, P, ${G})]);`);
|
|
440
|
+
}
|
|
441
|
+
emit(c, [`${v}(F, X)`, 'ent_resource(X)', `rdf(X, Predicate, V, ${G})`, `not(${allowed}(X, Predicate))`], 'Closed', 'V', 'Predicate', 'X');
|
|
442
|
+
});
|
|
443
|
+
lines.push(`implied_by(${bad}(F), [${issue}(F, _, _, _, _, C), sh_blocking(C)]);`);
|
|
444
|
+
lines.push(`implied_by(${ok}(F), not(${bad}(F)));`);
|
|
445
|
+
lines.push(`implies([${focus}(F, X), ${issue}(F, P, V, C, K, I)], sh_violation(${S}, F, P, V, C, K, I, X, ${G}));`);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return lines.join('\n') + '\n';
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Flag translation is deliberately conservative: unsupported regex syntax is
|
|
452
|
+
// rejected rather than changing a constraint's meaning silently.
|
|
453
|
+
function extendedPattern(pattern) {
|
|
454
|
+
let out = '', escaped = false, inClass = false;
|
|
455
|
+
for (const c of pattern) {
|
|
456
|
+
if (escaped) { out += '\\' + c; escaped = false; continue; }
|
|
457
|
+
if (c === '\\') { escaped = true; continue; }
|
|
458
|
+
if (c === '[') inClass = true;
|
|
459
|
+
if (c === ']') inClass = false;
|
|
460
|
+
if (inClass || !/[ \t\r\n]/.test(c)) out += c;
|
|
461
|
+
}
|
|
462
|
+
return out + (escaped ? '\\' : '');
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function dotAllPattern(pattern) {
|
|
466
|
+
let out = '', escaped = false, inClass = false;
|
|
467
|
+
for (const c of pattern) {
|
|
468
|
+
if (escaped) { out += '\\' + c; escaped = false; continue; }
|
|
469
|
+
if (c === '\\') { escaped = true; continue; }
|
|
470
|
+
if (c === '[') inClass = true;
|
|
471
|
+
if (c === ']') inClass = false;
|
|
472
|
+
out += c === '.' && !inClass ? '[\\s\\S]' : c;
|
|
473
|
+
}
|
|
474
|
+
return out + (escaped ? '\\' : '');
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function caseInsensitivePattern(pattern) {
|
|
478
|
+
// Expanding literals preserves LS's one regex primitive and its proof semantics.
|
|
479
|
+
// Escaped character classes and non-ASCII case folding require a richer regex
|
|
480
|
+
// operation; reject them instead of weakening a validation constraint.
|
|
481
|
+
if (/[^\x00-\x7F]|\\[pPuxckK0-9]|\(\?(?!:)/.test(pattern)) throw new EyemindError('--shacl: sh:flags i supports ASCII literals and character classes only');
|
|
482
|
+
let out = '', escaped = false, inClass = false;
|
|
483
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
484
|
+
const c = pattern[i];
|
|
485
|
+
if (escaped) { out += '\\' + c; escaped = false; continue; }
|
|
486
|
+
if (c === '\\') { escaped = true; continue; }
|
|
487
|
+
if (c === '[') inClass = true;
|
|
488
|
+
if (c === ']') inClass = false;
|
|
489
|
+
if (/[A-Za-z]/.test(c)) {
|
|
490
|
+
if (inClass && pattern[i + 1] === '-' && /[A-Za-z]/.test(pattern[i + 2] ?? '')) {
|
|
491
|
+
const end = pattern[i + 2]; out += c + '-' + end + (c === c.toLowerCase() ? c.toUpperCase() + '-' + end.toUpperCase() : c.toLowerCase() + '-' + end.toLowerCase()); i += 2;
|
|
492
|
+
} else out += inClass ? c.toLowerCase() + c.toUpperCase() : '[' + c.toLowerCase() + c.toUpperCase() + ']';
|
|
493
|
+
} else out += c;
|
|
494
|
+
}
|
|
495
|
+
return out + (escaped ? '\\' : '');
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const COMMON = String.raw`
|
|
499
|
+
const shN = 'http://www.w3.org/ns/shacl#';
|
|
500
|
+
const shR = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
501
|
+
const shX = 'http://www.w3.org/2001/XMLSchema#';
|
|
502
|
+
implies([rdf(X, iri(shR + 'type'), C, G), ent_resource(X)], sh_instance(X, C, G));
|
|
503
|
+
implies([sh_instance(X, C, G), rdf(C, iri('http://www.w3.org/2000/01/rdf-schema#subClassOf'), D, G)], sh_instance(X, D, G));
|
|
504
|
+
implies(rdf(C, iri('http://www.w3.org/2000/01/rdf-schema#subClassOf'), D, G), sh_subclass(C, D, G));
|
|
505
|
+
implies([sh_subclass(C, D, G), rdf(D, iri('http://www.w3.org/2000/01/rdf-schema#subClassOf'), E, G)], sh_subclass(C, E, G));
|
|
506
|
+
// The nodes of a data graph are its subjects and objects.
|
|
507
|
+
implies(rdf(S, _, O, G), [sh_graph_node(S, G), sh_graph_node(O, G)]);
|
|
508
|
+
fact(sh_kind(iri(_), iri(shN + 'IRI')));
|
|
509
|
+
fact(sh_kind(bnode(_, _), iri(shN + 'BlankNode')), sh_kind(skolem(_, _, _), iri(shN + 'BlankNode')));
|
|
510
|
+
fact(sh_kind(triple(_, _, _), iri(shN + 'TripleTerm')));
|
|
511
|
+
implied_by(sh_kind(T, iri(shN + 'Literal')), rv_literal(T));
|
|
512
|
+
for (const [kind, left, right] of [['BlankNodeOrIRI', 'BlankNode', 'IRI'], ['BlankNodeOrLiteral', 'BlankNode', 'Literal'], ['IRIOrLiteral', 'IRI', 'Literal']]) {
|
|
513
|
+
implied_by(sh_kind(T, iri(shN + kind)), sh_kind(T, iri(shN + left)));
|
|
514
|
+
implied_by(sh_kind(T, iri(shN + kind)), sh_kind(T, iri(shN + right)));
|
|
515
|
+
}
|
|
516
|
+
fact(sh_string(iri(S), S));
|
|
517
|
+
implied_by(sh_string(T, S), [rv_literal(T), srl_str(T, literal(S, datatype(shX + 'string')))]);
|
|
518
|
+
implied_by(sh_pattern(T, Pattern), [sh_string(T, Text), regex_match(Pattern, Text)]);
|
|
519
|
+
implied_by(sh_language(literal(_, lang(L)), Tag), atom_lower(L, Tag));
|
|
520
|
+
implied_by(sh_language(literal(_, lang(L, _)), Tag), atom_lower(L, Tag));
|
|
521
|
+
// sh:uniqueLang distinguishes a base direction as part of the language tag.
|
|
522
|
+
implied_by(sh_language_key(literal(_, lang(L)), Tag), atom_lower(L, Tag));
|
|
523
|
+
implied_by(sh_language_key(literal(_, lang(L, D)), tag(Tag, D)), atom_lower(L, Tag));
|
|
524
|
+
implied_by(sh_language_matches(L, '*'), ne(L, ''));
|
|
525
|
+
fact(sh_language_matches(L, L));
|
|
526
|
+
implied_by(sh_language_matches(L, Prefix), [atom_concat(Prefix, '-', P), atom_concat(P, _, L)]);
|
|
527
|
+
// SHACL lists grow from rdf:nil, so a cycle or a branching cell never forms one.
|
|
528
|
+
implies([ent_graph(G), not(rdf(iri(shR + 'nil'), iri(shR + 'first'), _, G)), not(rdf(iri(shR + 'nil'), iri(shR + 'rest'), _, G))], sh_list(iri(shR + 'nil'), [], G));
|
|
529
|
+
implies([sh_list(R, Members, G), rdf(L, iri(shR + 'rest'), R, G), ent_resource(L), findall(Y, rdf(L, iri(shR + 'rest'), Y, G), [_]),
|
|
530
|
+
findall(X, rdf(L, iri(shR + 'first'), X, G), [X])], sh_list(L, [X, ...Members], G));
|
|
531
|
+
implied_by(sh_duplicate([X, ...T]), ent_member(X, T));
|
|
532
|
+
implied_by(sh_duplicate([_, ...T]), sh_duplicate(T));
|
|
533
|
+
fact(sh_blocking('sh_none'));
|
|
534
|
+
// One report per graph, with conformance decided from the completed results
|
|
535
|
+
// whose severity is in the conformance-disallow set.
|
|
536
|
+
implies(ent_graph(G), sh_report(Report, G));
|
|
537
|
+
implied_by(sh_any(G), [sh_violation(_, _, _, _, _, _, I, _, G), sh_constraint(I, Severity), sh_disallowed(Severity)]);
|
|
538
|
+
implies(sh_report(Report, G), ent_report(Report, iri(shR + 'type'), iri(shN + 'ValidationReport'), G));
|
|
539
|
+
implies([sh_report(Report, G), not(sh_any(G))], ent_report(Report, iri(shN + 'conforms'), literal('true', datatype(shX + 'boolean')), G));
|
|
540
|
+
implies([sh_report(Report, G), sh_any(G)], ent_report(Report, iri(shN + 'conforms'), literal('false', datatype(shX + 'boolean')), G));
|
|
541
|
+
implies([sh_report(Report, G), sh_custom_disallows, sh_disallowed(Severity)], ent_report(Report, iri(shN + 'conformanceDisallows'), Severity, G));
|
|
542
|
+
implies(sh_violation(S, F, P, V, C, K, I, X, G), sh_result(Result, S, F, P, V, C, K, I, X, G));
|
|
543
|
+
implies([sh_result(Result, S, F, P, V, C, K, I, X, G), sh_report(Report, G), sh_constraint(I, Severity)], [
|
|
544
|
+
ent_report(Report, iri(shN + 'result'), Result, G), ent_report(Result, iri(shR + 'type'), iri(shN + 'ValidationResult'), G),
|
|
545
|
+
ent_report(Result, iri(shN + 'focusNode'), F, G), ent_report(Result, iri(shN + 'sourceShape'), S, G),
|
|
546
|
+
ent_report(Result, iri(shN + 'sourceConstraintComponent'), C, G), ent_report(Result, iri(shN + 'resultSeverity'), Severity, G)
|
|
547
|
+
]);
|
|
548
|
+
implies([sh_result(R, S, F, P, V, C, K, I, X, G), not_identical(P, 'sh_none')], ent_report(R, iri(shN + 'resultPath'), P, G));
|
|
549
|
+
implies([sh_result(R, S, F, P, V, C, K, I, X, G), not_identical(V, 'sh_none')], ent_report(R, iri(shN + 'value'), V, G));
|
|
550
|
+
implies([sh_result(R, S, F, P, V, C, K, I, X, G), sh_constraint_message(I, Message)], ent_report(R, iri(shN + 'resultMessage'), Message, G));
|
|
551
|
+
implies([sh_result(_, _, _, Path, _, _, _, _, _, G), sh_path_statement(Path, S, P, O, G)], ent_report(S, P, O, G));
|
|
552
|
+
`;
|
package/src/skos.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Positive SKOS Core entailments, W3C SKOS Reference sections 3–10.
|
|
2
|
+
// https://www.w3.org/TR/skos-reference/ . Integrity constraints and SKOS-XL
|
|
3
|
+
// are outside this profile. In particular, broader and closeMatch are not transitive.
|
|
4
|
+
const SKOS = 'http://www.w3.org/2004/02/skos/core#';
|
|
5
|
+
const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
6
|
+
const RDFS = 'http://www.w3.org/2000/01/rdf-schema#';
|
|
7
|
+
const OWL = 'http://www.w3.org/2002/07/owl#';
|
|
8
|
+
const iri = (s) => `iri(${JSON.stringify(s)})`;
|
|
9
|
+
const k = (s) => iri(SKOS + s);
|
|
10
|
+
const schema = [], facts = [];
|
|
11
|
+
const axiom = (s, p, o) => schema.push(`fact(sk_schema(${s}, ${p}, ${o}));`);
|
|
12
|
+
const sub = (p, q) => { facts.push(`fact(sk_sub(${k(p)}, ${q}));`); axiom(k(p), iri(RDFS + 'subPropertyOf'), q); };
|
|
13
|
+
const domain = (p, c) => { facts.push(`fact(sk_domain(${k(p)}, ${k(c)}));`); axiom(k(p), iri(RDFS + 'domain'), k(c)); };
|
|
14
|
+
const range = (p, c) => { facts.push(`fact(sk_range(${k(p)}, ${k(c)}));`); axiom(k(p), iri(RDFS + 'range'), k(c)); };
|
|
15
|
+
const inverse = (p, q) => { facts.push(`fact(sk_inverse(${k(p)}, ${k(q)}));`); axiom(k(p), iri(OWL + 'inverseOf'), k(q)); };
|
|
16
|
+
for (const c of ['Concept', 'ConceptScheme', 'Collection', 'OrderedCollection']) axiom(k(c), iri(RDF + 'type'), iri(OWL + 'Class'));
|
|
17
|
+
const relations = ['inScheme', 'hasTopConcept', 'topConceptOf', 'semanticRelation', 'broader', 'narrower', 'related',
|
|
18
|
+
'broaderTransitive', 'narrowerTransitive', 'member', 'memberList', 'mappingRelation', 'closeMatch', 'exactMatch', 'broadMatch', 'narrowMatch', 'relatedMatch'];
|
|
19
|
+
for (const p of relations) axiom(k(p), iri(RDF + 'type'), iri(OWL + 'ObjectProperty'));
|
|
20
|
+
for (const p of ['prefLabel', 'altLabel', 'hiddenLabel']) {
|
|
21
|
+
sub(p, iri(RDFS + 'label'));
|
|
22
|
+
axiom(k(p), iri(RDF + 'type'), iri(OWL + 'AnnotationProperty'));
|
|
23
|
+
axiom(k(p), iri(RDFS + 'range'), iri(RDF + 'PlainLiteral'));
|
|
24
|
+
}
|
|
25
|
+
for (const p of ['changeNote', 'definition', 'editorialNote', 'example', 'historyNote', 'scopeNote']) {
|
|
26
|
+
sub(p, k('note')); axiom(k(p), iri(RDF + 'type'), iri(OWL + 'AnnotationProperty'));
|
|
27
|
+
}
|
|
28
|
+
axiom(k('note'), iri(RDF + 'type'), iri(OWL + 'AnnotationProperty'));
|
|
29
|
+
axiom(k('notation'), iri(RDF + 'type'), iri(OWL + 'DatatypeProperty'));
|
|
30
|
+
range('inScheme', 'ConceptScheme'); domain('hasTopConcept', 'ConceptScheme'); range('hasTopConcept', 'Concept');
|
|
31
|
+
sub('topConceptOf', k('inScheme')); inverse('topConceptOf', 'hasTopConcept');
|
|
32
|
+
domain('semanticRelation', 'Concept'); range('semanticRelation', 'Concept');
|
|
33
|
+
for (const p of ['broaderTransitive', 'narrowerTransitive', 'related', 'mappingRelation']) sub(p, k('semanticRelation'));
|
|
34
|
+
sub('broader', k('broaderTransitive')); sub('narrower', k('narrowerTransitive'));
|
|
35
|
+
inverse('broader', 'narrower'); inverse('broaderTransitive', 'narrowerTransitive');
|
|
36
|
+
domain('member', 'Collection'); domain('memberList', 'OrderedCollection');
|
|
37
|
+
axiom(k('OrderedCollection'), iri(RDFS + 'subClassOf'), k('Collection'));
|
|
38
|
+
axiom(k('memberList'), iri(RDFS + 'range'), iri(RDF + 'List'));
|
|
39
|
+
axiom(k('memberList'), iri(RDF + 'type'), iri(OWL + 'FunctionalProperty'));
|
|
40
|
+
sub('closeMatch', k('mappingRelation')); sub('exactMatch', k('closeMatch'));
|
|
41
|
+
for (const [p, q] of [['broadMatch', 'broader'], ['narrowMatch', 'narrower'], ['relatedMatch', 'related']]) {
|
|
42
|
+
sub(p, k('mappingRelation')); sub(p, k(q));
|
|
43
|
+
}
|
|
44
|
+
inverse('broadMatch', 'narrowMatch');
|
|
45
|
+
for (const p of ['related', 'relatedMatch', 'closeMatch', 'exactMatch']) {
|
|
46
|
+
facts.push(`fact(sk_symmetric(${k(p)}));`); axiom(k(p), iri(RDF + 'type'), iri(OWL + 'SymmetricProperty'));
|
|
47
|
+
}
|
|
48
|
+
for (const p of ['broaderTransitive', 'narrowerTransitive', 'exactMatch']) {
|
|
49
|
+
facts.push(`fact(sk_transitive(${k(p)}));`); axiom(k(p), iri(RDF + 'type'), iri(OWL + 'TransitiveProperty'));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const skosRules = '\n' + [...schema, ...facts].join('\n') + String.raw`
|
|
53
|
+
implies([ent_graph(G), sk_schema(S, P, O)], rdf(S, P, O, G));
|
|
54
|
+
implies([rdf(S, P, O, G), sk_sub(P, Q)], rdf(S, Q, O, G));
|
|
55
|
+
implies([rdf(S, P, O, G), sk_domain(P, C)], rdf(S, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), C, G));
|
|
56
|
+
implies([rdf(S, P, O, G), sk_range(P, C)], rdf(O, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), C, G));
|
|
57
|
+
implies([rdf(S, P, O, G), sk_inverse(P, Q)], rdf(O, Q, S, G));
|
|
58
|
+
implies([rdf(S, Q, O, G), sk_inverse(P, Q)], rdf(O, P, S, G));
|
|
59
|
+
implies([rdf(S, P, O, G), sk_symmetric(P)], rdf(O, P, S, G));
|
|
60
|
+
implies([rdf(S, P, M, G), sk_transitive(P), rdf(M, P, O, G)], rdf(S, P, O, G));
|
|
61
|
+
implies(rdf(S, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), iri('http://www.w3.org/2004/02/skos/core#OrderedCollection'), G), rdf(S, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), iri('http://www.w3.org/2004/02/skos/core#Collection'), G));
|
|
62
|
+
implies([ent_graph(G)], sk_list(iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'), G));
|
|
63
|
+
implies([rdf(L, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), X, G), rdf(L, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), T, G), sk_list(T, G)], sk_list(L, G));
|
|
64
|
+
implies([sk_list(L, G), rdf(L, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), X, G)], sk_member(L, X, G));
|
|
65
|
+
implies([sk_list(L, G), rdf(L, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), T, G), sk_member(T, X, G)], sk_member(L, X, G));
|
|
66
|
+
implies([rdf(C, iri('http://www.w3.org/2004/02/skos/core#memberList'), L, G), sk_member(L, X, G)], rdf(C, iri('http://www.w3.org/2004/02/skos/core#member'), X, G));
|
|
67
|
+
`;
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// The release this checkout is; ./release keeps it in step with package.json.
|
|
2
|
-
export const VERSION = '0.
|
|
2
|
+
export const VERSION = '0.8.1';
|