@shaclmate/shacl-ast 4.0.13 → 4.0.15

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.
@@ -1,336 +1,49 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import DataFactory from "@rdfjs/data-model";
8
- import PrefixMap from "@rdfjs/prefix-map/PrefixMap.js";
9
- import TermMap from "@rdfjs/term-map";
10
- import TermSet from "@rdfjs/term-set";
11
- import { owl, sh } from "@tpluscode/rdf-ns-builders";
12
- import { Either, Left } from "purify-ts";
13
- import { Resource, ResourceSet } from "rdfjs-resource";
14
- import { Memoize } from "typescript-memoize";
15
- import { CurieFactory } from "./CurieFactory.js";
1
+ import { AbstractShapesGraph } from "./AbstractShapesGraph.js";
16
2
  import * as generated from "./generated.js";
17
- import { NodeShape } from "./NodeShape.js";
18
- import { Ontology } from "./Ontology.js";
19
- import { PropertyGroup } from "./PropertyGroup.js";
20
- import { PropertyShape } from "./PropertyShape.js";
21
- export class ShapesGraph {
22
- nodeShapesByIdentifier;
23
- ontologiesByIdentifier;
24
- propertyGroupsByIdentifier;
25
- propertyShapesByIdentifier;
26
- constructor(parameters) {
27
- this.nodeShapesByIdentifier = parameters.nodeShapesByIdentifier;
28
- this.ontologiesByIdentifier = parameters.ontologiesByIdentifier;
29
- this.propertyGroupsByIdentifier = parameters.propertyGroupsByIdentifier;
30
- this.propertyShapesByIdentifier = parameters.propertyShapesByIdentifier;
31
- }
32
- get nodeShapes() {
33
- return [...this.nodeShapesByIdentifier.values()];
34
- }
35
- nodeShapeByIdentifier(identifier) {
36
- const nodeShape = this.nodeShapesByIdentifier.get(identifier);
37
- return nodeShape
38
- ? Either.of(nodeShape)
39
- : Left(new Error(`no such node shape ${Resource.Identifier.toString(identifier)}`));
40
- }
41
- get ontologies() {
42
- return [...this.ontologiesByIdentifier.values()];
43
- }
44
- ontologyByIdentifier(identifier) {
45
- const ontology = this.ontologiesByIdentifier.get(identifier);
46
- return ontology
47
- ? Either.of(ontology)
48
- : Left(new Error(`no such ontology ${Resource.Identifier.toString(identifier)}`));
49
- }
50
- propertyGroupByIdentifier(identifier) {
51
- const propertyGroup = this.propertyGroupsByIdentifier.get(identifier);
52
- return propertyGroup
53
- ? Either.of(propertyGroup)
54
- : Left(new Error(`no such property group ${Resource.Identifier.toString(identifier)}`));
55
- }
56
- get propertyGroups() {
57
- return [...this.propertyGroupsByIdentifier.values()];
58
- }
59
- propertyShapeByIdentifier(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
- get propertyShapes() {
66
- return [...this.propertyShapesByIdentifier.values()];
67
- }
68
- shapeByIdentifier(identifier) {
69
- return this.nodeShapeByIdentifier(identifier).alt(this.propertyShapeByIdentifier(identifier));
3
+ const typeFunctions = {
4
+ NodeShape: generated.NodeShape,
5
+ Ontology: generated.Ontology,
6
+ PropertyGroup: generated.PropertyGroup,
7
+ PropertyShape: generated.PropertyShape,
8
+ };
9
+ export class ShapesGraph extends AbstractShapesGraph {
10
+ typeFunctions = typeFunctions;
11
+ static builder() {
12
+ return new ShapesGraph.Builder();
70
13
  }
71
14
  }
72
- __decorate([
73
- Memoize()
74
- ], ShapesGraph.prototype, "nodeShapes", null);
75
- __decorate([
76
- Memoize()
77
- ], ShapesGraph.prototype, "ontologies", null);
78
- __decorate([
79
- Memoize()
80
- ], ShapesGraph.prototype, "propertyGroups", null);
81
- __decorate([
82
- Memoize()
83
- ], ShapesGraph.prototype, "propertyShapes", null);
84
15
  (function (ShapesGraph) {
85
- class Factory {
86
- preferredLanguages;
87
- constructor(parameters) {
88
- this.preferredLanguages = parameters?.preferredLanguages ?? ["en", ""];
89
- }
90
- createShapesGraph({ dataset, prefixMap, ignoreUndefinedShapes, }) {
91
- function datasetHasMatch(subject, predicate, object, graph) {
92
- for (const _ of dataset.match(subject, predicate, object, graph)) {
93
- return true;
94
- }
95
- return false;
96
- }
97
- const curieFactory = new CurieFactory({
98
- prefixMap: prefixMap ?? new PrefixMap(undefined, { factory: DataFactory }),
99
- });
100
- const resourceSet = new ResourceSet(dataset);
101
- const curieResource = (identifier) => {
102
- if (identifier.termType === "NamedNode") {
103
- const curie = curieFactory.create(identifier).extract();
104
- if (curie) {
105
- return resourceSet.resource(curie);
106
- }
107
- }
108
- return resourceSet.resource(identifier);
109
- };
110
- const nodeShapesByIdentifier = new TermMap();
111
- const ontologiesByIdentifier = new TermMap();
112
- const propertyGroupsByIdentifier = new TermMap();
113
- const propertyShapesByIdentifier = new TermMap();
114
- // Have to instantiate ShapesGraph here so the shapes have references to it
115
- // Pass in the mutable TermMap's and then mutate them
116
- const shapesGraph = new ShapesGraph({
117
- nodeShapesByIdentifier,
118
- ontologiesByIdentifier,
119
- propertyGroupsByIdentifier,
120
- propertyShapesByIdentifier,
121
- });
122
- return Either.encase(() => {
123
- function readGraph() {
124
- const graphs = new TermSet();
125
- for (const quad of dataset) {
126
- graphs.add(quad.graph);
127
- }
128
- if (graphs.size !== 1) {
129
- return undefined;
130
- }
131
- const graph = [...graphs.values()][0];
132
- switch (graph.termType) {
133
- case "BlankNode":
134
- case "DefaultGraph":
135
- case "NamedNode":
136
- return graph;
137
- default:
138
- throw new RangeError(`expected NamedNode or default graph, actual ${graph.termType}`);
139
- }
140
- }
141
- const graph = readGraph();
142
- // Read ontologies
143
- for (const ontologyResource of resourceSet.instancesOf(owl.Ontology, {
144
- graph,
145
- })) {
146
- if (ontologiesByIdentifier.has(ontologyResource.identifier)) {
147
- continue;
148
- }
149
- this.createOntology({
150
- curieFactory,
151
- resource: curieResource(ontologyResource.identifier),
152
- shapesGraph,
153
- }).ifRight((ontology) => ontologiesByIdentifier.set(ontologyResource.identifier, ontology));
154
- }
155
- // Read property groups
156
- for (const propertyGroupResource of resourceSet.instancesOf(sh.PropertyGroup, { graph })) {
157
- if (propertyGroupResource.identifier.termType !== "NamedNode") {
158
- continue;
159
- }
160
- if (propertyGroupsByIdentifier.has(propertyGroupResource.identifier)) {
161
- continue;
162
- }
163
- this.createPropertyGroup({
164
- curieFactory,
165
- resource: curieResource(propertyGroupResource.identifier),
166
- shapesGraph,
167
- }).ifRight((propertyGroup) => propertyGroupsByIdentifier.set(propertyGroupResource.identifier, propertyGroup));
168
- }
169
- // Read shapes
170
- // Collect the shape identifiers in sets
171
- const shapeNodeSet = new TermSet();
172
- // Utility function for adding to the shapeNodeSet
173
- function addShapeNode(shapeNode) {
174
- switch (shapeNode.termType) {
175
- case "BlankNode":
176
- case "NamedNode":
177
- shapeNodeSet.add(shapeNode);
178
- return true;
179
- default:
180
- throw new RangeError(`unexpected shape node identifier term type: ${shapeNode.termType}`);
181
- }
182
- }
183
- // Test each shape condition
184
- // https://www.w3.org/TR/shacl/#shapes
185
- // Subject is a SHACL instance of sh:NodeShape or sh:PropertyShape
186
- for (const rdfType of [sh.NodeShape, sh.PropertyShape]) {
187
- for (const resource of resourceSet.instancesOf(rdfType, {
188
- graph,
189
- })) {
190
- addShapeNode(resource.identifier);
191
- }
192
- }
193
- // Subject of a triple with sh:targetClass, sh:targetNode, sh:targetObjectsOf, or sh:targetSubjectsOf predicate
194
- for (const predicate of [
195
- sh.targetClass,
196
- sh.targetNode,
197
- sh.targetObjectsOf,
198
- sh.targetSubjectsOf,
199
- ]) {
200
- for (const quad of dataset.match(null, predicate, null, graph)) {
201
- addShapeNode(quad.subject);
202
- }
203
- }
204
- // Subject of a triple that has a parameter as predicate
205
- // https://www.w3.org/TR/shacl/#constraints
206
- // https://www.w3.org/TR/shacl/#core-components
207
- for (const predicate of [
208
- sh.class,
209
- sh.datatype,
210
- sh.nodeKind,
211
- sh.minCount,
212
- sh.maxCount,
213
- sh.minExclusive,
214
- sh.minInclusive,
215
- sh.maxExclusive,
216
- sh.maxInclusive,
217
- sh.minLength,
218
- sh.maxLength,
219
- sh.pattern,
220
- sh.languageIn,
221
- sh.uniqueLang,
222
- sh.equals,
223
- sh.disjoint,
224
- sh.lessThan,
225
- sh.lessThanOrEquals,
226
- sh.not,
227
- sh.and,
228
- sh.or,
229
- sh.xone,
230
- sh.node,
231
- sh.property,
232
- sh.qualifiedValueShape,
233
- sh.qualifiedMinCount,
234
- sh.qualifiedMaxCount,
235
- sh.closed,
236
- sh.ignoredProperties,
237
- sh.hasValue,
238
- sh.in,
239
- ]) {
240
- for (const quad of dataset.match(null, predicate, null, graph)) {
241
- addShapeNode(quad.subject);
242
- }
243
- }
244
- // Object of a shape-expecting, non-list-taking parameter such as sh:node
245
- for (const predicate of [sh.node, sh.not, sh.property]) {
246
- for (const quad of dataset.match(null, predicate, null, graph)) {
247
- addShapeNode(quad.object);
248
- if (!ignoreUndefinedShapes && !datasetHasMatch(quad.object)) {
249
- throw new Error(`undefined shape: ${Resource.Identifier.toString(quad.object)}`);
250
- }
251
- }
252
- }
253
- // Member of a SHACL list that is a value of a shape-expecting and list-taking parameter such as sh:or
254
- for (const predicate of [sh.and, sh.or, sh.xone]) {
255
- for (const quad of dataset.match(null, predicate, null, graph)) {
256
- switch (quad.object.termType) {
257
- case "BlankNode":
258
- case "NamedNode":
259
- break;
260
- default:
261
- throw new RangeError(`expected list term to be a blank or named node, not ${quad.object.termType}`);
262
- }
263
- for (const value of resourceSet
264
- .resource(quad.object)
265
- .toList()
266
- .unsafeCoerce()) {
267
- const identifier = value.toIdentifier().unsafeCoerce();
268
- addShapeNode(identifier);
269
- if (!ignoreUndefinedShapes && !datasetHasMatch(identifier)) {
270
- throw new Error(`undefined shape: ${Resource.Identifier.toString(identifier)}`);
271
- }
272
- }
273
- }
274
- }
275
- // Separate shapes into node and property shapes.
276
- for (const shapeNode of shapeNodeSet) {
277
- if (dataset.match(shapeNode, sh.path, null, graph).size > 0) {
278
- // 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.
279
- propertyShapesByIdentifier.set(shapeNode, this.createPropertyShape({
280
- curieFactory,
281
- resource: curieResource(shapeNode),
282
- shapesGraph,
283
- }).unsafeCoerce());
284
- }
285
- else {
286
- // 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.
287
- nodeShapesByIdentifier.set(shapeNode, this.createNodeShape({
288
- curieFactory,
289
- resource: curieResource(shapeNode),
290
- shapesGraph,
291
- }).unsafeCoerce());
292
- }
293
- }
294
- return shapesGraph;
16
+ class Builder extends AbstractShapesGraph.AbstractBuilder {
17
+ typeFunctions = typeFunctions;
18
+ build() {
19
+ return new ShapesGraph({
20
+ nodeShapesByIdentifier: this.nodeShapesByIdentifier,
21
+ ontologiesByIdentifier: this.ontologiesByIdentifier,
22
+ propertyGroupsByIdentifier: this.propertyGroupsByIdentifier,
23
+ propertyShapesByIdentifier: this.propertyShapesByIdentifier,
295
24
  });
296
25
  }
297
- }
298
- ShapesGraph.Factory = Factory;
299
- class DefaultFactory extends Factory {
300
- createNodeShape({ resource, shapesGraph, }) {
301
- return generated.NodeShape.$fromRdfResource(resource, {
302
- ignoreRdfType: true,
303
- preferredLanguages: this.preferredLanguages,
304
- }).map((generatedShape) => new NodeShape(generatedShape, shapesGraph));
26
+ nodeShape(parameters) {
27
+ const nodeShape = generated.NodeShape.$create(parameters);
28
+ this.add(nodeShape);
29
+ return nodeShape;
305
30
  }
306
- createOntology({ resource, }) {
307
- return generated.Ontology.$fromRdfResource(resource, {
308
- ignoreRdfType: true,
309
- preferredLanguages: this.preferredLanguages,
310
- }).map((generatedOntology) => new Ontology(generatedOntology));
31
+ ontology(parameters) {
32
+ const ontology = generated.Ontology.$create(parameters);
33
+ this.add(ontology);
34
+ return ontology;
311
35
  }
312
- createPropertyGroup({ resource, }) {
313
- return generated.PropertyGroup.$fromRdfResource(resource, {
314
- ignoreRdfType: true,
315
- preferredLanguages: this.preferredLanguages,
316
- }).map((propertyGroup) => new PropertyGroup(propertyGroup));
36
+ propertyGroup(parameters) {
37
+ const propertyGroup = generated.PropertyGroup.$create(parameters);
38
+ this.add(propertyGroup);
39
+ return propertyGroup;
317
40
  }
318
- createPropertyShape({ curieFactory, resource, shapesGraph, }) {
319
- return generated.PropertyShape.$fromRdfResource(resource, {
320
- ignoreRdfType: true,
321
- preferredLanguages: this.preferredLanguages,
322
- }).map((generatedShape) => new PropertyShape({
323
- ...generatedShape,
324
- path: (generatedShape.path.termType === "NamedNode"
325
- ? curieFactory.create(generatedShape.path).extract()
326
- : undefined) ?? generatedShape.path,
327
- }, shapesGraph));
41
+ propertyShape(parameters) {
42
+ const propertyShape = generated.PropertyShape.$create(parameters);
43
+ this.add(propertyShape);
44
+ return propertyShape;
328
45
  }
329
46
  }
330
- const defaultFactory = new DefaultFactory();
331
- function create(parameters) {
332
- return defaultFactory.createShapesGraph(parameters);
333
- }
334
- ShapesGraph.create = create;
47
+ ShapesGraph.Builder = Builder;
335
48
  })(ShapesGraph || (ShapesGraph = {}));
336
49
  //# sourceMappingURL=ShapesGraph.js.map