-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
901 lines (836 loc) · 37.5 KB
/
convert.js
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
let fs = require('fs');
let inputDataRows = {}; //input data as graph nodes (we call them data rows here)
let errors = []; //error log
let memory_classes = {}; //classes without enumerations, enumeration instances and dataTypes
let memory_properties = {}; //properties
let memory_dataTypes = {}; //dataType classes
let memory_enumerations = {}; //enumeration classes
let memory_enumerationMembers = {}; //enumeration instances
let COUNTER_DATAROW_CLASS_SUPERSEDED = 0; //amount of skipped class data rows because they got superseded
let COUNTER_DATAROW_PROPERTY_SUPERSEDED = 0; //amount of skipped property data rows because they got superseded
let COUNTER_DATAROW_DATATYPE_SUPERSEDED = 0; //amount of skipped dataType data rows because they got superseded
let COUNTER_DATAROW_ENUMERATIONMEMBER_SUPERSEDED = 0; //amount of skipped enumeration data rows because they got superseded
let COUNTER_DATAROW_ERROR = 0; //amount of skipped data rows which contained errors
let outputData_classes = {}; //sdo_classes.jsonld
let outputData_properties = {}; //sdo_properties.jsonld
let outputData_enumerations = {}; //sdo_enumerations.jsonld
let outputData_enumerationMembers = {}; //sdo_enumerationMembers.jsonld
let outputData_dataTypes = {}; //sdo_dataTypes.jsonld
let outputData_classesMaterialized = {};//sdo_classesMaterialized.jsonld
/*
===============
Algorithm Start
===============
*/
let option_version = "latest";
let option_minify = true;
let option_materialize = false;
//programming objects
let PATH_OUTPUT_DIRECTORY = "data_output";
function convert(output_directory, PATH_INPUT_DIRECTORY, version, minify, materialize) {
//set option variables
option_version = version;
option_minify = minify;
option_materialize = materialize;
PATH_OUTPUT_DIRECTORY = output_directory + "/" + version;
if (fs.existsSync(PATH_INPUT_DIRECTORY + "/" + version + "/schema.jsonld")) {
//create output folder if not existent
if (!fs.existsSync(output_directory + "/" + version)) {
fs.mkdirSync(output_directory + "/" + version);
}
fs.readFile(PATH_INPUT_DIRECTORY + "/" + version + "/schema.jsonld", 'utf8', function (err, data) {
inputDataRows = JSON.parse(data);
inputDataRows = inputDataRows["@graph"];
startProcessTransformJSONLD();
});
} else {
print("e", "Could not find input file at '" + PATH_INPUT_DIRECTORY + "/" + version + "/schema.jsonld'. Is your input version correct?");
}
}
module.exports = convert;
//Starts the transformation and generation of data.
function startProcessTransformJSONLD() {
//Algorithm Step B
classifyInput();
//Algorithm Step C.1
extractEnumerationFromClasses();
//Algorithm Step C.2
extractDataTypesFromClasses();
//Algorithm Step C.3
//removeBlackListedClasses(); //commented out on purpose, user may use the function if he wants
//Algorithm Step D.1
inheritance_classes();
inheritance_enumerations();
//Algorithm Step D.2
inheritance_dataTypes();
//Algorithm Step D.3
inheritance_properties();
//Algorithm Step E.1
relationships_classes();
//Algorithm Step E.2
relationships_enumerations();
//Algorithm Step E.3
relationships_fillProperties();
if (option_materialize === false) {
//Algorithm Step F.1
outputCreate_classes();
outputCreate_properties();
outputCreate_dataTypes();
outputCreate_enumerations();
outputCreate_enumerationMembers();
} else {
//Algorithm Step F.2
outputCreate_classesMaterialized();
}
//Algorithm Step G.
if (option_minify === false) {
exportOutputFiles();
} else {
exportMinifiedOutputFiles();
}
exportErrorFiles();
exportMetaFiles();
}
/*
===============
Main Processes
===============
*/
//checks the dataRows and classifies them in the corresponding category
function classifyInput() {
for (let i = 0; i < inputDataRows.length; i++) {
//check @type of the dataRow
let type = inputDataRows[i]["@type"];
//if @type is an Array
if (check_isArray(type)) {
let isDataType = false;
for (let j = 0; j < type.length; j++) {
if (type[j] === "http://schema.org/DataType") {
isDataType = true;
break;
}
}
if (isDataType === true) {
//add to DataType collection
addDataType(inputDataRows[i]);
} else {
//there is no type
errors.push({
"message": "DataRow with multiple @type entries, which is not a DataType.",
"payload": inputDataRows[i]
});
COUNTER_DATAROW_ERROR++;
}
} else if (check_isString(type)) {
//@type is a string
switch (type) {
case "rdfs:Class":
addClass(inputDataRows[i]);
break;
case "rdf:Property":
addProperty(inputDataRows[i]);
break;
case "http://schema.org/DataType":
errors.push({
"message": "DataRow has a @type entry which is DataType (non Array).",
"payload": inputDataRows[i]
});
COUNTER_DATAROW_ERROR++;
break;
default:
//enumeration member
if (type.substring(0, ("http://schema.org/".length)) === "http://schema.org/") {
addEnumerationMember(inputDataRows[i]);
} else {
//expected to be an error in the file
errors.push({
"message": "DataRow has an unknown @type entry.",
"payload": inputDataRows[i]
});
COUNTER_DATAROW_ERROR++;
}
break;
}
} else if (check_isUndefined(type)) {
//there is no type
errors.push({
"message": "DataRow has no @type.",
"payload": inputDataRows[i]
});
COUNTER_DATAROW_ERROR++;
} else {
//type is something else
errors.push({
"message": "DataRow has a @type which is not handled right.",
"payload": inputDataRows[i]
});
COUNTER_DATAROW_ERROR++;
}
}
}
//checks if a data row for a class should create an entry in the memory and executes the process if needed
function addClass(classDataRow) {
if (!check_isUndefined(classDataRow["http://schema.org/supersededBy"])) {
// If this class has the supersededBy property, it should not be added since this class should not be used for new annotations
COUNTER_DATAROW_CLASS_SUPERSEDED++;
return false;
}
let className = classDataRow["rdfs:label"];
if (check_isUndefined(memory_classes[className])) {
//there is not yet a class with this name -> create object
let classObject = {};
classObject.name = classDataRow["rdfs:label"];
classObject.description = classDataRow["rdfs:comment"];
classObject.type = "Class";
classObject.superClasses = [];
classObject.subClasses = [];
let actClassParentsObj = classDataRow["rdfs:subClassOf"];
if (!check_isUndefined(actClassParentsObj)) {
//there are superClasses
let superClasses = [];
if (check_isArray(actClassParentsObj)) {
//array
superClasses = actClassParentsObj;
} else {
//not array -> suppose string
superClasses.push(actClassParentsObj);
}
for (let i = 0; i < superClasses.length; i++) {
let actSuperClass = clipURIString(superClasses[i]["@id"]);
if (actSuperClass !== "") {
classObject.superClasses.push(actSuperClass);
}
}
}
memory_classes[className] = classObject;
} else {
//class with this name already added
errors.push({
"message": "DataRow (Class) has a label which has already been used. (duplicate label)",
"payload": classDataRow
});
COUNTER_DATAROW_ERROR++;
}
}
//checks if a data row for an enumeration member should create an entry in the memory and executes the process if needed
function addEnumerationMember(enumerationMemberDataRow) {
if (!check_isUndefined(enumerationMemberDataRow["http://schema.org/supersededBy"])) {
// If this enumeration has the supersededBy property, it should not be added since this enumeration should not be used for new annotations
COUNTER_DATAROW_ENUMERATIONMEMBER_SUPERSEDED++;
return false;
}
let enumerationName = enumerationMemberDataRow["rdfs:label"];
if (check_isUndefined(memory_enumerationMembers[enumerationName])) {
//there is not yet a class with this name -> create object
let enumerationObject = {};
enumerationObject.name = enumerationMemberDataRow["rdfs:label"];
enumerationObject.description = enumerationMemberDataRow["rdfs:comment"];
enumerationObject.type = "EnumerationMember";
enumerationObject.domainEnumeration = null;
let actClassFor = enumerationMemberDataRow["@type"];
if (!check_isUndefined(actClassFor)) {
//there are classes for this enumeration
enumerationObject.domainEnumeration = clipURIString(actClassFor);
memory_enumerationMembers[enumerationName] = enumerationObject;
} else {
//there is no class for this enumeration
errors.push({
"message": "DataRow (Enumeration) misses a Class for which the enumeration is an instance for.",
"payload": enumerationMemberDataRow
});
}
} else {
//enumeration with this name already added
errors.push({
"message": "DataRow (Enumeration) has a label which has already been used. (duplicate label)",
"payload": enumerationMemberDataRow
});
COUNTER_DATAROW_ERROR++;
}
}
//checks if a data row for a dataType should create an entry in the memory and executes the process if needed
function addDataType(dataTypeDataRow) {
if (!check_isUndefined(dataTypeDataRow["http://schema.org/supersededBy"])) {
// If this dataType has the supersededBy property, it should not be added since this data type should not be used for new annotations
COUNTER_DATAROW_DATATYPE_SUPERSEDED++;
return false;
}
let dataTypeName = dataTypeDataRow["rdfs:label"];
if (check_isUndefined(memory_dataTypes[dataTypeName])) {
//there is not yet a dataType with this name -> create object
let dataTypeObject = {};
dataTypeObject.name = dataTypeDataRow["rdfs:label"];
dataTypeObject.description = dataTypeDataRow["rdfs:comment"];
dataTypeObject.type = "DataType";
dataTypeObject.superClasses = ["DataType"];
dataTypeObject.subClasses = [];
let actClassParentsObj = dataTypeDataRow["rdfs:subClassOf"];
if (!check_isUndefined(actClassParentsObj)) {
//there are superClasses
let superClasses = [];
if (check_isArray(actClassParentsObj)) {
//array
superClasses = actClassParentsObj;
} else {
//not array -> suppose string
superClasses.push(actClassParentsObj);
}
for (let i = 0; i < superClasses.length; i++) {
let actSuperClass = clipURIString(superClasses[i]["@id"]);
if (actSuperClass !== "") {
dataTypeObject.superClasses.push(actSuperClass);
}
}
}
memory_dataTypes[dataTypeName] = dataTypeObject;
} else {
//data type with this name already added
errors.push({
"message": "DataRow (DataType) has a label which has already been used. (duplicate label)",
"payload": dataTypeDataRow
});
COUNTER_DATAROW_ERROR++;
}
}
//checks if a data row for a property should create an entry in the memory and executes the process if needed
function addProperty(propertyDataRow) {
if (!check_isUndefined(propertyDataRow["http://schema.org/supersededBy"])) {
// If this class has the supersededBy property, it should not be added since this property should not be used for new annotations
COUNTER_DATAROW_PROPERTY_SUPERSEDED++;
return false;
}
let propertyName = propertyDataRow["rdfs:label"];
if (check_isUndefined(memory_properties[propertyName])) {
//there is not yet a property with this name -> create object
let propertyObject = {};
propertyObject.name = propertyDataRow["rdfs:label"];
propertyObject.description = propertyDataRow["rdfs:comment"];
propertyObject.type = "Property";
propertyObject.superProperties = [];
propertyObject.subProperties = [];
propertyObject.domainClasses = []; //domain
propertyObject.valueTypes = []; //range
let actPropertyParentsObj = propertyDataRow["rdfs:subPropertyOf"];
if (!check_isUndefined(actPropertyParentsObj)) {
//there are superProperties
let superProperties = [];
if (check_isArray(actPropertyParentsObj)) {
//array
superProperties = actPropertyParentsObj;
} else {
//not array -> suppose string
superProperties.push(actPropertyParentsObj);
}
for (let i = 0; i < superProperties.length; i++) {
let actSuperProperty = clipURIString(superProperties[i]["@id"]);
if (actSuperProperty !== "") {
propertyObject.superProperties.push(actSuperProperty);
}
}
}
let domainsObj = propertyDataRow["http://schema.org/domainIncludes"];
if (!check_isUndefined(domainsObj)) {
//there are domain classes
let domains = [];
if (check_isArray(domainsObj)) {
//array
domains = domainsObj;
} else {
//not array -> suppose string
domains.push(domainsObj);
}
for (let i = 0; i < domains.length; i++) {
propertyObject.domainClasses.push(clipURIString(domains[i]["@id"]));
}
}
let rangeObj = propertyDataRow["http://schema.org/rangeIncludes"];
if (!check_isUndefined(rangeObj)) {
//there are range classes
let ranges = [];
if (check_isArray(rangeObj)) {
//array
ranges = rangeObj;
} else {
//not array -> suppose string
ranges.push(rangeObj);
}
for (let i = 0; i < ranges.length; i++) {
propertyObject.valueTypes.push(clipURIString(ranges[i]["@id"]));
}
}
memory_properties[propertyName] = propertyObject;
} else {
//property with this name already added
errors.push({
"message": "DataRow (Property) has a label which has already been used. (duplicate label)",
"payload": propertyDataRow
});
COUNTER_DATAROW_ERROR++;
}
}
//check if there are subclasses of enumerations which are in the classes data, put them in enumerations data
function extractEnumerationFromClasses() {
let counterChanges = 0;
let keyArray = Object.keys(memory_classes);
for (let i = 0; i < keyArray.length; i++) {
let actClass = memory_classes[keyArray[i]];
for (let j = 0; j < actClass["superClasses"].length; j++) { //only "Thing" has no superClasses
//Enumerations have only 1 superclass -> "Enumeration"
if (actClass["superClasses"][j] === "Enumeration") {
//this class is an enumeration
actClass.type = "Enumeration";
actClass.enumerationMembers = [];
memory_enumerations[keyArray[i]] = actClass;
delete memory_classes[keyArray[i]];
counterChanges++;
break;
}
}
}
if (counterChanges > 0) {
extractEnumerationFromClasses(); //do again until there are no changes
}
}
//check if there are subclasses of dataTypes which are in the classes data, put them in dataType data
function extractDataTypesFromClasses() {
let counterChanges = 0;
let keyArray = Object.keys(memory_classes);
for (let i = 0; i < keyArray.length; i++) {
let actClass = memory_classes[keyArray[i]];
for (let j = 0; j < actClass["superClasses"].length; j++) { //only "Thing" has no superClasses
if (!check_isUndefined(memory_dataTypes[actClass["superClasses"][j]])) {
//this class is subclass of a dataType
actClass.type = "DataType";
memory_dataTypes[keyArray[i]] = actClass;
delete memory_classes[keyArray[i]];
counterChanges++;
break;
}
}
}
if (counterChanges > 0) {
extractDataTypesFromClasses(); //do again until there are no changes
}
}
//remove classes which are black listed (because eg. they belong to meta data, it does not make sense to use them?)
function removeBlackListedClasses() {
let blackListenedClasses = [
"Enumeration", "DataType", "Intangible"
];
let keyArray = Object.keys(memory_classes);
for (let i = 0; i < keyArray.length; i++) {
if (blackListenedClasses.indexOf(keyArray[i]) >= 0) {
//class is blacklisted, remove from classList
delete memory_classes[keyArray[i]];
}
}
}
//check superclasses for all classes. Add these classes as subclasses for the parent class/enumeration
function inheritance_classes() {
let keyArray = Object.keys(memory_classes);
for (let i = 0; i < keyArray.length; i++) {
let actSubClass = memory_classes[keyArray[i]];
for (let j = 0; j < actSubClass["superClasses"].length; j++) {
let actSuperClassName = actSubClass["superClasses"][j];
if (!check_isUndefined(memory_classes[actSuperClassName])) {
//add only if class is available
memory_classes[actSuperClassName]["subClasses"].push(actSubClass.name);
} else if (!check_isUndefined(memory_enumerations[actSuperClassName])) {
//add only if enumeration is available
memory_enumerations[actSuperClassName]["subClasses"].push(actSubClass.name);
} else {
//superclass is not in classes memory nor in enumerations memory
errors.push({
"message": "Class has a superClass which is not in classes memory nor in enumerations memory.",
"payload": actSubClass
});
print("d", " inheritance_classes " + JSON.stringify(actSubClass.name) + " - " + actSuperClassName);
}
}
}
}
//check superclasses for all enumerations. Add these enumerations as subclasses for the parent class/enumeration
function inheritance_enumerations() {
let keyArray = Object.keys(memory_enumerations);
for (let i = 0; i < keyArray.length; i++) {
let actSubClass = memory_enumerations[keyArray[i]];
for (let j = 0; j < actSubClass["superClasses"].length; j++) {
let actSuperClassName = actSubClass["superClasses"][j];
if (!check_isUndefined(memory_classes[actSuperClassName])) {
//add only if class is available
memory_classes[actSuperClassName]["subClasses"].push(actSubClass.name);
} else if (!check_isUndefined(memory_enumerations[actSuperClassName])) {
//add only if enumeration is available
memory_enumerations[actSuperClassName]["subClasses"].push(actSubClass.name);
} else {
//superclass is not in classes memory nor in enumerations memory
errors.push({
"message": "Enumeration has a superClass which is not in classes memory nor in enumerations memory.",
"payload": actSubClass
});
print("d", " inheritance_enumerations " + JSON.stringify(actSubClass.name) + " - " + actSuperClassName);
}
}
}
}
//check superclasses for all dataTypes and add these dataTypes as subclasses for the parent dataTypes/classes (class should be only "DataType")
function inheritance_dataTypes() {
let keyArray = Object.keys(memory_dataTypes);
for (let i = 0; i < keyArray.length; i++) {
let actDataType = memory_dataTypes[keyArray[i]];
for (let j = 0; j < actDataType["superClasses"].length; j++) {
let actSuperClassName = actDataType["superClasses"][j];
if (!check_isUndefined(memory_classes[actSuperClassName])) {
//add only if class is available
memory_classes[actSuperClassName]["subClasses"].push(actDataType.name);
} else if (!check_isUndefined(memory_dataTypes[actSuperClassName])) {
//add only if class is available
memory_dataTypes[actSuperClassName]["subClasses"].push(actDataType.name);
} else {
//superclass is not in dataTypes memory
errors.push({
"message": "DataType has a superClass which is not in dataTypes memory.",
"payload": actDataType
});
print("d", " inheritance_dataTypes " + JSON.stringify(actDataType.name) + " - " + actSuperClassName);
}
}
}
}
//check superProperties for all properties and add these properties as subProperties for the parent properties
function inheritance_properties() {
let keyArray = Object.keys(memory_properties);
for (let i = 0; i < keyArray.length; i++) {
let actProperty = memory_properties[keyArray[i]];
for (let j = 0; j < actProperty["superProperties"].length; j++) {
let actSuperPropertyName = actProperty["superProperties"][j];
if (!check_isUndefined(memory_properties[actSuperPropertyName])) {
//add only if class is available
memory_properties[actSuperPropertyName]["subProperties"].push(actProperty.name);
} else {
//superProperty is not in properties memory
errors.push({
"message": "Property has a superProperty which is not in properties memory.",
"payload": actProperty
});
print("d", " inheritance_properties " + JSON.stringify(actProperty.name) + " - " + actSuperPropertyName);
}
}
}
}
//check relationships for classes. Add properties fields
function relationships_classes() {
//part 1: create properties field for classes
let keyArray1 = Object.keys(memory_classes);
for (let i = 0; i < keyArray1.length; i++) {
memory_classes[keyArray1[i]]["properties"] = [];
}
}
//check relationships for enumerations. Add values to enumerationMembers field. Add properties field
function relationships_enumerations() {
let keyArray = Object.keys(memory_enumerationMembers);
for (let i = 0; i < keyArray.length; i++) {
//check the domain enumerations for all enumerationMembers
let actEnumerationMember = memory_enumerationMembers[keyArray[i]];
if (!check_isUndefined(memory_enumerations[actEnumerationMember.domainEnumeration])) {
//add only if class is available
memory_enumerations[actEnumerationMember.domainEnumeration]["enumerationMembers"].push(keyArray[i]);
} else {
//there is no enumeration with this name
errors.push({
"message": "EnumerationMember has a domain which is not in enumeration memory",
"payload": actEnumerationMember
});
//print("d"," relationships_enumerations "+JSON.stringify(actEnumerationMember.name)+" - "+actEnumerationMember.domainEnumeration);
}
}
keyArray = Object.keys(memory_enumerations);
for (let i = 0; i < keyArray.length; i++) {
//add properties field for enumerations
memory_enumerations[keyArray[i]]["properties"] = [];
}
}
//check relationships for properties. Add properties to their domains.
function relationships_fillProperties() {
//part 2: fill properties field
let keyArray = Object.keys(memory_properties);
for (let i = 0; i < keyArray.length; i++) {
//check the domainClasses for all properties
let actProperty = memory_properties[keyArray[i]];
for (let j = 0; j < actProperty["domainClasses"].length; j++) {
let actDomainName = actProperty["domainClasses"][j];
if (!check_isUndefined(memory_classes[actDomainName])) {
//add only if class is available
memory_classes[actDomainName]["properties"].push(keyArray[i]);
} else if (!check_isUndefined(memory_enumerations[actDomainName])) {
//add only if class is available
memory_enumerations[actDomainName]["properties"].push(keyArray[i]);
} else {
//there is no class with this name
errors.push({
"message": "Property has a domain which is not in classes memory",
"payload": actProperty
});
//print("d"," relationships_classes "+JSON.stringify(actProperty.name)+" - "+actDomainName);
}
}
}
}
//create the output data for the output file "sdo_classes.json"
function outputCreate_classes() {
//this function can be seen as an template if there may be a need to rearrange/rename data fields
let keyArray = Object.keys(memory_classes);
for (let i = 0; i < keyArray.length; i++) {
outputData_classes[keyArray[i]] = memory_classes[keyArray[i]];
}
}
//create the output data for the output file "sdo_properties.json"
function outputCreate_properties() {
//this function can be seen as an template if there may be a need to rearrange/rename data fields
let keyArray = Object.keys(memory_properties);
for (let i = 0; i < keyArray.length; i++) {
outputData_properties[keyArray[i]] = memory_properties[keyArray[i]];
}
}
//create the output data for the output file "sdo_dataTypes.json"
function outputCreate_dataTypes() {
//this function can be seen as an template if there may be a need to rearrange/rename data fields
let keyArray = Object.keys(memory_dataTypes);
for (let i = 0; i < keyArray.length; i++) {
outputData_dataTypes[keyArray[i]] = memory_dataTypes[keyArray[i]];
}
}
//create the output data for the output file "sdo_enumerations.json"
function outputCreate_enumerations() {
//this function can be seen as an template if there may be a need to rearrange/rename data fields
let keyArray = Object.keys(memory_enumerations);
for (let i = 0; i < keyArray.length; i++) {
outputData_enumerations[keyArray[i]] = memory_enumerations[keyArray[i]];
}
}
//create the output data for the output file "sdo_enumerationMembers.json"
function outputCreate_enumerationMembers() {
//this function can be seen as an template if there may be a need to rearrange/rename data fields
let keyArray = Object.keys(memory_enumerationMembers);
for (let i = 0; i < keyArray.length; i++) {
outputData_enumerationMembers[keyArray[i]] = memory_enumerationMembers[keyArray[i]];
}
}
//create the output data for the output file "sdo_classesMaterialized.json"
function outputCreate_classesMaterialized() {
//add classes
let keyArray = Object.keys(memory_classes);
for (let i = 0; i < keyArray.length; i++) {
outputData_classesMaterialized[keyArray[i]] = cloneJSON(memory_classes[keyArray[i]]);
}
//add enumerations
keyArray = Object.keys(memory_enumerations);
for (let i = 0; i < keyArray.length; i++) {
outputData_classesMaterialized[keyArray[i]] = cloneJSON(memory_enumerations[keyArray[i]]);
}
//add dataTypes
keyArray = Object.keys(memory_dataTypes);
for (let i = 0; i < keyArray.length; i++) {
outputData_classesMaterialized[keyArray[i]] = cloneJSON(memory_dataTypes[keyArray[i]]);
}
//inheritance of properties from superclasses
keyArray = Object.keys(outputData_classesMaterialized);
for (let i = 0; i < keyArray.length; i++) {
if (outputData_classesMaterialized[keyArray[i]].type === "Class" || outputData_classesMaterialized[keyArray[i]].type === "Enumeration") {
let properties = outputData_classesMaterialized[keyArray[i]].properties;
let superClasses = outputData_classesMaterialized[keyArray[i]].superClasses;
outputData_classesMaterialized[keyArray[i]].properties = uniquifyArray(rec_retrievePropertiesOfParents(superClasses, properties));
}
}
//materialize properties
keyArray = Object.keys(outputData_classesMaterialized);
for (let i = 0; i < keyArray.length; i++) {
if (outputData_classesMaterialized[keyArray[i]].type === "Class" || outputData_classesMaterialized[keyArray[i]].type === "Enumeration") {
let properties = outputData_classesMaterialized[keyArray[i]].properties;
let materializedProperties = [];
for (let j = 0; j < properties.length; j++) {
materializedProperties.push(memory_properties[properties[j]]);
}
outputData_classesMaterialized[keyArray[i]].properties = materializedProperties;
}
}
//materialize enumerationMembers
keyArray = Object.keys(outputData_classesMaterialized);
for (let i = 0; i < keyArray.length; i++) {
if (outputData_classesMaterialized[keyArray[i]].type === "Enumeration") {
let enumerationMembers = outputData_classesMaterialized[keyArray[i]].enumerationMembers;
let materializedEnumerationMembers = [];
for (let j = 0; j < enumerationMembers.length; j++) {
materializedEnumerationMembers.push(memory_enumerationMembers[enumerationMembers[j]]);
}
outputData_classesMaterialized[keyArray[i]].enumerationMembers = materializedEnumerationMembers;
}
}
}
//recursive function which retrieves the properties of all superClasses of a Class/DataType/Enumeration
//superClasses and resultContainer are Arrays
function rec_retrievePropertiesOfParents(superClasses, resultContainer) {
if (superClasses.length === 0) {
return resultContainer;
}
for (let i = 0; i < superClasses.length; i++) {
let actSuperClass = outputData_classesMaterialized[superClasses[i]];
if (actSuperClass !== undefined) {
//add properties of superclass
if (check_isArray(actSuperClass.properties)) {
if (actSuperClass.length !== 0) {
resultContainer.push.apply(resultContainer, actSuperClass.properties);
}
}
//add properties of superclasses of superclass (recursive)
rec_retrievePropertiesOfParents(actSuperClass.superClasses, resultContainer)
} else {
errors.push({
"message": "Entry in outputData_classesMaterialized has a superClass which is not in the set.",
"payload": superClasses[i]
});
console.log("d", "rec_retrievePropertiesOfParents " + superClasses[i]);
}
}
return resultContainer;
}
//export the output data files
function exportOutputFiles() {
if (option_materialize === false) {
writeDataInLocalFile("sdo_classes.json", stringifyJSON(outputData_classes));
writeDataInLocalFile("sdo_properties.json", stringifyJSON(outputData_properties));
writeDataInLocalFile("sdo_dataTypes.json", stringifyJSON(outputData_dataTypes));
writeDataInLocalFile("sdo_enumerations.json", stringifyJSON(outputData_enumerations));
writeDataInLocalFile("sdo_enumerationMembers.json", stringifyJSON(outputData_enumerationMembers));
} else {
writeDataInLocalFile("sdo_classesMaterialized.json", stringifyJSON(outputData_classesMaterialized));
}
}
//export a minified version of the output data files
function exportMinifiedOutputFiles() {
if (option_materialize === false) {
writeDataInLocalFile("sdo_classes.json", JSON.stringify(outputData_classes, null, 0));
writeDataInLocalFile("sdo_properties.json", JSON.stringify(outputData_properties, null, 0));
writeDataInLocalFile("sdo_dataTypes.json", JSON.stringify(outputData_dataTypes, null, 0));
writeDataInLocalFile("sdo_enumerations.json", JSON.stringify(outputData_enumerations, null, 0));
writeDataInLocalFile("sdo_enumerationMembers.json", JSON.stringify(outputData_enumerationMembers, null, 0));
} else {
writeDataInLocalFile("sdo_classesMaterialized.json", JSON.stringify(outputData_classesMaterialized, null, 0));
}
}
//export the error files
function exportErrorFiles() {
writeDataInLocalFile("ErrorLog.txt", "Amount of Errors: " + errors.length + "\n\n" + stringifyJSON(errors));
}
//export the meta data as file
function exportMetaFiles() {
let outputString = "";
outputString = outputString.concat("Amount of @graph nodes: " + inputDataRows.length);
outputString = outputString.concat("\nAmount of @graph nodes with errors: " + COUNTER_DATAROW_ERROR);
outputString = outputString.concat("\n\nAmount of Classes: " + Object.keys(memory_classes).length);
outputString = outputString.concat("\nAmount of Properties: " + Object.keys(memory_properties).length);
outputString = outputString.concat("\nAmount of DataTypes: " + Object.keys(memory_dataTypes).length);
outputString = outputString.concat("\nAmount of Enumerations: " + Object.keys(memory_enumerations).length);
outputString = outputString.concat("\nAmount of EnumerationMembers: " + Object.keys(memory_enumerationMembers).length);
outputString = outputString.concat("\n\nAmount of Classes (including Enumerations) superseded: " + COUNTER_DATAROW_CLASS_SUPERSEDED);
outputString = outputString.concat("\nAmount of Properties superseded: " + COUNTER_DATAROW_PROPERTY_SUPERSEDED);
outputString = outputString.concat("\nAmount of DataTypes superseded: " + COUNTER_DATAROW_DATATYPE_SUPERSEDED);
outputString = outputString.concat("\nAmount of EnumerationMembers superseded: " + COUNTER_DATAROW_ENUMERATIONMEMBER_SUPERSEDED);
writeDataInLocalFile("Log.txt", outputString);
}
/*
===============
Helper functions
===============
*/
function uniquifyArray(array) {
let seen = {};
let result = [];
for (let i = 0; i < array.length; i++) {
let item = array[i];
if (seen[item] !== 1) {
seen[item] = 1;
result.push(item);
}
}
return result;
}
function print(msgType, msg) {
switch (msgType) {
case "i":
console.log("Info: " + msg);
break;
case "w":
console.log("Warning: " + msg);
break;
case "e":
console.log("Error: " + msg);
break;
case "d":
console.log("Debug: " + msg);
break;
}
}
function stringifyJSON(data) {
return JSON.stringify(data, null, 2);
}
let contextURI = "http://schema.org/";
function clipURIString(str) {
return str.substr(contextURI.length, str.length - 1);
}
function check_isArray(object) {
return Array.isArray(object);
}
function check_isUndefined(object) {
return object === undefined;
}
function check_isString(object) {
return typeof object === "string"
}
function writeDataInLocalFile(outputPath, data) {
console.log("writing data into " + PATH_OUTPUT_DIRECTORY + "/" + outputPath + " .");
fs.writeFileSync(PATH_OUTPUT_DIRECTORY + "/" + outputPath, data, 'utf-8');
}
function cloneJSON(json) {
return JSON.parse(JSON.stringify(json));
}
/*
===============
Debug functions
===============
*/
function outputMemory() {
writeDataInLocalFile("memory_classes.json", stringifyJSON(memory_classes));
writeDataInLocalFile("memory_properties.json", stringifyJSON(memory_properties));
writeDataInLocalFile("memory_dataTypes.json", stringifyJSON(memory_dataTypes));
writeDataInLocalFile("memory_enumerations.json", stringifyJSON(memory_enumerations));
writeDataInLocalFile("memory_enumerationMembers.json", stringifyJSON(memory_enumerationMembers));
}
function printOccurrencesNumberOfPropertyOfArray(array, property) {
let resultArr = {};
for (let i = 0; i < array.length; i++) {
let actType = array[i][property];
if (resultArr[actType] > 0) {
resultArr[actType] = resultArr[actType] + 1;
} else {
resultArr[actType] = 1;
}
}
print("d", stringifyJSON(resultArr));
}
function printArrayOccurrences(array, property) {
for (let i = 0; i < array.length; i++) {
let actDataRow = array[i];
if (Array.isArray(actDataRow[property])) {
print("d", "Object '" + JSON.stringify(actDataRow) + "' has array for " + property);
}
}
}
function printAmountOfPropertiesForObject(arr, name) {
print("d", "Object '" + name + "' has " + Object.keys(arr).length + " properties.");
}
function printErrorLog(errors) {
for (let i = 0; i < errors.length; i++) {
print("d", errors[i].message + "\nPayload: " + JSON.stringify(errors[i].payload));
}
}