ldkit 2.2.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.
- package/esm/cli.d.ts +3 -0
- package/esm/cli.d.ts.map +1 -0
- package/esm/cli.js +117 -0
- package/{types → esm}/library/asynciterator.d.ts +1 -0
- package/esm/library/asynciterator.d.ts.map +1 -0
- package/{types → esm}/library/decoder.d.ts +1 -0
- package/esm/library/decoder.d.ts.map +1 -0
- package/esm/library/decoder.js +2 -2
- package/{types → esm}/library/encoder.d.ts +2 -1
- package/esm/library/encoder.d.ts.map +1 -0
- package/esm/library/encoder.js +3 -4
- package/{types → esm}/library/engine/mod.d.ts +1 -0
- package/esm/library/engine/mod.d.ts.map +1 -0
- package/{types → esm}/library/engine/query_engine.d.ts +1 -0
- package/esm/library/engine/query_engine.d.ts.map +1 -0
- package/{types → esm}/library/engine/query_engine_proxy.d.ts +1 -0
- package/esm/library/engine/query_engine_proxy.d.ts.map +1 -0
- package/{types → esm}/library/engine/query_resolvers.d.ts +2 -1
- package/esm/library/engine/query_resolvers.d.ts.map +1 -0
- package/{types → esm}/library/engine/types.d.ts +1 -0
- package/esm/library/engine/types.d.ts.map +1 -0
- package/{types → esm}/library/lens/lens.d.ts +1 -0
- package/esm/library/lens/lens.d.ts.map +1 -0
- package/esm/library/lens/mod.d.ts +2 -0
- package/esm/library/lens/mod.d.ts.map +1 -0
- package/{types → esm}/library/lens/query_builder.d.ts +1 -0
- package/esm/library/lens/query_builder.d.ts.map +1 -0
- package/{types → esm}/library/lens/search_helper.d.ts +1 -0
- package/esm/library/lens/search_helper.d.ts.map +1 -0
- package/{types → esm}/library/lens/types.d.ts +1 -0
- package/esm/library/lens/types.d.ts.map +1 -0
- package/{types → esm}/library/lens/update_helper.d.ts +1 -0
- package/esm/library/lens/update_helper.d.ts.map +1 -0
- package/{types → esm}/library/namespace.d.ts +1 -0
- package/esm/library/namespace.d.ts.map +1 -0
- package/{types → esm}/library/options.d.ts +26 -25
- package/esm/library/options.d.ts.map +1 -0
- package/{types → esm}/library/rdf.d.ts +4 -3
- package/esm/library/rdf.d.ts.map +1 -0
- package/{types → esm}/library/schema/data_types.d.ts +13 -2
- package/esm/library/schema/data_types.d.ts.map +1 -0
- package/{types → esm}/library/schema/interface.d.ts +1 -0
- package/esm/library/schema/interface.d.ts.map +1 -0
- package/{types → esm}/library/schema/mod.d.ts +2 -1
- package/esm/library/schema/mod.d.ts.map +1 -0
- package/{types → esm}/library/schema/schema.d.ts +1 -0
- package/esm/library/schema/schema.d.ts.map +1 -0
- package/{types → esm}/library/schema/search.d.ts +1 -0
- package/esm/library/schema/search.d.ts.map +1 -0
- package/{types → esm}/library/schema/utils.d.ts +1 -0
- package/esm/library/schema/utils.d.ts.map +1 -0
- package/{types → esm}/library/sparql/escape.d.ts +1 -0
- package/esm/library/sparql/escape.d.ts.map +1 -0
- package/{types → esm}/library/sparql/mod.d.ts +1 -0
- package/esm/library/sparql/mod.d.ts.map +1 -0
- package/{types → esm}/library/sparql/sparql_expression_builders.d.ts +1 -0
- package/esm/library/sparql/sparql_expression_builders.d.ts.map +1 -0
- package/{types → esm}/library/sparql/sparql_query_builders.d.ts +1 -0
- package/esm/library/sparql/sparql_query_builders.d.ts.map +1 -0
- package/{types → esm}/library/sparql/sparql_shared_builders.d.ts +1 -0
- package/esm/library/sparql/sparql_shared_builders.d.ts.map +1 -0
- package/{types → esm}/library/sparql/sparql_tag.d.ts +1 -0
- package/esm/library/sparql/sparql_tag.d.ts.map +1 -0
- package/{types → esm}/library/sparql/sparql_update_builders.d.ts +1 -0
- package/esm/library/sparql/sparql_update_builders.d.ts.map +1 -0
- package/{types → esm}/library/sparql/stringify.d.ts +1 -0
- package/esm/library/sparql/stringify.d.ts.map +1 -0
- package/esm/library/translator.d.ts +39 -0
- package/esm/library/translator.d.ts.map +1 -0
- package/esm/library/translator.js +62 -0
- package/{types → esm}/library/utils.d.ts +1 -0
- package/esm/library/utils.d.ts.map +1 -0
- package/esm/mod.d.ts +7 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +1 -0
- package/esm/namespaces/dbo.d.ts +11 -0
- package/esm/namespaces/dbo.d.ts.map +1 -0
- package/esm/namespaces/dc.d.ts +11 -0
- package/esm/namespaces/dc.d.ts.map +1 -0
- package/esm/namespaces/dcterms.d.ts +11 -0
- package/esm/namespaces/dcterms.d.ts.map +1 -0
- package/esm/namespaces/foaf.d.ts +11 -0
- package/esm/namespaces/foaf.d.ts.map +1 -0
- package/esm/namespaces/foaf.js +12 -0
- package/esm/namespaces/gr.d.ts +11 -0
- package/esm/namespaces/gr.d.ts.map +1 -0
- package/esm/namespaces/ldkit.d.ts +11 -0
- package/esm/namespaces/ldkit.d.ts.map +1 -0
- package/esm/namespaces/owl.d.ts +11 -0
- package/esm/namespaces/owl.d.ts.map +1 -0
- package/esm/namespaces/rdf.d.ts +11 -0
- package/esm/namespaces/rdf.d.ts.map +1 -0
- package/esm/namespaces/rdfs.d.ts +11 -0
- package/esm/namespaces/rdfs.d.ts.map +1 -0
- package/esm/namespaces/schema.d.ts +11 -0
- package/esm/namespaces/schema.d.ts.map +1 -0
- package/esm/namespaces/sioc.d.ts +11 -0
- package/esm/namespaces/sioc.d.ts.map +1 -0
- package/esm/namespaces/skos.d.ts +11 -0
- package/esm/namespaces/skos.d.ts.map +1 -0
- package/esm/namespaces/xsd.d.ts +11 -0
- package/esm/namespaces/xsd.d.ts.map +1 -0
- package/{types → esm}/namespaces.d.ts +1 -0
- package/esm/namespaces.d.ts.map +1 -0
- package/{types → esm}/rdf.d.ts +1 -0
- package/esm/rdf.d.ts.map +1 -0
- package/esm/scripts/context_to_schema.d.ts +12 -0
- package/esm/scripts/context_to_schema.d.ts.map +1 -0
- package/esm/scripts/context_to_schema.js +152 -0
- package/esm/scripts/schema_to_script.d.ts +19 -0
- package/esm/scripts/schema_to_script.d.ts.map +1 -0
- package/esm/scripts/schema_to_script.js +222 -0
- package/esm/scripts/shex_to_schema.d.ts +6 -0
- package/esm/scripts/shex_to_schema.d.ts.map +1 -0
- package/esm/scripts/shex_to_schema.js +233 -0
- package/esm/scripts/shexj_types.d.ts +537 -0
- package/esm/scripts/shexj_types.d.ts.map +1 -0
- package/esm/scripts/shexj_types.js +2 -0
- package/{types → esm}/sparql.d.ts +1 -0
- package/esm/sparql.d.ts.map +1 -0
- package/package.json +23 -40
- package/script/cli.d.ts +3 -0
- package/script/cli.d.ts.map +1 -0
- package/script/cli.js +119 -0
- package/script/library/asynciterator.d.ts +30 -0
- package/script/library/asynciterator.d.ts.map +1 -0
- package/script/library/decoder.d.ts +7 -0
- package/script/library/decoder.d.ts.map +1 -0
- package/script/library/decoder.js +6 -6
- package/script/library/encoder.d.ts +8 -0
- package/script/library/encoder.d.ts.map +1 -0
- package/script/library/encoder.js +2 -3
- package/script/library/engine/mod.d.ts +3 -0
- package/script/library/engine/mod.d.ts.map +1 -0
- package/script/library/engine/query_engine.d.ts +58 -0
- package/script/library/engine/query_engine.d.ts.map +1 -0
- package/script/library/engine/query_engine_proxy.d.ts +12 -0
- package/script/library/engine/query_engine_proxy.d.ts.map +1 -0
- package/script/library/engine/query_resolvers.d.ts +11 -0
- package/script/library/engine/query_resolvers.d.ts.map +1 -0
- package/script/library/engine/types.d.ts +24 -0
- package/script/library/engine/types.d.ts.map +1 -0
- package/script/library/lens/lens.d.ts +416 -0
- package/script/library/lens/lens.d.ts.map +1 -0
- package/script/library/lens/lens.js +2 -2
- package/script/library/lens/mod.d.ts +2 -0
- package/script/library/lens/mod.d.ts.map +1 -0
- package/script/library/lens/query_builder.d.ts +23 -0
- package/script/library/lens/query_builder.d.ts.map +1 -0
- package/script/library/lens/search_helper.d.ts +22 -0
- package/script/library/lens/search_helper.d.ts.map +1 -0
- package/script/library/lens/types.d.ts +9 -0
- package/script/library/lens/types.d.ts.map +1 -0
- package/script/library/lens/update_helper.d.ts +24 -0
- package/script/library/lens/update_helper.d.ts.map +1 -0
- package/script/library/namespace.d.ts +42 -0
- package/script/library/namespace.d.ts.map +1 -0
- package/script/library/namespace.js +1 -2
- package/script/library/options.d.ts +72 -0
- package/script/library/options.d.ts.map +1 -0
- package/script/library/options.js +3 -4
- package/script/library/rdf.d.ts +78 -0
- package/script/library/rdf.d.ts.map +1 -0
- package/script/library/schema/data_types.d.ts +57 -0
- package/script/library/schema/data_types.d.ts.map +1 -0
- package/script/library/schema/interface.d.ts +88 -0
- package/script/library/schema/interface.d.ts.map +1 -0
- package/script/library/schema/mod.d.ts +6 -0
- package/script/library/schema/mod.d.ts.map +1 -0
- package/script/library/schema/schema.d.ts +38 -0
- package/script/library/schema/schema.d.ts.map +1 -0
- package/script/library/schema/search.d.ts +21 -0
- package/script/library/schema/search.d.ts.map +1 -0
- package/script/library/schema/utils.d.ts +4 -0
- package/script/library/schema/utils.d.ts.map +1 -0
- package/script/library/sparql/escape.d.ts +2 -0
- package/script/library/sparql/escape.d.ts.map +1 -0
- package/script/library/sparql/mod.d.ts +5 -0
- package/script/library/sparql/mod.d.ts.map +1 -0
- package/script/library/sparql/sparql_expression_builders.d.ts +20 -0
- package/script/library/sparql/sparql_expression_builders.d.ts.map +1 -0
- package/script/library/sparql/sparql_query_builders.d.ts +105 -0
- package/script/library/sparql/sparql_query_builders.d.ts.map +1 -0
- package/script/library/sparql/sparql_shared_builders.d.ts +17 -0
- package/script/library/sparql/sparql_shared_builders.d.ts.map +1 -0
- package/script/library/sparql/sparql_tag.d.ts +32 -0
- package/script/library/sparql/sparql_tag.d.ts.map +1 -0
- package/script/library/sparql/sparql_update_builders.d.ts +91 -0
- package/script/library/sparql/sparql_update_builders.d.ts.map +1 -0
- package/script/library/sparql/stringify.d.ts +7 -0
- package/script/library/sparql/stringify.d.ts.map +1 -0
- package/script/library/translator.d.ts +39 -0
- package/script/library/translator.d.ts.map +1 -0
- package/script/library/translator.js +67 -0
- package/script/library/utils.d.ts +2 -0
- package/script/library/utils.d.ts.map +1 -0
- package/script/mod.d.ts +7 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +3 -1
- package/{types → script}/namespaces/dbo.d.ts +1 -0
- package/script/namespaces/dbo.d.ts.map +1 -0
- package/{types → script}/namespaces/dc.d.ts +1 -0
- package/script/namespaces/dc.d.ts.map +1 -0
- package/{types → script}/namespaces/dcterms.d.ts +1 -0
- package/script/namespaces/dcterms.d.ts.map +1 -0
- package/script/namespaces/foaf.d.ts +11 -0
- package/script/namespaces/foaf.d.ts.map +1 -0
- package/script/namespaces/foaf.js +12 -0
- package/{types → script}/namespaces/gr.d.ts +1 -0
- package/script/namespaces/gr.d.ts.map +1 -0
- package/{types → script}/namespaces/ldkit.d.ts +1 -0
- package/script/namespaces/ldkit.d.ts.map +1 -0
- package/{types → script}/namespaces/owl.d.ts +1 -0
- package/script/namespaces/owl.d.ts.map +1 -0
- package/{types → script}/namespaces/rdf.d.ts +1 -0
- package/script/namespaces/rdf.d.ts.map +1 -0
- package/{types → script}/namespaces/rdfs.d.ts +1 -0
- package/script/namespaces/rdfs.d.ts.map +1 -0
- package/{types → script}/namespaces/schema.d.ts +1 -0
- package/script/namespaces/schema.d.ts.map +1 -0
- package/{types → script}/namespaces/sioc.d.ts +1 -0
- package/script/namespaces/sioc.d.ts.map +1 -0
- package/{types → script}/namespaces/skos.d.ts +1 -0
- package/script/namespaces/skos.d.ts.map +1 -0
- package/{types → script}/namespaces/xsd.d.ts +1 -0
- package/script/namespaces/xsd.d.ts.map +1 -0
- package/script/namespaces.d.ts +31 -0
- package/script/namespaces.d.ts.map +1 -0
- package/script/rdf.d.ts +15 -0
- package/script/rdf.d.ts.map +1 -0
- package/script/scripts/context_to_schema.d.ts +12 -0
- package/script/scripts/context_to_schema.d.ts.map +1 -0
- package/script/scripts/context_to_schema.js +155 -0
- package/script/scripts/schema_to_script.d.ts +19 -0
- package/script/scripts/schema_to_script.d.ts.map +1 -0
- package/script/scripts/schema_to_script.js +225 -0
- package/script/scripts/shex_to_schema.d.ts +6 -0
- package/script/scripts/shex_to_schema.d.ts.map +1 -0
- package/script/scripts/shex_to_schema.js +241 -0
- package/script/scripts/shexj_types.d.ts +537 -0
- package/script/scripts/shexj_types.d.ts.map +1 -0
- package/script/scripts/shexj_types.js +3 -0
- package/script/sparql.d.ts +15 -0
- package/script/sparql.d.ts.map +1 -0
- package/types/library/lens/mod.d.ts +0 -1
- package/types/mod.d.ts +0 -5
- 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
|
+
}
|