@shaclmate/shacl-ast 2.0.13 → 2.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.
package/ShapesGraph.js CHANGED
@@ -1,201 +1,2 @@
1
- import TermMap from "@rdfjs/term-map";
2
- import TermSet from "@rdfjs/term-set";
3
- import { rdf, sh } from "@tpluscode/rdf-ns-builders";
4
- import { Maybe } from "purify-ts";
5
- import { Resource, ResourceSet } from "rdfjs-resource";
6
- import { NodeShape } from "./NodeShape.js";
7
- import { PropertyGroup } from "./PropertyGroup.js";
8
- import { PropertyShape } from "./PropertyShape.js";
9
- export class ShapesGraph {
10
- constructor(dataset) {
11
- this.dataset = dataset;
12
- this.node = this.readGraph();
13
- const { nodeShapes, nodeShapesByNode, propertyShapes, propertyShapesByNode, } = this.readShapes();
14
- this.nodeShapes = nodeShapes;
15
- this.nodeShapesByNode = nodeShapesByNode;
16
- this.propertyShapes = propertyShapes;
17
- this.propertyShapesByNode = propertyShapesByNode;
18
- const { propertyGroups, propertyGroupsByNode } = this.readPropertyGroups();
19
- this.propertyGroups = propertyGroups;
20
- this.propertyGroupsByNode = propertyGroupsByNode;
21
- }
22
- static fromDataset(dataset) {
23
- return new ShapesGraph(dataset);
24
- }
25
- nodeShapeByNode(nodeShapeNode) {
26
- return Maybe.fromNullable(this.nodeShapesByNode.get(nodeShapeNode));
27
- }
28
- propertyGroupByNode(propertyGroupNode) {
29
- return Maybe.fromNullable(this.propertyGroupsByNode.get(propertyGroupNode));
30
- }
31
- propertyShapeByNode(propertyShapeNode) {
32
- return Maybe.fromNullable(this.propertyShapesByNode.get(propertyShapeNode));
33
- }
34
- shapeByNode(node) {
35
- const nodeShape = this.nodeShapeByNode(node);
36
- if (nodeShape.isJust()) {
37
- return nodeShape;
38
- }
39
- return this.propertyShapeByNode(node);
40
- }
41
- readGraph() {
42
- const graphs = new TermSet();
43
- for (const quad of this.dataset) {
44
- graphs.add(quad.graph);
45
- }
46
- if (graphs.size !== 1) {
47
- return null;
48
- }
49
- const graph = [...graphs.values()][0];
50
- switch (graph.termType) {
51
- case "BlankNode":
52
- case "DefaultGraph":
53
- case "NamedNode":
54
- return graph;
55
- default:
56
- throw new RangeError(`expected NamedNode or default graph, actual ${graph.termType}`);
57
- }
58
- }
59
- readPropertyGroups() {
60
- const propertyGroups = [];
61
- const propertyGroupsByNode = new TermMap();
62
- for (const quad of this.dataset.match(null, rdf.type, sh.PropertyGroup, this.node)) {
63
- const subject = quad.subject;
64
- if (subject.termType !== "NamedNode") {
65
- continue;
66
- }
67
- if (propertyGroupsByNode.has(subject)) {
68
- continue;
69
- }
70
- const propertyGroup = new PropertyGroup(new Resource({ dataset: this.dataset, identifier: subject }));
71
- propertyGroups.push(propertyGroup);
72
- propertyGroupsByNode.set(subject, propertyGroup);
73
- }
74
- return { propertyGroups, propertyGroupsByNode };
75
- }
76
- readShapes() {
77
- const resourceSet = new ResourceSet({ dataset: this.dataset });
78
- // Collect the shape identifiers in sets
79
- const shapeNodeSet = new TermSet();
80
- // Utility function for doing the collection
81
- const addShapeNode = (shapeNode) => {
82
- switch (shapeNode.termType) {
83
- case "BlankNode":
84
- case "NamedNode":
85
- shapeNodeSet.add(shapeNode);
86
- break;
87
- }
88
- };
89
- // Test each shape condition
90
- // https://www.w3.org/TR/shacl/#shapes
91
- // Subject is a SHACL instance of sh:NodeShape or sh:PropertyShape
92
- for (const rdfType of [sh.NodeShape, sh.PropertyShape]) {
93
- for (const resource of resourceSet.instancesOf(rdfType, {
94
- graph: this.node,
95
- })) {
96
- addShapeNode(resource.identifier);
97
- }
98
- }
99
- // Subject of a triple with sh:targetClass, sh:targetNode, sh:targetObjectsOf, or sh:targetSubjectsOf predicate
100
- for (const predicate of [
101
- sh.targetClass,
102
- sh.targetNode,
103
- sh.targetObjectsOf,
104
- sh.targetSubjectsOf,
105
- ]) {
106
- for (const quad of this.dataset.match(null, predicate, null, this.node)) {
107
- addShapeNode(quad.subject);
108
- }
109
- }
110
- // Subject of a triple that has a parameter as predicate
111
- // https://www.w3.org/TR/shacl/#constraints
112
- // https://www.w3.org/TR/shacl/#core-components
113
- for (const predicate of [
114
- sh.class,
115
- sh.datatype,
116
- sh.nodeKind,
117
- sh.minCount,
118
- sh.maxCount,
119
- sh.minExclusive,
120
- sh.minInclusive,
121
- sh.maxExclusive,
122
- sh.maxInclusive,
123
- sh.minLength,
124
- sh.maxLength,
125
- sh.pattern,
126
- sh.languageIn,
127
- sh.uniqueLang,
128
- sh.equals,
129
- sh.disjoint,
130
- sh.lessThan,
131
- sh.lessThanOrEquals,
132
- sh.not,
133
- sh.and,
134
- sh.or,
135
- sh.xone,
136
- sh.node,
137
- sh.property,
138
- sh.qualifiedValueShape,
139
- sh.qualifiedMinCount,
140
- sh.qualifiedMaxCount,
141
- sh.closed,
142
- sh.ignoredProperties,
143
- sh.hasValue,
144
- sh.in,
145
- ]) {
146
- for (const quad of this.dataset.match(null, predicate, null, this.node)) {
147
- addShapeNode(quad.subject);
148
- }
149
- }
150
- // Object of a shape-expecting, non-list-taking parameter such as sh:node
151
- for (const predicate of [sh.node, sh.property]) {
152
- for (const quad of this.dataset.match(null, predicate, null, this.node)) {
153
- addShapeNode(quad.object);
154
- }
155
- }
156
- // Member of a SHACL list that is a value of a shape-expecting and list-taking parameter such as sh:or
157
- for (const predicate of [sh.and, sh["in"], sh.languageIn, sh.or, sh.xone]) {
158
- for (const quad of this.dataset.match(null, predicate, null, this.node)) {
159
- switch (quad.object.termType) {
160
- case "BlankNode":
161
- case "NamedNode":
162
- break;
163
- default:
164
- continue;
165
- }
166
- for (const value of resourceSet
167
- .resource(quad.object)
168
- .toList()
169
- .orDefault([])) {
170
- value.toIdentifier().ifRight(addShapeNode);
171
- }
172
- }
173
- }
174
- // Separate shapes into node and property shapes.
175
- const nodeShapes = [];
176
- const nodeShapesByNode = new TermMap();
177
- const propertyShapes = [];
178
- const propertyShapesByNode = new TermMap();
179
- for (const shapeNode of shapeNodeSet) {
180
- if (this.dataset.match(shapeNode, sh.path, null, this.node).size > 0) {
181
- // 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.
182
- const propertyShape = new PropertyShape(resourceSet.resource(shapeNode), this);
183
- propertyShapes.push(propertyShape);
184
- propertyShapesByNode.set(shapeNode, propertyShape);
185
- }
186
- else {
187
- // 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.
188
- const nodeShape = new NodeShape(resourceSet.resource(shapeNode), this);
189
- nodeShapes.push(nodeShape);
190
- nodeShapesByNode.set(shapeNode, nodeShape);
191
- }
192
- }
193
- return {
194
- nodeShapes,
195
- nodeShapesByNode,
196
- propertyShapes,
197
- propertyShapesByNode,
198
- };
199
- }
200
- }
1
+ export {};
201
2
  //# sourceMappingURL=ShapesGraph.js.map
@@ -0,0 +1,12 @@
1
+ import type { Factory } from "./Factory.js";
2
+ import { NodeShape } from "./NodeShape.js";
3
+ import { Ontology } from "./Ontology.js";
4
+ import { PropertyGroup } from "./PropertyGroup.js";
5
+ import { PropertyShape } from "./PropertyShape.js";
6
+ import type { Shape } from "./Shape.js";
7
+ type DefaultNodeShape = NodeShape<any, Ontology, PropertyGroup, DefaultPropertyShape, DefaultShape>;
8
+ type DefaultPropertyShape = PropertyShape<DefaultNodeShape, Ontology, PropertyGroup, any, DefaultShape>;
9
+ type DefaultShape = Shape<DefaultNodeShape, Ontology, PropertyGroup, DefaultPropertyShape, any>;
10
+ export declare const defaultFactory: Factory<DefaultNodeShape, Ontology, PropertyGroup, DefaultPropertyShape, DefaultShape>;
11
+ export {};
12
+ //# sourceMappingURL=defaultFactory.d.ts.map
@@ -0,0 +1,32 @@
1
+ import { NodeShape } from "./NodeShape.js";
2
+ import { Ontology } from "./Ontology.js";
3
+ import { PropertyGroup } from "./PropertyGroup.js";
4
+ import { PropertyShape } from "./PropertyShape.js";
5
+ import * as generated from "./generated.js";
6
+ export const defaultFactory = {
7
+ nodeShapeFromRdf({ resource, shapesGraph, }) {
8
+ return generated.ShaclCoreNodeShape.fromRdf({
9
+ ignoreRdfType: true,
10
+ resource,
11
+ }).map((generatedShape) => new NodeShape(generatedShape, shapesGraph));
12
+ },
13
+ ontologyFromRdf({ resource, }) {
14
+ return generated.OwlOntology.fromRdf({
15
+ ignoreRdfType: true,
16
+ resource,
17
+ }).map((generatedOntology) => new Ontology(generatedOntology));
18
+ },
19
+ propertyGroupFromRdf({ resource, }) {
20
+ return generated.ShaclCorePropertyGroup.fromRdf({
21
+ ignoreRdfType: true,
22
+ resource,
23
+ }).map((propertyGroup) => new PropertyGroup(propertyGroup));
24
+ },
25
+ propertyShapeFromRdf({ resource, shapesGraph, }) {
26
+ return generated.ShaclCorePropertyShape.fromRdf({
27
+ ignoreRdfType: true,
28
+ resource,
29
+ }).map((generatedShape) => new PropertyShape(generatedShape, shapesGraph));
30
+ },
31
+ };
32
+ //# sourceMappingURL=defaultFactory.js.map
package/generated.d.ts ADDED
@@ -0,0 +1,138 @@
1
+ import type * as rdfjs from "@rdfjs/types";
2
+ import { PropertyPath } from "@shaclmate/shacl-ast/PropertyPath.js";
3
+ import * as purify from "purify-ts";
4
+ import * as rdfjsResource from "rdfjs-resource";
5
+ export interface BaseShaclCoreShape {
6
+ readonly and: readonly (readonly (rdfjs.BlankNode | rdfjs.NamedNode)[])[];
7
+ readonly classes: readonly rdfjs.NamedNode[];
8
+ readonly comments: readonly rdfjs.Literal[];
9
+ readonly datatype: purify.Maybe<rdfjs.NamedNode>;
10
+ readonly deactivated: purify.Maybe<boolean>;
11
+ readonly flags: readonly string[];
12
+ readonly hasValues: readonly (rdfjs.BlankNode | rdfjs.NamedNode | rdfjs.Literal)[];
13
+ readonly identifier: rdfjs.BlankNode | rdfjs.NamedNode;
14
+ readonly in_: purify.Maybe<readonly (rdfjs.BlankNode | rdfjs.NamedNode | rdfjs.Literal)[]>;
15
+ readonly isDefinedBy: purify.Maybe<rdfjs.BlankNode | rdfjs.NamedNode>;
16
+ readonly labels: readonly rdfjs.Literal[];
17
+ readonly languageIn: purify.Maybe<readonly string[]>;
18
+ readonly maxCount: purify.Maybe<number>;
19
+ readonly maxExclusive: purify.Maybe<rdfjs.Literal>;
20
+ readonly maxInclusive: purify.Maybe<rdfjs.Literal>;
21
+ readonly maxLength: purify.Maybe<number>;
22
+ readonly minCount: purify.Maybe<number>;
23
+ readonly minExclusive: purify.Maybe<rdfjs.Literal>;
24
+ readonly minInclusive: purify.Maybe<rdfjs.Literal>;
25
+ readonly minLength: purify.Maybe<number>;
26
+ readonly nodeKind: purify.Maybe<rdfjs.NamedNode<"http://www.w3.org/ns/shacl#BlankNode" | "http://www.w3.org/ns/shacl#BlankNodeOrIRI" | "http://www.w3.org/ns/shacl#BlankNodeOrLiteral" | "http://www.w3.org/ns/shacl#IRI" | "http://www.w3.org/ns/shacl#IRIOrLiteral" | "http://www.w3.org/ns/shacl#Literal">>;
27
+ readonly nodes: readonly (rdfjs.BlankNode | rdfjs.NamedNode)[];
28
+ readonly not: readonly (rdfjs.BlankNode | rdfjs.NamedNode)[];
29
+ readonly or: readonly (readonly (rdfjs.BlankNode | rdfjs.NamedNode)[])[];
30
+ readonly patterns: readonly string[];
31
+ readonly type: "ShaclCoreNodeShape" | "ShaclCorePropertyShape";
32
+ readonly xone: readonly (readonly (rdfjs.BlankNode | rdfjs.NamedNode)[])[];
33
+ }
34
+ export declare namespace BaseShaclCoreShape {
35
+ function fromRdf({ ignoreRdfType: _ignoreRdfType, languageIn: _languageIn, resource: _resource, ..._context }: {
36
+ [_index: string]: any;
37
+ ignoreRdfType?: boolean;
38
+ languageIn?: readonly string[];
39
+ resource: rdfjsResource.Resource;
40
+ }): purify.Either<rdfjsResource.Resource.ValueError, {
41
+ and: readonly (readonly (rdfjs.BlankNode | rdfjs.NamedNode)[])[];
42
+ classes: readonly rdfjs.NamedNode[];
43
+ comments: readonly rdfjs.Literal[];
44
+ datatype: purify.Maybe<rdfjs.NamedNode>;
45
+ deactivated: purify.Maybe<boolean>;
46
+ flags: readonly string[];
47
+ hasValues: readonly (rdfjs.BlankNode | rdfjs.NamedNode | rdfjs.Literal)[];
48
+ identifier: rdfjs.BlankNode | rdfjs.NamedNode;
49
+ in_: purify.Maybe<readonly (rdfjs.BlankNode | rdfjs.NamedNode | rdfjs.Literal)[]>;
50
+ isDefinedBy: purify.Maybe<rdfjs.BlankNode | rdfjs.NamedNode>;
51
+ labels: readonly rdfjs.Literal[];
52
+ languageIn: purify.Maybe<readonly string[]>;
53
+ maxCount: purify.Maybe<number>;
54
+ maxExclusive: purify.Maybe<rdfjs.Literal>;
55
+ maxInclusive: purify.Maybe<rdfjs.Literal>;
56
+ maxLength: purify.Maybe<number>;
57
+ minCount: purify.Maybe<number>;
58
+ minExclusive: purify.Maybe<rdfjs.Literal>;
59
+ minInclusive: purify.Maybe<rdfjs.Literal>;
60
+ minLength: purify.Maybe<number>;
61
+ nodeKind: purify.Maybe<rdfjs.NamedNode<"http://www.w3.org/ns/shacl#BlankNode" | "http://www.w3.org/ns/shacl#BlankNodeOrIRI" | "http://www.w3.org/ns/shacl#BlankNodeOrLiteral" | "http://www.w3.org/ns/shacl#IRI" | "http://www.w3.org/ns/shacl#IRIOrLiteral" | "http://www.w3.org/ns/shacl#Literal">>;
62
+ nodes: readonly (rdfjs.BlankNode | rdfjs.NamedNode)[];
63
+ not: readonly (rdfjs.BlankNode | rdfjs.NamedNode)[];
64
+ or: readonly (readonly (rdfjs.BlankNode | rdfjs.NamedNode)[])[];
65
+ patterns: readonly string[];
66
+ xone: readonly (readonly (rdfjs.BlankNode | rdfjs.NamedNode)[])[];
67
+ }>;
68
+ }
69
+ export interface ShaclCorePropertyShape extends BaseShaclCoreShape {
70
+ readonly defaultValue: purify.Maybe<rdfjs.BlankNode | rdfjs.NamedNode | rdfjs.Literal>;
71
+ readonly descriptions: readonly rdfjs.Literal[];
72
+ readonly groups: readonly (rdfjs.BlankNode | rdfjs.NamedNode)[];
73
+ readonly identifier: rdfjs.BlankNode | rdfjs.NamedNode;
74
+ readonly names: readonly rdfjs.Literal[];
75
+ readonly order: purify.Maybe<number>;
76
+ readonly path: PropertyPath;
77
+ readonly type: "ShaclCorePropertyShape";
78
+ readonly uniqueLang: purify.Maybe<boolean>;
79
+ }
80
+ export declare namespace ShaclCorePropertyShape {
81
+ function fromRdf({ ignoreRdfType: _ignoreRdfType, languageIn: _languageIn, resource: _resource, ..._context }: {
82
+ [_index: string]: any;
83
+ ignoreRdfType?: boolean;
84
+ languageIn?: readonly string[];
85
+ resource: rdfjsResource.Resource;
86
+ }): purify.Either<rdfjsResource.Resource.ValueError, ShaclCorePropertyShape>;
87
+ }
88
+ export interface ShaclCorePropertyGroup {
89
+ readonly comments: readonly rdfjs.Literal[];
90
+ readonly identifier: rdfjs.BlankNode | rdfjs.NamedNode;
91
+ readonly labels: readonly rdfjs.Literal[];
92
+ readonly type: "ShaclCorePropertyGroup";
93
+ }
94
+ export declare namespace ShaclCorePropertyGroup {
95
+ function fromRdf({ ignoreRdfType: _ignoreRdfType, languageIn: _languageIn, resource: _resource, ..._context }: {
96
+ [_index: string]: any;
97
+ ignoreRdfType?: boolean;
98
+ languageIn?: readonly string[];
99
+ resource: rdfjsResource.Resource;
100
+ }): purify.Either<rdfjsResource.Resource.ValueError, ShaclCorePropertyGroup>;
101
+ }
102
+ export interface ShaclCoreNodeShape extends BaseShaclCoreShape {
103
+ readonly closed: purify.Maybe<boolean>;
104
+ readonly identifier: rdfjs.BlankNode | rdfjs.NamedNode;
105
+ readonly ignoredProperties: purify.Maybe<readonly rdfjs.NamedNode[]>;
106
+ readonly properties: readonly (rdfjs.BlankNode | rdfjs.NamedNode)[];
107
+ readonly type: "ShaclCoreNodeShape";
108
+ }
109
+ export declare namespace ShaclCoreNodeShape {
110
+ function fromRdf({ ignoreRdfType: _ignoreRdfType, languageIn: _languageIn, resource: _resource, ..._context }: {
111
+ [_index: string]: any;
112
+ ignoreRdfType?: boolean;
113
+ languageIn?: readonly string[];
114
+ resource: rdfjsResource.Resource;
115
+ }): purify.Either<rdfjsResource.Resource.ValueError, ShaclCoreNodeShape>;
116
+ }
117
+ export interface OwlOntology {
118
+ readonly identifier: rdfjs.BlankNode | rdfjs.NamedNode;
119
+ readonly labels: readonly rdfjs.Literal[];
120
+ readonly type: "OwlOntology";
121
+ }
122
+ export declare namespace OwlOntology {
123
+ function fromRdf({ ignoreRdfType: _ignoreRdfType, languageIn: _languageIn, resource: _resource, ..._context }: {
124
+ [_index: string]: any;
125
+ ignoreRdfType?: boolean;
126
+ languageIn?: readonly string[];
127
+ resource: rdfjsResource.Resource;
128
+ }): purify.Either<rdfjsResource.Resource.ValueError, OwlOntology>;
129
+ }
130
+ export type ShaclCoreShape = ShaclCoreNodeShape | ShaclCorePropertyShape;
131
+ export declare namespace ShaclCoreShape {
132
+ function fromRdf(parameters: {
133
+ [_index: string]: any;
134
+ ignoreRdfType?: boolean;
135
+ resource: rdfjsResource.Resource;
136
+ }): purify.Either<rdfjsResource.Resource.ValueError, ShaclCoreShape>;
137
+ }
138
+ //# sourceMappingURL=generated.d.ts.map