@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,79 @@
1
+ /**
2
+ * Query Result TTL Cache
3
+ *
4
+ * Short-lived cache (200ms TTL) for SPARQL query results.
5
+ * Prevents redundant queries during rapid render cycles where
6
+ * multiple components issue the same query within milliseconds.
7
+ *
8
+ * Key: `perspectiveUUID + queryText`
9
+ */
10
+
11
+ interface CacheEntry {
12
+ result: any;
13
+ expiry: number;
14
+ }
15
+
16
+ const cache = new Map<string, CacheEntry>();
17
+
18
+ const DEFAULT_TTL_MS = 200;
19
+
20
+ function makeKey(perspectiveUUID: string, queryText: string): string {
21
+ return `${perspectiveUUID}:${queryText}`;
22
+ }
23
+
24
+ /**
25
+ * Get a cached query result if it exists and hasn't expired.
26
+ */
27
+ export function getCachedResult(perspectiveUUID: string, queryText: string): any | undefined {
28
+ const key = makeKey(perspectiveUUID, queryText);
29
+ const entry = cache.get(key);
30
+ if (!entry) return undefined;
31
+ if (Date.now() > entry.expiry) {
32
+ cache.delete(key);
33
+ return undefined;
34
+ }
35
+ return entry.result;
36
+ }
37
+
38
+ /**
39
+ * Store a query result in the cache with TTL.
40
+ */
41
+ export function setCachedResult(perspectiveUUID: string, queryText: string, result: any, ttlMs: number = DEFAULT_TTL_MS): void {
42
+ // Lazy eviction of expired entries
43
+ const now = Date.now();
44
+ for (const [key, entry] of cache) {
45
+ if (now > entry.expiry) {
46
+ cache.delete(key);
47
+ }
48
+ }
49
+
50
+ const cacheKey = makeKey(perspectiveUUID, queryText);
51
+ cache.set(cacheKey, { result, expiry: now + ttlMs });
52
+ }
53
+
54
+ /**
55
+ * Invalidate all cache entries for a given perspective.
56
+ * Called after write operations to ensure stale data isn't served.
57
+ */
58
+ export function invalidatePerspectiveCache(perspectiveUUID: string): void {
59
+ const prefix = perspectiveUUID + ':';
60
+ for (const [key] of cache) {
61
+ if (key.startsWith(prefix)) {
62
+ cache.delete(key);
63
+ }
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Clear all cache entries (useful for testing).
69
+ */
70
+ export function clearQueryCache(): void {
71
+ cache.clear();
72
+ }
73
+
74
+ /**
75
+ * Get the current size of the cache (useful for testing).
76
+ */
77
+ export function queryCacheSize(): number {
78
+ return cache.size;
79
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Shared query helpers used by both the Prolog and SurrealQL query pipelines.
2
+ * Shared query helpers used by both the Prolog and SPARQL query pipelines.
3
3
  */
4
4
 
5
5
  import type { ParentScope } from "./types";
@@ -0,0 +1,301 @@
1
+ /**
2
+ * Batch SPARQL query builder for Ad4mModel eager-loading (includes).
3
+ *
4
+ * Uses direct triple + named graph storage model.
5
+ * Each AD4M link is stored as: <source> <predicate> <target> in a named graph
6
+ * with metadata in the default graph keyed by graph IRI.
7
+ *
8
+ * @module
9
+ */
10
+
11
+ import { resolveParentPredicate } from "./query-common";
12
+ import { getRelationsMetadata } from "./decorators";
13
+ import type { RelationMetadataEntry } from "./decorators";
14
+ import { formatSPARQLValue } from "./query-sparql";
15
+ import { Literal } from "../Literal";
16
+ import type { Query, IncludeMap, ModelMetadata, PropertyMetadata } from "./types";
17
+
18
+ // ──────────────────────────────────────────────────────────
19
+ // Helpers
20
+ // ──────────────────────────────────────────────────────────
21
+
22
+ function iri(value: string): string {
23
+ return `<${value}>`;
24
+ }
25
+
26
+ /**
27
+ * Check if a string value looks like a URI (has a scheme).
28
+ */
29
+ function looksLikeUri(value: string): boolean {
30
+ return /^[a-zA-Z][a-zA-Z0-9+\-._]*:/.test(value);
31
+ }
32
+
33
+ /**
34
+ * Convert a JS value to its literal: IRI form.
35
+ */
36
+ function valueToLiteralIri(value: any): string {
37
+ if (typeof value === 'string') {
38
+ if (looksLikeUri(value)) return value;
39
+ return Literal.from(value).toUrl();
40
+ }
41
+ if (typeof value === 'number') return Literal.from(value).toUrl();
42
+ if (typeof value === 'boolean') return Literal.from(value).toUrl();
43
+ return Literal.from(String(value)).toUrl();
44
+ }
45
+
46
+ /**
47
+ * Determine if a property stores values as literal: IRIs.
48
+ */
49
+ function isLiteralStoredProperty(propMeta: PropertyMetadata): boolean {
50
+ if (propMeta.flag) return false;
51
+ if (propMeta.resolveLanguage && propMeta.resolveLanguage !== "literal") return false;
52
+ return true;
53
+ }
54
+
55
+ interface DepthBranch {
56
+ depth: number;
57
+ relationName: string;
58
+ parentDepth: number;
59
+ parentPredicate: string;
60
+ targetClass: any;
61
+ targetMetadata: ModelMetadata;
62
+ includeMap?: IncludeMap;
63
+ direction: 'forward' | 'reverse';
64
+ }
65
+
66
+ function buildConformanceJoins(metadata: ModelMetadata, sourceVar: string, prefix: string): { joins: string[]; filters: string[] } {
67
+ const joins: string[] = [];
68
+ const filters: string[] = [];
69
+ let hasConformance = false;
70
+
71
+ for (const [, propMeta] of Object.entries(metadata.properties)) {
72
+ if (propMeta.required) {
73
+ if (propMeta.getter) continue;
74
+ hasConformance = true;
75
+ if (propMeta.flag && propMeta.initial) {
76
+ joins.push(`
77
+ ${sourceVar} ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
78
+ } else {
79
+ joins.push(`
80
+ ${sourceVar} ${iri(propMeta.predicate)} ?${prefix}_cfTarget_${propMeta.name} .`);
81
+ }
82
+ }
83
+ }
84
+
85
+ if (!hasConformance) {
86
+ for (const [, propMeta] of Object.entries(metadata.properties)) {
87
+ if (propMeta.initial) {
88
+ if (propMeta.flag) {
89
+ joins.push(`
90
+ ${sourceVar} ${iri(propMeta.predicate)} ${iri(propMeta.initial)} .`);
91
+ } else {
92
+ joins.push(`
93
+ ${sourceVar} ${iri(propMeta.predicate)} ?${prefix}_cfInitAny_${propMeta.name} .`);
94
+ }
95
+ break;
96
+ }
97
+ }
98
+ }
99
+
100
+ return { joins, filters };
101
+ }
102
+
103
+ function flattenIncludeTree(
104
+ parentDepth: number,
105
+ parentClass: any,
106
+ includeMap: IncludeMap,
107
+ nextDepth: { value: number },
108
+ ): DepthBranch[] {
109
+ const branches: DepthBranch[] = [];
110
+ const relMeta = getRelationsMetadata(parentClass);
111
+
112
+ for (const [relName, includeValue] of Object.entries(includeMap)) {
113
+ const meta: RelationMetadataEntry | undefined = relMeta[relName];
114
+ if (!meta || !meta.target) continue;
115
+
116
+ const TargetClass = meta.target();
117
+ const targetMetadata: ModelMetadata = (TargetClass as any).getModelMetadata
118
+ ? (TargetClass as any).getModelMetadata()
119
+ : { className: "Unknown", properties: {}, relations: {} };
120
+
121
+ const depth = nextDepth.value++;
122
+ const direction = meta.kind === 'belongsToOne' || meta.kind === 'belongsToMany'
123
+ ? 'reverse' : 'forward';
124
+
125
+ const nestedInclude = typeof includeValue === 'object' && includeValue !== null
126
+ ? (includeValue as any).include
127
+ : undefined;
128
+
129
+ branches.push({
130
+ depth,
131
+ relationName: relName,
132
+ parentDepth,
133
+ parentPredicate: meta.predicate,
134
+ targetClass: TargetClass,
135
+ targetMetadata,
136
+ includeMap: nestedInclude,
137
+ direction,
138
+ });
139
+
140
+ if (nestedInclude) {
141
+ branches.push(...flattenIncludeTree(depth, TargetClass, nestedInclude, nextDepth));
142
+ }
143
+ }
144
+
145
+ return branches;
146
+ }
147
+
148
+ // ──────────────────────────────────────────────────────────
149
+ // Main entry point
150
+ // ──────────────────────────────────────────────────────────
151
+
152
+ /**
153
+ * Build a single SPARQL query that fetches root instances plus all
154
+ * included relations in one query using UNION branches.
155
+ *
156
+ * Uses direct triple patterns in named graphs with metadata in default graph.
157
+ */
158
+ export function buildBatchSPARQLQuery(
159
+ rootMetadata: ModelMetadata,
160
+ query: Query,
161
+ rootModelClass: any,
162
+ ): string {
163
+ const includeMap = query.include;
164
+ if (!includeMap || Object.keys(includeMap).length === 0) {
165
+ throw new Error("buildBatchSPARQLQuery requires query.include to be non-empty");
166
+ }
167
+
168
+ const nextDepth = { value: 1 };
169
+ const branches = flattenIncludeTree(0, rootModelClass, includeMap, nextDepth);
170
+
171
+ // Root conformance
172
+ const rootConformance = buildConformanceJoins(rootMetadata, "?source", "root");
173
+ const rootJoins = [...rootConformance.joins];
174
+ const rootFilters = [...rootConformance.filters];
175
+
176
+ if (query.parent) {
177
+ const parentPredicate = resolveParentPredicate(query.parent, rootModelClass);
178
+ rootJoins.push(`
179
+ ${iri(query.parent.id)} ${iri(parentPredicate)} ?source .`);
180
+ }
181
+
182
+ if (query.where) {
183
+ for (const [propertyName, condition] of Object.entries(query.where)) {
184
+ if (propertyName === "base" || propertyName === "id") {
185
+ if (Array.isArray(condition)) {
186
+ const formatted = (condition as any[]).map(v => iri(v)).join(", ");
187
+ rootFilters.push(`?source IN (${formatted})`);
188
+ } else if (typeof condition === "string") {
189
+ rootFilters.push(`?source = ${iri(condition)}`);
190
+ }
191
+ continue;
192
+ }
193
+ if (propertyName === "author" || propertyName === "timestamp") continue;
194
+
195
+ const propMeta = rootMetadata.properties[propertyName];
196
+ if (!propMeta) continue;
197
+
198
+ const useParseLiteral = isLiteralStoredProperty(propMeta);
199
+
200
+ if (typeof condition === "string" || typeof condition === "number" || typeof condition === "boolean") {
201
+ if (useParseLiteral) {
202
+ // For literal-stored properties, just add a join for conformance — filtering is done in JS
203
+ rootJoins.push(`
204
+ ?source ${iri(propMeta.predicate)} ?root_wTarget_eq_${propertyName} .`);
205
+ } else {
206
+ rootJoins.push(`
207
+ ?source ${iri(propMeta.predicate)} ${iri(String(condition))} .`);
208
+ }
209
+ } else if (Array.isArray(condition)) {
210
+ if (useParseLiteral) {
211
+ // For literal-stored properties, just add a join for conformance — filtering is done in JS
212
+ rootJoins.push(`
213
+ ?source ${iri(propMeta.predicate)} ?root_wTarget_${propertyName} .`);
214
+ } else {
215
+ const formatted = (condition as any[]).map(v => iri(v)).join(", ");
216
+ rootJoins.push(`
217
+ ?source ${iri(propMeta.predicate)} ?root_wTarget_${propertyName} .`);
218
+ rootFilters.push(`?root_wTarget_${propertyName} IN (${formatted})`);
219
+ }
220
+ }
221
+ }
222
+ }
223
+
224
+ const rootJoinClause = rootJoins.join("\n");
225
+ const rootFilterClause = rootFilters.length > 0
226
+ ? `FILTER(\n ${rootFilters.join(" &&\n ")}\n )`
227
+ : "";
228
+
229
+ const unionBranches: string[] = [];
230
+
231
+ // Depth 0: root — direct triple pattern with named graph
232
+ unionBranches.push(`
233
+ {
234
+ ${rootJoinClause}
235
+ GRAPH ?linkGraph { ?source ?predicate ?target . }
236
+ FILTER(isIRI(?source) && isIRI(?predicate))
237
+ ?linkGraph <ad4m://ontology/author> ?author .
238
+ ?linkGraph <ad4m://ontology/timestamp> ?timestamp .
239
+ ${rootFilterClause}
240
+ BIND("0" AS ?depth)
241
+ BIND("" AS ?parentBase)
242
+ BIND("" AS ?relationName)
243
+ }
244
+ `);
245
+
246
+ // Depth N: include branches
247
+ for (const branch of branches) {
248
+ const childConformance = buildConformanceJoins(branch.targetMetadata, "?source", `d${branch.depth}`);
249
+ const childJoinClause = childConformance.joins.join("\n");
250
+ const childFilterClause = childConformance.filters.length > 0
251
+ ? `FILTER(\n ${childConformance.filters.join(" &&\n ")}\n )`
252
+ : "";
253
+
254
+ const parentJoinClause = branch.parentDepth === 0 ? rootJoinClause.replace(/\?source/g, '?parentBase') : '';
255
+ const parentFilterClause = branch.parentDepth === 0 ? rootFilterClause.replace(/\?source/g, '?parentBase') : '';
256
+
257
+ if (branch.direction === 'forward') {
258
+ // Forward: parent --predicate--> child (source)
259
+ unionBranches.push(`
260
+ {
261
+ # Depth ${branch.depth}: ${branch.relationName} (forward)
262
+ ?parentBase ${iri(branch.parentPredicate)} ?source .
263
+ ${childJoinClause}
264
+ GRAPH ?linkGraph { ?source ?predicate ?target . }
265
+ FILTER(isIRI(?source) && isIRI(?predicate))
266
+ ?linkGraph <ad4m://ontology/author> ?author .
267
+ ?linkGraph <ad4m://ontology/timestamp> ?timestamp .
268
+ ${childFilterClause}
269
+ ${parentJoinClause}
270
+ ${parentFilterClause}
271
+ BIND("${branch.depth}" AS ?depth)
272
+ BIND("${branch.relationName}" AS ?relationName)
273
+ }
274
+ `);
275
+ } else {
276
+ // Reverse: child (source) --predicate--> parent
277
+ unionBranches.push(`
278
+ {
279
+ # Depth ${branch.depth}: ${branch.relationName} (reverse)
280
+ ?source ${iri(branch.parentPredicate)} ?parentBase .
281
+ ${childJoinClause}
282
+ GRAPH ?linkGraph { ?source ?predicate ?target . }
283
+ FILTER(isIRI(?source) && isIRI(?predicate))
284
+ ?linkGraph <ad4m://ontology/author> ?author .
285
+ ?linkGraph <ad4m://ontology/timestamp> ?timestamp .
286
+ ${childFilterClause}
287
+ ${parentJoinClause}
288
+ ${parentFilterClause}
289
+ BIND("${branch.depth}" AS ?depth)
290
+ BIND("${branch.relationName}" AS ?relationName)
291
+ }
292
+ `);
293
+ }
294
+ }
295
+
296
+ return `
297
+ SELECT ?depth ?parentBase ?relationName ?source ?predicate ?target ?author ?timestamp WHERE {
298
+ ${unionBranches.join("\n UNION\n")}
299
+ }
300
+ `.trim();
301
+ }