summaryrefslogtreecommitdiff
path: root/js/prolog.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/prolog.js')
-rw-r--r--js/prolog.js87
1 files changed, 65 insertions, 22 deletions
diff --git a/js/prolog.js b/js/prolog.js
index 50b9176..429ac72 100644
--- a/js/prolog.js
+++ b/js/prolog.js
@@ -6,7 +6,7 @@
// a constant
var firstCharacterPos = {'line': 0, 'ch': 0};
- var makePrologTerminalHandler = function (jqConsole, editor, problem_id) {
+ var makePrologTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) {
var promptMode = true, // default: query composition; alternative: query result browsing
tcs = function terminalCommandSuccess (data) {
var t, lines, i;
@@ -36,16 +36,15 @@
'step': 'run',
'program': editor.getDoc().getValue(),
'query': command,
-// 'language': 'prolog',
-// 'problem_group': problem_group,
-// 'problem': problem
- 'problem_id': problem_id
+ 'problem_id': problem_id,
+ 'trace': activityHandler.addAndPurge({'typ': 'slv', 'qry': command})
}).then(tcs, tcf).done();
}
else {
// not in prompt mode -- we should never land here, but handle it anyway
codeq.comms.sendQuery({
- 'step': 'end'
+ 'step': 'end',
+ 'trace': activityHandler.addAndPurge({'typ': 'stp'})
}).then(tcs, tcf).done();
}
}, {
@@ -64,14 +63,16 @@
// space or semicolon -> show next answer
event.which = 59; // semicolon
codeq.comms.sendQuery({
- 'step': 'next'
+ 'step': 'next',
+ 'trace': activityHandler.addAndPurge({'typ': 'nxt'})
}).then(tcs, tcf).done();
}
else {
// everything else: stop searching for answers
event.which = 46; // full stop
codeq.comms.sendQuery({
- 'step': 'end'
+ 'step': 'end',
+ 'trace': activityHandler.addAndPurge({'typ': 'stp'})
}).then(tcs, tcf).done();
}
}
@@ -80,6 +81,56 @@
return {};
};
+ var makeActivityHandler = function (editor) {
+ var lastActivityMillis = Date.now(),
+ deltaActivityMillis = function deltaActivityMillisFunc () {
+ var now = Date.now(),
+ dt = Math.max(0, Math.min(30000, now - lastActivityMillis)); // 0 sec <= dt <= 30 sec
+ lastActivityMillis = now;
+ return dt;
+ },
+ queue = [],
+ ts = null,
+ timer = function () {
+ var promise;
+ ts = null;
+ if (queue.length === 0) return Q(true);
+ promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue());
+ queue.length = 0;
+ return promise;
+ },
+ flush = function () {
+ clearTimeout(ts);
+ return timer();
+ };
+
+ return {
+ "trace": function (trace) {
+ trace['dt'] = deltaActivityMillis();
+ return trace;
+ },
+ 'queue': function (trace) {
+ queue.push(trace);
+ if (ts === null) setTimeout(timer, 10000); // flush every 10 seconds
+ },
+ 'queueTrace': function (trace) {
+ this.queue(this.trace(trace));
+ },
+ 'flush': flush,
+ 'addAndPurge': function (trace) {
+ var accumulatedTrace = queue;
+ queue = [];
+ trace['dt'] = deltaActivityMillis();
+ accumulatedTrace.push(trace);
+ if (ts !== null) {
+ clearTimeout(ts);
+ ts = null;
+ }
+ return accumulatedTrace;
+ }
+ };
+ };
+
/**
* Creates a new handler for the given Prolog assignment definition.
*
@@ -93,6 +144,7 @@
jqConsole = $('#console'),
jqHints = $('#info'),
editor = CodeMirror(jqEditor[0], { cursorHeight: 0.85, lineNumbers: true, matchBrackets: true }),
+ activityHandler = makeActivityHandler(editor),
/* controller = jqConsole.console({
promptLabel: '?- ',
commandValidate: function (line) {
@@ -106,7 +158,7 @@
promptHistory: false,
welcomeMessage: 'Prolog REPL.'
}),*/
- terminal = makePrologTerminalHandler(jqConsole, editor, problem.id),
+ terminal = makePrologTerminalHandler(jqConsole, editor, problem.id, activityHandler),
/** Object.<string, HintDefinition> */
hintDefs = problem.hint,
hintCounter = 0, // for generating unique class-names
@@ -211,13 +263,6 @@
hintCleaners.push(close);
}
- },
- lastActivityMillis = Date.now(),
- deltaActivityMillis = function deltaActivityMillisFunc () {
- var now = Date.now(),
- dt = Math.max(0, Math.min(30000, now - lastActivityMillis)); // 0 sec <= dt <= 30 sec
- lastActivityMillis = now;
- return dt;
};
editor.setValue(info.solution);
@@ -226,15 +271,14 @@
editor.on('change', function (instance, changeObj) {
var doc = editor.getDoc(),
- pos = codeq.codePointCount(doc.getRange(firstCharacterPos, changeObj.from)),
- dt = deltaActivityMillis();
+ pos = codeq.codePointCount(doc.getRange(firstCharacterPos, changeObj.from));
if (changeObj.removed) {
-// codeq.comms.sendActivity({'typ': 'rm', 'dt': dt, 'off': pos, 'len': codeq.codePointCount(changeObj.removed)});
+ activityHandler.queueTrace({'typ': 'rm', 'off': pos, 'len': codeq.codePointCount(changeObj.removed)});
}
if (changeObj.text) {
-// codeq.comms.sendActivity({'typ': 'ins', 'dt': dt, 'off': pos, 'txt': changeObj.text});
+ activityHandler.queueTrace({'typ': 'ins', 'off': pos, 'txt': changeObj.text});
}
});
@@ -297,8 +341,7 @@
// $(jqButtons.get(2)).on('click', function () { handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]); });
$('#btn_code_run').on('click', function () {
- var doc = editor.getDoc(),
- dt = deltaActivityMillis();
+ var doc = editor.getDoc();
// codeq.comms.sendActivity({'typ': 'slv', 'dt': dt, 'qry': });
// handler.processServerHints([{id:'list_empty'}]);
});