@grafctory/keep-sparql 0.0.0-stage → 0.14.2
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/README.md +115 -2
- package/dist/detail/decode.d.ts +40 -0
- package/dist/detail/decode.d.ts.map +1 -0
- package/dist/detail/decode.js +160 -0
- package/dist/detail/decode.js.map +1 -0
- package/dist/detail/encode.d.ts +45 -0
- package/dist/detail/encode.d.ts.map +1 -0
- package/dist/detail/encode.js +104 -0
- package/dist/detail/encode.js.map +1 -0
- package/dist/detail/index.d.ts +19 -0
- package/dist/detail/index.d.ts.map +1 -0
- package/dist/detail/index.js +61 -0
- package/dist/detail/index.js.map +1 -0
- package/dist/detail/schema.d.ts +86 -0
- package/dist/detail/schema.d.ts.map +1 -0
- package/dist/detail/schema.js +89 -0
- package/dist/detail/schema.js.map +1 -0
- package/dist/detect/encode.d.ts +23 -0
- package/dist/detect/encode.d.ts.map +1 -0
- package/dist/detect/encode.js +30 -0
- package/dist/detect/encode.js.map +1 -0
- package/dist/detect/index.d.ts +23 -0
- package/dist/detect/index.d.ts.map +1 -0
- package/dist/detect/index.js +37 -0
- package/dist/detect/index.js.map +1 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +158 -0
- package/dist/index.js.map +1 -0
- package/dist/modify/encode.d.ts +17 -0
- package/dist/modify/encode.d.ts.map +1 -0
- package/dist/modify/encode.js +233 -0
- package/dist/modify/encode.js.map +1 -0
- package/dist/modify/index.d.ts +21 -0
- package/dist/modify/index.d.ts.map +1 -0
- package/dist/modify/index.js +50 -0
- package/dist/modify/index.js.map +1 -0
- package/dist/patterns.d.ts +199 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +321 -0
- package/dist/patterns.js.map +1 -0
- package/dist/select/decode.d.ts +20 -0
- package/dist/select/decode.d.ts.map +1 -0
- package/dist/select/decode.js +134 -0
- package/dist/select/decode.js.map +1 -0
- package/dist/select/encode.d.ts +86 -0
- package/dist/select/encode.d.ts.map +1 -0
- package/dist/select/encode.js +647 -0
- package/dist/select/encode.js.map +1 -0
- package/dist/select/index.d.ts +19 -0
- package/dist/select/index.d.ts.map +1 -0
- package/dist/select/index.js +62 -0
- package/dist/select/index.js.map +1 -0
- package/dist/select/schema.d.ts +75 -0
- package/dist/select/schema.d.ts.map +1 -0
- package/dist/select/schema.js +84 -0
- package/dist/select/schema.js.map +1 -0
- package/dist/spine.d.ts +167 -0
- package/dist/spine.d.ts.map +1 -0
- package/dist/spine.js +180 -0
- package/dist/spine.js.map +1 -0
- package/dist/terms.d.ts +116 -0
- package/dist/terms.d.ts.map +1 -0
- package/dist/terms.js +251 -0
- package/dist/terms.js.map +1 -0
- package/package.json +66 -4
package/dist/spine.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Pass spine.
|
|
18
|
+
*
|
|
19
|
+
* Runs a batch of requests through one repository operation and settles each request on its own. A pass supplies the
|
|
20
|
+
* operation text and the decoder of one request, while the spine provides what every pass shares:
|
|
21
|
+
*
|
|
22
|
+
* - a variable scope that encoder and decoder agree on;
|
|
23
|
+
* - a single round trip per batch;
|
|
24
|
+
* - the solution rows of each request, selected by the entry they bind;
|
|
25
|
+
* - the nested requests a {@link Decoded | decoded value} depends on, run through the {@link Broker};
|
|
26
|
+
* - failure isolation: a failing operation rejects the requests it served and no other, and the handler always
|
|
27
|
+
* settles normally, so one failure never aborts the batching run or the requests of other batches in it.
|
|
28
|
+
*
|
|
29
|
+
* Decoders are pure: they turn solution rows into a {@link Decoded | decoded value} and never issue a request. The
|
|
30
|
+
* spine runs the nested requests the value needs and completes it, so a decoder can be tested from literal rows and
|
|
31
|
+
* literal responses alone, with no broker to mock and no repository to query against.
|
|
32
|
+
*
|
|
33
|
+
* @module
|
|
34
|
+
*/
|
|
35
|
+
import { createScope } from "@grafctory/core/scope";
|
|
36
|
+
import { variable } from "@grafctory/wire-sparql";
|
|
37
|
+
/**
|
|
38
|
+
* Creates a decoded value that depends on no request.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam T The value type
|
|
41
|
+
*
|
|
42
|
+
* @param value The value
|
|
43
|
+
*
|
|
44
|
+
* @returns The decoded value, depending on no request
|
|
45
|
+
*/
|
|
46
|
+
export function plain(value) {
|
|
47
|
+
return { details: [], selects: [], build: () => value };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Creates a decoded value read off a resource the detail pass materialises.
|
|
51
|
+
*
|
|
52
|
+
* @typeParam T The value type
|
|
53
|
+
*
|
|
54
|
+
* @param request The detail request materialising the resource
|
|
55
|
+
* @param read Reads the value off the materialised resource
|
|
56
|
+
*
|
|
57
|
+
* @returns The decoded value, depending on `request`
|
|
58
|
+
*/
|
|
59
|
+
export function resource(request, read) {
|
|
60
|
+
return { details: [request], selects: [], build: ([resource]) => read(resource) };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a decoded value read off a collection the select pass materialises.
|
|
64
|
+
*
|
|
65
|
+
* @typeParam T The value type
|
|
66
|
+
*
|
|
67
|
+
* @param request The select request materialising the collection
|
|
68
|
+
* @param read Reads the value off the materialised collection
|
|
69
|
+
*
|
|
70
|
+
* @returns The decoded value, depending on `request`
|
|
71
|
+
*/
|
|
72
|
+
export function collection(request, read) {
|
|
73
|
+
return { details: [], selects: [request], build: (_, [values]) => read(values) };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Gathers decoded values into one decoded list.
|
|
77
|
+
*
|
|
78
|
+
* @typeParam T The value type
|
|
79
|
+
*
|
|
80
|
+
* @param items The decoded values to gather
|
|
81
|
+
*
|
|
82
|
+
* @returns The decoded list of the values of `items`, in order, depending on every request they depend on
|
|
83
|
+
*/
|
|
84
|
+
export function all(items) {
|
|
85
|
+
return {
|
|
86
|
+
details: items.flatMap(({ details }) => details),
|
|
87
|
+
selects: items.flatMap(({ selects }) => selects),
|
|
88
|
+
build: (resources, collections) => items.reduce(({ values, details, selects }, item) => ({
|
|
89
|
+
values: [...values, item.build(resources.slice(details, details + item.details.length), collections.slice(selects, selects + item.selects.length))],
|
|
90
|
+
details: details + item.details.length,
|
|
91
|
+
selects: selects + item.selects.length
|
|
92
|
+
}), { values: [], details: 0, selects: 0 }).values
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Converts a decoded value.
|
|
97
|
+
*
|
|
98
|
+
* @typeParam T The value type
|
|
99
|
+
* @typeParam U The converted value type
|
|
100
|
+
*
|
|
101
|
+
* @param decoded The decoded value to convert
|
|
102
|
+
* @param mapper Converts the completed value
|
|
103
|
+
*
|
|
104
|
+
* @returns The decoded converted value, depending on the requests `decoded` depends on
|
|
105
|
+
*/
|
|
106
|
+
export function map({ details, selects, build }, mapper) {
|
|
107
|
+
return { details, selects, build: (resources, collections) => mapper(build(resources, collections)) };
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Runs a reading pass over a batch.
|
|
111
|
+
*
|
|
112
|
+
* Each request is decoded from the rows binding its own entry and settles with the completed value, or with the
|
|
113
|
+
* error raised while decoding or completing it. A request matching no row is decoded from no rows, so it settles as
|
|
114
|
+
* the pass decides rather than hanging. A query failure rejects every request of the batch. The returned promise
|
|
115
|
+
* does not wait for the nested requests decoded values depend on, which run in a later batching round.
|
|
116
|
+
*
|
|
117
|
+
* @param batch The requests to serve, sharing one plan
|
|
118
|
+
* @param client The repository the query runs against
|
|
119
|
+
* @param broker The channel running the nested requests decoded values depend on
|
|
120
|
+
* @param pass The encoder, owner column and decoder of the pass
|
|
121
|
+
*
|
|
122
|
+
* @returns A promise resolving once every request has been decoded or rejected
|
|
123
|
+
*/
|
|
124
|
+
export function read(batch, client, broker, pass) {
|
|
125
|
+
const scope = createScope(variable);
|
|
126
|
+
const query = pass.encode(scope);
|
|
127
|
+
return settle(batch, query === undefined ? Promise.resolve(new Map())
|
|
128
|
+
: client.select(query).then(tuples => owned(tuples, pass.owner(scope))), (item, rows) => Promise.resolve()
|
|
129
|
+
.then(() => pass.decode(scope, item, rows))
|
|
130
|
+
.then(decoded => complete(decoded, broker)));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Runs a writing pass over a batch.
|
|
134
|
+
*
|
|
135
|
+
* Applies the update the pass encodes and settles each request through the pass decoder. An update failure rejects
|
|
136
|
+
* every request of the batch and the returned promise still resolves, so the handler never fails the batching run.
|
|
137
|
+
*
|
|
138
|
+
* @param batch The requests to apply
|
|
139
|
+
* @param client The repository the update is applied against
|
|
140
|
+
* @param pass The encoder and decoder of the pass
|
|
141
|
+
*
|
|
142
|
+
* @returns A promise resolving once every request has been settled
|
|
143
|
+
*/
|
|
144
|
+
export function write(batch, client, pass) {
|
|
145
|
+
const scope = createScope(variable);
|
|
146
|
+
return settle(batch, client.update(pass.encode(scope)).then(() => new Map()), item => Promise.resolve().then(() => pass.decode(scope, item)));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Settles every request of a batch once `rows` resolves. Each request settles with the value `decode` returns from
|
|
150
|
+
* its own rows, or with the error `decode` raises. If `rows` fails, every request is rejected with that error. The
|
|
151
|
+
* returned promise resolves in either case, once every request has been decoded or rejected.
|
|
152
|
+
*/
|
|
153
|
+
function settle(batch, rows, decode) {
|
|
154
|
+
return rows.then(index => batch.forEach(item => decode(item, index.get(item.request.entry) ?? []).then(value => item.resolve(value), (error) => item.reject(error))), (error) => batch.forEach(({ reject }) => reject(error)));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Completes a decoded value through the broker: every nested request is run at once, and the value is built from their
|
|
158
|
+
* responses once every one of them has settled, so a failing request fails the value as a whole.
|
|
159
|
+
*/
|
|
160
|
+
function complete({ details, selects, build }, broker) {
|
|
161
|
+
return Promise.all([
|
|
162
|
+
Promise.all(details.map(request => broker.detail(request))),
|
|
163
|
+
Promise.all(selects.map(request => broker.select(request)))
|
|
164
|
+
]).then(([resources, collections]) => build(resources, collections));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Indexes the solution rows of a query by the entry bound in their `owner` column, in solution order. A row binding
|
|
168
|
+
* no named node on that column belongs to no request and is left out of the index.
|
|
169
|
+
*/
|
|
170
|
+
function owned(tuples, owner) {
|
|
171
|
+
// the index is filled in place: rebuilding it per row would copy it once per solution, quadratic in the
|
|
172
|
+
// result, which is the cost the batched passes exist to avoid (sw-developer:code, performance-critical hot path)
|
|
173
|
+
return tuples.reduce((index, tuple) => {
|
|
174
|
+
const bound = tuple[owner];
|
|
175
|
+
return bound?.kind === "named"
|
|
176
|
+
? index.set(bound.iri, [...(index.get(bound.iri) ?? []), tuple])
|
|
177
|
+
: index;
|
|
178
|
+
}, new Map());
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=spine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spine.js","sourceRoot":"","sources":["../src/spine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,WAAW,EAAc,MAAM,uBAAuB,CAAC;AAGhE,OAAO,EAAiE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AA+EjH;;;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@grafctory/blue/value";
|
|
2
|
+
import type { Tag } from "@grafctory/core/language";
|
|
3
|
+
import { Options } from "@grafctory/qest/model";
|
|
4
|
+
import { type Reference, type Value, type Values } from "@grafctory/qest/state";
|
|
5
|
+
import { type Term } from "@grafctory/trio";
|
|
6
|
+
/**
|
|
7
|
+
* Converts the options of a set-matching or focus constraint to individual match {@link Term | terms} (§5.7.3).
|
|
8
|
+
*
|
|
9
|
+
* A localised dictionary option expands to one language-tagged term per text, the `und` tag included. A scalar option
|
|
10
|
+
* maps to a term typed after the {@link Range | range} variant it fits, and an option array maps element-wise. A
|
|
11
|
+
* `null` option is kept as the absent-value option, which the emitted condition tests as an unbound value (§5.7.3).
|
|
12
|
+
*
|
|
13
|
+
* @param value The constraint options
|
|
14
|
+
* @param range The range of the constrained value
|
|
15
|
+
*
|
|
16
|
+
* @returns The match terms, `null` standing for the absent value
|
|
17
|
+
*
|
|
18
|
+
* @throws {@link !RangeError RangeError} If a scalar option fits no variant of `range`
|
|
19
|
+
*/
|
|
20
|
+
export declare function optionsToTerms(value: Options, range: Range): readonly (null | Term)[];
|
|
21
|
+
/**
|
|
22
|
+
* Converts a property's write values to the RDF {@link Term | terms} to store under a resolved
|
|
23
|
+
* {@link Shape | shape} variant, the terms an insert states as the objects of the property's stored edges.
|
|
24
|
+
*
|
|
25
|
+
* A boolean, number, or string value becomes a literal typed after the variant; a string shape carrying the `sh:IRI`
|
|
26
|
+
* datatype renders an IRI node instead, as in {@link valueToTerm}. A localised dictionary becomes one language-tagged
|
|
27
|
+
* term per text (§4.3), the `und` tag included, so a string variant sharing the property never claims it on read
|
|
28
|
+
* (§3.1); a plain string is never localised text and is dropped. A reference becomes its IRI. A nested resource becomes
|
|
29
|
+
* its declared id if present (a captive resource with its own identity). An embedded resource becomes a fresh
|
|
30
|
+
* `urn:uuid:` IRI instead, so the store never writes blank nodes and can read back every embedded resource it stores.
|
|
31
|
+
*
|
|
32
|
+
* Values not fitting the variant are dropped. A nested resource carrying no content is dropped too, so no link to an
|
|
33
|
+
* empty subject is stored and an empty embedded resource reads back as an absent property, as the contract requires.
|
|
34
|
+
*
|
|
35
|
+
* @param values The write values, a single value or a value set
|
|
36
|
+
* @param shape The range variant the values are typed after
|
|
37
|
+
*
|
|
38
|
+
* @returns The terms to store, possibly empty
|
|
39
|
+
*
|
|
40
|
+
* @throws {@link !RangeError RangeError} If `shape` is a union shape
|
|
41
|
+
*/
|
|
42
|
+
export declare function valuesToTerms(values: Values, shape: Shape): readonly Term[];
|
|
43
|
+
/**
|
|
44
|
+
* Converts a scalar operand to the RDF {@link Term} typed after its resolved {@link Shape | shape} (§5.7.1).
|
|
45
|
+
*
|
|
46
|
+
* A string shape carrying the `sh:IRI` datatype (an `id` or `type` entry, or a reference-ranged property) renders an
|
|
47
|
+
* IRI node rather than a literal; an IRI-shaped literal (a `url`) is told apart by datatype alone.
|
|
48
|
+
*
|
|
49
|
+
* @param value The operand to convert
|
|
50
|
+
* @param shape The range variant the operand resolves to
|
|
51
|
+
*
|
|
52
|
+
* @returns The typed operand term
|
|
53
|
+
*
|
|
54
|
+
* @throws {@link !RangeError RangeError} If `shape` is a resource or union shape, neither of which types a scalar
|
|
55
|
+
*/
|
|
56
|
+
export declare function valueToTerm(value: Value, shape: Shape): Term;
|
|
57
|
+
/**
|
|
58
|
+
* Converts a solution {@link Term} back to the QEST {@link Value} it stands for under a resolved
|
|
59
|
+
* {@link Shape | shape} variant (§3.4), the read-side counterpart of {@link valueToTerm}.
|
|
60
|
+
*
|
|
61
|
+
* A boolean or number variant takes a literal of a matching datatype. A string variant carrying the `sh:IRI` datatype
|
|
62
|
+
* takes an IRI, any other string variant a typed literal, as its lexical form. A localised variant takes its coalesced
|
|
63
|
+
* label (§6.2), a language-tagged or plain literal, as a string. A reference variant takes an IRI.
|
|
64
|
+
*
|
|
65
|
+
* @param term The solution term to convert
|
|
66
|
+
* @param shape The range variant the term was read under
|
|
67
|
+
*
|
|
68
|
+
* @returns The decoded value, or `undefined` if `term` does not fit `shape`
|
|
69
|
+
*
|
|
70
|
+
* @throws {@link !RangeError RangeError} If `shape` is a resource or union shape, neither of which types a scalar
|
|
71
|
+
*/
|
|
72
|
+
export declare function termToValue(term: Term, shape: Shape): undefined | Value;
|
|
73
|
+
/**
|
|
74
|
+
* Converts a solution {@link Term} bound where a resource is expected to the {@link Reference} naming it.
|
|
75
|
+
*
|
|
76
|
+
* The store reads only resources named by IRIs, embedded resources included, so a graph loaded by other means must
|
|
77
|
+
* be skolemised first. A blank node is rejected rather than skipped: skipping it would silently drop the embedded
|
|
78
|
+
* resource it names, together with all the content requested under that resource.
|
|
79
|
+
*
|
|
80
|
+
* @param value The solution term to convert
|
|
81
|
+
*
|
|
82
|
+
* @returns The IRI of a named node, or `undefined` for a literal, which names no resource
|
|
83
|
+
*
|
|
84
|
+
* @throws {@link !RangeError RangeError} If `value` is a blank node
|
|
85
|
+
*/
|
|
86
|
+
export declare function termToReference(value: Term): undefined | Reference;
|
|
87
|
+
/**
|
|
88
|
+
* Converts a computed solution {@link Term} back to the QEST {@link Value} it stands for (§3.4).
|
|
89
|
+
*
|
|
90
|
+
* A computed value carries no declared shape, so its datatype alone settles its type. An IRI keeps its reference
|
|
91
|
+
* identity. A literal maps to its transport type: an `xsd:boolean` to a boolean, a numeric datatype to a number, and
|
|
92
|
+
* any other datatype (the `xsd:string` of a plain literal included) to its lexical form. The mapping is many-to-one,
|
|
93
|
+
* so processing-type distinctions are not preserved: a date and a plain string both come back as strings.
|
|
94
|
+
*
|
|
95
|
+
* @param value The solution term to convert, an IRI reference or a plain or typed literal
|
|
96
|
+
*
|
|
97
|
+
* @returns The decoded value: a reference, boolean, number, or string
|
|
98
|
+
*
|
|
99
|
+
* @throws {@link !RangeError RangeError} If `value` is a blank node or a language-tagged literal, neither of which
|
|
100
|
+
* stands for a scalar value a computed cell can carry, as neither is told by a datatype the transform could yield
|
|
101
|
+
*/
|
|
102
|
+
export declare function computedToValue(value: Term): Value;
|
|
103
|
+
/**
|
|
104
|
+
* Coalesces the language-tagged terms of a localised property to the texts under the tag a language priority picks
|
|
105
|
+
* (§6.2), the read-side counterpart of the SPARQL coalescing in `patterns.ts`, applying the very same priority rule.
|
|
106
|
+
*
|
|
107
|
+
* The first tag of `locale` some term carries wins, and every text under it is returned; terms under any other tag,
|
|
108
|
+
* and terms carrying no tag, are left out. A priority naming no tag the terms carry picks nothing.
|
|
109
|
+
*
|
|
110
|
+
* @param terms The solution terms of the property
|
|
111
|
+
* @param locale The language priority, most preferred first
|
|
112
|
+
*
|
|
113
|
+
* @returns The texts under the winning tag, in term order; empty when no priority tag is carried
|
|
114
|
+
*/
|
|
115
|
+
export declare function coalesce(terms: readonly Term[], locale: readonly Tag[]): readonly string[];
|
|
116
|
+
//# sourceMappingURL=terms.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terms.d.ts","sourceRoot":"","sources":["../src/terms.ts"],"names":[],"mappings":"AA6BA,OAAO,EAAE,KAAK,KAAK,EAAM,KAAK,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAInE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAA0B,KAAK,SAAS,EAAE,KAAK,KAAK,EAAE,KAAK,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACxG,OAAO,EAAiB,KAAK,IAAI,EAAS,MAAM,iBAAiB,CAAC;AAKlE;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,EAAE,CAoBrF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,SAAS,IAAI,EAAE,CA2C3E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAkC5D;AAKD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,GAAG,SAAS,GAAG,KAAK,CAkCvE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,IAAI,GAAG,SAAS,GAAG,SAAS,CAclE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,IAAI,GAAG,KAAK,CA0BlD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,EAAE,MAAM,EAAE,SAAS,GAAG,EAAE,GAAG,SAAS,MAAM,EAAE,CAQ1F"}
|
package/dist/terms.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Value and term codecs.
|
|
18
|
+
*
|
|
19
|
+
* The conversions between the QEST values a store exchanges with its callers and the RDF terms a repository stores,
|
|
20
|
+
* each driven by the shape variant the value is typed after: write values and constraint options to the terms to
|
|
21
|
+
* store or match, solution terms back to the values they stand for. No SPARQL is composed here, so a connector for
|
|
22
|
+
* another RDF-backed query language keeps this module as it is and rewrites the emitters composing the text alone.
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
import { getShapeId } from "@grafctory/blue/resource";
|
|
27
|
+
import { getOptionBranch, getShapeBranches } from "@grafctory/blue/union";
|
|
28
|
+
import { sh } from "@grafctory/blue/value";
|
|
29
|
+
import { error, isBoolean, isNumber, isObject, isString } from "@grafctory/core";
|
|
30
|
+
import { some } from "@grafctory/core/arrays";
|
|
31
|
+
import { isNumeric as isNumericDatatype, xsd } from "@grafctory/core/datatype";
|
|
32
|
+
import { isReference, isVacuous } from "@grafctory/qest/state";
|
|
33
|
+
import { named, tagged, typed } from "@grafctory/trio";
|
|
34
|
+
//// Encoders //////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
35
|
+
/**
|
|
36
|
+
* Converts the options of a set-matching or focus constraint to individual match {@link Term | terms} (§5.7.3).
|
|
37
|
+
*
|
|
38
|
+
* A localised dictionary option expands to one language-tagged term per text, the `und` tag included. A scalar option
|
|
39
|
+
* maps to a term typed after the {@link Range | range} variant it fits, and an option array maps element-wise. A
|
|
40
|
+
* `null` option is kept as the absent-value option, which the emitted condition tests as an unbound value (§5.7.3).
|
|
41
|
+
*
|
|
42
|
+
* @param value The constraint options
|
|
43
|
+
* @param range The range of the constrained value
|
|
44
|
+
*
|
|
45
|
+
* @returns The match terms, `null` standing for the absent value
|
|
46
|
+
*
|
|
47
|
+
* @throws {@link !RangeError RangeError} If a scalar option fits no variant of `range`
|
|
48
|
+
*/
|
|
49
|
+
export function optionsToTerms(value, range) {
|
|
50
|
+
if (isObject(value)) {
|
|
51
|
+
return Object.entries(value).flatMap(([language, values]) => some(values).map(text => tagged(text, language)));
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const variants = getShapeBranches(range.shape);
|
|
55
|
+
return some(value).map(option => option === null ? null : valueToTerm(option, getOptionBranch(option, variants)
|
|
56
|
+
?? error(new RangeError(`unresolved range variant for value <${String(option)}>`))));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts a property's write values to the RDF {@link Term | terms} to store under a resolved
|
|
61
|
+
* {@link Shape | shape} variant, the terms an insert states as the objects of the property's stored edges.
|
|
62
|
+
*
|
|
63
|
+
* A boolean, number, or string value becomes a literal typed after the variant; a string shape carrying the `sh:IRI`
|
|
64
|
+
* datatype renders an IRI node instead, as in {@link valueToTerm}. A localised dictionary becomes one language-tagged
|
|
65
|
+
* term per text (§4.3), the `und` tag included, so a string variant sharing the property never claims it on read
|
|
66
|
+
* (§3.1); a plain string is never localised text and is dropped. A reference becomes its IRI. A nested resource becomes
|
|
67
|
+
* its declared id if present (a captive resource with its own identity). An embedded resource becomes a fresh
|
|
68
|
+
* `urn:uuid:` IRI instead, so the store never writes blank nodes and can read back every embedded resource it stores.
|
|
69
|
+
*
|
|
70
|
+
* Values not fitting the variant are dropped. A nested resource carrying no content is dropped too, so no link to an
|
|
71
|
+
* empty subject is stored and an empty embedded resource reads back as an absent property, as the contract requires.
|
|
72
|
+
*
|
|
73
|
+
* @param values The write values, a single value or a value set
|
|
74
|
+
* @param shape The range variant the values are typed after
|
|
75
|
+
*
|
|
76
|
+
* @returns The terms to store, possibly empty
|
|
77
|
+
*
|
|
78
|
+
* @throws {@link !RangeError RangeError} If `shape` is a union shape
|
|
79
|
+
*/
|
|
80
|
+
export function valuesToTerms(values, shape) {
|
|
81
|
+
switch (shape.kind) {
|
|
82
|
+
case "boolean":
|
|
83
|
+
return some(values).filter(isBoolean).map(value => typed(value, xsd.boolean));
|
|
84
|
+
case "number":
|
|
85
|
+
return some(values).filter(isNumber).map(value => typed(value, shape.datatype ?? xsd.double));
|
|
86
|
+
case "string":
|
|
87
|
+
return some(values).filter(isString).map(value => shape.datatype === sh.IRI
|
|
88
|
+
? named(value)
|
|
89
|
+
: typed(value, shape.datatype));
|
|
90
|
+
case "dictionary":
|
|
91
|
+
return some(values).flatMap(value => isObject(value)
|
|
92
|
+
? Object.entries(value).flatMap(([tag, texts]) => some(texts).filter(isString).map(text => tagged(text, tag)))
|
|
93
|
+
: []);
|
|
94
|
+
case "reference":
|
|
95
|
+
return some(values).filter(isReference).map(value => named(value));
|
|
96
|
+
case "resource":
|
|
97
|
+
const id = getShapeId(shape);
|
|
98
|
+
return some(values).filter(v => isObject(v)).flatMap(value => {
|
|
99
|
+
const node = id !== undefined ? value[id] : undefined;
|
|
100
|
+
return isReference(node) ? [named(node)] : isVacuous(value) ? [] : [named()];
|
|
101
|
+
});
|
|
102
|
+
case "union": // a range variant is always a flattened branch, never a union
|
|
103
|
+
throw new RangeError(`unsupported union variant`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Converts a scalar operand to the RDF {@link Term} typed after its resolved {@link Shape | shape} (§5.7.1).
|
|
108
|
+
*
|
|
109
|
+
* A string shape carrying the `sh:IRI` datatype (an `id` or `type` entry, or a reference-ranged property) renders an
|
|
110
|
+
* IRI node rather than a literal; an IRI-shaped literal (a `url`) is told apart by datatype alone.
|
|
111
|
+
*
|
|
112
|
+
* @param value The operand to convert
|
|
113
|
+
* @param shape The range variant the operand resolves to
|
|
114
|
+
*
|
|
115
|
+
* @returns The typed operand term
|
|
116
|
+
*
|
|
117
|
+
* @throws {@link !RangeError RangeError} If `shape` is a resource or union shape, neither of which types a scalar
|
|
118
|
+
*/
|
|
119
|
+
export function valueToTerm(value, shape) {
|
|
120
|
+
switch (shape.kind) {
|
|
121
|
+
case "boolean":
|
|
122
|
+
return typed(String(value), xsd.boolean);
|
|
123
|
+
case "number":
|
|
124
|
+
return typed(String(value), shape.datatype ?? xsd.double);
|
|
125
|
+
case "string":
|
|
126
|
+
return shape.datatype === sh.IRI
|
|
127
|
+
? named(String(value))
|
|
128
|
+
: typed(String(value), shape.datatype);
|
|
129
|
+
case "dictionary": // a localised dictionary shape is coalesced to a plain xsd:string (§6)
|
|
130
|
+
return typed(String(value));
|
|
131
|
+
case "reference":
|
|
132
|
+
return named(String(value));
|
|
133
|
+
case "resource": // a scalar operand never resolves to an expanded resource shape
|
|
134
|
+
throw new RangeError(`unsupported embedded resource variant for value <${String(value)}>`);
|
|
135
|
+
case "union": // a range variant is always a flattened branch, never a union
|
|
136
|
+
throw new RangeError(`unsupported union variant for value <${String(value)}>`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
//// Decoders //////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
140
|
+
/**
|
|
141
|
+
* Converts a solution {@link Term} back to the QEST {@link Value} it stands for under a resolved
|
|
142
|
+
* {@link Shape | shape} variant (§3.4), the read-side counterpart of {@link valueToTerm}.
|
|
143
|
+
*
|
|
144
|
+
* A boolean or number variant takes a literal of a matching datatype. A string variant carrying the `sh:IRI` datatype
|
|
145
|
+
* takes an IRI, any other string variant a typed literal, as its lexical form. A localised variant takes its coalesced
|
|
146
|
+
* label (§6.2), a language-tagged or plain literal, as a string. A reference variant takes an IRI.
|
|
147
|
+
*
|
|
148
|
+
* @param term The solution term to convert
|
|
149
|
+
* @param shape The range variant the term was read under
|
|
150
|
+
*
|
|
151
|
+
* @returns The decoded value, or `undefined` if `term` does not fit `shape`
|
|
152
|
+
*
|
|
153
|
+
* @throws {@link !RangeError RangeError} If `shape` is a resource or union shape, neither of which types a scalar
|
|
154
|
+
*/
|
|
155
|
+
export function termToValue(term, shape) {
|
|
156
|
+
switch (shape.kind) {
|
|
157
|
+
case "boolean":
|
|
158
|
+
return term.kind === "typed" && term.datatype === xsd.boolean ? term.text === "true" : undefined;
|
|
159
|
+
case "number":
|
|
160
|
+
return term.kind === "typed" && isNumericDatatype(term.datatype) ? Number(term.text) : undefined;
|
|
161
|
+
case "string":
|
|
162
|
+
return shape.datatype === sh.IRI
|
|
163
|
+
? term.kind === "named" ? term.iri : undefined
|
|
164
|
+
: term.kind === "typed" ? term.text : undefined;
|
|
165
|
+
case "dictionary":
|
|
166
|
+
return term.kind === "tagged" || term.kind === "typed" ? term.text : undefined;
|
|
167
|
+
case "reference":
|
|
168
|
+
return term.kind === "named" ? term.iri : undefined;
|
|
169
|
+
case "resource": // an embedded resource is expanded by its caller, never decoded from a single term
|
|
170
|
+
throw new RangeError(`unsupported embedded resource variant`);
|
|
171
|
+
case "union": // a range variant is always a flattened branch, never a union
|
|
172
|
+
throw new RangeError(`unsupported union variant`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Converts a solution {@link Term} bound where a resource is expected to the {@link Reference} naming it.
|
|
177
|
+
*
|
|
178
|
+
* The store reads only resources named by IRIs, embedded resources included, so a graph loaded by other means must
|
|
179
|
+
* be skolemised first. A blank node is rejected rather than skipped: skipping it would silently drop the embedded
|
|
180
|
+
* resource it names, together with all the content requested under that resource.
|
|
181
|
+
*
|
|
182
|
+
* @param value The solution term to convert
|
|
183
|
+
*
|
|
184
|
+
* @returns The IRI of a named node, or `undefined` for a literal, which names no resource
|
|
185
|
+
*
|
|
186
|
+
* @throws {@link !RangeError RangeError} If `value` is a blank node
|
|
187
|
+
*/
|
|
188
|
+
export function termToReference(value) {
|
|
189
|
+
if (value.kind === "blank") {
|
|
190
|
+
throw new RangeError(`unsupported blank node <_:${value.label}>`);
|
|
191
|
+
}
|
|
192
|
+
else if (value.kind === "named") {
|
|
193
|
+
return value.iri;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Converts a computed solution {@link Term} back to the QEST {@link Value} it stands for (§3.4).
|
|
201
|
+
*
|
|
202
|
+
* A computed value carries no declared shape, so its datatype alone settles its type. An IRI keeps its reference
|
|
203
|
+
* identity. A literal maps to its transport type: an `xsd:boolean` to a boolean, a numeric datatype to a number, and
|
|
204
|
+
* any other datatype (the `xsd:string` of a plain literal included) to its lexical form. The mapping is many-to-one,
|
|
205
|
+
* so processing-type distinctions are not preserved: a date and a plain string both come back as strings.
|
|
206
|
+
*
|
|
207
|
+
* @param value The solution term to convert, an IRI reference or a plain or typed literal
|
|
208
|
+
*
|
|
209
|
+
* @returns The decoded value: a reference, boolean, number, or string
|
|
210
|
+
*
|
|
211
|
+
* @throws {@link !RangeError RangeError} If `value` is a blank node or a language-tagged literal, neither of which
|
|
212
|
+
* stands for a scalar value a computed cell can carry, as neither is told by a datatype the transform could yield
|
|
213
|
+
*/
|
|
214
|
+
export function computedToValue(value) {
|
|
215
|
+
if (value.kind === "blank") {
|
|
216
|
+
throw new RangeError(`unsupported blank node <_:${value.label}>`);
|
|
217
|
+
}
|
|
218
|
+
else if (value.kind === "named") {
|
|
219
|
+
return value.iri;
|
|
220
|
+
}
|
|
221
|
+
else if (value.kind === "tagged") {
|
|
222
|
+
throw new RangeError(`unsupported language-tagged literal <${value.text}@${value.language}>`);
|
|
223
|
+
}
|
|
224
|
+
else if (value.datatype === xsd.boolean) {
|
|
225
|
+
return value.text === "true";
|
|
226
|
+
}
|
|
227
|
+
else if (isNumericDatatype(value.datatype)) {
|
|
228
|
+
return Number(value.text);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
return value.text;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Coalesces the language-tagged terms of a localised property to the texts under the tag a language priority picks
|
|
236
|
+
* (§6.2), the read-side counterpart of the SPARQL coalescing in `patterns.ts`, applying the very same priority rule.
|
|
237
|
+
*
|
|
238
|
+
* The first tag of `locale` some term carries wins, and every text under it is returned; terms under any other tag,
|
|
239
|
+
* and terms carrying no tag, are left out. A priority naming no tag the terms carry picks nothing.
|
|
240
|
+
*
|
|
241
|
+
* @param terms The solution terms of the property
|
|
242
|
+
* @param locale The language priority, most preferred first
|
|
243
|
+
*
|
|
244
|
+
* @returns The texts under the winning tag, in term order; empty when no priority tag is carried
|
|
245
|
+
*/
|
|
246
|
+
export function coalesce(terms, locale) {
|
|
247
|
+
const tagged = terms.flatMap(term => term.kind === "tagged" ? [term] : []);
|
|
248
|
+
const winning = locale.find(tag => tagged.some(term => term.language === tag));
|
|
249
|
+
return tagged.filter(term => term.language === winning).map(term => term.text);
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=terms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terms.js","sourceRoot":"","sources":["../src/terms.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC1E,OAAO,EAAc,EAAE,EAAc,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACjF,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,SAAS,IAAI,iBAAiB,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAG/E,OAAO,EAAE,WAAW,EAAE,SAAS,EAA2C,MAAM,uBAAuB,CAAC;AACxG,OAAO,EAAE,KAAK,EAAE,MAAM,EAAa,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAGlE,wHAAwH;AAExH;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc,EAAE,KAAY;IAE1D,IAAK,QAAQ,CAAC,KAAK,CAAC,EAAG,CAAC;QAEvB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,EAAE,CAC3D,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAChD,CAA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AC,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAE3E,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC,CAAC;IAE/E,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEhF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grafctory/keep-sparql",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.14.2",
|
|
4
|
+
"description": "SPARQL 1.1 repository connector for @grafctory/keep.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"json-ld",
|
|
8
|
+
"model-driven",
|
|
9
|
+
"sparql"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://grafctory.github.io/keep/",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/grafctory/keep/issues",
|
|
14
|
+
"email": "info@metreeca.com"
|
|
15
|
+
},
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Metreeca",
|
|
19
|
+
"email": "info@metreeca.com",
|
|
20
|
+
"url": "https://www.metreeca.com/"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/grafctory/keep.git",
|
|
25
|
+
"directory": "packages/connectors/keep-sparql"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"@grafctory/source": "./src/index.ts",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE",
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22.22.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"clean": "ush rm dist docs node_modules",
|
|
45
|
+
"build": "ush rm dist && tsc -p tsconfig.build.json",
|
|
46
|
+
"check": "npx vitest --typecheck run"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@grafctory/blue": "^0.15.0",
|
|
50
|
+
"@grafctory/core": "^0.14.0",
|
|
51
|
+
"@grafctory/keep": "^0.14.2",
|
|
52
|
+
"@grafctory/keep-flake": "^0.14.2",
|
|
53
|
+
"@grafctory/qest": "^0.15.0",
|
|
54
|
+
"@grafctory/tape": "^0.13.0",
|
|
55
|
+
"@grafctory/trio": "^0.3.0",
|
|
56
|
+
"@grafctory/wire-sparql": "^0.13.0"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@grafctory/keep-suite": "^0.14.2",
|
|
60
|
+
"@grafctory/wire-sparql-http": "^0.13.0",
|
|
61
|
+
"@grafctory/wire-sparql-oxigraph": "^0.13.0",
|
|
62
|
+
"@grafctory/wire-sparql-rdf4j": "^0.13.0",
|
|
63
|
+
"oxigraph": "^0.5.13",
|
|
64
|
+
"testcontainers": "^12.1.0",
|
|
65
|
+
"typescript": "^6.0.3",
|
|
66
|
+
"vitest": "^4.0.13"
|
|
67
|
+
}
|
|
68
|
+
}
|