@coasys/ad4m 0.13.0-test-1 → 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.
Files changed (115) hide show
  1. package/lib/index.cjs +2377 -849
  2. package/lib/index.js +2354 -846
  3. package/lib/src/Literal.js +6 -5
  4. package/lib/src/agent/AgentClient.d.ts +5 -1
  5. package/lib/src/agent/AgentClient.js +48 -2
  6. package/lib/src/expression/ExpressionRef.js +2 -2
  7. package/lib/src/index.d.ts +0 -1
  8. package/lib/src/index.js +0 -1
  9. package/lib/src/language/Language.d.ts +26 -176
  10. package/lib/src/language/Language.js +28 -2
  11. package/lib/src/model/Ad4mModel.d.ts +52 -28
  12. package/lib/src/model/Ad4mModel.js +255 -118
  13. package/lib/src/model/Ad4mModel.test.js +1727 -537
  14. package/lib/src/model/ModelQueryBuilder.d.ts +67 -34
  15. package/lib/src/model/ModelQueryBuilder.js +208 -79
  16. package/lib/src/model/Subject.js +6 -6
  17. package/lib/src/model/decorators.d.ts +14 -8
  18. package/lib/src/model/decorators.js +41 -14
  19. package/lib/src/model/hydration-batch.d.ts +34 -0
  20. package/lib/src/model/hydration-batch.js +147 -0
  21. package/lib/src/model/hydration-batching.test.d.ts +7 -0
  22. package/lib/src/model/hydration-batching.test.js +456 -0
  23. package/lib/src/model/hydration.d.ts +4 -3
  24. package/lib/src/model/hydration.js +365 -86
  25. package/lib/src/model/index.d.ts +4 -1
  26. package/lib/src/model/index.js +4 -1
  27. package/lib/src/model/json-schema.js +2 -2
  28. package/lib/src/model/query-cache.d.ts +30 -0
  29. package/lib/src/model/query-cache.js +66 -0
  30. package/lib/src/model/query-cache.test.d.ts +1 -0
  31. package/lib/src/model/query-cache.test.js +43 -0
  32. package/lib/src/model/query-common.d.ts +1 -1
  33. package/lib/src/model/query-common.js +1 -1
  34. package/lib/src/model/query-sparql-batch.d.ts +17 -0
  35. package/lib/src/model/query-sparql-batch.js +260 -0
  36. package/lib/src/model/query-sparql.d.ts +124 -0
  37. package/lib/src/model/query-sparql.js +767 -0
  38. package/lib/src/model/query-sparql.test.d.ts +1 -0
  39. package/lib/src/model/query-sparql.test.js +495 -0
  40. package/lib/src/model/{surreal-utils.d.ts → query-utils.d.ts} +12 -9
  41. package/lib/src/model/{surreal-utils.js → query-utils.js} +43 -27
  42. package/lib/src/model/relation-filtering.test.js +15 -15
  43. package/lib/src/model/sdna.js +1 -1
  44. package/lib/src/model/shacl-gen.js +42 -18
  45. package/lib/src/model/subscription-delta.test.d.ts +24 -0
  46. package/lib/src/model/subscription-delta.test.js +180 -0
  47. package/lib/src/model/subscription-pool.d.ts +39 -0
  48. package/lib/src/model/subscription-pool.js +165 -0
  49. package/lib/src/model/types.d.ts +52 -4
  50. package/lib/src/model/types.js +1 -1
  51. package/lib/src/perspectives/PerspectiveClient.d.ts +3 -17
  52. package/lib/src/perspectives/PerspectiveClient.js +42 -66
  53. package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
  54. package/lib/src/perspectives/PerspectiveClient.test.js +169 -0
  55. package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -30
  56. package/lib/src/perspectives/PerspectiveProxy.js +239 -177
  57. package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
  58. package/lib/src/perspectives/PerspectiveResolver.js +0 -11
  59. package/lib/src/perspectives/WakerSubscriptionManager.d.ts +3 -3
  60. package/lib/src/perspectives/WakerSubscriptionManager.js +7 -8
  61. package/lib/src/runtime/RuntimeResolver.d.ts +11 -0
  62. package/lib/src/runtime/RuntimeResolver.js +56 -0
  63. package/lib/src/schema.gql +11 -1
  64. package/lib/src/shacl/SHACLFlow.d.ts +1 -1
  65. package/lib/src/shacl/SHACLFlow.js +9 -9
  66. package/lib/src/shacl/SHACLShape.d.ts +1 -1
  67. package/lib/src/shacl/SHACLShape.js +38 -38
  68. package/lib/src/typeDefs.js +11 -1
  69. package/lib/src/utils.d.ts +6 -6
  70. package/lib/src/utils.js +6 -6
  71. package/package.json +1 -1
  72. package/src/Ad4mClient.test.ts +56 -1
  73. package/src/Literal.test.ts +10 -5
  74. package/src/Literal.ts +6 -5
  75. package/src/agent/AgentClient.ts +53 -1
  76. package/src/expression/ExpressionRef.ts +2 -2
  77. package/src/index.ts +0 -1
  78. package/src/language/Language.ts +55 -231
  79. package/src/model/Ad4mModel.test.ts +1709 -572
  80. package/src/model/Ad4mModel.ts +274 -120
  81. package/src/model/ModelQueryBuilder.ts +234 -87
  82. package/src/model/Subject.ts +6 -6
  83. package/src/model/decorators.ts +51 -22
  84. package/src/model/hydration-batch.ts +205 -0
  85. package/src/model/hydration-batching.test.ts +563 -0
  86. package/src/model/hydration.ts +349 -84
  87. package/src/model/index.ts +4 -1
  88. package/src/model/json-schema.ts +2 -2
  89. package/src/model/query-cache.test.ts +54 -0
  90. package/src/model/query-cache.ts +79 -0
  91. package/src/model/query-common.ts +1 -1
  92. package/src/model/query-sparql-batch.ts +301 -0
  93. package/src/model/query-sparql.test.ts +568 -0
  94. package/src/model/query-sparql.ts +820 -0
  95. package/src/model/{surreal-utils.ts → query-utils.ts} +44 -27
  96. package/src/model/relation-filtering.test.ts +15 -15
  97. package/src/model/sdna.ts +1 -1
  98. package/src/model/shacl-gen.ts +42 -17
  99. package/src/model/subscription-delta.test.ts +207 -0
  100. package/src/model/subscription-pool.ts +211 -0
  101. package/src/model/types.ts +53 -4
  102. package/src/perspectives/PerspectiveClient.test.ts +204 -0
  103. package/src/perspectives/PerspectiveClient.ts +41 -68
  104. package/src/perspectives/PerspectiveProxy.ts +244 -181
  105. package/src/perspectives/PerspectiveResolver.ts +0 -8
  106. package/src/perspectives/WakerSubscriptionManager.ts +8 -9
  107. package/src/runtime/RuntimeResolver.ts +43 -0
  108. package/src/shacl/SHACLFlow.ts +9 -9
  109. package/src/shacl/SHACLShape.ts +39 -39
  110. package/src/utils.ts +6 -6
  111. package/lib/src/model/query-surreal.d.ts +0 -44
  112. package/lib/src/model/query-surreal.js +0 -249
  113. package/src/language/LanguageContext.ts +0 -37
  114. package/src/model/query-surreal.ts +0 -280
  115. package/tsconfig-browser.json +0 -82
@@ -0,0 +1,767 @@
1
+ /**
2
+ * SPARQL query building utilities for Ad4mModel.
3
+ *
4
+ * Uses the direct triple + named graph storage model where each AD4M link
5
+ * is stored in a named graph (keyed by link hash) with metadata in the default graph.
6
+ *
7
+ * All AD4M URIs (source, predicate, target) become RDF IRIs.
8
+ *
9
+ * @module
10
+ */
11
+ import { Literal } from "../Literal";
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
+ }
66
+ /**
67
+ * Check if a string value looks like a URI (has a scheme).
68
+ */
69
+ function looksLikeUri(value) {
70
+ return /^[a-zA-Z][a-zA-Z0-9+\-._]*:/.test(value);
71
+ }
72
+ /**
73
+ * Convert a JS value to its literal: IRI form, matching how the Rust executor
74
+ * stores property values that don't have a resolveLanguage set.
75
+ * Strings that already look like URIs are returned as-is.
76
+ */
77
+ function valueToLiteralIri(value) {
78
+ if (typeof value === 'string') {
79
+ if (looksLikeUri(value))
80
+ return value;
81
+ return Literal.from(value).toUrl();
82
+ }
83
+ if (typeof value === 'number') {
84
+ return Literal.from(value).toUrl();
85
+ }
86
+ if (typeof value === 'boolean') {
87
+ return Literal.from(value).toUrl();
88
+ }
89
+ return Literal.from(String(value)).toUrl();
90
+ }
91
+ /**
92
+ * Determine if a property stores values as literal: IRIs (needs parse_literal for comparisons).
93
+ * Properties with resolveLanguage === "literal" AND properties without resolveLanguage
94
+ * both store values as literal: URIs. Only properties with a non-literal resolveLanguage
95
+ * (e.g. file storage) or flag properties store raw URIs.
96
+ */
97
+ function isLiteralStoredProperty(propMeta) {
98
+ if (propMeta.flag)
99
+ return false;
100
+ // If resolveLanguage is set to something other than "literal", it's a language expression URI
101
+ if (propMeta.resolveLanguage && propMeta.resolveLanguage !== "literal")
102
+ return false;
103
+ // Everything else (resolveLanguage === "literal" or undefined) stores as literal: URIs
104
+ return true;
105
+ }
106
+ /**
107
+ * Escape a string for use as a SPARQL string literal.
108
+ */
109
+ function escapeSPARQL(value) {
110
+ return value
111
+ .replace(/\\/g, "\\\\")
112
+ .replace(/"/g, '\\"')
113
+ .replace(/\n/g, "\\n")
114
+ .replace(/\r/g, "\\r")
115
+ .replace(/\t/g, "\\t");
116
+ }
117
+ /**
118
+ * Format a value as a SPARQL string literal (double-quoted).
119
+ */
120
+ export function formatSPARQLValue(value) {
121
+ if (typeof value === "string") {
122
+ return `"${escapeSPARQL(value)}"`;
123
+ }
124
+ return `"${String(value)}"`;
125
+ }
126
+ /**
127
+ * Format an AD4M URI as an RDF IRI for use in SPARQL triple patterns.
128
+ * All AD4M link source/predicate/target values become IRIs in angle brackets.
129
+ * The Rust SPARQL service transparently transforms these to valid IRI format.
130
+ */
131
+ function iri(value) {
132
+ return `<${value}>`;
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
+ }
158
+ /**
159
+ * Build a SPARQL query that returns all links belonging to instances of
160
+ * the given model class, optionally filtered by `where` conditions.
161
+ *
162
+ * Uses direct triple patterns: ?source ?predicate ?target
163
+ * with named graph storage and metadata keyed by graph IRI.
164
+ */
165
+ export function buildSPARQLQuery(metadata, allRelationsMetadata, query, modelClass) {
166
+ const joinPatterns = [];
167
+ const filterExpressions = [];
168
+ // Parent filter — direct triple pattern
169
+ if (query.parent) {
170
+ const parentPredicate = resolveParentPredicate(query.parent, modelClass);
171
+ joinPatterns.push(`
172
+ ${iri(query.parent.id)} ${iri(parentPredicate)} ?source .`);
173
+ }
174
+ // Required property JOIN patterns — direct triple patterns
175
+ let hasConformance = false;
176
+ for (const [, propMeta] of Object.entries(metadata.properties)) {
177
+ if (propMeta.required) {
178
+ if (propMeta.getter)
179
+ continue;
180
+ hasConformance = true;
181
+ if (propMeta.flag && propMeta.initial) {
182
+ joinPatterns.push(`
183
+ ?source ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
184
+ }
185
+ else {
186
+ joinPatterns.push(`
187
+ ?source ${iri(propMeta.predicate)} ?cfTarget_${propMeta.name} .`);
188
+ }
189
+ }
190
+ }
191
+ // Fallback: initial-value JOIN patterns
192
+ if (!hasConformance) {
193
+ for (const [, propMeta] of Object.entries(metadata.properties)) {
194
+ if (propMeta.initial) {
195
+ hasConformance = true;
196
+ if (propMeta.flag) {
197
+ joinPatterns.push(`
198
+ ?source ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
199
+ }
200
+ else {
201
+ joinPatterns.push(`
202
+ ?source ${iri(propMeta.predicate)} ?cfInitTarget_${propMeta.name} .`);
203
+ }
204
+ break;
205
+ }
206
+ }
207
+ }
208
+ // Fallback: open-world structural matching
209
+ // Use a subquery with DISTINCT to avoid row multiplication when the main
210
+ // ?source ?predicate ?target pattern cross-joins with multiple structural matches.
211
+ if (!hasConformance && joinPatterns.length === 0) {
212
+ const knownPredicates = [];
213
+ for (const [, propMeta] of Object.entries(metadata.properties)) {
214
+ if (propMeta.predicate) {
215
+ knownPredicates.push(iri(propMeta.predicate));
216
+ }
217
+ }
218
+ if (metadata.relations) {
219
+ for (const [, relMeta] of Object.entries(metadata.relations)) {
220
+ if (relMeta.predicate) {
221
+ knownPredicates.push(iri(relMeta.predicate));
222
+ }
223
+ }
224
+ }
225
+ if (knownPredicates.length > 0) {
226
+ joinPatterns.push(`
227
+ { SELECT DISTINCT ?source WHERE { ?source ?cf_structPred ?cf_structTarget . FILTER(?cf_structPred IN (${knownPredicates.join(", ")})) } }`);
228
+ }
229
+ }
230
+ // Build WHERE clause filters from user query
231
+ const { joins: userJoins, filters: userFilters } = buildSPARQLWhereFilters(metadata, allRelationsMetadata, query.where);
232
+ joinPatterns.push(...userJoins);
233
+ filterExpressions.push(...userFilters);
234
+ // Main triple pattern — fetches all links for matched sources
235
+ // The direct triple in named graph: GRAPH ?linkGraph { ?source ?predicate ?target }
236
+ // FILTER(isIRI(?source)) excludes non-IRI subjects
237
+ const joinClause = joinPatterns.join("\n");
238
+ const filterClause = filterExpressions.length > 0
239
+ ? `FILTER(\n ${filterExpressions.join(" &&\n ")}\n )`
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
+ }
270
+ return `
271
+ SELECT ?source ?predicate ?target ?author ?timestamp WHERE {${joinClause}
272
+ GRAPH ?linkGraph { ?source ?predicate ?target . }
273
+ FILTER(isIRI(?source) && isIRI(?predicate))
274
+ ?linkGraph <ad4m://ontology/author> ?author .
275
+ ?linkGraph <ad4m://ontology/timestamp> ?timestamp .
276
+ ${filterClause}
277
+ }
278
+ ${buildSPARQLOrderLimitOffset(metadata, query)}
279
+ `.trim();
280
+ }
281
+ /**
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().
289
+ */
290
+ export function buildSPARQLOrderLimitOffset(_metadata, query) {
291
+ return "";
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
+ }
423
+ /**
424
+ * Build a SPARQL query to fetch all links for a single instance.
425
+ */
426
+ export function buildSPARQLGetDataQuery(baseExpression) {
427
+ return `
428
+ SELECT ?source ?predicate ?target ?author ?timestamp WHERE {
429
+ GRAPH ?linkGraph { ${iri(baseExpression)} ?predicate ?target . }
430
+ FILTER(isIRI(?predicate))
431
+ BIND(${iri(baseExpression)} AS ?source)
432
+ ?linkGraph <ad4m://ontology/author> ?author .
433
+ ?linkGraph <ad4m://ontology/timestamp> ?timestamp .
434
+ }
435
+ `.trim();
436
+ }
437
+ /**
438
+ * Group flat SPARQL link rows into the same shape that SPARQL returned:
439
+ * `{ source_uri: string, links: Array<{predicate, target, author, timestamp}> }`
440
+ */
441
+ export function groupSPARQLResults(rows) {
442
+ const grouped = new Map();
443
+ for (const row of rows) {
444
+ const key = row.source;
445
+ if (!grouped.has(key)) {
446
+ grouped.set(key, []);
447
+ }
448
+ grouped.get(key).push({
449
+ predicate: row.predicate,
450
+ target: row.target,
451
+ author: row.author,
452
+ timestamp: row.timestamp,
453
+ });
454
+ }
455
+ return Array.from(grouped.entries()).map(([source_uri, links]) => ({
456
+ source_uri,
457
+ links,
458
+ }));
459
+ }
460
+ /**
461
+ * Build SPARQL FILTER expressions from user `where` conditions.
462
+ * Uses direct triple patterns instead of link-node reification.
463
+ */
464
+ function buildSPARQLWhereFilters(metadata, _allRelationsMetadata, where) {
465
+ if (!where)
466
+ return { joins: [], filters: [] };
467
+ const joins = [];
468
+ const filters = [];
469
+ for (const [propertyName, condition] of Object.entries(where)) {
470
+ // 'base' maps to ?source
471
+ if (propertyName === "base" || propertyName === "id") {
472
+ if (Array.isArray(condition)) {
473
+ const formatted = condition.map(v => iri(v)).join(", ");
474
+ filters.push(`?source IN (${formatted})`);
475
+ }
476
+ else if (typeof condition === "object" && condition !== null) {
477
+ const ops = condition;
478
+ if (ops.not !== undefined) {
479
+ if (Array.isArray(ops.not)) {
480
+ const formatted = ops.not.map(v => iri(v)).join(", ");
481
+ filters.push(`!(?source IN (${formatted}))`);
482
+ }
483
+ else {
484
+ filters.push(`?source != ${iri(ops.not)}`);
485
+ }
486
+ }
487
+ }
488
+ else {
489
+ filters.push(`?source = ${iri(String(condition))}`);
490
+ }
491
+ continue;
492
+ }
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
+ }
540
+ continue;
541
+ }
542
+ const propMeta = metadata.properties[propertyName];
543
+ if (!propMeta)
544
+ continue;
545
+ // Skip reverse relations (belongsToOne / belongsToMany) — these are registered as
546
+ // read-only properties but the link direction is reversed (target→source, not source→target).
547
+ // Adding a forward join pattern would match nothing. These are filtered in JS post-filter.
548
+ const relMeta = _allRelationsMetadata[propertyName];
549
+ if (relMeta && (relMeta.kind === 'belongsToOne' || relMeta.kind === 'belongsToMany')) {
550
+ continue;
551
+ }
552
+ // Determine if this property stores values as literal: IRIs
553
+ const useParseLiteral = isLiteralStoredProperty(propMeta);
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.
561
+ if (useParseLiteral) {
562
+ if (typeof condition === "object" && condition !== null && !Array.isArray(condition)) {
563
+ const ops = condition;
564
+ const hasCompOps = ops.gt !== undefined || ops.gte !== undefined ||
565
+ ops.lt !== undefined || ops.lte !== undefined ||
566
+ ops.between !== undefined || ops.contains !== undefined;
567
+ if (hasCompOps) {
568
+ // Comparison operators: join only, filter in JS
569
+ joins.push(`
570
+ ?source ${iri(propMeta.predicate)} ?wTarget_cmp_${propertyName} .`);
571
+ }
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})`);
591
+ }
592
+ else {
593
+ // Simple equality: push down to SPARQL
594
+ joins.push(`
595
+ ?source ${iri(propMeta.predicate)} ?wTarget_${propertyName} .`);
596
+ filters.push(`STR(<ad4m://fn/parse_literal>(?wTarget_${propertyName})) = ${formatSPARQLValue(condition)}`);
597
+ }
598
+ }
599
+ else if (Array.isArray(condition)) {
600
+ const formatted = condition.map(v => iri(v)).join(", ");
601
+ joins.push(`
602
+ ?source ${iri(propMeta.predicate)} ?wTarget_${propertyName} .`);
603
+ filters.push(`?wTarget_${propertyName} IN (${formatted})`);
604
+ }
605
+ else if (typeof condition === "object" && condition !== null) {
606
+ const ops = condition;
607
+ if (ops.not !== undefined) {
608
+ if (Array.isArray(ops.not)) {
609
+ const formatted = ops.not.map(v => iri(v)).join(", ");
610
+ filters.push(`
611
+ NOT EXISTS {
612
+ ?source ${iri(propMeta.predicate)} ?wTarget_not_${propertyName} .
613
+ FILTER(?wTarget_not_${propertyName} IN (${formatted}))
614
+ }
615
+ `);
616
+ }
617
+ else {
618
+ filters.push(`
619
+ NOT EXISTS {
620
+ ?source ${iri(propMeta.predicate)} ${iri(ops.not)} .
621
+ }
622
+ `);
623
+ }
624
+ }
625
+ const hasCompOps = ops.gt !== undefined || ops.gte !== undefined ||
626
+ ops.lt !== undefined || ops.lte !== undefined ||
627
+ ops.between !== undefined || ops.contains !== undefined;
628
+ if (hasCompOps) {
629
+ joins.push(`
630
+ ?source ${iri(propMeta.predicate)} ?wTarget_cmp_${propertyName} .`);
631
+ }
632
+ }
633
+ else {
634
+ // Simple equality — non-literal property, use exact IRI match
635
+ joins.push(`
636
+ ?source ${iri(propMeta.predicate)} ${iri(String(condition))} .`);
637
+ }
638
+ }
639
+ return { joins, filters };
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
+ }