-
Notifications
You must be signed in to change notification settings - Fork 0
/
comunica_runner.mjs
93 lines (76 loc) · 2.69 KB
/
comunica_runner.mjs
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
import { QueryEngineFactory as QueryEngineFactoryLTQT } from "@comunica/query-sparql-link-traversal";
import { QueryEngine } from "@comunica/query-sparql-file";
import fs from 'fs';
import { Command } from 'commander';
const program = new Command();
program
.name('evaluation')
.description('CLI program to run a TREE evaluation')
.version('0.0.0')
.requiredOption('-f, --file-path <string>', 'File path of the query to be executed')
.requiredOption('-r, --root-nodes <string...>', 'the first data sources to query')
.requiredOption('-d, --metadata <string...>', 'the metadata file')
.option('-m, --mode <mode>', 'The configuration of the engine', 'TREE')
.option('-t, --timeout <number>', 'Timeout of the query in second', 120)
.parse(process.argv);
const options = program.opts();
const queryFilePath = options.filePath;
const timeout = options.timeout * 1000;
const rootNodes = options.rootNodes;
const metadataNode = options.metadata;
const mode = options.mode;
let engine = null;
if (mode === "TREE") {
const config = "./evaluation/config_comunica_follow_tree.json";
engine = await new QueryEngineFactoryLTQT().create({ configPath: config });
} else if (mode === "TREE-GUIDED") {
const config = "./evaluation/config_comunica_follow_tree_solver.json";
engine = await new QueryEngineFactoryLTQT().create({ configPath: config });
} else if (mode === "DATA-DUMP") {
engine = new QueryEngine();
} else {
throw new Error(`the mode "${mode}" is not supported`);
}
const regexpSummary = /(TOTAL),([+-]?[0-9]*[.]?[0-9]+),([0-9]+)/;
const returnedMessage = (nResults, timeExec, nRequest) => {
const resp = {
nResults,
timeExec,
nRequest
};
return JSON.stringify(resp);
};
const timer = setTimeout(() => {
console.log(returnedMessage(nResults, timeout));
process.exit(1);
}, timeout);
const query = fs.readFileSync(queryFilePath).toString();
const results = await engine.query(
query, {
sources: rootNodes.concat(metadataNode),
lenient: true,
});
const { data } = await engine.resultToString(results,
'stats');
let nResults = 0;
let timeExec = NaN;
let nRequests = NaN;
data.on('data', (res) => {
const currentRes = res.toString();
if (regexpSummary.test(currentRes)) {
let tag = (currentRes).match(regexpSummary);
timeExec = Number(tag[2]);
nRequests = Number(tag[3]);
} else if (currentRes.indexOf("HTTP requests") === -1) {
nResults += 1;
}
});
data.on('error', (error) => {
clearTimeout(timer);
throw new Error(error);
});
data.on('end', () => {
console.log(returnedMessage(nResults, timeExec, nRequests));
clearTimeout(timer);
process.exit(0);
});