-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
148 lines (135 loc) · 4.23 KB
/
index.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
const spawn = require('child_process').spawn;
const path = require('path');
const os = require("os");
const fs = require("fs");
const N3 = require('n3');
let profile = 'owl';
let distPath = path.resolve(__dirname, 'dist', 'n3unit.pvm');
let ontologyPath = path.resolve(__dirname, 'test', profile, 'ontology.ttl');
let profilePath = path.resolve(__dirname, 'profiles', profile + '.n3');
let dataPath = path.resolve(__dirname, 'test', profile, 'INVFUNC_Wrong.ttl');
let queryPath = path.resolve(__dirname, '..', 'constraint-rules', 'examples', 'query.n3');
let files = [{
type: 'pvm',
path: distPath
}, {
type: 'ttl',
path: ontologyPath
}, {
type: 'n3',
path: profilePath
}];
const profileDir = path.resolve(__dirname, 'test', profile);
let testFiles = fs.readdirSync(profileDir);
let realStart = new Date();
let tested = 0;
let p = Promise.resolve();
testFiles.forEach(function (file) {
if (file.toLowerCase().indexOf('correct') > 0 || file.toLowerCase().indexOf('wrong') > 0) {
tested++;
let myFiles = JSON.parse(JSON.stringify(files));
myFiles.push({type: 'ttl', path: path.resolve(profileDir, file)});
p = p.then(function () {
// console.log(`doing ${file}`);
let start = new Date();
return eye(myFiles, queryPath).then(function ({out, err, numbers}) {
let end = new Date();
let timing = end.getTime() - start.getTime();
// console.log(`it took ${timing}`);
var parser = N3.Parser();
var store = N3.Store();
return new Promise(function (fulfill, reject) {
parser.parse(out, function (err, triple, prefixes) {
if (triple) {
store.addTriple(triple);
}
else {
if (store.getTriples().length === 0 && file.toLowerCase().indexOf('correct') > 0) {
fulfill(true);
} else if (store.getTriples().length > 0 && file.toLowerCase().indexOf('wrong') > 0) {
fulfill(true);
}
else {
fulfill(false);
console.error(out);
}
}
});
});
// console.log(out);
// console.error(err);
}).then(function (success) {
if (success) {
// console.log(`success for ${file}`);
}
else {
console.error(`failure for ${file}`);
}
}).catch(function (err) {
console.error(err);
});
});
}
else {
p = p.then(function () {
return Promise.resolve();
});
}
});
p.then(function() {
let realEnd = new Date();
let timing = realEnd.getTime() - realStart.getTime();
console.log(`it took ${timing} for ${tested} files (${timing / tested} per file)`);
});
function eye(files = [], query = null) {
const eyePath = os.platform() === 'win32' ? "C:/Program Files/eye/bin/eye.cmd" : '/opt/eye/bin/eye.sh';
const swiplPath = os.platform() === 'win32' ? "swipl" : '/usr/bin/swipl';
const type = files[0].type === 'pvm' ? 'pvm' : '';
const args = createArgs(files, query);
return new Promise(function (fulfill, reject) {
let child;
if (type === 'pvm') {
child = spawn(swiplPath, args, {shell: true});
}
else {
child = spawn(eyePath, args, {shell: true});
}
let output = '';
let errorLog = '';
child.stdout.on('data', function (data) {
output += data;
});
child.stderr.on('data', function (data) {
errorLog += data;
});
child.on('exit', function () {
const numbers = errorLog.match(/reasoning (\d+)[\s\S]+?(\d+)/);
if (errorLog.indexOf('** ERROR **') >= 0) {
reject(errorLog);
}
fulfill({out: output, err: errorLog, numbers});
});
});
function createArgs(files, query) {
const thisArgs = [];
let i = 0;
if (files[0].type === 'pvm') {
thisArgs.push('-x');
thisArgs.push(files[0].path.replace(/\\/g, '/'));
thisArgs.push('--');
i = 1;
}
for (i; i < files.length; i++) {
if (files[i].type === 'n3p') {
thisArgs.push('--plugin');
}
thisArgs.push(files[i].path.replace(/\\/g, '/'));
}
if (query) {
thisArgs.push('--query');
thisArgs.push(query.replace(/\\/g, '/'));
}
thisArgs.push('--nope');
return thisArgs;
}
}