@wazootech/sparql-engine 0.0.0-stage → 0.5.8
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 +21 -0
- package/README.md +729 -2
- package/THIRD_PARTY_NOTICES.md +20 -0
- package/dist/ast-CIKmrXH9.d.ts +282 -0
- package/dist/data-factory-76Dxrf0g.js +286 -0
- package/dist/index.d.ts +365 -0
- package/dist/index.js +5023 -0
- package/dist/memory-store-DhAGI-j6.d.ts +102 -0
- package/dist/memory-store-Mq4x2Ya1.js +316 -0
- package/dist/mod-C4GFefO7.d.ts +78 -0
- package/dist/parser-CTZiNJfu.js +69170 -0
- package/dist/parser.d.ts +65 -0
- package/dist/parser.js +1 -0
- package/dist/public-BNHiaGho.d.ts +234 -0
- package/dist/public-D0uwoGhU.js +416 -0
- package/dist/rdf-terms-DGpxNnyn.d.ts +83 -0
- package/dist/serialize-BSkHhuBp.js +335 -0
- package/dist/serialize.d.ts +2 -0
- package/dist/serialize.js +1 -0
- package/dist/sparql-engine-interface-Bies9KjE.d.ts +102 -0
- package/dist/store.d.ts +2 -0
- package/dist/store.js +1 -0
- package/dist/term-s9I2y-ck.js +767 -0
- package/dist/term.d.ts +2 -0
- package/dist/term.js +4 -0
- package/dist/turtle-parser-CzWF7KXf.d.ts +83 -0
- package/package.json +117 -4
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
The published package contains code derived from the project below. Its license
|
|
4
|
+
is reproduced as that license requires.
|
|
5
|
+
|
|
6
|
+
## SPARQL.js (`sparqljs` 3.7.4)
|
|
7
|
+
|
|
8
|
+
<https://github.com/RubenVerborgh/SPARQL.js>
|
|
9
|
+
|
|
10
|
+
The SPARQL grammar in `src/parser/sparql.jison`, and the parser generated from
|
|
11
|
+
it, are derived from this project.
|
|
12
|
+
|
|
13
|
+
The MIT License (MIT)
|
|
14
|
+
Copyright ©2014 Ruben Verborgh
|
|
15
|
+
|
|
16
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { l as RdfTerm, r as RdfNamedNode, t as RdfBaseQuad } from "./rdf-terms-DGpxNnyn.js";
|
|
2
|
+
type Term = RdfTerm;
|
|
3
|
+
interface VariableTerm {
|
|
4
|
+
termType: "Variable";
|
|
5
|
+
value: string;
|
|
6
|
+
}
|
|
7
|
+
interface Wildcard {
|
|
8
|
+
termType: "Wildcard";
|
|
9
|
+
value: "*";
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* ReifiedQuad is an RDF/JS Quad produced by the parser from SPARQL
|
|
13
|
+
* quoted-triple syntax. Two optional markers distinguish the RDF 1.2 forms:
|
|
14
|
+
*
|
|
15
|
+
* - `tripleTerm` marks data triple terms (`<<( s p o )>>`) — the evaluator
|
|
16
|
+
* treats them as data and never expands them into reifier statements.
|
|
17
|
+
* - `reifier` carries the reifier binding of `<< s p o ~ r >>` so the
|
|
18
|
+
* evaluator binds it instead of minting a fresh internal reifier.
|
|
19
|
+
*/
|
|
20
|
+
interface ReifiedQuad extends RdfBaseQuad {
|
|
21
|
+
tripleTerm?: boolean;
|
|
22
|
+
reifier?: Term;
|
|
23
|
+
}
|
|
24
|
+
type PathElement = RdfNamedNode | PropertyPath;
|
|
25
|
+
interface PropertyPath {
|
|
26
|
+
type: "path";
|
|
27
|
+
pathType: "|" | "/" | "^" | "+" | "*" | "?" | "!";
|
|
28
|
+
items: PathElement[];
|
|
29
|
+
}
|
|
30
|
+
type TriplePredicate = Term | PropertyPath;
|
|
31
|
+
interface Triple {
|
|
32
|
+
subject: Term;
|
|
33
|
+
predicate: TriplePredicate;
|
|
34
|
+
object: Term;
|
|
35
|
+
}
|
|
36
|
+
type Expression = OperationExpression | ExistsExpression | MembershipExpression | FunctionCallExpression | AggregateExpression | Term;
|
|
37
|
+
interface OperationExpression {
|
|
38
|
+
type: "operation";
|
|
39
|
+
operator: string;
|
|
40
|
+
args: Expression[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* ExistsExpression is `EXISTS { ... }` / `NOT EXISTS { ... }`: its single
|
|
44
|
+
* argument is the group graph pattern, not an expression.
|
|
45
|
+
*/
|
|
46
|
+
interface ExistsExpression {
|
|
47
|
+
/**
|
|
48
|
+
* type is its own discriminant, not "operation": the evaluator and the
|
|
49
|
+
* expression walkers switch on it to reach `patterns`, so the native
|
|
50
|
+
* compiler (scriptc) never has to narrow a value out of the operation
|
|
51
|
+
* argument union by assertion.
|
|
52
|
+
*/
|
|
53
|
+
type: "exists";
|
|
54
|
+
operator: "exists" | "notexists";
|
|
55
|
+
/**
|
|
56
|
+
* patterns is the EXISTS group's pattern list. It is a distinct field, not
|
|
57
|
+
* an `args` arm, so the evaluator narrows with `in` instead of casting a
|
|
58
|
+
* value out of the operation-argument union — a cast the native compiler
|
|
59
|
+
* (scriptc) cannot represent.
|
|
60
|
+
*/
|
|
61
|
+
patterns: Pattern[];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* MembershipExpression is `x IN (...)` / `x NOT IN (...)`: its arguments are
|
|
65
|
+
* the tested expression and the list of candidate expressions.
|
|
66
|
+
*/
|
|
67
|
+
interface MembershipExpression {
|
|
68
|
+
/** type is its own discriminant; see ExistsExpression. */
|
|
69
|
+
type: "membership";
|
|
70
|
+
operator: "in" | "notin";
|
|
71
|
+
/** subject is the tested expression. */
|
|
72
|
+
subject: Expression;
|
|
73
|
+
/** candidates are the IN list's expressions. */
|
|
74
|
+
candidates: Expression[];
|
|
75
|
+
}
|
|
76
|
+
interface FunctionCallExpression {
|
|
77
|
+
type: "functionCall";
|
|
78
|
+
function: string | Term;
|
|
79
|
+
args: Expression[];
|
|
80
|
+
/** distinct is set by the parser for `f(DISTINCT ...)` argument lists. */
|
|
81
|
+
distinct?: boolean;
|
|
82
|
+
}
|
|
83
|
+
interface AggregateExpression {
|
|
84
|
+
type: "aggregate";
|
|
85
|
+
aggregation: string;
|
|
86
|
+
/** expression is the aggregated expression, or the wildcard for `COUNT(*)`. */
|
|
87
|
+
expression: Expression | Wildcard;
|
|
88
|
+
/**
|
|
89
|
+
* wildcard marks `COUNT(*)`. It is a flag rather than a re-inspection of
|
|
90
|
+
* `expression` because the native compiler (scriptc) cannot re-box the
|
|
91
|
+
* Wildcard record into the `Expression | Wildcard` union at evaluation
|
|
92
|
+
* time: the record was built inside the parser's dynamic value union.
|
|
93
|
+
*/
|
|
94
|
+
wildcard?: boolean;
|
|
95
|
+
distinct: boolean;
|
|
96
|
+
separator?: string;
|
|
97
|
+
}
|
|
98
|
+
type ValuePatternRow = Record<string, Term | undefined>;
|
|
99
|
+
type Pattern = BgpPattern | FilterPattern | BindPattern | OptionalPattern | UnionPattern | GroupPattern | GraphPattern | MinusPattern | ServicePattern | ValuesPattern | QuadsPattern | SelectSubQuery;
|
|
100
|
+
interface BgpPattern {
|
|
101
|
+
type: "bgp";
|
|
102
|
+
triples: Triple[];
|
|
103
|
+
}
|
|
104
|
+
interface BindPattern {
|
|
105
|
+
type: "bind";
|
|
106
|
+
variable: VariableTerm;
|
|
107
|
+
expression: Expression;
|
|
108
|
+
}
|
|
109
|
+
interface QuadsPattern {
|
|
110
|
+
type: "quads";
|
|
111
|
+
name?: Term;
|
|
112
|
+
triples?: Triple[];
|
|
113
|
+
patterns?: Pattern[];
|
|
114
|
+
}
|
|
115
|
+
type Quads = BgpPattern | GraphPattern | QuadsPattern;
|
|
116
|
+
interface FilterPattern {
|
|
117
|
+
type: "filter";
|
|
118
|
+
expression: Expression;
|
|
119
|
+
}
|
|
120
|
+
interface OptionalPattern {
|
|
121
|
+
type: "optional";
|
|
122
|
+
patterns: Pattern[];
|
|
123
|
+
}
|
|
124
|
+
interface UnionPattern {
|
|
125
|
+
type: "union";
|
|
126
|
+
patterns: Pattern[];
|
|
127
|
+
}
|
|
128
|
+
interface GroupPattern {
|
|
129
|
+
type: "group";
|
|
130
|
+
patterns: Pattern[];
|
|
131
|
+
}
|
|
132
|
+
interface GraphPattern {
|
|
133
|
+
type: "graph";
|
|
134
|
+
name: Term;
|
|
135
|
+
patterns: Pattern[];
|
|
136
|
+
triples?: Triple[];
|
|
137
|
+
}
|
|
138
|
+
interface MinusPattern {
|
|
139
|
+
type: "minus";
|
|
140
|
+
patterns: Pattern[];
|
|
141
|
+
}
|
|
142
|
+
interface ServicePattern {
|
|
143
|
+
type: "service";
|
|
144
|
+
name: Term;
|
|
145
|
+
patterns: Pattern[];
|
|
146
|
+
silent: boolean;
|
|
147
|
+
}
|
|
148
|
+
interface ValuesPattern {
|
|
149
|
+
type: "values";
|
|
150
|
+
values: ValuePatternRow[];
|
|
151
|
+
}
|
|
152
|
+
type SelectSubQuery = SelectQuery;
|
|
153
|
+
interface FromClause {
|
|
154
|
+
default: Term[];
|
|
155
|
+
named: Term[];
|
|
156
|
+
}
|
|
157
|
+
interface Ordering {
|
|
158
|
+
expression: Expression;
|
|
159
|
+
descending?: boolean;
|
|
160
|
+
}
|
|
161
|
+
interface Grouping {
|
|
162
|
+
variable?: VariableTerm;
|
|
163
|
+
expression: Expression;
|
|
164
|
+
}
|
|
165
|
+
interface VariableExpression {
|
|
166
|
+
termType: "VariableExpression";
|
|
167
|
+
variable: VariableTerm;
|
|
168
|
+
expression: Expression;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* QueryModifiers are the solution modifiers (and trailing `VALUES`) every
|
|
172
|
+
* query form accepts in the SPARQL grammar.
|
|
173
|
+
*/
|
|
174
|
+
interface QueryModifiers {
|
|
175
|
+
group?: Grouping[];
|
|
176
|
+
having?: Expression[];
|
|
177
|
+
order?: Ordering[];
|
|
178
|
+
limit?: number;
|
|
179
|
+
offset?: number;
|
|
180
|
+
values?: ValuePatternRow[];
|
|
181
|
+
}
|
|
182
|
+
interface SelectQuery {
|
|
183
|
+
type: "query";
|
|
184
|
+
queryType: "SELECT";
|
|
185
|
+
prefixes?: Record<string, string>;
|
|
186
|
+
base?: string;
|
|
187
|
+
variables: Array<VariableTerm | Wildcard | VariableExpression>;
|
|
188
|
+
where?: Pattern[];
|
|
189
|
+
group?: Grouping[];
|
|
190
|
+
having?: Expression[];
|
|
191
|
+
order?: Ordering[];
|
|
192
|
+
limit?: number;
|
|
193
|
+
offset?: number;
|
|
194
|
+
values?: ValuePatternRow[];
|
|
195
|
+
distinct?: boolean;
|
|
196
|
+
reduced?: boolean;
|
|
197
|
+
from?: FromClause;
|
|
198
|
+
}
|
|
199
|
+
interface AskQuery extends QueryModifiers {
|
|
200
|
+
type: "query";
|
|
201
|
+
queryType: "ASK";
|
|
202
|
+
prefixes?: Record<string, string>;
|
|
203
|
+
base?: string;
|
|
204
|
+
where?: Pattern[];
|
|
205
|
+
from?: FromClause;
|
|
206
|
+
}
|
|
207
|
+
interface ConstructQuery extends QueryModifiers {
|
|
208
|
+
type: "query";
|
|
209
|
+
queryType: "CONSTRUCT";
|
|
210
|
+
prefixes?: Record<string, string>;
|
|
211
|
+
base?: string;
|
|
212
|
+
template?: Triple[];
|
|
213
|
+
where?: Pattern[];
|
|
214
|
+
from?: FromClause;
|
|
215
|
+
}
|
|
216
|
+
interface DescribeQuery extends QueryModifiers {
|
|
217
|
+
type: "query";
|
|
218
|
+
queryType: "DESCRIBE";
|
|
219
|
+
prefixes?: Record<string, string>;
|
|
220
|
+
base?: string;
|
|
221
|
+
variables: Array<VariableTerm | Wildcard>;
|
|
222
|
+
where?: Pattern[];
|
|
223
|
+
from?: FromClause;
|
|
224
|
+
}
|
|
225
|
+
type Query = SelectQuery | AskQuery | ConstructQuery | DescribeQuery;
|
|
226
|
+
interface GraphOrDefault {
|
|
227
|
+
type: "graph" | "default";
|
|
228
|
+
name?: Term;
|
|
229
|
+
}
|
|
230
|
+
interface GraphRefAll {
|
|
231
|
+
type: "graph" | "default" | "named" | "all";
|
|
232
|
+
name?: Term;
|
|
233
|
+
}
|
|
234
|
+
interface InsertDeleteOperation {
|
|
235
|
+
updateType: "insert" | "delete" | "deletewhere" | "deleteinsert" | "insertdelete";
|
|
236
|
+
type?: undefined;
|
|
237
|
+
graph?: Term;
|
|
238
|
+
insert?: Pattern[];
|
|
239
|
+
delete?: Pattern[];
|
|
240
|
+
where?: Pattern[];
|
|
241
|
+
using?: FromClause;
|
|
242
|
+
}
|
|
243
|
+
interface ClearDropOperation {
|
|
244
|
+
type: "clear" | "drop";
|
|
245
|
+
updateType?: undefined;
|
|
246
|
+
silent?: boolean;
|
|
247
|
+
graph: GraphRefAll;
|
|
248
|
+
}
|
|
249
|
+
interface CreateOperation {
|
|
250
|
+
type: "create";
|
|
251
|
+
updateType?: undefined;
|
|
252
|
+
silent?: boolean;
|
|
253
|
+
graph: GraphOrDefault;
|
|
254
|
+
}
|
|
255
|
+
interface CopyMoveOperation {
|
|
256
|
+
type: "copy" | "move" | "add";
|
|
257
|
+
updateType?: undefined;
|
|
258
|
+
silent?: boolean;
|
|
259
|
+
source: GraphOrDefault;
|
|
260
|
+
destination: GraphOrDefault;
|
|
261
|
+
}
|
|
262
|
+
interface LoadOperation {
|
|
263
|
+
type: "load";
|
|
264
|
+
updateType?: undefined;
|
|
265
|
+
silent?: boolean;
|
|
266
|
+
source: Term;
|
|
267
|
+
destination?: Term;
|
|
268
|
+
}
|
|
269
|
+
type UpdateOperation = InsertDeleteOperation | ClearDropOperation | CreateOperation | CopyMoveOperation | LoadOperation;
|
|
270
|
+
interface UpdateQuery {
|
|
271
|
+
type: "update";
|
|
272
|
+
prefixes?: Record<string, string>;
|
|
273
|
+
/**
|
|
274
|
+
* base is the effective base IRI the grammar recorded at parse time (the
|
|
275
|
+
* query's BASE directive, or the parser's base option when there is no
|
|
276
|
+
* directive). Update ASTs carry it like query ASTs.
|
|
277
|
+
*/
|
|
278
|
+
base?: string;
|
|
279
|
+
updates: UpdateOperation[];
|
|
280
|
+
}
|
|
281
|
+
type SparqlQuery = Query | UpdateQuery;
|
|
282
|
+
export { Quads as A, Triple as B, MinusPattern as C, PathElement as D, Ordering as E, SelectQuery as F, ValuePatternRow as G, UnionPattern as H, SelectSubQuery as I, VariableTerm as J, ValuesPattern as K, ServicePattern as L, Query as M, QueryModifiers as N, Pattern as O, ReifiedQuad as P, SparqlQuery as R, MembershipExpression as S, OptionalPattern as T, UpdateOperation as U, TriplePredicate as V, UpdateQuery as W, Wildcard as Y, GraphRefAll as _, ClearDropOperation as a, InsertDeleteOperation as b, CreateOperation as c, Expression as d, FilterPattern as f, GraphPattern as g, GraphOrDefault as h, BindPattern as i, QuadsPattern as j, PropertyPath as k, DescribeQuery as l, FunctionCallExpression as m, AskQuery as n, ConstructQuery as o, FromClause as p, VariableExpression as q, BgpPattern as r, CopyMoveOperation as s, AggregateExpression as t, ExistsExpression as u, GroupPattern as v, OperationExpression as w, LoadOperation as x, Grouping as y, Term as z };
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
// src/parser/term-values.ts
|
|
2
|
+
/**
|
|
3
|
+
* Plain-record RDF/JS terms for the SPARQL AST.
|
|
4
|
+
*
|
|
5
|
+
* The parser builds every term of the AST here instead of through the
|
|
6
|
+
* `DataFactory` classes. The AST is a union of record shapes (`Expression`,
|
|
7
|
+
* `Pattern`, ...), and the native compiler (scriptc, issue #221) cannot hold a
|
|
8
|
+
* class instance in a union of records. A record that carries its own `equals`
|
|
9
|
+
* is still a complete RDF/JS term (`termType`, `value`, `equals`, ...), which
|
|
10
|
+
* is all the evaluator and consumers read; term equality is by value, as in
|
|
11
|
+
* `DataFactory`.
|
|
12
|
+
*/
|
|
13
|
+
const RDF_LANG_STRING$1 = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString";
|
|
14
|
+
const RDF_DIR_LANG_STRING$1 = "http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString";
|
|
15
|
+
const XSD_STRING = "http://www.w3.org/2001/XMLSchema#string";
|
|
16
|
+
function namedNodeEquals(other) {
|
|
17
|
+
return other !== null && other !== void 0 && other.termType === "NamedNode" && other.value === this.value;
|
|
18
|
+
}
|
|
19
|
+
/** namedNodeTerm builds an IRI term. */
|
|
20
|
+
function namedNodeTerm(value) {
|
|
21
|
+
return {
|
|
22
|
+
termType: "NamedNode",
|
|
23
|
+
value,
|
|
24
|
+
equals: namedNodeEquals
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function blankNodeEquals(other) {
|
|
28
|
+
return other !== null && other !== void 0 && other.termType === "BlankNode" && other.value === this.value;
|
|
29
|
+
}
|
|
30
|
+
/** blankNodeTerm builds a blank node term. */
|
|
31
|
+
function blankNodeTerm(value) {
|
|
32
|
+
return {
|
|
33
|
+
termType: "BlankNode",
|
|
34
|
+
value,
|
|
35
|
+
equals: blankNodeEquals
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function variableEquals(other) {
|
|
39
|
+
return other !== null && other !== void 0 && other.termType === "Variable" && other.value === this.value;
|
|
40
|
+
}
|
|
41
|
+
/** variableTerm builds a query variable term. */
|
|
42
|
+
function variableTerm(value) {
|
|
43
|
+
return {
|
|
44
|
+
termType: "Variable",
|
|
45
|
+
value,
|
|
46
|
+
equals: variableEquals
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function defaultGraphEquals(other) {
|
|
50
|
+
return other !== null && other !== void 0 && other.termType === "DefaultGraph";
|
|
51
|
+
}
|
|
52
|
+
const DEFAULT_GRAPH = {
|
|
53
|
+
termType: "DefaultGraph",
|
|
54
|
+
value: "",
|
|
55
|
+
equals: defaultGraphEquals
|
|
56
|
+
};
|
|
57
|
+
/** defaultGraphTerm returns the default graph term. */
|
|
58
|
+
function defaultGraphTerm() {
|
|
59
|
+
return DEFAULT_GRAPH;
|
|
60
|
+
}
|
|
61
|
+
// src/parser/literal-terms.ts
|
|
62
|
+
function literalEquals(other) {
|
|
63
|
+
if (other === null || other === void 0 || other.termType !== "Literal") return false;
|
|
64
|
+
return this.value === other.value && this.language === other.language && (this.direction ?? "") === (other.direction ?? "") && this.datatype.value === other.datatype.value;
|
|
65
|
+
}
|
|
66
|
+
function literalOf(value, language, direction, datatype) {
|
|
67
|
+
return {
|
|
68
|
+
termType: "Literal",
|
|
69
|
+
value,
|
|
70
|
+
language,
|
|
71
|
+
direction,
|
|
72
|
+
datatype,
|
|
73
|
+
equals: literalEquals
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** literalTerm builds a literal with a datatype (xsd:string by default). */
|
|
77
|
+
function literalTerm(value, datatype) {
|
|
78
|
+
return literalOf(value, "", "", datatype === void 0 ? namedNodeTerm(XSD_STRING) : datatype);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* languageLiteralTerm builds a language-tagged literal; a direction makes it
|
|
82
|
+
* an rdf:dirLangString literal (RDF 1.2).
|
|
83
|
+
*/
|
|
84
|
+
function languageLiteralTerm(value, language, direction) {
|
|
85
|
+
return literalOf(value, language.toLowerCase(), direction, namedNodeTerm(direction ? RDF_DIR_LANG_STRING$1 : RDF_LANG_STRING$1));
|
|
86
|
+
}
|
|
87
|
+
// src/term/identity.ts
|
|
88
|
+
/**
|
|
89
|
+
* termKey renders a deterministic key for an RDF/JS term, normalizing its
|
|
90
|
+
* type, value, and — for literals — language and datatype, with RDF-star
|
|
91
|
+
* nesting handled recursively. Two terms produce the same key exactly when
|
|
92
|
+
* they are the same RDF term, so termKey is a sound hash-index key.
|
|
93
|
+
*/
|
|
94
|
+
/**
|
|
95
|
+
* escapeTermValue escapes the characters reserved by the termKey
|
|
96
|
+
* serialization (the `|` field separators and the \` escape marker) so
|
|
97
|
+
* that distinct term values can never render the same key. Backslash is
|
|
98
|
+
* escaped first so escape sequences themselves stay unambiguous.
|
|
99
|
+
*/
|
|
100
|
+
function escapeTermValue(value) {
|
|
101
|
+
return value.replaceAll("\\", "\\\\").replaceAll("|", "\\|");
|
|
102
|
+
}
|
|
103
|
+
function termKey(term) {
|
|
104
|
+
switch (term.termType) {
|
|
105
|
+
case "NamedNode": return `uri:${escapeTermValue(term.value)}`;
|
|
106
|
+
case "BlankNode": return `bnode:${escapeTermValue(term.value)}`;
|
|
107
|
+
case "Variable": return `var:${escapeTermValue(term.value)}`;
|
|
108
|
+
case "DefaultGraph": return "default";
|
|
109
|
+
case "Literal": return `literal:${escapeTermValue(term.value)}|${escapeTermValue(term.language ?? "")}|${escapeTermValue(term.direction ?? "")}|${escapeTermValue(term.datatype?.value ?? "")}`;
|
|
110
|
+
case "Quad": return `quad:${termKey(term.subject)}|${termKey(term.predicate)}|` + termKey(term.object);
|
|
111
|
+
default: throw new Error("Unsupported RDF term type");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* sameRdfTerm compares two RDF/JS terms by type, value, and literal
|
|
116
|
+
* language/datatype. RDF-star triple terms are compared structurally on
|
|
117
|
+
* subject, predicate, and object — the graph is a property of the statement,
|
|
118
|
+
* not of the triple term, so it is not part of term identity.
|
|
119
|
+
*/
|
|
120
|
+
function sameRdfTerm(a, b) {
|
|
121
|
+
if (a.termType !== b.termType) return false;
|
|
122
|
+
switch (a.termType) {
|
|
123
|
+
case "NamedNode": return a.value === b.value;
|
|
124
|
+
case "BlankNode": return a.value === b.value;
|
|
125
|
+
case "Variable": return a.value === b.value;
|
|
126
|
+
case "DefaultGraph": return true;
|
|
127
|
+
case "Literal": {
|
|
128
|
+
const other = b;
|
|
129
|
+
return a.value === other.value && a.language === other.language && (a.direction ?? "") === (other.direction ?? "") && (a.datatype?.value ?? "") === (other.datatype?.value ?? "");
|
|
130
|
+
}
|
|
131
|
+
case "Quad": {
|
|
132
|
+
const other = b;
|
|
133
|
+
return sameRdfTerm(a.subject, other.subject) && sameRdfTerm(a.predicate, other.predicate) && sameRdfTerm(a.object, other.object);
|
|
134
|
+
}
|
|
135
|
+
default: throw new Error("Unsupported RDF term type");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// src/parser/quad-terms.ts
|
|
139
|
+
/** quadComponentsEqual compares quad components to another term. */
|
|
140
|
+
function quadComponentsEqual(subject, predicate, object, graph, other) {
|
|
141
|
+
if (other === null || other === void 0 || other.termType !== "Quad") return false;
|
|
142
|
+
const otherQuad = other;
|
|
143
|
+
return termKey(subject) === termKey(otherQuad.subject) && termKey(predicate) === termKey(otherQuad.predicate) && termKey(object) === termKey(otherQuad.object) && termKey(graph) === termKey(otherQuad.graph);
|
|
144
|
+
}
|
|
145
|
+
/** quadTerm builds a quad in the default graph. */
|
|
146
|
+
function quadTerm(subject, predicate, object) {
|
|
147
|
+
const graph = defaultGraphTerm();
|
|
148
|
+
return {
|
|
149
|
+
termType: "Quad",
|
|
150
|
+
value: "",
|
|
151
|
+
subject,
|
|
152
|
+
predicate,
|
|
153
|
+
object,
|
|
154
|
+
graph,
|
|
155
|
+
equals: (other) => quadComponentsEqual(subject, predicate, object, graph, other)
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// src/term/quad-narrow.ts
|
|
159
|
+
/**
|
|
160
|
+
* narrowNestedQuad validates a base quad's components and returns a strict RdfQuad.
|
|
161
|
+
*/
|
|
162
|
+
function narrowNestedQuad(term) {
|
|
163
|
+
const subject = narrowQuadSubject(term.subject);
|
|
164
|
+
const predicate = narrowQuadPredicate(term.predicate);
|
|
165
|
+
const object = narrowQuadObject(term.object);
|
|
166
|
+
const graph = narrowQuadGraph(term.graph);
|
|
167
|
+
if (subject === void 0 || predicate === void 0 || object === void 0 || graph === void 0) return;
|
|
168
|
+
return {
|
|
169
|
+
termType: "Quad",
|
|
170
|
+
value: "",
|
|
171
|
+
subject,
|
|
172
|
+
predicate,
|
|
173
|
+
object,
|
|
174
|
+
graph,
|
|
175
|
+
equals: term.equals
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* narrowQuadSubject narrows an RDF term to a valid quad subject position.
|
|
180
|
+
*/
|
|
181
|
+
function narrowQuadSubject(term) {
|
|
182
|
+
if (term.termType === "NamedNode") return term;
|
|
183
|
+
if (term.termType === "BlankNode") return term;
|
|
184
|
+
if (term.termType === "Variable") return term;
|
|
185
|
+
if (term.termType === "Quad") return narrowNestedQuad(term);
|
|
186
|
+
}
|
|
187
|
+
/** narrowQuadPredicate narrows an RDF term to a valid quad predicate position. */
|
|
188
|
+
function narrowQuadPredicate(term) {
|
|
189
|
+
if (term.termType === "NamedNode") return term;
|
|
190
|
+
if (term.termType === "Variable") return term;
|
|
191
|
+
}
|
|
192
|
+
/** narrowQuadObject narrows an RDF term to a valid quad object position. */
|
|
193
|
+
function narrowQuadObject(term) {
|
|
194
|
+
if (term.termType === "NamedNode") return term;
|
|
195
|
+
if (term.termType === "BlankNode") return term;
|
|
196
|
+
if (term.termType === "Literal") return term;
|
|
197
|
+
if (term.termType === "Variable") return term;
|
|
198
|
+
if (term.termType === "Quad") return narrowNestedQuad(term);
|
|
199
|
+
}
|
|
200
|
+
/** narrowQuadGraph narrows an RDF term to a valid quad graph position. */
|
|
201
|
+
function narrowQuadGraph(term) {
|
|
202
|
+
if (term.termType === "DefaultGraph") return term;
|
|
203
|
+
if (term.termType === "NamedNode") return term;
|
|
204
|
+
if (term.termType === "BlankNode") return term;
|
|
205
|
+
if (term.termType === "Variable") return term;
|
|
206
|
+
}
|
|
207
|
+
/** requireQuadGraph narrows a term to a graph position or throws. */
|
|
208
|
+
function requireQuadGraph(term) {
|
|
209
|
+
const graph = narrowQuadGraph(term);
|
|
210
|
+
if (graph === void 0) throw new Error("Invalid graph term for quad graph position");
|
|
211
|
+
return graph;
|
|
212
|
+
}
|
|
213
|
+
/** quadDedupKey is a stable key for deduplicating quads in dataset construction. */
|
|
214
|
+
function quadDedupKey(quad) {
|
|
215
|
+
return `${termKey(quad.subject)}\u0000${termKey(quad.predicate)}\u0000${termKey(quad.object)}\u0000${termKey(quad.graph)}`;
|
|
216
|
+
}
|
|
217
|
+
// src/term/data-factory.ts
|
|
218
|
+
let blankNodeCounter = 0;
|
|
219
|
+
const RDF_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString";
|
|
220
|
+
const RDF_DIR_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString";
|
|
221
|
+
/** defaultGraphInstance is the shared default graph term. */
|
|
222
|
+
const defaultGraphInstance = defaultGraphTerm();
|
|
223
|
+
function newNamedNode(value) {
|
|
224
|
+
return namedNodeTerm(value);
|
|
225
|
+
}
|
|
226
|
+
function newBlankNode(value) {
|
|
227
|
+
if (value === void 0) {
|
|
228
|
+
blankNodeCounter += 1;
|
|
229
|
+
return blankNodeTerm(`b${blankNodeCounter}`);
|
|
230
|
+
}
|
|
231
|
+
return blankNodeTerm(value);
|
|
232
|
+
}
|
|
233
|
+
function newLiteral(value, languageOrDatatype) {
|
|
234
|
+
if (typeof languageOrDatatype === "string") return languageLiteralTerm(value, languageOrDatatype, "");
|
|
235
|
+
if (languageOrDatatype === void 0) return literalTerm(value);
|
|
236
|
+
if ("termType" in languageOrDatatype) return literalTerm(value, languageOrDatatype);
|
|
237
|
+
const direction = languageOrDatatype.direction;
|
|
238
|
+
return languageLiteralTerm(value, languageOrDatatype.language, direction === "ltr" || direction === "rtl" ? direction : "");
|
|
239
|
+
}
|
|
240
|
+
function newQuad(subject, predicate, object, graph) {
|
|
241
|
+
const graphTerm = graph === void 0 ? defaultGraphInstance : graph;
|
|
242
|
+
const subjectArm = narrowQuadSubject(subject);
|
|
243
|
+
const predicateArm = narrowQuadPredicate(predicate);
|
|
244
|
+
const objectArm = narrowQuadObject(object);
|
|
245
|
+
const graphArm = narrowQuadGraph(graphTerm);
|
|
246
|
+
if (subjectArm === void 0 || predicateArm === void 0 || objectArm === void 0 || graphArm === void 0) throw new Error("Invalid quad term for one or more positions");
|
|
247
|
+
return {
|
|
248
|
+
termType: "Quad",
|
|
249
|
+
value: "",
|
|
250
|
+
subject: subjectArm,
|
|
251
|
+
predicate: predicateArm,
|
|
252
|
+
object: objectArm,
|
|
253
|
+
graph: graphArm,
|
|
254
|
+
equals: (other) => quadComponentsEqual(subjectArm, predicateArm, objectArm, graphArm, other)
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function newFromTerm(original) {
|
|
258
|
+
switch (original.termType) {
|
|
259
|
+
case "NamedNode": return newNamedNode(original.value);
|
|
260
|
+
case "BlankNode": return newBlankNode(original.value);
|
|
261
|
+
case "Literal": return newLiteral(original.value, original.language ? {
|
|
262
|
+
language: original.language,
|
|
263
|
+
direction: original.direction
|
|
264
|
+
} : original.datatype);
|
|
265
|
+
case "Variable": return variableTerm(original.value);
|
|
266
|
+
case "DefaultGraph": return defaultGraphInstance;
|
|
267
|
+
case "Quad": {
|
|
268
|
+
const quad = original;
|
|
269
|
+
return newQuad(quad.subject, quad.predicate, quad.object, quad.graph);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function newFromQuad(original) {
|
|
274
|
+
return newQuad(original.subject, original.predicate, original.object, original.graph);
|
|
275
|
+
}
|
|
276
|
+
const DataFactory = {
|
|
277
|
+
namedNode: newNamedNode,
|
|
278
|
+
blankNode: newBlankNode,
|
|
279
|
+
literal: newLiteral,
|
|
280
|
+
variable: variableTerm,
|
|
281
|
+
defaultGraph: () => defaultGraphInstance,
|
|
282
|
+
quad: newQuad,
|
|
283
|
+
fromTerm: newFromTerm,
|
|
284
|
+
fromQuad: newFromQuad
|
|
285
|
+
};
|
|
286
|
+
export { variableTerm as _, narrowQuadObject as a, quadDedupKey as c, sameRdfTerm as d, termKey as f, namedNodeTerm as g, blankNodeTerm as h, defaultGraphInstance as i, requireQuadGraph as l, literalTerm as m, RDF_DIR_LANG_STRING as n, narrowQuadPredicate as o, languageLiteralTerm as p, RDF_LANG_STRING as r, narrowQuadSubject as s, DataFactory as t, quadTerm as u };
|