rdfjs-resource 2.0.7 → 3.0.0
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/README.md +2 -25
- package/dist/DatasetValues.d.ts +14 -8
- package/dist/DatasetValues.js +156 -2
- package/dist/MissingValueError.d.ts +3 -3
- package/dist/MissingValueError.js +4 -3
- package/dist/MistypedPrimitiveValueError.d.ts +13 -0
- package/dist/MistypedPrimitiveValueError.js +15 -0
- package/dist/MistypedTermValueError.d.ts +5 -6
- package/dist/MistypedTermValueError.js +8 -7
- package/dist/MistypedValueError.d.ts +15 -0
- package/dist/MistypedValueError.js +13 -0
- package/dist/PropertyPath.d.ts +47 -0
- package/dist/PropertyPath.js +153 -0
- package/dist/Resource.d.ts +21 -35
- package/dist/Resource.js +28 -47
- package/dist/Term.d.ts +6 -0
- package/dist/Term.js +31 -0
- package/dist/Value.d.ts +103 -0
- package/dist/Value.js +173 -0
- package/dist/ValueError.d.ts +4 -4
- package/dist/ValueError.js +2 -2
- package/dist/Values.d.ts +42 -34
- package/dist/Values.js +42 -19
- package/dist/vocabularies.d.ts +7 -0
- package/dist/vocabularies.js +8 -0
- package/package.json +2 -1
- package/dist/AbstractTermValue.d.ts +0 -32
- package/dist/AbstractTermValue.js +0 -50
- package/dist/DatasetObjectValues.d.ts +0 -14
- package/dist/DatasetObjectValues.js +0 -71
- package/dist/DatasetSubjectValues.d.ts +0 -12
- package/dist/DatasetSubjectValues.js +0 -47
- package/dist/IdentifierValue.d.ts +0 -11
- package/dist/IdentifierValue.js +0 -16
- package/dist/TermValue.d.ts +0 -73
- package/dist/TermValue.js +0 -127
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ npm i rdfjs-resource
|
|
|
17
17
|
### Wrap an RDF/JS `BlankNode` or `NamedNode` in a `Resource`
|
|
18
18
|
|
|
19
19
|
```
|
|
20
|
-
const resource = new Resource(
|
|
20
|
+
const resource = new Resource(dataset, identifier);
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
### Retrieve a value (object) of the `Resource`
|
|
@@ -32,34 +32,11 @@ const value = resource.value(rdf.type).toNumber().orDefault(0);
|
|
|
32
32
|
|
|
33
33
|
See the `Resource.test.ts` for additional usage.
|
|
34
34
|
|
|
35
|
-
### `MutableResource`
|
|
36
|
-
|
|
37
|
-
`Resource` instances are immutable. To create a mutable resource, you have to supply an [RDF/JS DataFactory](https://rdf.js.org/data-model-spec/) as well as a graph identifier (`BlankNode | DefaultGraph | NamedNode`) to add/delete quads to/from in the supplied `DatasetCore`.
|
|
38
|
-
|
|
39
|
-
```
|
|
40
|
-
import { rdf } from "@tpluscode/rdf-ns-builders";
|
|
41
|
-
|
|
42
|
-
const mutableResource = new MutableResource({
|
|
43
|
-
dataFactory: DataFactory,
|
|
44
|
-
dataset,
|
|
45
|
-
identifier: DataFactory.namedNode("http://example.com/subject"),
|
|
46
|
-
mutateGraph: DataFactory.defaultGraph(),
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
mutableResource.add(rdf.type, rdf.Resource);
|
|
50
|
-
```
|
|
51
|
-
|
|
52
35
|
### `ResourceSet`
|
|
53
36
|
|
|
54
37
|
For convenience, you can wrap a `DatasetCore` in a `ResourceSet`, then instantiate `Resource`s from that:
|
|
55
38
|
|
|
56
39
|
```
|
|
57
|
-
const resourceSet = new ResourceSet(
|
|
40
|
+
const resourceSet = new ResourceSet(dataset);
|
|
58
41
|
resourceSet.resource(identifier).value(rdf.type);
|
|
59
42
|
```
|
|
60
|
-
|
|
61
|
-
### Named `Resource`
|
|
62
|
-
|
|
63
|
-
`Resource` and `MutableResource` take a type parameter for the `identifier` specified in the resource's constructor. The parameter defaults to `BlankNode | NamedNode`.
|
|
64
|
-
|
|
65
|
-
It's often useful to only deal in named resources (`Resource<NamedNode>`). The `ResourceSet` abstraction provides a `namedResource` factory method for convenience. `MutableResourceSet` provides a similar `namedMutableResource` factory method.
|
package/dist/DatasetValues.d.ts
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
|
-
import type { DataFactory,
|
|
1
|
+
import type { DataFactory, Quad_Graph, Variable } from "@rdfjs/types";
|
|
2
|
+
import type { PropertyPath } from "./PropertyPath.js";
|
|
2
3
|
import type { Resource } from "./Resource.js";
|
|
4
|
+
import type { Term } from "./Term.js";
|
|
5
|
+
import { Value } from "./Value.js";
|
|
3
6
|
import { Values } from "./Values.js";
|
|
4
|
-
export declare
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
constructor({ dataFactory, focusResource, graph,
|
|
7
|
+
export declare class DatasetValues extends Values<Value> {
|
|
8
|
+
private readonly dataFactory;
|
|
9
|
+
private readonly graph;
|
|
10
|
+
private readonly unique;
|
|
11
|
+
constructor({ dataFactory, focusResource, graph, propertyPath, unique, }: {
|
|
9
12
|
dataFactory: DataFactory;
|
|
10
13
|
focusResource: Resource;
|
|
11
14
|
graph: Exclude<Quad_Graph, Variable> | null;
|
|
12
|
-
|
|
15
|
+
propertyPath: PropertyPath;
|
|
13
16
|
unique: boolean;
|
|
14
17
|
});
|
|
15
18
|
get length(): number;
|
|
16
|
-
|
|
19
|
+
private get dataset();
|
|
20
|
+
[Symbol.iterator](): Iterator<Value<Term>>;
|
|
21
|
+
toArray(): readonly Value<Term>[];
|
|
22
|
+
private terms;
|
|
17
23
|
}
|
|
18
24
|
//# sourceMappingURL=DatasetValues.d.ts.map
|
package/dist/DatasetValues.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import TermSet from "@rdfjs/term-set";
|
|
2
|
+
import { Value } from "./Value.js";
|
|
1
3
|
import { Values } from "./Values.js";
|
|
2
4
|
export class DatasetValues extends Values {
|
|
3
|
-
constructor({ dataFactory, focusResource, graph,
|
|
4
|
-
super({ focusResource,
|
|
5
|
+
constructor({ dataFactory, focusResource, graph, propertyPath, unique, }) {
|
|
6
|
+
super({ focusResource, propertyPath });
|
|
5
7
|
this.dataFactory = dataFactory;
|
|
6
8
|
this.graph = graph;
|
|
7
9
|
this.unique = unique;
|
|
@@ -13,8 +15,160 @@ export class DatasetValues extends Values {
|
|
|
13
15
|
}
|
|
14
16
|
return length;
|
|
15
17
|
}
|
|
18
|
+
get dataset() {
|
|
19
|
+
return this.focusResource.dataset;
|
|
20
|
+
}
|
|
21
|
+
*[Symbol.iterator]() {
|
|
22
|
+
if (this.unique) {
|
|
23
|
+
const uniqueTerms = new TermSet();
|
|
24
|
+
for (const term of this.terms({
|
|
25
|
+
focusIdentifier: this.focusResource.identifier,
|
|
26
|
+
propertyPath: this.propertyPath,
|
|
27
|
+
})) {
|
|
28
|
+
if (uniqueTerms.has(term)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
yield new Value({
|
|
32
|
+
dataFactory: this.dataFactory,
|
|
33
|
+
focusResource: this.focusResource,
|
|
34
|
+
propertyPath: this.propertyPath,
|
|
35
|
+
term,
|
|
36
|
+
});
|
|
37
|
+
uniqueTerms.add(term);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
for (const term of this.terms({
|
|
42
|
+
focusIdentifier: this.focusResource.identifier,
|
|
43
|
+
propertyPath: this.propertyPath,
|
|
44
|
+
})) {
|
|
45
|
+
yield new Value({
|
|
46
|
+
dataFactory: this.dataFactory,
|
|
47
|
+
focusResource: this.focusResource,
|
|
48
|
+
propertyPath: this.propertyPath,
|
|
49
|
+
term,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
16
54
|
toArray() {
|
|
17
55
|
return [...this];
|
|
18
56
|
}
|
|
57
|
+
*terms({ inverse = false, focusIdentifier, propertyPath, }) {
|
|
58
|
+
switch (propertyPath.termType) {
|
|
59
|
+
case "AlternativePath": {
|
|
60
|
+
for (const member of propertyPath.members) {
|
|
61
|
+
yield* this.terms({ inverse, focusIdentifier, propertyPath: member });
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case "InversePath": {
|
|
66
|
+
yield* this.terms({
|
|
67
|
+
inverse: !inverse,
|
|
68
|
+
focusIdentifier,
|
|
69
|
+
propertyPath: propertyPath.path,
|
|
70
|
+
});
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case "NamedNode": {
|
|
74
|
+
const [subject, object] = inverse
|
|
75
|
+
? [null, focusIdentifier]
|
|
76
|
+
: [focusIdentifier, null];
|
|
77
|
+
for (const quad of this.dataset.match(subject, propertyPath, object, this.graph)) {
|
|
78
|
+
const term = inverse ? quad.subject : quad.object;
|
|
79
|
+
switch (term.termType) {
|
|
80
|
+
case "BlankNode":
|
|
81
|
+
case "Literal":
|
|
82
|
+
case "NamedNode":
|
|
83
|
+
yield term;
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
throw new Error(`unexpected termType ${term.termType}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case "OneOrMorePath": {
|
|
92
|
+
const visited = new TermSet();
|
|
93
|
+
const queue = [focusIdentifier];
|
|
94
|
+
while (queue.length > 0) {
|
|
95
|
+
// biome-ignore lint/style/noNonNullAssertion: .length > 0
|
|
96
|
+
const node = queue.shift();
|
|
97
|
+
if (visited.has(node))
|
|
98
|
+
continue;
|
|
99
|
+
visited.add(node);
|
|
100
|
+
for (const next of this.terms({
|
|
101
|
+
inverse,
|
|
102
|
+
focusIdentifier: node,
|
|
103
|
+
propertyPath: propertyPath.path,
|
|
104
|
+
})) {
|
|
105
|
+
if (next.termType === "NamedNode" ||
|
|
106
|
+
next.termType === "BlankNode") {
|
|
107
|
+
queue.push(next);
|
|
108
|
+
}
|
|
109
|
+
yield next;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "SequencePath": {
|
|
115
|
+
const members = inverse
|
|
116
|
+
? [...propertyPath.members].reverse()
|
|
117
|
+
: propertyPath.members;
|
|
118
|
+
let reached = new TermSet([focusIdentifier]);
|
|
119
|
+
for (const member of members) {
|
|
120
|
+
const nextReached = new TermSet();
|
|
121
|
+
for (const node of reached) {
|
|
122
|
+
if (node.termType === "NamedNode" ||
|
|
123
|
+
node.termType === "BlankNode") {
|
|
124
|
+
for (const next of this.terms({
|
|
125
|
+
inverse,
|
|
126
|
+
focusIdentifier: node,
|
|
127
|
+
propertyPath: member,
|
|
128
|
+
})) {
|
|
129
|
+
nextReached.add(next);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
reached = nextReached;
|
|
134
|
+
}
|
|
135
|
+
yield* reached;
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case "ZeroOrMorePath": {
|
|
139
|
+
yield focusIdentifier;
|
|
140
|
+
const visited = new TermSet();
|
|
141
|
+
const queue = [focusIdentifier];
|
|
142
|
+
while (queue.length > 0) {
|
|
143
|
+
// biome-ignore lint/style/noNonNullAssertion: .length > 0
|
|
144
|
+
const node = queue.shift();
|
|
145
|
+
if (visited.has(node))
|
|
146
|
+
continue;
|
|
147
|
+
visited.add(node);
|
|
148
|
+
for (const next of this.terms({
|
|
149
|
+
inverse,
|
|
150
|
+
focusIdentifier: node,
|
|
151
|
+
propertyPath: propertyPath.path,
|
|
152
|
+
})) {
|
|
153
|
+
if (next.termType === "NamedNode" ||
|
|
154
|
+
next.termType === "BlankNode") {
|
|
155
|
+
queue.push(next);
|
|
156
|
+
}
|
|
157
|
+
yield next;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case "ZeroOrOnePath": {
|
|
163
|
+
yield focusIdentifier;
|
|
164
|
+
yield* this.terms({
|
|
165
|
+
inverse,
|
|
166
|
+
focusIdentifier,
|
|
167
|
+
propertyPath: propertyPath.path,
|
|
168
|
+
});
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
19
173
|
}
|
|
20
174
|
//# sourceMappingURL=DatasetValues.js.map
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { PropertyPath } from "./PropertyPath.js";
|
|
2
2
|
import type { Resource } from "./Resource.js";
|
|
3
3
|
import { ValueError } from "./ValueError.js";
|
|
4
4
|
export declare class MissingValueError extends ValueError {
|
|
5
|
-
constructor({ focusResource,
|
|
5
|
+
constructor({ focusResource, propertyPath, }: {
|
|
6
6
|
focusResource: Resource;
|
|
7
|
-
|
|
7
|
+
propertyPath: PropertyPath;
|
|
8
8
|
});
|
|
9
9
|
}
|
|
10
10
|
//# sourceMappingURL=MissingValueError.d.ts.map
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Identifier } from "./Identifier.js";
|
|
2
|
+
import { PropertyPath } from "./PropertyPath.js";
|
|
2
3
|
import { ValueError } from "./ValueError.js";
|
|
3
4
|
export class MissingValueError extends ValueError {
|
|
4
|
-
constructor({ focusResource,
|
|
5
|
+
constructor({ focusResource, propertyPath, }) {
|
|
5
6
|
super({
|
|
6
7
|
focusResource,
|
|
7
|
-
message: `${Identifier.toString(focusResource.identifier)} missing ${
|
|
8
|
-
|
|
8
|
+
message: `${Identifier.toString(focusResource.identifier)} missing ${PropertyPath.$toString(propertyPath)}`,
|
|
9
|
+
propertyPath,
|
|
9
10
|
});
|
|
10
11
|
}
|
|
11
12
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MistypedValueError } from "./MistypedValueError.js";
|
|
2
|
+
import type { Primitive } from "./Primitive.js";
|
|
3
|
+
import { PropertyPath } from "./PropertyPath.js";
|
|
4
|
+
import type { Resource } from "./Resource.js";
|
|
5
|
+
export declare class MistypedPrimitiveValueError extends MistypedValueError<Primitive> {
|
|
6
|
+
constructor({ actualValue, expectedValueType, focusResource, propertyPath, }: {
|
|
7
|
+
actualValue: Primitive;
|
|
8
|
+
expectedValueType: string;
|
|
9
|
+
focusResource: Resource;
|
|
10
|
+
propertyPath: PropertyPath;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=MistypedPrimitiveValueError.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Identifier } from "./Identifier.js";
|
|
2
|
+
import { MistypedValueError } from "./MistypedValueError.js";
|
|
3
|
+
import { PropertyPath } from "./PropertyPath.js";
|
|
4
|
+
export class MistypedPrimitiveValueError extends MistypedValueError {
|
|
5
|
+
constructor({ actualValue, expectedValueType, focusResource, propertyPath, }) {
|
|
6
|
+
super({
|
|
7
|
+
actualValue,
|
|
8
|
+
expectedValueType,
|
|
9
|
+
focusResource,
|
|
10
|
+
message: `expected ${Identifier.toString(focusResource.identifier)} ${PropertyPath.$toString(propertyPath)} to be a ${expectedValueType}, was ${typeof actualValue}`,
|
|
11
|
+
propertyPath,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=MistypedPrimitiveValueError.js.map
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type { BlankNode, Literal, NamedNode, Quad, Variable } from "@rdfjs/types";
|
|
2
|
+
import { MistypedValueError } from "./MistypedValueError.js";
|
|
3
|
+
import { PropertyPath } from "./PropertyPath.js";
|
|
2
4
|
import type { Resource } from "./Resource.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
readonly actualValue: BlankNode | Literal | NamedNode | Quad | Variable;
|
|
6
|
-
readonly expectedValueType: string;
|
|
7
|
-
constructor({ actualValue, expectedValueType, focusResource, predicate, }: {
|
|
5
|
+
export declare class MistypedTermValueError extends MistypedValueError<BlankNode | Literal | NamedNode | Quad | Variable> {
|
|
6
|
+
constructor({ actualValue, expectedValueType, focusResource, propertyPath, }: {
|
|
8
7
|
actualValue: BlankNode | Literal | NamedNode | Quad | Variable;
|
|
9
8
|
expectedValueType: string;
|
|
10
9
|
focusResource: Resource;
|
|
11
|
-
|
|
10
|
+
propertyPath: PropertyPath;
|
|
12
11
|
});
|
|
13
12
|
}
|
|
14
13
|
//# sourceMappingURL=MistypedTermValueError.d.ts.map
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { Identifier } from "./Identifier.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { MistypedValueError } from "./MistypedValueError.js";
|
|
3
|
+
import { PropertyPath } from "./PropertyPath.js";
|
|
4
|
+
export class MistypedTermValueError extends MistypedValueError {
|
|
5
|
+
constructor({ actualValue, expectedValueType, focusResource, propertyPath, }) {
|
|
5
6
|
super({
|
|
7
|
+
actualValue,
|
|
8
|
+
expectedValueType,
|
|
6
9
|
focusResource,
|
|
7
|
-
message: `expected ${Identifier.toString(focusResource.identifier)} ${
|
|
8
|
-
|
|
10
|
+
message: `expected ${Identifier.toString(focusResource.identifier)} ${PropertyPath.$toString(propertyPath)} to be a ${expectedValueType}, was ${actualValue.termType}`,
|
|
11
|
+
propertyPath,
|
|
9
12
|
});
|
|
10
|
-
this.actualValue = actualValue;
|
|
11
|
-
this.expectedValueType = expectedValueType;
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
//# sourceMappingURL=MistypedTermValueError.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PropertyPath } from "./PropertyPath.js";
|
|
2
|
+
import type { Resource } from "./Resource.js";
|
|
3
|
+
import { ValueError } from "./ValueError.js";
|
|
4
|
+
export declare abstract class MistypedValueError<T> extends ValueError {
|
|
5
|
+
readonly actualValue: T;
|
|
6
|
+
readonly expectedValueType: string;
|
|
7
|
+
constructor({ actualValue, expectedValueType, focusResource, message, propertyPath, }: {
|
|
8
|
+
actualValue: T;
|
|
9
|
+
expectedValueType: string;
|
|
10
|
+
message: string;
|
|
11
|
+
focusResource: Resource;
|
|
12
|
+
propertyPath: PropertyPath;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=MistypedValueError.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ValueError } from "./ValueError.js";
|
|
2
|
+
export class MistypedValueError extends ValueError {
|
|
3
|
+
constructor({ actualValue, expectedValueType, focusResource, message, propertyPath, }) {
|
|
4
|
+
super({
|
|
5
|
+
focusResource,
|
|
6
|
+
message,
|
|
7
|
+
propertyPath,
|
|
8
|
+
});
|
|
9
|
+
this.actualValue = actualValue;
|
|
10
|
+
this.expectedValueType = expectedValueType;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=MistypedValueError.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { NamedNode, Quad_Graph, Variable } from "@rdfjs/types";
|
|
2
|
+
import { Either } from "purify-ts";
|
|
3
|
+
import { Resource } from "./Resource.js";
|
|
4
|
+
import { ResourceSet } from "./ResourceSet.js";
|
|
5
|
+
interface AlternativePath {
|
|
6
|
+
readonly termType: "AlternativePath";
|
|
7
|
+
readonly members: readonly PropertyPath[];
|
|
8
|
+
}
|
|
9
|
+
interface InversePath {
|
|
10
|
+
readonly termType: "InversePath";
|
|
11
|
+
readonly path: PropertyPath;
|
|
12
|
+
}
|
|
13
|
+
interface OneOrMorePath {
|
|
14
|
+
readonly termType: "OneOrMorePath";
|
|
15
|
+
readonly path: PropertyPath;
|
|
16
|
+
}
|
|
17
|
+
type PredicatePath = NamedNode;
|
|
18
|
+
interface SequencePath {
|
|
19
|
+
readonly termType: "SequencePath";
|
|
20
|
+
readonly members: readonly PropertyPath[];
|
|
21
|
+
}
|
|
22
|
+
interface ZeroOrMorePath {
|
|
23
|
+
readonly termType: "ZeroOrMorePath";
|
|
24
|
+
readonly path: PropertyPath;
|
|
25
|
+
}
|
|
26
|
+
interface ZeroOrOnePath {
|
|
27
|
+
readonly termType: "ZeroOrOnePath";
|
|
28
|
+
readonly path: PropertyPath;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A SHACL property path.
|
|
32
|
+
*/
|
|
33
|
+
export type PropertyPath = AlternativePath | InversePath | OneOrMorePath | PredicatePath | SequencePath | ZeroOrMorePath | ZeroOrOnePath;
|
|
34
|
+
export declare namespace PropertyPath {
|
|
35
|
+
function $fromRdf(resource: Resource, options?: {
|
|
36
|
+
graph?: Exclude<Quad_Graph, Variable>;
|
|
37
|
+
}): Either<Error, PropertyPath>;
|
|
38
|
+
type $Filter = object;
|
|
39
|
+
function $filter(_filter: $Filter, _value: PropertyPath): boolean;
|
|
40
|
+
function $toString(propertyPath: PropertyPath): string;
|
|
41
|
+
function $toRdf(propertyPath: PropertyPath, options?: {
|
|
42
|
+
graph?: Exclude<Quad_Graph, Variable>;
|
|
43
|
+
resourceSet?: ResourceSet;
|
|
44
|
+
}): Resource;
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
47
|
+
//# sourceMappingURL=PropertyPath.d.ts.map
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import dataFactory from "@rdfjs/data-model";
|
|
2
|
+
import datasetFactory from "@rdfjs/dataset";
|
|
3
|
+
import { Either, Left } from "purify-ts";
|
|
4
|
+
import { Resource } from "./Resource.js";
|
|
5
|
+
import { ResourceSet } from "./ResourceSet.js";
|
|
6
|
+
import { sh } from "./vocabularies.js";
|
|
7
|
+
export var PropertyPath;
|
|
8
|
+
(function (PropertyPath) {
|
|
9
|
+
function $fromRdf(resource, options) {
|
|
10
|
+
// Predicate path
|
|
11
|
+
// sh:path ex:parent
|
|
12
|
+
if (resource.identifier.termType === "NamedNode") {
|
|
13
|
+
return Either.of(resource.identifier);
|
|
14
|
+
}
|
|
15
|
+
// The other property path types are BlankNodes
|
|
16
|
+
const getPropertyPathList = (list) => {
|
|
17
|
+
return list.chain((values) => {
|
|
18
|
+
const members = [];
|
|
19
|
+
for (const value of values) {
|
|
20
|
+
const resource = value.toResource().toMaybe();
|
|
21
|
+
if (resource.isNothing()) {
|
|
22
|
+
return Left(new Error("non-identifier in property path list"));
|
|
23
|
+
}
|
|
24
|
+
const member = PropertyPath.$fromRdf(resource.unsafeCoerce());
|
|
25
|
+
if (member.isLeft()) {
|
|
26
|
+
return member;
|
|
27
|
+
}
|
|
28
|
+
members.push(member.unsafeCoerce());
|
|
29
|
+
}
|
|
30
|
+
return Either.of(members);
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
// Sequence path
|
|
34
|
+
// sh:path ( ex:parent ex:firstName )
|
|
35
|
+
{
|
|
36
|
+
const list = resource.toList();
|
|
37
|
+
if (list.isRight()) {
|
|
38
|
+
return getPropertyPathList(list).map((members) => ({
|
|
39
|
+
termType: "SequencePath",
|
|
40
|
+
members,
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const quad of resource.dataset.match(resource.identifier, null, null, options?.graph)) {
|
|
45
|
+
switch (quad.object.termType) {
|
|
46
|
+
case "BlankNode":
|
|
47
|
+
case "NamedNode":
|
|
48
|
+
break;
|
|
49
|
+
default:
|
|
50
|
+
return Left(new Error(`non-BlankNode/NamedNode property path object on path ${Resource.Identifier.toString(resource.identifier)}: ${quad.object.termType} ${quad.object.value}`));
|
|
51
|
+
}
|
|
52
|
+
const objectResource = new Resource(resource.dataset, quad.object);
|
|
53
|
+
// Alternative path
|
|
54
|
+
// sh:path: [ sh:alternativePath ( ex:father ex:mother ) ]
|
|
55
|
+
if (quad.predicate.equals(sh.alternativePath)) {
|
|
56
|
+
return getPropertyPathList(objectResource.toList()).map((members) => ({
|
|
57
|
+
termType: "AlternativePath",
|
|
58
|
+
members,
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
// Inverse path
|
|
62
|
+
// sh:path: [ sh:inversePath ex:parent ]
|
|
63
|
+
if (quad.predicate.equals(sh.inversePath)) {
|
|
64
|
+
return PropertyPath.$fromRdf(objectResource).map((path) => ({
|
|
65
|
+
termType: "InversePath",
|
|
66
|
+
path,
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
// One or more path
|
|
70
|
+
if (quad.predicate.equals(sh.oneOrMorePath)) {
|
|
71
|
+
return PropertyPath.$fromRdf(objectResource).map((path) => ({
|
|
72
|
+
termType: "OneOrMorePath",
|
|
73
|
+
path,
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
// Zero or more path
|
|
77
|
+
if (quad.predicate.equals(sh.zeroOrMorePath)) {
|
|
78
|
+
return PropertyPath.$fromRdf(objectResource).map((path) => ({
|
|
79
|
+
termType: "ZeroOrMorePath",
|
|
80
|
+
path,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
if (quad.predicate.equals(sh.zeroOrOnePath)) {
|
|
84
|
+
return PropertyPath.$fromRdf(objectResource).map((path) => ({
|
|
85
|
+
termType: "ZeroOrOnePath",
|
|
86
|
+
path,
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return Left(new Error(`unrecognized or ill-formed SHACL property path ${Resource.Identifier.toString(resource.identifier)}`));
|
|
91
|
+
}
|
|
92
|
+
PropertyPath.$fromRdf = $fromRdf;
|
|
93
|
+
function $filter(_filter, _value) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
PropertyPath.$filter = $filter;
|
|
97
|
+
function $toString(propertyPath) {
|
|
98
|
+
switch (propertyPath.termType) {
|
|
99
|
+
case "AlternativePath":
|
|
100
|
+
case "SequencePath":
|
|
101
|
+
return `${propertyPath.termType}([${propertyPath.members.map(PropertyPath.toString).join(", ")}])`;
|
|
102
|
+
case "InversePath":
|
|
103
|
+
case "OneOrMorePath":
|
|
104
|
+
case "ZeroOrMorePath":
|
|
105
|
+
case "ZeroOrOnePath":
|
|
106
|
+
return `${propertyPath.termType}(${PropertyPath.$toString(propertyPath.path)})`;
|
|
107
|
+
case "NamedNode":
|
|
108
|
+
return `PredicatePath(${propertyPath.value})`;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
PropertyPath.$toString = $toString;
|
|
112
|
+
function $toRdf(propertyPath, options) {
|
|
113
|
+
const graph = options?.graph;
|
|
114
|
+
const resourceSet = options?.resourceSet ??
|
|
115
|
+
new ResourceSet(datasetFactory.dataset(), { dataFactory: dataFactory });
|
|
116
|
+
if (propertyPath.termType === "NamedNode") {
|
|
117
|
+
return resourceSet.resource(propertyPath);
|
|
118
|
+
}
|
|
119
|
+
const resource = resourceSet.resource(dataFactory.blankNode());
|
|
120
|
+
switch (propertyPath.termType) {
|
|
121
|
+
case "AlternativePath":
|
|
122
|
+
case "SequencePath": {
|
|
123
|
+
const members = propertyPath.members.map((member) => PropertyPath.$toRdf(member, { graph, resourceSet }).identifier);
|
|
124
|
+
if (propertyPath.termType === "AlternativePath") {
|
|
125
|
+
resource.addList(sh.alternativePath, members, { graph });
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
resource.addListItems(members, { graph });
|
|
129
|
+
}
|
|
130
|
+
return resource;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
let predicate;
|
|
134
|
+
switch (propertyPath.termType) {
|
|
135
|
+
case "InversePath":
|
|
136
|
+
predicate = sh.inversePath;
|
|
137
|
+
break;
|
|
138
|
+
case "OneOrMorePath":
|
|
139
|
+
predicate = sh.oneOrMorePath;
|
|
140
|
+
break;
|
|
141
|
+
case "ZeroOrMorePath":
|
|
142
|
+
predicate = sh.zeroOrMorePath;
|
|
143
|
+
break;
|
|
144
|
+
case "ZeroOrOnePath":
|
|
145
|
+
predicate = sh.zeroOrOnePath;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
resource.add(predicate, PropertyPath.$toRdf(propertyPath.path, { graph, resourceSet }).identifier, graph);
|
|
149
|
+
return resource;
|
|
150
|
+
}
|
|
151
|
+
PropertyPath.$toRdf = $toRdf;
|
|
152
|
+
})(PropertyPath || (PropertyPath = {}));
|
|
153
|
+
//# sourceMappingURL=PropertyPath.js.map
|