-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.ts
308 lines (269 loc) · 11.2 KB
/
converter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
type SchemaOptions = {
/**base path to be included on $ref fields */
basePath?: string;
/**properties to ignore */
excludeProperties?: string[];
/**use logger*/
log?: boolean;
};
/**Transform each model a TTL in a separate schema and return a list with all the schemas.
* All sub-models are referenced with $ref */
export function getSchemasFromTtl(validTtlFile: string, options?: SchemaOptions) {
const schemas = [];
const fileText = removeExtraSpacesAndLinebreaks(validTtlFile);
const models = getListOfDotSeparatedModels(fileText);
const prefixes = getPrefixes(models);
models.forEach(model => {
model = model.trim();
if (model.startsWith('@prefix')) return;
model = replacePrefixes(model, prefixes);
const label = getLabel(model);
const title = getShapeName(model);
validateIfTypeIsIgnored(model, `${label}- ${title}`);
const modelSchema = {
id: label,
title: title,
type: 'object',
required: [],
properties: {},
additionalProperties: model.includes('<http://www.w3.org/ns/shacl#closed> true') ? false : true,
};
addPropertiesToSchema(model, options, modelSchema);
schemas.push(modelSchema);
});
if (options?.log) console.log(`Successfully loaded models: ${schemas.map(s => s.id).join(', ')}`);
return schemas;
}
/**Transform a TTL in one unique schema. The first model is the main one, and all the sub-models schemas
* are added directly to the corresponding property, instead of referencing them with $ref (if found in the same TTL) */
export function getUniqueSchemaFromTtl(validTtlFile: string, options?: SchemaOptions) {
const schemas = getSchemasFromTtl(validTtlFile, options);
const mainSchema = schemas[0];
Object.keys(mainSchema.properties).forEach(prop => {
const propRef = mainSchema.properties[prop].$ref?.replace(options?.basePath ?? '', '');
const itemRef = (mainSchema.properties[prop].items)?.$ref?.replace(options?.basePath ?? '', '');
if (propRef) {
mainSchema.properties[prop] = schemas.find(s => s.id == propRef) ?? mainSchema.properties[prop];
} else if (itemRef) {
mainSchema.properties[prop].items = schemas.find(s => s.id == itemRef) ?? mainSchema.properties[prop].items;
} else {
return;
}
});
return mainSchema;
}
// ---------------- Helper Functions ----------------------
function replacePrefixes(model: string, prefixes: object) {
let words = model.replace(/\[/g, '[ ').split(' ');
Object.keys(prefixes).forEach(prefix => {
words = words.map(word => (word.includes(`${prefix}:`) ? `<${word.replace(`${prefix}:`, prefixes[prefix])}>` : word));
});
return words.join(' ').replace(/\[ /g, '[');
}
function getPrefixes(models: string[]) {
const prefixes = {};
models.forEach(model => {
model = model.trim();
if (!model.startsWith('@prefix')) return;
const prefix = model.replace('@prefix ', '').split(':');
prefixes[prefix[0].trim()] = prefix.slice(1).join(':').replace('<', '').replace('>', '').trim();
});
return prefixes;
}
function addPropertiesToSchema(model: string, options: SchemaOptions, schema: any) {
model.split('<http://www.w3.org/ns/shacl#property>').forEach(propLine => {
propLine = propLine.trim();
if (!propLine.startsWith('[')) return;
const { propName, propSchema, isRequired } = parseProperties(propLine, options);
schema.properties[propName] = propSchema;
if (isRequired) (schema.required as string[]).push(propName);
});
options?.excludeProperties.forEach(prop => delete schema.properties[prop]);
}
function parseProperties(
propLine: string,
options: SchemaOptions,
): { propName: string; propSchema: any; isRequired: boolean } {
const propSchema: any = {};
let propName: string;
let isRequired = false;
let isArray = true;
getListOfSemiColonSeparatedProperties(propLine).forEach(x => {
const keyValue = x.trim().split(' ');
const key = keyValue[0];
let value = keyValue.slice(1).join(' ');
switch (key) {
case '<http://www.w3.org/ns/shacl#path>':
value = value.replace('<', '').replace('>', '');
propName = value.includes('#') ? value.split('#')[1] : value.split('/').slice(-1)[0];
break;
case '<http://www.w3.org/ns/shacl#name>':
propSchema.title = value.substring(1, value.length - 1);
break;
case '<http://www.w3.org/ns/shacl#datatype>':
propSchema.type = convertDatatype(value);
break;
case '<http://www.w3.org/ns/shacl#description>':
propSchema.description = value.substring(1, value.length - 1);
break;
case '<http://www.w3.org/ns/shacl#maxExclusive>':
propSchema.exclusiveMaximum = parseFloat(value.replace(/\"/, ''));
break;
case '<http://www.w3.org/ns/shacl#maxInclusive>':
propSchema.maximum = parseFloat(value.replace(/\"/, ''));
break;
case '<http://www.w3.org/ns/shacl#minInclusive>':
propSchema.minimum = parseFloat(value.replace(/\"/, ''));
break;
case '<http://www.w3.org/ns/shacl#minExclusive>':
propSchema.exclusiveMinimum = parseFloat(value.replace(/\"/, ''));
break;
case '<http://www.w3.org/ns/shacl#maxLength>':
propSchema.maxLength = parseFloat(value.replace(/\"/, ''));
break;
case '<http://www.w3.org/ns/shacl#minLength>':
propSchema.minLength = parseFloat(value.replace(/\"/, ''));
break;
case '<http://www.w3.org/ns/shacl#pattern>':
propSchema.pattern = value
.substring(1, value.length - 1)
.replace(/\\\\/g, '\\')
.replace(/\\\\/g, '\\');
break;
case '<http://www.w3.org/ns/shacl#in>':
value = value.trim();
value = value.substring(1, value.length - 1);
value = value.trim();
value = value.substring(1, value.length - 1);
propSchema.enum = value.replace('""', '" "').split('" "');
propSchema.type = 'string';
break;
case '<http://www.w3.org/ns/shacl#minCount>':
const minCount = parseInt(value.replace(/\"/, ''));
if (minCount > 0) isRequired = true;
if (minCount > 0) propSchema.minItems = minCount;
break;
case '<http://www.w3.org/ns/shacl#maxCount>':
const maxCount = parseInt(value.replace(/\"/, ''));
if (maxCount == 1) isArray = false;
if (isArray) propSchema.maxItems = maxCount;
break;
case '<http://www.w3.org/ns/shacl#node>':
propSchema.type = 'object';
propSchema.$ref = (options?.basePath ? options.basePath : '') + getLabel(value);
break;
}
});
if (options && options.excludeProperties.includes(propName)) isRequired = false;
if (isArray) {
propSchema.items = { ...propSchema };
propSchema.type = 'array';
delete propSchema.items.maxItems;
delete propSchema.items.minItems;
delete propSchema.items.description;
delete propSchema.$ref;
} else {
delete propSchema.minItems;
delete propSchema.maxItems;
}
if (!propName) throw { message: `Model has no path and property name could not be defined: ${propLine}` };
return { propName, propSchema, isRequired };
}
function convertDatatype(value: string): string {
switch (value) {
case '<http://www.w3.org/2001/XMLSchema#string>':
return 'string';
case '<http://www.w3.org/2001/XMLSchema#dateTime>':
return 'string';
case '<http://www.w3.org/2001/XMLSchema#anyURI>':
return 'string';
case '<http://www.w3.org/2001/XMLSchema#double>':
return 'number';
case '<http://www.w3.org/2001/XMLSchema#integer>':
return 'integer';
case '<http://www.w3.org/2001/XMLSchema#boolean>':
return 'boolean';
default:
throw { message: 'Invalid data type ' + value.toString() };
}
}
function getListOfDotSeparatedModels(fileText: string) {
const letters = [];
let isInString = false;
let isInBracks = false;
const isNotInFloat = (j: number) => parseInt(fileText.charAt(j + 1)).toString() == 'NaN';
for (let i = 0; i < fileText.length; i++) {
let char = fileText.charAt(i);
if (char == '"') isInString = !isInString;
if (char == '<') isInBracks = true;
if (char == '>') isInBracks = false;
if (char == '.' && !isInString && !isInBracks && isNotInFloat(i)) char = '::MODULES_SEPARATOR::';
letters.push(char);
}
const models = letters.join('').split('::MODULES_SEPARATOR::').slice(0, -1);
return models;
}
function getListOfSemiColonSeparatedProperties(propLine: string) {
propLine = propLine.substring(1, propLine.length - 2);
const letters = [];
let isInString = false;
for (let i = 0; i < propLine.length; i++) {
let char = propLine.charAt(i);
if (char == '"') isInString = !isInString;
if (char == ';' && !isInString) char = '::PROP_SEPARATOR::';
letters.push(char);
}
return letters.join('').split('::PROP_SEPARATOR::').slice(0, -1);
}
function removeExtraSpacesAndLinebreaks(turtle: string) {
const letters = [];
let isInString = false;
for (let i = 0; i < turtle.length; i++) {
let char = turtle.charAt(i);
if (char == '"') isInString = !isInString;
if (char == ';' && !isInString) char = '::SEMICOLON_SEPARATOR::';
letters.push(char);
}
return letters
.join('')
.replace(/::SEMICOLON_SEPARATOR::/g, ' ; ')
.replace(/((#)(?=(?:[^">]|"[^"]*")*$)).*/gm, '') //Remove comments
.replace(/\r?\n/g, '') //Remove line breaks
.replace(/ +(?= )/g, '') //Remove multiple spaces
.replace(/\t/g, '') //Remove tabs
.trim();
}
function getLabel(modelString: string): string {
const fullName = modelString.split(' ')[0].replace('<', '').replace('>', '');
let labelName = fullName.includes('#') ? fullName.split('#')[1] : fullName.split('/').slice(-1)[0];
labelName = labelName.endsWith('Shape') ? labelName.substring(0, labelName.length - 5) : labelName;
return labelName.charAt(0).toLowerCase() + labelName.slice(1);
}
function getShapeName(modelString: string): string {
const model = modelString.split(';');
try {
const fullName = model
.find(x => x.trim().startsWith('<http://www.w3.org/ns/shacl#targetClass>'))
.trim()
.split(' ')[1]
.trim();
return fullName.startsWith('<')
? (fullName.includes('#') ? fullName.split('#')[1] : fullName.split('/').slice(-1)[0]).replace('>', '')
: fullName;
} catch {
throw { message: 'targetClass is required in model' };
}
}
function validateIfTypeIsIgnored(modelString: string, label: string): void {
try {
const ignoresType = modelString
.split(';')
.find(x => x.trim().startsWith('<http://www.w3.org/ns/shacl#ignoredProperties>'))
.trim()
.split(' ')
.find(x => x == '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>');
if (!ignoresType) throw {};
} catch {
throw { message: 'redf:type must be ignored' };
}
}