@coasys/ad4m 0.12.1-dev → 0.13.0-test-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/lib/index.cjs +1469 -781
- package/lib/index.js +1453 -779
- package/lib/src/Literal.js +6 -5
- package/lib/src/agent/AgentClient.d.ts +8 -1
- package/lib/src/agent/AgentClient.js +73 -2
- package/lib/src/expression/ExpressionRef.js +2 -2
- package/lib/src/language/Language.d.ts +41 -3
- package/lib/src/model/Ad4mModel.d.ts +28 -28
- package/lib/src/model/Ad4mModel.js +129 -101
- package/lib/src/model/Ad4mModel.test.js +1543 -535
- package/lib/src/model/ModelQueryBuilder.d.ts +48 -34
- package/lib/src/model/ModelQueryBuilder.js +163 -78
- package/lib/src/model/Subject.js +6 -6
- package/lib/src/model/decorators.d.ts +14 -8
- package/lib/src/model/decorators.js +41 -14
- package/lib/src/model/hydration-batch.d.ts +34 -0
- package/lib/src/model/hydration-batch.js +147 -0
- package/lib/src/model/hydration.d.ts +3 -3
- package/lib/src/model/hydration.js +106 -36
- package/lib/src/model/index.d.ts +4 -1
- package/lib/src/model/index.js +4 -1
- package/lib/src/model/json-schema.js +2 -2
- package/lib/src/model/query-cache.d.ts +30 -0
- package/lib/src/model/query-cache.js +66 -0
- package/lib/src/model/query-cache.test.d.ts +1 -0
- package/lib/src/model/query-cache.test.js +43 -0
- package/lib/src/model/query-common.d.ts +1 -1
- package/lib/src/model/query-common.js +1 -1
- package/lib/src/model/query-sparql-batch.d.ts +17 -0
- package/lib/src/model/query-sparql-batch.js +260 -0
- package/lib/src/model/query-sparql.d.ts +47 -0
- package/lib/src/model/query-sparql.js +336 -0
- package/lib/src/model/query-utils.d.ts +45 -0
- package/lib/src/model/query-utils.js +140 -0
- package/lib/src/model/relation-filtering.test.js +15 -15
- package/lib/src/model/sdna.js +1 -1
- package/lib/src/model/shacl-gen.js +42 -18
- package/lib/src/model/subscription-pool.d.ts +39 -0
- package/lib/src/model/subscription-pool.js +119 -0
- package/lib/src/model/types.d.ts +3 -3
- package/lib/src/model/types.js +1 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +1 -17
- package/lib/src/perspectives/PerspectiveClient.js +5 -57
- package/lib/src/perspectives/PerspectiveProxy.d.ts +51 -30
- package/lib/src/perspectives/PerspectiveProxy.js +224 -177
- package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveResolver.js +0 -11
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +3 -3
- package/lib/src/perspectives/WakerSubscriptionManager.js +7 -8
- package/lib/src/runtime/RuntimeClient.d.ts +2 -0
- package/lib/src/runtime/RuntimeClient.js +17 -0
- package/lib/src/runtime/RuntimeResolver.d.ts +14 -0
- package/lib/src/runtime/RuntimeResolver.js +84 -0
- package/lib/src/schema.gql +14 -1
- package/lib/src/shacl/SHACLFlow.d.ts +1 -1
- package/lib/src/shacl/SHACLFlow.js +9 -9
- package/lib/src/shacl/SHACLShape.d.ts +1 -1
- package/lib/src/shacl/SHACLShape.js +38 -38
- package/lib/src/typeDefs.js +14 -1
- package/lib/src/utils.d.ts +6 -6
- package/lib/src/utils.js +6 -6
- package/package.json +1 -1
- package/src/Literal.test.ts +10 -5
- package/src/Literal.ts +6 -5
- package/src/agent/AgentClient.ts +81 -1
- package/src/expression/ExpressionRef.ts +2 -2
- package/src/language/Language.ts +48 -3
- package/src/model/Ad4mModel.test.ts +1515 -565
- package/src/model/Ad4mModel.ts +142 -102
- package/src/model/ModelQueryBuilder.ts +185 -86
- package/src/model/Subject.ts +6 -6
- package/src/model/decorators.ts +51 -22
- package/src/model/hydration-batch.ts +205 -0
- package/src/model/hydration.ts +102 -39
- package/src/model/index.ts +4 -1
- package/src/model/json-schema.ts +2 -2
- package/src/model/query-cache.test.ts +54 -0
- package/src/model/query-cache.ts +79 -0
- package/src/model/query-common.ts +1 -1
- package/src/model/query-sparql-batch.ts +301 -0
- package/src/model/query-sparql.ts +363 -0
- package/src/model/{surreal-utils.ts → query-utils.ts} +40 -27
- package/src/model/relation-filtering.test.ts +15 -15
- package/src/model/sdna.ts +1 -1
- package/src/model/shacl-gen.ts +42 -17
- package/src/model/subscription-pool.ts +163 -0
- package/src/model/types.ts +3 -3
- package/src/perspectives/PerspectiveClient.ts +5 -61
- package/src/perspectives/PerspectiveProxy.ts +226 -181
- package/src/perspectives/PerspectiveResolver.ts +0 -8
- package/src/perspectives/WakerSubscriptionManager.ts +8 -9
- package/src/runtime/RuntimeClient.ts +19 -0
- package/src/runtime/RuntimeResolver.ts +58 -0
- package/src/shacl/SHACLFlow.ts +9 -9
- package/src/shacl/SHACLShape.ts +39 -39
- package/src/utils.ts +6 -6
- package/src/model/query-surreal.ts +0 -280
package/src/model/json-schema.ts
CHANGED
|
@@ -353,7 +353,7 @@ export function buildModelFromJSONSchema(
|
|
|
353
353
|
|
|
354
354
|
// Default resolveLanguage to 'literal' for all remaining property types,
|
|
355
355
|
// matching the @Property decorator behaviour. Without this, string values
|
|
356
|
-
// stored as literal
|
|
356
|
+
// stored as literal: URLs are returned unparsed.
|
|
357
357
|
if (resolveLanguage === undefined || resolveLanguage === null) {
|
|
358
358
|
resolveLanguage = 'literal';
|
|
359
359
|
}
|
|
@@ -361,7 +361,7 @@ export function buildModelFromJSONSchema(
|
|
|
361
361
|
// If property is required, ensure it has an initial value
|
|
362
362
|
if (isRequired && !initial) {
|
|
363
363
|
if (isObjectType(propertySchema)) {
|
|
364
|
-
initial = 'literal
|
|
364
|
+
initial = 'literal:json:{}';
|
|
365
365
|
} else {
|
|
366
366
|
initial = "ad4m://undefined";
|
|
367
367
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { getCachedResult, setCachedResult, invalidatePerspectiveCache, clearQueryCache, queryCacheSize } from './query-cache';
|
|
2
|
+
|
|
3
|
+
describe('query-cache', () => {
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
clearQueryCache();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe('lazy eviction', () => {
|
|
9
|
+
it('should remove expired entries when setting a new one', () => {
|
|
10
|
+
// Insert with a very short TTL
|
|
11
|
+
setCachedResult('p1', 'SELECT ?x', 'result1', 1);
|
|
12
|
+
expect(queryCacheSize()).toBe(1);
|
|
13
|
+
|
|
14
|
+
// Wait for expiry
|
|
15
|
+
const start = Date.now();
|
|
16
|
+
while (Date.now() - start < 5) { /* busy wait 5ms */ }
|
|
17
|
+
|
|
18
|
+
// Expired entry should still be in the map (not yet evicted)
|
|
19
|
+
// but getCachedResult should return undefined
|
|
20
|
+
expect(getCachedResult('p1', 'SELECT ?x')).toBeUndefined();
|
|
21
|
+
|
|
22
|
+
// Setting a new entry should evict the expired one
|
|
23
|
+
setCachedResult('p2', 'SELECT ?y', 'result2', 10000);
|
|
24
|
+
// Only the new entry should remain
|
|
25
|
+
expect(queryCacheSize()).toBe(1);
|
|
26
|
+
expect(getCachedResult('p2', 'SELECT ?y')).toBe('result2');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('invalidatePerspectiveCache', () => {
|
|
31
|
+
it('should remove all entries for a specific perspective', () => {
|
|
32
|
+
setCachedResult('p1', 'query1', 'r1', 10000);
|
|
33
|
+
setCachedResult('p1', 'query2', 'r2', 10000);
|
|
34
|
+
setCachedResult('p2', 'query1', 'r3', 10000);
|
|
35
|
+
expect(queryCacheSize()).toBe(3);
|
|
36
|
+
|
|
37
|
+
invalidatePerspectiveCache('p1');
|
|
38
|
+
|
|
39
|
+
expect(queryCacheSize()).toBe(1);
|
|
40
|
+
expect(getCachedResult('p1', 'query1')).toBeUndefined();
|
|
41
|
+
expect(getCachedResult('p1', 'query2')).toBeUndefined();
|
|
42
|
+
expect(getCachedResult('p2', 'query1')).toBe('r3');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should not affect other perspectives', () => {
|
|
46
|
+
setCachedResult('p1', 'q', 'r1', 10000);
|
|
47
|
+
setCachedResult('p2', 'q', 'r2', 10000);
|
|
48
|
+
|
|
49
|
+
invalidatePerspectiveCache('p3');
|
|
50
|
+
|
|
51
|
+
expect(queryCacheSize()).toBe(2);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|