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