This repository was archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.js
More file actions
executable file
·306 lines (272 loc) · 10.7 KB
/
console.js
File metadata and controls
executable file
·306 lines (272 loc) · 10.7 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* A Prolog console with history pane.
**/
function PrologConsole(client, options) {
var that = this;
var prolog;
var useOverlay = options.use_console_overlay;
var nextButtonDiv = options.next_button_div || 'btn_query_next';
var historyDiv = options.history_div || 'history';
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.rdf_namespaces = {};
this.init = function () {
var userQuery = ace.edit(queryDiv);
userQuery.setTheme("ace/theme/solarized_light");
userQuery.getSession().setMode("ace/mode/prolog");
userQuery.getSession().setUseWrapMode(true);
userQuery.setOptions({
showGutter: false,
printMarginColumn: false,
highlightActiveLine: false,
highlightGutterLine: false,
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
wrap: false
});
userQuery.commands.addCommand({
name: 'send_query', readOnly: false,
bindKey: {win: 'Ctrl-Enter', mac: 'Command-Enter'},
exec: function(editor) { that.query(); }
});
userQuery.commands.addCommand({
name: 'next_result', readOnly: false,
bindKey: {win: 'Ctrl-;', mac: 'Command-;'},
exec: function(editor) { that.nextSolution(); }
});
userQuery.commands.addCommand({
name: 'next_history', readOnly: false,
bindKey: {win: 'Up', mac: 'Up'},
exec: function(editor) { that.nextHistoryItem(); }
});
userQuery.commands.addCommand({
name: 'previous_history', readOnly: false,
bindKey: {win: 'Down', mac: 'Down'},
exec: function(editor) { that.previousHistoryItem(); }
});
userQuery.resize(true);
this.initAutoCompletion();
// Create console iosOverlay
var console = document.getElementById('console');
if(console) {
var consoleOverlay = document.createElement("div");
consoleOverlay.setAttribute("id", "console-overlay");
consoleOverlay.className = "ios-overlay ios-overlay-hide div-overlay";
consoleOverlay.innerHTML += '<span class="title">Processing Query</span';
consoleOverlay.style.display = 'none';
console.appendChild(consoleOverlay);
var spinner = createSpinner();
consoleOverlay.appendChild(spinner.el);
}
var history = ace.edit(historyDiv);
history.setTheme("ace/theme/solarized_light");
history.getSession().setMode("ace/mode/prolog");
history.getSession().setUseWrapMode(true);
history.setOptions({
readOnly: true,
showGutter: false,
printMarginColumn: false,
highlightActiveLine: false,
highlightGutterLine: false,
wrap: false
});
setInactive(document.getElementById(nextButtonDiv));
setInterval(that.updateNamespaces, 10000);
that.updateNamespaces();
};
this.updateNamespaces = function(objectName) {
var pl = client.newProlog();
if(!pl) return;
pl.jsonQuery("findall([_X,_Y], rdf_current_ns(_X,_Y), NS).",
function(result) {
pl.finishClient();
if(result.solution) {
var namespaces = {};
for(i in result.solution.NS) {
namespaces[result.solution.NS[i][1]] = result.solution.NS[i][0];
}
that.rdf_namespaces = namespaces;
}
}
);
};
this.queryPredicateNames = function() {
if( ! prologNames ) {
prolog = this.newProlog();
if(!prolog) return;
prologNames = [];
// Query for predicates/modules and collect all results
prolog.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.showConsoleOverlay = function () {
var consoleOverlay = document.getElementById('console-overlay');
if(consoleOverlay) {
consoleOverlay.style.display = 'block';
consoleOverlay.className = consoleOverlay.className.replace("hide","show");
consoleOverlay.style.pointerEvents = "auto";
}
};
this.hideConsoleOverlay = function () {
var consoleOverlay = document.getElementById('console-overlay');
if(consoleOverlay) {
//consoleOverlay.style.display = 'none';
consoleOverlay.className = consoleOverlay.className.replace("show","hide");
consoleOverlay.style.pointerEvents = "none";
}
};
this.newProlog = function() {
if(!client.ros) return;
if (prolog && prolog.finished == false) {
ace.edit(historyDiv).setValue(ace.edit(historyDiv).getValue() + "stopped.\n", -1);
ace.edit(historyDiv).navigateFileEnd();
prolog.finishClient();
prolog = undefined;
}
return new JsonProlog(client.ros, {});
}
this.query = function () {
var query = ace.edit(queryDiv);
var history = ace.edit(historyDiv);
var q = query.getValue().trim();
if (q.substr(q.length - 1) == ".") {
q = q.substr(0, q.length - 1);
prolog = this.newProlog();
if(useOverlay) that.showConsoleOverlay();
history.setValue(history.getValue() + "\n\n?- " + q + ".\n", -1);
history.navigateFileEnd();
setActive(document.getElementById(nextButtonDiv));
prolog.jsonQuery(q+", ignore(marker_publish)", function(result) {
if(useOverlay) that.hideConsoleOverlay();
history.setValue(history.getValue() + prolog.format(result,that.rdf_namespaces), -1);
history.navigateFileEnd();
if( ! result.value ) setInactive(document.getElementById(nextButtonDiv));
}, mode=1); // incremental mode
query.setValue("");
that.addHistoryItem(q);
historyIndex = -1;
}
else {
if (prolog != null && prolog.finished == false) {
if(useOverlay) that.hideConsoleOverlay();
history.setValue(history.getValue() + "stopped.\n\n", -1);
history.navigateFileEnd();
prolog.finishClient();
prolog = undefined;
}
else {
alert("Invalid prolog query '" + q + "'. Prolog queries always end with a dot.");
}
}
};
this.nextSolution = function () {
var history = ace.edit(historyDiv);
if(useOverlay) that.showConsoleOverlay();
prolog.nextQuery(function(result) {
if(useOverlay) that.hideConsoleOverlay();
history.setValue(history.getValue() + prolog.format(result,that.rdf_namespaces), -1);
history.navigateFileEnd();
if( ! result.value ) setInactive(document.getElementById(nextButtonDiv));
});
ace.edit(queryDiv).focus();
};
// TODO(daniel): better use CSS class / disabled selector
function setActive(div) {
div.style.pointerEvents = "auto";
div.style.backgroundColor = "#dadada";
div.style.color = "#606060";
div.style.opacity = "1.0";
};
function setInactive(div) {
div.style.pointerEvents = "none";
div.style.backgroundColor = "#cfcfcf";
div.style.color = "#adadad";
div.style.opacity = "0.2";
};
// append the selected query to the user_query form
this.addSelectedToQueryform = function (selectid, focus) {
var select = document.getElementById(selectid);
this.setQueryValue(select.options[select.selectedIndex].value, focus);
};
// set the value of the query editor and move the cursor to the end
this.setQueryValue = function (val, focus){
var user_query = ace.edit(queryDiv);
user_query.setValue(val, -1);
if(focus) user_query.focus();
// disabled because it behaviour is not nice when wrapping disabled
//user_query.navigateFileEnd();
};
///////////////////////////////
//////////// History
///////////////////////////////
this.addHistoryItem = function (query) {
$.ajax({
url: "/knowrob/add_history_item",
type: "POST",
contentType: "application/json",
data: JSON.stringify({query: query}),
dataType: "json"
}).done( function (request) {})
};
this.setHistoryItem = function (index) {
$.ajax({
url: "/knowrob/get_history_item",
type: "POST",
contentType: "application/json",
data: JSON.stringify({index: index}),
dataType: "json",
success: function (data) {
ace.edit(queryDiv).setValue(data.item);
historyIndex = data.index;
}
}).done( function (request) {})
};
this.nextHistoryItem = function () {
this.setHistoryItem(historyIndex+1);
};
this.previousHistoryItem = function () {
this.setHistoryItem(historyIndex-1);
};
this.zoomIn = function() {
$('#history').css('font-size', parseInt($('#history').css("font-size")) + 2);
$('#user_query').css('font-size', parseInt($('#user_query').css("font-size")) + 2);
};
this.zoomOut = function() {
$('#history').css('font-size', parseInt($('#history').css("font-size")) - 2);
$('#user_query').css('font-size', parseInt($('#user_query').css("font-size")) - 2);
};
};