@coasys/ad4m 0.12.0-dev-whisper-lang → 0.12.0-rc1-dev.1
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 +1746 -322
- package/lib/index.js +1745 -323
- package/lib/src/ai/AIClient.d.ts +1 -1
- package/lib/src/ai/AIClient.js +4 -5
- package/lib/src/ai/AIResolver.d.ts +1 -1
- package/lib/src/ai/AIResolver.js +2 -3
- package/lib/src/index.d.ts +2 -0
- package/lib/src/index.js +2 -0
- package/lib/src/model/Ad4mModel.js +21 -7
- package/lib/src/model/decorators.js +196 -6
- package/lib/src/model/util.d.ts +1 -1
- package/lib/src/model/util.js +15 -12
- package/lib/src/perspectives/PerspectiveClient.d.ts +11 -1
- package/lib/src/perspectives/PerspectiveClient.js +15 -4
- package/lib/src/perspectives/PerspectiveProxy.d.ts +124 -8
- package/lib/src/perspectives/PerspectiveProxy.js +573 -274
- package/lib/src/perspectives/PerspectiveResolver.d.ts +1 -1
- package/lib/src/perspectives/PerspectiveResolver.js +5 -4
- package/lib/src/schema.gql +2 -2
- package/lib/src/shacl/SHACLFlow.d.ts +171 -0
- package/lib/src/shacl/SHACLFlow.js +370 -0
- package/lib/src/shacl/SHACLFlow.test.d.ts +1 -0
- package/lib/src/shacl/SHACLFlow.test.js +205 -0
- package/lib/src/shacl/SHACLShape.d.ts +108 -0
- package/lib/src/shacl/SHACLShape.js +566 -0
- package/lib/src/shacl/SHACLShape.test.d.ts +1 -0
- package/lib/src/shacl/SHACLShape.test.js +336 -0
- package/lib/src/typeDefs.js +2 -2
- package/package.json +1 -2
- package/src/ai/AIClient.ts +4 -6
- package/src/ai/AIResolver.ts +1 -2
- package/src/index.ts +2 -0
- package/src/model/Ad4mModel.ts +23 -7
- package/src/model/decorators.ts +221 -5
- package/src/model/util.ts +15 -12
- package/src/perspectives/PerspectiveClient.ts +15 -4
- package/src/perspectives/PerspectiveProxy.ts +588 -304
- package/src/perspectives/PerspectiveResolver.ts +1 -1
- package/src/shacl/SHACLFlow.test.ts +237 -0
- package/src/shacl/SHACLFlow.ts +503 -0
- package/src/shacl/SHACLShape.test.ts +400 -0
- package/src/shacl/SHACLShape.ts +755 -0
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
import { Link } from "../links/Links";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extract namespace from a URI
|
|
5
|
+
* Examples:
|
|
6
|
+
* - "recipe://name" -> "recipe://"
|
|
7
|
+
* - "https://example.com/vocab#term" -> "https://example.com/vocab#"
|
|
8
|
+
* - "https://example.com/vocab/term" -> "https://example.com/vocab/"
|
|
9
|
+
* - "recipe:Recipe" -> "recipe:"
|
|
10
|
+
*/
|
|
11
|
+
function extractNamespace(uri: string): string {
|
|
12
|
+
// Handle hash fragments first (highest priority)
|
|
13
|
+
const hashIndex = uri.lastIndexOf('#');
|
|
14
|
+
if (hashIndex !== -1) {
|
|
15
|
+
return uri.substring(0, hashIndex + 1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Handle protocol-style URIs with paths
|
|
19
|
+
const protocolMatch = uri.match(/^([a-zA-Z][a-zA-Z0-9+.-]*:\/\/)(.*)$/);
|
|
20
|
+
if (protocolMatch) {
|
|
21
|
+
const afterScheme = protocolMatch[2];
|
|
22
|
+
const lastSlash = afterScheme.lastIndexOf('/');
|
|
23
|
+
if (lastSlash !== -1) {
|
|
24
|
+
// Has path segments - namespace includes up to last slash
|
|
25
|
+
return protocolMatch[1] + afterScheme.substring(0, lastSlash + 1);
|
|
26
|
+
}
|
|
27
|
+
// Simple protocol URI without path (e.g., "recipe://name")
|
|
28
|
+
return protocolMatch[1];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Handle colon-separated (namespace:localName)
|
|
32
|
+
const colonMatch = uri.match(/^([a-zA-Z][a-zA-Z0-9+.-]*:)/);
|
|
33
|
+
if (colonMatch) {
|
|
34
|
+
return colonMatch[1];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Fallback: no clear namespace
|
|
38
|
+
return '';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Escape special characters for Turtle string literals
|
|
43
|
+
* Handles backslashes, quotes, newlines, and other control characters
|
|
44
|
+
*/
|
|
45
|
+
function escapeTurtleString(value: string): string {
|
|
46
|
+
return value
|
|
47
|
+
.replace(/\\/g, '\\\\') // Backslash must be first
|
|
48
|
+
.replace(/"/g, '\\"') // Double quotes
|
|
49
|
+
.replace(/\n/g, '\\n') // Newlines
|
|
50
|
+
.replace(/\r/g, '\\r') // Carriage returns
|
|
51
|
+
.replace(/\t/g, '\\t') // Tabs
|
|
52
|
+
.replace(/\b/g, '\\b') // Backspace
|
|
53
|
+
.replace(/\f/g, '\\f'); // Form feed
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Extract local name from a URI
|
|
58
|
+
* Examples:
|
|
59
|
+
* - "recipe://name" -> "name"
|
|
60
|
+
* - "https://example.com/vocab#term" -> "term"
|
|
61
|
+
* - "https://example.com/vocab/term" -> "term"
|
|
62
|
+
* - "recipe:Recipe" -> "Recipe"
|
|
63
|
+
*/
|
|
64
|
+
function extractLocalName(uri: string): string {
|
|
65
|
+
// Handle hash fragments
|
|
66
|
+
const hashIndex = uri.lastIndexOf('#');
|
|
67
|
+
if (hashIndex !== -1) {
|
|
68
|
+
return uri.substring(hashIndex + 1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Handle slash-based namespaces
|
|
72
|
+
const lastSlash = uri.lastIndexOf('/');
|
|
73
|
+
if (lastSlash !== -1 && lastSlash < uri.length - 1) {
|
|
74
|
+
return uri.substring(lastSlash + 1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Handle colon-separated
|
|
78
|
+
const colonMatch = uri.match(/^[a-zA-Z][a-zA-Z0-9+.-]*:(.+)$/);
|
|
79
|
+
if (colonMatch) {
|
|
80
|
+
return colonMatch[1];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Fallback: entire URI
|
|
84
|
+
return uri;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* AD4M Action - represents a link operation
|
|
89
|
+
*/
|
|
90
|
+
export interface AD4MAction {
|
|
91
|
+
action: string;
|
|
92
|
+
source: string;
|
|
93
|
+
predicate: string;
|
|
94
|
+
target: string;
|
|
95
|
+
local?: boolean;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* SHACL Property Shape
|
|
100
|
+
* Represents constraints on a single property path
|
|
101
|
+
*/
|
|
102
|
+
export interface SHACLPropertyShape {
|
|
103
|
+
/** Property name (e.g., "name", "ingredients") - used for generating named URIs */
|
|
104
|
+
name?: string;
|
|
105
|
+
|
|
106
|
+
/** The property path (predicate URI) */
|
|
107
|
+
path: string;
|
|
108
|
+
|
|
109
|
+
/** Expected datatype (e.g., xsd:string, xsd:integer) */
|
|
110
|
+
datatype?: string;
|
|
111
|
+
|
|
112
|
+
/** Node kind constraint (IRI, Literal, BlankNode) */
|
|
113
|
+
nodeKind?: 'IRI' | 'Literal' | 'BlankNode';
|
|
114
|
+
|
|
115
|
+
/** Minimum cardinality (required if >= 1) */
|
|
116
|
+
minCount?: number;
|
|
117
|
+
|
|
118
|
+
/** Maximum cardinality (single-valued if 1, omit for collections) */
|
|
119
|
+
maxCount?: number;
|
|
120
|
+
|
|
121
|
+
/** Regex pattern for string validation */
|
|
122
|
+
pattern?: string;
|
|
123
|
+
|
|
124
|
+
/** Minimum value (inclusive) for numeric properties */
|
|
125
|
+
minInclusive?: number;
|
|
126
|
+
|
|
127
|
+
/** Maximum value (inclusive) for numeric properties */
|
|
128
|
+
maxInclusive?: number;
|
|
129
|
+
|
|
130
|
+
/** Fixed value constraint (for Flag properties) */
|
|
131
|
+
hasValue?: string;
|
|
132
|
+
|
|
133
|
+
/** AD4M-specific: Local-only property */
|
|
134
|
+
local?: boolean;
|
|
135
|
+
|
|
136
|
+
/** AD4M-specific: Writable property */
|
|
137
|
+
writable?: boolean;
|
|
138
|
+
|
|
139
|
+
/** AD4M-specific: Language to resolve property values through */
|
|
140
|
+
resolveLanguage?: string;
|
|
141
|
+
|
|
142
|
+
/** AD4M-specific: Setter action for this property */
|
|
143
|
+
setter?: AD4MAction[];
|
|
144
|
+
|
|
145
|
+
/** AD4M-specific: Adder action for collection properties */
|
|
146
|
+
adder?: AD4MAction[];
|
|
147
|
+
|
|
148
|
+
/** AD4M-specific: Remover action for collection properties */
|
|
149
|
+
remover?: AD4MAction[];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* SHACL Node Shape
|
|
154
|
+
* Defines constraints for instances of a class
|
|
155
|
+
*/
|
|
156
|
+
export class SHACLShape {
|
|
157
|
+
/** URI of this shape (e.g., recipe:RecipeShape) */
|
|
158
|
+
nodeShapeUri: string;
|
|
159
|
+
|
|
160
|
+
/** Target class this shape applies to (e.g., recipe:Recipe) */
|
|
161
|
+
targetClass?: string;
|
|
162
|
+
|
|
163
|
+
/** Property constraints */
|
|
164
|
+
properties: SHACLPropertyShape[];
|
|
165
|
+
|
|
166
|
+
/** AD4M-specific: Constructor actions for creating instances */
|
|
167
|
+
constructor_actions?: AD4MAction[];
|
|
168
|
+
|
|
169
|
+
/** AD4M-specific: Destructor actions for removing instances */
|
|
170
|
+
destructor_actions?: AD4MAction[];
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Create a new SHACL Shape
|
|
174
|
+
* @param targetClassOrShapeUri - If one argument: the target class (shape URI auto-derived as {class}Shape)
|
|
175
|
+
* If two arguments: first is shape URI, second is target class
|
|
176
|
+
* @param targetClass - Optional target class when first arg is shape URI
|
|
177
|
+
*/
|
|
178
|
+
constructor(targetClassOrShapeUri: string, targetClass?: string) {
|
|
179
|
+
if (targetClass !== undefined) {
|
|
180
|
+
// Two arguments: explicit shape URI and target class
|
|
181
|
+
this.nodeShapeUri = targetClassOrShapeUri;
|
|
182
|
+
this.targetClass = targetClass;
|
|
183
|
+
} else {
|
|
184
|
+
// One argument: derive shape URI from target class
|
|
185
|
+
this.targetClass = targetClassOrShapeUri;
|
|
186
|
+
// Derive shape URI by appending "Shape" to the local name
|
|
187
|
+
const namespace = extractNamespace(targetClassOrShapeUri);
|
|
188
|
+
const localName = extractLocalName(targetClassOrShapeUri);
|
|
189
|
+
this.nodeShapeUri = `${namespace}${localName}Shape`;
|
|
190
|
+
}
|
|
191
|
+
this.properties = [];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Add a property constraint to this shape
|
|
196
|
+
*/
|
|
197
|
+
addProperty(prop: SHACLPropertyShape): void {
|
|
198
|
+
this.properties.push(prop);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Set constructor actions for this shape
|
|
203
|
+
*/
|
|
204
|
+
setConstructorActions(actions: AD4MAction[]): void {
|
|
205
|
+
this.constructor_actions = actions;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Set destructor actions for this shape
|
|
210
|
+
*/
|
|
211
|
+
setDestructorActions(actions: AD4MAction[]): void {
|
|
212
|
+
this.destructor_actions = actions;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Serialize shape to Turtle (RDF) format
|
|
217
|
+
*/
|
|
218
|
+
toTurtle(): string {
|
|
219
|
+
let turtle = `@prefix sh: <http://www.w3.org/ns/shacl#> .\n`;
|
|
220
|
+
turtle += `@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n`;
|
|
221
|
+
turtle += `@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n`;
|
|
222
|
+
turtle += `@prefix ad4m: <ad4m://> .\n\n`;
|
|
223
|
+
|
|
224
|
+
turtle += `<${this.nodeShapeUri}>\n`;
|
|
225
|
+
turtle += ` a sh:NodeShape ;\n`;
|
|
226
|
+
|
|
227
|
+
if (this.targetClass) {
|
|
228
|
+
turtle += ` sh:targetClass <${this.targetClass}> ;\n`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Add property shapes
|
|
232
|
+
for (let i = 0; i < this.properties.length; i++) {
|
|
233
|
+
const prop = this.properties[i];
|
|
234
|
+
const isLast = i === this.properties.length - 1;
|
|
235
|
+
|
|
236
|
+
turtle += ` sh:property [\n`;
|
|
237
|
+
turtle += ` sh:path <${prop.path}> ;\n`;
|
|
238
|
+
|
|
239
|
+
if (prop.datatype) {
|
|
240
|
+
turtle += ` sh:datatype <${prop.datatype}> ;\n`;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (prop.nodeKind) {
|
|
244
|
+
turtle += ` sh:nodeKind sh:${prop.nodeKind} ;\n`;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (prop.minCount !== undefined) {
|
|
248
|
+
turtle += ` sh:minCount ${prop.minCount} ;\n`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (prop.maxCount !== undefined) {
|
|
252
|
+
turtle += ` sh:maxCount ${prop.maxCount} ;\n`;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (prop.pattern) {
|
|
256
|
+
turtle += ` sh:pattern "${escapeTurtleString(prop.pattern)}" ;\n`;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (prop.minInclusive !== undefined) {
|
|
260
|
+
turtle += ` sh:minInclusive ${prop.minInclusive} ;\n`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (prop.maxInclusive !== undefined) {
|
|
264
|
+
turtle += ` sh:maxInclusive ${prop.maxInclusive} ;\n`;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (prop.hasValue) {
|
|
268
|
+
turtle += ` sh:hasValue "${escapeTurtleString(prop.hasValue)}" ;\n`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// AD4M-specific metadata
|
|
272
|
+
if (prop.local !== undefined) {
|
|
273
|
+
turtle += ` ad4m:local ${prop.local} ;\n`;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (prop.writable !== undefined) {
|
|
277
|
+
turtle += ` ad4m:writable ${prop.writable} ;\n`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Remove trailing semicolon and close bracket
|
|
281
|
+
turtle = turtle.slice(0, -2) + '\n';
|
|
282
|
+
turtle += isLast ? ` ] .\n` : ` ] ;\n`;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return turtle;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Serialize shape to AD4M Links (RDF triples)
|
|
290
|
+
* Stores the shape as a graph of links in a Perspective
|
|
291
|
+
*/
|
|
292
|
+
toLinks(): Link[] {
|
|
293
|
+
const links: Link[] = [];
|
|
294
|
+
|
|
295
|
+
// Shape type declaration
|
|
296
|
+
links.push({
|
|
297
|
+
source: this.nodeShapeUri,
|
|
298
|
+
predicate: "rdf://type",
|
|
299
|
+
target: "sh://NodeShape"
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// Target class
|
|
303
|
+
if (this.targetClass) {
|
|
304
|
+
links.push({
|
|
305
|
+
source: this.nodeShapeUri,
|
|
306
|
+
predicate: "sh://targetClass",
|
|
307
|
+
target: this.targetClass
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Constructor actions
|
|
312
|
+
if (this.constructor_actions && this.constructor_actions.length > 0) {
|
|
313
|
+
links.push({
|
|
314
|
+
source: this.nodeShapeUri,
|
|
315
|
+
predicate: "ad4m://constructor",
|
|
316
|
+
target: `literal://string:${JSON.stringify(this.constructor_actions)}`
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Destructor actions
|
|
321
|
+
if (this.destructor_actions && this.destructor_actions.length > 0) {
|
|
322
|
+
links.push({
|
|
323
|
+
source: this.nodeShapeUri,
|
|
324
|
+
predicate: "ad4m://destructor",
|
|
325
|
+
target: `literal://string:${JSON.stringify(this.destructor_actions)}`
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Property shapes (each gets a named URI: {namespace}/{ClassName}.{propertyName})
|
|
330
|
+
for (let i = 0; i < this.properties.length; i++) {
|
|
331
|
+
const prop = this.properties[i];
|
|
332
|
+
|
|
333
|
+
// Generate named property shape URI
|
|
334
|
+
let propShapeId: string;
|
|
335
|
+
if (prop.name && this.targetClass) {
|
|
336
|
+
// Extract namespace from targetClass
|
|
337
|
+
const namespace = extractNamespace(this.targetClass);
|
|
338
|
+
const className = extractLocalName(this.targetClass);
|
|
339
|
+
// Use format: {namespace}{ClassName}.{propertyName}
|
|
340
|
+
propShapeId = `${namespace}${className}.${prop.name}`;
|
|
341
|
+
} else {
|
|
342
|
+
// Fallback to blank node if name is missing
|
|
343
|
+
propShapeId = `_:propShape${i}`;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Link shape to property shape
|
|
347
|
+
links.push({
|
|
348
|
+
source: this.nodeShapeUri,
|
|
349
|
+
predicate: "sh://property",
|
|
350
|
+
target: propShapeId
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// Property path
|
|
354
|
+
links.push({
|
|
355
|
+
source: propShapeId,
|
|
356
|
+
predicate: "sh://path",
|
|
357
|
+
target: prop.path
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
// Constraints
|
|
361
|
+
if (prop.datatype) {
|
|
362
|
+
links.push({
|
|
363
|
+
source: propShapeId,
|
|
364
|
+
predicate: "sh://datatype",
|
|
365
|
+
target: prop.datatype
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (prop.nodeKind) {
|
|
370
|
+
links.push({
|
|
371
|
+
source: propShapeId,
|
|
372
|
+
predicate: "sh://nodeKind",
|
|
373
|
+
target: `sh://${prop.nodeKind}`
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (prop.minCount !== undefined) {
|
|
378
|
+
links.push({
|
|
379
|
+
source: propShapeId,
|
|
380
|
+
predicate: "sh://minCount",
|
|
381
|
+
target: `literal://${prop.minCount}^^xsd:integer`
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (prop.maxCount !== undefined) {
|
|
386
|
+
links.push({
|
|
387
|
+
source: propShapeId,
|
|
388
|
+
predicate: "sh://maxCount",
|
|
389
|
+
target: `literal://${prop.maxCount}^^xsd:integer`
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (prop.pattern) {
|
|
394
|
+
links.push({
|
|
395
|
+
source: propShapeId,
|
|
396
|
+
predicate: "sh://pattern",
|
|
397
|
+
target: `literal://${prop.pattern}`
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (prop.minInclusive !== undefined) {
|
|
402
|
+
links.push({
|
|
403
|
+
source: propShapeId,
|
|
404
|
+
predicate: "sh://minInclusive",
|
|
405
|
+
target: `literal://${prop.minInclusive}`
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (prop.maxInclusive !== undefined) {
|
|
410
|
+
links.push({
|
|
411
|
+
source: propShapeId,
|
|
412
|
+
predicate: "sh://maxInclusive",
|
|
413
|
+
target: `literal://${prop.maxInclusive}`
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (prop.hasValue) {
|
|
418
|
+
links.push({
|
|
419
|
+
source: propShapeId,
|
|
420
|
+
predicate: "sh://hasValue",
|
|
421
|
+
target: `literal://${prop.hasValue}`
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// AD4M-specific metadata
|
|
426
|
+
if (prop.local !== undefined) {
|
|
427
|
+
links.push({
|
|
428
|
+
source: propShapeId,
|
|
429
|
+
predicate: "ad4m://local",
|
|
430
|
+
target: `literal://${prop.local}`
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (prop.writable !== undefined) {
|
|
435
|
+
links.push({
|
|
436
|
+
source: propShapeId,
|
|
437
|
+
predicate: "ad4m://writable",
|
|
438
|
+
target: `literal://${prop.writable}`
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (prop.resolveLanguage) {
|
|
443
|
+
links.push({
|
|
444
|
+
source: propShapeId,
|
|
445
|
+
predicate: "ad4m://resolveLanguage",
|
|
446
|
+
target: `literal://string:${prop.resolveLanguage}`
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// AD4M-specific actions
|
|
451
|
+
if (prop.setter && prop.setter.length > 0) {
|
|
452
|
+
links.push({
|
|
453
|
+
source: propShapeId,
|
|
454
|
+
predicate: "ad4m://setter",
|
|
455
|
+
target: `literal://string:${JSON.stringify(prop.setter)}`
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (prop.adder && prop.adder.length > 0) {
|
|
460
|
+
links.push({
|
|
461
|
+
source: propShapeId,
|
|
462
|
+
predicate: "ad4m://adder",
|
|
463
|
+
target: `literal://string:${JSON.stringify(prop.adder)}`
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (prop.remover && prop.remover.length > 0) {
|
|
468
|
+
links.push({
|
|
469
|
+
source: propShapeId,
|
|
470
|
+
predicate: "ad4m://remover",
|
|
471
|
+
target: `literal://string:${JSON.stringify(prop.remover)}`
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return links;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Reconstruct shape from AD4M Links
|
|
481
|
+
*/
|
|
482
|
+
static fromLinks(links: Link[], shapeUri: string): SHACLShape {
|
|
483
|
+
// Find target class
|
|
484
|
+
const targetClassLink = links.find(l =>
|
|
485
|
+
l.source === shapeUri && l.predicate === "sh://targetClass"
|
|
486
|
+
);
|
|
487
|
+
|
|
488
|
+
const shape = new SHACLShape(shapeUri, targetClassLink?.target);
|
|
489
|
+
|
|
490
|
+
// Find constructor actions
|
|
491
|
+
const constructorLink = links.find(l =>
|
|
492
|
+
l.source === shapeUri && l.predicate === "ad4m://constructor"
|
|
493
|
+
);
|
|
494
|
+
if (constructorLink) {
|
|
495
|
+
try {
|
|
496
|
+
const jsonStr = constructorLink.target.replace('literal://string:', '');
|
|
497
|
+
shape.constructor_actions = JSON.parse(jsonStr);
|
|
498
|
+
} catch (e) {
|
|
499
|
+
// Ignore parse errors
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Find destructor actions
|
|
504
|
+
const destructorLink = links.find(l =>
|
|
505
|
+
l.source === shapeUri && l.predicate === "ad4m://destructor"
|
|
506
|
+
);
|
|
507
|
+
if (destructorLink) {
|
|
508
|
+
try {
|
|
509
|
+
const jsonStr = destructorLink.target.replace('literal://string:', '');
|
|
510
|
+
shape.destructor_actions = JSON.parse(jsonStr);
|
|
511
|
+
} catch (e) {
|
|
512
|
+
// Ignore parse errors
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Find all property shapes
|
|
517
|
+
const propShapeLinks = links.filter(l =>
|
|
518
|
+
l.source === shapeUri && l.predicate === "sh://property"
|
|
519
|
+
);
|
|
520
|
+
|
|
521
|
+
for (const propLink of propShapeLinks) {
|
|
522
|
+
const propShapeId = propLink.target;
|
|
523
|
+
|
|
524
|
+
// Reconstruct property from its links
|
|
525
|
+
const pathLink = links.find(l =>
|
|
526
|
+
l.source === propShapeId && l.predicate === "sh://path"
|
|
527
|
+
);
|
|
528
|
+
|
|
529
|
+
if (!pathLink) continue;
|
|
530
|
+
|
|
531
|
+
// Extract property name from propShapeId if it's a named URI
|
|
532
|
+
// Format: {namespace}{ClassName}.{propertyName}
|
|
533
|
+
let propertyName: string | undefined;
|
|
534
|
+
if (!propShapeId.startsWith('_:')) {
|
|
535
|
+
const lastDotIndex = propShapeId.lastIndexOf('.');
|
|
536
|
+
if (lastDotIndex !== -1) {
|
|
537
|
+
propertyName = propShapeId.substring(lastDotIndex + 1);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const prop: SHACLPropertyShape = {
|
|
542
|
+
name: propertyName,
|
|
543
|
+
path: pathLink.target
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// Extract constraints
|
|
547
|
+
const datatypeLink = links.find(l =>
|
|
548
|
+
l.source === propShapeId && l.predicate === "sh://datatype"
|
|
549
|
+
);
|
|
550
|
+
if (datatypeLink) prop.datatype = datatypeLink.target;
|
|
551
|
+
|
|
552
|
+
const nodeKindLink = links.find(l =>
|
|
553
|
+
l.source === propShapeId && l.predicate === "sh://nodeKind"
|
|
554
|
+
);
|
|
555
|
+
if (nodeKindLink) {
|
|
556
|
+
prop.nodeKind = nodeKindLink.target.replace('sh://', '') as any;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
const minCountLink = links.find(l =>
|
|
560
|
+
l.source === propShapeId && l.predicate === "sh://minCount"
|
|
561
|
+
);
|
|
562
|
+
if (minCountLink) {
|
|
563
|
+
// Handle both formats: literal://5^^xsd:integer and literal://number:5
|
|
564
|
+
let val = minCountLink.target.replace('literal://', '').replace(/\^\^.*$/, '');
|
|
565
|
+
if (val.startsWith('number:')) val = val.substring(7);
|
|
566
|
+
prop.minCount = parseInt(val);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
const maxCountLink = links.find(l =>
|
|
570
|
+
l.source === propShapeId && l.predicate === "sh://maxCount"
|
|
571
|
+
);
|
|
572
|
+
if (maxCountLink) {
|
|
573
|
+
// Handle both formats: literal://5^^xsd:integer and literal://number:5
|
|
574
|
+
let val = maxCountLink.target.replace('literal://', '').replace(/\^\^.*$/, '');
|
|
575
|
+
if (val.startsWith('number:')) val = val.substring(7);
|
|
576
|
+
prop.maxCount = parseInt(val);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const patternLink = links.find(l =>
|
|
580
|
+
l.source === propShapeId && l.predicate === "sh://pattern"
|
|
581
|
+
);
|
|
582
|
+
if (patternLink) {
|
|
583
|
+
prop.pattern = patternLink.target.replace('literal://', '');
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const minInclusiveLink = links.find(l =>
|
|
587
|
+
l.source === propShapeId && l.predicate === "sh://minInclusive"
|
|
588
|
+
);
|
|
589
|
+
if (minInclusiveLink) {
|
|
590
|
+
// Handle both formats: literal://5 and literal://number:5
|
|
591
|
+
let val = minInclusiveLink.target.replace('literal://', '');
|
|
592
|
+
if (val.startsWith('number:')) val = val.substring(7);
|
|
593
|
+
prop.minInclusive = parseFloat(val);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const maxInclusiveLink = links.find(l =>
|
|
597
|
+
l.source === propShapeId && l.predicate === "sh://maxInclusive"
|
|
598
|
+
);
|
|
599
|
+
if (maxInclusiveLink) {
|
|
600
|
+
// Handle both formats: literal://5 and literal://number:5
|
|
601
|
+
let val = maxInclusiveLink.target.replace('literal://', '');
|
|
602
|
+
if (val.startsWith('number:')) val = val.substring(7);
|
|
603
|
+
prop.maxInclusive = parseFloat(val);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const hasValueLink = links.find(l =>
|
|
607
|
+
l.source === propShapeId && l.predicate === "sh://hasValue"
|
|
608
|
+
);
|
|
609
|
+
if (hasValueLink) {
|
|
610
|
+
prop.hasValue = hasValueLink.target.replace('literal://', '');
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// AD4M-specific
|
|
614
|
+
const localLink = links.find(l =>
|
|
615
|
+
l.source === propShapeId && l.predicate === "ad4m://local"
|
|
616
|
+
);
|
|
617
|
+
if (localLink) {
|
|
618
|
+
// Handle both formats: literal://true and literal://boolean:true
|
|
619
|
+
let val = localLink.target.replace('literal://', '');
|
|
620
|
+
if (val.startsWith('boolean:')) val = val.substring(8);
|
|
621
|
+
prop.local = val === 'true';
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const writableLink = links.find(l =>
|
|
625
|
+
l.source === propShapeId && l.predicate === "ad4m://writable"
|
|
626
|
+
);
|
|
627
|
+
if (writableLink) {
|
|
628
|
+
// Handle both formats: literal://true and literal://boolean:true
|
|
629
|
+
let val = writableLink.target.replace('literal://', '');
|
|
630
|
+
if (val.startsWith('boolean:')) val = val.substring(8);
|
|
631
|
+
prop.writable = val === 'true';
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
const resolveLangLink = links.find(l =>
|
|
635
|
+
l.source === propShapeId && l.predicate === "ad4m://resolveLanguage"
|
|
636
|
+
);
|
|
637
|
+
if (resolveLangLink) {
|
|
638
|
+
prop.resolveLanguage = resolveLangLink.target.replace('literal://string:', '');
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// Parse action arrays
|
|
642
|
+
const setterLink = links.find(l =>
|
|
643
|
+
l.source === propShapeId && l.predicate === "ad4m://setter"
|
|
644
|
+
);
|
|
645
|
+
if (setterLink) {
|
|
646
|
+
try {
|
|
647
|
+
const jsonStr = setterLink.target.replace('literal://string:', '');
|
|
648
|
+
prop.setter = JSON.parse(jsonStr);
|
|
649
|
+
} catch (e) {
|
|
650
|
+
// Ignore parse errors
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const adderLink = links.find(l =>
|
|
655
|
+
l.source === propShapeId && l.predicate === "ad4m://adder"
|
|
656
|
+
);
|
|
657
|
+
if (adderLink) {
|
|
658
|
+
try {
|
|
659
|
+
const jsonStr = adderLink.target.replace('literal://string:', '');
|
|
660
|
+
prop.adder = JSON.parse(jsonStr);
|
|
661
|
+
} catch (e) {
|
|
662
|
+
// Ignore parse errors
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const removerLink = links.find(l =>
|
|
667
|
+
l.source === propShapeId && l.predicate === "ad4m://remover"
|
|
668
|
+
);
|
|
669
|
+
if (removerLink) {
|
|
670
|
+
try {
|
|
671
|
+
const jsonStr = removerLink.target.replace('literal://string:', '');
|
|
672
|
+
prop.remover = JSON.parse(jsonStr);
|
|
673
|
+
} catch (e) {
|
|
674
|
+
// Ignore parse errors
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
shape.addProperty(prop);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
return shape;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Convert the shape to a JSON-serializable object.
|
|
686
|
+
* Useful for passing to addSdna() as shaclJson parameter.
|
|
687
|
+
*
|
|
688
|
+
* @returns JSON-serializable object representing the shape
|
|
689
|
+
*/
|
|
690
|
+
toJSON(): object {
|
|
691
|
+
return {
|
|
692
|
+
node_shape_uri: this.nodeShapeUri,
|
|
693
|
+
target_class: this.targetClass,
|
|
694
|
+
properties: this.properties.map(p => ({
|
|
695
|
+
path: p.path,
|
|
696
|
+
name: p.name,
|
|
697
|
+
datatype: p.datatype,
|
|
698
|
+
node_kind: p.nodeKind,
|
|
699
|
+
min_count: p.minCount,
|
|
700
|
+
max_count: p.maxCount,
|
|
701
|
+
min_inclusive: p.minInclusive,
|
|
702
|
+
max_inclusive: p.maxInclusive,
|
|
703
|
+
pattern: p.pattern,
|
|
704
|
+
has_value: p.hasValue,
|
|
705
|
+
local: p.local,
|
|
706
|
+
writable: p.writable,
|
|
707
|
+
resolve_language: p.resolveLanguage,
|
|
708
|
+
setter: p.setter,
|
|
709
|
+
adder: p.adder,
|
|
710
|
+
remover: p.remover,
|
|
711
|
+
})),
|
|
712
|
+
constructor_actions: this.constructor_actions,
|
|
713
|
+
destructor_actions: this.destructor_actions,
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Create a shape from a JSON object (inverse of toJSON)
|
|
719
|
+
*/
|
|
720
|
+
static fromJSON(json: any): SHACLShape {
|
|
721
|
+
const shape = json.node_shape_uri
|
|
722
|
+
? new SHACLShape(json.node_shape_uri, json.target_class)
|
|
723
|
+
: new SHACLShape(json.target_class);
|
|
724
|
+
|
|
725
|
+
for (const p of json.properties || []) {
|
|
726
|
+
shape.addProperty({
|
|
727
|
+
path: p.path,
|
|
728
|
+
name: p.name,
|
|
729
|
+
datatype: p.datatype,
|
|
730
|
+
nodeKind: p.node_kind,
|
|
731
|
+
minCount: p.min_count,
|
|
732
|
+
maxCount: p.max_count,
|
|
733
|
+
minInclusive: p.min_inclusive,
|
|
734
|
+
maxInclusive: p.max_inclusive,
|
|
735
|
+
pattern: p.pattern,
|
|
736
|
+
hasValue: p.has_value,
|
|
737
|
+
local: p.local,
|
|
738
|
+
writable: p.writable,
|
|
739
|
+
resolveLanguage: p.resolve_language,
|
|
740
|
+
setter: p.setter,
|
|
741
|
+
adder: p.adder,
|
|
742
|
+
remover: p.remover,
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
if (json.constructor_actions) {
|
|
747
|
+
shape.constructor_actions = json.constructor_actions;
|
|
748
|
+
}
|
|
749
|
+
if (json.destructor_actions) {
|
|
750
|
+
shape.destructor_actions = json.destructor_actions;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
return shape;
|
|
754
|
+
}
|
|
755
|
+
}
|