This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapesGraph.ts
274 lines (250 loc) · 8.75 KB
/
ShapesGraph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import TermMap from "@rdfjs/term-map";
import TermSet from "@rdfjs/term-set";
import type {
BlankNode,
DatasetCore,
DefaultGraph,
NamedNode,
Term,
} from "@rdfjs/types";
import { rdf, sh } from "@tpluscode/rdf-ns-builders";
import { Maybe } from "purify-ts";
import { Resource, ResourceSet } from "rdfjs-resource";
import { NodeShape } from "./NodeShape.js";
import { PropertyGroup } from "./PropertyGroup.js";
import { PropertyShape } from "./PropertyShape.js";
import type { Shape } from "./Shape.js";
export class ShapesGraph {
readonly node: BlankNode | DefaultGraph | NamedNode | null;
readonly nodeShapes: readonly NodeShape[];
readonly propertyGroups: readonly PropertyGroup[];
readonly propertyShapes: readonly PropertyShape[];
private readonly nodeShapesByNode: TermMap<BlankNode | NamedNode, NodeShape>;
private readonly propertyGroupsByNode: TermMap<
BlankNode | NamedNode,
PropertyGroup
>;
private readonly propertyShapesByNode: TermMap<
BlankNode | NamedNode,
PropertyShape
>;
private constructor(readonly dataset: DatasetCore) {
this.node = this.readGraph();
const {
nodeShapes,
nodeShapesByNode,
propertyShapes,
propertyShapesByNode,
} = this.readShapes();
this.nodeShapes = nodeShapes;
this.nodeShapesByNode = nodeShapesByNode;
this.propertyShapes = propertyShapes;
this.propertyShapesByNode = propertyShapesByNode;
const { propertyGroups, propertyGroupsByNode } = this.readPropertyGroups();
this.propertyGroups = propertyGroups;
this.propertyGroupsByNode = propertyGroupsByNode;
}
static fromDataset(dataset: DatasetCore): ShapesGraph {
return new ShapesGraph(dataset);
}
nodeShapeByNode(nodeShapeNode: BlankNode | NamedNode): Maybe<NodeShape> {
return Maybe.fromNullable(this.nodeShapesByNode.get(nodeShapeNode));
}
propertyGroupByNode(propertyGroupNode: NamedNode): Maybe<PropertyGroup> {
return Maybe.fromNullable(this.propertyGroupsByNode.get(propertyGroupNode));
}
propertyShapeByNode(
propertyShapeNode: BlankNode | NamedNode,
): Maybe<PropertyShape> {
return Maybe.fromNullable(this.propertyShapesByNode.get(propertyShapeNode));
}
shapeByNode(node: BlankNode | NamedNode): Maybe<Shape> {
const nodeShape = this.nodeShapeByNode(node);
if (nodeShape.isJust()) {
return nodeShape;
}
return this.propertyShapeByNode(node);
}
private readGraph(): BlankNode | DefaultGraph | NamedNode | null {
const graphs = new TermSet();
for (const quad of this.dataset) {
graphs.add(quad.graph);
}
if (graphs.size !== 1) {
return null;
}
const graph = [...graphs.values()][0];
switch (graph.termType) {
case "BlankNode":
case "DefaultGraph":
case "NamedNode":
return graph;
default:
throw new RangeError(
`expected NamedNode or default graph, actual ${graph.termType}`,
);
}
}
private readPropertyGroups(): {
propertyGroups: PropertyGroup[];
propertyGroupsByNode: TermMap<BlankNode | NamedNode, PropertyGroup>;
} {
const propertyGroups: PropertyGroup[] = [];
const propertyGroupsByNode: TermMap<BlankNode | NamedNode, PropertyGroup> =
new TermMap();
for (const quad of this.dataset.match(
null,
rdf.type,
sh.PropertyGroup,
this.node,
)) {
const subject = quad.subject;
if (subject.termType !== "NamedNode") {
continue;
}
if (propertyGroupsByNode.has(subject)) {
continue;
}
const propertyGroup = new PropertyGroup(
new Resource({ dataset: this.dataset, identifier: subject }),
);
propertyGroups.push(propertyGroup);
propertyGroupsByNode.set(subject, propertyGroup);
}
return { propertyGroups, propertyGroupsByNode };
}
private readShapes(): {
nodeShapes: readonly NodeShape[];
nodeShapesByNode: TermMap<BlankNode | NamedNode, NodeShape>;
propertyShapes: readonly PropertyShape[];
propertyShapesByNode: TermMap<BlankNode | NamedNode, PropertyShape>;
} {
const resourceSet = new ResourceSet({ dataset: this.dataset });
// Collect the shape identifiers in sets
const shapeNodeSet = new TermSet<BlankNode | NamedNode>();
// Utility function for doing the collection
const addShapeNode = (shapeNode: Term) => {
switch (shapeNode.termType) {
case "BlankNode":
case "NamedNode":
shapeNodeSet.add(shapeNode);
break;
}
};
// Test each shape condition
// https://www.w3.org/TR/shacl/#shapes
// Subject is a SHACL instance of sh:NodeShape or sh:PropertyShape
for (const rdfType of [sh.NodeShape, sh.PropertyShape]) {
for (const resource of resourceSet.instancesOf(rdfType, {
graph: this.node,
})) {
addShapeNode(resource.identifier);
}
}
// Subject of a triple with sh:targetClass, sh:targetNode, sh:targetObjectsOf, or sh:targetSubjectsOf predicate
for (const predicate of [
sh.targetClass,
sh.targetNode,
sh.targetObjectsOf,
sh.targetSubjectsOf,
]) {
for (const quad of this.dataset.match(null, predicate, null, this.node)) {
addShapeNode(quad.subject);
}
}
// Subject of a triple that has a parameter as predicate
// https://www.w3.org/TR/shacl/#constraints
// https://www.w3.org/TR/shacl/#core-components
for (const predicate of [
sh.class,
sh.datatype,
sh.nodeKind,
sh.minCount,
sh.maxCount,
sh.minExclusive,
sh.minInclusive,
sh.maxExclusive,
sh.maxInclusive,
sh.minLength,
sh.maxLength,
sh.pattern,
sh.languageIn,
sh.uniqueLang,
sh.equals,
sh.disjoint,
sh.lessThan,
sh.lessThanOrEquals,
sh.not,
sh.and,
sh.or,
sh.xone,
sh.node,
sh.property,
sh.qualifiedValueShape,
sh.qualifiedMinCount,
sh.qualifiedMaxCount,
sh.closed,
sh.ignoredProperties,
sh.hasValue,
sh.in,
]) {
for (const quad of this.dataset.match(null, predicate, null, this.node)) {
addShapeNode(quad.subject);
}
}
// Object of a shape-expecting, non-list-taking parameter such as sh:node
for (const predicate of [sh.node, sh.property]) {
for (const quad of this.dataset.match(null, predicate, null, this.node)) {
addShapeNode(quad.object);
}
}
// Member of a SHACL list that is a value of a shape-expecting and list-taking parameter such as sh:or
// biome-ignore lint/complexity/useLiteralKeys: <explanation>
for (const predicate of [sh.and, sh["in"], sh.languageIn, sh.or, sh.xone]) {
for (const quad of this.dataset.match(null, predicate, null, this.node)) {
switch (quad.object.termType) {
case "BlankNode":
case "NamedNode":
break;
default:
continue;
}
for (const value of resourceSet
.resource(quad.object)
.toList()
.orDefault([])) {
value.toIdentifier().ifRight(addShapeNode);
}
}
}
// Separate shapes into node and property shapes.
const nodeShapes: NodeShape[] = [];
const nodeShapesByNode: TermMap<BlankNode | NamedNode, NodeShape> =
new TermMap();
const propertyShapes: PropertyShape[] = [];
const propertyShapesByNode: TermMap<BlankNode | NamedNode, PropertyShape> =
new TermMap();
for (const shapeNode of shapeNodeSet) {
if (this.dataset.match(shapeNode, sh.path, null, this.node).size > 0) {
// 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.
const propertyShape = new PropertyShape(
resourceSet.resource(shapeNode),
this,
);
propertyShapes.push(propertyShape);
propertyShapesByNode.set(shapeNode, propertyShape);
} else {
// 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.
const nodeShape = new NodeShape(resourceSet.resource(shapeNode), this);
nodeShapes.push(nodeShape);
nodeShapesByNode.set(shapeNode, nodeShape);
}
}
return {
nodeShapes,
nodeShapesByNode,
propertyShapes,
propertyShapesByNode,
};
}
}