@ldo/jsonld-dataset-proxy 0.0.1-alpha.24 → 1.0.0-alpha.1
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 +86 -71
- package/dist/ContextUtil.d.ts +12 -7
- package/dist/ContextUtil.js +69 -31
- package/dist/ContextUtil.js.map +1 -1
- package/dist/JsonldDatasetProxyBuilder.d.ts +4 -3
- package/dist/JsonldDatasetProxyBuilder.js +2 -2
- package/dist/JsonldDatasetProxyBuilder.js.map +1 -1
- package/dist/ProxyContext.d.ts +9 -9
- package/dist/ProxyContext.js +23 -25
- package/dist/ProxyContext.js.map +1 -1
- package/dist/arrayProxy/modifyArray.js +2 -2
- package/dist/arrayProxy/modifyArray.js.map +1 -1
- package/dist/graphOf.d.ts +2 -1
- package/dist/graphOf.js +4 -16
- package/dist/graphOf.js.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +7 -5
- package/dist/index.js.map +1 -1
- package/dist/jsonldDatasetProxy.d.ts +2 -1
- package/dist/jsonldDatasetProxy.js.map +1 -1
- package/dist/language/languagesOf.d.ts +2 -1
- package/dist/language/languagesOf.js +3 -2
- package/dist/language/languagesOf.js.map +1 -1
- package/dist/setProxy/ObjectSetProxy.d.ts +17 -0
- package/dist/setProxy/ObjectSetProxy.js +31 -0
- package/dist/setProxy/ObjectSetProxy.js.map +1 -0
- package/dist/setProxy/SetProxy.d.ts +29 -0
- package/dist/setProxy/SetProxy.js +72 -0
- package/dist/setProxy/SetProxy.js.map +1 -0
- package/dist/setProxy/SubjectSetProxy.d.ts +15 -0
- package/dist/setProxy/SubjectSetProxy.js +38 -0
- package/dist/setProxy/SubjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardObjectSetProxy.d.ts +20 -0
- package/dist/setProxy/WildcardObjectSetProxy.js +49 -0
- package/dist/setProxy/WildcardObjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardSubjectSetProxy.d.ts +19 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js +31 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js.map +1 -0
- package/dist/setProxy/createNewSetProxy.d.ts +5 -0
- package/dist/setProxy/createNewSetProxy.js +27 -0
- package/dist/setProxy/createNewSetProxy.js.map +1 -0
- package/dist/setProxy/isSetProxy.d.ts +2 -0
- package/dist/setProxy/isSetProxy.js +13 -0
- package/dist/setProxy/isSetProxy.js.map +1 -0
- package/dist/setProxy/ldSet/BasicLdSet.d.ts +36 -0
- package/dist/setProxy/ldSet/BasicLdSet.js +193 -0
- package/dist/setProxy/ldSet/BasicLdSet.js.map +1 -0
- package/dist/setProxy/ldSet/LdSet.d.ts +30 -0
- package/dist/setProxy/ldSet/LdSet.js +3 -0
- package/dist/setProxy/ldSet/LdSet.js.map +1 -0
- package/dist/setProxy/set.d.ts +2 -0
- package/dist/setProxy/set.js +9 -0
- package/dist/setProxy/set.js.map +1 -0
- package/dist/subjectProxy/SubjectProxy.d.ts +1 -1
- package/dist/subjectProxy/createSubjectHandler.js +7 -5
- package/dist/subjectProxy/createSubjectHandler.js.map +1 -1
- package/dist/subjectProxy/deleteFromDataset.js +6 -25
- package/dist/subjectProxy/deleteFromDataset.js.map +1 -1
- package/dist/subjectProxy/getValueForKey.d.ts +2 -2
- package/dist/subjectProxy/getValueForKey.js +7 -9
- package/dist/subjectProxy/getValueForKey.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.js +2 -3
- package/dist/types.js.map +1 -1
- package/dist/util/NodeSet.d.ts +0 -2
- package/dist/util/NodeSet.js +0 -10
- package/dist/util/NodeSet.js.map +1 -1
- package/dist/util/RawObject.d.ts +3 -2
- package/dist/util/addObjectToDataset.js +9 -9
- package/dist/util/addObjectToDataset.js.map +1 -1
- package/dist/util/getNodeFromRaw.d.ts +1 -1
- package/dist/util/getNodeFromRaw.js +10 -4
- package/dist/util/getNodeFromRaw.js.map +1 -1
- package/dist/util/isProxy.d.ts +4 -3
- package/dist/util/isProxy.js +2 -2
- package/dist/util/isProxy.js.map +1 -1
- package/dist/util/nodeToJsonldRepresentation.d.ts +3 -2
- package/dist/util/nodeToJsonldRepresentation.js.map +1 -1
- package/package.json +5 -5
- package/src/ContextUtil.ts +118 -35
- package/src/JsonldDatasetProxyBuilder.ts +14 -13
- package/src/LdoJsonldContext.ts +14 -0
- package/src/ProxyContext.ts +47 -39
- package/src/graphOf.ts +16 -27
- package/src/index.ts +7 -5
- package/src/jsonldDatasetProxy.ts +2 -1
- package/src/language/languagesOf.ts +7 -3
- package/src/setProxy/ObjectSetProxy.ts +66 -0
- package/src/setProxy/SetProxy.ts +141 -0
- package/src/setProxy/SubjectSetProxy.ts +85 -0
- package/src/setProxy/WildcardObjectSetProxy.ts +89 -0
- package/src/setProxy/WildcardSubjectSetProxy.ts +58 -0
- package/src/setProxy/createNewSetProxy.ts +47 -0
- package/src/setProxy/isSetProxy.ts +7 -0
- package/src/setProxy/ldSet/BasicLdSet.ts +266 -0
- package/src/setProxy/ldSet/LdSet.ts +189 -0
- package/src/setProxy/set.ts +14 -0
- package/src/subjectProxy/SubjectProxy.ts +1 -1
- package/src/subjectProxy/createSubjectHandler.ts +15 -6
- package/src/subjectProxy/deleteFromDataset.ts +12 -23
- package/src/subjectProxy/getValueForKey.ts +12 -12
- package/src/types.ts +2 -2
- package/src/util/NodeSet.ts +0 -12
- package/src/util/RawObject.ts +10 -2
- package/src/util/addObjectToDataset.ts +15 -9
- package/src/util/getNodeFromRaw.ts +14 -5
- package/src/util/isProxy.ts +7 -6
- package/src/util/nodeToJsonldRepresentation.ts +3 -2
- package/src/arrayProxy/ArrayProxy.ts +0 -20
- package/src/arrayProxy/arrayMethods.ts +0 -219
- package/src/arrayProxy/createArrayHandler.ts +0 -177
- package/src/arrayProxy/isArrayProxy.ts +0 -23
- package/src/arrayProxy/modifyArray.ts +0 -133
package/src/ProxyContext.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import type { GraphNode, QuadMatch } from "@ldo/rdf-utils";
|
|
1
|
+
import type { GraphNode, QuadMatch, SubjectNode } from "@ldo/rdf-utils";
|
|
2
2
|
import type { BlankNode, Dataset, NamedNode } from "@rdfjs/types";
|
|
3
|
-
import type { ArrayProxyTarget } from "./arrayProxy/createArrayHandler";
|
|
4
|
-
import { createArrayHandler } from "./arrayProxy/createArrayHandler";
|
|
5
3
|
import { createSubjectHandler } from "./subjectProxy/createSubjectHandler";
|
|
6
4
|
import type { SubjectProxy } from "./subjectProxy/SubjectProxy";
|
|
7
|
-
import type {
|
|
8
|
-
import { _getUnderlyingArrayTarget } from "./types";
|
|
5
|
+
import type { SetProxy } from "./setProxy/SetProxy";
|
|
9
6
|
import type { ContextUtil } from "./ContextUtil";
|
|
10
7
|
import type { LanguageOrdering } from "./language/languageTypes";
|
|
8
|
+
import { namedNode } from "@rdfjs/data-model";
|
|
9
|
+
import type { RawValue } from "./util/RawObject";
|
|
10
|
+
import { createNewSetProxy } from "./setProxy/createNewSetProxy";
|
|
11
11
|
|
|
12
12
|
export interface ProxyContextOptions {
|
|
13
13
|
dataset: Dataset;
|
|
14
14
|
contextUtil: ContextUtil;
|
|
15
15
|
writeGraphs: GraphNode[];
|
|
16
16
|
languageOrdering: LanguageOrdering;
|
|
17
|
-
prefilledArrayTargets?: ArrayProxyTarget[];
|
|
18
17
|
state?: Record<string, unknown>;
|
|
19
18
|
}
|
|
20
19
|
|
|
20
|
+
const rdfType = namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
|
|
21
|
+
|
|
21
22
|
/**
|
|
22
23
|
* This file keeps track of the target objects used in the proxies.
|
|
23
24
|
* The reason is so that JSON.stringify does not recurse inifinitely
|
|
@@ -25,7 +26,7 @@ export interface ProxyContextOptions {
|
|
|
25
26
|
*/
|
|
26
27
|
export class ProxyContext {
|
|
27
28
|
private subjectMap: Map<string, SubjectProxy> = new Map();
|
|
28
|
-
private
|
|
29
|
+
private setMap: Map<string, SetProxy<NonNullable<RawValue>>> = new Map();
|
|
29
30
|
|
|
30
31
|
readonly dataset: Dataset;
|
|
31
32
|
readonly contextUtil: ContextUtil;
|
|
@@ -39,29 +40,24 @@ export class ProxyContext {
|
|
|
39
40
|
this.writeGraphs = options.writeGraphs;
|
|
40
41
|
this.languageOrdering = options.languageOrdering;
|
|
41
42
|
this.state = options.state || {};
|
|
42
|
-
if (options.prefilledArrayTargets) {
|
|
43
|
-
options.prefilledArrayTargets.forEach((target) => {
|
|
44
|
-
this.createArrayProxy(target[0], target[2], target);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
43
|
}
|
|
48
44
|
|
|
49
45
|
public createSubjectProxy(node: NamedNode | BlankNode): SubjectProxy {
|
|
50
46
|
if (!this.subjectMap.has(node.value)) {
|
|
51
|
-
const proxy =
|
|
52
|
-
{ "@id": node },
|
|
53
|
-
this.createSubjectHandler(),
|
|
54
|
-
) as unknown as SubjectProxy;
|
|
47
|
+
const proxy = this.createNewSubjectProxy(node);
|
|
55
48
|
this.subjectMap.set(node.value, proxy);
|
|
56
49
|
}
|
|
57
50
|
return this.subjectMap.get(node.value) as SubjectProxy;
|
|
58
51
|
}
|
|
59
52
|
|
|
60
|
-
protected
|
|
61
|
-
return
|
|
53
|
+
protected createNewSubjectProxy(node: NamedNode | BlankNode): SubjectProxy {
|
|
54
|
+
return new Proxy(
|
|
55
|
+
{ "@id": node },
|
|
56
|
+
createSubjectHandler(this),
|
|
57
|
+
) as unknown as SubjectProxy;
|
|
62
58
|
}
|
|
63
59
|
|
|
64
|
-
private
|
|
60
|
+
private getSetKey(...quadMatch: QuadMatch) {
|
|
65
61
|
return `${quadMatch[0]?.value || "undefined"}|${
|
|
66
62
|
quadMatch[1]?.value || "undefined"
|
|
67
63
|
}|${quadMatch[2]?.value || "undefined"}|${
|
|
@@ -69,42 +65,54 @@ export class ProxyContext {
|
|
|
69
65
|
}`;
|
|
70
66
|
}
|
|
71
67
|
|
|
72
|
-
public
|
|
68
|
+
public createSetProxy(
|
|
73
69
|
quadMatch: QuadMatch,
|
|
74
|
-
isSubjectOriented
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
)
|
|
84
|
-
this.
|
|
70
|
+
isSubjectOriented?: boolean,
|
|
71
|
+
isLangStringSet?: boolean,
|
|
72
|
+
): SetProxy {
|
|
73
|
+
const key = this.getSetKey(...quadMatch);
|
|
74
|
+
if (!this.setMap.has(key)) {
|
|
75
|
+
const proxy = this.createNewSetProxy(
|
|
76
|
+
quadMatch,
|
|
77
|
+
isSubjectOriented,
|
|
78
|
+
isLangStringSet,
|
|
79
|
+
);
|
|
80
|
+
this.setMap.set(key, proxy);
|
|
85
81
|
}
|
|
86
|
-
return this.
|
|
82
|
+
return this.setMap.get(key)!;
|
|
87
83
|
}
|
|
88
84
|
|
|
89
|
-
protected
|
|
90
|
-
|
|
85
|
+
protected createNewSetProxy(
|
|
86
|
+
quadMatch: QuadMatch,
|
|
87
|
+
isSubjectOriented?: boolean,
|
|
88
|
+
isLangStringSet?: boolean,
|
|
89
|
+
) {
|
|
90
|
+
return createNewSetProxy(
|
|
91
|
+
quadMatch,
|
|
92
|
+
isSubjectOriented ?? false,
|
|
93
|
+
this,
|
|
94
|
+
isLangStringSet,
|
|
95
|
+
);
|
|
91
96
|
}
|
|
92
97
|
|
|
93
98
|
public duplicate(alternativeOptions: Partial<ProxyContextOptions>) {
|
|
94
|
-
const prefilledArrayTargets: ArrayProxyTarget[] = [];
|
|
95
|
-
this.arrayMap.forEach((value) => {
|
|
96
|
-
prefilledArrayTargets.push(value[_getUnderlyingArrayTarget]);
|
|
97
|
-
});
|
|
98
99
|
const fullOptions: ProxyContextOptions = {
|
|
99
100
|
...{
|
|
100
101
|
dataset: this.dataset,
|
|
101
102
|
contextUtil: this.contextUtil,
|
|
102
103
|
writeGraphs: this.writeGraphs,
|
|
103
104
|
languageOrdering: this.languageOrdering,
|
|
104
|
-
prefilledArrayTargets,
|
|
105
105
|
},
|
|
106
106
|
...alternativeOptions,
|
|
107
107
|
};
|
|
108
108
|
return new ProxyContext(fullOptions);
|
|
109
109
|
}
|
|
110
|
+
|
|
111
|
+
public getRdfType(subjectNode: SubjectNode): NamedNode[] {
|
|
112
|
+
return this.dataset
|
|
113
|
+
.match(subjectNode, rdfType)
|
|
114
|
+
.toArray()
|
|
115
|
+
.map((quad) => quad.object)
|
|
116
|
+
.filter((object): object is NamedNode => object.termType === "NamedNode");
|
|
117
|
+
}
|
|
110
118
|
}
|
package/src/graphOf.ts
CHANGED
|
@@ -1,58 +1,47 @@
|
|
|
1
1
|
import type { ObjectNode, GraphNode } from "@ldo/rdf-utils";
|
|
2
2
|
import { namedNode } from "@rdfjs/data-model";
|
|
3
|
-
import {
|
|
4
|
-
getSubjectProxyFromObject,
|
|
5
|
-
isSubjectProxy,
|
|
6
|
-
} from "./subjectProxy/isSubjectProxy";
|
|
3
|
+
import { getSubjectProxyFromObject } from "./subjectProxy/isSubjectProxy";
|
|
7
4
|
import type { ObjectLike } from "./types";
|
|
8
5
|
import {
|
|
9
|
-
_getNodeAtIndex,
|
|
10
6
|
_getUnderlyingDataset,
|
|
11
7
|
_getUnderlyingMatch,
|
|
12
8
|
_getUnderlyingNode,
|
|
13
9
|
_proxyContext,
|
|
14
10
|
} from "./types";
|
|
11
|
+
import type { LdSet } from "./setProxy/ldSet/LdSet";
|
|
12
|
+
import { getNodeFromRawValue } from "./util/getNodeFromRaw";
|
|
15
13
|
|
|
16
14
|
/**
|
|
17
15
|
* Returns the graph for which a defined triple is a member
|
|
18
16
|
* @param subject A JsonldDatasetProxy that represents the subject
|
|
19
17
|
* @param predicate The key on the JsonldDatasetProxy
|
|
20
|
-
* @param object The direct object. This can be a JsonldDatasetProxy
|
|
18
|
+
* @param object The direct object. This can be a JsonldDatasetProxy. This field
|
|
19
|
+
* is optional.
|
|
21
20
|
* @returns a list of graphs for which the triples are members
|
|
22
21
|
*/
|
|
23
22
|
export function graphOf<Subject extends ObjectLike, Key extends keyof Subject>(
|
|
24
23
|
subject: Subject,
|
|
25
24
|
predicate: Key,
|
|
26
|
-
object?: NonNullable<Subject[Key]> extends
|
|
27
|
-
? number | ObjectLike
|
|
28
|
-
: ObjectLike,
|
|
25
|
+
object?: NonNullable<Subject[Key]> extends LdSet<infer T> ? T : Subject[Key],
|
|
29
26
|
): GraphNode[] {
|
|
30
27
|
const subjectProxy = getSubjectProxyFromObject(subject);
|
|
31
28
|
const proxyContext = subjectProxy[_proxyContext];
|
|
32
29
|
const subjectNode = subjectProxy[_getUnderlyingNode];
|
|
33
30
|
const predicateNode = namedNode(
|
|
34
|
-
proxyContext.contextUtil.keyToIri(
|
|
31
|
+
proxyContext.contextUtil.keyToIri(
|
|
32
|
+
predicate as string,
|
|
33
|
+
proxyContext.getRdfType(subjectNode),
|
|
34
|
+
),
|
|
35
35
|
);
|
|
36
|
-
let objectNode: ObjectNode | null;
|
|
36
|
+
let objectNode: ObjectNode | undefined | null;
|
|
37
37
|
if (object == null) {
|
|
38
38
|
objectNode = null;
|
|
39
|
-
} else if (typeof object === "number") {
|
|
40
|
-
const proxyArray = subject[predicate];
|
|
41
|
-
if (!proxyArray[_getUnderlyingMatch]) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
`Key "${String(predicate)}" of ${subject} is not an array.`,
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
if (!proxyArray[object]) {
|
|
47
|
-
throw new Error(`Index ${object} does not exist.`);
|
|
48
|
-
}
|
|
49
|
-
if (isSubjectProxy(proxyArray[object])) {
|
|
50
|
-
objectNode = proxyArray[object][1];
|
|
51
|
-
}
|
|
52
|
-
objectNode = proxyArray[_getNodeAtIndex](object);
|
|
53
39
|
} else {
|
|
54
|
-
const
|
|
55
|
-
|
|
40
|
+
const datatype = proxyContext.contextUtil.getDataType(
|
|
41
|
+
predicate as string,
|
|
42
|
+
proxyContext.getRdfType(subjectNode),
|
|
43
|
+
);
|
|
44
|
+
objectNode = getNodeFromRawValue(object, proxyContext, datatype);
|
|
56
45
|
}
|
|
57
46
|
const quads = subjectProxy[_getUnderlyingDataset].match(
|
|
58
47
|
subjectNode,
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from "./jsonldDatasetProxy";
|
|
|
9
9
|
export * from "./write";
|
|
10
10
|
export * from "./graphOf";
|
|
11
11
|
export * from "./setLanguagePreferences";
|
|
12
|
+
export * from "./LdoJsonldContext";
|
|
12
13
|
|
|
13
14
|
export * from "./language/languagesOf";
|
|
14
15
|
export * from "./language/languageMapProxy";
|
|
@@ -16,11 +17,12 @@ export * from "./language/languageSet";
|
|
|
16
17
|
export * from "./language/languageTypes";
|
|
17
18
|
export * from "./language/languageUtils";
|
|
18
19
|
|
|
19
|
-
export * from "./
|
|
20
|
-
export * from "./
|
|
21
|
-
export * from "./
|
|
22
|
-
export * from "./
|
|
23
|
-
export * from "./
|
|
20
|
+
export * from "./setProxy/createNewSetProxy";
|
|
21
|
+
export * from "./setProxy/isSetProxy";
|
|
22
|
+
export * from "./setProxy/SetProxy";
|
|
23
|
+
export * from "./setProxy/set";
|
|
24
|
+
export * from "./setProxy/ldSet/LdSet";
|
|
25
|
+
export * from "./setProxy/ldSet/BasicLdSet";
|
|
24
26
|
|
|
25
27
|
export * from "./subjectProxy/createSubjectHandler";
|
|
26
28
|
export * from "./subjectProxy/SubjectProxy";
|
|
@@ -4,6 +4,7 @@ import type { ContextDefinition } from "jsonld";
|
|
|
4
4
|
import { ContextUtil } from "./ContextUtil";
|
|
5
5
|
import { JsonldDatasetProxyBuilder } from "./JsonldDatasetProxyBuilder";
|
|
6
6
|
import { ProxyContext } from "./ProxyContext";
|
|
7
|
+
import type { LdoJsonldContext } from "./LdoJsonldContext";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Creates a JSON-LD Dataset Proxy
|
|
@@ -14,7 +15,7 @@ import { ProxyContext } from "./ProxyContext";
|
|
|
14
15
|
*/
|
|
15
16
|
export function jsonldDatasetProxy(
|
|
16
17
|
inputDataset: Dataset,
|
|
17
|
-
context: ContextDefinition,
|
|
18
|
+
context: ContextDefinition | LdoJsonldContext,
|
|
18
19
|
): JsonldDatasetProxyBuilder {
|
|
19
20
|
const contextUtil = new ContextUtil(context);
|
|
20
21
|
const proxyContext = new ProxyContext({
|
|
@@ -3,6 +3,7 @@ import { getSubjectProxyFromObject } from "../subjectProxy/isSubjectProxy";
|
|
|
3
3
|
import type { ObjectLike } from "../types";
|
|
4
4
|
import { _getUnderlyingNode, _proxyContext } from "../types";
|
|
5
5
|
import { createLanguageMapProxy } from "./languageMapProxy";
|
|
6
|
+
import type { LdSet } from "../setProxy/ldSet/LdSet";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* -----------------------------------------------------------------------------
|
|
@@ -25,7 +26,7 @@ export type LanguageSet = Set<string>;
|
|
|
25
26
|
export type LanguageOfConditionalReturn<
|
|
26
27
|
SubjectObject extends ObjectLike,
|
|
27
28
|
Key extends keyof SubjectObject,
|
|
28
|
-
> = NonNullable<SubjectObject[Key]> extends
|
|
29
|
+
> = NonNullable<SubjectObject[Key]> extends LdSet<unknown>
|
|
29
30
|
? LanguageSetMap
|
|
30
31
|
: LanguageMap;
|
|
31
32
|
|
|
@@ -51,11 +52,14 @@ export function languagesOf<
|
|
|
51
52
|
const proxy = getSubjectProxyFromObject(subjectObject);
|
|
52
53
|
const proxyContext = proxy[_proxyContext];
|
|
53
54
|
const subject = proxy[_getUnderlyingNode];
|
|
54
|
-
const
|
|
55
|
+
const rdfTypes = proxyContext.getRdfType(subject);
|
|
56
|
+
const predicate = namedNode(
|
|
57
|
+
proxyContext.contextUtil.keyToIri(key as string, rdfTypes),
|
|
58
|
+
);
|
|
55
59
|
return createLanguageMapProxy<LanguageMap>(
|
|
56
60
|
subject,
|
|
57
61
|
predicate,
|
|
58
62
|
proxyContext,
|
|
59
|
-
proxyContext.contextUtil.
|
|
63
|
+
proxyContext.contextUtil.isSet(key as string, rdfTypes),
|
|
60
64
|
) as LanguageOfConditionalReturn<SubjectObject, Key>;
|
|
61
65
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { GraphNode, PredicateNode, SubjectNode } from "@ldo/rdf-utils";
|
|
2
|
+
import type { RawObject, RawValue } from "../util/RawObject";
|
|
3
|
+
import { WildcardObjectSetProxy } from "./WildcardObjectSetProxy";
|
|
4
|
+
import { addObjectToDataset } from "../util/addObjectToDataset";
|
|
5
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
6
|
+
|
|
7
|
+
export type ObjectSetProxyQuadMatch = [
|
|
8
|
+
SubjectNode,
|
|
9
|
+
PredicateNode,
|
|
10
|
+
undefined | null,
|
|
11
|
+
GraphNode | undefined | null,
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
export class ObjectSetProxy<
|
|
15
|
+
T extends NonNullable<RawValue>,
|
|
16
|
+
> extends WildcardObjectSetProxy<T> {
|
|
17
|
+
protected quadMatch: ObjectSetProxyQuadMatch;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
context: ProxyContext,
|
|
21
|
+
quadMatch: ObjectSetProxyQuadMatch,
|
|
22
|
+
isLangSet?: boolean,
|
|
23
|
+
) {
|
|
24
|
+
super(context, quadMatch, isLangSet);
|
|
25
|
+
this.quadMatch = quadMatch;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Appends a new element with a specified value to the end of the Set.
|
|
30
|
+
*/
|
|
31
|
+
add(value: T): this {
|
|
32
|
+
addObjectToDataset(
|
|
33
|
+
{
|
|
34
|
+
"@id": this.quadMatch[0],
|
|
35
|
+
[this.context.contextUtil.iriToKey(
|
|
36
|
+
this.quadMatch[1].value,
|
|
37
|
+
this.context.getRdfType(this.quadMatch[0]),
|
|
38
|
+
)]: value,
|
|
39
|
+
} as RawObject,
|
|
40
|
+
false,
|
|
41
|
+
this.context,
|
|
42
|
+
);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Clears the set of all values
|
|
48
|
+
*/
|
|
49
|
+
clear(): void {
|
|
50
|
+
for (const value of this) {
|
|
51
|
+
this.delete(value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Deletes an item for the set
|
|
57
|
+
* @param value the item to delete
|
|
58
|
+
* @returns true if the item was present before deletion
|
|
59
|
+
*/
|
|
60
|
+
delete(value: T): boolean {
|
|
61
|
+
const { dataset } = this.context;
|
|
62
|
+
const quads = this.getQuads(value);
|
|
63
|
+
quads.forEach((quad) => dataset.delete(quad));
|
|
64
|
+
return quads.size > 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file handles the underlying functionality of a set, including hidden
|
|
3
|
+
* helper methods
|
|
4
|
+
*/
|
|
5
|
+
import type { Dataset, Quad } from "@rdfjs/types";
|
|
6
|
+
import type {
|
|
7
|
+
GraphNode,
|
|
8
|
+
ObjectNode,
|
|
9
|
+
QuadMatch,
|
|
10
|
+
SubjectNode,
|
|
11
|
+
} from "@ldo/rdf-utils";
|
|
12
|
+
import {
|
|
13
|
+
_isSubjectOriented,
|
|
14
|
+
_getUnderlyingDataset,
|
|
15
|
+
_proxyContext,
|
|
16
|
+
_isLangString,
|
|
17
|
+
_getUnderlyingMatch,
|
|
18
|
+
_getUnderlyingNode,
|
|
19
|
+
_writeGraphs,
|
|
20
|
+
} from "../types";
|
|
21
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
22
|
+
import type { RawValue } from "../util/RawObject";
|
|
23
|
+
import { nodeToJsonldRepresentation } from "../util/nodeToJsonldRepresentation";
|
|
24
|
+
import { BasicLdSet } from "./ldSet/BasicLdSet";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A Set Proxy represents a set of items in a dataset and is a proxy for
|
|
28
|
+
* accessing those items in the dataset.
|
|
29
|
+
*/
|
|
30
|
+
export abstract class SetProxy<
|
|
31
|
+
T extends NonNullable<RawValue> = NonNullable<RawValue>,
|
|
32
|
+
> extends BasicLdSet<T> {
|
|
33
|
+
protected quadMatch: QuadMatch;
|
|
34
|
+
protected context: ProxyContext;
|
|
35
|
+
|
|
36
|
+
constructor(context: ProxyContext, quadMatch: QuadMatch) {
|
|
37
|
+
super();
|
|
38
|
+
this.context = context;
|
|
39
|
+
this.quadMatch = quadMatch;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Gets the subject, predicate and object for this set
|
|
44
|
+
*/
|
|
45
|
+
protected abstract getQuads(value?: T): Dataset<Quad, Quad>;
|
|
46
|
+
|
|
47
|
+
protected abstract getNodeOfFocus(quad: Quad): SubjectNode | ObjectNode;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The add method on a wildcard set does nothing.
|
|
51
|
+
* @deprecated You cannot add data to a wildcard set as it is simply a proxy to an underlying dataset
|
|
52
|
+
*/
|
|
53
|
+
add(_value: T) {
|
|
54
|
+
console.warn(
|
|
55
|
+
'You\'ve attempted to call "add" on a wildcard set. You cannot add data to a wildcard set as it is simply a proxy to an underlying dataset',
|
|
56
|
+
);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The clear method on an abstract set does nothing.
|
|
62
|
+
* @deprecated You cannot clear data from an abstract set as it is simply a proxy to an underlying dataset
|
|
63
|
+
*/
|
|
64
|
+
clear(): void {
|
|
65
|
+
console.warn(
|
|
66
|
+
'You\'ve attempted to call "clear" on an abstract set. You cannot clear data from an abstract set as it is simply a proxy to an underlying dataset',
|
|
67
|
+
);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The delete method on an abstract set does nothing.
|
|
73
|
+
* @deprecated You cannot delete data from an abstract set as it is simply a proxy to an underlying dataset
|
|
74
|
+
*/
|
|
75
|
+
delete(_value: T): boolean {
|
|
76
|
+
console.warn(
|
|
77
|
+
'You\'ve attempted to call "clear" on an abstract set. You cannot delete data from an abstract set as it is simply a proxy to an underlying dataset',
|
|
78
|
+
);
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
has(value: T): boolean {
|
|
83
|
+
return this.getQuads(value).size > 0;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get size() {
|
|
87
|
+
return this.getQuads().size;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
entries(): IterableIterator<[T, T]> {
|
|
91
|
+
const iteratorSet = new Set<[T, T]>();
|
|
92
|
+
for (const value of this) {
|
|
93
|
+
iteratorSet.add([value, value]);
|
|
94
|
+
}
|
|
95
|
+
return iteratorSet[Symbol.iterator]();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
keys(): IterableIterator<T> {
|
|
99
|
+
return this.values();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
values(): IterableIterator<T> {
|
|
103
|
+
return this[Symbol.iterator]();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
[Symbol.iterator](): IterableIterator<T> {
|
|
107
|
+
const quads = this.getQuads();
|
|
108
|
+
const collection: T[] = quads.toArray().map((quad) => {
|
|
109
|
+
const quadSubject = this.getNodeOfFocus(quad);
|
|
110
|
+
return nodeToJsonldRepresentation(quadSubject, this.context) as T;
|
|
111
|
+
});
|
|
112
|
+
return new Set(collection)[Symbol.iterator]();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
get [Symbol.toStringTag]() {
|
|
116
|
+
// TODO: Change this to be human readable.
|
|
117
|
+
return "LdSet";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get [_getUnderlyingDataset](): Dataset {
|
|
121
|
+
return this.context.dataset;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
get [_getUnderlyingMatch](): QuadMatch {
|
|
125
|
+
return this.quadMatch;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get [_proxyContext](): ProxyContext {
|
|
129
|
+
return this.context;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
set [_proxyContext](newContext: ProxyContext) {
|
|
133
|
+
this.context = newContext;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
get [_writeGraphs](): GraphNode[] {
|
|
137
|
+
return this.context.writeGraphs;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
abstract get [_isSubjectOriented](): boolean;
|
|
141
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type GraphNode,
|
|
3
|
+
type ObjectNode,
|
|
4
|
+
type PredicateNode,
|
|
5
|
+
} from "@ldo/rdf-utils";
|
|
6
|
+
import type { RawObject } from "../util/RawObject";
|
|
7
|
+
import { addObjectToDataset } from "../util/addObjectToDataset";
|
|
8
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
9
|
+
import { WildcardSubjectSetProxy } from "./WildcardSubjectSetProxy";
|
|
10
|
+
import { _getUnderlyingNode } from "../types";
|
|
11
|
+
import { defaultGraph, quad } from "@rdfjs/data-model";
|
|
12
|
+
import {
|
|
13
|
+
createTransactionDatasetFactory,
|
|
14
|
+
TransactionDataset,
|
|
15
|
+
} from "@ldo/subscribable-dataset";
|
|
16
|
+
import { createDatasetFactory } from "@ldo/dataset";
|
|
17
|
+
import { getNodeFromRawObject } from "../util/getNodeFromRaw";
|
|
18
|
+
import { nodeToString } from "../util/NodeSet";
|
|
19
|
+
|
|
20
|
+
export type SubjectSetProxyQuadMatch = [
|
|
21
|
+
undefined | null,
|
|
22
|
+
PredicateNode,
|
|
23
|
+
ObjectNode,
|
|
24
|
+
GraphNode | undefined | null,
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
export class SubjectSetProxy<
|
|
28
|
+
T extends RawObject,
|
|
29
|
+
> extends WildcardSubjectSetProxy<T> {
|
|
30
|
+
protected quadMatch: SubjectSetProxyQuadMatch;
|
|
31
|
+
|
|
32
|
+
constructor(context: ProxyContext, quadMatch: SubjectSetProxyQuadMatch) {
|
|
33
|
+
super(context, quadMatch);
|
|
34
|
+
this.quadMatch = quadMatch;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Appends a new element with a specified value to the end of the Set.
|
|
39
|
+
*/
|
|
40
|
+
add(value: T): this {
|
|
41
|
+
if (typeof value !== "object") {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Cannot add a literal "${value}"(${typeof value}) to a subject-oriented collection.`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
// Create a test dataset to see if the inputted data is valid
|
|
47
|
+
const testDataset = new TransactionDataset(
|
|
48
|
+
this.context.dataset,
|
|
49
|
+
createDatasetFactory(),
|
|
50
|
+
createTransactionDatasetFactory(),
|
|
51
|
+
);
|
|
52
|
+
addObjectToDataset(
|
|
53
|
+
value,
|
|
54
|
+
false,
|
|
55
|
+
this.context.duplicate({
|
|
56
|
+
writeGraphs: [defaultGraph()],
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
const isValidAddition =
|
|
60
|
+
testDataset.match(
|
|
61
|
+
getNodeFromRawObject(value, this.context.contextUtil),
|
|
62
|
+
this.quadMatch[1],
|
|
63
|
+
this.quadMatch[2],
|
|
64
|
+
).size !== 0;
|
|
65
|
+
if (!isValidAddition) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Cannot add value to collection. This must contain a quad that matches (${nodeToString(
|
|
68
|
+
this.quadMatch[0],
|
|
69
|
+
)}, ${nodeToString(this.quadMatch[1])}, ${nodeToString(
|
|
70
|
+
this.quadMatch[2],
|
|
71
|
+
)}, ${nodeToString(this.quadMatch[3])})`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Add the object if everything's okay
|
|
76
|
+
const added = addObjectToDataset(value as RawObject, false, this.context);
|
|
77
|
+
const addedNode = added[_getUnderlyingNode];
|
|
78
|
+
this.context.writeGraphs.forEach((graph) => {
|
|
79
|
+
this.context.dataset.add(
|
|
80
|
+
quad(addedNode, this.quadMatch[1], this.quadMatch[2], graph),
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SubjectNode,
|
|
3
|
+
PredicateNode,
|
|
4
|
+
ObjectNode,
|
|
5
|
+
GraphNode,
|
|
6
|
+
} from "@ldo/rdf-utils";
|
|
7
|
+
import type { Dataset, Quad } from "@rdfjs/types";
|
|
8
|
+
import type { RawValue } from "../util/RawObject";
|
|
9
|
+
import { SetProxy } from "./SetProxy";
|
|
10
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
11
|
+
import { getNodeFromRawValue } from "../util/getNodeFromRaw";
|
|
12
|
+
import { _isSubjectOriented } from "../types";
|
|
13
|
+
import { filterQuadsByLanguageOrdering } from "../language/languageUtils";
|
|
14
|
+
|
|
15
|
+
export type WildcardObjectSetProxyQuadMatch = [
|
|
16
|
+
SubjectNode | undefined | null,
|
|
17
|
+
PredicateNode | undefined | null,
|
|
18
|
+
undefined | null,
|
|
19
|
+
GraphNode | undefined | null,
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A WildcardObjectProxy represents a set of nodes in a dataset that are all the
|
|
24
|
+
* object of a given subject and predicate. Because this is a wildcard, the
|
|
25
|
+
* subject and predicate don't necissarily need to be defined.
|
|
26
|
+
*/
|
|
27
|
+
export class WildcardObjectSetProxy<
|
|
28
|
+
T extends NonNullable<RawValue>,
|
|
29
|
+
> extends SetProxy<T> {
|
|
30
|
+
protected quadMatch: WildcardObjectSetProxyQuadMatch;
|
|
31
|
+
protected isLangStringSet: boolean;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
context: ProxyContext,
|
|
35
|
+
quadMatch: WildcardObjectSetProxyQuadMatch,
|
|
36
|
+
isLangStringSet?: boolean,
|
|
37
|
+
) {
|
|
38
|
+
super(context, quadMatch);
|
|
39
|
+
this.quadMatch = quadMatch;
|
|
40
|
+
this.isLangStringSet = isLangStringSet ?? false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected getQuads(value?: T | undefined): Dataset<Quad, Quad> {
|
|
44
|
+
const { dataset } = this.context;
|
|
45
|
+
let quads: Dataset<Quad, Quad>;
|
|
46
|
+
// Get the RDF Node that represents the value, skip if no value
|
|
47
|
+
const subject = this.quadMatch[0] ?? undefined;
|
|
48
|
+
const predicate = this.quadMatch[1] ?? undefined;
|
|
49
|
+
const graph = this.quadMatch[3] ?? undefined;
|
|
50
|
+
if (value) {
|
|
51
|
+
// Get datatype if applicable
|
|
52
|
+
let datatype: string | undefined = undefined;
|
|
53
|
+
if (this.quadMatch[0] && predicate) {
|
|
54
|
+
const rdfType = this.context.getRdfType(this.quadMatch[0]);
|
|
55
|
+
const key = this.context.contextUtil.iriToKey(predicate.value, rdfType);
|
|
56
|
+
datatype = this.context.contextUtil.getDataType(key, rdfType);
|
|
57
|
+
}
|
|
58
|
+
const valueNode = getNodeFromRawValue(value, this.context, datatype);
|
|
59
|
+
quads = dataset.match(subject, predicate, valueNode, graph);
|
|
60
|
+
// If there is no valueNode, we must filter by value manually as we
|
|
61
|
+
// weren't able to deduce the datatype.
|
|
62
|
+
if (!valueNode) {
|
|
63
|
+
quads = quads.filter(
|
|
64
|
+
(quad) =>
|
|
65
|
+
quad.object.termType === "Literal" && quad.object.value === value,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
// SPO for no value
|
|
70
|
+
quads = dataset.match(subject, predicate, undefined, graph);
|
|
71
|
+
}
|
|
72
|
+
// If this is a langStringSet, filter by language preferences
|
|
73
|
+
if (this.isLangStringSet) {
|
|
74
|
+
return filterQuadsByLanguageOrdering(
|
|
75
|
+
quads,
|
|
76
|
+
this.context.languageOrdering,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return quads;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected getNodeOfFocus(quad: Quad): ObjectNode {
|
|
83
|
+
return quad.object as ObjectNode;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
get [_isSubjectOriented](): false {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|