@coasys/ad4m 0.13.0-postmessage-ws-proxy.3 → 0.13.0-postmessage-ws-proxy.5
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 +214 -145
- package/lib/index.js +205 -146
- package/lib/src/Ad4mClient.d.ts +1 -0
- package/lib/src/Ad4mClient.js +1 -1
- package/lib/src/ai/AIClient.js +1 -1
- package/lib/src/apiClient.d.ts +4 -1
- package/lib/src/apiClient.js +6 -1
- package/lib/src/index.d.ts +3 -0
- package/lib/src/index.js +3 -0
- package/lib/src/model/Ad4mModel.d.ts +0 -15
- package/lib/src/model/Ad4mModel.js +2 -118
- package/lib/src/model/ModelQueryBuilder.js +12 -22
- package/lib/src/model/decorators.d.ts +7 -4
- package/lib/src/model/shacl-gen.js +5 -1
- package/lib/src/model/types.d.ts +2 -1
- package/lib/src/shacl/NodeExpression.d.ts +44 -0
- package/lib/src/shacl/NodeExpression.js +39 -0
- package/lib/src/shacl/SHACLShape.d.ts +5 -0
- package/lib/src/shacl/SHACLShape.js +32 -1
- package/lib/src/shacl/builders.d.ts +58 -0
- package/lib/src/shacl/builders.js +76 -0
- package/lib/src/shacl/index.d.ts +36 -0
- package/lib/src/shacl/index.js +33 -0
- package/package.json +1 -1
- package/src/Ad4mClient.ts +2 -2
- package/src/ai/AIClient.ts +1 -1
- package/src/apiClient.ts +8 -1
- package/src/index.ts +3 -0
- package/src/model/Ad4mModel.test.ts +10 -10
- package/src/model/Ad4mModel.ts +2 -113
- package/src/model/ModelQueryBuilder.ts +12 -23
- package/src/model/decorators.ts +7 -4
- package/src/model/shacl-gen.ts +6 -1
- package/src/model/types.ts +2 -1
- package/src/shacl/NodeExpression.ts +54 -0
- package/src/shacl/SHACLShape.test.ts +62 -0
- package/src/shacl/SHACLShape.ts +48 -1
- package/src/shacl/builders.ts +86 -0
- package/src/shacl/index.ts +72 -0
|
@@ -9,48 +9,6 @@ import { buildSPARQLQuery } from "./query-sparql";
|
|
|
9
9
|
import { ModelQueryBuilder } from "./ModelQueryBuilder";
|
|
10
10
|
import { normalizeValue, } from "./hydration";
|
|
11
11
|
// ---------------------------------------------------------------------------
|
|
12
|
-
// Default decoder for file-storage (non-literal resolveLanguage) properties
|
|
13
|
-
// ---------------------------------------------------------------------------
|
|
14
|
-
/**
|
|
15
|
-
* When the Rust executor resolves a non-literal expression it may return a
|
|
16
|
-
* `FileData`-shaped object (`{ data_base64, file_type, ... }`) rather than a
|
|
17
|
-
* raw URI string. If the model class has no custom `transform` registered
|
|
18
|
-
* (e.g. it was synthesised from a SHACL shape at runtime) we still want to
|
|
19
|
-
* decode the content rather than passing raw `FileData` to callers.
|
|
20
|
-
*
|
|
21
|
-
* Decoding rules (mirrors the JS `decodeFileAsJson` / `decodeFileAsString` helpers):
|
|
22
|
-
* - `application/json` (default when `file_type` is absent) → JSON.parse
|
|
23
|
-
* - anything else → raw decoded string
|
|
24
|
-
*
|
|
25
|
-
* If `resolved` is not a `FileData` object the value is returned unchanged so
|
|
26
|
-
* that callers can use this as a safe unconditional fallback.
|
|
27
|
-
*/
|
|
28
|
-
function defaultFileDecode(resolved) {
|
|
29
|
-
if (resolved !== null &&
|
|
30
|
-
typeof resolved === 'object' &&
|
|
31
|
-
'data_base64' in resolved) {
|
|
32
|
-
const fd = resolved;
|
|
33
|
-
let raw;
|
|
34
|
-
try {
|
|
35
|
-
raw = atob(fd.data_base64);
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
return resolved; // malformed base64 — keep as-is
|
|
39
|
-
}
|
|
40
|
-
const isJson = !fd.file_type || fd.file_type === 'application/json';
|
|
41
|
-
if (isJson) {
|
|
42
|
-
try {
|
|
43
|
-
return JSON.parse(raw);
|
|
44
|
-
}
|
|
45
|
-
catch {
|
|
46
|
-
return raw;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return raw;
|
|
50
|
-
}
|
|
51
|
-
return resolved;
|
|
52
|
-
}
|
|
53
|
-
// ---------------------------------------------------------------------------
|
|
54
12
|
// Helpers for Rust-side include resolution
|
|
55
13
|
// ---------------------------------------------------------------------------
|
|
56
14
|
/**
|
|
@@ -97,24 +55,6 @@ function jsonToModelInstance(ModelClass, perspective, json, include, properties)
|
|
|
97
55
|
}
|
|
98
56
|
instance[key] = value;
|
|
99
57
|
}
|
|
100
|
-
// Apply property transform functions from decorators (only for literal-resolved
|
|
101
|
-
// or no-resolveLanguage properties; non-literal properties are resolved + transformed
|
|
102
|
-
// asynchronously in executeModelQuery).
|
|
103
|
-
try {
|
|
104
|
-
const propsMeta = getPropertiesMetadata(ModelClass);
|
|
105
|
-
for (const [propName, opts] of Object.entries(propsMeta)) {
|
|
106
|
-
const o = opts;
|
|
107
|
-
if (typeof o.transform !== 'function' || !(propName in json))
|
|
108
|
-
continue;
|
|
109
|
-
// Skip non-literal resolveLanguage props — they need async expression resolution first
|
|
110
|
-
if (o.resolveLanguage != null && o.resolveLanguage !== 'literal')
|
|
111
|
-
continue;
|
|
112
|
-
instance[propName] = o.transform(instance[propName]);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
catch (e) {
|
|
116
|
-
console.debug('jsonToModelInstance: transform metadata unavailable:', e);
|
|
117
|
-
}
|
|
118
58
|
// Recursively convert included relation values to class instances
|
|
119
59
|
if (include) {
|
|
120
60
|
const relMeta = getRelationsMetadata(ModelClass);
|
|
@@ -352,8 +292,8 @@ export class Ad4mModel {
|
|
|
352
292
|
...(options.getter !== undefined && { getter: options.getter }),
|
|
353
293
|
...(options.prologSetter !== undefined && { prologSetter: options.prologSetter }),
|
|
354
294
|
...(options.local !== undefined && { local: options.local }),
|
|
355
|
-
...(options.
|
|
356
|
-
...(options.
|
|
295
|
+
...(options.flag !== undefined && { flag: options.flag }),
|
|
296
|
+
...(options.transform !== undefined && { transform: options.transform })
|
|
357
297
|
};
|
|
358
298
|
}
|
|
359
299
|
// Extract relations (relations) from WeakMap registry
|
|
@@ -830,57 +770,6 @@ export class Ad4mModel {
|
|
|
830
770
|
return [];
|
|
831
771
|
return arr.map((json) => jsonToModelInstance(this, perspective, json, include, properties));
|
|
832
772
|
}
|
|
833
|
-
/**
|
|
834
|
-
* Resolve non-literal (file-language) properties on an array of already-constructed
|
|
835
|
-
* model instances. Handles two cases:
|
|
836
|
-
*
|
|
837
|
-
* 1. The Rust executor returned a raw URI string → fetch via `getExpression` then apply transform.
|
|
838
|
-
* 2. The Rust executor already resolved the expression and returned a `FileData` object
|
|
839
|
-
* (happens when the perspective backend eagerly fetches file content) → apply transform
|
|
840
|
-
* (or `defaultFileDecode` if no transform is registered on the class, e.g. for SHACL-
|
|
841
|
-
* synthesised model classes).
|
|
842
|
-
*
|
|
843
|
-
* Extracted as a public static so `ModelQueryBuilder.subscribe()` can reuse it without
|
|
844
|
-
* duplicating logic or introducing a circular dependency.
|
|
845
|
-
* @internal
|
|
846
|
-
*/
|
|
847
|
-
static async resolveNonLiteralProps(perspective, instances) {
|
|
848
|
-
const propsMeta = getPropertiesMetadata(this);
|
|
849
|
-
const resolveProps = Object.entries(propsMeta).filter(([, opts]) => opts.resolveLanguage != null &&
|
|
850
|
-
opts.resolveLanguage !== 'literal');
|
|
851
|
-
if (resolveProps.length === 0)
|
|
852
|
-
return;
|
|
853
|
-
await Promise.all(instances.map(async (inst) => {
|
|
854
|
-
for (const [propName, opts] of resolveProps) {
|
|
855
|
-
const val = inst[propName];
|
|
856
|
-
const transform = opts.transform;
|
|
857
|
-
const applyTransform = (resolved) => typeof transform === 'function' ? transform(resolved) : defaultFileDecode(resolved);
|
|
858
|
-
if (typeof val === 'string' && val && !val.startsWith('literal:')) {
|
|
859
|
-
// Case 1: raw URI — fetch from language runtime
|
|
860
|
-
try {
|
|
861
|
-
const expression = await perspective.getExpression(val);
|
|
862
|
-
if (expression) {
|
|
863
|
-
let resolved;
|
|
864
|
-
try {
|
|
865
|
-
resolved = JSON.parse(expression.data);
|
|
866
|
-
}
|
|
867
|
-
catch {
|
|
868
|
-
resolved = expression.data;
|
|
869
|
-
}
|
|
870
|
-
inst[propName] = applyTransform(resolved);
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
catch (e) {
|
|
874
|
-
console.debug(`resolveNonLiteralProps: resolution failed for '${propName}':`, e);
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
else if (val !== null && val !== undefined && typeof val === 'object') {
|
|
878
|
-
// Case 2: already resolved by Rust — apply transform / default decode
|
|
879
|
-
inst[propName] = applyTransform(val);
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
}));
|
|
883
|
-
}
|
|
884
773
|
// instancesFromQueryResult — removed (superseded by Rust executeModelQuery pipeline)
|
|
885
774
|
/**
|
|
886
775
|
* Execute a model query via the executor-side endpoint.
|
|
@@ -900,11 +789,6 @@ export class Ad4mModel {
|
|
|
900
789
|
const instances = result.instances.map((json) => {
|
|
901
790
|
return jsonToModelInstance(this, perspective, json, query.include, query.properties);
|
|
902
791
|
});
|
|
903
|
-
// Resolve non-literal expressions (e.g. file languages where the stored
|
|
904
|
-
// value is a content-addressed hash that must be fetched from the language
|
|
905
|
-
// runtime). The Rust endpoint may return either a raw target URI (string)
|
|
906
|
-
// or an already-resolved FileData object — both cases are handled here.
|
|
907
|
-
await this.resolveNonLiteralProps(perspective, instances);
|
|
908
792
|
// Take snapshots for dirty tracking (exclude $-prefixed projection keys)
|
|
909
793
|
const snapshotRelations = query.include
|
|
910
794
|
? Object.fromEntries(Object.entries(query.include).filter(([k]) => !k.startsWith('$')))
|
|
@@ -347,14 +347,8 @@ export class ModelQueryBuilder {
|
|
|
347
347
|
const parseResults = (raw) => {
|
|
348
348
|
return ctor.parseModelResult(this.perspective, raw, this.queryParams.include, this.queryParams.properties);
|
|
349
349
|
};
|
|
350
|
-
//
|
|
351
|
-
|
|
352
|
-
const resolveAndReturn = async (instances) => {
|
|
353
|
-
await ctor.resolveNonLiteralProps(this.perspective, instances);
|
|
354
|
-
return instances;
|
|
355
|
-
};
|
|
356
|
-
// Parse initial results (with non-literal resolution)
|
|
357
|
-
const initialResults = await resolveAndReturn(parseResults(initialModelResult));
|
|
350
|
+
// Transforms are now applied by the Rust executor during hydration
|
|
351
|
+
const initialResults = parseResults(initialModelResult);
|
|
358
352
|
// Track last emitted result fingerprint to suppress duplicate callbacks
|
|
359
353
|
let lastResultFingerprint = null;
|
|
360
354
|
const buildFingerprint = (results) => {
|
|
@@ -367,20 +361,16 @@ export class ModelQueryBuilder {
|
|
|
367
361
|
// When Rust re-runs the model query and finds changed results, it pushes them.
|
|
368
362
|
const unsubscribe = this.perspective.client.subscribeToQueryUpdates(subscriptionId, (rawResult) => {
|
|
369
363
|
try {
|
|
370
|
-
const
|
|
371
|
-
console.debug(`[ModelQueryBuilder.subscribe] Update received for ${subscriptionId}: ${
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
callback(results);
|
|
381
|
-
}).catch((e) => {
|
|
382
|
-
console.error('Model subscription update resolve error:', e);
|
|
383
|
-
});
|
|
364
|
+
const results = parseResults(rawResult);
|
|
365
|
+
console.debug(`[ModelQueryBuilder.subscribe] Update received for ${subscriptionId}: ${results.length} instances`);
|
|
366
|
+
const fp = buildFingerprint(results);
|
|
367
|
+
if (fp === lastResultFingerprint) {
|
|
368
|
+
console.debug(`[ModelQueryBuilder.subscribe] Fingerprint unchanged, skipping callback`);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
console.debug(`[ModelQueryBuilder.subscribe] Fingerprint changed, calling callback with ${results.length} results`);
|
|
372
|
+
lastResultFingerprint = fp;
|
|
373
|
+
callback(results);
|
|
384
374
|
}
|
|
385
375
|
catch (e) {
|
|
386
376
|
console.error('Model subscription update parse error:', e);
|
|
@@ -130,11 +130,14 @@ export interface PropertyOptions {
|
|
|
130
130
|
*/
|
|
131
131
|
local?: boolean;
|
|
132
132
|
/**
|
|
133
|
-
* Optional transform
|
|
134
|
-
* This is
|
|
135
|
-
*
|
|
133
|
+
* Optional transform expression to modify the property value.
|
|
134
|
+
* This is a SHACL-AF Node Expression that runs in the Rust model query engine.
|
|
135
|
+
* Examples: `fileToDataUri`, `concat(literal('prefix_'), focus())`, etc.
|
|
136
|
+
* Builders (`focus`, `literal`, `path`, `exists`, `ifExpr`, `concat`,
|
|
137
|
+
* `coalesce`, `fn`) and the `fileToDataUri` built-in are exported from
|
|
138
|
+
* the package root: `import { concat, literal, focus } from '@coasys/ad4m'`.
|
|
136
139
|
*/
|
|
137
|
-
transform?: (
|
|
140
|
+
transform?: import('../shacl/NodeExpression').NodeExpression;
|
|
138
141
|
/**
|
|
139
142
|
* Allowed values for this property (enum constraint).
|
|
140
143
|
* Maps to SHACL `sh:in`. Each entry has a `value` (the stored RDF term)
|
|
@@ -109,7 +109,7 @@ export function buildSHACL(subjectName, target, properties, allRelationsMeta, co
|
|
|
109
109
|
if (propMeta.writable !== undefined) {
|
|
110
110
|
propShape.writable = propMeta.writable;
|
|
111
111
|
}
|
|
112
|
-
if (propMeta.resolveLanguage) {
|
|
112
|
+
if (propMeta.resolveLanguage != null) {
|
|
113
113
|
propShape.resolveLanguage = propMeta.resolveLanguage;
|
|
114
114
|
}
|
|
115
115
|
// Explicit getter SPARQL — the executor evaluates this when
|
|
@@ -118,6 +118,10 @@ export function buildSHACL(subjectName, target, properties, allRelationsMeta, co
|
|
|
118
118
|
if (propMeta.getter) {
|
|
119
119
|
propShape.getter = propMeta.getter;
|
|
120
120
|
}
|
|
121
|
+
// Serializable transform expression (SHACL-AF Node Expression)
|
|
122
|
+
if (propMeta.transform) {
|
|
123
|
+
propShape.transform = propMeta.transform;
|
|
124
|
+
}
|
|
121
125
|
// ── Setter actions ──────────────────────────────────────────────
|
|
122
126
|
if (propMeta.prologSetter) {
|
|
123
127
|
console.warn(`[SHACL Generation] Custom Prolog setter for property '${propName}' in class '${subjectName}' is not yet supported. ` +
|
package/lib/src/model/types.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* creating circular runtime dependencies on `Ad4mModel.ts`.
|
|
7
7
|
*/
|
|
8
8
|
import type { Ad4mModel } from "./Ad4mModel";
|
|
9
|
+
import type { NodeExpression } from "../shacl/NodeExpression";
|
|
9
10
|
export type WhereOps = {
|
|
10
11
|
not: string | number | boolean | string[] | number[];
|
|
11
12
|
between: [number, number];
|
|
@@ -322,7 +323,7 @@ export interface PropertyMetadata {
|
|
|
322
323
|
/** Whether stored locally only */
|
|
323
324
|
local?: boolean;
|
|
324
325
|
/** Transform function */
|
|
325
|
-
transform?:
|
|
326
|
+
transform?: NodeExpression;
|
|
326
327
|
/** Whether this is a flag property */
|
|
327
328
|
flag?: boolean;
|
|
328
329
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHACL-AF Node Expressions for property transforms.
|
|
3
|
+
*
|
|
4
|
+
* A `NodeExpression` is a JSON-serializable tree that describes how to transform
|
|
5
|
+
* a resolved property value. Unlike JS lambdas, these are portable, inspectable,
|
|
6
|
+
* and can be executed in the Rust model query engine.
|
|
7
|
+
*
|
|
8
|
+
* Each expression is a tagged union that evaluates to a `Value` (JSON-compatible).
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Union type representing all supported transform expressions.
|
|
12
|
+
* Each variant maps to a SHACL-AF §6 expression construct.
|
|
13
|
+
*/
|
|
14
|
+
export type NodeExpression = {
|
|
15
|
+
type: 'focus';
|
|
16
|
+
} | {
|
|
17
|
+
type: 'literal';
|
|
18
|
+
value: string | number | boolean;
|
|
19
|
+
} | {
|
|
20
|
+
type: 'path';
|
|
21
|
+
predicate: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'exists';
|
|
24
|
+
expr: NodeExpression;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'if';
|
|
27
|
+
cond: NodeExpression;
|
|
28
|
+
then: NodeExpression;
|
|
29
|
+
else?: NodeExpression;
|
|
30
|
+
} | {
|
|
31
|
+
type: 'concat';
|
|
32
|
+
args: NodeExpression[];
|
|
33
|
+
} | {
|
|
34
|
+
type: 'coalesce';
|
|
35
|
+
args: NodeExpression[];
|
|
36
|
+
} | {
|
|
37
|
+
type: 'function';
|
|
38
|
+
iri: string;
|
|
39
|
+
args: NodeExpression[];
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Type guard to check if a value is a valid NodeExpression.
|
|
43
|
+
*/
|
|
44
|
+
export declare function isNodeExpression(v: unknown): v is NodeExpression;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHACL-AF Node Expressions for property transforms.
|
|
3
|
+
*
|
|
4
|
+
* A `NodeExpression` is a JSON-serializable tree that describes how to transform
|
|
5
|
+
* a resolved property value. Unlike JS lambdas, these are portable, inspectable,
|
|
6
|
+
* and can be executed in the Rust model query engine.
|
|
7
|
+
*
|
|
8
|
+
* Each expression is a tagged union that evaluates to a `Value` (JSON-compatible).
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Type guard to check if a value is a valid NodeExpression.
|
|
12
|
+
*/
|
|
13
|
+
export function isNodeExpression(v) {
|
|
14
|
+
if (typeof v !== 'object' || v === null)
|
|
15
|
+
return false;
|
|
16
|
+
const obj = v;
|
|
17
|
+
const type = obj.type;
|
|
18
|
+
switch (type) {
|
|
19
|
+
case 'focus':
|
|
20
|
+
return typeof obj.type === 'string';
|
|
21
|
+
case 'literal':
|
|
22
|
+
return typeof obj.value === 'string' || typeof obj.value === 'number' || typeof obj.value === 'boolean';
|
|
23
|
+
case 'path':
|
|
24
|
+
return typeof obj.predicate === 'string';
|
|
25
|
+
case 'exists':
|
|
26
|
+
return isNodeExpression(obj.expr);
|
|
27
|
+
case 'if':
|
|
28
|
+
return isNodeExpression(obj.cond) && isNodeExpression(obj.then) &&
|
|
29
|
+
(obj.else === undefined || isNodeExpression(obj.else));
|
|
30
|
+
case 'concat':
|
|
31
|
+
return Array.isArray(obj.args) && obj.args.every(isNodeExpression);
|
|
32
|
+
case 'coalesce':
|
|
33
|
+
return Array.isArray(obj.args) && obj.args.every(isNodeExpression);
|
|
34
|
+
case 'function':
|
|
35
|
+
return typeof obj.iri === 'string' && Array.isArray(obj.args) && obj.args.every(isNodeExpression);
|
|
36
|
+
default:
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Link } from "../links/Links";
|
|
2
|
+
import type { NodeExpression } from "./NodeExpression";
|
|
2
3
|
/**
|
|
3
4
|
* AD4M Action - represents a link operation
|
|
4
5
|
*/
|
|
@@ -94,6 +95,10 @@ export interface SHACLPropertyShape {
|
|
|
94
95
|
* relation. `false` opts out of DB-level type filtering while keeping
|
|
95
96
|
* hydration via `include`. */
|
|
96
97
|
filter?: boolean;
|
|
98
|
+
/** AD4M-specific: Transform expression (SHACL-AF Node Expression).
|
|
99
|
+
* Applied to resolved property values in the Rust model query engine.
|
|
100
|
+
* Only used for properties with resolveLanguage set. */
|
|
101
|
+
transform?: NodeExpression;
|
|
97
102
|
}
|
|
98
103
|
/**
|
|
99
104
|
* SHACL Node Shape
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isNodeExpression } from "./NodeExpression";
|
|
1
2
|
/**
|
|
2
3
|
* Extract namespace from a URI
|
|
3
4
|
* Examples:
|
|
@@ -328,7 +329,7 @@ export class SHACLShape {
|
|
|
328
329
|
target: `literal:${prop.writable}`
|
|
329
330
|
});
|
|
330
331
|
}
|
|
331
|
-
if (prop.resolveLanguage) {
|
|
332
|
+
if (prop.resolveLanguage != null) {
|
|
332
333
|
links.push({
|
|
333
334
|
source: propShapeId,
|
|
334
335
|
predicate: "ad4m://resolveLanguage",
|
|
@@ -420,6 +421,13 @@ export class SHACLShape {
|
|
|
420
421
|
target: `literal:${prop.filter}`
|
|
421
422
|
});
|
|
422
423
|
}
|
|
424
|
+
if (prop.transform && typeof prop.transform === 'object') {
|
|
425
|
+
links.push({
|
|
426
|
+
source: propShapeId,
|
|
427
|
+
predicate: "ad4m://transform",
|
|
428
|
+
target: `literal:string:${JSON.stringify(prop.transform)}`
|
|
429
|
+
});
|
|
430
|
+
}
|
|
423
431
|
}
|
|
424
432
|
return links;
|
|
425
433
|
}
|
|
@@ -639,6 +647,22 @@ export class SHACLShape {
|
|
|
639
647
|
val = val.substring(8);
|
|
640
648
|
prop.filter = val === 'true';
|
|
641
649
|
}
|
|
650
|
+
const transformLink = links.find(l => l.source === propShapeId && l.predicate === "ad4m://transform");
|
|
651
|
+
if (transformLink) {
|
|
652
|
+
const jsonStr = transformLink.target.replace(/^literal:\/\/string:|^literal:string:/, '');
|
|
653
|
+
try {
|
|
654
|
+
const parsed = JSON.parse(jsonStr);
|
|
655
|
+
if (!isNodeExpression(parsed)) {
|
|
656
|
+
throw new Error(`Invalid transform for property ${propShapeId}: ` +
|
|
657
|
+
`payload is not a valid NodeExpression. Received: ${jsonStr}`);
|
|
658
|
+
}
|
|
659
|
+
prop.transform = parsed;
|
|
660
|
+
}
|
|
661
|
+
catch (e) {
|
|
662
|
+
throw new Error(`Failed to deserialize transform for property ${propShapeId}: ` +
|
|
663
|
+
`${e instanceof Error ? e.message : String(e)}. Payload: ${jsonStr}`);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
642
666
|
shape.addProperty(prop);
|
|
643
667
|
}
|
|
644
668
|
return shape;
|
|
@@ -680,6 +704,7 @@ export class SHACLShape {
|
|
|
680
704
|
where_filter: p.whereFilter,
|
|
681
705
|
where_predicates: p.wherePredicates,
|
|
682
706
|
filter: p.filter,
|
|
707
|
+
transform: p.transform,
|
|
683
708
|
})),
|
|
684
709
|
constructor_actions: this.constructor_actions,
|
|
685
710
|
destructor_actions: this.destructor_actions,
|
|
@@ -693,6 +718,11 @@ export class SHACLShape {
|
|
|
693
718
|
? new SHACLShape(json.node_shape_uri, json.target_class)
|
|
694
719
|
: new SHACLShape(json.target_class);
|
|
695
720
|
for (const p of json.properties || []) {
|
|
721
|
+
// Validate transform if present
|
|
722
|
+
if (p.transform && !isNodeExpression(p.transform)) {
|
|
723
|
+
throw new Error(`Invalid transform for property ${p.name}: ` +
|
|
724
|
+
`payload is not a valid NodeExpression. Received: ${JSON.stringify(p.transform)}`);
|
|
725
|
+
}
|
|
696
726
|
shape.addProperty({
|
|
697
727
|
path: p.path,
|
|
698
728
|
name: p.name,
|
|
@@ -719,6 +749,7 @@ export class SHACLShape {
|
|
|
719
749
|
whereFilter: p.where_filter,
|
|
720
750
|
wherePredicates: p.where_predicates,
|
|
721
751
|
filter: p.filter,
|
|
752
|
+
transform: p.transform,
|
|
722
753
|
});
|
|
723
754
|
}
|
|
724
755
|
if (json.constructor_actions) {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builder functions for SHACL-AF Node Expressions.
|
|
3
|
+
*
|
|
4
|
+
* These functions construct JSON-serializable expression trees for property transforms.
|
|
5
|
+
* All builders return frozen objects to prevent accidental mutation.
|
|
6
|
+
*/
|
|
7
|
+
import type { NodeExpression } from './NodeExpression';
|
|
8
|
+
/**
|
|
9
|
+
* Identity expression: pass the focus node through unchanged.
|
|
10
|
+
*/
|
|
11
|
+
export declare function focus(): NodeExpression;
|
|
12
|
+
/**
|
|
13
|
+
* Literal constant expression.
|
|
14
|
+
*/
|
|
15
|
+
export declare function literal(value: string | number | boolean): NodeExpression;
|
|
16
|
+
/**
|
|
17
|
+
* Path expression: navigate to a property on the resolved object.
|
|
18
|
+
* The predicate can be a full IRI (e.g., "file_storage://data_base64")
|
|
19
|
+
* or a simple property name. The Rust evaluator will strip the namespace.
|
|
20
|
+
*/
|
|
21
|
+
export declare function path(predicate: string): NodeExpression;
|
|
22
|
+
/**
|
|
23
|
+
* Existence check: returns true if the inner expression is non-null and non-empty.
|
|
24
|
+
*/
|
|
25
|
+
export declare function exists(expr: NodeExpression): NodeExpression;
|
|
26
|
+
/**
|
|
27
|
+
* Conditional expression with optional else branch.
|
|
28
|
+
* Returns a chainable builder that supports `.then()` and `.else()`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function ifExpr(cond: NodeExpression): {
|
|
31
|
+
then: (thenExpr: NodeExpression) => {
|
|
32
|
+
else: (elseExpr?: NodeExpression) => Readonly<{
|
|
33
|
+
readonly else: NodeExpression;
|
|
34
|
+
readonly type: "if";
|
|
35
|
+
readonly cond: NodeExpression;
|
|
36
|
+
readonly then: NodeExpression;
|
|
37
|
+
}>;
|
|
38
|
+
toExpression: () => Readonly<{
|
|
39
|
+
readonly type: "if";
|
|
40
|
+
readonly cond: NodeExpression;
|
|
41
|
+
readonly then: NodeExpression;
|
|
42
|
+
}>;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* String concatenation: joins evaluated arguments as strings.
|
|
47
|
+
* Null/undefined arguments are skipped.
|
|
48
|
+
*/
|
|
49
|
+
export declare function concat(...args: NodeExpression[]): NodeExpression;
|
|
50
|
+
/**
|
|
51
|
+
* Coalesce: returns the first non-null evaluated argument.
|
|
52
|
+
*/
|
|
53
|
+
export declare function coalesce(...args: NodeExpression[]): NodeExpression;
|
|
54
|
+
/**
|
|
55
|
+
* Function call: invoke a named transform function by IRI.
|
|
56
|
+
* Primarily used for built-in functions like "ad4m://fn/defaultFileDecode".
|
|
57
|
+
*/
|
|
58
|
+
export declare function fn(iri: string, args?: NodeExpression[]): NodeExpression;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builder functions for SHACL-AF Node Expressions.
|
|
3
|
+
*
|
|
4
|
+
* These functions construct JSON-serializable expression trees for property transforms.
|
|
5
|
+
* All builders return frozen objects to prevent accidental mutation.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Identity expression: pass the focus node through unchanged.
|
|
9
|
+
*/
|
|
10
|
+
export function focus() {
|
|
11
|
+
return Object.freeze({ type: 'focus' });
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Literal constant expression.
|
|
15
|
+
*/
|
|
16
|
+
export function literal(value) {
|
|
17
|
+
return Object.freeze({ type: 'literal', value });
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Path expression: navigate to a property on the resolved object.
|
|
21
|
+
* The predicate can be a full IRI (e.g., "file_storage://data_base64")
|
|
22
|
+
* or a simple property name. The Rust evaluator will strip the namespace.
|
|
23
|
+
*/
|
|
24
|
+
export function path(predicate) {
|
|
25
|
+
return Object.freeze({ type: 'path', predicate });
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Existence check: returns true if the inner expression is non-null and non-empty.
|
|
29
|
+
*/
|
|
30
|
+
export function exists(expr) {
|
|
31
|
+
return Object.freeze({ type: 'exists', expr });
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Conditional expression with optional else branch.
|
|
35
|
+
* Returns a chainable builder that supports `.then()` and `.else()`.
|
|
36
|
+
*/
|
|
37
|
+
export function ifExpr(cond) {
|
|
38
|
+
return {
|
|
39
|
+
then: (thenExpr) => ({
|
|
40
|
+
else: (elseExpr) => {
|
|
41
|
+
return Object.freeze({
|
|
42
|
+
type: 'if',
|
|
43
|
+
cond,
|
|
44
|
+
then: thenExpr,
|
|
45
|
+
...(elseExpr !== undefined && { else: elseExpr }),
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
// Allow chaining without explicit else()
|
|
49
|
+
toExpression: () => Object.freeze({
|
|
50
|
+
type: 'if',
|
|
51
|
+
cond,
|
|
52
|
+
then: thenExpr,
|
|
53
|
+
}),
|
|
54
|
+
}),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* String concatenation: joins evaluated arguments as strings.
|
|
59
|
+
* Null/undefined arguments are skipped.
|
|
60
|
+
*/
|
|
61
|
+
export function concat(...args) {
|
|
62
|
+
return Object.freeze({ type: 'concat', args });
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Coalesce: returns the first non-null evaluated argument.
|
|
66
|
+
*/
|
|
67
|
+
export function coalesce(...args) {
|
|
68
|
+
return Object.freeze({ type: 'coalesce', args });
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Function call: invoke a named transform function by IRI.
|
|
72
|
+
* Primarily used for built-in functions like "ad4m://fn/defaultFileDecode".
|
|
73
|
+
*/
|
|
74
|
+
export function fn(iri, args = []) {
|
|
75
|
+
return Object.freeze({ type: 'function', iri, args });
|
|
76
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHACL-AF Transform DSL — public API
|
|
3
|
+
*
|
|
4
|
+
* Re-exports builder functions and provides common built-in transforms.
|
|
5
|
+
*/
|
|
6
|
+
import { focus, literal, path, exists, ifExpr, concat, coalesce, fn } from './builders';
|
|
7
|
+
export type { NodeExpression } from './NodeExpression';
|
|
8
|
+
export { isNodeExpression } from './NodeExpression';
|
|
9
|
+
export { focus, literal, path, exists, ifExpr, concat, coalesce, fn, };
|
|
10
|
+
/**
|
|
11
|
+
* Built-in transform: FileData → data: URI
|
|
12
|
+
*
|
|
13
|
+
* Converts a file stored via FILE_STORAGE_LANGUAGE into a data: URL.
|
|
14
|
+
* If the object has a `data_base64` field, returns:
|
|
15
|
+
* `data:{file_type};base64,{data_base64}`
|
|
16
|
+
* Otherwise passes through the input unchanged.
|
|
17
|
+
*
|
|
18
|
+
* This is the Rust equivalent of the JS `defaultFileDecode` function.
|
|
19
|
+
*
|
|
20
|
+
* Usage:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* @Property({
|
|
23
|
+
* through: 'we://image',
|
|
24
|
+
* resolveLanguage: FILE_STORAGE_LANGUAGE,
|
|
25
|
+
* transform: fileToDataUri,
|
|
26
|
+
* })
|
|
27
|
+
* avatar?: string;
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare const fileToDataUri: Readonly<{
|
|
31
|
+
readonly else: import("./NodeExpression").NodeExpression;
|
|
32
|
+
readonly type: "if";
|
|
33
|
+
readonly cond: import("./NodeExpression").NodeExpression;
|
|
34
|
+
readonly then: import("./NodeExpression").NodeExpression;
|
|
35
|
+
}>;
|
|
36
|
+
export { SHACLShape, SHACLPropertyShape, AD4MAction, ConformanceCondition, } from './SHACLShape';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHACL-AF Transform DSL — public API
|
|
3
|
+
*
|
|
4
|
+
* Re-exports builder functions and provides common built-in transforms.
|
|
5
|
+
*/
|
|
6
|
+
import { focus, literal, path, exists, ifExpr, concat, coalesce, fn, } from './builders';
|
|
7
|
+
export { isNodeExpression } from './NodeExpression';
|
|
8
|
+
export { focus, literal, path, exists, ifExpr, concat, coalesce, fn, };
|
|
9
|
+
/**
|
|
10
|
+
* Built-in transform: FileData → data: URI
|
|
11
|
+
*
|
|
12
|
+
* Converts a file stored via FILE_STORAGE_LANGUAGE into a data: URL.
|
|
13
|
+
* If the object has a `data_base64` field, returns:
|
|
14
|
+
* `data:{file_type};base64,{data_base64}`
|
|
15
|
+
* Otherwise passes through the input unchanged.
|
|
16
|
+
*
|
|
17
|
+
* This is the Rust equivalent of the JS `defaultFileDecode` function.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* ```typescript
|
|
21
|
+
* @Property({
|
|
22
|
+
* through: 'we://image',
|
|
23
|
+
* resolveLanguage: FILE_STORAGE_LANGUAGE,
|
|
24
|
+
* transform: fileToDataUri,
|
|
25
|
+
* })
|
|
26
|
+
* avatar?: string;
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export const fileToDataUri = ifExpr(exists(path('file_storage://data_base64')))
|
|
30
|
+
.then(concat(literal('data:'), coalesce(path('file_storage://file_type'), literal('image/png')), literal(';base64,'), path('file_storage://data_base64')))
|
|
31
|
+
.else(focus());
|
|
32
|
+
// Re-export SHACL types for convenience
|
|
33
|
+
export { SHACLShape, } from './SHACLShape';
|
package/package.json
CHANGED
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"ws": "8.13.0"
|
|
49
49
|
},
|
|
50
50
|
"patchedDependencies": {},
|
|
51
|
-
"version": "0.13.0-postmessage-ws-proxy.
|
|
51
|
+
"version": "0.13.0-postmessage-ws-proxy.5",
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "patch-package && tsc && pnpm run bundle",
|
|
54
54
|
"bundle": "rollup -c rollup.config.js",
|