@coasys/ad4m 0.12.0-dev-whisper-lang → 0.12.0-rc1-dev

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 (42) hide show
  1. package/lib/index.cjs +1742 -320
  2. package/lib/index.js +1741 -321
  3. package/lib/src/ai/AIClient.d.ts +1 -1
  4. package/lib/src/ai/AIClient.js +4 -5
  5. package/lib/src/ai/AIResolver.d.ts +1 -1
  6. package/lib/src/ai/AIResolver.js +2 -3
  7. package/lib/src/index.d.ts +2 -0
  8. package/lib/src/index.js +2 -0
  9. package/lib/src/model/Ad4mModel.js +17 -5
  10. package/lib/src/model/decorators.js +196 -6
  11. package/lib/src/model/util.d.ts +1 -1
  12. package/lib/src/model/util.js +15 -12
  13. package/lib/src/perspectives/PerspectiveClient.d.ts +11 -1
  14. package/lib/src/perspectives/PerspectiveClient.js +15 -4
  15. package/lib/src/perspectives/PerspectiveProxy.d.ts +124 -8
  16. package/lib/src/perspectives/PerspectiveProxy.js +573 -274
  17. package/lib/src/perspectives/PerspectiveResolver.d.ts +1 -1
  18. package/lib/src/perspectives/PerspectiveResolver.js +5 -4
  19. package/lib/src/schema.gql +2 -2
  20. package/lib/src/shacl/SHACLFlow.d.ts +171 -0
  21. package/lib/src/shacl/SHACLFlow.js +370 -0
  22. package/lib/src/shacl/SHACLFlow.test.d.ts +1 -0
  23. package/lib/src/shacl/SHACLFlow.test.js +205 -0
  24. package/lib/src/shacl/SHACLShape.d.ts +108 -0
  25. package/lib/src/shacl/SHACLShape.js +566 -0
  26. package/lib/src/shacl/SHACLShape.test.d.ts +1 -0
  27. package/lib/src/shacl/SHACLShape.test.js +336 -0
  28. package/lib/src/typeDefs.js +2 -2
  29. package/package.json +1 -2
  30. package/src/ai/AIClient.ts +4 -6
  31. package/src/ai/AIResolver.ts +1 -2
  32. package/src/index.ts +2 -0
  33. package/src/model/Ad4mModel.ts +19 -5
  34. package/src/model/decorators.ts +221 -5
  35. package/src/model/util.ts +15 -12
  36. package/src/perspectives/PerspectiveClient.ts +15 -4
  37. package/src/perspectives/PerspectiveProxy.ts +588 -304
  38. package/src/perspectives/PerspectiveResolver.ts +1 -1
  39. package/src/shacl/SHACLFlow.test.ts +237 -0
  40. package/src/shacl/SHACLFlow.ts +503 -0
  41. package/src/shacl/SHACLShape.test.ts +400 -0
  42. package/src/shacl/SHACLShape.ts +755 -0
@@ -0,0 +1,400 @@
1
+ import { SHACLShape, SHACLPropertyShape, AD4MAction } from './SHACLShape';
2
+
3
+ describe('SHACLShape', () => {
4
+ describe('toLinks()', () => {
5
+ it('creates basic shape links', () => {
6
+ const shape = new SHACLShape('recipe://Recipe');
7
+ const links = shape.toLinks();
8
+
9
+ // Should have targetClass link
10
+ const targetClassLink = links.find(l => l.predicate === 'sh://targetClass');
11
+ expect(targetClassLink).toBeDefined();
12
+ expect(targetClassLink!.source).toBe('recipe://RecipeShape');
13
+ expect(targetClassLink!.target).toBe('recipe://Recipe');
14
+ });
15
+
16
+ it('creates property shape links with named URIs', () => {
17
+ const shape = new SHACLShape('recipe://Recipe');
18
+ const prop: SHACLPropertyShape = {
19
+ name: 'name',
20
+ path: 'recipe://name',
21
+ datatype: 'xsd:string',
22
+ minCount: 1,
23
+ maxCount: 1,
24
+ };
25
+ shape.addProperty(prop);
26
+ const links = shape.toLinks();
27
+
28
+ // Property shape should use named URI
29
+ const propLink = links.find(l => l.predicate === 'sh://property');
30
+ expect(propLink).toBeDefined();
31
+ expect(propLink!.target).toBe('recipe://Recipe.name'); // Named URI, not blank node
32
+
33
+ // Path link
34
+ const pathLink = links.find(l =>
35
+ l.source === 'recipe://Recipe.name' && l.predicate === 'sh://path'
36
+ );
37
+ expect(pathLink).toBeDefined();
38
+ expect(pathLink!.target).toBe('recipe://name');
39
+
40
+ // Datatype link
41
+ const datatypeLink = links.find(l =>
42
+ l.source === 'recipe://Recipe.name' && l.predicate === 'sh://datatype'
43
+ );
44
+ expect(datatypeLink).toBeDefined();
45
+ expect(datatypeLink!.target).toBe('xsd:string');
46
+
47
+ // Cardinality links
48
+ const minCountLink = links.find(l =>
49
+ l.source === 'recipe://Recipe.name' && l.predicate === 'sh://minCount'
50
+ );
51
+ expect(minCountLink).toBeDefined();
52
+ expect(minCountLink!.target).toContain('1');
53
+
54
+ const maxCountLink = links.find(l =>
55
+ l.source === 'recipe://Recipe.name' && l.predicate === 'sh://maxCount'
56
+ );
57
+ expect(maxCountLink).toBeDefined();
58
+ expect(maxCountLink!.target).toContain('1');
59
+ });
60
+
61
+ it('creates action links', () => {
62
+ const shape = new SHACLShape('recipe://Recipe');
63
+ const setterAction: AD4MAction = {
64
+ action: 'addLink',
65
+ source: 'this',
66
+ predicate: 'recipe://name',
67
+ target: 'value',
68
+ };
69
+ const prop: SHACLPropertyShape = {
70
+ name: 'title',
71
+ path: 'recipe://title',
72
+ setter: [setterAction],
73
+ };
74
+ shape.addProperty(prop);
75
+ const links = shape.toLinks();
76
+
77
+ // Setter action link
78
+ const setterLink = links.find(l =>
79
+ l.source === 'recipe://Recipe.title' && l.predicate === 'ad4m://setter'
80
+ );
81
+ expect(setterLink).toBeDefined();
82
+ expect(setterLink!.target).toContain('addLink');
83
+ });
84
+
85
+ it('includes constructor and destructor actions', () => {
86
+ const shape = new SHACLShape('recipe://Recipe');
87
+ shape.constructor_actions = [{
88
+ action: 'addLink',
89
+ source: 'this',
90
+ predicate: 'ad4m://type',
91
+ target: 'recipe://Recipe',
92
+ }];
93
+ shape.destructor_actions = [{
94
+ action: 'removeLink',
95
+ source: 'this',
96
+ predicate: 'ad4m://type',
97
+ target: 'recipe://Recipe',
98
+ }];
99
+ const links = shape.toLinks();
100
+
101
+ const constructorLink = links.find(l => l.predicate === 'ad4m://constructor');
102
+ expect(constructorLink).toBeDefined();
103
+ expect(constructorLink!.target).toContain('addLink');
104
+
105
+ const destructorLink = links.find(l => l.predicate === 'ad4m://destructor');
106
+ expect(destructorLink).toBeDefined();
107
+ expect(destructorLink!.target).toContain('removeLink');
108
+ });
109
+ });
110
+
111
+ describe('fromLinks()', () => {
112
+ it('reconstructs shape from links', () => {
113
+ const originalShape = new SHACLShape('recipe://Recipe');
114
+ originalShape.addProperty({
115
+ name: 'name',
116
+ path: 'recipe://name',
117
+ datatype: 'xsd:string',
118
+ minCount: 1,
119
+ });
120
+
121
+ const links = originalShape.toLinks();
122
+ const reconstructed = SHACLShape.fromLinks(links, 'recipe://RecipeShape');
123
+
124
+ expect(reconstructed.targetClass).toBe('recipe://Recipe');
125
+ expect(reconstructed.properties.length).toBe(1);
126
+ expect(reconstructed.properties[0].path).toBe('recipe://name');
127
+ expect(reconstructed.properties[0].datatype).toBe('xsd:string');
128
+ expect(reconstructed.properties[0].minCount).toBe(1);
129
+ });
130
+
131
+ it('handles multiple properties', () => {
132
+ const originalShape = new SHACLShape('recipe://Recipe');
133
+ originalShape.addProperty({
134
+ name: 'name',
135
+ path: 'recipe://name',
136
+ datatype: 'xsd:string',
137
+ });
138
+ originalShape.addProperty({
139
+ name: 'servings',
140
+ path: 'recipe://servings',
141
+ datatype: 'xsd:integer',
142
+ });
143
+
144
+ const links = originalShape.toLinks();
145
+ const reconstructed = SHACLShape.fromLinks(links, 'recipe://RecipeShape');
146
+
147
+ expect(reconstructed.properties.length).toBe(2);
148
+ const nameProp = reconstructed.properties.find(p => p.path === 'recipe://name');
149
+ const servingsProp = reconstructed.properties.find(p => p.path === 'recipe://servings');
150
+ expect(nameProp).toBeDefined();
151
+ expect(servingsProp).toBeDefined();
152
+ expect(nameProp!.datatype).toBe('xsd:string');
153
+ expect(servingsProp!.datatype).toBe('xsd:integer');
154
+ });
155
+
156
+ it('reconstructs action arrays', () => {
157
+ const originalShape = new SHACLShape('recipe://Recipe');
158
+ const setterAction: AD4MAction = {
159
+ action: 'addLink',
160
+ source: 'this',
161
+ predicate: 'recipe://name',
162
+ target: 'value',
163
+ };
164
+ originalShape.addProperty({
165
+ name: 'name',
166
+ path: 'recipe://name',
167
+ setter: [setterAction],
168
+ });
169
+
170
+ const links = originalShape.toLinks();
171
+ const reconstructed = SHACLShape.fromLinks(links, 'recipe://RecipeShape');
172
+
173
+ expect(reconstructed.properties[0].setter).toBeDefined();
174
+ expect(reconstructed.properties[0].setter!.length).toBe(1);
175
+ expect(reconstructed.properties[0].setter![0].action).toBe('addLink');
176
+ });
177
+ });
178
+
179
+ describe('round-trip serialization', () => {
180
+ it('preserves all property attributes', () => {
181
+ const original = new SHACLShape('test://Model');
182
+ original.addProperty({
183
+ name: 'field',
184
+ path: 'test://field',
185
+ datatype: 'xsd:string',
186
+ nodeKind: 'Literal',
187
+ minCount: 0,
188
+ maxCount: 5,
189
+ pattern: '^[a-z]+$',
190
+ minInclusive: 10,
191
+ maxInclusive: 100,
192
+ hasValue: 'expectedValue',
193
+ resolveLanguage: 'test://language',
194
+ local: true,
195
+ writable: true,
196
+ setter: [{ action: 'addLink', source: 'this', predicate: 'test://field', target: 'value' }],
197
+ adder: [{ action: 'addLink', source: 'this', predicate: 'test://items', target: 'value' }],
198
+ remover: [{ action: 'removeLink', source: 'this', predicate: 'test://items', target: 'value' }],
199
+ });
200
+
201
+ const links = original.toLinks();
202
+ const reconstructed = SHACLShape.fromLinks(links, 'test://ModelShape');
203
+
204
+ const prop = reconstructed.properties[0];
205
+ expect(prop.path).toBe('test://field');
206
+ expect(prop.datatype).toBe('xsd:string');
207
+ expect(prop.nodeKind).toBe('Literal');
208
+ expect(prop.minCount).toBe(0);
209
+ expect(prop.maxCount).toBe(5);
210
+ expect(prop.pattern).toBe('^[a-z]+$');
211
+ expect(prop.minInclusive).toBe(10);
212
+ expect(prop.maxInclusive).toBe(100);
213
+ expect(prop.hasValue).toBe('expectedValue');
214
+ expect(prop.resolveLanguage).toBe('test://language');
215
+ expect(prop.local).toBe(true);
216
+ expect(prop.writable).toBe(true);
217
+ expect(prop.setter).toBeDefined();
218
+ expect(prop.adder).toBeDefined();
219
+ expect(prop.remover).toBeDefined();
220
+ });
221
+
222
+ it('preserves constructor and destructor actions', () => {
223
+ const original = new SHACLShape('test://Model');
224
+ original.constructor_actions = [
225
+ { action: 'addLink', source: 'this', predicate: 'rdf://type', target: 'test://Model' }
226
+ ];
227
+ original.destructor_actions = [
228
+ { action: 'removeLink', source: 'this', predicate: 'rdf://type', target: 'test://Model' }
229
+ ];
230
+
231
+ const links = original.toLinks();
232
+ const reconstructed = SHACLShape.fromLinks(links, 'test://ModelShape');
233
+
234
+ expect(reconstructed.constructor_actions).toBeDefined();
235
+ expect(reconstructed.constructor_actions!.length).toBe(1);
236
+ expect(reconstructed.constructor_actions![0].action).toBe('addLink');
237
+
238
+ expect(reconstructed.destructor_actions).toBeDefined();
239
+ expect(reconstructed.destructor_actions!.length).toBe(1);
240
+ expect(reconstructed.destructor_actions![0].action).toBe('removeLink');
241
+ });
242
+ });
243
+
244
+ describe('edge cases', () => {
245
+ it('handles empty shape', () => {
246
+ const shape = new SHACLShape('test://Empty');
247
+ const links = shape.toLinks();
248
+
249
+ expect(links.length).toBeGreaterThanOrEqual(1); // At least targetClass link
250
+
251
+ const reconstructed = SHACLShape.fromLinks(links, 'test://EmptyShape');
252
+ expect(reconstructed.targetClass).toBe('test://Empty');
253
+ expect(reconstructed.properties.length).toBe(0);
254
+ });
255
+
256
+ it('handles URI with hash fragment', () => {
257
+ const shape = new SHACLShape('https://example.com/vocab#Recipe');
258
+ const links = shape.toLinks();
259
+
260
+ const targetClassLink = links.find(l => l.predicate === 'sh://targetClass');
261
+ expect(targetClassLink!.source).toBe('https://example.com/vocab#RecipeShape');
262
+ });
263
+
264
+ it('falls back to blank nodes when property has no name', () => {
265
+ const shape = new SHACLShape('test://Model');
266
+ shape.addProperty({
267
+ path: 'test://unnamed',
268
+ // No name property
269
+ });
270
+ const links = shape.toLinks();
271
+
272
+ const propLink = links.find(l => l.predicate === 'sh://property');
273
+ expect(propLink).toBeDefined();
274
+ // Should use blank node format when no name provided
275
+ expect(propLink!.target).toMatch(/_:propShape\d+|test:\/\/Model\./);
276
+ });
277
+ });
278
+
279
+ describe('toJSON/fromJSON', () => {
280
+ it('serializes and deserializes basic shape', () => {
281
+ const original = new SHACLShape('recipe://Recipe');
282
+ original.addProperty({
283
+ name: 'name',
284
+ path: 'recipe://name',
285
+ datatype: 'xsd:string',
286
+ minCount: 1,
287
+ });
288
+
289
+ const json = original.toJSON();
290
+ const reconstructed = SHACLShape.fromJSON(json);
291
+
292
+ expect(reconstructed.targetClass).toBe('recipe://Recipe');
293
+ expect(reconstructed.properties.length).toBe(1);
294
+ expect(reconstructed.properties[0].name).toBe('name');
295
+ expect(reconstructed.properties[0].path).toBe('recipe://name');
296
+ expect(reconstructed.properties[0].datatype).toBe('xsd:string');
297
+ expect(reconstructed.properties[0].minCount).toBe(1);
298
+ });
299
+
300
+ it('preserves nodeShapeUri in round-trip', () => {
301
+ const original = new SHACLShape('custom://CustomShape', 'recipe://Recipe');
302
+
303
+ const json = original.toJSON();
304
+ const reconstructed = SHACLShape.fromJSON(json);
305
+
306
+ expect(reconstructed.nodeShapeUri).toBe('custom://CustomShape');
307
+ expect(reconstructed.targetClass).toBe('recipe://Recipe');
308
+ });
309
+
310
+ it('preserves minInclusive and maxInclusive', () => {
311
+ const original = new SHACLShape('test://Model');
312
+ original.addProperty({
313
+ name: 'rating',
314
+ path: 'test://rating',
315
+ datatype: 'xsd:integer',
316
+ minInclusive: 1,
317
+ maxInclusive: 5,
318
+ });
319
+
320
+ const json = original.toJSON();
321
+ const reconstructed = SHACLShape.fromJSON(json);
322
+
323
+ expect(reconstructed.properties[0].minInclusive).toBe(1);
324
+ expect(reconstructed.properties[0].maxInclusive).toBe(5);
325
+ });
326
+
327
+ it('preserves resolveLanguage', () => {
328
+ const original = new SHACLShape('test://Model');
329
+ original.addProperty({
330
+ name: 'content',
331
+ path: 'test://content',
332
+ resolveLanguage: 'literal',
333
+ });
334
+
335
+ const json = original.toJSON();
336
+ const reconstructed = SHACLShape.fromJSON(json);
337
+
338
+ expect(reconstructed.properties[0].resolveLanguage).toBe('literal');
339
+ });
340
+
341
+ it('preserves constructor and destructor actions', () => {
342
+ const original = new SHACLShape('test://Model');
343
+ original.constructor_actions = [
344
+ { action: 'addLink', source: 'this', predicate: 'rdf://type', target: 'test://Model' }
345
+ ];
346
+ original.destructor_actions = [
347
+ { action: 'removeLink', source: 'this', predicate: 'rdf://type', target: 'test://Model' }
348
+ ];
349
+
350
+ const json = original.toJSON();
351
+ const reconstructed = SHACLShape.fromJSON(json);
352
+
353
+ expect(reconstructed.constructor_actions).toEqual(original.constructor_actions);
354
+ expect(reconstructed.destructor_actions).toEqual(original.destructor_actions);
355
+ });
356
+
357
+ it('preserves all property attributes', () => {
358
+ const original = new SHACLShape('test://Model');
359
+ original.addProperty({
360
+ name: 'field',
361
+ path: 'test://field',
362
+ datatype: 'xsd:string',
363
+ nodeKind: 'Literal',
364
+ minCount: 0,
365
+ maxCount: 5,
366
+ minInclusive: 0,
367
+ maxInclusive: 100,
368
+ pattern: '^[a-z]+$',
369
+ hasValue: 'default',
370
+ local: true,
371
+ writable: true,
372
+ resolveLanguage: 'literal',
373
+ setter: [{ action: 'addLink', source: 'this', predicate: 'test://field', target: 'value' }],
374
+ adder: [{ action: 'addLink', source: 'this', predicate: 'test://items', target: 'value' }],
375
+ remover: [{ action: 'removeLink', source: 'this', predicate: 'test://items', target: 'value' }],
376
+ });
377
+
378
+ const json = original.toJSON();
379
+ const reconstructed = SHACLShape.fromJSON(json);
380
+
381
+ const prop = reconstructed.properties[0];
382
+ expect(prop.name).toBe('field');
383
+ expect(prop.path).toBe('test://field');
384
+ expect(prop.datatype).toBe('xsd:string');
385
+ expect(prop.nodeKind).toBe('Literal');
386
+ expect(prop.minCount).toBe(0);
387
+ expect(prop.maxCount).toBe(5);
388
+ expect(prop.minInclusive).toBe(0);
389
+ expect(prop.maxInclusive).toBe(100);
390
+ expect(prop.pattern).toBe('^[a-z]+$');
391
+ expect(prop.hasValue).toBe('default');
392
+ expect(prop.local).toBe(true);
393
+ expect(prop.writable).toBe(true);
394
+ expect(prop.resolveLanguage).toBe('literal');
395
+ expect(prop.setter).toEqual(original.properties[0].setter);
396
+ expect(prop.adder).toEqual(original.properties[0].adder);
397
+ expect(prop.remover).toEqual(original.properties[0].remover);
398
+ });
399
+ });
400
+ });