rdfjs-resource 1.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/LICENSE +201 -0
- package/MutableResource.d.ts +30 -0
- package/MutableResource.js +46 -0
- package/MutableResourceSet.d.ts +19 -0
- package/MutableResourceSet.js +20 -0
- package/README.md +2 -0
- package/Resource.d.ts +87 -0
- package/Resource.js +258 -0
- package/ResourceSet.d.ts +20 -0
- package/ResourceSet.js +42 -0
- package/createRdfList.d.ts +14 -0
- package/createRdfList.js +33 -0
- package/getRdfInstanceQuads.d.ts +16 -0
- package/getRdfInstanceQuads.js +59 -0
- package/getRdfInstances.d.ts +7 -0
- package/getRdfInstances.js +10 -0
- package/getRdfList.d.ts +7 -0
- package/getRdfList.js +49 -0
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/isRdfInstanceOf.d.ts +6 -0
- package/isRdfInstanceOf.js +44 -0
- package/package.json +51 -0
- package/vitest.config.d.ts +3 -0
- package/vitest.config.js +15 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { BlankNode, DataFactory, NamedNode, Quad, Quad_Graph, Quad_Object, Variable } from "@rdfjs/types";
|
|
2
|
+
import type { Maybe } from "purify-ts";
|
|
3
|
+
import { Resource } from "./Resource.js";
|
|
4
|
+
type Value = Exclude<Quad_Object, Quad | Variable>;
|
|
5
|
+
/**
|
|
6
|
+
* Resource subclass with operations to mutate the underlying dataset.
|
|
7
|
+
*/
|
|
8
|
+
export declare class MutableResource<IdentifierT extends Resource.Identifier = Resource.Identifier, MutateGraphT extends MutableResource.MutateGraph = MutableResource.MutateGraph> extends Resource<IdentifierT> {
|
|
9
|
+
readonly dataFactory: DataFactory;
|
|
10
|
+
readonly mutateGraph: MutateGraphT;
|
|
11
|
+
constructor({ dataFactory, mutateGraph, ...resourceParameters }: MutableResource.Parameters<IdentifierT, MutateGraphT>);
|
|
12
|
+
add(predicate: NamedNode, value: Value): this;
|
|
13
|
+
addMaybe(predicate: NamedNode, value: Maybe<Value>): this;
|
|
14
|
+
addList(predicate: NamedNode, valuesList: Iterable<Value>, options?: MutableResource.AddListOptions): this;
|
|
15
|
+
delete(predicate: NamedNode, value?: Value): this;
|
|
16
|
+
set(predicate: NamedNode, value: Value): this;
|
|
17
|
+
}
|
|
18
|
+
export declare namespace MutableResource {
|
|
19
|
+
interface AddListOptions {
|
|
20
|
+
generateIdentifier?: (item: Value, itemIndex: number) => BlankNode | NamedNode;
|
|
21
|
+
rdfType?: NamedNode;
|
|
22
|
+
}
|
|
23
|
+
type MutateGraph = Exclude<Quad_Graph, Variable>;
|
|
24
|
+
interface Parameters<IdentifierT extends Resource.Identifier, MutateGraphT extends MutateGraph> extends Resource.Parameters<IdentifierT> {
|
|
25
|
+
dataFactory: DataFactory;
|
|
26
|
+
mutateGraph: MutateGraphT;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=MutableResource.d.ts.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { rdf } from "@tpluscode/rdf-ns-builders";
|
|
2
|
+
import { Resource } from "./Resource.js";
|
|
3
|
+
import { createRdfList } from "./createRdfList.js";
|
|
4
|
+
/**
|
|
5
|
+
* Resource subclass with operations to mutate the underlying dataset.
|
|
6
|
+
*/
|
|
7
|
+
export class MutableResource extends Resource {
|
|
8
|
+
constructor({ dataFactory, mutateGraph, ...resourceParameters }) {
|
|
9
|
+
super(resourceParameters);
|
|
10
|
+
this.dataFactory = dataFactory;
|
|
11
|
+
this.mutateGraph = mutateGraph;
|
|
12
|
+
}
|
|
13
|
+
add(predicate, value) {
|
|
14
|
+
this.dataset.add(this.dataFactory.quad(this.identifier, predicate, value, this.mutateGraph));
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
addMaybe(predicate, value) {
|
|
18
|
+
value.ifJust((value) => this.add(predicate, value));
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
addList(predicate, valuesList, options) {
|
|
22
|
+
const listIdentifier = createRdfList({
|
|
23
|
+
dataFactory: this.dataFactory,
|
|
24
|
+
dataset: this.dataset,
|
|
25
|
+
generateIdentifier: options?.generateIdentifier,
|
|
26
|
+
items: valuesList,
|
|
27
|
+
});
|
|
28
|
+
if (options?.rdfType) {
|
|
29
|
+
this.dataset.add(this.dataFactory.quad(listIdentifier, rdf.type, options.rdfType, this.mutateGraph));
|
|
30
|
+
}
|
|
31
|
+
return this.add(predicate, listIdentifier);
|
|
32
|
+
}
|
|
33
|
+
delete(predicate, value) {
|
|
34
|
+
for (const quad of [
|
|
35
|
+
...this.dataset.match(this.identifier, predicate, value, this.mutateGraph),
|
|
36
|
+
]) {
|
|
37
|
+
this.dataset.delete(quad);
|
|
38
|
+
}
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
set(predicate, value) {
|
|
42
|
+
this.delete(predicate);
|
|
43
|
+
return this.add(predicate, value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=MutableResource.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DataFactory, DatasetCore } from "@rdfjs/types";
|
|
2
|
+
import { MutableResource } from "./MutableResource.js";
|
|
3
|
+
import type { Resource } from "./Resource.js";
|
|
4
|
+
import { ResourceSet } from "./ResourceSet.js";
|
|
5
|
+
/**
|
|
6
|
+
* A ResourceSet wraps an RDF/JS dataset with convenient resource factory methods.
|
|
7
|
+
*/
|
|
8
|
+
export declare class MutableResourceSet extends ResourceSet {
|
|
9
|
+
protected readonly dataFactory: DataFactory;
|
|
10
|
+
constructor({ dataFactory, dataset, }: {
|
|
11
|
+
dataFactory: DataFactory;
|
|
12
|
+
dataset: DatasetCore;
|
|
13
|
+
});
|
|
14
|
+
mutableResource({ identifier, mutateGraph, }: {
|
|
15
|
+
identifier: Resource.Identifier;
|
|
16
|
+
mutateGraph: MutableResource.MutateGraph;
|
|
17
|
+
}): MutableResource;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=MutableResourceSet.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MutableResource } from "./MutableResource.js";
|
|
2
|
+
import { ResourceSet } from "./ResourceSet.js";
|
|
3
|
+
/**
|
|
4
|
+
* A ResourceSet wraps an RDF/JS dataset with convenient resource factory methods.
|
|
5
|
+
*/
|
|
6
|
+
export class MutableResourceSet extends ResourceSet {
|
|
7
|
+
constructor({ dataFactory, dataset, }) {
|
|
8
|
+
super({ dataset });
|
|
9
|
+
this.dataFactory = dataFactory;
|
|
10
|
+
}
|
|
11
|
+
mutableResource({ identifier, mutateGraph, }) {
|
|
12
|
+
return new MutableResource({
|
|
13
|
+
dataFactory: this.dataFactory,
|
|
14
|
+
dataset: this.dataset,
|
|
15
|
+
identifier,
|
|
16
|
+
mutateGraph,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=MutableResourceSet.js.map
|
package/README.md
ADDED
package/Resource.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { BlankNode, DataFactory, DatasetCore, Literal, NamedNode, Quad, Quad_Object, Quad_Subject, Variable } from "@rdfjs/types";
|
|
2
|
+
import { Either, Maybe } from "purify-ts";
|
|
3
|
+
/**
|
|
4
|
+
* A Resource abstraction over subjects or objects in an RDF/JS dataset.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Resource<IdentifierT extends Resource.Identifier = Resource.Identifier> {
|
|
7
|
+
readonly dataset: DatasetCore;
|
|
8
|
+
readonly identifier: IdentifierT;
|
|
9
|
+
constructor({ dataset, identifier }: Resource.Parameters<IdentifierT>);
|
|
10
|
+
isInstanceOf(class_: NamedNode, options?: {
|
|
11
|
+
excludeSubclasses?: boolean;
|
|
12
|
+
instanceOfPredicate?: NamedNode;
|
|
13
|
+
subClassOfPredicate?: NamedNode;
|
|
14
|
+
}): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Consider the resource itself as an RDF list.
|
|
17
|
+
*/
|
|
18
|
+
toList(): Either<Error, readonly Resource.Value[]>;
|
|
19
|
+
/**
|
|
20
|
+
* Get the first matching value of dataset statements (this.identifier, property, value).
|
|
21
|
+
*/
|
|
22
|
+
value(property: NamedNode, filter?: (value: Resource.Value) => boolean): Maybe<Resource.Value>;
|
|
23
|
+
/**
|
|
24
|
+
* Get the first matching subject of dataset statements (subject, property, this.identifier).
|
|
25
|
+
*/
|
|
26
|
+
valueOf(property: NamedNode, filter?: (subject: Resource.ValueOf) => boolean): Maybe<Resource.ValueOf>;
|
|
27
|
+
/**
|
|
28
|
+
* Get all values of dataset statements (this.identifier, property, value).
|
|
29
|
+
*/
|
|
30
|
+
values(property: NamedNode): Generator<Resource.Value>;
|
|
31
|
+
/**
|
|
32
|
+
* Get the first subject of dataset statements (subject, property, this.identifier).
|
|
33
|
+
*/
|
|
34
|
+
valuesOf(property: NamedNode): Generator<Resource.ValueOf>;
|
|
35
|
+
}
|
|
36
|
+
export declare namespace Resource {
|
|
37
|
+
type Identifier = BlankNode | NamedNode;
|
|
38
|
+
namespace Identifier {
|
|
39
|
+
function fromString({ dataFactory, identifier, }: {
|
|
40
|
+
dataFactory: DataFactory;
|
|
41
|
+
identifier: string;
|
|
42
|
+
}): BlankNode | NamedNode<string>;
|
|
43
|
+
function toString(identifier: Identifier): string;
|
|
44
|
+
}
|
|
45
|
+
interface Parameters<IdentifierT extends Identifier> {
|
|
46
|
+
dataset: DatasetCore;
|
|
47
|
+
identifier: IdentifierT;
|
|
48
|
+
}
|
|
49
|
+
class Value {
|
|
50
|
+
private readonly object;
|
|
51
|
+
private readonly subjectResource;
|
|
52
|
+
constructor(object: Exclude<Quad_Object, Quad | Variable>, subjectResource: Resource);
|
|
53
|
+
isBoolean(): boolean;
|
|
54
|
+
isDate(): boolean;
|
|
55
|
+
isIdentifier(): boolean;
|
|
56
|
+
isIri(): boolean;
|
|
57
|
+
isList(): boolean;
|
|
58
|
+
isLiteral(): boolean;
|
|
59
|
+
isNumber(): boolean;
|
|
60
|
+
isPrimitive(): boolean;
|
|
61
|
+
isString(): boolean;
|
|
62
|
+
toBoolean(): Maybe<boolean>;
|
|
63
|
+
toDate(): Maybe<Date>;
|
|
64
|
+
toIdentifier(): Maybe<Resource.Identifier>;
|
|
65
|
+
toIri(): Maybe<NamedNode>;
|
|
66
|
+
toLiteral(): Maybe<Literal>;
|
|
67
|
+
toList(): Either<Error, readonly Resource.Value[]>;
|
|
68
|
+
toNamedResource(): Maybe<Resource<NamedNode>>;
|
|
69
|
+
toNumber(): Maybe<number>;
|
|
70
|
+
toPrimitive(): Maybe<boolean | Date | number | string>;
|
|
71
|
+
toResource(): Maybe<Resource>;
|
|
72
|
+
toString(): Maybe<string>;
|
|
73
|
+
toTerm(): Exclude<Quad_Object, Quad | Variable>;
|
|
74
|
+
}
|
|
75
|
+
class ValueOf {
|
|
76
|
+
private readonly objectResource;
|
|
77
|
+
private readonly subject;
|
|
78
|
+
constructor(objectResource: Resource, subject: Exclude<Quad_Subject, Quad | Variable>);
|
|
79
|
+
isIri(): boolean;
|
|
80
|
+
toIdentifier(): Identifier;
|
|
81
|
+
toIri(): Maybe<NamedNode>;
|
|
82
|
+
toNamedResource(): Maybe<Resource<NamedNode>>;
|
|
83
|
+
toResource(): Resource;
|
|
84
|
+
toTerm(): Exclude<Quad_Subject, Quad | Variable>;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=Resource.d.ts.map
|
package/Resource.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { Either, Just, Left, Maybe, Nothing } from "purify-ts";
|
|
2
|
+
import { fromRdf } from "rdf-literal";
|
|
3
|
+
import { getRdfList } from "./getRdfList.js";
|
|
4
|
+
import { isRdfInstanceOf } from "./isRdfInstanceOf.js";
|
|
5
|
+
function defaultValueOfFilter(_valueOf) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
function defaultValueFilter(_value) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A Resource abstraction over subjects or objects in an RDF/JS dataset.
|
|
13
|
+
*/
|
|
14
|
+
export class Resource {
|
|
15
|
+
constructor({ dataset, identifier }) {
|
|
16
|
+
this.dataset = dataset;
|
|
17
|
+
this.identifier = identifier;
|
|
18
|
+
}
|
|
19
|
+
isInstanceOf(class_, options) {
|
|
20
|
+
return isRdfInstanceOf({
|
|
21
|
+
class_,
|
|
22
|
+
dataset: this.dataset,
|
|
23
|
+
instance: this.identifier,
|
|
24
|
+
...options,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Consider the resource itself as an RDF list.
|
|
29
|
+
*/
|
|
30
|
+
toList() {
|
|
31
|
+
return Either.encase(() => [
|
|
32
|
+
...getRdfList({
|
|
33
|
+
dataset: this.dataset,
|
|
34
|
+
node: this.identifier,
|
|
35
|
+
}),
|
|
36
|
+
].map((value) => new Resource.Value(value, this)));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get the first matching value of dataset statements (this.identifier, property, value).
|
|
40
|
+
*/
|
|
41
|
+
value(property, filter) {
|
|
42
|
+
const filter_ = filter ?? defaultValueFilter;
|
|
43
|
+
for (const value of this.values(property)) {
|
|
44
|
+
if (filter_(value)) {
|
|
45
|
+
return Maybe.of(value);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return Maybe.empty();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the first matching subject of dataset statements (subject, property, this.identifier).
|
|
52
|
+
*/
|
|
53
|
+
valueOf(property, filter) {
|
|
54
|
+
const filter_ = filter ?? defaultValueOfFilter;
|
|
55
|
+
for (const valueOf_ of this.valuesOf(property)) {
|
|
56
|
+
if (filter_(valueOf_)) {
|
|
57
|
+
return Just(valueOf_);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return Nothing;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get all values of dataset statements (this.identifier, property, value).
|
|
64
|
+
*/
|
|
65
|
+
*values(property) {
|
|
66
|
+
for (const quad of this.dataset.match(this.identifier, property, null, null)) {
|
|
67
|
+
switch (quad.object.termType) {
|
|
68
|
+
case "BlankNode":
|
|
69
|
+
case "Literal":
|
|
70
|
+
case "NamedNode":
|
|
71
|
+
yield new Resource.Value(quad.object, this);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get the first subject of dataset statements (subject, property, this.identifier).
|
|
78
|
+
*/
|
|
79
|
+
*valuesOf(property) {
|
|
80
|
+
for (const quad of this.dataset.match(null, property, this.identifier, null)) {
|
|
81
|
+
switch (quad.subject.termType) {
|
|
82
|
+
case "BlankNode":
|
|
83
|
+
case "NamedNode":
|
|
84
|
+
yield new Resource.ValueOf(this, quad.subject);
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
(function (Resource) {
|
|
91
|
+
let Identifier;
|
|
92
|
+
(function (Identifier) {
|
|
93
|
+
function fromString({ dataFactory, identifier, }) {
|
|
94
|
+
if (identifier.startsWith("_:")) {
|
|
95
|
+
return dataFactory.blankNode(identifier.substring("_:".length));
|
|
96
|
+
}
|
|
97
|
+
if (identifier.startsWith("<") &&
|
|
98
|
+
identifier.endsWith(">") &&
|
|
99
|
+
identifier.length > 2) {
|
|
100
|
+
return dataFactory.namedNode(identifier.substring(1, identifier.length - 1));
|
|
101
|
+
}
|
|
102
|
+
throw new RangeError(identifier);
|
|
103
|
+
}
|
|
104
|
+
Identifier.fromString = fromString;
|
|
105
|
+
// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation>
|
|
106
|
+
function toString(identifier) {
|
|
107
|
+
switch (identifier.termType) {
|
|
108
|
+
case "BlankNode":
|
|
109
|
+
return `_:${identifier.value}`;
|
|
110
|
+
case "NamedNode":
|
|
111
|
+
return `<${identifier.value}>`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
Identifier.toString = toString;
|
|
115
|
+
})(Identifier = Resource.Identifier || (Resource.Identifier = {}));
|
|
116
|
+
class Value {
|
|
117
|
+
constructor(object, subjectResource) {
|
|
118
|
+
this.object = object;
|
|
119
|
+
this.subjectResource = subjectResource;
|
|
120
|
+
}
|
|
121
|
+
isBoolean() {
|
|
122
|
+
return this.toBoolean().isJust();
|
|
123
|
+
}
|
|
124
|
+
isDate() {
|
|
125
|
+
return this.toDate().isJust();
|
|
126
|
+
}
|
|
127
|
+
isIdentifier() {
|
|
128
|
+
switch (this.object.termType) {
|
|
129
|
+
case "BlankNode":
|
|
130
|
+
case "NamedNode":
|
|
131
|
+
return true;
|
|
132
|
+
default:
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
isIri() {
|
|
137
|
+
return this.object.termType === "NamedNode";
|
|
138
|
+
}
|
|
139
|
+
isList() {
|
|
140
|
+
return this.toList().isRight();
|
|
141
|
+
}
|
|
142
|
+
isLiteral() {
|
|
143
|
+
return this.object.termType === "Literal";
|
|
144
|
+
}
|
|
145
|
+
isNumber() {
|
|
146
|
+
return this.toNumber().isJust();
|
|
147
|
+
}
|
|
148
|
+
isPrimitive() {
|
|
149
|
+
return this.toPrimitive().isJust();
|
|
150
|
+
}
|
|
151
|
+
isString() {
|
|
152
|
+
return this.toString().isJust();
|
|
153
|
+
}
|
|
154
|
+
toBoolean() {
|
|
155
|
+
return this.toPrimitive().chain((primitive) => typeof primitive === "boolean" ? Just(primitive) : Nothing);
|
|
156
|
+
}
|
|
157
|
+
toDate() {
|
|
158
|
+
return this.toPrimitive().chain((primitive) => primitive instanceof Date ? Just(primitive) : Nothing);
|
|
159
|
+
}
|
|
160
|
+
toIdentifier() {
|
|
161
|
+
switch (this.object.termType) {
|
|
162
|
+
case "BlankNode":
|
|
163
|
+
case "NamedNode":
|
|
164
|
+
return Just(this.object);
|
|
165
|
+
default:
|
|
166
|
+
return Nothing;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
toIri() {
|
|
170
|
+
return this.object.termType === "NamedNode" ? Just(this.object) : Nothing;
|
|
171
|
+
}
|
|
172
|
+
toLiteral() {
|
|
173
|
+
return this.object.termType === "Literal" ? Just(this.object) : Nothing;
|
|
174
|
+
}
|
|
175
|
+
toList() {
|
|
176
|
+
const object = this.object;
|
|
177
|
+
switch (object.termType) {
|
|
178
|
+
case "BlankNode":
|
|
179
|
+
case "NamedNode": {
|
|
180
|
+
return Either.encase(() => {
|
|
181
|
+
return [
|
|
182
|
+
...getRdfList({
|
|
183
|
+
dataset: this.subjectResource.dataset,
|
|
184
|
+
node: object,
|
|
185
|
+
}),
|
|
186
|
+
].map((value) => new Value(value, this.subjectResource));
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
default:
|
|
190
|
+
return Left(new Error("object is not an identifier"));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
toNamedResource() {
|
|
194
|
+
return this.toIri().map((identifier) => new Resource({
|
|
195
|
+
dataset: this.subjectResource.dataset,
|
|
196
|
+
identifier,
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
toNumber() {
|
|
200
|
+
return this.toPrimitive().chain((primitive) => typeof primitive === "number" ? Just(primitive) : Nothing);
|
|
201
|
+
}
|
|
202
|
+
toPrimitive() {
|
|
203
|
+
if (this.object.termType !== "Literal") {
|
|
204
|
+
return Nothing;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
return Just(fromRdf(this.object, true));
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
return Nothing;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
toResource() {
|
|
214
|
+
return this.toIdentifier().map((identifier) => new Resource({ dataset: this.subjectResource.dataset, identifier }));
|
|
215
|
+
}
|
|
216
|
+
toString() {
|
|
217
|
+
return this.toPrimitive().chain((primitive) => typeof primitive === "string" ? Just(primitive) : Nothing);
|
|
218
|
+
}
|
|
219
|
+
toTerm() {
|
|
220
|
+
return this.object;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
Resource.Value = Value;
|
|
224
|
+
class ValueOf {
|
|
225
|
+
constructor(objectResource, subject) {
|
|
226
|
+
this.objectResource = objectResource;
|
|
227
|
+
this.subject = subject;
|
|
228
|
+
}
|
|
229
|
+
isIri() {
|
|
230
|
+
return this.subject.termType === "NamedNode";
|
|
231
|
+
}
|
|
232
|
+
toIdentifier() {
|
|
233
|
+
return this.subject;
|
|
234
|
+
}
|
|
235
|
+
toIri() {
|
|
236
|
+
return this.subject.termType === "NamedNode"
|
|
237
|
+
? Just(this.subject)
|
|
238
|
+
: Nothing;
|
|
239
|
+
}
|
|
240
|
+
toNamedResource() {
|
|
241
|
+
return this.toIri().map((identifier) => new Resource({
|
|
242
|
+
dataset: this.objectResource.dataset,
|
|
243
|
+
identifier,
|
|
244
|
+
}));
|
|
245
|
+
}
|
|
246
|
+
toResource() {
|
|
247
|
+
return new Resource({
|
|
248
|
+
dataset: this.objectResource.dataset,
|
|
249
|
+
identifier: this.subject,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
toTerm() {
|
|
253
|
+
return this.subject;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
Resource.ValueOf = ValueOf;
|
|
257
|
+
})(Resource || (Resource = {}));
|
|
258
|
+
//# sourceMappingURL=Resource.js.map
|
package/ResourceSet.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DatasetCore, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { Resource } from "./Resource.js";
|
|
3
|
+
import type { GetRdfInstanceQuadsParameters } from "./getRdfInstanceQuads.js";
|
|
4
|
+
/**
|
|
5
|
+
* A ResourceSet wraps an RDF/JS dataset with convenient resource factory methods.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ResourceSet {
|
|
8
|
+
readonly dataset: DatasetCore;
|
|
9
|
+
constructor({ dataset }: {
|
|
10
|
+
dataset: DatasetCore;
|
|
11
|
+
});
|
|
12
|
+
instancesOf(class_: NamedNode, options?: ResourceSet.InstancesOfOptions): Generator<Resource>;
|
|
13
|
+
namedInstancesOf(class_: NamedNode, options?: ResourceSet.InstancesOfOptions): Generator<Resource<NamedNode>>;
|
|
14
|
+
namedResource(identifier: NamedNode): Resource<NamedNode>;
|
|
15
|
+
resource(identifier: Resource.Identifier): Resource;
|
|
16
|
+
}
|
|
17
|
+
export declare namespace ResourceSet {
|
|
18
|
+
type InstancesOfOptions = Omit<GetRdfInstanceQuadsParameters, "class_" | "dataset">;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=ResourceSet.d.ts.map
|
package/ResourceSet.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Resource } from "./Resource.js";
|
|
2
|
+
import { getRdfInstances } from "./getRdfInstances.js";
|
|
3
|
+
/**
|
|
4
|
+
* A ResourceSet wraps an RDF/JS dataset with convenient resource factory methods.
|
|
5
|
+
*/
|
|
6
|
+
export class ResourceSet {
|
|
7
|
+
constructor({ dataset }) {
|
|
8
|
+
this.dataset = dataset;
|
|
9
|
+
}
|
|
10
|
+
*instancesOf(class_, options) {
|
|
11
|
+
for (const identifier of getRdfInstances({
|
|
12
|
+
class_,
|
|
13
|
+
dataset: this.dataset,
|
|
14
|
+
...options,
|
|
15
|
+
})) {
|
|
16
|
+
yield this.resource(identifier);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
*namedInstancesOf(class_, options) {
|
|
20
|
+
for (const identifier of getRdfInstances({
|
|
21
|
+
class_,
|
|
22
|
+
dataset: this.dataset,
|
|
23
|
+
...options,
|
|
24
|
+
})) {
|
|
25
|
+
if (identifier.termType === "NamedNode")
|
|
26
|
+
yield this.namedResource(identifier);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
namedResource(identifier) {
|
|
30
|
+
return new Resource({
|
|
31
|
+
dataset: this.dataset,
|
|
32
|
+
identifier,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
resource(identifier) {
|
|
36
|
+
return new Resource({
|
|
37
|
+
dataset: this.dataset,
|
|
38
|
+
identifier,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=ResourceSet.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BlankNode, DataFactory, DatasetCore, DefaultGraph, Literal, NamedNode } from "@rdfjs/types";
|
|
2
|
+
/**
|
|
3
|
+
* Create an RDF list for a sequence of items and return the list's identifier
|
|
4
|
+
*
|
|
5
|
+
* The generateIdentifier function can be used to generate custom identifiers for the list and its tails instead of using blank nodes.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createRdfList({ dataFactory, dataset, generateIdentifier, graph, items, }: {
|
|
8
|
+
dataFactory: DataFactory;
|
|
9
|
+
dataset: DatasetCore;
|
|
10
|
+
generateIdentifier?: (item: BlankNode | Literal | NamedNode, itemIndex: number) => BlankNode | NamedNode;
|
|
11
|
+
graph?: BlankNode | DefaultGraph | NamedNode;
|
|
12
|
+
items: Iterable<BlankNode | Literal | NamedNode>;
|
|
13
|
+
}): BlankNode | NamedNode;
|
|
14
|
+
//# sourceMappingURL=createRdfList.d.ts.map
|
package/createRdfList.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { rdf } from "@tpluscode/rdf-ns-builders";
|
|
2
|
+
/**
|
|
3
|
+
* Create an RDF list for a sequence of items and return the list's identifier
|
|
4
|
+
*
|
|
5
|
+
* The generateIdentifier function can be used to generate custom identifiers for the list and its tails instead of using blank nodes.
|
|
6
|
+
*/
|
|
7
|
+
export function createRdfList({ dataFactory, dataset, generateIdentifier, graph, items, }) {
|
|
8
|
+
if (!generateIdentifier) {
|
|
9
|
+
generateIdentifier = () => dataFactory.blankNode();
|
|
10
|
+
}
|
|
11
|
+
let currentHead;
|
|
12
|
+
let list;
|
|
13
|
+
let itemIndex = 0;
|
|
14
|
+
for (const item of items) {
|
|
15
|
+
const newHead = generateIdentifier(item, itemIndex);
|
|
16
|
+
if (currentHead) {
|
|
17
|
+
dataset.add(dataFactory.quad(currentHead, rdf.rest, newHead, graph));
|
|
18
|
+
currentHead = newHead;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
currentHead = list = newHead;
|
|
22
|
+
}
|
|
23
|
+
dataset.add(dataFactory.quad(currentHead, rdf.first, item, graph));
|
|
24
|
+
itemIndex++;
|
|
25
|
+
}
|
|
26
|
+
if (!list) {
|
|
27
|
+
return rdf.nil;
|
|
28
|
+
}
|
|
29
|
+
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
30
|
+
dataset.add(dataFactory.quad(currentHead, rdf.rest, rdf.nil, graph));
|
|
31
|
+
return list;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=createRdfList.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { DatasetCore, NamedNode, Quad, Quad_Graph, Variable } from "@rdfjs/types";
|
|
2
|
+
export interface GetRdfInstanceQuadsParameters {
|
|
3
|
+
class_: NamedNode;
|
|
4
|
+
dataset: DatasetCore;
|
|
5
|
+
excludeSubclasses?: boolean;
|
|
6
|
+
graph?: Exclude<Quad_Graph, Variable> | null;
|
|
7
|
+
instanceOfPredicate?: NamedNode;
|
|
8
|
+
subClassOfPredicate?: NamedNode;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Get all unique RDF instanceQuads of a given class in the given dataset.
|
|
12
|
+
*
|
|
13
|
+
* Returns the quads declaring an instance to be of the given class or one of its subclasses.
|
|
14
|
+
*/
|
|
15
|
+
export declare function getRdfInstanceQuads({ class_, dataset, graph, excludeSubclasses, instanceOfPredicate, subClassOfPredicate, }: GetRdfInstanceQuadsParameters): Generator<Quad>;
|
|
16
|
+
//# sourceMappingURL=getRdfInstanceQuads.d.ts.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import TermSet from "@rdfjs/term-set";
|
|
2
|
+
import { rdf, rdfs } from "@tpluscode/rdf-ns-builders";
|
|
3
|
+
/**
|
|
4
|
+
* Get all unique RDF instanceQuads of a given class in the given dataset.
|
|
5
|
+
*
|
|
6
|
+
* Returns the quads declaring an instance to be of the given class or one of its subclasses.
|
|
7
|
+
*/
|
|
8
|
+
export function* getRdfInstanceQuads({ class_, dataset, graph, excludeSubclasses, instanceOfPredicate, subClassOfPredicate, }) {
|
|
9
|
+
yield* getRdfInstanceQuadsRecursive({
|
|
10
|
+
class_,
|
|
11
|
+
dataset,
|
|
12
|
+
graph: graph ?? null,
|
|
13
|
+
excludeSubclasses: excludeSubclasses ?? false,
|
|
14
|
+
instanceOfPredicate: instanceOfPredicate ?? rdf.type,
|
|
15
|
+
instanceQuads: new TermSet(),
|
|
16
|
+
subClassOfPredicate: subClassOfPredicate ?? rdfs.subClassOf,
|
|
17
|
+
visitedClasses: new TermSet(),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function* getRdfInstanceQuadsRecursive({ class_, dataset, excludeSubclasses, graph, instanceOfPredicate, instanceQuads, subClassOfPredicate, visitedClasses, }) {
|
|
21
|
+
// Get instanceQuads of the class
|
|
22
|
+
for (const quad of dataset.match(null, instanceOfPredicate, class_, graph)) {
|
|
23
|
+
switch (quad.subject.termType) {
|
|
24
|
+
case "BlankNode":
|
|
25
|
+
case "NamedNode":
|
|
26
|
+
if (!instanceQuads.has(quad)) {
|
|
27
|
+
yield quad;
|
|
28
|
+
instanceQuads.add(quad);
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
default:
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
visitedClasses.add(class_);
|
|
36
|
+
if (excludeSubclasses) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
// Recurse into class's sub-classes that haven't been visited yet.
|
|
40
|
+
for (const quad of dataset.match(null, subClassOfPredicate, class_, graph)) {
|
|
41
|
+
if (quad.subject.termType !== "NamedNode") {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (visitedClasses.has(quad.subject)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
yield* getRdfInstanceQuadsRecursive({
|
|
48
|
+
class_: quad.subject,
|
|
49
|
+
dataset,
|
|
50
|
+
excludeSubclasses,
|
|
51
|
+
graph,
|
|
52
|
+
instanceOfPredicate,
|
|
53
|
+
instanceQuads,
|
|
54
|
+
subClassOfPredicate,
|
|
55
|
+
visitedClasses,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=getRdfInstanceQuads.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { type GetRdfInstanceQuadsParameters } from "./getRdfInstanceQuads.js";
|
|
3
|
+
/**
|
|
4
|
+
* Get all unique RDF instances of a given class in the given dataset.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getRdfInstances(kwds: GetRdfInstanceQuadsParameters): Generator<BlankNode | NamedNode>;
|
|
7
|
+
//# sourceMappingURL=getRdfInstances.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { getRdfInstanceQuads, } from "./getRdfInstanceQuads.js";
|
|
2
|
+
/**
|
|
3
|
+
* Get all unique RDF instances of a given class in the given dataset.
|
|
4
|
+
*/
|
|
5
|
+
export function* getRdfInstances(kwds) {
|
|
6
|
+
for (const instanceQuad of getRdfInstanceQuads(kwds)) {
|
|
7
|
+
yield instanceQuad.subject;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=getRdfInstances.js.map
|
package/getRdfList.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BlankNode, DatasetCore, DefaultGraph, Literal, NamedNode } from "@rdfjs/types";
|
|
2
|
+
export declare function getRdfList({ dataset, graph, node, }: {
|
|
3
|
+
dataset: DatasetCore;
|
|
4
|
+
graph?: BlankNode | DefaultGraph | NamedNode | null;
|
|
5
|
+
node: BlankNode | NamedNode;
|
|
6
|
+
}): Generator<BlankNode | Literal | NamedNode>;
|
|
7
|
+
//# sourceMappingURL=getRdfList.d.ts.map
|
package/getRdfList.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import TermSet from "@rdfjs/term-set";
|
|
2
|
+
import { rdf } from "@tpluscode/rdf-ns-builders";
|
|
3
|
+
export function* getRdfList({ dataset, graph, node, }) {
|
|
4
|
+
if (node.equals(rdf.nil)) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const firstObjects = [
|
|
8
|
+
...new TermSet([...dataset.match(node, rdf.first, null, graph)].map((quad) => quad.object)),
|
|
9
|
+
];
|
|
10
|
+
if (firstObjects.length === 0) {
|
|
11
|
+
throw new RangeError(`RDF list ${node.value} has no rdf:first quad`);
|
|
12
|
+
}
|
|
13
|
+
if (firstObjects.length > 1) {
|
|
14
|
+
throw new RangeError(`RDF list ${node.value} has multiple rdf:first objects: ${JSON.stringify(firstObjects.map((object) => object.value))}`);
|
|
15
|
+
}
|
|
16
|
+
const firstObject = firstObjects[0];
|
|
17
|
+
switch (firstObject.termType) {
|
|
18
|
+
case "BlankNode":
|
|
19
|
+
case "Literal":
|
|
20
|
+
case "NamedNode":
|
|
21
|
+
break;
|
|
22
|
+
default:
|
|
23
|
+
throw new RangeError(`rdf:first from ${node.value} must point to a blank or named node or a literal, not ${firstObject.termType}`);
|
|
24
|
+
}
|
|
25
|
+
const restObjects = [
|
|
26
|
+
...new TermSet([...dataset.match(node, rdf.rest, null, graph)].map((quad) => quad.object)),
|
|
27
|
+
];
|
|
28
|
+
if (restObjects.length === 0) {
|
|
29
|
+
throw new RangeError(`RDF list ${node.value} has no rdf:rest quad`);
|
|
30
|
+
}
|
|
31
|
+
if (restObjects.length > 1) {
|
|
32
|
+
throw new RangeError(`RDF list ${node.value} has multiple rdf:rest objects: ${JSON.stringify(restObjects.map((object) => object.value))}`);
|
|
33
|
+
}
|
|
34
|
+
const restObject = restObjects[0];
|
|
35
|
+
switch (restObject.termType) {
|
|
36
|
+
case "BlankNode":
|
|
37
|
+
case "NamedNode":
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
throw new RangeError(`rdf:rest from ${node.value} must point to a blank or named node, not ${restObject.termType}`);
|
|
41
|
+
}
|
|
42
|
+
yield firstObject;
|
|
43
|
+
yield* getRdfList({
|
|
44
|
+
dataset,
|
|
45
|
+
graph,
|
|
46
|
+
node: restObject,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=getRdfList.js.map
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import type { GetRdfInstanceQuadsParameters } from "./getRdfInstanceQuads.js";
|
|
3
|
+
export declare function isRdfInstanceOf({ class_, instance, dataset, excludeSubclasses, instanceOfPredicate, subClassOfPredicate, }: GetRdfInstanceQuadsParameters & {
|
|
4
|
+
instance: BlankNode | NamedNode;
|
|
5
|
+
}): boolean;
|
|
6
|
+
//# sourceMappingURL=isRdfInstanceOf.d.ts.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import TermSet from "@rdfjs/term-set";
|
|
2
|
+
import { rdf, rdfs } from "@tpluscode/rdf-ns-builders";
|
|
3
|
+
export function isRdfInstanceOf({ class_, instance, dataset, excludeSubclasses, instanceOfPredicate, subClassOfPredicate, }) {
|
|
4
|
+
return isRdfInstanceOfRecursive({
|
|
5
|
+
class_,
|
|
6
|
+
dataset,
|
|
7
|
+
excludeSubclasses: excludeSubclasses ?? false,
|
|
8
|
+
instance,
|
|
9
|
+
instanceOfPredicate: instanceOfPredicate ?? rdf.type,
|
|
10
|
+
subClassOfPredicate: subClassOfPredicate ?? rdfs.subClassOf,
|
|
11
|
+
visitedClasses: new TermSet(),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function isRdfInstanceOfRecursive({ class_, dataset, excludeSubclasses, instance, instanceOfPredicate, subClassOfPredicate, visitedClasses, }) {
|
|
15
|
+
for (const _ of dataset.match(instance, instanceOfPredicate, class_)) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
visitedClasses.add(class_);
|
|
19
|
+
if (excludeSubclasses) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
// Recurse into class's sub-classes that haven't been visited yet.
|
|
23
|
+
for (const quad of dataset.match(null, subClassOfPredicate, class_, null)) {
|
|
24
|
+
if (quad.subject.termType !== "NamedNode") {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (visitedClasses.has(quad.subject)) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (isRdfInstanceOfRecursive({
|
|
31
|
+
class_: quad.subject,
|
|
32
|
+
dataset,
|
|
33
|
+
excludeSubclasses,
|
|
34
|
+
instance,
|
|
35
|
+
instanceOfPredicate,
|
|
36
|
+
subClassOfPredicate,
|
|
37
|
+
visitedClasses,
|
|
38
|
+
})) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=isRdfInstanceOf.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"@rdfjs/term-set": "^1.1.0",
|
|
4
|
+
"@rdfjs/types": "^1.1.0",
|
|
5
|
+
"@tpluscode/rdf-ns-builders": "^4.3.0",
|
|
6
|
+
"@types/rdfjs__term-set": "^1.0.2",
|
|
7
|
+
"purify-ts": "~2.1.0",
|
|
8
|
+
"rdf-literal": "^1.3.2"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@biomejs/biome": "1.8.3",
|
|
12
|
+
"@tsconfig/strictest": "^2.0.5",
|
|
13
|
+
"@types/n3": "^1.16.5",
|
|
14
|
+
"n3": "^1.20.4",
|
|
15
|
+
"rimraf": "^6.0.1",
|
|
16
|
+
"typescript": "~5.5",
|
|
17
|
+
"vitest": "^2.0.5"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=20"
|
|
21
|
+
},
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"files": ["*.d.ts", "*.js"],
|
|
24
|
+
"license": "Apache-2.0",
|
|
25
|
+
"name": "rdfjs-resource",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -b",
|
|
28
|
+
"check": "biome check",
|
|
29
|
+
"check:write": "biome check --write",
|
|
30
|
+
"check:write:unsafe": "biome check --write --unsafe",
|
|
31
|
+
"clean": "rimraf *.d.ts* *.js *.js.map __tests__/*.d.ts* __tests__/*.js __tests__/*.js.map tsconfig.tsbuildinfo",
|
|
32
|
+
"format": "biome format",
|
|
33
|
+
"format:write": "biome format --write",
|
|
34
|
+
"format:write:unsafe": "biome format --write --unsafe",
|
|
35
|
+
"rebuild": "run-s clean build",
|
|
36
|
+
"lint": "biome lint",
|
|
37
|
+
"lint:write": "biome lint --write",
|
|
38
|
+
"lint:write:unsafe": "biome lint --write --unsafe",
|
|
39
|
+
"test": "biome check && vitest run",
|
|
40
|
+
"test:coverage": "biome check && vitest run --coverage",
|
|
41
|
+
"test:watch": "vitest watch",
|
|
42
|
+
"watch": "tsc -w --preserveWatchOutput"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/minorg/rdfjs-resource"
|
|
47
|
+
},
|
|
48
|
+
"type": "module",
|
|
49
|
+
"types": "index.d.ts",
|
|
50
|
+
"version": "1.0.0"
|
|
51
|
+
}
|
package/vitest.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="vitest" />
|
|
2
|
+
import { defineConfig } from "vite";
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
coverage: {
|
|
6
|
+
provider: "v8",
|
|
7
|
+
reporter: ["text", "json-summary", "json"],
|
|
8
|
+
// If you want a coverage reports even if your tests are failing, include the reportOnFailure option
|
|
9
|
+
reportOnFailure: true,
|
|
10
|
+
},
|
|
11
|
+
include: ["**/*.test.ts"],
|
|
12
|
+
passWithNoTests: true,
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=vitest.config.js.map
|