-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
189 lines (152 loc) · 3.84 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
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
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const path = require('path')
const { config } = require('./config.js')
const tmp = require('./lib/tmp.js')
const eye = require('./lib/eye/eye.js')
const cwm = require('./lib/cwm/cwm.js')
const jen3 = require('./lib/jen3/jen3.js')
const { generateLink, resolveLink } = require('./lib/gen_link.js')
const { checkBuiltinInput } = require('./lib/check_builtin_input.js')
const app = express()
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/n3/editor/s/*', (req, res) => {
res.sendFile(path.join(__dirname, "editor/index.html"));
});
app.use('/n3/editor', express.static(path.join(__dirname, "editor")));
app.use('/n3/lib/eyebrow', express.static(path.join(__dirname, "lib/eyebrow")));
app.use('/n3/config.js', express.static(path.join(__dirname, 'config.js')));
app.get('/n3', (request, response) => {
console.log('GET /')
const html =
`<html>
<body>
<h1>Welcome to the N3 server!</h1>
<h3>Serving N3 since 2nd June 2020.</h3>
config:
<pre>${JSON.stringify(config, null, 4)}</pre> </p>
</body>
</html>`
response.writeHead(200, { 'Content-Type': 'text/html' })
response.end(html)
})
app.post('/n3', (request, response) => {
console.log("POST /")
const data = request.body
// console.log("data:", data);
console.log(
"task:", data.task,
(data.system? ", system: " + data.system : "")
);
function ctu(ret) {
// console.log("ret:", ret)
if (ret.error) {
console.error("error", ret.error)
}
response.send(ret)
}
switch (data.task) {
case 'derivations':
case 'deductive_closure':
doReasoning(data, ctu)
break
case 'explain':
doExplaining(data, ctu)
break
case 'imperate':
doImperating(data, ctu)
break
case 'generate_link':
doGenerateLink(data, ctu)
break
case 'resolve_link':
doResolveLink(data, ctu)
break
case 'check_builtin_input':
doCheckBuiltinInput(data, ctu)
break
default:
ctu({ error: 'unknown task: ' + data.task })
}
})
app.listen(config.http.port)
console.log(`Listening at ${config.http.hostname}:${config.http.port}`)
function doReasoning(options, ctu) {
tmp.save(options.formula, (file) => {
function end(ret) {
tmp.del(file)
ctu(ret)
}
var reasoner = null;
switch (options.system) {
case "eye":
reasoner = eye
break
case "cwm":
reasoner = cwm
break
case "jen3":
reasoner = jen3
break
default:
end({ error: `unsupported system: "${options.system}"` })
break
}
if (reasoner)
reasoner.exec(options, file, end)
})
}
function doExplaining(options, ctu) {
tmp.save(options.formula, (file) => {
var reasoner = null;
switch (options.system) {
case "eye":
reasoner = eye
break
default:
end({ error: `unsupported system: "${options.system}"` })
break
}
reasoner.exec(options, file, (explanation) => {
tmp.del(file)
ctu(explanation)
})
})
}
function doImperating(options, ctu) {
tmp.save(options.formula, (file) => {
var reasoner = jen3;
reasoner.exec(options, file, (code) => {
tmp.del(file)
ctu(code)
})
})
}
function doGenerateLink(options, ctu) {
generateLink(options.formula, options.format)
.then((id) => { console.log("generated link:", id); ctu({ success: id }) })
.catch((error) => { ctu({ error: error }) })
}
function doResolveLink(options, ctu) {
resolveLink(options.id)
.then((data) => {
// console.log("resolved link:", data);
ctu({ success: data })
})
.catch((error) => { ctu({ error: error }) })
}
function doCheckBuiltinInput(options, ctu) {
tmp.save(options.definitions, (defFile) => {
tmp.save(options.test, (testFile) => {
function end(ret) {
tmp.del(defFile)
tmp.del(testFile)
ctu(ret)
}
checkBuiltinInput(defFile, testFile, end)
})
})
}