@coasys/ad4m 0.12.0-rc1-dev.2 → 0.12.0-rc1-dev.4

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 (153) hide show
  1. package/jest.config.js +10 -3
  2. package/lib/index.cjs +6893 -5884
  3. package/lib/index.js +6889 -5872
  4. package/lib/src/Ad4mClient.d.ts +9 -16
  5. package/lib/src/Ad4mClient.js +46 -25
  6. package/lib/src/Literal.d.ts +1 -2
  7. package/lib/src/Literal.js +38 -35
  8. package/lib/src/SmartLiteral.d.ts +1 -2
  9. package/lib/src/SmartLiteral.js +30 -15
  10. package/lib/src/agent/AgentClient.d.ts +1 -4
  11. package/lib/src/agent/AgentClient.js +79 -99
  12. package/lib/src/ai/AIClient.d.ts +1 -2
  13. package/lib/src/ai/AIClient.js +224 -220
  14. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  15. package/lib/src/expression/ExpressionClient.js +73 -86
  16. package/lib/src/index.d.ts +1 -1
  17. package/lib/src/index.js +1 -1
  18. package/lib/src/language/LanguageClient.d.ts +1 -1
  19. package/lib/src/language/LanguageClient.js +52 -52
  20. package/lib/src/model/Ad4mModel.d.ts +1351 -144
  21. package/lib/src/model/Ad4mModel.js +3760 -228
  22. package/lib/src/model/Ad4mModel.test.js +522 -979
  23. package/lib/src/model/Subject.d.ts +3 -3
  24. package/lib/src/model/Subject.js +25 -25
  25. package/lib/src/model/decorators.d.ts +444 -100
  26. package/lib/src/model/decorators.js +686 -321
  27. package/lib/src/model/util.d.ts +6 -6
  28. package/lib/src/model/util.js +24 -24
  29. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +1 -5
  30. package/lib/src/neighbourhood/NeighbourhoodClient.js +179 -242
  31. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +1 -2
  32. package/lib/src/neighbourhood/NeighbourhoodProxy.js +28 -13
  33. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +5 -1
  34. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  35. package/lib/src/perspectives/PerspectiveClient.js +240 -316
  36. package/lib/src/perspectives/PerspectiveProxy.d.ts +99 -61
  37. package/lib/src/perspectives/PerspectiveProxy.js +485 -832
  38. package/lib/src/runtime/RuntimeClient.d.ts +1 -5
  39. package/lib/src/runtime/RuntimeClient.js +238 -412
  40. package/lib/src/shacl/SHACLFlow.test.js +105 -164
  41. package/lib/src/shacl/SHACLShape.d.ts +12 -17
  42. package/lib/src/shacl/SHACLShape.js +115 -128
  43. package/lib/src/shacl/SHACLShape.test.js +176 -244
  44. package/lib/src/shims/reflect-metadata.d.ts +0 -0
  45. package/lib/src/shims/type-graphql.d.ts +32 -0
  46. package/lib/src/src/Ad4mClient.d.ts +28 -0
  47. package/lib/src/src/Address.d.ts +1 -0
  48. package/lib/src/src/DID.d.ts +1 -0
  49. package/lib/src/src/Exception.d.ts +7 -0
  50. package/lib/src/src/Literal.d.ts +7 -0
  51. package/lib/src/src/PubSub.d.ts +17 -0
  52. package/lib/src/src/SmartLiteral.d.ts +12 -0
  53. package/lib/src/src/agent/Agent.d.ts +110 -0
  54. package/lib/src/src/agent/AgentClient.d.ts +62 -0
  55. package/lib/src/src/agent/AgentResolver.d.ts +30 -0
  56. package/lib/src/src/agent/AgentStatus.d.ts +8 -0
  57. package/lib/src/src/ai/AIClient.d.ts +32 -0
  58. package/lib/src/src/ai/AIResolver.d.ts +75 -0
  59. package/lib/src/src/ai/Tasks.d.ts +37 -0
  60. package/lib/src/src/buildSchema.d.ts +1 -0
  61. package/lib/src/src/expression/Expression.d.ts +28 -0
  62. package/lib/src/src/expression/ExpressionClient.d.ts +13 -0
  63. package/lib/src/src/expression/ExpressionRef.d.ts +9 -0
  64. package/lib/src/src/expression/ExpressionResolver.d.ts +10 -0
  65. package/lib/src/src/index.d.ts +35 -0
  66. package/lib/src/src/jestSetup.d.ts +1 -0
  67. package/lib/src/src/language/Icon.d.ts +4 -0
  68. package/lib/src/src/language/Language.d.ts +206 -0
  69. package/lib/src/src/language/LanguageClient.d.ts +17 -0
  70. package/lib/src/src/language/LanguageContext.d.ts +37 -0
  71. package/lib/src/src/language/LanguageHandle.d.ts +9 -0
  72. package/lib/src/src/language/LanguageMeta.d.ts +35 -0
  73. package/lib/src/src/language/LanguageRef.d.ts +6 -0
  74. package/lib/src/src/language/LanguageResolver.d.ts +19 -0
  75. package/lib/src/src/links/Links.d.ts +39 -0
  76. package/lib/src/src/links/Links.test.d.ts +1 -0
  77. package/lib/src/src/model/Ad4mModel.d.ts +1438 -0
  78. package/lib/src/src/model/Ad4mModel.test.d.ts +1 -0
  79. package/lib/src/src/model/Subject.d.ts +25 -0
  80. package/lib/src/src/model/decorators.d.ts +545 -0
  81. package/lib/src/src/model/util.d.ts +12 -0
  82. package/lib/src/src/neighbourhood/Neighbourhood.d.ts +10 -0
  83. package/lib/src/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
  84. package/lib/src/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
  85. package/lib/src/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
  86. package/lib/src/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
  87. package/lib/src/src/perspectives/LinkQuery.d.ts +11 -0
  88. package/lib/src/src/perspectives/Perspective.d.ts +38 -0
  89. package/lib/src/src/perspectives/Perspective.test.d.ts +1 -0
  90. package/lib/src/src/perspectives/PerspectiveClient.d.ts +97 -0
  91. package/lib/src/src/perspectives/PerspectiveDiff.d.ts +9 -0
  92. package/lib/src/src/perspectives/PerspectiveHandle.d.ts +18 -0
  93. package/lib/src/src/perspectives/PerspectiveProxy.d.ts +865 -0
  94. package/lib/src/src/perspectives/PerspectiveResolver.d.ts +57 -0
  95. package/lib/src/src/runtime/RuntimeClient.d.ts +56 -0
  96. package/lib/src/src/runtime/RuntimeResolver.d.ts +132 -0
  97. package/lib/src/src/shacl/SHACLFlow.d.ts +171 -0
  98. package/lib/src/src/shacl/SHACLFlow.test.d.ts +1 -0
  99. package/lib/src/src/shacl/SHACLShape.d.ts +117 -0
  100. package/lib/src/src/shacl/SHACLShape.test.d.ts +1 -0
  101. package/lib/src/src/typeDefs.d.ts +1 -0
  102. package/lib/src/src/unwrapApolloResult.d.ts +2 -0
  103. package/lib/src/src/utils.d.ts +25 -0
  104. package/lib/src/utils.d.ts +0 -8
  105. package/lib/src/utils.js +7 -21
  106. package/package.json +15 -14
  107. package/src/Ad4mClient.test.ts +1315 -1564
  108. package/src/Ad4mClient.ts +54 -55
  109. package/src/Literal.test.ts +26 -45
  110. package/src/Literal.ts +66 -82
  111. package/src/SmartLiteral.ts +58 -74
  112. package/src/agent/AgentClient.ts +99 -144
  113. package/src/ai/AIClient.ts +359 -409
  114. package/src/expression/ExpressionClient.ts +96 -142
  115. package/src/index.ts +1 -1
  116. package/src/language/LanguageClient.ts +113 -137
  117. package/src/model/Ad4mModel.test.ts +616 -1114
  118. package/src/model/Ad4mModel.ts +4386 -451
  119. package/src/model/Subject.ts +159 -0
  120. package/src/model/decorators.ts +1168 -667
  121. package/src/model/util.ts +54 -53
  122. package/src/neighbourhood/NeighbourhoodClient.ts +266 -392
  123. package/src/neighbourhood/NeighbourhoodProxy.test.ts +7 -3
  124. package/src/neighbourhood/NeighbourhoodProxy.ts +40 -58
  125. package/src/perspectives/PerspectiveClient.ts +540 -779
  126. package/src/perspectives/PerspectiveProxy.ts +2067 -2860
  127. package/src/runtime/RuntimeClient.ts +476 -775
  128. package/src/shacl/SHACLFlow.test.ts +129 -196
  129. package/src/shacl/SHACLShape.test.ts +190 -284
  130. package/src/shacl/SHACLShape.ts +200 -224
  131. package/src/utils.ts +17 -31
  132. package/tsconfig.json +10 -9
  133. package/src/model/IMPROVEMENTS.md +0 -482
  134. package/src/model/PREDICATE_COLLISION_FIX.md +0 -651
  135. package/src/model/README.md +0 -317
  136. package/src/model/SHACL_MUTATION_DECOUPLING.md +0 -356
  137. package/src/model/SUBSCRIPTION_STRATEGY.md +0 -317
  138. package/src/model/TC39_STAGE_3_DECORATORS.md +0 -609
  139. package/src/model/mutation.ts +0 -461
  140. package/src/model/parentUtils.ts +0 -91
  141. package/src/model/prolog/generatePrologFacts.ts +0 -176
  142. package/src/model/prolog/index.ts +0 -1
  143. package/src/model/query/ModelQueryBuilder.ts +0 -173
  144. package/src/model/query/fetchInstance.ts +0 -181
  145. package/src/model/query/hydration.ts +0 -273
  146. package/src/model/query/operations.ts +0 -510
  147. package/src/model/query/snapshot.ts +0 -126
  148. package/src/model/query/surrealCompiler.ts +0 -376
  149. package/src/model/schema/fromJSONSchema.ts +0 -411
  150. package/src/model/schema/metadata.ts +0 -148
  151. package/src/model/subscription.ts +0 -307
  152. package/src/model/transaction.ts +0 -60
  153. package/src/model/types.ts +0 -207
@@ -10,16 +10,16 @@ import { Link } from "../links/Links";
10
10
  */
11
11
  function extractNamespace(uri: string): string {
12
12
  // Handle hash fragments first (highest priority)
13
- const hashIndex = uri.lastIndexOf("#");
13
+ const hashIndex = uri.lastIndexOf('#');
14
14
  if (hashIndex !== -1) {
15
15
  return uri.substring(0, hashIndex + 1);
16
16
  }
17
-
17
+
18
18
  // Handle protocol-style URIs with paths
19
19
  const protocolMatch = uri.match(/^([a-zA-Z][a-zA-Z0-9+.-]*:\/\/)(.*)$/);
20
20
  if (protocolMatch) {
21
21
  const afterScheme = protocolMatch[2];
22
- const lastSlash = afterScheme.lastIndexOf("/");
22
+ const lastSlash = afterScheme.lastIndexOf('/');
23
23
  if (lastSlash !== -1) {
24
24
  // Has path segments - namespace includes up to last slash
25
25
  return protocolMatch[1] + afterScheme.substring(0, lastSlash + 1);
@@ -27,15 +27,15 @@ function extractNamespace(uri: string): string {
27
27
  // Simple protocol URI without path (e.g., "recipe://name")
28
28
  return protocolMatch[1];
29
29
  }
30
-
30
+
31
31
  // Handle colon-separated (namespace:localName)
32
32
  const colonMatch = uri.match(/^([a-zA-Z][a-zA-Z0-9+.-]*:)/);
33
33
  if (colonMatch) {
34
34
  return colonMatch[1];
35
35
  }
36
-
36
+
37
37
  // Fallback: no clear namespace
38
- return "";
38
+ return '';
39
39
  }
40
40
 
41
41
  /**
@@ -44,13 +44,13 @@ function extractNamespace(uri: string): string {
44
44
  */
45
45
  function escapeTurtleString(value: string): string {
46
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
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
54
  }
55
55
 
56
56
  /**
@@ -63,23 +63,23 @@ function escapeTurtleString(value: string): string {
63
63
  */
64
64
  function extractLocalName(uri: string): string {
65
65
  // Handle hash fragments
66
- const hashIndex = uri.lastIndexOf("#");
66
+ const hashIndex = uri.lastIndexOf('#');
67
67
  if (hashIndex !== -1) {
68
68
  return uri.substring(hashIndex + 1);
69
69
  }
70
-
70
+
71
71
  // Handle slash-based namespaces
72
- const lastSlash = uri.lastIndexOf("/");
72
+ const lastSlash = uri.lastIndexOf('/');
73
73
  if (lastSlash !== -1 && lastSlash < uri.length - 1) {
74
74
  return uri.substring(lastSlash + 1);
75
75
  }
76
-
76
+
77
77
  // Handle colon-separated
78
78
  const colonMatch = uri.match(/^[a-zA-Z][a-zA-Z0-9+.-]*:(.+)$/);
79
79
  if (colonMatch) {
80
80
  return colonMatch[1];
81
81
  }
82
-
82
+
83
83
  // Fallback: entire URI
84
84
  return uri;
85
85
  }
@@ -110,7 +110,7 @@ export interface SHACLPropertyShape {
110
110
  datatype?: string;
111
111
 
112
112
  /** Node kind constraint (IRI, Literal, BlankNode) */
113
- nodeKind?: "IRI" | "Literal" | "BlankNode";
113
+ nodeKind?: 'IRI' | 'Literal' | 'BlankNode';
114
114
 
115
115
  /** Minimum cardinality (required if >= 1) */
116
116
  minCount?: number;
@@ -133,8 +133,8 @@ export interface SHACLPropertyShape {
133
133
  /** AD4M-specific: Local-only property */
134
134
  local?: boolean;
135
135
 
136
- /** AD4M-specific: Read-only property (no setter actions) */
137
- readOnly?: boolean;
136
+ /** AD4M-specific: Writable property */
137
+ writable?: boolean;
138
138
 
139
139
  /** AD4M-specific: Language to resolve property values through */
140
140
  resolveLanguage?: string;
@@ -147,9 +147,6 @@ export interface SHACLPropertyShape {
147
147
 
148
148
  /** AD4M-specific: Remover action for collection properties */
149
149
  remover?: AD4MAction[];
150
-
151
- /** AD4M-specific: Reverse path traversal — sh:inversePath instead of sh:path */
152
- inversePath?: boolean;
153
150
  }
154
151
 
155
152
  /**
@@ -166,19 +163,15 @@ export class SHACLShape {
166
163
  /** Property constraints */
167
164
  properties: SHACLPropertyShape[];
168
165
 
169
- /**
170
- * Parent shape URIs for SHACL inheritance (sh:node references).
171
- * When a model class extends another @Model-decorated class, the child shape
172
- * references the parent shape via sh:node rather than duplicating its properties.
173
- */
174
- parentShapes?: string[];
175
-
176
166
  /** AD4M-specific: Constructor actions for creating instances */
177
167
  constructor_actions?: AD4MAction[];
178
168
 
179
169
  /** AD4M-specific: Destructor actions for removing instances */
180
170
  destructor_actions?: AD4MAction[];
181
171
 
172
+ /** Parent shape URIs for model inheritance (sh:node references) */
173
+ parentShapes: string[];
174
+
182
175
  /**
183
176
  * Create a new SHACL Shape
184
177
  * @param targetClassOrShapeUri - If one argument: the target class (shape URI auto-derived as {class}Shape)
@@ -199,23 +192,26 @@ export class SHACLShape {
199
192
  this.nodeShapeUri = `${namespace}${localName}Shape`;
200
193
  }
201
194
  this.properties = [];
195
+ this.parentShapes = [];
202
196
  }
203
197
 
204
198
  /**
205
- * Add a property constraint to this shape
199
+ * Add a parent shape reference (sh:node) for model inheritance.
200
+ * When a @Model class extends another @Model, the child shape
201
+ * references the parent shape so SHACL validators can walk the
202
+ * class hierarchy.
206
203
  */
207
- addProperty(prop: SHACLPropertyShape): void {
208
- this.properties.push(prop);
204
+ addParentShape(parentShapeUri: string): void {
205
+ if (!this.parentShapes.includes(parentShapeUri)) {
206
+ this.parentShapes.push(parentShapeUri);
207
+ }
209
208
  }
210
209
 
211
210
  /**
212
- * Add a parent shape reference (sh:node) for SHACL inheritance.
213
- * The child shape will reference the parent shape instead of duplicating its properties.
214
- * @param parentShapeUri - URI of the parent SHACL shape
211
+ * Add a property constraint to this shape
215
212
  */
216
- addParentShape(parentShapeUri: string): void {
217
- this.parentShapes = this.parentShapes ?? [];
218
- this.parentShapes.push(parentShapeUri);
213
+ addProperty(prop: SHACLPropertyShape): void {
214
+ this.properties.push(prop);
219
215
  }
220
216
 
221
217
  /**
@@ -231,7 +227,7 @@ export class SHACLShape {
231
227
  setDestructorActions(actions: AD4MAction[]): void {
232
228
  this.destructor_actions = actions;
233
229
  }
234
-
230
+
235
231
  /**
236
232
  * Serialize shape to Turtle (RDF) format
237
233
  */
@@ -240,82 +236,76 @@ export class SHACLShape {
240
236
  turtle += `@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n`;
241
237
  turtle += `@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n`;
242
238
  turtle += `@prefix ad4m: <ad4m://> .\n\n`;
243
-
239
+
244
240
  turtle += `<${this.nodeShapeUri}>\n`;
245
241
  turtle += ` a sh:NodeShape ;\n`;
246
-
242
+
247
243
  if (this.targetClass) {
248
244
  turtle += ` sh:targetClass <${this.targetClass}> ;\n`;
249
245
  }
250
246
 
251
- // Emit sh:node references for parent shapes (SHACL inheritance)
252
- if (this.parentShapes && this.parentShapes.length > 0) {
253
- for (const parentUri of this.parentShapes) {
254
- turtle += ` sh:node <${parentUri}> ;\n`;
255
- }
247
+ // Emit sh:node references for parent shapes (model inheritance)
248
+ for (const parentUri of this.parentShapes) {
249
+ turtle += ` sh:node <${parentUri}> ;\n`;
256
250
  }
257
-
251
+
258
252
  // Add property shapes
259
253
  for (let i = 0; i < this.properties.length; i++) {
260
254
  const prop = this.properties[i];
261
255
  const isLast = i === this.properties.length - 1;
262
-
256
+
263
257
  turtle += ` sh:property [\n`;
264
- if (prop.inversePath) {
265
- turtle += ` sh:path [ sh:inversePath <${prop.path}> ] ;\n`;
266
- } else {
267
- turtle += ` sh:path <${prop.path}> ;\n`;
268
- }
269
-
258
+ turtle += ` sh:path <${prop.path}> ;\n`;
259
+
270
260
  if (prop.datatype) {
271
261
  turtle += ` sh:datatype <${prop.datatype}> ;\n`;
272
262
  }
273
-
263
+
274
264
  if (prop.nodeKind) {
275
265
  turtle += ` sh:nodeKind sh:${prop.nodeKind} ;\n`;
276
266
  }
277
-
267
+
278
268
  if (prop.minCount !== undefined) {
279
269
  turtle += ` sh:minCount ${prop.minCount} ;\n`;
280
270
  }
281
-
271
+
282
272
  if (prop.maxCount !== undefined) {
283
273
  turtle += ` sh:maxCount ${prop.maxCount} ;\n`;
284
274
  }
285
-
275
+
286
276
  if (prop.pattern) {
287
277
  turtle += ` sh:pattern "${escapeTurtleString(prop.pattern)}" ;\n`;
288
278
  }
289
-
279
+
290
280
  if (prop.minInclusive !== undefined) {
291
281
  turtle += ` sh:minInclusive ${prop.minInclusive} ;\n`;
292
282
  }
293
-
283
+
294
284
  if (prop.maxInclusive !== undefined) {
295
285
  turtle += ` sh:maxInclusive ${prop.maxInclusive} ;\n`;
296
286
  }
297
-
287
+
298
288
  if (prop.hasValue) {
299
289
  turtle += ` sh:hasValue "${escapeTurtleString(prop.hasValue)}" ;\n`;
300
290
  }
301
-
291
+
302
292
  // AD4M-specific metadata
303
293
  if (prop.local !== undefined) {
304
294
  turtle += ` ad4m:local ${prop.local} ;\n`;
305
295
  }
306
-
307
- if (prop.readOnly !== undefined) {
308
- turtle += ` ad4m:readOnly ${prop.readOnly} ;\n`;
296
+
297
+ if (prop.writable !== undefined) {
298
+ turtle += ` ad4m:writable ${prop.writable} ;\n`;
309
299
  }
310
-
300
+
311
301
  // Remove trailing semicolon and close bracket
312
- turtle = turtle.slice(0, -2) + "\n";
302
+ turtle = turtle.slice(0, -2) + '\n';
313
303
  turtle += isLast ? ` ] .\n` : ` ] ;\n`;
314
304
  }
315
-
305
+
316
306
  return turtle;
317
307
  }
318
-
308
+
319
309
  /**
320
310
  * Serialize shape to AD4M Links (RDF triples)
321
311
  * Stores the shape as a graph of links in a Perspective
@@ -327,7 +317,7 @@ export class SHACLShape {
327
317
  links.push({
328
318
  source: this.nodeShapeUri,
329
319
  predicate: "rdf://type",
330
- target: "sh://NodeShape",
320
+ target: "sh://NodeShape"
331
321
  });
332
322
 
333
323
  // Target class
@@ -335,46 +325,34 @@ export class SHACLShape {
335
325
  links.push({
336
326
  source: this.nodeShapeUri,
337
327
  predicate: "sh://targetClass",
338
- target: this.targetClass,
328
+ target: this.targetClass
339
329
  });
340
330
  }
341
331
 
342
- // Parent shape references (sh:node — SHACL inheritance)
343
- if (this.parentShapes && this.parentShapes.length > 0) {
344
- for (const parentUri of this.parentShapes) {
345
- links.push({
346
- source: this.nodeShapeUri,
347
- predicate: "sh://node",
348
- target: parentUri,
349
- });
350
- }
351
- }
352
-
353
- // Constructor actions – always emit this link (even for an empty array)
354
- // so the Rust backend can distinguish "class registered but no init steps"
355
- // from "class not registered", and create_subject can succeed with 0
356
- // commands for classes that have no @Flag / initial-valued properties.
357
- if (this.constructor_actions !== undefined) {
332
+ // Constructor actions — always emit, even for an empty array.
333
+ // An empty `[]` tells the executor the shape is valid but has no
334
+ // required initial links (all-optional model with no @Flag).
335
+ if (this.constructor_actions) {
358
336
  links.push({
359
337
  source: this.nodeShapeUri,
360
338
  predicate: "ad4m://constructor",
361
- target: `literal://string:${JSON.stringify(this.constructor_actions)}`,
339
+ target: `literal://string:${JSON.stringify(this.constructor_actions)}`
362
340
  });
363
341
  }
364
342
 
365
- // Destructor actions
366
- if (this.destructor_actions && this.destructor_actions.length > 0) {
343
+ // Destructor actions — same rationale as constructor.
344
+ if (this.destructor_actions) {
367
345
  links.push({
368
346
  source: this.nodeShapeUri,
369
347
  predicate: "ad4m://destructor",
370
- target: `literal://string:${JSON.stringify(this.destructor_actions)}`,
348
+ target: `literal://string:${JSON.stringify(this.destructor_actions)}`
371
349
  });
372
350
  }
373
-
351
+
374
352
  // Property shapes (each gets a named URI: {namespace}/{ClassName}.{propertyName})
375
353
  for (let i = 0; i < this.properties.length; i++) {
376
354
  const prop = this.properties[i];
377
-
355
+
378
356
  // Generate named property shape URI
379
357
  let propShapeId: string;
380
358
  if (prop.name && this.targetClass) {
@@ -387,100 +365,100 @@ export class SHACLShape {
387
365
  // Fallback to blank node if name is missing
388
366
  propShapeId = `_:propShape${i}`;
389
367
  }
390
-
368
+
391
369
  // Link shape to property shape
392
370
  links.push({
393
371
  source: this.nodeShapeUri,
394
372
  predicate: "sh://property",
395
- target: propShapeId,
373
+ target: propShapeId
396
374
  });
397
-
398
- // Property path (forward or inverse)
375
+
376
+ // Property path
399
377
  links.push({
400
378
  source: propShapeId,
401
- predicate: prop.inversePath ? "sh://inversePath" : "sh://path",
402
- target: prop.path,
379
+ predicate: "sh://path",
380
+ target: prop.path
403
381
  });
404
-
382
+
405
383
  // Constraints
406
384
  if (prop.datatype) {
407
385
  links.push({
408
386
  source: propShapeId,
409
387
  predicate: "sh://datatype",
410
- target: prop.datatype,
388
+ target: prop.datatype
411
389
  });
412
390
  }
413
-
391
+
414
392
  if (prop.nodeKind) {
415
393
  links.push({
416
394
  source: propShapeId,
417
395
  predicate: "sh://nodeKind",
418
- target: `sh://${prop.nodeKind}`,
396
+ target: `sh://${prop.nodeKind}`
419
397
  });
420
398
  }
421
-
399
+
422
400
  if (prop.minCount !== undefined) {
423
401
  links.push({
424
402
  source: propShapeId,
425
403
  predicate: "sh://minCount",
426
- target: `literal://${prop.minCount}^^xsd:integer`,
404
+ target: `literal://${prop.minCount}^^xsd:integer`
427
405
  });
428
406
  }
429
-
407
+
430
408
  if (prop.maxCount !== undefined) {
431
409
  links.push({
432
410
  source: propShapeId,
433
411
  predicate: "sh://maxCount",
434
- target: `literal://${prop.maxCount}^^xsd:integer`,
412
+ target: `literal://${prop.maxCount}^^xsd:integer`
435
413
  });
436
414
  }
437
-
415
+
438
416
  if (prop.pattern) {
439
417
  links.push({
440
418
  source: propShapeId,
441
419
  predicate: "sh://pattern",
442
- target: `literal://${prop.pattern}`,
420
+ target: `literal://${prop.pattern}`
443
421
  });
444
422
  }
445
-
423
+
446
424
  if (prop.minInclusive !== undefined) {
447
425
  links.push({
448
426
  source: propShapeId,
449
427
  predicate: "sh://minInclusive",
450
- target: `literal://${prop.minInclusive}`,
428
+ target: `literal://${prop.minInclusive}`
451
429
  });
452
430
  }
453
-
431
+
454
432
  if (prop.maxInclusive !== undefined) {
455
433
  links.push({
456
434
  source: propShapeId,
457
435
  predicate: "sh://maxInclusive",
458
- target: `literal://${prop.maxInclusive}`,
436
+ target: `literal://${prop.maxInclusive}`
459
437
  });
460
438
  }
461
-
439
+
462
440
  if (prop.hasValue) {
463
441
  links.push({
464
442
  source: propShapeId,
465
443
  predicate: "sh://hasValue",
466
- target: `literal://${prop.hasValue}`,
444
+ target: `literal://${prop.hasValue}`
467
445
  });
468
446
  }
469
-
447
+
470
448
  // AD4M-specific metadata
471
449
  if (prop.local !== undefined) {
472
450
  links.push({
473
451
  source: propShapeId,
474
452
  predicate: "ad4m://local",
475
- target: `literal://${prop.local}`,
453
+ target: `literal://${prop.local}`
476
454
  });
477
455
  }
478
-
479
- if (prop.readOnly !== undefined) {
456
+
457
+ if (prop.writable !== undefined) {
480
458
  links.push({
481
459
  source: propShapeId,
482
- predicate: "ad4m://readOnly",
483
- target: `literal://${prop.readOnly}`,
460
+ predicate: "ad4m://writable",
461
+ target: `literal://${prop.writable}`
484
462
  });
485
463
  }
486
464
 
@@ -488,7 +466,7 @@ export class SHACLShape {
488
466
  links.push({
489
467
  source: propShapeId,
490
468
  predicate: "ad4m://resolveLanguage",
491
- target: `literal://string:${prop.resolveLanguage}`,
469
+ target: `literal://string:${prop.resolveLanguage}`
492
470
  });
493
471
  }
494
472
 
@@ -497,7 +475,7 @@ export class SHACLShape {
497
475
  links.push({
498
476
  source: propShapeId,
499
477
  predicate: "ad4m://setter",
500
- target: `literal://string:${JSON.stringify(prop.setter)}`,
478
+ target: `literal://string:${JSON.stringify(prop.setter)}`
501
479
  });
502
480
  }
503
481
 
@@ -505,7 +483,7 @@ export class SHACLShape {
505
483
  links.push({
506
484
  source: propShapeId,
507
485
  predicate: "ad4m://adder",
508
- target: `literal://string:${JSON.stringify(prop.adder)}`,
486
+ target: `literal://string:${JSON.stringify(prop.adder)}`
509
487
  });
510
488
  }
511
489
 
@@ -513,32 +491,32 @@ export class SHACLShape {
513
491
  links.push({
514
492
  source: propShapeId,
515
493
  predicate: "ad4m://remover",
516
- target: `literal://string:${JSON.stringify(prop.remover)}`,
494
+ target: `literal://string:${JSON.stringify(prop.remover)}`
517
495
  });
518
496
  }
519
497
  }
520
498
 
521
499
  return links;
522
500
  }
523
-
501
+
524
502
  /**
525
503
  * Reconstruct shape from AD4M Links
526
504
  */
527
505
  static fromLinks(links: Link[], shapeUri: string): SHACLShape {
528
506
  // Find target class
529
- const targetClassLink = links.find(
530
- (l) => l.source === shapeUri && l.predicate === "sh://targetClass",
507
+ const targetClassLink = links.find(l =>
508
+ l.source === shapeUri && l.predicate === "sh://targetClass"
531
509
  );
532
-
510
+
533
511
  const shape = new SHACLShape(shapeUri, targetClassLink?.target);
534
512
 
535
513
  // Find constructor actions
536
- const constructorLink = links.find(
537
- (l) => l.source === shapeUri && l.predicate === "ad4m://constructor",
514
+ const constructorLink = links.find(l =>
515
+ l.source === shapeUri && l.predicate === "ad4m://constructor"
538
516
  );
539
517
  if (constructorLink) {
540
518
  try {
541
- const jsonStr = constructorLink.target.replace("literal://string:", "");
519
+ const jsonStr = constructorLink.target.replace('literal://string:', '');
542
520
  shape.constructor_actions = JSON.parse(jsonStr);
543
521
  } catch (e) {
544
522
  // Ignore parse errors
@@ -546,12 +524,12 @@ export class SHACLShape {
546
524
  }
547
525
 
548
526
  // Find destructor actions
549
- const destructorLink = links.find(
550
- (l) => l.source === shapeUri && l.predicate === "ad4m://destructor",
527
+ const destructorLink = links.find(l =>
528
+ l.source === shapeUri && l.predicate === "ad4m://destructor"
551
529
  );
552
530
  if (destructorLink) {
553
531
  try {
554
- const jsonStr = destructorLink.target.replace("literal://string:", "");
532
+ const jsonStr = destructorLink.target.replace('literal://string:', '');
555
533
  shape.destructor_actions = JSON.parse(jsonStr);
556
534
  } catch (e) {
557
535
  // Ignore parse errors
@@ -559,169 +537,161 @@ export class SHACLShape {
559
537
  }
560
538
 
561
539
  // Find all property shapes
562
- const propShapeLinks = links.filter(
563
- (l) => l.source === shapeUri && l.predicate === "sh://property",
540
+ const propShapeLinks = links.filter(l =>
541
+ l.source === shapeUri && l.predicate === "sh://property"
564
542
  );
565
-
543
+
566
544
  for (const propLink of propShapeLinks) {
567
545
  const propShapeId = propLink.target;
568
-
546
+
569
547
  // Reconstruct property from its links
570
- const pathLink = links.find(
571
- (l) => l.source === propShapeId && l.predicate === "sh://path",
548
+ const pathLink = links.find(l =>
549
+ l.source === propShapeId && l.predicate === "sh://path"
572
550
  );
573
-
551
+
574
552
  if (!pathLink) continue;
575
-
553
+
576
554
  // Extract property name from propShapeId if it's a named URI
577
555
  // Format: {namespace}{ClassName}.{propertyName}
578
556
  let propertyName: string | undefined;
579
- if (!propShapeId.startsWith("_:")) {
580
- const lastDotIndex = propShapeId.lastIndexOf(".");
557
+ if (!propShapeId.startsWith('_:')) {
558
+ const lastDotIndex = propShapeId.lastIndexOf('.');
581
559
  if (lastDotIndex !== -1) {
582
560
  propertyName = propShapeId.substring(lastDotIndex + 1);
583
561
  }
584
562
  }
585
-
563
+
586
564
  const prop: SHACLPropertyShape = {
587
565
  name: propertyName,
588
- path: pathLink.target,
566
+ path: pathLink.target
589
567
  };
590
-
568
+
591
569
  // Extract constraints
592
- const datatypeLink = links.find(
593
- (l) => l.source === propShapeId && l.predicate === "sh://datatype",
570
+ const datatypeLink = links.find(l =>
571
+ l.source === propShapeId && l.predicate === "sh://datatype"
594
572
  );
595
573
  if (datatypeLink) prop.datatype = datatypeLink.target;
596
-
597
- const nodeKindLink = links.find(
598
- (l) => l.source === propShapeId && l.predicate === "sh://nodeKind",
574
+
575
+ const nodeKindLink = links.find(l =>
576
+ l.source === propShapeId && l.predicate === "sh://nodeKind"
599
577
  );
600
578
  if (nodeKindLink) {
601
- prop.nodeKind = nodeKindLink.target.replace("sh://", "") as any;
579
+ prop.nodeKind = nodeKindLink.target.replace('sh://', '') as any;
602
580
  }
603
-
604
- const minCountLink = links.find(
605
- (l) => l.source === propShapeId && l.predicate === "sh://minCount",
581
+
582
+ const minCountLink = links.find(l =>
583
+ l.source === propShapeId && l.predicate === "sh://minCount"
606
584
  );
607
585
  if (minCountLink) {
608
586
  // Handle both formats: literal://5^^xsd:integer and literal://number:5
609
- let val = minCountLink.target
610
- .replace("literal://", "")
611
- .replace(/\^\^.*$/, "");
612
- if (val.startsWith("number:")) val = val.substring(7);
587
+ let val = minCountLink.target.replace('literal://', '').replace(/\^\^.*$/, '');
588
+ if (val.startsWith('number:')) val = val.substring(7);
613
589
  prop.minCount = parseInt(val);
614
590
  }
615
-
616
- const maxCountLink = links.find(
617
- (l) => l.source === propShapeId && l.predicate === "sh://maxCount",
591
+
592
+ const maxCountLink = links.find(l =>
593
+ l.source === propShapeId && l.predicate === "sh://maxCount"
618
594
  );
619
595
  if (maxCountLink) {
620
596
  // Handle both formats: literal://5^^xsd:integer and literal://number:5
621
- let val = maxCountLink.target
622
- .replace("literal://", "")
623
- .replace(/\^\^.*$/, "");
624
- if (val.startsWith("number:")) val = val.substring(7);
597
+ let val = maxCountLink.target.replace('literal://', '').replace(/\^\^.*$/, '');
598
+ if (val.startsWith('number:')) val = val.substring(7);
625
599
  prop.maxCount = parseInt(val);
626
600
  }
627
-
628
- const patternLink = links.find(
629
- (l) => l.source === propShapeId && l.predicate === "sh://pattern",
601
+
602
+ const patternLink = links.find(l =>
603
+ l.source === propShapeId && l.predicate === "sh://pattern"
630
604
  );
631
605
  if (patternLink) {
632
- prop.pattern = patternLink.target.replace("literal://", "");
606
+ prop.pattern = patternLink.target.replace('literal://', '');
633
607
  }
634
-
635
- const minInclusiveLink = links.find(
636
- (l) => l.source === propShapeId && l.predicate === "sh://minInclusive",
608
+
609
+ const minInclusiveLink = links.find(l =>
610
+ l.source === propShapeId && l.predicate === "sh://minInclusive"
637
611
  );
638
612
  if (minInclusiveLink) {
639
613
  // Handle both formats: literal://5 and literal://number:5
640
- let val = minInclusiveLink.target.replace("literal://", "");
641
- if (val.startsWith("number:")) val = val.substring(7);
614
+ let val = minInclusiveLink.target.replace('literal://', '');
615
+ if (val.startsWith('number:')) val = val.substring(7);
642
616
  prop.minInclusive = parseFloat(val);
643
617
  }
644
618
 
645
- const maxInclusiveLink = links.find(
646
- (l) => l.source === propShapeId && l.predicate === "sh://maxInclusive",
619
+ const maxInclusiveLink = links.find(l =>
620
+ l.source === propShapeId && l.predicate === "sh://maxInclusive"
647
621
  );
648
622
  if (maxInclusiveLink) {
649
623
  // Handle both formats: literal://5 and literal://number:5
650
- let val = maxInclusiveLink.target.replace("literal://", "");
651
- if (val.startsWith("number:")) val = val.substring(7);
624
+ let val = maxInclusiveLink.target.replace('literal://', '');
625
+ if (val.startsWith('number:')) val = val.substring(7);
652
626
  prop.maxInclusive = parseFloat(val);
653
627
  }
654
-
655
- const hasValueLink = links.find(
656
- (l) => l.source === propShapeId && l.predicate === "sh://hasValue",
628
+
629
+ const hasValueLink = links.find(l =>
630
+ l.source === propShapeId && l.predicate === "sh://hasValue"
657
631
  );
658
632
  if (hasValueLink) {
659
- prop.hasValue = hasValueLink.target.replace("literal://", "");
633
+ prop.hasValue = hasValueLink.target.replace('literal://', '');
660
634
  }
661
-
635
+
662
636
  // AD4M-specific
663
- const localLink = links.find(
664
- (l) => l.source === propShapeId && l.predicate === "ad4m://local",
637
+ const localLink = links.find(l =>
638
+ l.source === propShapeId && l.predicate === "ad4m://local"
665
639
  );
666
640
  if (localLink) {
667
641
  // Handle both formats: literal://true and literal://boolean:true
668
- let val = localLink.target.replace("literal://", "");
669
- if (val.startsWith("boolean:")) val = val.substring(8);
670
- prop.local = val === "true";
642
+ let val = localLink.target.replace('literal://', '');
643
+ if (val.startsWith('boolean:')) val = val.substring(8);
644
+ prop.local = val === 'true';
671
645
  }
672
-
673
- const readOnlyLink = links.find(
674
- (l) => l.source === propShapeId && l.predicate === "ad4m://readOnly",
646
+
647
+ const writableLink = links.find(l =>
648
+ l.source === propShapeId && l.predicate === "ad4m://writable"
675
649
  );
676
- if (readOnlyLink) {
650
+ if (writableLink) {
677
651
  // Handle both formats: literal://true and literal://boolean:true
678
- let val = readOnlyLink.target.replace("literal://", "");
679
- if (val.startsWith("boolean:")) val = val.substring(8);
680
- prop.readOnly = val === "true";
652
+ let val = writableLink.target.replace('literal://', '');
653
+ if (val.startsWith('boolean:')) val = val.substring(8);
654
+ prop.writable = val === 'true';
681
655
  }
682
656
 
683
- const resolveLangLink = links.find(
684
- (l) =>
685
- l.source === propShapeId && l.predicate === "ad4m://resolveLanguage",
657
+ const resolveLangLink = links.find(l =>
658
+ l.source === propShapeId && l.predicate === "ad4m://resolveLanguage"
686
659
  );
687
660
  if (resolveLangLink) {
688
- prop.resolveLanguage = resolveLangLink.target.replace(
689
- "literal://string:",
690
- "",
691
- );
661
+ prop.resolveLanguage = resolveLangLink.target.replace('literal://string:', '');
692
662
  }
693
663
 
694
664
  // Parse action arrays
695
- const setterLink = links.find(
696
- (l) => l.source === propShapeId && l.predicate === "ad4m://setter",
665
+ const setterLink = links.find(l =>
666
+ l.source === propShapeId && l.predicate === "ad4m://setter"
697
667
  );
698
668
  if (setterLink) {
699
669
  try {
700
- const jsonStr = setterLink.target.replace("literal://string:", "");
670
+ const jsonStr = setterLink.target.replace('literal://string:', '');
701
671
  prop.setter = JSON.parse(jsonStr);
702
672
  } catch (e) {
703
673
  // Ignore parse errors
704
674
  }
705
675
  }
706
676
 
707
- const adderLink = links.find(
708
- (l) => l.source === propShapeId && l.predicate === "ad4m://adder",
677
+ const adderLink = links.find(l =>
678
+ l.source === propShapeId && l.predicate === "ad4m://adder"
709
679
  );
710
680
  if (adderLink) {
711
681
  try {
712
- const jsonStr = adderLink.target.replace("literal://string:", "");
682
+ const jsonStr = adderLink.target.replace('literal://string:', '');
713
683
  prop.adder = JSON.parse(jsonStr);
714
684
  } catch (e) {
715
685
  // Ignore parse errors
716
686
  }
717
687
  }
718
688
 
719
- const removerLink = links.find(
720
- (l) => l.source === propShapeId && l.predicate === "ad4m://remover",
689
+ const removerLink = links.find(l =>
690
+ l.source === propShapeId && l.predicate === "ad4m://remover"
721
691
  );
722
692
  if (removerLink) {
723
693
  try {
724
- const jsonStr = removerLink.target.replace("literal://string:", "");
694
+ const jsonStr = removerLink.target.replace('literal://string:', '');
725
695
  prop.remover = JSON.parse(jsonStr);
726
696
  } catch (e) {
727
697
  // Ignore parse errors
@@ -730,21 +700,22 @@ export class SHACLShape {
730
700
 
731
701
  shape.addProperty(prop);
732
702
  }
733
-
703
+
734
704
  return shape;
735
705
  }
736
706
 
737
707
  /**
738
708
  * Convert the shape to a JSON-serializable object.
739
709
  * Useful for passing to addSdna() as shaclJson parameter.
740
- *
710
+ *
741
711
  * @returns JSON-serializable object representing the shape
742
712
  */
743
713
  toJSON(): object {
744
714
  return {
745
715
  node_shape_uri: this.nodeShapeUri,
746
716
  target_class: this.targetClass,
747
- properties: this.properties.map((p) => ({
717
+ parent_shapes: this.parentShapes.length > 0 ? this.parentShapes : undefined,
718
+ properties: this.properties.map(p => ({
748
719
  path: p.path,
749
720
  name: p.name,
750
721
  datatype: p.datatype,
@@ -756,7 +727,7 @@ export class SHACLShape {
756
727
  pattern: p.pattern,
757
728
  has_value: p.hasValue,
758
729
  local: p.local,
759
- read_only: p.readOnly,
730
+ writable: p.writable,
760
731
  resolve_language: p.resolveLanguage,
761
732
  setter: p.setter,
762
733
  adder: p.adder,
@@ -774,7 +745,7 @@ export class SHACLShape {
774
745
  const shape = json.node_shape_uri
775
746
  ? new SHACLShape(json.node_shape_uri, json.target_class)
776
747
  : new SHACLShape(json.target_class);
777
-
748
+
778
749
  for (const p of json.properties || []) {
779
750
  shape.addProperty({
780
751
  path: p.path,
@@ -788,21 +759,26 @@ export class SHACLShape {
788
759
  pattern: p.pattern,
789
760
  hasValue: p.has_value,
790
761
  local: p.local,
791
- readOnly: p.read_only,
762
+ writable: p.writable,
792
763
  resolveLanguage: p.resolve_language,
793
764
  setter: p.setter,
794
765
  adder: p.adder,
795
766
  remover: p.remover,
796
767
  });
797
768
  }
798
-
769
+
799
770
  if (json.constructor_actions) {
800
771
  shape.constructor_actions = json.constructor_actions;
801
772
  }
802
773
  if (json.destructor_actions) {
803
774
  shape.destructor_actions = json.destructor_actions;
804
775
  }
805
-
776
+ if (json.parent_shapes) {
777
+ for (const ps of json.parent_shapes) {
778
+ shape.addParentShape(ps);
779
+ }
780
+ }
781
+
806
782
  return shape;
807
783
  }
808
784
  }