-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformscript.js
320 lines (255 loc) · 9.94 KB
/
transformscript.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
/* load the rdf mapping and vocab into the script */
.load rdfMapping.js
.load rdfVocab.js
/* Containers for transformation data */
var vocab = [];
var headings = {};
var buffer;
/*Make vocabs for dataset*/
for(var i = 0; i < data_vocab.length; i++){
if(i == 0){vocab = [];}
var key = data_vocab[i].name;
vocab[key] = data_vocab[i];
}
/*Read function of the csv file*/
function read(input) {
buffer = [];
fs.readFile(input, "utf-8", (err, data) => {
if (err) throw err;
// console.log(data);
buffer = data.split('\n');
console.log(buffer.length);
//Make arrays of each line
for (var i = 0; i < buffer.length; i++) {
buffer[i] = buffer[i].split(',');
var buffer2 = [];
var string = "";
var makingstring = false;
for(var j = 0; j < buffer[i].length; j++){
if(!makingstring){
if(buffer[i][j].charAt(0) == '"'){
makingstring = true;
}else{
buffer2.push(buffer[i][j]);
}
}
if(buffer[i][j].charAt(buffer[i][j].length-1) == '"'){
string += buffer[i][j];
buffer2.push(string.substring(1, string.length -1));
string = "";
makingstring = false;
}else if(makingstring){
string += buffer[i][j];
}
}
buffer[i] = buffer2;
/*for(var j = 0; j < buffer[i].length; j++){
buffer[i][j] = buffer[i][j].substring(1, buffer[i][j].length-1);
}*/
}
});
}
/*Hash function - used to hash namespaces for keys*/
String.prototype.hashCode = function () {
var hash = 0;
if (this.length == 0) return hash;
for (i = 0; i < this.length; i++) {
char = this.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return (hash + 2147483647) + 1;
}
/*Build array for headings, to lookup colum positions*/
function build() {
headings = []; //reset headings
for (var i = 0; i < buffer[0].length; i++) { //for all elements i first buffer row
var value = buffer[0][i];
headings[value] = i; //add an entry with value as key and arrayposition as value
}
};
//function to transform structure
function the(data, buffer){
var count = 0;
var arango_value = []; //main value array
var arango_value2 = {}; //array for namepsace nodes
var arango_edge = []; //array for all edges
var head = headings;
function loop(data, line){
var obj = {}
if(data !== null){
obj = {"_key":count.toString()};
obj['namespace+id'] = "";
//console.log(obj._key);
count ++;
}
for(key in data){
//console.log(key);
if(key === "prefix"){
/* console.log("------------------")
console.log("get prefix!")
console.log(data[key])
console.log(vocab[data[key]])
console.log(data)
console.log("------------------")
*/
obj['namespace'] = vocab[data[key]].namespace;
var nameHash = obj.namespace.hashCode().toString();
var isThere = false;
if(arango_value2.length !== 0){
for(key2 in arango_value2){
if(key2 == nameHash){
isThere = true;
}
}
}
if(!isThere && nameHash !== undefined){
arango_value2[nameHash] = {"_key": nameHash, "label": obj.namespace};
arango_edge.push({"_from": 0, "_to": nameHash});
//console.log(arango_value2);
}
obj['namespace+id'] = vocab[data[key]].namespace;
}
if(key === 'constant' || key === "propertyName"){
obj['namespace+id'] += data[key];
}
if(key === "column" || key === "literalValue"){
var test = data[key].value;
var field = head[test];
var val = line[field];
obj[key] = data[key].value;
obj['value'] = line[field];
if(!isNaN(obj.value)){
obj.value = +obj.value;
}
}else if(Array.isArray(data[key])){
data[key].forEach(function(entry) {
var ArrToObj = {"_from": obj._key, "_to":loop(entry, line)};
if(typeof ArrToObj._to != 'undefined'){
arango_edge.push(ArrToObj);
}
});
}else if(typeof data[key] === 'object'){
var ObjToObj = {"_from": obj._key, "_to":loop(data[key], line)};
if(typeof ObjToObj._to != 'undefined'){
arango_edge.push(ObjToObj);
}
}else{
obj[key] = data[key];
}
}
if(data !== null){
obj['label'] = "";
if(typeof obj.prefix != 'undefined'){
obj.label = obj.label + obj.prefix + " ";
}
if(typeof obj.value != 'undefined'){
obj.label = obj.label + obj.value;
}
if(typeof obj.propertyName != 'undefined'){
obj.label = obj.label + ": " + obj.propertyName;
}
//handle prefix mapping of keys here.....
if(obj.__type !== "Property"){
if(obj.value !== undefined){
obj['namespace+id'] += obj.value.toString();
obj['old_key'] = obj._key;
obj._key = obj["namespace+id"].hashCode().toString();
for(var i = 0; i < arango_edge.length; i++){
if(arango_edge[i]._from === obj.old_key){
arango_edge[i]._from = obj._key;
}else if(arango_edge[i]._to === obj.old_key){
arango_edge[i]._to = obj._key;
}
}
}
if(obj.namespace !== undefined){
arango_edge.push({"_from":obj.namespace.hashCode().toString(), "_to":obj._key});
}
}
//End prefix handling of keys
var existsInSet = false;
for (key in arango_value){
if(arango_value[key]._key === obj._key){
existsInSet = true;
break;
}
}
if(!existsInSet){
arango_value.push(obj);
}
}
return obj._key;
};
var rootNode = {"_key":count.toString(), "label": "Start node", "value": "Start node"};
count ++;
for(var i = 1; i < buffer.length; i++){
//for(var i = 1; i < 3; i++){
//console.log(buffer[i][0]);
arango_edge.push({"_from": 0, "_to":(count).toString()});
obKey = loop(data, buffer[i]);
//console.log(obKey);
}
//delete rootNode.graphRoots;
arango_value.push(rootNode);
/*Add all namespaces to node collection*/
for(key in arango_value2){
arango_value.push(arango_value2[key]);
}
/*Sort the info so it's easier to read*/
arango_value.sort(function(a, b) {
return a._key - b._key;
});
arango_edge.sort(function(a, b) {
return a._from - b._from;
});
//Save as array with objects
write_array(arango_value, arango_edge);
//Save one object per line
//write_object(arango_value, arango_edge);
}
function write_array(arangoValue, arangoEdge){
var fs = require('fs');
var stamp = new Date().toISOString().replace('T', ' ').replace('.', '')
fs.writeFile(stamp + "_arango_value.json", JSON.stringify(arangoValue), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
fs.writeFile(stamp + "_arango_edge.json", JSON.stringify(arangoEdge), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
function write_object(arangoValue, arangoEdge){
var fs = require('fs');
var stamp = new Date().toISOString().replace('T', ' ').replace('.', '')
//write nodes
console.log("writing node values to file...");
for(var i = 0; i < arangoValue.length; i++){
fs.appendFileSync(stamp + "_arango_value.json", JSON.stringify(arangoValue[i]) + '\n', function(err) {
if(err) {
return console.log(err);
}
//console.log("The file was started...");
});
}
console.log("The file \""+stamp+"_arango_value.json\" was saved!");
console.log("writing edge values to file...");
//Write edges
for(var i = 0; i < arangoEdge.length; i++){
fs.appendFileSync(stamp + "_arango_edge.json", JSON.stringify(arangoEdge[i])+'\n', function(err) {
if(err) {
return console.log(err);
}
//console.log("The file was started...");
});
}
console.log("The file \""+stamp+"_arango_edge.json\" was saved!");
}
function run(){
the(data, buffer);
}