@coasys/ad4m 0.13.0-test-2 → 0.13.0-test-3
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/lib/index.cjs +1060 -147
- package/lib/index.js +1054 -147
- package/lib/src/index.d.ts +0 -1
- package/lib/src/index.js +0 -1
- package/lib/src/language/Language.d.ts +26 -214
- package/lib/src/language/Language.js +28 -2
- package/lib/src/model/Ad4mModel.d.ts +25 -1
- package/lib/src/model/Ad4mModel.js +139 -30
- package/lib/src/model/Ad4mModel.test.js +214 -32
- package/lib/src/model/ModelQueryBuilder.d.ts +19 -0
- package/lib/src/model/ModelQueryBuilder.js +62 -18
- package/lib/src/model/hydration-batching.test.d.ts +7 -0
- package/lib/src/model/hydration-batching.test.js +456 -0
- package/lib/src/model/hydration.d.ts +1 -0
- package/lib/src/model/hydration.js +279 -70
- package/lib/src/model/query-sparql.d.ts +78 -1
- package/lib/src/model/query-sparql.js +447 -16
- package/lib/src/model/query-sparql.test.d.ts +1 -0
- package/lib/src/model/query-sparql.test.js +495 -0
- package/lib/src/model/query-utils.js +4 -0
- package/lib/src/model/subscription-delta.test.d.ts +24 -0
- package/lib/src/model/subscription-delta.test.js +180 -0
- package/lib/src/model/subscription-pool.js +48 -2
- package/lib/src/model/types.d.ts +49 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +2 -0
- package/lib/src/perspectives/PerspectiveClient.js +37 -9
- package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveClient.test.js +169 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +3 -0
- package/lib/src/perspectives/PerspectiveProxy.js +15 -0
- package/package.json +1 -1
- package/src/Ad4mClient.test.ts +56 -1
- package/src/index.ts +0 -1
- package/src/language/Language.ts +54 -275
- package/src/model/Ad4mModel.test.ts +222 -35
- package/src/model/Ad4mModel.ts +144 -30
- package/src/model/ModelQueryBuilder.ts +66 -18
- package/src/model/hydration-batching.test.ts +563 -0
- package/src/model/hydration.ts +249 -47
- package/src/model/query-sparql.test.ts +568 -0
- package/src/model/query-sparql.ts +473 -16
- package/src/model/query-utils.ts +4 -0
- package/src/model/subscription-delta.test.ts +207 -0
- package/src/model/subscription-pool.ts +50 -2
- package/src/model/types.ts +50 -1
- package/src/perspectives/PerspectiveClient.test.ts +204 -0
- package/src/perspectives/PerspectiveClient.ts +36 -7
- package/src/perspectives/PerspectiveProxy.ts +18 -0
- package/lib/src/model/query-surreal.d.ts +0 -44
- package/lib/src/model/query-surreal.js +0 -249
- package/lib/src/model/surreal-utils.d.ts +0 -42
- package/lib/src/model/surreal-utils.js +0 -128
- package/src/language/LanguageContext.ts +0 -37
- package/tsconfig-browser.json +0 -82
|
@@ -10,6 +10,59 @@
|
|
|
10
10
|
*/
|
|
11
11
|
import { Literal } from "../Literal";
|
|
12
12
|
import { resolveParentPredicate } from "./query-common";
|
|
13
|
+
/**
|
|
14
|
+
* Check whether a `where` clause contains filters that cannot be pushed down
|
|
15
|
+
* to SPARQL and must be evaluated in JS after hydration.
|
|
16
|
+
*
|
|
17
|
+
* JS-only filters include:
|
|
18
|
+
* - Literal-stored properties (equality, comparison, contains, etc.)
|
|
19
|
+
* - Reverse relations (belongsToOne / belongsToMany)
|
|
20
|
+
* - author / timestamp filters (skipped by buildSPARQLWhereFilters)
|
|
21
|
+
*
|
|
22
|
+
* When JS-only filters exist, SPARQL-level LIMIT/OFFSET must NOT be applied
|
|
23
|
+
* because the database would cap the candidate set before JS filters run,
|
|
24
|
+
* potentially discarding matches beyond the LIMIT.
|
|
25
|
+
*/
|
|
26
|
+
export function hasJsOnlyWhereFilters(metadata, allRelationsMetadata, where) {
|
|
27
|
+
if (!where)
|
|
28
|
+
return false;
|
|
29
|
+
for (const [propertyName, condition] of Object.entries(where)) {
|
|
30
|
+
if (propertyName === "base" || propertyName === "id")
|
|
31
|
+
continue;
|
|
32
|
+
// author/timestamp: return true so that count() falls back to the full hydration
|
|
33
|
+
// path (which uses buildSPARQLQuery — which DOES bind ?author/?timestamp via the
|
|
34
|
+
// named graph block). buildSPARQLCountQuery intentionally omits the named graph,
|
|
35
|
+
// so emitting an ?author FILTER there would produce unbound-variable SPARQL.
|
|
36
|
+
// buildSPARQLWhereFilters still pushes these into FILTER when called from
|
|
37
|
+
// buildSPARQLQuery / buildSPARQLGroupedCountQuery, which both include the named graph.
|
|
38
|
+
if (propertyName === "author" || propertyName === "timestamp")
|
|
39
|
+
return true;
|
|
40
|
+
const propMeta = metadata.properties[propertyName];
|
|
41
|
+
if (!propMeta)
|
|
42
|
+
continue;
|
|
43
|
+
// Reverse relations are JS-only
|
|
44
|
+
const relMeta = allRelationsMetadata[propertyName];
|
|
45
|
+
if (relMeta && (relMeta.kind === 'belongsToOne' || relMeta.kind === 'belongsToMany')) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
// Literal-stored properties: equality/IN/NOT can be pushed to SPARQL via
|
|
49
|
+
// <ad4m://fn/parse_literal>(), but comparison operators (gt/lt/gte/lte/between/contains)
|
|
50
|
+
// must remain JS-only because parse_literal returns strings.
|
|
51
|
+
if (isLiteralStoredProperty(propMeta)) {
|
|
52
|
+
if (typeof condition === 'object' && condition !== null && !Array.isArray(condition)) {
|
|
53
|
+
const ops = condition;
|
|
54
|
+
const hasCompOps = ops.gt !== undefined || ops.gte !== undefined ||
|
|
55
|
+
ops.lt !== undefined || ops.lte !== undefined ||
|
|
56
|
+
ops.between !== undefined || ops.contains !== undefined;
|
|
57
|
+
if (hasCompOps)
|
|
58
|
+
return true;
|
|
59
|
+
// NOT with simple value can be pushed to SPARQL — not JS-only
|
|
60
|
+
}
|
|
61
|
+
// Simple equality and IN filters can be pushed to SPARQL — not JS-only
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
13
66
|
/**
|
|
14
67
|
* Check if a string value looks like a URI (has a scheme).
|
|
15
68
|
*/
|
|
@@ -78,6 +131,30 @@ export function formatSPARQLValue(value) {
|
|
|
78
131
|
function iri(value) {
|
|
79
132
|
return `<${value}>`;
|
|
80
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Map a user-facing property name to the SPARQL variable alias used in the query.
|
|
136
|
+
*
|
|
137
|
+
* Properties appear in the query as:
|
|
138
|
+
* - Required properties: `?cfTarget_${name}`
|
|
139
|
+
* - Where-clause joins: `?wTarget_${name}`
|
|
140
|
+
* - Initial-value fallback: `?cfInitTarget_${name}`
|
|
141
|
+
*
|
|
142
|
+
* For ORDER BY, we prefer the conformance variable (always present for required props),
|
|
143
|
+
* then the where-target variable.
|
|
144
|
+
*/
|
|
145
|
+
function mapPropertyToSPARQLVar(propertyName, metadata) {
|
|
146
|
+
const propMeta = metadata.properties[propertyName];
|
|
147
|
+
if (propMeta) {
|
|
148
|
+
if (propMeta.required && !propMeta.getter && !(propMeta.flag && propMeta.initial)) {
|
|
149
|
+
return `?cfTarget_${propertyName}`;
|
|
150
|
+
}
|
|
151
|
+
if (propMeta.initial && !propMeta.flag) {
|
|
152
|
+
return `?cfInitTarget_${propertyName}`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Fallback: where-clause variable or raw name
|
|
156
|
+
return `?wTarget_${propertyName}`;
|
|
157
|
+
}
|
|
81
158
|
/**
|
|
82
159
|
* Build a SPARQL query that returns all links belonging to instances of
|
|
83
160
|
* the given model class, optionally filtered by `where` conditions.
|
|
@@ -161,6 +238,35 @@ export function buildSPARQLQuery(metadata, allRelationsMetadata, query, modelCla
|
|
|
161
238
|
const filterClause = filterExpressions.length > 0
|
|
162
239
|
? `FILTER(\n ${filterExpressions.join(" &&\n ")}\n )`
|
|
163
240
|
: "";
|
|
241
|
+
// Determine if SPARQL-level pagination is safe.
|
|
242
|
+
// When JS-only where filters exist, we must NOT limit at the SPARQL level
|
|
243
|
+
// because the database would cap candidates before JS filters run.
|
|
244
|
+
// Without an explicit ORDER BY, SPARQL OFFSET/LIMIT produces non-deterministic
|
|
245
|
+
// ordering across different query plans and index traversals. Fall back to
|
|
246
|
+
// JS-level slicing in that case to preserve the grouping order from the full
|
|
247
|
+
// result set.
|
|
248
|
+
const canPaginateInSPARQL = !hasJsOnlyWhereFilters(metadata, allRelationsMetadata, query.where)
|
|
249
|
+
&& (query.limit !== undefined || query.offset !== undefined)
|
|
250
|
+
&& query.order !== undefined && Object.keys(query.order).length > 0;
|
|
251
|
+
// Build a pagination subquery that constrains ?source to only the page
|
|
252
|
+
// of interest. This avoids fetching all links for all matching instances.
|
|
253
|
+
const paginationSubquery = canPaginateInSPARQL
|
|
254
|
+
? buildPaginationSubquery(joinPatterns, filterExpressions, metadata, query)
|
|
255
|
+
: '';
|
|
256
|
+
if (paginationSubquery) {
|
|
257
|
+
// Use the pagination subquery to constrain ?source, then fetch all links
|
|
258
|
+
// for those sources only.
|
|
259
|
+
return `
|
|
260
|
+
SELECT ?source ?predicate ?target ?author ?timestamp WHERE {
|
|
261
|
+
${paginationSubquery}${joinClause}
|
|
262
|
+
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
263
|
+
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
264
|
+
?linkGraph <ad4m://ontology/author> ?author .
|
|
265
|
+
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
266
|
+
${filterClause}
|
|
267
|
+
}
|
|
268
|
+
`.trim();
|
|
269
|
+
}
|
|
164
270
|
return `
|
|
165
271
|
SELECT ?source ?predicate ?target ?author ?timestamp WHERE {${joinClause}
|
|
166
272
|
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
@@ -174,10 +280,146 @@ export function buildSPARQLQuery(metadata, allRelationsMetadata, query, modelCla
|
|
|
174
280
|
}
|
|
175
281
|
/**
|
|
176
282
|
* Build ORDER BY / LIMIT / OFFSET clauses for the SPARQL query.
|
|
283
|
+
*
|
|
284
|
+
* NOTE: The outer SELECT returns multiple rows per instance (one per link),
|
|
285
|
+
* so LIMIT/OFFSET at the outer level would cut off links mid-instance.
|
|
286
|
+
* This function is kept for backward compatibility and always returns "".
|
|
287
|
+
* SPARQL-level pagination is handled via a subquery in buildSPARQLQuery
|
|
288
|
+
* using buildPaginationSubquery().
|
|
177
289
|
*/
|
|
178
|
-
function buildSPARQLOrderLimitOffset(_metadata,
|
|
290
|
+
export function buildSPARQLOrderLimitOffset(_metadata, query) {
|
|
179
291
|
return "";
|
|
180
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* Build an inner subquery that selects DISTINCT ?source URIs with
|
|
295
|
+
* ORDER BY, LIMIT, and OFFSET applied at the instance level.
|
|
296
|
+
*
|
|
297
|
+
* Because the outer SELECT returns multiple rows per instance (one per link),
|
|
298
|
+
* we cannot apply LIMIT/OFFSET there. Instead this subquery constrains
|
|
299
|
+
* ?source to only the instances on the requested page.
|
|
300
|
+
*
|
|
301
|
+
* @internal — used by buildSPARQLQuery when SPARQL-level pagination is safe.
|
|
302
|
+
*/
|
|
303
|
+
export function buildPaginationSubquery(joinPatterns, filterExpressions, metadata, query) {
|
|
304
|
+
const innerJoin = joinPatterns.join('\n');
|
|
305
|
+
const innerFilter = filterExpressions.length > 0
|
|
306
|
+
? `FILTER(\n ${filterExpressions.join(' &&\n ')}\n )`
|
|
307
|
+
: '';
|
|
308
|
+
// Build ORDER BY clause from query.order.
|
|
309
|
+
// When ORDER BY references non-grouped variables we must use SAMPLE()
|
|
310
|
+
// aggregates so the query is valid SPARQL 1.1.
|
|
311
|
+
let orderByClause = '';
|
|
312
|
+
const orderJoinPatterns = [];
|
|
313
|
+
const sampleProjections = []; // e.g. (SAMPLE(?cfTarget_title) AS ?pg_sort_0)
|
|
314
|
+
const needsGroupBy = query.order && Object.keys(query.order).length > 0;
|
|
315
|
+
if (query.order) {
|
|
316
|
+
let sortIdx = 0;
|
|
317
|
+
const orderTerms = Object.entries(query.order).map(([prop, dir]) => {
|
|
318
|
+
const sparqlVar = mapPropertyToSPARQLVar(prop, metadata);
|
|
319
|
+
// Check if this order property's variable is already bound by existing
|
|
320
|
+
// join patterns. If not, add an OPTIONAL join so ORDER BY is deterministic.
|
|
321
|
+
const varAlreadyBound = innerJoin.includes(sparqlVar);
|
|
322
|
+
if (!varAlreadyBound) {
|
|
323
|
+
const propMeta = metadata.properties[prop];
|
|
324
|
+
if (propMeta && propMeta.predicate && !propMeta.getter) {
|
|
325
|
+
orderJoinPatterns.push(`\n OPTIONAL { ?source <${propMeta.predicate}> ${sparqlVar} . }`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
// Use SAMPLE() aggregate so ORDER BY is valid with GROUP BY ?source.
|
|
329
|
+
const sortAlias = `?pg_sort_${sortIdx++}`;
|
|
330
|
+
sampleProjections.push(`(SAMPLE(${sparqlVar}) AS ${sortAlias})`);
|
|
331
|
+
return dir === 'DESC' ? `DESC(${sortAlias})` : `ASC(${sortAlias})`;
|
|
332
|
+
});
|
|
333
|
+
if (orderTerms.length > 0) {
|
|
334
|
+
orderByClause = `ORDER BY ${orderTerms.join(' ')}`;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const limitClause = query.limit !== undefined ? `LIMIT ${query.limit}` : '';
|
|
338
|
+
const offsetClause = query.offset !== undefined && query.offset > 0 ? `OFFSET ${query.offset}` : '';
|
|
339
|
+
// When there's no ORDER BY, DISTINCT alone handles deduplication — no GROUP BY needed.
|
|
340
|
+
// When there IS ORDER BY, use GROUP BY ?source with SAMPLE() aggregates for the sort keys.
|
|
341
|
+
const sampleSelect = sampleProjections.length > 0 ? ' ' + sampleProjections.join(' ') : '';
|
|
342
|
+
const groupByClause = needsGroupBy ? 'GROUP BY ?source' : '';
|
|
343
|
+
return `
|
|
344
|
+
{ SELECT DISTINCT ?source${sampleSelect} WHERE {${innerJoin}${orderJoinPatterns.join('')}
|
|
345
|
+
FILTER(isIRI(?source))
|
|
346
|
+
${innerFilter}
|
|
347
|
+
} ${groupByClause} ${orderByClause} ${limitClause} ${offsetClause} }\n`;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Build a SPARQL COUNT query that returns the total number of matching
|
|
351
|
+
* instances WITHOUT hydrating them. Uses the same WHERE clause as
|
|
352
|
+
* buildSPARQLQuery but wraps it in SELECT (COUNT(DISTINCT ?source) AS ?count).
|
|
353
|
+
*
|
|
354
|
+
* LIMIT/OFFSET from the query are stripped internally — this always counts
|
|
355
|
+
* the full result set.
|
|
356
|
+
*/
|
|
357
|
+
export function buildSPARQLCountQuery(metadata, allRelationsMetadata, query, modelClass) {
|
|
358
|
+
// Build the same join/filter patterns as buildSPARQLQuery
|
|
359
|
+
const joinPatterns = [];
|
|
360
|
+
const filterExpressions = [];
|
|
361
|
+
if (query.parent) {
|
|
362
|
+
const parentPredicate = resolveParentPredicate(query.parent, modelClass);
|
|
363
|
+
joinPatterns.push(`\n ${iri(query.parent.id)} ${iri(parentPredicate)} ?source .`);
|
|
364
|
+
}
|
|
365
|
+
let hasConformance = false;
|
|
366
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
367
|
+
if (propMeta.required) {
|
|
368
|
+
if (propMeta.getter)
|
|
369
|
+
continue;
|
|
370
|
+
hasConformance = true;
|
|
371
|
+
if (propMeta.flag && propMeta.initial) {
|
|
372
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ?cfTarget_${propMeta.name} .`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (!hasConformance) {
|
|
380
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
381
|
+
if (propMeta.initial) {
|
|
382
|
+
hasConformance = true;
|
|
383
|
+
if (propMeta.flag) {
|
|
384
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ?cfInitTarget_${propMeta.name} .`);
|
|
388
|
+
}
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (!hasConformance && joinPatterns.length === 0) {
|
|
394
|
+
const knownPredicates = [];
|
|
395
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
396
|
+
if (propMeta.predicate)
|
|
397
|
+
knownPredicates.push(iri(propMeta.predicate));
|
|
398
|
+
}
|
|
399
|
+
if (metadata.relations) {
|
|
400
|
+
for (const [, relMeta] of Object.entries(metadata.relations)) {
|
|
401
|
+
if (relMeta.predicate)
|
|
402
|
+
knownPredicates.push(iri(relMeta.predicate));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (knownPredicates.length > 0) {
|
|
406
|
+
joinPatterns.push(`\n { SELECT DISTINCT ?source WHERE { ?source ?cf_structPred ?cf_structTarget . FILTER(?cf_structPred IN (${knownPredicates.join(", ")})) } }`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const { joins: userJoins, filters: userFilters } = buildSPARQLWhereFilters(metadata, allRelationsMetadata, query.where);
|
|
410
|
+
joinPatterns.push(...userJoins);
|
|
411
|
+
filterExpressions.push(...userFilters);
|
|
412
|
+
const joinClause = joinPatterns.join('\n');
|
|
413
|
+
const filterClause = filterExpressions.length > 0
|
|
414
|
+
? `FILTER(\n ${filterExpressions.join(' &&\n ')}\n )`
|
|
415
|
+
: '';
|
|
416
|
+
return `
|
|
417
|
+
SELECT (COUNT(DISTINCT ?source) AS ?count) WHERE {${joinClause}
|
|
418
|
+
FILTER(isIRI(?source))
|
|
419
|
+
${filterClause}
|
|
420
|
+
}
|
|
421
|
+
`.trim();
|
|
422
|
+
}
|
|
181
423
|
/**
|
|
182
424
|
* Build a SPARQL query to fetch all links for a single instance.
|
|
183
425
|
*/
|
|
@@ -248,7 +490,53 @@ function buildSPARQLWhereFilters(metadata, _allRelationsMetadata, where) {
|
|
|
248
490
|
}
|
|
249
491
|
continue;
|
|
250
492
|
}
|
|
251
|
-
if (propertyName === "author"
|
|
493
|
+
if (propertyName === "author") {
|
|
494
|
+
// ?author is bound by the named graph block in buildSPARQLQuery /
|
|
495
|
+
// buildSPARQLGroupedCountQuery — push equality/IN filters into SPARQL.
|
|
496
|
+
if (Array.isArray(condition)) {
|
|
497
|
+
const formatted = condition.map(v => formatSPARQLValue(String(v))).join(', ');
|
|
498
|
+
filters.push(`STR(?author) IN (${formatted})`);
|
|
499
|
+
}
|
|
500
|
+
else if (typeof condition === 'object' && condition !== null) {
|
|
501
|
+
const ops = condition;
|
|
502
|
+
if (ops.not !== undefined) {
|
|
503
|
+
if (Array.isArray(ops.not)) {
|
|
504
|
+
const formatted = ops.not.map((v) => formatSPARQLValue(String(v))).join(', ');
|
|
505
|
+
filters.push(`!(STR(?author) IN (${formatted}))`);
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
filters.push(`STR(?author) != ${formatSPARQLValue(String(ops.not))}`);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
// Other operators (gt/lt/etc.) fall through to JS post-filter
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
filters.push(`STR(?author) = ${formatSPARQLValue(String(condition))}`);
|
|
515
|
+
}
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
if (propertyName === "timestamp") {
|
|
519
|
+
// ?timestamp is bound by the named graph block — push numeric comparisons.
|
|
520
|
+
if (typeof condition === 'object' && condition !== null && !Array.isArray(condition)) {
|
|
521
|
+
const ops = condition;
|
|
522
|
+
if (ops.gt !== undefined)
|
|
523
|
+
filters.push(`?timestamp > ${Number(ops.gt)}`);
|
|
524
|
+
if (ops.gte !== undefined)
|
|
525
|
+
filters.push(`?timestamp >= ${Number(ops.gte)}`);
|
|
526
|
+
if (ops.lt !== undefined)
|
|
527
|
+
filters.push(`?timestamp < ${Number(ops.lt)}`);
|
|
528
|
+
if (ops.lte !== undefined)
|
|
529
|
+
filters.push(`?timestamp <= ${Number(ops.lte)}`);
|
|
530
|
+
if (ops.between !== undefined && Array.isArray(ops.between)) {
|
|
531
|
+
filters.push(`?timestamp >= ${Number(ops.between[0])} && ?timestamp <= ${Number(ops.between[1])}`);
|
|
532
|
+
}
|
|
533
|
+
if (ops.not !== undefined) {
|
|
534
|
+
filters.push(`?timestamp != ${Number(ops.not)}`);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
else if (typeof condition === 'number' || (typeof condition === 'string' && !isNaN(Number(condition)))) {
|
|
538
|
+
filters.push(`?timestamp = ${Number(condition)}`);
|
|
539
|
+
}
|
|
252
540
|
continue;
|
|
253
541
|
}
|
|
254
542
|
const propMeta = metadata.properties[propertyName];
|
|
@@ -263,33 +551,49 @@ function buildSPARQLWhereFilters(metadata, _allRelationsMetadata, where) {
|
|
|
263
551
|
}
|
|
264
552
|
// Determine if this property stores values as literal: IRIs
|
|
265
553
|
const useParseLiteral = isLiteralStoredProperty(propMeta);
|
|
266
|
-
// For literal-stored properties,
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
// (
|
|
270
|
-
//
|
|
554
|
+
// For literal-stored properties, push equality/IN/NOT filters to SPARQL using
|
|
555
|
+
// <ad4m://fn/parse_literal>() which extracts the raw value from literal: IRIs.
|
|
556
|
+
// The correct SPARQL syntax is <ad4m://fn/parse_literal>(?var) — NOT fn::parse_literal
|
|
557
|
+
// (which was SurrealDB syntax that Oxigraph rejects at parse time).
|
|
558
|
+
// Comparison operators (gt/lt/gte/lte/between/contains) remain JS-only because
|
|
559
|
+
// parse_literal returns strings and numeric/date comparisons would be unreliable.
|
|
560
|
+
// JS post-filter in instancesFromQueryResult remains as a safety net for all cases.
|
|
271
561
|
if (useParseLiteral) {
|
|
272
|
-
// Just ensure the property exists — no value filtering in SPARQL
|
|
273
562
|
if (typeof condition === "object" && condition !== null && !Array.isArray(condition)) {
|
|
274
563
|
const ops = condition;
|
|
275
564
|
const hasCompOps = ops.gt !== undefined || ops.gte !== undefined ||
|
|
276
565
|
ops.lt !== undefined || ops.lte !== undefined ||
|
|
277
|
-
ops.between !== undefined || ops.contains !== undefined
|
|
278
|
-
ops.not !== undefined;
|
|
566
|
+
ops.between !== undefined || ops.contains !== undefined;
|
|
279
567
|
if (hasCompOps) {
|
|
568
|
+
// Comparison operators: join only, filter in JS
|
|
280
569
|
joins.push(`
|
|
281
570
|
?source ${iri(propMeta.predicate)} ?wTarget_cmp_${propertyName} .`);
|
|
282
571
|
}
|
|
283
|
-
|
|
572
|
+
if (ops.not !== undefined) {
|
|
573
|
+
// NOT filter: push down to SPARQL
|
|
574
|
+
joins.push(`
|
|
575
|
+
?source ${iri(propMeta.predicate)} ?wTarget_${propertyName} .`);
|
|
576
|
+
if (Array.isArray(ops.not)) {
|
|
577
|
+
const formatted = ops.not.map((v) => formatSPARQLValue(v)).join(", ");
|
|
578
|
+
filters.push(`STR(<ad4m://fn/parse_literal>(?wTarget_${propertyName})) NOT IN (${formatted})`);
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
filters.push(`STR(<ad4m://fn/parse_literal>(?wTarget_${propertyName})) != ${formatSPARQLValue(ops.not)}`);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
else if (Array.isArray(condition)) {
|
|
586
|
+
// IN filter: push down to SPARQL
|
|
587
|
+
joins.push(`
|
|
588
|
+
?source ${iri(propMeta.predicate)} ?wTarget_${propertyName} .`);
|
|
589
|
+
const formatted = condition.map((v) => formatSPARQLValue(v)).join(", ");
|
|
590
|
+
filters.push(`STR(<ad4m://fn/parse_literal>(?wTarget_${propertyName})) IN (${formatted})`);
|
|
284
591
|
}
|
|
285
592
|
else {
|
|
286
|
-
// Simple equality
|
|
287
|
-
// NOTE: push-down FILTER with fn::parse_literal was attempted but removed because
|
|
288
|
-
// Oxigraph's query parser rejects the fn:: custom function syntax at parse time
|
|
289
|
-
// (custom functions are only available at execution time). All literal property
|
|
290
|
-
// filtering is done in JS post-filter after hydration resolves values.
|
|
593
|
+
// Simple equality: push down to SPARQL
|
|
291
594
|
joins.push(`
|
|
292
595
|
?source ${iri(propMeta.predicate)} ?wTarget_${propertyName} .`);
|
|
596
|
+
filters.push(`STR(<ad4m://fn/parse_literal>(?wTarget_${propertyName})) = ${formatSPARQLValue(condition)}`);
|
|
293
597
|
}
|
|
294
598
|
}
|
|
295
599
|
else if (Array.isArray(condition)) {
|
|
@@ -334,3 +638,130 @@ function buildSPARQLWhereFilters(metadata, _allRelationsMetadata, where) {
|
|
|
334
638
|
}
|
|
335
639
|
return { joins, filters };
|
|
336
640
|
}
|
|
641
|
+
/**
|
|
642
|
+
* Parse a SPARQL COUNT result row into a number.
|
|
643
|
+
* Handles `row.count?.value` (RDF binding), `row.count` (direct number/string),
|
|
644
|
+
* NaN results, and empty/malformed results.
|
|
645
|
+
*
|
|
646
|
+
* @returns The parsed count, or `null` if the result cannot be interpreted.
|
|
647
|
+
*/
|
|
648
|
+
export function parseSparqlCount(result) {
|
|
649
|
+
if (!Array.isArray(result) || result.length === 0)
|
|
650
|
+
return null;
|
|
651
|
+
const row = result[0];
|
|
652
|
+
const val = row.count?.value ?? row.count;
|
|
653
|
+
if (val === undefined || val === null)
|
|
654
|
+
return null;
|
|
655
|
+
const parsed = typeof val === 'number' ? val : parseInt(String(val), 10);
|
|
656
|
+
return isNaN(parsed) ? null : parsed;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Build a SPARQL query that counts child instances grouped by parent IRI.
|
|
660
|
+
*
|
|
661
|
+
* Used by the projection Path A (`count: true`) in `hydrateRelations` to get
|
|
662
|
+
* per-parent counts in a single round-trip with zero instance hydration.
|
|
663
|
+
*
|
|
664
|
+
* Includes the full named graph block so that `?author` and `?timestamp` are
|
|
665
|
+
* bound and available for SPARQL FILTER expressions (unlike the intentionally
|
|
666
|
+
* minimal `buildSPARQLCountQuery` which omits them for speed).
|
|
667
|
+
*
|
|
668
|
+
* @param metadata - Model metadata for the **child** (target) class
|
|
669
|
+
* @param allRelationsMetadata - Relations metadata for the child class
|
|
670
|
+
* @param relationPredicate - The predicate linking `?parent` → `?source` (child)
|
|
671
|
+
* @param parentIds - IRI strings of all parent instances to count for
|
|
672
|
+
* @param where - Optional filter applied to child instances
|
|
673
|
+
*/
|
|
674
|
+
export function buildSPARQLGroupedCountQuery(metadata, allRelationsMetadata, relationPredicate, parentIds, where) {
|
|
675
|
+
const joinPatterns = [];
|
|
676
|
+
const filterExpressions = [];
|
|
677
|
+
// Parent → child join and IN filter
|
|
678
|
+
joinPatterns.push(`\n ?parent ${iri(relationPredicate)} ?source .`);
|
|
679
|
+
const parentIris = parentIds.map(id => iri(id)).join(', ');
|
|
680
|
+
filterExpressions.push(`?parent IN (${parentIris})`);
|
|
681
|
+
// Conformance patterns (mirrors buildSPARQLCountQuery logic)
|
|
682
|
+
let hasConformance = false;
|
|
683
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
684
|
+
if (propMeta.required) {
|
|
685
|
+
if (propMeta.getter)
|
|
686
|
+
continue;
|
|
687
|
+
hasConformance = true;
|
|
688
|
+
if (propMeta.flag && propMeta.initial) {
|
|
689
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ?cfTarget_${propMeta.name} .`);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if (!hasConformance) {
|
|
697
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
698
|
+
if (propMeta.initial) {
|
|
699
|
+
hasConformance = true;
|
|
700
|
+
if (propMeta.flag) {
|
|
701
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
|
|
702
|
+
}
|
|
703
|
+
else {
|
|
704
|
+
joinPatterns.push(`\n ?source ${iri(propMeta.predicate)} ?cfInitTarget_${propMeta.name} .`);
|
|
705
|
+
}
|
|
706
|
+
break;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (!hasConformance && joinPatterns.length <= 1) {
|
|
711
|
+
// Only the parent join so far — add open-world structural matching
|
|
712
|
+
const knownPredicates = [];
|
|
713
|
+
for (const [, propMeta] of Object.entries(metadata.properties)) {
|
|
714
|
+
if (propMeta.predicate)
|
|
715
|
+
knownPredicates.push(iri(propMeta.predicate));
|
|
716
|
+
}
|
|
717
|
+
if (metadata.relations) {
|
|
718
|
+
for (const [, relMeta] of Object.entries(metadata.relations)) {
|
|
719
|
+
if (relMeta.predicate)
|
|
720
|
+
knownPredicates.push(iri(relMeta.predicate));
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
if (knownPredicates.length > 0) {
|
|
724
|
+
joinPatterns.push(`\n { SELECT DISTINCT ?source WHERE { ?source ?cf_structPred ?cf_structTarget . FILTER(?cf_structPred IN (${knownPredicates.join(', ')})) } }`);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
// User where-clause filters (including author/timestamp after the SPARQL push)
|
|
728
|
+
const { joins: userJoins, filters: userFilters } = buildSPARQLWhereFilters(metadata, allRelationsMetadata, where);
|
|
729
|
+
joinPatterns.push(...userJoins);
|
|
730
|
+
filterExpressions.push(...userFilters);
|
|
731
|
+
const joinClause = joinPatterns.join('\n');
|
|
732
|
+
const filterClause = filterExpressions.length > 0
|
|
733
|
+
? `FILTER(\n ${filterExpressions.join(' &&\n ')}\n )`
|
|
734
|
+
: '';
|
|
735
|
+
return `
|
|
736
|
+
SELECT ?parent (COUNT(DISTINCT ?source) AS ?count) WHERE {${joinClause}
|
|
737
|
+
GRAPH ?linkGraph { ?source ?predicate ?target . }
|
|
738
|
+
FILTER(isIRI(?source) && isIRI(?predicate))
|
|
739
|
+
?linkGraph <ad4m://ontology/author> ?author .
|
|
740
|
+
?linkGraph <ad4m://ontology/timestamp> ?timestamp .
|
|
741
|
+
${filterClause}
|
|
742
|
+
}
|
|
743
|
+
GROUP BY ?parent
|
|
744
|
+
`.trim();
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Parse the result of `buildSPARQLGroupedCountQuery` into a map from parent IRI
|
|
748
|
+
* to count. Missing parents (zero matches) are absent from the map — callers
|
|
749
|
+
* should use `map.get(id) ?? 0`.
|
|
750
|
+
*
|
|
751
|
+
* @returns Map from parentId string → count number
|
|
752
|
+
*/
|
|
753
|
+
export function parseSparqlGroupedCount(result) {
|
|
754
|
+
const out = new Map();
|
|
755
|
+
if (!Array.isArray(result))
|
|
756
|
+
return out;
|
|
757
|
+
for (const row of result) {
|
|
758
|
+
const parentVal = row.parent?.value ?? row.parent;
|
|
759
|
+
const countVal = row.count?.value ?? row.count;
|
|
760
|
+
if (!parentVal || countVal === undefined || countVal === null)
|
|
761
|
+
continue;
|
|
762
|
+
const parsed = typeof countVal === 'number' ? countVal : parseInt(String(countVal), 10);
|
|
763
|
+
if (!isNaN(parsed))
|
|
764
|
+
out.set(String(parentVal), parsed);
|
|
765
|
+
}
|
|
766
|
+
return out;
|
|
767
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|