forked from kloni/node-prolog-swi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswipl.js
84 lines (69 loc) · 2.16 KB
/
swipl.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
/* $Id$
node.js addon for SWI-Prolog
Author: Tom Klonikowski
E-mail: [email protected]
Copyright (C): 2012, Tom Klonikowski
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
var swipl = require("./build/Release/libswipl");
function Module(mod) {
this.module_name = mod;
}
Module.prototype = {
module : function(mod) {
return new Module(mod);
},
term_type : function(term) {
var r = swipl.term_type(term);
if(this.DEBUG) console.log("[libswipl] term_type: " + r + " " + term);
return r;
},
open_query : function(predicate, args) {
return new swipl.Query(predicate, args, this.module_name);
},
call_predicate : function(name, args) {
var query = new swipl.Query(name, args, this.module_name);
var result = false;
var r = query.next_solution();
if (r) {
result = r;
if(this.DEBUG) console.log("[libswipl] call_predicate: "
+ JSON.stringify(r));
} else {
var ex = query.exception();
if (ex) {
throw new Error(ex.exc);
} else {
if(this.DEBUG) console.log("[libswipl] call_predicate: " + r);
}
}
query.close();
return result;
},
assert : function(term) {
if(this.DEBUG) console.log("[libswipl] assert: " + term);
return this.call_predicate("assert", [ term ]);
}
};
exports.module = function(mod) {
return new Module(mod);
};
exports.initialise = function(prog) {
if (!prog) {
prog = 'node';
}
return swipl.initialise(prog);
};
exports.cleanup = function() {
return swipl.cleanup();
};