-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrologConsole.js
More file actions
executable file
·206 lines (181 loc) · 6.51 KB
/
PrologConsole.js
File metadata and controls
executable file
·206 lines (181 loc) · 6.51 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var ace = require('brace');
require('brace/mode/prolog');
require('brace/mode/xml');
require('brace/theme/monokai');
require('brace/theme/solarized_light');
require('brace/ext/language_tools');
var aceLangTools = ace.acequire("ace/ext/language_tools");
var ROSPrologClient = require('@openease/ros-clients').ROSPrologClient;
/**
* A Prolog console with history pane.
**/
module.exports = function(client, options) {
var that = this;
var prolog;
this.on_query = options.on_query || function(qid,q){};
this.on_query_answer = options.on_query_answer || function(qid,answer){};
this.on_query_finish = options.on_query_finish || function(qid){};
var queryDiv = options.query_div || 'user_query';
// Names of prolog predicates and modules for auto completion
var prologNames;
// The index to the currently active history item
// history items are saved on the server and queried using AJAX
var historyIndex = -1;
this.init = function () {
var queryInput = ace.edit(queryDiv);
//queryInput.setTheme("ace/theme/solarized_light");
queryInput.getSession().setMode("ace/mode/prolog");
queryInput.getSession().setUseWrapMode(true);
queryInput.setOptions({
showGutter: false,
printMarginColumn: false,
highlightActiveLine: false,
highlightGutterLine: false,
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
wrap: false,
maxLines: Infinity
});
queryInput.commands.addCommand({
name: 'send_query', readOnly: false,
bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
exec: function(editor) { that.query(); }
});
queryInput.commands.addCommand({
name: 'next_result', readOnly: false,
bindKey: {win: 'Ctrl-;', mac: 'Command-;'},
exec: function(editor) { that.nextSolution(); }
});
queryInput.commands.addCommand({
name: 'next_history', readOnly: false,
bindKey: {win: 'Up', mac: 'Up'},
exec: function(editor) { that.nextHistoryItem(); }
});
queryInput.commands.addCommand({
name: 'previous_history', readOnly: false,
bindKey: {win: 'Down', mac: 'Down'},
exec: function(editor) { that.previousHistoryItem(); }
});
queryInput.setShowPrintMargin(false);
queryInput.renderer.setScrollMargin(6, 6, 6, 6);
queryInput.resize(true);
this.initAutoCompletion();
};
this.queryPredicateNames = function() {
if(!client.ros) return;
if( ! prologNames ) {
var pl = new ROSPrologClient(client.ros, {});
if(!pl) return;
prologNames = [];
// Query for predicates/modules and collect all results
pl.jsonQuery("findall(X, current_predicate(X/_);current_module(X), L)", function(x) {
if (x.value) {
// Parse each value
var lines = x.value.split("\n");
for(i=1; i<lines.length-1; ++i) {
var tmp = lines[i].split(" = ");
if(tmp.length==2) {
prologNames.push(tmp[1].trim());
}
}
prologNames.sort();
}
else {
console.warn("Unable to query prolog names.");
console.warn(x);
}
}, mode=0);
}
return prologNames;
};
this.initAutoCompletion = function() {
// Add completer for prolog code
aceLangTools.addCompleter({
getCompletions: function(editor, session, pos, prefix, callback) {
var names = that.queryPredicateNames();
if( names ) {
callback(null, names.map(function(x) {
return {name: x, value: x, score: 100, meta: "pl"};
}));
}
}
});
};
this.newProlog = function() {
if(!client.ros) return;
if (prolog && prolog.finished == false) {
that.finishProlog(prolog);
prolog = undefined;
}
return new ROSPrologClient(client.ros, {});
}
this.finishProlog = function(pl) {
pl.finishClient();
that.on_query_finish(pl.qid);
};
this.query = function (query_string) {
var query = ace.edit(queryDiv);
if(!query_string) {
query_string = query.getValue().trim();
}
if (query_string.substr(query_string.length - 1) == ".") {
query_string = query_string.substr(0, query_string.length - 1);
}
prolog = that.newProlog();
that.on_query(prolog.qid,query_string);
prolog.jsonQuery(query_string, function(result) {
that.on_query_answer(prolog.qid,result);
}, mode=1); // incremental mode
query.setValue("");
that.addHistoryItem(query_string);
historyIndex = -1;
};
this.nextSolution = function () {
if (prolog != null && prolog.finished == false) {
prolog.nextQuery(function(result) {
that.on_query_answer(prolog.qid,result);
});
ace.edit(queryDiv).focus();
}
};
// set the value of the query editor and move the cursor to the end
this.setQueryValue = function (val, focus){
var queryInput = ace.edit(queryDiv);
queryInput.setValue(val, -1);
if(focus) queryInput.focus();
};
///////////////////////////////
//////////// History
///////////////////////////////
this.addHistoryItem = function (query) {
var pl = new ROSPrologClient(client.ros, {});
if(!pl) return;
pl.jsonQuery("history_add('"+query.replaceAll("'","\\'")+"').",
function(result) {
//console.info(result);
}
);
};
this.setHistoryItem = function (index) {
var pl = new ROSPrologClient(client.ros, {});
if(!pl) return;
pl.jsonQuery("history_get("+index+",Q).",
function(result) {
if(result.solution) {
var queryInput = ace.edit(queryDiv);
queryInput.setValue(result.solution.Q + ".");
queryInput.focus();
historyIndex = index;
}
}
);
};
this.nextHistoryItem = function () {
this.setHistoryItem(historyIndex+1);
};
this.previousHistoryItem = function () {
if(historyIndex>0) {
this.setHistoryItem(historyIndex-1);
}
};
};