@shaclmate/compiler 2.0.13 → 2.0.14

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 (204) hide show
  1. package/Compiler.d.ts +15 -0
  2. package/Compiler.js +20 -0
  3. package/ShapesGraphToAstTransformer.d.ts +18 -23
  4. package/ShapesGraphToAstTransformer.js +21 -447
  5. package/_ShapesGraphToAstTransformer/NodeShapeAstType.d.ts +3 -0
  6. package/_ShapesGraphToAstTransformer/NodeShapeAstType.js +2 -0
  7. package/_ShapesGraphToAstTransformer/index.d.ts +10 -0
  8. package/_ShapesGraphToAstTransformer/index.js +10 -0
  9. package/_ShapesGraphToAstTransformer/pickLiteral.d.ts +4 -0
  10. package/_ShapesGraphToAstTransformer/pickLiteral.js +18 -0
  11. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.d.ts +4 -0
  12. package/_ShapesGraphToAstTransformer/propertyShapeNodeKinds.js +26 -0
  13. package/_ShapesGraphToAstTransformer/shapeAstName.d.ts +5 -0
  14. package/_ShapesGraphToAstTransformer/shapeAstName.js +28 -0
  15. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.d.ts +6 -0
  16. package/_ShapesGraphToAstTransformer/transformNodeShapeToAstType.js +165 -0
  17. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.d.ts +13 -0
  18. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstCompositeType.js +198 -0
  19. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.d.ts +12 -0
  20. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstIdentifierType.js +29 -0
  21. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.d.ts +12 -0
  22. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstLiteralType.js +43 -0
  23. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.d.ts +6 -0
  24. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstObjectTypeProperty.js +31 -0
  25. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.d.ts +14 -0
  26. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstTermType.js +21 -0
  27. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.d.ts +16 -0
  28. package/_ShapesGraphToAstTransformer/transformPropertyShapeToAstType.js +58 -0
  29. package/ast/Ast.d.ts +5 -0
  30. package/ast/CompositeType.d.ts +13 -0
  31. package/ast/CompositeType.js +2 -0
  32. package/ast/IdentifierType.d.ts +3 -6
  33. package/ast/IntersectionType.d.ts +2 -2
  34. package/ast/ListType.d.ts +53 -0
  35. package/ast/ListType.js +2 -0
  36. package/ast/LiteralType.d.ts +4 -3
  37. package/ast/Name.d.ts +1 -0
  38. package/ast/ObjectCompositeType.d.ts +22 -0
  39. package/ast/ObjectCompositeType.js +2 -0
  40. package/ast/ObjectIntersectionType.d.ts +8 -0
  41. package/ast/ObjectIntersectionType.js +2 -0
  42. package/ast/ObjectType.d.ts +91 -19
  43. package/ast/ObjectType.js +26 -1
  44. package/ast/ObjectUnionType.d.ts +19 -0
  45. package/ast/ObjectUnionType.js +2 -0
  46. package/ast/PlaceholderType.d.ts +7 -0
  47. package/ast/PlaceholderType.js +2 -0
  48. package/ast/SetType.d.ts +1 -1
  49. package/ast/TermType.d.ts +11 -0
  50. package/ast/TermType.js +2 -0
  51. package/ast/Type.d.ts +11 -3
  52. package/ast/UnionType.d.ts +3 -3
  53. package/ast/index.d.ts +6 -3
  54. package/ast/index.js +6 -3
  55. package/enums/MintingStrategy.d.ts +5 -0
  56. package/enums/MintingStrategy.js +2 -0
  57. package/enums/PropertyVisibility.d.ts +2 -0
  58. package/enums/PropertyVisibility.js +2 -0
  59. package/enums/TsFeature.d.ts +5 -0
  60. package/enums/TsFeature.js +16 -0
  61. package/enums/TsObjectDeclarationType.d.ts +5 -0
  62. package/enums/TsObjectDeclarationType.js +2 -0
  63. package/enums/index.d.ts +5 -0
  64. package/enums/index.js +5 -0
  65. package/generators/Generator.d.ts +5 -0
  66. package/generators/Generator.js +2 -0
  67. package/generators/index.d.ts +2 -2
  68. package/generators/index.js +2 -2
  69. package/generators/json/AstJsonGenerator.d.ts +4 -5
  70. package/generators/json/AstJsonGenerator.js +31 -22
  71. package/generators/ts/BooleanType.d.ts +7 -3
  72. package/generators/ts/BooleanType.js +58 -4
  73. package/generators/ts/DateTimeType.d.ts +17 -0
  74. package/generators/ts/DateTimeType.js +58 -0
  75. package/generators/ts/DeclaredType.d.ts +17 -0
  76. package/generators/ts/DeclaredType.js +10 -0
  77. package/generators/ts/IdentifierType.d.ts +7 -17
  78. package/generators/ts/IdentifierType.js +69 -60
  79. package/generators/ts/Import.d.ts +18 -0
  80. package/generators/ts/Import.js +59 -0
  81. package/generators/ts/ListType.d.ts +18 -8
  82. package/generators/ts/ListType.js +197 -43
  83. package/generators/ts/LiteralType.d.ts +13 -11
  84. package/generators/ts/LiteralType.js +36 -51
  85. package/generators/ts/NumberType.d.ts +7 -3
  86. package/generators/ts/NumberType.js +55 -4
  87. package/generators/ts/ObjectType.d.ts +47 -25
  88. package/generators/ts/ObjectType.js +222 -49
  89. package/generators/ts/ObjectUnionType.d.ts +58 -0
  90. package/generators/ts/ObjectUnionType.js +481 -0
  91. package/generators/ts/OptionType.d.ts +12 -4
  92. package/generators/ts/OptionType.js +65 -16
  93. package/generators/ts/PrimitiveType.d.ts +15 -5
  94. package/generators/ts/PrimitiveType.js +20 -29
  95. package/generators/ts/SetType.d.ts +12 -4
  96. package/generators/ts/SetType.js +103 -29
  97. package/generators/ts/StringType.d.ts +8 -3
  98. package/generators/ts/StringType.js +57 -3
  99. package/generators/ts/TermType.d.ts +52 -0
  100. package/generators/ts/TermType.js +234 -0
  101. package/generators/ts/TsGenerator.d.ts +4 -12
  102. package/generators/ts/TsGenerator.js +44 -100
  103. package/generators/ts/Type.d.ts +107 -46
  104. package/generators/ts/Type.js +78 -61
  105. package/generators/ts/TypeFactory.d.ts +4 -5
  106. package/generators/ts/TypeFactory.js +212 -89
  107. package/generators/ts/UnionType.d.ts +19 -6
  108. package/generators/ts/UnionType.js +230 -120
  109. package/generators/ts/_ObjectType/IdentifierProperty.d.ts +27 -7
  110. package/generators/ts/_ObjectType/IdentifierProperty.js +143 -31
  111. package/generators/ts/_ObjectType/Property.d.ts +125 -9
  112. package/generators/ts/_ObjectType/Property.js +25 -3
  113. package/generators/ts/_ObjectType/ShaclProperty.d.ts +23 -5
  114. package/generators/ts/_ObjectType/ShaclProperty.js +100 -26
  115. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.d.ts +24 -6
  116. package/generators/ts/_ObjectType/TypeDiscriminatorProperty.js +75 -13
  117. package/generators/ts/_ObjectType/classDeclaration.d.ts +2 -1
  118. package/generators/ts/_ObjectType/classDeclaration.js +79 -111
  119. package/generators/ts/_ObjectType/createFunctionDeclaration.d.ts +5 -0
  120. package/generators/ts/_ObjectType/createFunctionDeclaration.js +60 -0
  121. package/generators/ts/_ObjectType/equalsFunctionDeclaration.d.ts +2 -1
  122. package/generators/ts/_ObjectType/equalsFunctionDeclaration.js +12 -20
  123. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.d.ts +11 -0
  124. package/generators/ts/_ObjectType/equalsFunctionOrMethodDeclaration.js +70 -0
  125. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.d.ts +4 -0
  126. package/generators/ts/_ObjectType/fromJsonFunctionDeclarations.js +78 -0
  127. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.d.ts +4 -0
  128. package/generators/ts/_ObjectType/fromRdfFunctionDeclarations.js +91 -0
  129. package/generators/ts/_ObjectType/hashFunctionDeclaration.d.ts +3 -3
  130. package/generators/ts/_ObjectType/hashFunctionDeclaration.js +11 -55
  131. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.d.ts +11 -0
  132. package/generators/ts/_ObjectType/hashFunctionOrMethodDeclaration.js +64 -0
  133. package/generators/ts/_ObjectType/index.d.ts +8 -2
  134. package/generators/ts/_ObjectType/index.js +8 -2
  135. package/generators/ts/_ObjectType/interfaceDeclaration.d.ts +3 -2
  136. package/generators/ts/_ObjectType/interfaceDeclaration.js +11 -2
  137. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.d.ts +5 -0
  138. package/generators/ts/_ObjectType/jsonSchemaFunctionDeclaration.js +19 -0
  139. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.d.ts +5 -0
  140. package/generators/ts/_ObjectType/jsonUiSchemaFunctionDeclaration.js +31 -0
  141. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.d.ts +5 -0
  142. package/generators/ts/_ObjectType/jsonZodSchemaFunctionDeclaration.js +37 -0
  143. package/generators/ts/_ObjectType/rdfjsTermExpression.d.ts +6 -0
  144. package/generators/ts/{rdfjsTermExpression.js → _ObjectType/rdfjsTermExpression.js} +3 -1
  145. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.d.ts +4 -0
  146. package/generators/ts/_ObjectType/sparqlFunctionDeclarations.js +101 -0
  147. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.d.ts +5 -0
  148. package/generators/ts/_ObjectType/toJsonFunctionDeclaration.js +19 -0
  149. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.d.ts +10 -0
  150. package/generators/ts/_ObjectType/toJsonFunctionOrMethodDeclaration.js +59 -0
  151. package/generators/ts/_ObjectType/toJsonReturnType.d.ts +3 -0
  152. package/generators/ts/_ObjectType/toJsonReturnType.js +19 -0
  153. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.d.ts +3 -2
  154. package/generators/ts/_ObjectType/toRdfFunctionDeclaration.js +11 -45
  155. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.d.ts +10 -0
  156. package/generators/ts/_ObjectType/toRdfFunctionOrMethodDeclaration.js +67 -0
  157. package/generators/ts/objectInitializer.d.ts +2 -0
  158. package/generators/ts/objectInitializer.js +14 -0
  159. package/generators/ts/tsComment.d.ts +5 -0
  160. package/generators/ts/tsComment.js +11 -0
  161. package/generators/ts/tsName.d.ts +1 -1
  162. package/generators/ts/tsName.js +1 -0
  163. package/index.d.ts +1 -1
  164. package/index.js +1 -1
  165. package/input/NodeShape.d.ts +46 -0
  166. package/input/NodeShape.js +165 -0
  167. package/input/Ontology.d.ts +13 -0
  168. package/input/Ontology.js +30 -0
  169. package/input/PropertyShape.d.ts +15 -0
  170. package/input/PropertyShape.js +34 -0
  171. package/input/Shape.d.ts +4 -0
  172. package/input/Shape.js +2 -0
  173. package/input/ShapesGraph.d.ts +9 -0
  174. package/input/ShapesGraph.js +50 -0
  175. package/input/ancestorClassIris.d.ts +4 -0
  176. package/input/ancestorClassIris.js +21 -0
  177. package/input/descendantClassIris.d.ts +4 -0
  178. package/input/descendantClassIris.js +21 -0
  179. package/input/generated.d.ts +274 -0
  180. package/input/generated.js +1420 -0
  181. package/input/index.d.ts +7 -0
  182. package/input/index.js +7 -0
  183. package/input/tsFeatures.d.ts +10 -0
  184. package/input/tsFeatures.js +42 -0
  185. package/package.json +14 -8
  186. package/ast/MintingStrategy.d.ts +0 -8
  187. package/ast/MintingStrategy.js +0 -9
  188. package/generators/ts/Configuration.d.ts +0 -29
  189. package/generators/ts/Configuration.js +0 -40
  190. package/generators/ts/IntersectionType.d.ts +0 -14
  191. package/generators/ts/IntersectionType.js +0 -26
  192. package/generators/ts/RdfjsTermType.d.ts +0 -25
  193. package/generators/ts/RdfjsTermType.js +0 -46
  194. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.d.ts +0 -4
  195. package/generators/ts/_ObjectType/fromRdfFunctionDeclaration.js +0 -72
  196. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.d.ts +0 -4
  197. package/generators/ts/_ObjectType/sparqlGraphPatternsClassDeclaration.js +0 -46
  198. package/generators/ts/rdfjsTermExpression.d.ts +0 -5
  199. package/vocabularies/dashDataset.d.ts +0 -3
  200. package/vocabularies/dashDataset.js +0 -2370
  201. package/vocabularies/index.d.ts +0 -2
  202. package/vocabularies/index.js +0 -2
  203. package/vocabularies/shaclmate.d.ts +0 -15
  204. package/vocabularies/shaclmate.js +0 -6
@@ -1,119 +1,63 @@
1
- import { Project, StructureKind, } from "ts-morph";
2
- import { Configuration as GlobalConfiguration } from "./Configuration.js";
1
+ import { Project, } from "ts-morph";
2
+ import * as ast from "../../ast/index.js";
3
+ import { ObjectType } from "./ObjectType.js";
4
+ import { ObjectUnionType } from "./ObjectUnionType.js";
3
5
  import { TypeFactory } from "./TypeFactory.js";
4
- import { tsName } from "./tsName.js";
5
6
  export class TsGenerator {
6
- constructor(ast, configuration) {
7
- this.ast = ast;
8
- this.configuration = configuration ?? new TsGenerator.Configuration();
9
- }
10
- generate() {
11
- const astObjectTypes = this.ast.objectTypes.concat();
12
- astObjectTypes.sort((left, right) => {
13
- if (left.ancestorObjectTypes.some((ancestorObjectType) => ancestorObjectType.name.identifier.equals(right.name.identifier))) {
14
- // Right is an ancestor of left, right must come first
15
- return 1;
16
- }
17
- if (right.ancestorObjectTypes.some((ancestorObjectType) => ancestorObjectType.name.identifier.equals(left.name.identifier))) {
18
- // Left is an ancestor of right, left must come first
19
- return -1;
20
- }
21
- // Neither is an ancestor of the other, sort by name
22
- return tsName(left.name).localeCompare(tsName(right.name));
23
- });
7
+ generate(ast_) {
8
+ const sortedAstObjectTypes = ast.ObjectType.toposort(ast_.objectTypes);
24
9
  const project = new Project({
25
10
  useInMemoryFileSystem: true,
26
11
  });
27
12
  const sourceFile = project.createSourceFile("generated.ts");
28
- const typeFactory = new TypeFactory({ configuration: this.configuration });
29
- const objectTypes = astObjectTypes.flatMap((astObjectType) => {
30
- const type = typeFactory.createTypeFromAstType(astObjectType);
31
- return type.kind === "ObjectType" ? [type] : [];
13
+ const typeFactory = new TypeFactory({
14
+ dataFactoryVariable: ast_.tsDataFactoryVariable,
15
+ });
16
+ this.addDeclarations({
17
+ objectTypes: sortedAstObjectTypes.flatMap((astObjectType) => {
18
+ const type = typeFactory.createTypeFromAstType(astObjectType);
19
+ return type instanceof ObjectType ? [type] : [];
20
+ }),
21
+ objectUnionTypes: ast_.objectUnionTypes.flatMap((astObjectUnionType) => {
22
+ const type = typeFactory.createTypeFromAstType(astObjectUnionType);
23
+ return type instanceof ObjectUnionType ? [type] : [];
24
+ }),
25
+ sourceFile,
32
26
  });
33
- this.addDeclarations(objectTypes, sourceFile);
34
27
  sourceFile.saveSync();
35
28
  return project.getFileSystem().readFileSync(sourceFile.getFilePath());
36
29
  }
37
- addDeclarations(objectTypes, sourceFile) {
38
- this.addImportDeclarations(objectTypes, sourceFile);
39
- for (const objectType of objectTypes) {
40
- switch (this.configuration.objectTypeDeclarationType) {
41
- case "class":
42
- sourceFile.addClass(objectType.classDeclaration());
43
- break;
44
- case "interface":
45
- sourceFile.addInterface(objectType.interfaceDeclaration());
46
- break;
47
- }
48
- const moduleStatements = [];
49
- if (this.configuration.features.has("equals") &&
50
- this.configuration.objectTypeDeclarationType === "interface") {
51
- moduleStatements.push(objectType.equalsFunctionDeclaration());
52
- }
53
- if (this.configuration.features.has("fromRdf")) {
54
- moduleStatements.push(objectType.fromRdfFunctionDeclaration());
55
- }
56
- if (this.configuration.features.has("hash")) {
57
- moduleStatements.push(objectType.hashFunctionDeclaration());
58
- }
59
- if (this.configuration.features.has("sparql-graph-patterns")) {
60
- if (objectType.parentObjectTypes.length > 1) {
61
- throw new RangeError(`object type '${objectType.name}' has multiple super object types, can't use with SPARQL graph patterns`);
30
+ addDeclarations({ objectTypes, objectUnionTypes, sourceFile, }) {
31
+ // sourceFile.addStatements(this.configuration.dataFactoryImport);
32
+ sourceFile.addStatements('import { DataFactory as dataFactory } from "n3"');
33
+ // Gather imports
34
+ const imports = [];
35
+ for (const declaredType of [...objectTypes, ...objectUnionTypes]) {
36
+ imports.push(...declaredType.declarationImports);
37
+ }
38
+ // Deduplicate and add imports
39
+ const addedStringImports = new Set();
40
+ const addedStructureImports = [];
41
+ for (const import_ of imports) {
42
+ if (typeof import_ === "string") {
43
+ if (!addedStringImports.has(import_)) {
44
+ sourceFile.addStatements([import_]);
62
45
  }
63
- moduleStatements.push(objectType.sparqlGraphPatternsClassDeclaration());
46
+ addedStringImports.add(import_);
47
+ continue;
64
48
  }
65
- if (this.configuration.features.has("toRdf") &&
66
- this.configuration.objectTypeDeclarationType === "interface") {
67
- moduleStatements.push(objectType.toRdfFunctionDeclaration());
49
+ if (!addedStructureImports.find((addedStructureImport) => addedStructureImport.moduleSpecifier === import_.moduleSpecifier)) {
50
+ sourceFile.addStatements([import_]);
51
+ addedStructureImports.push(import_);
68
52
  }
69
- sourceFile.addModule({
70
- isExported: objectType.export_,
71
- kind: StructureKind.Module,
72
- name: objectType.name,
73
- statements: moduleStatements,
74
- });
75
- }
76
- }
77
- addImportDeclarations(objectTypes, sourceFile) {
78
- sourceFile.addImportDeclaration({
79
- moduleSpecifier: "purify-ts",
80
- namespaceImport: "purify",
81
- });
82
- sourceFile.addImportDeclaration({
83
- isTypeOnly: true,
84
- moduleSpecifier: "@rdfjs/types",
85
- namespaceImport: "rdfjs",
86
- });
87
- sourceFile.addStatements(this.configuration.dataFactoryImport);
88
- if (this.configuration.features.has("equals")) {
89
- sourceFile.addImportDeclaration({
90
- moduleSpecifier: "purify-ts-helpers",
91
- namespaceImport: "purifyHelpers",
92
- });
93
53
  }
94
- if (this.configuration.features.has("fromRdf") ||
95
- this.configuration.features.has("toRdf")) {
96
- sourceFile.addImportDeclaration({
97
- moduleSpecifier: "rdfjs-resource",
98
- namespaceImport: "rdfjsResource",
99
- });
100
- }
101
- if (this.configuration.features.has("sparql-graph-patterns")) {
102
- sourceFile.addImportDeclaration({
103
- moduleSpecifier: "@kos-kit/sparql-builder",
104
- namespaceImport: "sparqlBuilder",
105
- });
106
- }
107
- const typeImportStatements = new Set();
54
+ // Add type declarations
108
55
  for (const objectType of objectTypes) {
109
- for (const importStatement of objectType.importStatements) {
110
- typeImportStatements.add(importStatement);
111
- }
56
+ sourceFile.addStatements(objectType.declarations);
57
+ }
58
+ for (const objectUnionType of objectUnionTypes) {
59
+ sourceFile.addStatements(objectUnionType.declarations);
112
60
  }
113
- sourceFile.addStatements([...typeImportStatements]);
114
61
  }
115
62
  }
116
- (function (TsGenerator) {
117
- TsGenerator.Configuration = GlobalConfiguration;
118
- })(TsGenerator || (TsGenerator = {}));
119
63
  //# sourceMappingURL=TsGenerator.js.map
@@ -1,73 +1,147 @@
1
+ import type { BlankNode, Literal, NamedNode, Variable } from "@rdfjs/types";
1
2
  import { Maybe } from "purify-ts";
2
- import type * as ast from "../../ast";
3
- import type { Configuration } from "./Configuration.js";
3
+ import type { Import } from "./Import.js";
4
+ /**
5
+ * Abstract base class for generating TypeScript expressions and statemenst in the TypeScript generator.
6
+ *
7
+ * Subclasses are used for both property types (c.f., property* methods) and node/object types.
8
+ */
4
9
  export declare abstract class Type {
5
- abstract readonly kind: ast.Type["kind"] | "ListType";
6
- abstract readonly name: string;
7
- protected readonly configuration: Configuration;
8
- constructor({ configuration, }: {
9
- configuration: Configuration;
10
- });
11
10
  /**
12
11
  * Expressions that convert a source type or types to this type. It should include the type itself.
13
12
  */
14
- get conversions(): readonly Type.Conversion[];
13
+ abstract readonly conversions: readonly Type.Conversion[];
15
14
  /**
16
- * A property that discriminates sub-types of this type e.g., termType on RDF/JS terms.
15
+ * A function (reference or declaration) that compares two property values of this type, returning a
16
+ * purifyHelpers.Equatable.EqualsResult.
17
17
  */
18
- get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
18
+ abstract readonly equalsFunction: string;
19
19
  /**
20
- * Imports used by other methods on this type.
20
+ * JSON-compatible returned by propertyToJsonExpression.
21
21
  */
22
- get importStatements(): readonly string[];
22
+ abstract readonly jsonName: string;
23
23
  /**
24
- * An optional sparqlBuilder.GraphPattern expression that's chained to the object of another pattern, such as a list item.
25
- *
26
- * If the type is e.g., an RDF/JS term it won't have additional graph patterns beyond the basic (s, p, o), and this
27
- * method will return nothing.
24
+ * Is a value of this type mutable?
28
25
  */
29
- chainSparqlGraphPatternExpression(_: {
30
- variables: {
31
- subject: string;
32
- };
33
- }): Maybe<Type.SparqlGraphPatternExpression | Type.SparqlGraphPatternsExpression>;
26
+ abstract readonly mutable: boolean;
34
27
  /**
35
- * A function (reference or declaration) that compares two values of this type, returning a
36
- * purifyHelpers.Equatable.EqualsResult.
28
+ * Name of the type.
29
+ */
30
+ abstract readonly name: string;
31
+ /**
32
+ * Imports necessary to use this type.
33
+ */
34
+ abstract readonly useImports: readonly Import[];
35
+ protected readonly dataFactoryVariable: string;
36
+ constructor({ dataFactoryVariable, }: {
37
+ dataFactoryVariable: string;
38
+ });
39
+ /**
40
+ * A property that discriminates sub-types of this type e.g., termType on RDF/JS terms.
37
41
  */
38
- abstract equalsFunction(): string;
42
+ get discriminatorProperty(): Maybe<Type.DiscriminatorProperty>;
43
+ /**
44
+ * An expression that converts a JSON object in the same format as the propertyToJsonExpression to a value of this type.
45
+ */
46
+ abstract fromJsonExpression(parameters: {
47
+ variables: {
48
+ value: string;
49
+ };
50
+ }): string;
39
51
  /**
40
52
  * An expression that converts a rdfjsResource.Resource.Values to an Either of value/values
41
- * of this type.
53
+ * of this type for a property.
42
54
  */
43
55
  abstract fromRdfExpression(parameters: {
44
56
  variables: {
57
+ context: string;
58
+ languageIn: string;
45
59
  predicate: string;
46
60
  resource: string;
47
61
  resourceValues: string;
48
62
  };
49
63
  }): string;
50
64
  /**
51
- * Statements that use hasher.update to hash a value of this type.
65
+ * Statements that use hasher.update to hash a property value of this type.
52
66
  */
53
67
  abstract hashStatements(parameters: {
68
+ depth: number;
54
69
  variables: {
55
70
  hasher: string;
56
71
  value: string;
57
72
  };
58
73
  }): readonly string[];
59
74
  /**
60
- * An sparqlBuilder.GraphPattern expression for a property, typically building a basic graph pattern.
75
+ * Element object for a JSON Forms UI schema.
61
76
  */
62
- propertySparqlGraphPatternExpression({ variables, }: {
77
+ jsonUiSchemaElement(_parameters: {
78
+ variables: {
79
+ scopePrefix: string;
80
+ };
81
+ }): Maybe<string>;
82
+ /**
83
+ * Zod schema for the JSON version of the type (the result of propertyToJson).
84
+ */
85
+ abstract jsonZodSchema(parameters: {
86
+ variables: {
87
+ zod: string;
88
+ };
89
+ }): string;
90
+ /**
91
+ * An array of SPARQL.js CONSTRUCT template triples for a value of this type, as strings (so they can incorporate runtime calls).
92
+ *
93
+ * This method is called in two contexts:
94
+ * (1) By an ObjectType.Property. The property expects a basic graph pattern (subject, property path, property object). The property calls this method to get additional triples in which the propertyObject is the subject. For example, if the type is a nested object, it would include (propertyObject, nestedPredicate, nestedObject) triples.
95
+ * (2) By another Type. For example, ListType calls this method to with the item variable as a subject in order to chain additional patterns on items.
96
+ *
97
+ * Term types with no additional properties should return an empty array.
98
+ */
99
+ sparqlConstructTemplateTriples({ context, variables, }: {
100
+ context: "property";
63
101
  variables: {
64
102
  object: string;
65
103
  predicate: string;
66
104
  subject: string;
105
+ variablePrefix: string;
106
+ };
107
+ } | {
108
+ context: "type";
109
+ variables: {
110
+ subject: string;
111
+ variablePrefix: string;
67
112
  };
68
- }): Type.SparqlGraphPatternExpression | Type.SparqlGraphPatternsExpression;
113
+ }): readonly string[];
69
114
  /**
70
- * An expression that converts a value of this type to one that that can be .add'd to
115
+ * An array of SPARQL.js where patterns for a value of this type, as strings (so they can incorporate runtime calls).
116
+ *
117
+ * See note in sparqlConstructTemplateTriples re: how this method is used.
118
+ */
119
+ sparqlWherePatterns({ context, variables, }: {
120
+ context: "property";
121
+ variables: {
122
+ object: string;
123
+ predicate: string;
124
+ subject: string;
125
+ variablePrefix: string;
126
+ };
127
+ } | {
128
+ context: "type";
129
+ variables: {
130
+ subject: string;
131
+ variablePrefix: string;
132
+ };
133
+ }): readonly string[];
134
+ /**
135
+ * An expression that converts a value of this type to a JSON-LD compatible value. It can assume the presence
136
+ * of the correct JSON-LD context.
137
+ */
138
+ abstract toJsonExpression(parameters: {
139
+ variables: {
140
+ value: string;
141
+ };
142
+ }): string;
143
+ /**
144
+ * An expression that converts a property value of this type to one that that can be .add'd to
71
145
  * an rdfjsResource.Resource.
72
146
  */
73
147
  abstract toRdfExpression(parameters: {
@@ -79,30 +153,17 @@ export declare abstract class Type {
79
153
  value: string;
80
154
  };
81
155
  }): string;
156
+ protected rdfjsTermExpression(rdfjsTerm: Omit<BlankNode, "equals"> | Omit<Literal, "equals"> | Omit<NamedNode, "equals"> | Omit<Variable, "equals">): string;
82
157
  }
83
158
  export declare namespace Type {
84
159
  interface Conversion {
85
160
  readonly conversionExpression: (value: string) => string;
86
- readonly sourceTypeCheckExpression?: (value: string) => string;
161
+ readonly sourceTypeCheckExpression: (value: string) => string;
87
162
  readonly sourceTypeName: string;
88
163
  }
89
164
  interface DiscriminatorProperty {
90
165
  readonly name: string;
91
166
  readonly values: readonly string[];
92
167
  }
93
- class SparqlGraphPatternExpression {
94
- private readonly value;
95
- constructor(value: string);
96
- toSparqlGraphPatternExpression(): this;
97
- toSparqlGraphPatternsExpression(): SparqlGraphPatternExpression;
98
- toString(): string;
99
- }
100
- class SparqlGraphPatternsExpression {
101
- private readonly value;
102
- constructor(value: string);
103
- toSparqlGraphPatternExpression(): SparqlGraphPatternExpression;
104
- toSparqlGraphPatternsExpression(): this;
105
- toString(): string;
106
- }
107
168
  }
108
169
  //# sourceMappingURL=Type.d.ts.map
@@ -1,18 +1,15 @@
1
1
  import { Maybe } from "purify-ts";
2
+ import { invariant } from "ts-invariant";
3
+ import { rdfjsTermExpression } from "./_ObjectType/rdfjsTermExpression.js";
4
+ import { objectInitializer } from "./objectInitializer.js";
5
+ /**
6
+ * Abstract base class for generating TypeScript expressions and statemenst in the TypeScript generator.
7
+ *
8
+ * Subclasses are used for both property types (c.f., property* methods) and node/object types.
9
+ */
2
10
  export class Type {
3
- constructor({ configuration, }) {
4
- this.configuration = configuration;
5
- }
6
- /**
7
- * Expressions that convert a source type or types to this type. It should include the type itself.
8
- */
9
- get conversions() {
10
- return [
11
- {
12
- conversionExpression: (value) => value,
13
- sourceTypeName: this.name,
14
- },
15
- ];
11
+ constructor({ dataFactoryVariable, }) {
12
+ this.dataFactoryVariable = dataFactoryVariable;
16
13
  }
17
14
  /**
18
15
  * A property that discriminates sub-types of this type e.g., termType on RDF/JS terms.
@@ -21,63 +18,83 @@ export class Type {
21
18
  return Maybe.empty();
22
19
  }
23
20
  /**
24
- * Imports used by other methods on this type.
21
+ * Element object for a JSON Forms UI schema.
25
22
  */
26
- get importStatements() {
27
- return [];
23
+ jsonUiSchemaElement(_parameters) {
24
+ return Maybe.empty();
28
25
  }
29
26
  /**
30
- * An optional sparqlBuilder.GraphPattern expression that's chained to the object of another pattern, such as a list item.
27
+ * An array of SPARQL.js CONSTRUCT template triples for a value of this type, as strings (so they can incorporate runtime calls).
28
+ *
29
+ * This method is called in two contexts:
30
+ * (1) By an ObjectType.Property. The property expects a basic graph pattern (subject, property path, property object). The property calls this method to get additional triples in which the propertyObject is the subject. For example, if the type is a nested object, it would include (propertyObject, nestedPredicate, nestedObject) triples.
31
+ * (2) By another Type. For example, ListType calls this method to with the item variable as a subject in order to chain additional patterns on items.
31
32
  *
32
- * If the type is e.g., an RDF/JS term it won't have additional graph patterns beyond the basic (s, p, o), and this
33
- * method will return nothing.
33
+ * Term types with no additional properties should return an empty array.
34
34
  */
35
- chainSparqlGraphPatternExpression(_) {
36
- return Maybe.empty();
35
+ sparqlConstructTemplateTriples({ context, variables, }) {
36
+ switch (context) {
37
+ case "property": {
38
+ const objectPrefix = `${this.dataFactoryVariable}.variable!(`;
39
+ const objectSuffix = ")";
40
+ invariant(variables.object.startsWith(objectPrefix));
41
+ invariant(variables.object.endsWith(objectSuffix));
42
+ return [
43
+ objectInitializer({
44
+ object: variables.object,
45
+ predicate: variables.predicate,
46
+ subject: variables.subject,
47
+ }),
48
+ ].concat(this.sparqlConstructTemplateTriples({
49
+ context: "type",
50
+ variables: {
51
+ subject: variables.object,
52
+ variablePrefix: variables.object.substring(objectPrefix.length, variables.object.length - objectSuffix.length),
53
+ },
54
+ }));
55
+ }
56
+ case "type":
57
+ return [];
58
+ }
37
59
  }
38
60
  /**
39
- * An sparqlBuilder.GraphPattern expression for a property, typically building a basic graph pattern.
61
+ * An array of SPARQL.js where patterns for a value of this type, as strings (so they can incorporate runtime calls).
62
+ *
63
+ * See note in sparqlConstructTemplateTriples re: how this method is used.
40
64
  */
41
- propertySparqlGraphPatternExpression({ variables, }) {
42
- let expression = `sparqlBuilder.GraphPattern.basic(${variables.subject}, ${variables.predicate}, ${variables.object})`;
43
- this.chainSparqlGraphPatternExpression({
44
- variables: { subject: "object" },
45
- }).ifJust((chainSparqlGraphPatternExpression) => {
46
- expression = `sparqlBuilder.GraphPattern.group(${expression}.chainObject(object => ${chainSparqlGraphPatternExpression.toSparqlGraphPatternsExpression()}))`;
47
- });
48
- return new Type.SparqlGraphPatternExpression(expression);
49
- }
50
- }
51
- (function (Type) {
52
- class SparqlGraphPatternExpression {
53
- constructor(value) {
54
- this.value = value;
55
- }
56
- toSparqlGraphPatternExpression() {
57
- return this;
58
- }
59
- toSparqlGraphPatternsExpression() {
60
- return new SparqlGraphPatternExpression(`[${this.value}]`);
61
- }
62
- toString() {
63
- return this.value;
65
+ sparqlWherePatterns({ context, variables, }) {
66
+ switch (context) {
67
+ case "property": {
68
+ const objectPrefix = `${this.dataFactoryVariable}.variable!(`;
69
+ const objectSuffix = ")";
70
+ invariant(variables.object.startsWith(objectPrefix));
71
+ invariant(variables.object.endsWith(objectSuffix));
72
+ return [
73
+ objectInitializer({
74
+ triples: `[${objectInitializer({
75
+ object: variables.object,
76
+ predicate: variables.predicate,
77
+ subject: variables.subject,
78
+ })}]`,
79
+ type: '"bgp"',
80
+ }),
81
+ ].concat(this.sparqlWherePatterns({
82
+ context: "type",
83
+ variables: {
84
+ subject: variables.object,
85
+ variablePrefix: variables.object.substring(objectPrefix.length, variables.object.length - objectSuffix.length),
86
+ },
87
+ }));
88
+ }
89
+ case "type":
90
+ return [];
64
91
  }
65
92
  }
66
- Type.SparqlGraphPatternExpression = SparqlGraphPatternExpression;
67
- class SparqlGraphPatternsExpression {
68
- constructor(value) {
69
- this.value = value;
70
- }
71
- toSparqlGraphPatternExpression() {
72
- return new SparqlGraphPatternExpression(`sparqlBuilder.GraphPattern.group(${this.value})`);
73
- }
74
- toSparqlGraphPatternsExpression() {
75
- return this;
76
- }
77
- toString() {
78
- return this.value;
79
- }
93
+ rdfjsTermExpression(rdfjsTerm) {
94
+ return rdfjsTermExpression({
95
+ dataFactoryVariable: this.dataFactoryVariable,
96
+ rdfjsTerm,
97
+ });
80
98
  }
81
- Type.SparqlGraphPatternsExpression = SparqlGraphPatternsExpression;
82
- })(Type || (Type = {}));
99
+ }
83
100
  //# sourceMappingURL=Type.js.map
@@ -1,12 +1,11 @@
1
- import type * as ast from "../../ast";
2
- import type { Configuration } from "./Configuration";
1
+ import type * as ast from "../../ast/index.js";
3
2
  import type { Type } from "./Type.js";
4
3
  export declare class TypeFactory {
5
4
  private cachedObjectTypePropertiesByIdentifier;
6
5
  private cachedObjectTypesByIdentifier;
7
- private readonly configuration;
8
- constructor({ configuration }: {
9
- configuration: Configuration;
6
+ private readonly dataFactoryVariable;
7
+ constructor({ dataFactoryVariable }: {
8
+ dataFactoryVariable: string;
10
9
  });
11
10
  createTypeFromAstType(astType: ast.Type): Type;
12
11
  private createObjectTypeFromAstType;