ldkit 2.3.0 → 2.4.0

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 (239) hide show
  1. package/esm/cli.d.ts +3 -0
  2. package/esm/cli.d.ts.map +1 -0
  3. package/esm/cli.js +117 -0
  4. package/{types → esm}/library/asynciterator.d.ts +1 -0
  5. package/esm/library/asynciterator.d.ts.map +1 -0
  6. package/{types → esm}/library/decoder.d.ts +1 -0
  7. package/esm/library/decoder.d.ts.map +1 -0
  8. package/{types → esm}/library/encoder.d.ts +1 -0
  9. package/esm/library/encoder.d.ts.map +1 -0
  10. package/{types → esm}/library/engine/mod.d.ts +1 -0
  11. package/esm/library/engine/mod.d.ts.map +1 -0
  12. package/{types → esm}/library/engine/query_engine.d.ts +1 -0
  13. package/esm/library/engine/query_engine.d.ts.map +1 -0
  14. package/{types → esm}/library/engine/query_engine_proxy.d.ts +1 -0
  15. package/esm/library/engine/query_engine_proxy.d.ts.map +1 -0
  16. package/{types → esm}/library/engine/query_resolvers.d.ts +2 -1
  17. package/esm/library/engine/query_resolvers.d.ts.map +1 -0
  18. package/{types → esm}/library/engine/types.d.ts +1 -0
  19. package/esm/library/engine/types.d.ts.map +1 -0
  20. package/{types → esm}/library/lens/lens.d.ts +1 -0
  21. package/esm/library/lens/lens.d.ts.map +1 -0
  22. package/esm/library/lens/mod.d.ts +2 -0
  23. package/esm/library/lens/mod.d.ts.map +1 -0
  24. package/{types → esm}/library/lens/query_builder.d.ts +1 -0
  25. package/esm/library/lens/query_builder.d.ts.map +1 -0
  26. package/{types → esm}/library/lens/search_helper.d.ts +1 -0
  27. package/esm/library/lens/search_helper.d.ts.map +1 -0
  28. package/{types → esm}/library/lens/types.d.ts +1 -0
  29. package/esm/library/lens/types.d.ts.map +1 -0
  30. package/{types → esm}/library/lens/update_helper.d.ts +1 -0
  31. package/esm/library/lens/update_helper.d.ts.map +1 -0
  32. package/{types → esm}/library/namespace.d.ts +1 -0
  33. package/esm/library/namespace.d.ts.map +1 -0
  34. package/{types → esm}/library/options.d.ts +26 -25
  35. package/esm/library/options.d.ts.map +1 -0
  36. package/{types → esm}/library/rdf.d.ts +4 -3
  37. package/esm/library/rdf.d.ts.map +1 -0
  38. package/{types → esm}/library/schema/data_types.d.ts +1 -0
  39. package/esm/library/schema/data_types.d.ts.map +1 -0
  40. package/{types → esm}/library/schema/interface.d.ts +1 -0
  41. package/esm/library/schema/interface.d.ts.map +1 -0
  42. package/{types → esm}/library/schema/mod.d.ts +1 -0
  43. package/esm/library/schema/mod.d.ts.map +1 -0
  44. package/{types → esm}/library/schema/schema.d.ts +1 -0
  45. package/esm/library/schema/schema.d.ts.map +1 -0
  46. package/{types → esm}/library/schema/search.d.ts +1 -0
  47. package/esm/library/schema/search.d.ts.map +1 -0
  48. package/{types → esm}/library/schema/utils.d.ts +1 -0
  49. package/esm/library/schema/utils.d.ts.map +1 -0
  50. package/{types → esm}/library/sparql/escape.d.ts +1 -0
  51. package/esm/library/sparql/escape.d.ts.map +1 -0
  52. package/{types → esm}/library/sparql/mod.d.ts +1 -0
  53. package/esm/library/sparql/mod.d.ts.map +1 -0
  54. package/{types → esm}/library/sparql/sparql_expression_builders.d.ts +1 -0
  55. package/esm/library/sparql/sparql_expression_builders.d.ts.map +1 -0
  56. package/{types → esm}/library/sparql/sparql_query_builders.d.ts +1 -0
  57. package/esm/library/sparql/sparql_query_builders.d.ts.map +1 -0
  58. package/{types → esm}/library/sparql/sparql_shared_builders.d.ts +1 -0
  59. package/esm/library/sparql/sparql_shared_builders.d.ts.map +1 -0
  60. package/{types → esm}/library/sparql/sparql_tag.d.ts +1 -0
  61. package/esm/library/sparql/sparql_tag.d.ts.map +1 -0
  62. package/{types → esm}/library/sparql/sparql_update_builders.d.ts +1 -0
  63. package/esm/library/sparql/sparql_update_builders.d.ts.map +1 -0
  64. package/{types → esm}/library/sparql/stringify.d.ts +1 -0
  65. package/esm/library/sparql/stringify.d.ts.map +1 -0
  66. package/{types → esm}/library/translator.d.ts +1 -0
  67. package/esm/library/translator.d.ts.map +1 -0
  68. package/{types → esm}/library/utils.d.ts +1 -0
  69. package/esm/library/utils.d.ts.map +1 -0
  70. package/{types → esm}/mod.d.ts +1 -0
  71. package/esm/mod.d.ts.map +1 -0
  72. package/esm/namespaces/dbo.d.ts +11 -0
  73. package/esm/namespaces/dbo.d.ts.map +1 -0
  74. package/esm/namespaces/dc.d.ts +11 -0
  75. package/esm/namespaces/dc.d.ts.map +1 -0
  76. package/esm/namespaces/dcterms.d.ts +11 -0
  77. package/esm/namespaces/dcterms.d.ts.map +1 -0
  78. package/esm/namespaces/foaf.d.ts +11 -0
  79. package/esm/namespaces/foaf.d.ts.map +1 -0
  80. package/esm/namespaces/foaf.js +12 -0
  81. package/esm/namespaces/gr.d.ts +11 -0
  82. package/esm/namespaces/gr.d.ts.map +1 -0
  83. package/esm/namespaces/ldkit.d.ts +11 -0
  84. package/esm/namespaces/ldkit.d.ts.map +1 -0
  85. package/esm/namespaces/owl.d.ts +11 -0
  86. package/esm/namespaces/owl.d.ts.map +1 -0
  87. package/esm/namespaces/rdf.d.ts +11 -0
  88. package/esm/namespaces/rdf.d.ts.map +1 -0
  89. package/esm/namespaces/rdfs.d.ts +11 -0
  90. package/esm/namespaces/rdfs.d.ts.map +1 -0
  91. package/esm/namespaces/schema.d.ts +11 -0
  92. package/esm/namespaces/schema.d.ts.map +1 -0
  93. package/esm/namespaces/sioc.d.ts +11 -0
  94. package/esm/namespaces/sioc.d.ts.map +1 -0
  95. package/esm/namespaces/skos.d.ts +11 -0
  96. package/esm/namespaces/skos.d.ts.map +1 -0
  97. package/esm/namespaces/xsd.d.ts +11 -0
  98. package/esm/namespaces/xsd.d.ts.map +1 -0
  99. package/{types → esm}/namespaces.d.ts +1 -0
  100. package/esm/namespaces.d.ts.map +1 -0
  101. package/{types → esm}/rdf.d.ts +1 -0
  102. package/esm/rdf.d.ts.map +1 -0
  103. package/esm/scripts/context_to_schema.d.ts +12 -0
  104. package/esm/scripts/context_to_schema.d.ts.map +1 -0
  105. package/esm/scripts/context_to_schema.js +152 -0
  106. package/esm/scripts/schema_to_script.d.ts +19 -0
  107. package/esm/scripts/schema_to_script.d.ts.map +1 -0
  108. package/esm/scripts/schema_to_script.js +222 -0
  109. package/esm/scripts/shex_to_schema.d.ts +6 -0
  110. package/esm/scripts/shex_to_schema.d.ts.map +1 -0
  111. package/esm/scripts/shex_to_schema.js +233 -0
  112. package/esm/scripts/shexj_types.d.ts +537 -0
  113. package/esm/scripts/shexj_types.d.ts.map +1 -0
  114. package/esm/scripts/shexj_types.js +2 -0
  115. package/{types → esm}/sparql.d.ts +1 -0
  116. package/esm/sparql.d.ts.map +1 -0
  117. package/package.json +23 -40
  118. package/script/cli.d.ts +3 -0
  119. package/script/cli.d.ts.map +1 -0
  120. package/script/cli.js +119 -0
  121. package/script/library/asynciterator.d.ts +30 -0
  122. package/script/library/asynciterator.d.ts.map +1 -0
  123. package/script/library/decoder.d.ts +7 -0
  124. package/script/library/decoder.d.ts.map +1 -0
  125. package/script/library/encoder.d.ts +8 -0
  126. package/script/library/encoder.d.ts.map +1 -0
  127. package/script/library/engine/mod.d.ts +3 -0
  128. package/script/library/engine/mod.d.ts.map +1 -0
  129. package/script/library/engine/query_engine.d.ts +58 -0
  130. package/script/library/engine/query_engine.d.ts.map +1 -0
  131. package/script/library/engine/query_engine_proxy.d.ts +12 -0
  132. package/script/library/engine/query_engine_proxy.d.ts.map +1 -0
  133. package/script/library/engine/query_resolvers.d.ts +11 -0
  134. package/script/library/engine/query_resolvers.d.ts.map +1 -0
  135. package/script/library/engine/types.d.ts +24 -0
  136. package/script/library/engine/types.d.ts.map +1 -0
  137. package/script/library/lens/lens.d.ts +416 -0
  138. package/script/library/lens/lens.d.ts.map +1 -0
  139. package/script/library/lens/lens.js +2 -2
  140. package/script/library/lens/mod.d.ts +2 -0
  141. package/script/library/lens/mod.d.ts.map +1 -0
  142. package/script/library/lens/query_builder.d.ts +23 -0
  143. package/script/library/lens/query_builder.d.ts.map +1 -0
  144. package/script/library/lens/search_helper.d.ts +22 -0
  145. package/script/library/lens/search_helper.d.ts.map +1 -0
  146. package/script/library/lens/types.d.ts +9 -0
  147. package/script/library/lens/types.d.ts.map +1 -0
  148. package/script/library/lens/update_helper.d.ts +24 -0
  149. package/script/library/lens/update_helper.d.ts.map +1 -0
  150. package/script/library/namespace.d.ts +42 -0
  151. package/script/library/namespace.d.ts.map +1 -0
  152. package/script/library/namespace.js +1 -2
  153. package/script/library/options.d.ts +72 -0
  154. package/script/library/options.d.ts.map +1 -0
  155. package/script/library/options.js +3 -4
  156. package/script/library/rdf.d.ts +78 -0
  157. package/script/library/rdf.d.ts.map +1 -0
  158. package/script/library/schema/data_types.d.ts +57 -0
  159. package/script/library/schema/data_types.d.ts.map +1 -0
  160. package/script/library/schema/interface.d.ts +88 -0
  161. package/script/library/schema/interface.d.ts.map +1 -0
  162. package/script/library/schema/mod.d.ts +6 -0
  163. package/script/library/schema/mod.d.ts.map +1 -0
  164. package/script/library/schema/schema.d.ts +38 -0
  165. package/script/library/schema/schema.d.ts.map +1 -0
  166. package/script/library/schema/search.d.ts +21 -0
  167. package/script/library/schema/search.d.ts.map +1 -0
  168. package/script/library/schema/utils.d.ts +4 -0
  169. package/script/library/schema/utils.d.ts.map +1 -0
  170. package/script/library/sparql/escape.d.ts +2 -0
  171. package/script/library/sparql/escape.d.ts.map +1 -0
  172. package/script/library/sparql/mod.d.ts +5 -0
  173. package/script/library/sparql/mod.d.ts.map +1 -0
  174. package/script/library/sparql/sparql_expression_builders.d.ts +20 -0
  175. package/script/library/sparql/sparql_expression_builders.d.ts.map +1 -0
  176. package/script/library/sparql/sparql_query_builders.d.ts +105 -0
  177. package/script/library/sparql/sparql_query_builders.d.ts.map +1 -0
  178. package/script/library/sparql/sparql_shared_builders.d.ts +17 -0
  179. package/script/library/sparql/sparql_shared_builders.d.ts.map +1 -0
  180. package/script/library/sparql/sparql_tag.d.ts +32 -0
  181. package/script/library/sparql/sparql_tag.d.ts.map +1 -0
  182. package/script/library/sparql/sparql_update_builders.d.ts +91 -0
  183. package/script/library/sparql/sparql_update_builders.d.ts.map +1 -0
  184. package/script/library/sparql/stringify.d.ts +7 -0
  185. package/script/library/sparql/stringify.d.ts.map +1 -0
  186. package/script/library/translator.d.ts +39 -0
  187. package/script/library/translator.d.ts.map +1 -0
  188. package/script/library/translator.js +3 -4
  189. package/script/library/utils.d.ts +2 -0
  190. package/script/library/utils.d.ts.map +1 -0
  191. package/script/mod.d.ts +7 -0
  192. package/script/mod.d.ts.map +1 -0
  193. package/{types → script}/namespaces/dbo.d.ts +1 -0
  194. package/script/namespaces/dbo.d.ts.map +1 -0
  195. package/{types → script}/namespaces/dc.d.ts +1 -0
  196. package/script/namespaces/dc.d.ts.map +1 -0
  197. package/{types → script}/namespaces/dcterms.d.ts +1 -0
  198. package/script/namespaces/dcterms.d.ts.map +1 -0
  199. package/script/namespaces/foaf.d.ts +11 -0
  200. package/script/namespaces/foaf.d.ts.map +1 -0
  201. package/script/namespaces/foaf.js +12 -0
  202. package/{types → script}/namespaces/gr.d.ts +1 -0
  203. package/script/namespaces/gr.d.ts.map +1 -0
  204. package/{types → script}/namespaces/ldkit.d.ts +1 -0
  205. package/script/namespaces/ldkit.d.ts.map +1 -0
  206. package/{types → script}/namespaces/owl.d.ts +1 -0
  207. package/script/namespaces/owl.d.ts.map +1 -0
  208. package/{types → script}/namespaces/rdf.d.ts +1 -0
  209. package/script/namespaces/rdf.d.ts.map +1 -0
  210. package/{types → script}/namespaces/rdfs.d.ts +1 -0
  211. package/script/namespaces/rdfs.d.ts.map +1 -0
  212. package/{types → script}/namespaces/schema.d.ts +1 -0
  213. package/script/namespaces/schema.d.ts.map +1 -0
  214. package/{types → script}/namespaces/sioc.d.ts +1 -0
  215. package/script/namespaces/sioc.d.ts.map +1 -0
  216. package/{types → script}/namespaces/skos.d.ts +1 -0
  217. package/script/namespaces/skos.d.ts.map +1 -0
  218. package/{types → script}/namespaces/xsd.d.ts +1 -0
  219. package/script/namespaces/xsd.d.ts.map +1 -0
  220. package/script/namespaces.d.ts +31 -0
  221. package/script/namespaces.d.ts.map +1 -0
  222. package/script/rdf.d.ts +15 -0
  223. package/script/rdf.d.ts.map +1 -0
  224. package/script/scripts/context_to_schema.d.ts +12 -0
  225. package/script/scripts/context_to_schema.d.ts.map +1 -0
  226. package/script/scripts/context_to_schema.js +155 -0
  227. package/script/scripts/schema_to_script.d.ts +19 -0
  228. package/script/scripts/schema_to_script.d.ts.map +1 -0
  229. package/script/scripts/schema_to_script.js +225 -0
  230. package/script/scripts/shex_to_schema.d.ts +6 -0
  231. package/script/scripts/shex_to_schema.d.ts.map +1 -0
  232. package/script/scripts/shex_to_schema.js +241 -0
  233. package/script/scripts/shexj_types.d.ts +537 -0
  234. package/script/scripts/shexj_types.d.ts.map +1 -0
  235. package/script/scripts/shexj_types.js +3 -0
  236. package/script/sparql.d.ts +15 -0
  237. package/script/sparql.d.ts.map +1 -0
  238. package/types/library/lens/mod.d.ts +0 -1
  239. package/types/namespaces/foaf.d.ts +0 -10
@@ -0,0 +1,222 @@
1
+ import { dbo, dc, dcterms, foaf, gr, ldkit, owl, rdf, rdfs, schema, sioc, skos, xsd, } from "../namespaces.js";
2
+ const NAMESPACES = [
3
+ dbo,
4
+ dc,
5
+ dcterms,
6
+ foaf,
7
+ gr,
8
+ ldkit,
9
+ owl,
10
+ rdf,
11
+ rdfs,
12
+ schema,
13
+ sioc,
14
+ skos,
15
+ xsd,
16
+ ];
17
+ export function schemaToScript(schemas) {
18
+ const printer = new SchemaPrinter();
19
+ return printer.print(schemas);
20
+ }
21
+ class SchemaPrinter {
22
+ constructor() {
23
+ Object.defineProperty(this, "usedNamespaces", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: new Set()
28
+ });
29
+ Object.defineProperty(this, "space", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: " "
34
+ });
35
+ }
36
+ print(schemas) {
37
+ const orderedSchemas = this.orderSchemasByDependencies(schemas);
38
+ const printedSchemas = [];
39
+ for (const schema of orderedSchemas) {
40
+ this.extractNamespaces(schema);
41
+ const printedSchema = this.printSchema(schema);
42
+ printedSchemas.push(printedSchema);
43
+ }
44
+ if (this.usedNamespaces.size > 0) {
45
+ printedSchemas.unshift(this.printImports());
46
+ }
47
+ return printedSchemas.join("\n");
48
+ }
49
+ orderSchemasByDependencies(schemas) {
50
+ const orderedSchemas = [];
51
+ const processedSchemas = new Set();
52
+ const dependencies = schemas.map((schema) => {
53
+ return {
54
+ schemaName: schema.name,
55
+ dependencies: this.getSchemaDependencies(schema),
56
+ };
57
+ });
58
+ let unresolved = schemas.length;
59
+ while (unresolved > 0) {
60
+ let progress = false;
61
+ for (const info of dependencies) {
62
+ if (processedSchemas.has(info.schemaName)) {
63
+ continue;
64
+ }
65
+ const allDepsProcessed = info.dependencies.every((dep) => processedSchemas.has(dep));
66
+ if (allDepsProcessed) {
67
+ orderedSchemas.push(schemas.find((schema) => schema.name === info.schemaName));
68
+ processedSchemas.add(info.schemaName);
69
+ unresolved--;
70
+ progress = true;
71
+ }
72
+ }
73
+ if (!progress) {
74
+ throw new Error("Circular dependency detected");
75
+ }
76
+ }
77
+ return orderedSchemas;
78
+ }
79
+ getSchemaDependencies(schema) {
80
+ const dependencies = [];
81
+ for (const property of Object.values(schema.properties)) {
82
+ if (property.schemaRef) {
83
+ dependencies.push(property.schemaRef);
84
+ }
85
+ if (property.schema) {
86
+ dependencies.push(...this.getSchemaDependencies(property.schema));
87
+ }
88
+ }
89
+ return dependencies;
90
+ }
91
+ extractNamespace(value) {
92
+ if (value === "@id") {
93
+ this.usedNamespaces.add(this.printPrefix(ldkit));
94
+ return;
95
+ }
96
+ for (const namespace of NAMESPACES) {
97
+ if (value.startsWith(namespace.$iri)) {
98
+ this.usedNamespaces.add(this.printPrefix(namespace));
99
+ return;
100
+ }
101
+ }
102
+ }
103
+ extractNamespaces(schema) {
104
+ for (const typeValue of schema.type) {
105
+ this.extractNamespace(typeValue);
106
+ }
107
+ for (const property of Object.values(schema.properties)) {
108
+ this.extractNamespace(property.id);
109
+ if (property.type) {
110
+ this.extractNamespace(property.type);
111
+ }
112
+ if (property.schema) {
113
+ this.extractNamespaces(property.schema);
114
+ }
115
+ }
116
+ }
117
+ printImports() {
118
+ const namespacesString = Array.from(this.usedNamespaces)
119
+ .toSorted()
120
+ .join(", ");
121
+ return `import { ${namespacesString} } from "ldkit/namespaces";\n`;
122
+ }
123
+ printSchema(schema) {
124
+ const type = this.printType(schema);
125
+ const properties = this.printProperties(schema);
126
+ return [
127
+ `export const ${schema.name} = {`,
128
+ this.indent(type),
129
+ this.indent(properties),
130
+ `} as const;\n`,
131
+ ].filter(Boolean).join("\n");
132
+ }
133
+ printSubSchema(schema) {
134
+ const type = this.printType(schema);
135
+ const properties = this.printProperties(schema);
136
+ return [
137
+ `"@schema": {`,
138
+ this.indent(type),
139
+ this.indent(properties),
140
+ `},`,
141
+ ].filter(Boolean).join("\n");
142
+ }
143
+ printProperties(schema) {
144
+ if (Object.keys(schema.properties).length === 0) {
145
+ return null;
146
+ }
147
+ return Object.entries(schema.properties)
148
+ .map(([key, prop]) => this.printProperty(key, prop))
149
+ .join("\n");
150
+ }
151
+ printProperty(key, prop) {
152
+ if (!prop.schema && !prop.schemaRef &&
153
+ !prop.optional && !prop.array &&
154
+ !prop.multilang && !prop.inverse &&
155
+ (!prop.type || prop.type === xsd.string)) {
156
+ return `${this.printKey(key)}: ${this.printPrefixed(prop.id)},`;
157
+ }
158
+ const builder = [
159
+ `${this.printKey(key)}: {`,
160
+ this.indent(`"@id": ${this.printPrefixed(prop.id)},`),
161
+ ];
162
+ if (prop.type) {
163
+ builder.push(this.indent(`"@type": ${this.printPrefixed(prop.type)},`));
164
+ }
165
+ if (prop.schema) {
166
+ const subSchema = this.printSubSchema(prop.schema);
167
+ builder.push(this.indent(subSchema));
168
+ }
169
+ else if (prop.schemaRef) {
170
+ builder.push(this.indent(`"@schema": ${prop.schemaRef},`));
171
+ }
172
+ const flags = ["optional", "array", "multilang", "inverse"];
173
+ for (const flag of flags) {
174
+ if (prop[flag]) {
175
+ builder.push(this.indent(`"@${flag}": true,`));
176
+ }
177
+ }
178
+ builder.push("},");
179
+ return builder.join("\n");
180
+ }
181
+ printType(schema) {
182
+ if (schema.type.length === 0) {
183
+ return null;
184
+ }
185
+ if (schema.type.length === 1) {
186
+ return `"@type": ${this.printPrefixed(schema.type[0])},`;
187
+ }
188
+ return [
189
+ `"@type": [`,
190
+ ...schema.type.map((type) => ` ${this.printPrefixed(type)},`),
191
+ `],`,
192
+ ].join("\n");
193
+ }
194
+ printPrefix(namespace) {
195
+ return namespace.$prefix.substring(0, namespace.$prefix.length - 1);
196
+ }
197
+ printPrefixed(value) {
198
+ if (value === "@id") {
199
+ return `${this.printPrefix(ldkit)}.IRI`;
200
+ }
201
+ for (const namespace of NAMESPACES) {
202
+ if (value.startsWith(namespace.$iri)) {
203
+ return `${this.printPrefix(namespace)}.${value.substring(namespace.$iri.length)}`;
204
+ }
205
+ }
206
+ return `"${value}"`;
207
+ }
208
+ printKey(key) {
209
+ if (key.match(/^[a-zA-Z0-9_]+$/)) {
210
+ return key;
211
+ }
212
+ return `"${key}"`;
213
+ }
214
+ indent(text) {
215
+ if (text === null) {
216
+ return null;
217
+ }
218
+ return text.split("\n")
219
+ .map((line) => `${this.space}${line}`)
220
+ .join("\n");
221
+ }
222
+ }
@@ -0,0 +1,6 @@
1
+ import { SchemaSpec } from "./schema_to_script.js";
2
+ import type { Schema as ShexSchema } from "./shexj_types.js";
3
+ export declare function shexcToShexj(shexc: string): ShexSchema;
4
+ export declare function shexjToSchema(shexj: ShexSchema): SchemaSpec[];
5
+ export declare function shexcToSchema(shexc: string): SchemaSpec[];
6
+ //# sourceMappingURL=shex_to_schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shex_to_schema.d.ts","sourceRoot":"","sources":["../../src/scripts/shex_to_schema.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgB,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,KAAK,EAEV,MAAM,IAAI,UAAU,EAMrB,MAAM,kBAAkB,CAAC;AAG1B,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAItD;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,EAAE,CAG7D;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,EAAE,CAGzD"}
@@ -0,0 +1,233 @@
1
+ import ShexParser from "@shexjs/parser";
2
+ import { ldkit, rdf } from "../namespaces.js";
3
+ export function shexcToShexj(shexc) {
4
+ // Parse the Shexc string using the ShexJS parser
5
+ const baseIri = "";
6
+ return ShexParser.construct(baseIri).parse(shexc);
7
+ }
8
+ export function shexjToSchema(shexj) {
9
+ const converter = new ShexConverter();
10
+ return converter.process(shexj);
11
+ }
12
+ export function shexcToSchema(shexc) {
13
+ const shexj = shexcToShexj(shexc);
14
+ return shexjToSchema(shexj);
15
+ }
16
+ class ShexConverter {
17
+ constructor() {
18
+ Object.defineProperty(this, "schemas", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: new Map()
23
+ });
24
+ Object.defineProperty(this, "shexj", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "idMapping", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: new Map()
35
+ });
36
+ }
37
+ process(shexj) {
38
+ this.shexj = shexj;
39
+ this.buildIdMapping();
40
+ this.visit(this.shexj, {});
41
+ return [...this.schemas.values()];
42
+ }
43
+ visit(nodeOrRef, context) {
44
+ if (nodeOrRef === undefined) {
45
+ return;
46
+ }
47
+ const isRef = typeof nodeOrRef === "string";
48
+ const node = isRef ? this.idMapping.get(nodeOrRef) : nodeOrRef;
49
+ if (typeof node !== "object" || node === null) {
50
+ return;
51
+ }
52
+ if (Array.isArray(node)) {
53
+ for (const item of node) {
54
+ this.visit(item, context);
55
+ }
56
+ return;
57
+ }
58
+ switch (node.type) {
59
+ case "Schema":
60
+ if (!node.shapes) {
61
+ throw new Error("No shapes found in ShexJ schema");
62
+ }
63
+ this.visit(node.shapes, {});
64
+ break;
65
+ case "ShapeDecl":
66
+ if (context.shapeAnd || context.shapeOr) {
67
+ this.visit(node.shapeExpr, context);
68
+ }
69
+ else if (isRef) {
70
+ context.property.schemaRef = this.getSuffix(node.id);
71
+ }
72
+ else {
73
+ const schema = this.createSchema(node.id);
74
+ this.visit(node.shapeExpr, { schema });
75
+ }
76
+ break;
77
+ case "Shape":
78
+ if (context.property) {
79
+ const schema = this.createAnonymousSchema();
80
+ context.property.schema = schema;
81
+ this.visit(node.expression, {
82
+ ...context,
83
+ property: undefined,
84
+ schema,
85
+ });
86
+ }
87
+ else {
88
+ const schema = context.schema ??
89
+ this.createSchema(node.id);
90
+ this.visit(node.expression, { ...context, schema });
91
+ }
92
+ break;
93
+ case "ShapeAnd": {
94
+ const root = !context.shapeAnd && !context.shapeOr;
95
+ const schema = root ? this.createAnonymousSchema() : context.schema;
96
+ if (root) {
97
+ if (context.property) {
98
+ context.property.schema = schema;
99
+ }
100
+ this.visit(node.shapeExprs, {
101
+ schema,
102
+ shapeAnd: true,
103
+ });
104
+ }
105
+ else {
106
+ this.visit(node.shapeExprs, { ...context, shapeAnd: true, schema });
107
+ }
108
+ break;
109
+ }
110
+ case "ShapeOr": {
111
+ const root = !context.shapeAnd && !context.shapeOr;
112
+ const schema = root ? this.createAnonymousSchema() : context.schema;
113
+ if (root) {
114
+ if (context.property) {
115
+ context.property.schema = schema;
116
+ }
117
+ this.visit(node.shapeExprs, {
118
+ schema,
119
+ shapeOr: true,
120
+ });
121
+ }
122
+ else {
123
+ this.visit(node.shapeExprs, { ...context, shapeOr: true, schema });
124
+ }
125
+ break;
126
+ }
127
+ case "EachOf":
128
+ this.visit(node.expressions, context);
129
+ break;
130
+ case "OneOf":
131
+ this.visit(node.expressions, { ...context, optional: true });
132
+ break;
133
+ case "TripleConstraint": {
134
+ if (node.predicate === rdf.type) {
135
+ if (!context.shapeOr) {
136
+ this.visit(node.valueExpr, { ...context, type: true });
137
+ }
138
+ break;
139
+ }
140
+ const property = {
141
+ id: node.predicate,
142
+ };
143
+ if (node.min === 0 || context.optional || context.shapeOr) {
144
+ property.optional = true;
145
+ }
146
+ if (node.max && node.max > 1 || node.max === -1) {
147
+ property.array = true;
148
+ }
149
+ if (node.inverse) {
150
+ property.inverse = true;
151
+ }
152
+ const name = this.getSuffix(node.predicate);
153
+ context.schema.properties[name] = property;
154
+ this.visit(node.valueExpr, { ...context, property });
155
+ break;
156
+ }
157
+ case "NodeConstraint": {
158
+ if (context.type) {
159
+ if (node.values?.length) {
160
+ context.schema.type.push(node.values[0]);
161
+ }
162
+ }
163
+ else if (context.property) {
164
+ const property = context.property;
165
+ if (node.nodeKind === "iri") { // IRI
166
+ property.type = ldkit.IRI;
167
+ }
168
+ else if (node.datatype) { // literal
169
+ property.type = node.datatype;
170
+ }
171
+ else if (node.values?.length) { // enumeration
172
+ const first = node.values[0];
173
+ if (typeof first === "string") {
174
+ property.type = ldkit.IRI;
175
+ }
176
+ else if (typeof first === "object" && first.type !== undefined) {
177
+ property.type = first.type;
178
+ }
179
+ }
180
+ }
181
+ break;
182
+ }
183
+ }
184
+ }
185
+ createSchema(id) {
186
+ if (this.schemas.has(id)) {
187
+ throw new Error(`Duplicate schema ID: ${id}`);
188
+ }
189
+ const name = this.getSuffix(id);
190
+ const schema = {
191
+ name,
192
+ type: [],
193
+ properties: {},
194
+ };
195
+ this.schemas.set(id, schema);
196
+ return schema;
197
+ }
198
+ createAnonymousSchema() {
199
+ return {
200
+ name: "AnonymousSchema",
201
+ type: [],
202
+ properties: {},
203
+ };
204
+ }
205
+ buildIdMapping() {
206
+ const queue = [this.shexj];
207
+ while (queue.length > 0) {
208
+ const current = queue.shift();
209
+ if (Array.isArray(current)) {
210
+ for (const item of current) {
211
+ queue.push(item);
212
+ }
213
+ }
214
+ else if (typeof current === "object" && current !== null) {
215
+ for (const [key, value] of Object.entries(current)) {
216
+ if (key === "id") {
217
+ this.idMapping.set(value, current);
218
+ }
219
+ else if (typeof value === "object") {
220
+ queue.push(value);
221
+ }
222
+ }
223
+ }
224
+ }
225
+ }
226
+ getSuffix(value) {
227
+ const cutoff = Math.max(value.lastIndexOf("#"), value.lastIndexOf("/"));
228
+ if (cutoff === -1) {
229
+ return value;
230
+ }
231
+ return value.substring(cutoff + 1);
232
+ }
233
+ }