-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
112 lines (91 loc) · 2.93 KB
/
app.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
// export LD_PRELOAD=/usr/lib/libswipl.so
// export LD_PRELOAD=/usr/lib64/swipl-7.2.3/lib/x86_64-linux/libswipl.so.7.2.3
// node: symbol lookup error: /usr/lib/swi-prolog/lib/amd64/socket.so: undefined symbol: PL_new_atom
// node --use_strict app.js
const swipl = require('swipl');
swipl.call('consult(trainscheduling)'); // consult the prolog file
// ----------------------------- HELPER FUNCTIONS -------------------------------- //
function expand1d(arr) {
var ret = [];
while (arr != '[]') {
ret.push(arr.head);
arr = arr.tail;
}
return ret;
}
function expand2d(arr) {
var ret = [];
while (arr != '[]') {
ret.push(expand1d(arr.head));
arr = arr.tail;
}
return ret;
}
function expand3d(arr) {
var ret = [];
while (arr != '[]') {
ret.push(expand2d(arr.head));
arr = arr.tail;
}
return ret;
}
function toString2dArray(arr) {
var ret = '[';
for (var i = 0; i < arr.length; i++) {
if (i != 0) ret += ','
ret = ret + '[';
ret += arr[i];
ret = ret + ']';
}
ret += ']';
return ret;
}
function toString1dArray(arr) {
return '[' + arr + ']';
}
// -------------------------- END OF HELPER FUNCTIONS ----------------------------- //
var cors = require('cors')
var bodyParser = require('body-parser')
var express = require('express')
var app = express()
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
var port = process.env.PORT || 3000;
app.listen(port);
console.log('Server started! At http://localhost:' + port);
app.post('/getPlan', function (req, res) {
var cntNodes = req.body.x;
var adjMat = req.body.y;
var cntMat = req.body.z;
var startStations = req.body.s1;
var endStations = req.body.s2;
var startTimes = req.body.r;
var dueTimes = req.body.d;
// removing the added predicates
swipl.call('retractall(mat(_))');
swipl.call('retractall(cntMat(_))');
swipl.call('retractall(maxNodes(_))');
// adding the adjMat predicate
var adjMatString = toString2dArray(adjMat);
var adjMatPredicate = 'assertz(mat(' + adjMatString + '))';
swipl.call(adjMatPredicate);
// adding the cntMat predicate
var cntMatString = toString2dArray(cntMat);
var cntMatPredicate = 'assertz(cntMat(' + cntMatString + '))';
swipl.call(cntMatPredicate);
// adding cntNodes predicate
swipl.call('assertz(maxNodes(' + cntNodes + '))');
// calling the main predicate to solve the problem
var solveProblemPredicate = 'solveProblem(' + toString1dArray(startStations) +
', ' + toString1dArray(endStations) +
', ' + toString1dArray(startTimes) +
', ' + toString1dArray(dueTimes) +
', Plan, TotalDelay)';
console.log(solveProblemPredicate);
var ret = swipl.call(solveProblemPredicate);
if (ret.TotalDelay >= 0)
res.send(expand3d(ret.Plan));
else
res.send(null);
})