@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.
Files changed (42) hide show
  1. package/lib/index.cjs +1746 -322
  2. package/lib/index.js +1745 -323
  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 +21 -7
  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 +23 -7
  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
@@ -1,6 +1,7 @@
1
1
  import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
2
2
  import { Subject } from "./Subject";
3
3
  import { capitalize, propertyNameToSetterName, singularToPlural, stringifyObjectLiteral } from "./util";
4
+ import { SHACLShape, SHACLPropertyShape } from "../shacl/SHACLShape";
4
5
 
5
6
  export class PerspectiveAction {
6
7
  action: string
@@ -114,8 +115,8 @@ export function InstanceQuery(options?: InstanceQueryParams) {
114
115
  }
115
116
 
116
117
  // Fallback to SurrealDB (SdnaOnly mode)
117
- // Get all instances first
118
- let allInstances = await perspective.getAllSubjectInstances(subjectClassName)
118
+ // Get all instances first - pass the class constructor, not just the name
119
+ let allInstances = await perspective.getAllSubjectInstances(target)
119
120
 
120
121
  // Filter by where clause if provided
121
122
  if(options && options.where) {
@@ -742,9 +743,9 @@ export function ModelOptions(opts: ModelOptionsOptions) {
742
743
  target: "value",
743
744
  ...(local && { local: true })
744
745
  }]
745
- collectionCode += `collection_adder(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`
746
- collectionCode += `collection_remover(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`
747
- collectionCode += `collection_setter(${uuid}, "${singularToPlural(collection)}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`
746
+ collectionCode += `collection_adder(${uuid}, "${collection}", '${stringifyObjectLiteral(collectionAdderAction)}').\n`
747
+ collectionCode += `collection_remover(${uuid}, "${collection}", '${stringifyObjectLiteral(collectionRemoverAction)}').\n`
748
+ collectionCode += `collection_setter(${uuid}, "${collection}", '${stringifyObjectLiteral(collectionSetterAction)}').\n`
748
749
  }
749
750
 
750
751
  collectionsCode.push(collectionCode)
@@ -769,6 +770,221 @@ export function ModelOptions(opts: ModelOptionsOptions) {
769
770
  }
770
771
  }
771
772
 
773
+ // Generate SHACL shape (W3C standard + AD4M action definitions)
774
+ target.generateSHACL = function() {
775
+ const subjectName = opts.name;
776
+ const obj = target.prototype;
777
+
778
+ // Determine namespace from first property or collection, or use default
779
+ let namespace = "ad4m://";
780
+ const properties = obj.__properties || {};
781
+ const collections = obj.__collections || {};
782
+
783
+ // Try properties first
784
+ if (Object.keys(properties).length > 0) {
785
+ const firstProp = properties[Object.keys(properties)[0]];
786
+ if (firstProp.through) {
787
+ // Extract namespace from through predicate (e.g., "recipe://name" -> "recipe://")
788
+ const match = firstProp.through.match(/^([^:]+:\/\/)/);
789
+ if (match) {
790
+ namespace = match[1];
791
+ }
792
+ }
793
+ }
794
+ // Fall back to collections if no properties
795
+ else if (Object.keys(collections).length > 0) {
796
+ const firstColl = collections[Object.keys(collections)[0]];
797
+ if (firstColl.through) {
798
+ const match = firstColl.through.match(/^([^:]+:\/\/)/);
799
+ if (match) {
800
+ namespace = match[1];
801
+ }
802
+ }
803
+ }
804
+
805
+ // Create SHACL shape
806
+ const shapeUri = `${namespace}${subjectName}Shape`;
807
+ const targetClass = `${namespace}${subjectName}`;
808
+ const shape = new SHACLShape(shapeUri, targetClass);
809
+
810
+ // === Extract Constructor Actions (same logic as generateSDNA) ===
811
+ let constructorActions = [];
812
+ if(obj.subjectConstructor && obj.subjectConstructor.length) {
813
+ constructorActions = constructorActions.concat(obj.subjectConstructor);
814
+ }
815
+
816
+ // === Extract Destructor Actions ===
817
+ let destructorActions = [];
818
+
819
+ // Convert properties to SHACL property shapes
820
+ for (const propName in properties) {
821
+ const propMeta = properties[propName];
822
+
823
+ if (!propMeta.through) continue; // Skip properties without predicates
824
+
825
+ const propShape: SHACLPropertyShape = {
826
+ name: propName, // Property name for generating named URIs
827
+ path: propMeta.through,
828
+ };
829
+
830
+ // Determine datatype from initial value or resolveLanguage
831
+ if (propMeta.resolveLanguage === "literal") {
832
+ // If it resolves via literal language, it's likely a string
833
+ propShape.datatype = "xsd://string";
834
+ } else if (propMeta.initial) {
835
+ // Try to infer from initial value type
836
+ const initialType = typeof obj[propName];
837
+ if (initialType === "number") {
838
+ propShape.datatype = "xsd://integer";
839
+ } else if (initialType === "boolean") {
840
+ propShape.datatype = "xsd://boolean";
841
+ } else if (initialType === "string") {
842
+ propShape.datatype = "xsd://string";
843
+ }
844
+ }
845
+
846
+ // Cardinality constraints
847
+ if (propMeta.required) {
848
+ propShape.minCount = 1;
849
+ }
850
+
851
+ // Single-valued properties get maxCount 1
852
+ // (collections are handled separately below)
853
+ if (!propMeta.collection) {
854
+ propShape.maxCount = 1;
855
+ }
856
+
857
+ // Flag properties have fixed value
858
+ if (propMeta.flag && propMeta.initial) {
859
+ propShape.hasValue = propMeta.initial;
860
+ }
861
+
862
+ // AD4M-specific metadata
863
+ if (propMeta.local !== undefined) {
864
+ propShape.local = propMeta.local;
865
+ }
866
+
867
+ if (propMeta.writable !== undefined) {
868
+ propShape.writable = propMeta.writable;
869
+ }
870
+
871
+ if (propMeta.resolveLanguage) {
872
+ propShape.resolveLanguage = propMeta.resolveLanguage;
873
+ }
874
+
875
+ // === Extract Setter Actions (same logic as generateSDNA) ===
876
+ if (propMeta.setter) {
877
+ // Custom setter defined - not yet supported in SHACL
878
+ console.warn(
879
+ `[SHACL Generation] Custom Prolog setter for property '${propName}' in class '${subjectName}' is not yet supported. ` +
880
+ `The property will be created without setter actions. Consider using standard writable properties or provide explicit SHACL JSON.`
881
+ );
882
+ // TODO: Parse custom Prolog setter to extract actions
883
+ } else if (propMeta.writable && propMeta.through) {
884
+ let setter = obj[propertyNameToSetterName(propName)];
885
+ if (typeof setter === "function") {
886
+ propShape.setter = [{
887
+ action: "setSingleTarget",
888
+ source: "this",
889
+ predicate: propMeta.through,
890
+ target: "value",
891
+ ...(propMeta.local && { local: true })
892
+ }];
893
+ }
894
+ }
895
+
896
+ // Add to constructor actions if property has initial value
897
+ if (propMeta.initial) {
898
+ constructorActions.push({
899
+ action: "addLink",
900
+ source: "this",
901
+ predicate: propMeta.through,
902
+ target: propMeta.initial,
903
+ });
904
+
905
+ // Add to destructor actions
906
+ destructorActions.push({
907
+ action: "removeLink",
908
+ source: "this",
909
+ predicate: propMeta.through,
910
+ target: "*",
911
+ });
912
+ }
913
+
914
+ shape.addProperty(propShape);
915
+ }
916
+
917
+ // Convert collections to SHACL property shapes
918
+ // (collections variable already declared above for namespace inference)
919
+ for (const collName in collections) {
920
+ const collMeta = collections[collName];
921
+
922
+ if (!collMeta.through) continue;
923
+
924
+ const collShape: SHACLPropertyShape = {
925
+ name: collName, // Collection name for generating named URIs
926
+ path: collMeta.through,
927
+ // Collections have no maxCount (unlimited)
928
+ // minCount defaults to 0 (optional)
929
+ };
930
+
931
+ // Determine if it's a reference (IRI) or literal
932
+ // Collections typically contain references (IRIs) to other entities
933
+ // They're literals only if explicitly marked or contain primitive values
934
+ if (collMeta.where?.isInstance) {
935
+ // Collection of typed entities - definitely IRIs
936
+ collShape.nodeKind = 'IRI';
937
+ } else {
938
+ // Default to IRI for collections (most common case)
939
+ // Literal collections are rare and would need explicit marking
940
+ collShape.nodeKind = 'IRI';
941
+ }
942
+
943
+ // AD4M-specific metadata
944
+ if (collMeta.local !== undefined) {
945
+ collShape.local = collMeta.local;
946
+ }
947
+
948
+ if (collMeta.writable !== undefined) {
949
+ collShape.writable = collMeta.writable;
950
+ }
951
+
952
+ // === Extract Collection Actions (adder/remover) ===
953
+ // Adder action - adds a link to the collection
954
+ collShape.adder = [{
955
+ action: "addLink",
956
+ source: "this",
957
+ predicate: collMeta.through,
958
+ target: "value",
959
+ ...(collMeta.local && { local: true })
960
+ }];
961
+
962
+ // Remover action - removes a link from the collection
963
+ collShape.remover = [{
964
+ action: "removeLink",
965
+ source: "this",
966
+ predicate: collMeta.through,
967
+ target: "value",
968
+ ...(collMeta.local && { local: true })
969
+ }];
970
+
971
+ shape.addProperty(collShape);
972
+ }
973
+
974
+ // Set constructor and destructor actions on the shape
975
+ if (constructorActions.length > 0) {
976
+ shape.setConstructorActions(constructorActions);
977
+ }
978
+ if (destructorActions.length > 0) {
979
+ shape.setDestructorActions(destructorActions);
980
+ }
981
+
982
+ return {
983
+ shape,
984
+ name: subjectName
985
+ };
986
+ }
987
+
772
988
  Object.defineProperty(target, 'type', {configurable: true});
773
989
  }
774
990
  }
package/src/model/util.ts CHANGED
@@ -35,11 +35,12 @@ export function collectionToAdderName(collection: string): string {
35
35
  return `add${capitalize(pluralToSingular(collection))}`
36
36
  }
37
37
 
38
- // e.g. "addEntry" -> "entries"
38
+ // e.g. "addComments" -> "comments"
39
39
  export function collectionAdderToName(adderName: string): string {
40
- let singular = adderName.substring(3)
41
- let plural = singularToPlural(singular)
42
- return plural.charAt(0).toLowerCase() + plural.slice(1)
40
+ // Extract the collection name after "add" and lowercase first char
41
+ // The method name already has the plural collection name (e.g., "addComments")
42
+ let collectionName = adderName.substring(3)
43
+ return collectionName.charAt(0).toLowerCase() + collectionName.slice(1)
43
44
  }
44
45
 
45
46
  // e.g. "comments" -> "removeComment"
@@ -47,17 +48,19 @@ export function collectionToRemoverName(collection: string): string {
47
48
  return `remove${capitalize(pluralToSingular(collection))}`
48
49
  }
49
50
 
50
- // e.g. "removeEntry" -> "entries"
51
+ // e.g. "removeComments" -> "comments"
51
52
  export function collectionRemoverToName(removerName: string): string {
52
- let singular = removerName.substring(6)
53
- let plural = singularToPlural(singular)
54
- return plural.charAt(0).toLowerCase() + plural.slice(1)
53
+ // Extract the collection name after "remove" and lowercase first char
54
+ // The method name already has the plural collection name (e.g., "removeComments")
55
+ let collectionName = removerName.substring(6)
56
+ return collectionName.charAt(0).toLowerCase() + collectionName.slice(1)
55
57
  }
56
58
 
57
- export function collectionSetterToName(adderName: string): string {
58
- let singular = adderName.substring(13)
59
- let plural = singularToPlural(singular)
60
- return plural.charAt(0).toLowerCase() + plural.slice(1)
59
+ export function collectionSetterToName(setterName: string): string {
60
+ // Extract the collection name after "setCollection" and lowercase first char
61
+ // The method name already has the plural collection name (e.g., "setCollectionComments")
62
+ let collectionName = setterName.substring(13)
63
+ return collectionName.charAt(0).toLowerCase() + collectionName.slice(1)
61
64
  }
62
65
 
63
66
  // e.g. "comments" -> "addComment"
@@ -430,11 +430,22 @@ export class PerspectiveClient {
430
430
  return perspectiveRemoveLink
431
431
  }
432
432
 
433
- async addSdna(uuid: string, name: string, sdnaCode: string, sdnaType: "subject_class" | "flow" | "custom"): Promise<boolean> {
433
+ /**
434
+ * Adds Social DNA code to a perspective.
435
+ *
436
+ * Preferred usage: pass shaclJson (from SHACLShape.toJSON()) as the primary schema definition.
437
+ * The sdnaCode parameter is kept for backward compatibility but SHACL is the source of truth
438
+ * for all SDNA operations. Prolog engines remain available for complex queries.
439
+ *
440
+ * @param sdnaCode - Legacy Prolog code (pass empty string when using shaclJson)
441
+ * @param shaclJson - SHACL JSON string from SHACLShape.toJSON() (recommended)
442
+ */
443
+ async addSdna(uuid: string, name: string, sdnaCode: string | undefined, sdnaType: "subject_class" | "flow" | "custom", shaclJson?: string): Promise<boolean> {
434
444
  return unwrapApolloResult(await this.#apolloClient.mutate({
435
- mutation: gql`mutation perspectiveAddSdna($uuid: String!, $name: String!, $sdnaCode: String!, $sdnaType: String!) {
436
- perspectiveAddSdna(uuid: $uuid, name: $name, sdnaCode: $sdnaCode, sdnaType: $sdnaType)
437
- }`, variables: { uuid, name, sdnaCode, sdnaType }
445
+ mutation: gql`mutation perspectiveAddSdna($uuid: String!, $name: String!, $sdnaCode: String, $sdnaType: String!, $shaclJson: String) {
446
+ perspectiveAddSdna(uuid: $uuid, name: $name, sdnaCode: $sdnaCode, sdnaType: $sdnaType, shaclJson: $shaclJson)
447
+ }`,
448
+ variables: { uuid, name, sdnaCode: sdnaCode || "", sdnaType, shaclJson }
438
449
  })).perspectiveAddSdna
439
450
  }
440
451