This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
512 lines (483 loc) · 15.5 KB
/
script.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
/**************************/
/** Loading core modules **/
/**************************/
// module for file I/O operations
var fs = require('fs');
// module for handling and transforming file paths
var path = require('path');
// module for handling HTTP connections
var http = require('http');
// module for dealing with query strings
var querystring = require('querystring');
// module for reading console input
var readline = require('readline');
/*************************/
/** Declaring constants **/
/*************************/
// sparql endpoint host
var sparqlHost = 'ulcs.fusepool.info';
// sparql endpoint port
var sparqlPort = '8890';
// sparql endpoint path
var sparqlPath = '/sparql';
// sparql username for authentication
var sparqlUser = '';
// sparql password for authentication
var sparqlPass = '';
// basic auth header for sparql
var basicAuth = '';
// timeout parameter of sparql requests (15 mins)
var timeout = 15 * 60 * 1000;
// format parameter of sparql requests
var formats = {
JSON: 'application/json',
TURTLE: 'text/turtle',
RDF: 'application/rdf+xml',
FORM: 'application/x-www-form-urlencoded',
AUTO: 'auto'
};
// parameter that needs to be replaced in the queries
var staticUri = 'http://d-nb.info/gnd/118529692';
// relative path of folder containing the sparql files
var resourceFolder = 'sparql';
// file names
var files = {
T0: 'T0_select-all-GND-IDs-(milos).rq',
T02: 'T0-2_clear-RDF-Graph-for-a-GND-ID-(milos).rq',
T1: 'T1_create-RDF-Graph-for-a-GND-ID-(milos).rq',
T3: 'T3_data-from-DBpedia-(milos)-insert.rq',
T32: 'T3-2_textsDBpedia-incl-subjects+categories-(carl+milos)-insert.rq',
T4: 'T4_coocConcepts-(milos)-insert.rq',
T43: 'T4-3_coocConcepts_w-distance-(milos)-insert.rq',
T44DDC: 'T4-4_coocConcepts_w-distance-add-type-DDC-(milos)-insert.rq',
T44GND: 'T4-4_coocConcepts_w-distance-add-type-GND-(milos)-insert.rq',
T44RVK: 'T4-4_coocConcepts_w-distance-add-type-RVK-(milos)-insert.rq',
T44SSG: 'T4-4_coocConcepts_w-distance-add-type-SSG-(milos)-insert.rq',
T5: 'T5_getTitlesByConcept-(milos)-insert.rq'
};
// relative file paths
var filePaths = {
T0: path.join(__dirname, resourceFolder, files.T0),
T02: path.join(__dirname, resourceFolder, files.T02),
T1: path.join(__dirname, resourceFolder, files.T1),
T3: path.join(__dirname, resourceFolder, files.T3),
T32: path.join(__dirname, resourceFolder, files.T32),
T4: path.join(__dirname, resourceFolder, files.T4),
T43: path.join(__dirname, resourceFolder, files.T43),
T44DDC: path.join(__dirname, resourceFolder, files.T44DDC),
T44GND: path.join(__dirname, resourceFolder, files.T44GND),
T44RVK: path.join(__dirname, resourceFolder, files.T44RVK),
T44SSG: path.join(__dirname, resourceFolder, files.T44SSG),
T5: path.join(__dirname, resourceFolder, files.T5)
};
/********************/
/** Main variables **/
/********************/
// if false the script will read the URIs from the data
// folder using the resultFolder, otherwise runs T0 (from console input)
var RUN_T0;
// stored result of T0 query (data folder)
var resultFolder = './data/T0_all/';
// list of result files
var resultFiles = fs.readdirSync(resultFolder);
// current result file
var fileIndex = 0;
// global array for storing the list of URIs
var URIs = new Array();
// local index for looping through the list of URIs
var index = 0;
// global index for looping through the list of URIs
var globalIndex = 0;
// global variable to indicate first cycle after T0
var first = true;
// limit for T0 (only for query)
var limit = 1000;
// global offset for T0 (only for query)
var offset = 0;
// flags for synchronization
var readyFlags = {
T0: false,
T02: false,
T1: false,
T3: false,
T32: false,
T4: false,
T43: false,
T44DDC: false,
T44GND: false,
T44RVK: false,
T44SSG: false,
T5: false
};
// sparql queries read from file
var queries = {
T0: readQuery(filePaths.T0),
T02: readQuery(filePaths.T02),
T1: readQuery(filePaths.T1),
T3: readQuery(filePaths.T3),
T32: readQuery(filePaths.T32),
T4: readQuery(filePaths.T4),
T43: readQuery(filePaths.T43),
T44DDC: readQuery(filePaths.T44DDC),
T44GND: readQuery(filePaths.T44GND),
T44RVK: readQuery(filePaths.T44RVK),
T44SSG: readQuery(filePaths.T44SSG),
T5: readQuery(filePaths.T5)
};
// STARTING SCRIPT
initialize();
/********************/
/** Main functions **/
/********************/
/**
* Get user inputs before starting execution.
*/
function initialize() {
console.log('\n---== Starting Script ==---');
var rl = readline.createInterface({input: process.stdin, output: process.stdout});
console.log('\nPlease provide credential for "' + sparqlHost + ':' + sparqlPort + sparqlPath + '" for basic HTTP authentication!');
// get credentials from console input
rl.question('Username: ', function (answer1) {
sparqlUser = answer1;
rl.question('Password: ', function (answer2) {
sparqlPass = answer2;
// run T0 from query or from file
console.log('\nDo you want to execute T0 query? (If not the script will use the results in "' + resultFolder + '" folder!)');
rl.question('Run T0? (y/n): ', function (answer3) {
RUN_T0 = (answer3 == 'y' || answer3 == 'Y') ? true : false;
rl.close();
start();
});
});
});
}
/**
* Starts the script by getting the list of URIs either
* querying it from the SPARQL endpoint using T0 or reading
* it from file. (The file contains the result of T0 as plain text.)
**/
function start() {
console.time('Execution time');
basicAuth = 'Basic ' + new Buffer(sparqlUser + ':' + sparqlPass).toString('base64');
if (RUN_T0) {
console.log('\nExecuting T0 query...');
queryURIs();
}
else {
console.log('\nReading "' + resultFiles[fileIndex] + '" file...');
readURIs();
}
}
/**
* Gets all the URIs using the T0 query.
**/
function queryURIs() {
var query = replaceAll(queries.T0, '<offset>', offset);
query = replaceAll(query, '<limit>', limit);
// empty array
URIs = new Array();
// get POST data
var data = getData(query, formats.JSON);
var options = getOptions(data)
// create HTTP request
var request = http.request(options, function (response) {
response.setEncoding('utf8');
response.on('data', function (data) {
var json = JSON.parse(data);
json = json.results.bindings;
for (var i = 0; i < json.length; i++) {
URIs.push(json[i].gndid.value);
}
// indicate first cycle after T0
first = true;
syncCallback();
});
});
request.on('error', function (e) {
console.error('FATAL: Request returned with error while querying T0.');
process.exit(1);
});
request.write(data);
request.end();
}
/**
* Reads all the URIs from file. (Result of T0.)
**/
function readURIs() {
var filePath = path.join(resultFolder, resultFiles[fileIndex]);
// empty array
URIs = new Array();
// read file asynchronously
fs.readFile(filePath, 'utf-8', function (error, data) {
// if file cannot be read abort with fatal error
if (error) {
console.error('FATAL: Could not read file "' + filePath + '"');
process.exit(1);
}
// if data is empty abort with fatal error
if (isEmpty(data)) {
console.error('FATAL: File is empty "' + filePath + '"');
process.exit(1);
}
// set global list of URIs
URIs = data.split('\n');
// clean array
URIs = cleanArray(URIs);
// indicate first cycle after T0
first = true;
syncCallback();
});
}
/**
* Replaces the static URI in the query and starts the first
* it asynchronously. (Clear query.)
**/
function startAsyncQueriesPart0(currentUri) {
console.log((globalIndex + 1) + '. <' + currentUri + '>');
// T0-2 query
var query = replaceAll(queries.T02, staticUri, currentUri);
runQuery(query, 'T02');
}
/**
* Replaces the static URI in the queries and starts the first
* group asynchronously.
**/
function startAsyncQueriesPart1(currentUri) {
// T1 query
var query = replaceAll(queries.T1, staticUri, currentUri);
runQuery(query, 'T1');
// T3 query
query = replaceAll(queries.T3, staticUri, currentUri);
runQuery(query, 'T3');
// T32 query
query = replaceAll(queries.T32, staticUri, currentUri);
query = replaceURISegments(query, staticUri, currentUri);
runQuery(query, 'T32');
// T4 query
query = replaceAll(queries.T4, staticUri, currentUri);
runQuery(query, 'T4');
// T43 query
query = replaceAll(queries.T43, staticUri, currentUri);
runQuery(query, 'T43');
// T5 query
query = replaceAll(queries.T5, staticUri, currentUri);
runQuery(query, 'T5');
}
/**
* Replaces the static URI in the queries and starts the second
* group asynchronously.
**/
function startAsyncQueriesPart2(currentUri) {
// T44DDC query
var query = replaceAll(queries.T44DDC, staticUri, currentUri);
runQuery(query, 'T44DDC');
// T44GND query
query = replaceAll(queries.T44GND, staticUri, currentUri);
runQuery(query, 'T44GND');
// T44RVK query
query = replaceAll(queries.T44RVK, staticUri, currentUri);
runQuery(query, 'T44RVK');
// T44SSG query
query = replaceAll(queries.T44SSG, staticUri, currentUri);
runQuery(query, 'T44SSG');
}
/**
* Runs the supplied query asynchronously.
**/
function runQuery(query, queryName) {
var data = getData(query, formats.AUTO);
var options = getOptions(data)
// create HTTP request
var request = http.request(options, function (response) {
if (response.statusCode == 200) {
console.log('\t' + queryName + ' done');
}
else {
console.error('\tERROR: Response returned with status code ' + response.statusCode + ' for query "' + queryName + '"');
}
// set query ready flag
readyFlags[queryName] = true;
// synchronize with other threads
syncCallback();
});
request.on('error', function (e) {
console.error('\tERROR: Request returned with error for query "' + queryName + '"');
readyFlags[queryName] = true;
syncCallback();
});
request.write(data);
request.end();
}
/**
* Synchronizes the asynchronously started queries. It also keeps
* the first and second group of queries from running concurrently.
**/
function syncCallback() {
// if the first query have finished call the first group with the same URI (clear query)
if (readyFlags['T02']) {
// reset ready flags
readyFlags['T02'] = false;
// get current URI
var currentUri = URIs[index];
// start the second group of queries
startAsyncQueriesPart1(currentUri);
return;
}
// if all queries have finished in the first group call the second group with the same URI
if (readyFlags['T1'] && readyFlags['T3'] && readyFlags['T32'] && readyFlags['T4'] && readyFlags['T43'] && readyFlags['T5']) {
// reset ready flags
readyFlags['T1'] = false;
readyFlags['T3'] = false;
readyFlags['T32'] = false;
readyFlags['T4'] = false;
readyFlags['T43'] = false;
readyFlags['T5'] = false;
// get current URI
var currentUri = URIs[index];
// start the second group of queries
startAsyncQueriesPart2(currentUri);
return;
}
// if all queries have finished in the second group call the first group with the next URI
if (readyFlags['T44DDC'] && readyFlags['T44GND'] && readyFlags['T44RVK'] && readyFlags['T44SSG']) {
// reset ready flags
readyFlags['T44DDC'] = false;
readyFlags['T44GND'] = false;
readyFlags['T44RVK'] = false;
readyFlags['T44SSG'] = false;
// increase index
index++;
// increase global index
globalIndex++;
// go to the next URI if there is any
if (index < URIs.length) {
// get current URI
var currentUri = URIs[index];
// start the first group of queries
startAsyncQueriesPart0(currentUri);
return;
}
else {
if (RUN_T0) {
if (URIs.length > 0) {
index = 0;
// increase offset for T0
offset += limit;
console.log('Executing T0 query...');
queryURIs();
}
else {
console.timeEnd('Execution time');
process.exit(0);
}
}
else {
if (fileIndex < resultFiles.length - 1) {
index = 0;
// increase file index
fileIndex++;
console.log('\nReading "' + resultFiles[fileIndex] + '" file...');
readURIs();
}
else {
console.timeEnd('Execution time');
process.exit(0);
}
}
}
}
// if this is the first run (after T0)
if (first) {
first = false;
console.log('Iterate through each GND-ID...');
// get current URI
var currentUri = URIs[index];
// start the first group of queries
startAsyncQueriesPart0(currentUri);
return;
}
}
/**
* Replaces the last segment of uri1 with the last
* segment of uri2 in the query text. (For query T32.)
**/
function replaceURISegments(query, uri1, uri2) {
// get last segment from uri1
var str1 = uri1.substr(uri1.lastIndexOf("/") + 1);
// get last segment from uri2
var str2 = uri2.substr(uri2.lastIndexOf("/") + 1);
// replace all
return replaceAll(query, str1, str2);
}
/*********************/
/** Other functions **/
/*********************/
/**
* Create URI encoded data for HTTP POST.
**/
function getData(query, format) {
return data = querystring.stringify({
query: query,
format: format,
timeout: timeout
});
}
/**
* Create options for HTTP POST.
**/
function getOptions(data) {
return options = {
host: sparqlHost,
port: sparqlPort,
path: sparqlPath,
method: 'POST',
headers: {
'Authorization': basicAuth,
'Content-Type': formats.FORM,
'Content-Length': Buffer.byteLength(data)
}
};
}
/**
* Reads file synchronously from supplied path.
**/
function readQuery(filePath) {
var data = fs.readFileSync(filePath, 'utf-8');
if (isEmpty(data)) {
console.error('FATAL: File is empty "' + filePath + '"');
process.exit(1);
}
// remove comment lines
data = data.replace(/^#.*$/gm, '');
// remove blank lines
data = data.replace(/^\s*$/gm, '');
return data;
}
/**
* Replaces all occurrences of str1 to str2 in text.
**/
function replaceAll(text, str1, str2) {
var regex = new RegExp(str1, 'g');
return text.replace(regex, str2);
}
/**
* Checks if variable is empty.
**/
function isEmpty(data) {
if (typeof data === 'undefined' || data === '' || data === null || data.length == 0) {
return true;
}
return false;
}
/**
* Remove empty elements from array.
**/
function cleanArray(actual) {
var newArray = new Array();
for (var i = 0; i < actual.length; i++) {
if (actual[i]) {
newArray.push(actual[i]);
}
}
return newArray;
}