@shaclmate/shacl-ast 4.0.14 → 4.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AbstractShapesGraph.d.ts +75 -0
- package/dist/AbstractShapesGraph.js +380 -0
- package/dist/ShapesGraph.d.ts +21 -74
- package/dist/ShapesGraph.js +36 -319
- package/dist/generated.d.ts +190 -46
- package/dist/generated.js +1387 -98
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +3 -9
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type PrefixMap from "@rdfjs/prefix-map/PrefixMap.js";
|
|
2
|
+
import TermMap from "@rdfjs/term-map";
|
|
3
|
+
import type { BlankNode, DatasetCore, NamedNode } from "@rdfjs/types";
|
|
4
|
+
import { Resource, ResourceSet } from "@rdfx/resource";
|
|
5
|
+
import { Either } from "purify-ts";
|
|
6
|
+
import type * as generated from "./generated.js";
|
|
7
|
+
export declare abstract class AbstractShapesGraph<NodeShapeT extends generated.NodeShape, OntologyT extends generated.Ontology, PropertyGroupT extends generated.PropertyGroup, PropertyShapeT extends generated.PropertyShape> {
|
|
8
|
+
private readonly nodeShapesByIdentifier;
|
|
9
|
+
private readonly ontologiesByIdentifier;
|
|
10
|
+
private readonly propertyGroupsByIdentifier;
|
|
11
|
+
private readonly propertyShapesByIdentifier;
|
|
12
|
+
protected abstract readonly typeFunctions: {
|
|
13
|
+
NodeShape: TypeFunctions<NodeShapeT>;
|
|
14
|
+
Ontology: TypeFunctions<OntologyT>;
|
|
15
|
+
PropertyGroup: TypeFunctions<PropertyGroupT>;
|
|
16
|
+
PropertyShape: TypeFunctions<PropertyShapeT>;
|
|
17
|
+
};
|
|
18
|
+
constructor(parameters: {
|
|
19
|
+
nodeShapesByIdentifier: IdentifierMap<NodeShapeT>;
|
|
20
|
+
ontologiesByIdentifier: IdentifierMap<OntologyT>;
|
|
21
|
+
propertyGroupsByIdentifier: IdentifierMap<PropertyGroupT>;
|
|
22
|
+
propertyShapesByIdentifier: IdentifierMap<PropertyShapeT>;
|
|
23
|
+
});
|
|
24
|
+
get nodeShapes(): readonly NodeShapeT[];
|
|
25
|
+
get ontologies(): readonly OntologyT[];
|
|
26
|
+
get propertyGroups(): readonly PropertyGroupT[];
|
|
27
|
+
get propertyShapes(): readonly PropertyShapeT[];
|
|
28
|
+
nodeShape(identifier: BlankNode | NamedNode): Either<Error, NodeShapeT>;
|
|
29
|
+
ontology(identifier: BlankNode | NamedNode): Either<Error, OntologyT>;
|
|
30
|
+
propertyGroup(identifier: BlankNode | NamedNode): Either<Error, PropertyGroupT>;
|
|
31
|
+
propertyShape(identifier: BlankNode | NamedNode): Either<Error, PropertyShapeT>;
|
|
32
|
+
shape(identifier: BlankNode | NamedNode): Either<Error, NodeShapeT | PropertyShapeT>;
|
|
33
|
+
/**
|
|
34
|
+
* Convert the shapes graph to a dataset.
|
|
35
|
+
*/
|
|
36
|
+
toDataset(): DatasetCore;
|
|
37
|
+
/**
|
|
38
|
+
* Convert the shapes graph to an RDF string.
|
|
39
|
+
*
|
|
40
|
+
* If the format isn't specified, defaults to N-Triples.
|
|
41
|
+
*/
|
|
42
|
+
toString(options?: {
|
|
43
|
+
format?: "application/n-triples" | "application/n-quads";
|
|
44
|
+
}): string;
|
|
45
|
+
}
|
|
46
|
+
type IdentifierMap<T> = TermMap<BlankNode | NamedNode, T>;
|
|
47
|
+
type TypeFunctions<T> = {
|
|
48
|
+
$fromRdfResource: (resource: Resource, options?: {
|
|
49
|
+
ignoreRdfType?: boolean;
|
|
50
|
+
}) => Either<Error, T>;
|
|
51
|
+
$toRdfResource: (value: T, options?: {
|
|
52
|
+
resourceSet?: ResourceSet;
|
|
53
|
+
}) => Resource;
|
|
54
|
+
};
|
|
55
|
+
export declare namespace AbstractShapesGraph {
|
|
56
|
+
abstract class AbstractBuilder<NodeShapeT extends generated.NodeShape, OntologyT extends generated.Ontology, PropertyGroupT extends generated.PropertyGroup, PropertyShapeT extends generated.PropertyShape> {
|
|
57
|
+
protected readonly nodeShapesByIdentifier: IdentifierMap<NodeShapeT>;
|
|
58
|
+
protected readonly ontologiesByIdentifier: IdentifierMap<OntologyT>;
|
|
59
|
+
protected readonly propertyGroupsByIdentifier: IdentifierMap<PropertyGroupT>;
|
|
60
|
+
protected readonly propertyShapesByIdentifier: IdentifierMap<PropertyShapeT>;
|
|
61
|
+
protected abstract readonly typeFunctions: {
|
|
62
|
+
NodeShape: TypeFunctions<NodeShapeT>;
|
|
63
|
+
Ontology: TypeFunctions<OntologyT>;
|
|
64
|
+
PropertyGroup: TypeFunctions<PropertyGroupT>;
|
|
65
|
+
PropertyShape: TypeFunctions<PropertyShapeT>;
|
|
66
|
+
};
|
|
67
|
+
add(...objects: readonly (NodeShapeT | OntologyT | PropertyGroupT | PropertyShapeT)[]): this;
|
|
68
|
+
parseDataset(dataset: DatasetCore, options?: {
|
|
69
|
+
ignoreUndefinedShapes?: boolean;
|
|
70
|
+
prefixMap?: PrefixMap;
|
|
71
|
+
}): Either<Error, this>;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export {};
|
|
75
|
+
//# sourceMappingURL=AbstractShapesGraph.d.ts.map
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import DataFactory from "@rdfjs/data-model";
|
|
2
|
+
import DatasetFactory from "@rdfjs/dataset";
|
|
3
|
+
import TermMap from "@rdfjs/term-map";
|
|
4
|
+
import TermSet from "@rdfjs/term-set";
|
|
5
|
+
import { Resource, ResourceSet } from "@rdfx/resource";
|
|
6
|
+
import { owl, sh } from "@tpluscode/rdf-ns-builders";
|
|
7
|
+
import { Either, Left } from "purify-ts";
|
|
8
|
+
import { CurieFactory } from "./CurieFactory.js";
|
|
9
|
+
export class AbstractShapesGraph {
|
|
10
|
+
nodeShapesByIdentifier;
|
|
11
|
+
ontologiesByIdentifier;
|
|
12
|
+
propertyGroupsByIdentifier;
|
|
13
|
+
propertyShapesByIdentifier;
|
|
14
|
+
constructor(parameters) {
|
|
15
|
+
// Defensive copies
|
|
16
|
+
this.nodeShapesByIdentifier = new TermMap([
|
|
17
|
+
...parameters.nodeShapesByIdentifier.entries(),
|
|
18
|
+
]);
|
|
19
|
+
this.ontologiesByIdentifier = new TermMap([
|
|
20
|
+
...parameters.ontologiesByIdentifier.entries(),
|
|
21
|
+
]);
|
|
22
|
+
this.propertyGroupsByIdentifier = new TermMap([
|
|
23
|
+
...parameters.propertyGroupsByIdentifier.entries(),
|
|
24
|
+
]);
|
|
25
|
+
this.propertyShapesByIdentifier = new TermMap([
|
|
26
|
+
...parameters.propertyShapesByIdentifier.entries(),
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
get nodeShapes() {
|
|
30
|
+
return [...this.nodeShapesByIdentifier.values()];
|
|
31
|
+
}
|
|
32
|
+
get ontologies() {
|
|
33
|
+
return [...this.ontologiesByIdentifier.values()];
|
|
34
|
+
}
|
|
35
|
+
get propertyGroups() {
|
|
36
|
+
return [...this.propertyGroupsByIdentifier.values()];
|
|
37
|
+
}
|
|
38
|
+
get propertyShapes() {
|
|
39
|
+
return [...this.propertyShapesByIdentifier.values()];
|
|
40
|
+
}
|
|
41
|
+
nodeShape(identifier) {
|
|
42
|
+
const nodeShape = this.nodeShapesByIdentifier.get(identifier);
|
|
43
|
+
return nodeShape
|
|
44
|
+
? Either.of(nodeShape)
|
|
45
|
+
: Left(new Error(`no such node shape ${Resource.Identifier.toString(identifier)}`));
|
|
46
|
+
}
|
|
47
|
+
ontology(identifier) {
|
|
48
|
+
const ontology = this.ontologiesByIdentifier.get(identifier);
|
|
49
|
+
return ontology
|
|
50
|
+
? Either.of(ontology)
|
|
51
|
+
: Left(new Error(`no such ontology ${Resource.Identifier.toString(identifier)}`));
|
|
52
|
+
}
|
|
53
|
+
propertyGroup(identifier) {
|
|
54
|
+
const propertyGroup = this.propertyGroupsByIdentifier.get(identifier);
|
|
55
|
+
return propertyGroup
|
|
56
|
+
? Either.of(propertyGroup)
|
|
57
|
+
: Left(new Error(`no such property group ${Resource.Identifier.toString(identifier)}`));
|
|
58
|
+
}
|
|
59
|
+
propertyShape(identifier) {
|
|
60
|
+
const propertyShape = this.propertyShapesByIdentifier.get(identifier);
|
|
61
|
+
return propertyShape
|
|
62
|
+
? Either.of(propertyShape)
|
|
63
|
+
: Left(new Error(`no such property shape ${Resource.Identifier.toString(identifier)}`));
|
|
64
|
+
}
|
|
65
|
+
shape(identifier) {
|
|
66
|
+
return this.nodeShape(identifier).alt(this.propertyShape(identifier));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Convert the shapes graph to a dataset.
|
|
70
|
+
*/
|
|
71
|
+
toDataset() {
|
|
72
|
+
const dataset = DatasetFactory.dataset();
|
|
73
|
+
const resourceSet = new ResourceSet(dataset);
|
|
74
|
+
for (const nodeShape of this.nodeShapes) {
|
|
75
|
+
this.typeFunctions.NodeShape.$toRdfResource(nodeShape, { resourceSet });
|
|
76
|
+
}
|
|
77
|
+
for (const ontology of this.ontologies) {
|
|
78
|
+
this.typeFunctions.Ontology.$toRdfResource(ontology, { resourceSet });
|
|
79
|
+
}
|
|
80
|
+
for (const propertyGroup of this.propertyGroups) {
|
|
81
|
+
this.typeFunctions.PropertyGroup.$toRdfResource(propertyGroup, {
|
|
82
|
+
resourceSet,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
for (const propertyShape of this.propertyShapes) {
|
|
86
|
+
this.typeFunctions.PropertyShape.$toRdfResource(propertyShape, {
|
|
87
|
+
resourceSet,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return dataset;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Convert the shapes graph to an RDF string.
|
|
94
|
+
*
|
|
95
|
+
* If the format isn't specified, defaults to N-Triples.
|
|
96
|
+
*/
|
|
97
|
+
toString(options) {
|
|
98
|
+
const format = options?.format ?? "application/n-triples";
|
|
99
|
+
function termToString(term) {
|
|
100
|
+
switch (term.termType) {
|
|
101
|
+
case "NamedNode":
|
|
102
|
+
return `<${term.value}>`;
|
|
103
|
+
case "BlankNode":
|
|
104
|
+
return `_:${term.value}`;
|
|
105
|
+
case "Literal": {
|
|
106
|
+
const escaped = term.value
|
|
107
|
+
.replace(/\\/g, "\\\\")
|
|
108
|
+
.replace(/"/g, '\\"')
|
|
109
|
+
.replace(/\n/g, "\\n")
|
|
110
|
+
.replace(/\r/g, "\\r");
|
|
111
|
+
if (term.language)
|
|
112
|
+
return `"${escaped}"@${term.language}`;
|
|
113
|
+
if (term.datatype.value !== "http://www.w3.org/2001/XMLSchema#string")
|
|
114
|
+
return `"${escaped}"^^<${term.datatype.value}>`;
|
|
115
|
+
return `"${escaped}"`;
|
|
116
|
+
}
|
|
117
|
+
default:
|
|
118
|
+
throw new Error(`unexpected term type: ${term.termType}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const lines = [];
|
|
122
|
+
switch (format) {
|
|
123
|
+
case "application/n-quads": {
|
|
124
|
+
for (const quad of this.toDataset()) {
|
|
125
|
+
const graphString = quad.graph.termType === "DefaultGraph"
|
|
126
|
+
? ""
|
|
127
|
+
: ` ${termToString(quad.graph)}`;
|
|
128
|
+
lines.push(`${termToString(quad.subject)} ${termToString(quad.predicate)} ${termToString(quad.object)}${graphString} .\n`);
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case "application/n-triples":
|
|
133
|
+
{
|
|
134
|
+
for (const quad of this.toDataset()) {
|
|
135
|
+
lines.push(`${termToString(quad.subject)} ${termToString(quad.predicate)} ${termToString(quad.object)} .\n`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
return lines.join("");
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
(function (AbstractShapesGraph) {
|
|
144
|
+
class AbstractBuilder {
|
|
145
|
+
nodeShapesByIdentifier = new TermMap();
|
|
146
|
+
ontologiesByIdentifier = new TermMap();
|
|
147
|
+
propertyGroupsByIdentifier = new TermMap();
|
|
148
|
+
propertyShapesByIdentifier = new TermMap();
|
|
149
|
+
add(...objects) {
|
|
150
|
+
for (const object of objects) {
|
|
151
|
+
switch (object.$type) {
|
|
152
|
+
case "NodeShape":
|
|
153
|
+
this.nodeShapesByIdentifier.set(object.$identifier, object);
|
|
154
|
+
break;
|
|
155
|
+
case "Ontology":
|
|
156
|
+
this.ontologiesByIdentifier.set(object.$identifier, object);
|
|
157
|
+
break;
|
|
158
|
+
case "PropertyGroup":
|
|
159
|
+
this.propertyGroupsByIdentifier.set(object.$identifier, object);
|
|
160
|
+
break;
|
|
161
|
+
case "PropertyShape":
|
|
162
|
+
this.propertyShapesByIdentifier.set(object.$identifier, object);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
parseDataset(dataset, options) {
|
|
169
|
+
function datasetHasMatch(subject, predicate, object, graph) {
|
|
170
|
+
for (const _ of dataset.match(subject, predicate, object, graph)) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
let curieDataset;
|
|
176
|
+
if (options?.prefixMap) {
|
|
177
|
+
const curieCache = new Map();
|
|
178
|
+
curieDataset = DatasetFactory.dataset();
|
|
179
|
+
const curieFactory = new CurieFactory({
|
|
180
|
+
prefixMap: options.prefixMap,
|
|
181
|
+
});
|
|
182
|
+
const termToCurie = (term) => {
|
|
183
|
+
if (term.termType !== "NamedNode") {
|
|
184
|
+
return term;
|
|
185
|
+
}
|
|
186
|
+
const cachedCurie = curieCache.get(term.value);
|
|
187
|
+
if (cachedCurie) {
|
|
188
|
+
return cachedCurie;
|
|
189
|
+
}
|
|
190
|
+
const curie = curieFactory.create(term).extract() ?? term;
|
|
191
|
+
curieCache.set(term.value, curie);
|
|
192
|
+
return curie;
|
|
193
|
+
};
|
|
194
|
+
for (const quad of dataset) {
|
|
195
|
+
const curieObject = termToCurie(quad.object);
|
|
196
|
+
const curieSubject = termToCurie(quad.subject);
|
|
197
|
+
if (!Object.is(curieObject, quad.object) ||
|
|
198
|
+
!Object.is(curieSubject, quad.subject)) {
|
|
199
|
+
curieDataset.add(DataFactory.quad(curieSubject, quad.predicate, curieObject, quad.graph));
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
curieDataset.add(quad);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
curieDataset = dataset;
|
|
208
|
+
}
|
|
209
|
+
const curieResourceSet = new ResourceSet(curieDataset);
|
|
210
|
+
return Either.encase(() => {
|
|
211
|
+
function readGraph() {
|
|
212
|
+
const graphs = new TermSet();
|
|
213
|
+
for (const quad of dataset) {
|
|
214
|
+
graphs.add(quad.graph);
|
|
215
|
+
}
|
|
216
|
+
if (graphs.size !== 1) {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
const graph = [...graphs.values()][0];
|
|
220
|
+
switch (graph.termType) {
|
|
221
|
+
case "BlankNode":
|
|
222
|
+
case "DefaultGraph":
|
|
223
|
+
case "NamedNode":
|
|
224
|
+
return graph;
|
|
225
|
+
default:
|
|
226
|
+
throw new RangeError(`expected NamedNode or default graph, actual ${graph.termType}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const graph = readGraph();
|
|
230
|
+
// Read ontologies
|
|
231
|
+
for (const ontologyResource of curieResourceSet.instancesOf(owl.Ontology, {
|
|
232
|
+
graph,
|
|
233
|
+
})) {
|
|
234
|
+
if (this.ontologiesByIdentifier.has(ontologyResource.identifier)) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
this.typeFunctions.Ontology.$fromRdfResource(ontologyResource, {
|
|
238
|
+
ignoreRdfType: true,
|
|
239
|
+
}).ifRight((ontology) => this.add(ontology));
|
|
240
|
+
}
|
|
241
|
+
// Read property groups
|
|
242
|
+
for (const propertyGroupResource of curieResourceSet.instancesOf(sh.PropertyGroup, { graph })) {
|
|
243
|
+
if (propertyGroupResource.identifier.termType !== "NamedNode") {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (this.propertyGroupsByIdentifier.has(propertyGroupResource.identifier)) {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
this.typeFunctions.PropertyGroup.$fromRdfResource(curieResourceSet.resource(propertyGroupResource.identifier), { ignoreRdfType: true }).ifRight((propertyGroup) => this.add(propertyGroup));
|
|
250
|
+
}
|
|
251
|
+
// Read shapes
|
|
252
|
+
// Collect the shape identifiers in sets
|
|
253
|
+
const shapeNodeSet = new TermSet();
|
|
254
|
+
// Utility function for adding to the shapeNodeSet
|
|
255
|
+
function addShapeNode(shapeNode) {
|
|
256
|
+
switch (shapeNode.termType) {
|
|
257
|
+
case "BlankNode":
|
|
258
|
+
case "NamedNode":
|
|
259
|
+
shapeNodeSet.add(shapeNode);
|
|
260
|
+
return true;
|
|
261
|
+
default:
|
|
262
|
+
throw new RangeError(`unexpected shape node identifier term type: ${shapeNode.termType}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// Test each shape condition
|
|
266
|
+
// https://www.w3.org/TR/shacl/#shapes
|
|
267
|
+
// Subject is a SHACL instance of sh:NodeShape or sh:PropertyShape
|
|
268
|
+
for (const rdfType of [sh.NodeShape, sh.PropertyShape]) {
|
|
269
|
+
for (const resource of curieResourceSet.instancesOf(rdfType, {
|
|
270
|
+
graph,
|
|
271
|
+
})) {
|
|
272
|
+
addShapeNode(resource.identifier);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
// Subject of a triple with sh:targetClass, sh:targetNode, sh:targetObjectsOf, or sh:targetSubjectsOf predicate
|
|
276
|
+
for (const predicate of [
|
|
277
|
+
sh.targetClass,
|
|
278
|
+
sh.targetNode,
|
|
279
|
+
sh.targetObjectsOf,
|
|
280
|
+
sh.targetSubjectsOf,
|
|
281
|
+
]) {
|
|
282
|
+
for (const quad of dataset.match(null, predicate, null, graph)) {
|
|
283
|
+
addShapeNode(quad.subject);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Subject of a triple that has a parameter as predicate
|
|
287
|
+
// https://www.w3.org/TR/shacl/#constraints
|
|
288
|
+
// https://www.w3.org/TR/shacl/#core-components
|
|
289
|
+
for (const predicate of [
|
|
290
|
+
sh.class,
|
|
291
|
+
sh.datatype,
|
|
292
|
+
sh.nodeKind,
|
|
293
|
+
sh.minCount,
|
|
294
|
+
sh.maxCount,
|
|
295
|
+
sh.minExclusive,
|
|
296
|
+
sh.minInclusive,
|
|
297
|
+
sh.maxExclusive,
|
|
298
|
+
sh.maxInclusive,
|
|
299
|
+
sh.minLength,
|
|
300
|
+
sh.maxLength,
|
|
301
|
+
sh.pattern,
|
|
302
|
+
sh.languageIn,
|
|
303
|
+
sh.uniqueLang,
|
|
304
|
+
sh.equals,
|
|
305
|
+
sh.disjoint,
|
|
306
|
+
sh.lessThan,
|
|
307
|
+
sh.lessThanOrEquals,
|
|
308
|
+
sh.not,
|
|
309
|
+
sh.and,
|
|
310
|
+
sh.or,
|
|
311
|
+
sh.xone,
|
|
312
|
+
sh.node,
|
|
313
|
+
sh.property,
|
|
314
|
+
sh.qualifiedValueShape,
|
|
315
|
+
sh.qualifiedMinCount,
|
|
316
|
+
sh.qualifiedMaxCount,
|
|
317
|
+
sh.closed,
|
|
318
|
+
sh.ignoredProperties,
|
|
319
|
+
sh.hasValue,
|
|
320
|
+
sh.in,
|
|
321
|
+
]) {
|
|
322
|
+
for (const quad of dataset.match(null, predicate, null, graph)) {
|
|
323
|
+
addShapeNode(quad.subject);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// Object of a shape-expecting, non-list-taking parameter such as sh:node
|
|
327
|
+
for (const predicate of [sh.node, sh.not, sh.property]) {
|
|
328
|
+
for (const quad of dataset.match(null, predicate, null, graph)) {
|
|
329
|
+
addShapeNode(quad.object);
|
|
330
|
+
if (!options?.ignoreUndefinedShapes &&
|
|
331
|
+
!datasetHasMatch(quad.object)) {
|
|
332
|
+
throw new Error(`undefined shape: ${Resource.Identifier.toString(quad.object)}`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// Member of a SHACL list that is a value of a shape-expecting and list-taking parameter such as sh:or
|
|
337
|
+
for (const predicate of [sh.and, sh.or, sh.xone]) {
|
|
338
|
+
for (const quad of dataset.match(null, predicate, null, graph)) {
|
|
339
|
+
switch (quad.object.termType) {
|
|
340
|
+
case "BlankNode":
|
|
341
|
+
case "NamedNode":
|
|
342
|
+
break;
|
|
343
|
+
default:
|
|
344
|
+
throw new RangeError(`expected list term to be a blank or named node, not ${quad.object.termType}`);
|
|
345
|
+
}
|
|
346
|
+
for (const value of curieResourceSet
|
|
347
|
+
.resource(quad.object)
|
|
348
|
+
.toList()
|
|
349
|
+
.unsafeCoerce()) {
|
|
350
|
+
const identifier = value.toIdentifier().unsafeCoerce();
|
|
351
|
+
addShapeNode(identifier);
|
|
352
|
+
if (!options?.ignoreUndefinedShapes &&
|
|
353
|
+
!datasetHasMatch(identifier)) {
|
|
354
|
+
throw new Error(`undefined shape: ${Resource.Identifier.toString(identifier)}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
// Separate shapes into node and property shapes.
|
|
360
|
+
for (const shapeNode of shapeNodeSet) {
|
|
361
|
+
if (dataset.match(shapeNode, sh.path, null, graph).size > 0) {
|
|
362
|
+
// A property shape is a shape in the shapes graph that is the subject of a triple that has sh:path as its predicate. A shape has at most one value for sh:path. Each value of sh:path in a shape must be a well-formed SHACL property path. It is recommended, but not required, for a property shape to be declared as a SHACL instance of sh:PropertyShape. SHACL instances of sh:PropertyShape have one value for the property sh:path.
|
|
363
|
+
this.add(this.typeFunctions.PropertyShape.$fromRdfResource(curieResourceSet.resource(shapeNode), {
|
|
364
|
+
ignoreRdfType: true,
|
|
365
|
+
}).unsafeCoerce());
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
// A node shape is a shape in the shapes graph that is not the subject of a triple with sh:path as its predicate. It is recommended, but not required, for a node shape to be declared as a SHACL instance of sh:NodeShape. SHACL instances of sh:NodeShape cannot have a value for the property sh:path.
|
|
369
|
+
this.add(this.typeFunctions.NodeShape.$fromRdfResource(curieResourceSet.resource(shapeNode), {
|
|
370
|
+
ignoreRdfType: true,
|
|
371
|
+
}).unsafeCoerce());
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return this;
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
AbstractShapesGraph.AbstractBuilder = AbstractBuilder;
|
|
379
|
+
})(AbstractShapesGraph || (AbstractShapesGraph = {}));
|
|
380
|
+
//# sourceMappingURL=AbstractShapesGraph.js.map
|
package/dist/ShapesGraph.d.ts
CHANGED
|
@@ -1,80 +1,27 @@
|
|
|
1
|
-
import
|
|
2
|
-
import TermMap from "@rdfjs/term-map";
|
|
3
|
-
import type { BlankNode, DatasetCore, NamedNode } from "@rdfjs/types";
|
|
4
|
-
import { Either } from "purify-ts";
|
|
5
|
-
import { Resource } from "rdfjs-resource";
|
|
6
|
-
import { CurieFactory } from "./CurieFactory.js";
|
|
1
|
+
import { AbstractShapesGraph } from "./AbstractShapesGraph.js";
|
|
7
2
|
import * as generated from "./generated.js";
|
|
8
|
-
export declare class ShapesGraph
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
propertyGroupsByIdentifier: TermMap<BlankNode | NamedNode, PropertyGroupT>;
|
|
17
|
-
propertyShapesByIdentifier: TermMap<BlankNode | NamedNode, PropertyShapeT>;
|
|
18
|
-
});
|
|
19
|
-
get nodeShapes(): readonly NodeShapeT[];
|
|
20
|
-
get ontologies(): readonly OntologyT[];
|
|
21
|
-
get propertyGroups(): readonly PropertyGroupT[];
|
|
22
|
-
get propertyShapes(): readonly PropertyShapeT[];
|
|
23
|
-
nodeShape(identifier: BlankNode | NamedNode): Either<Error, NodeShapeT>;
|
|
24
|
-
ontology(identifier: BlankNode | NamedNode): Either<Error, OntologyT>;
|
|
25
|
-
propertyGroup(identifier: BlankNode | NamedNode): Either<Error, PropertyGroupT>;
|
|
26
|
-
propertyShape(identifier: BlankNode | NamedNode): Either<Error, PropertyShapeT>;
|
|
27
|
-
shape(identifier: BlankNode | NamedNode): Either<Error, ShapeT>;
|
|
3
|
+
export declare class ShapesGraph extends AbstractShapesGraph<generated.NodeShape, generated.Ontology, generated.PropertyGroup, generated.PropertyShape> {
|
|
4
|
+
protected readonly typeFunctions: {
|
|
5
|
+
readonly NodeShape: typeof generated.NodeShape;
|
|
6
|
+
readonly Ontology: typeof generated.Ontology;
|
|
7
|
+
readonly PropertyGroup: typeof generated.PropertyGroup;
|
|
8
|
+
readonly PropertyShape: typeof generated.PropertyShape;
|
|
9
|
+
};
|
|
10
|
+
static builder(): ShapesGraph.Builder;
|
|
28
11
|
}
|
|
29
12
|
export declare namespace ShapesGraph {
|
|
30
|
-
|
|
31
|
-
protected
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
resource: Resource;
|
|
43
|
-
shapesGraph: ShapesGraph<NodeShapeT, OntologyT, PropertyGroupT, PropertyShapeT, ShapeT>;
|
|
44
|
-
}): Either<Error, NodeShapeT>;
|
|
45
|
-
protected abstract createOntology(parameters: {
|
|
46
|
-
curieFactory: CurieFactory;
|
|
47
|
-
resource: Resource;
|
|
48
|
-
shapesGraph: ShapesGraph<NodeShapeT, OntologyT, PropertyGroupT, PropertyShapeT, ShapeT>;
|
|
49
|
-
}): Either<Error, OntologyT>;
|
|
50
|
-
protected abstract createPropertyGroup(parameters: {
|
|
51
|
-
curieFactory: CurieFactory;
|
|
52
|
-
resource: Resource;
|
|
53
|
-
shapesGraph: ShapesGraph<NodeShapeT, OntologyT, PropertyGroupT, PropertyShapeT, ShapeT>;
|
|
54
|
-
}): Either<Error, PropertyGroupT>;
|
|
55
|
-
protected abstract createPropertyShape(parameters: {
|
|
56
|
-
curieFactory: CurieFactory;
|
|
57
|
-
resource: Resource;
|
|
58
|
-
shapesGraph: ShapesGraph<NodeShapeT, OntologyT, PropertyGroupT, PropertyShapeT, ShapeT>;
|
|
59
|
-
}): Either<Error, PropertyShapeT>;
|
|
13
|
+
class Builder extends AbstractShapesGraph.AbstractBuilder<generated.NodeShape, generated.Ontology, generated.PropertyGroup, generated.PropertyShape> {
|
|
14
|
+
protected readonly typeFunctions: {
|
|
15
|
+
readonly NodeShape: typeof generated.NodeShape;
|
|
16
|
+
readonly Ontology: typeof generated.Ontology;
|
|
17
|
+
readonly PropertyGroup: typeof generated.PropertyGroup;
|
|
18
|
+
readonly PropertyShape: typeof generated.PropertyShape;
|
|
19
|
+
};
|
|
20
|
+
build(): ShapesGraph;
|
|
21
|
+
nodeShape(parameters?: Parameters<typeof generated.NodeShape.$create>[0]): generated.NodeShape;
|
|
22
|
+
ontology(parameters?: Parameters<typeof generated.Ontology.$create>[0]): generated.Ontology;
|
|
23
|
+
propertyGroup(parameters?: Parameters<typeof generated.PropertyGroup.$create>[0]): generated.PropertyGroup;
|
|
24
|
+
propertyShape(parameters: Parameters<typeof generated.PropertyShape.$create>[0]): generated.PropertyShape;
|
|
60
25
|
}
|
|
61
|
-
type DefaultShapesGraph = ShapesGraph<generated.NodeShape, generated.Ontology, generated.PropertyGroup, generated.PropertyShape, generated.Shape>;
|
|
62
|
-
class DefaultFactory extends Factory<generated.NodeShape, generated.Ontology, generated.PropertyGroup, generated.PropertyShape, generated.Shape> {
|
|
63
|
-
protected createNodeShape({ resource }: {
|
|
64
|
-
resource: Resource;
|
|
65
|
-
}): Either<Error, generated.NodeShape>;
|
|
66
|
-
protected createOntology({ resource, }: {
|
|
67
|
-
resource: Resource;
|
|
68
|
-
}): Either<Error, generated.Ontology>;
|
|
69
|
-
protected createPropertyGroup({ resource, }: {
|
|
70
|
-
resource: Resource;
|
|
71
|
-
}): Either<Error, generated.PropertyGroup>;
|
|
72
|
-
protected createPropertyShape({ curieFactory, resource, }: {
|
|
73
|
-
curieFactory: CurieFactory;
|
|
74
|
-
resource: Resource;
|
|
75
|
-
}): Either<Error, generated.PropertyShape>;
|
|
76
|
-
}
|
|
77
|
-
export function create(parameters: Parameters<DefaultFactory["createShapesGraph"]>[0]): Either<Error, DefaultShapesGraph>;
|
|
78
|
-
export {};
|
|
79
26
|
}
|
|
80
27
|
//# sourceMappingURL=ShapesGraph.d.ts.map
|