-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserio.js
More file actions
369 lines (338 loc) · 9.58 KB
/
userio.js
File metadata and controls
369 lines (338 loc) · 9.58 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Gets user's input formula, interprets it on model, and prints out the evaluation
// history to the sout div.
function processFormula() {
if(DOMAIN.length==0) {
return outputErr('ERROR: you must first load a model.','sout');
}
var s = document.getElementById('sin').value;
s = s.replace(/ /g,'');
if(s=='') {
return outputErr('ERROR: you have not entered a string.','sout');
}
var t = parse(s);
if(t.length==0) {
t = parse('('+s+')');
if(t.length==0) {
return outputErr('ERROR: Malformed formula.','sout');
}
}
try {
var l = insertDen(t,[]);
var i = interpret(l)(getG());
var out = printout(i,0);
out = i[1]+" is "+i[0]+" on this model\n\n \nEvaluation History:\n-------------------\n\n"+out;
} catch(err) {
outputErr(err,'sout');
return;
}
document.getElementById('sout').innerHTML = '<pre>'+out+'</pre>';
function getG() {
var x = Object.keys(CONSTANTS);
var out = {};
for(var i=0;i<x.length;i++) {
out[x[i]] = CONSTANTS[x[i]];
}
return out;
}
}
// Array, Int -> String
// Takes an evaluation tree as returned by interpret(), together with an int representing
// the depth of the current tree, and returns a string representation of the evaluation
// history.
function printout(a,d) {
if(Array.isArray(a[0])) {
var out = "";
for(var i=0;i<a.length;i++) {
out = out+printout(a[i],d);
}
return out;
}
if(a.length==3) {
return getspaces(d)+a[1]+" is "+a[0]+" on "+JSON.stringify(a[2])+"\n";
}
if(a.length==4) {
return getspaces(d)+a[1]+" is "+a[0]+" on "+JSON.stringify(a[2])+"\n"+printout(a[3],d+1);
}
if(a.length==5) {
return getspaces(d)+a[1]+" is "+a[0]+" on "+JSON.stringify(a[2])+"\n"+printout(a[3],d+1)+printout(a[4],d+1);
}
}
function getspaces(d) {
var out = "";
for(var i=0;i<d;i++) {
out = out+" ";
}
return out;
}
// Generate Model From User Input
// ================================
// Attempts to load model entered by user.
function loadModel() {
clearGlobals();
var dm = document.getElementById('d').value.split(' ').join('');
try {
mkMdl('domain',dm);
} catch(err) {
outputErr(err,'mout');
return;
}
var e = document.getElementById('model');
var lis = e.getElementsByTagName('li');
for(var i=1;i<lis.length;i++) {
var l = document.getElementById(i+'l').value.split(' ').join('');
var r = document.getElementById(i+'r').value.split(' ').join('');
if(!l=='') {
try {
mkMdl(l,r);
} catch(err) {
outputErr(err,'mout');
return;
}
}
}
printModel(model);
}
// Adds the information from the user's model to the global variables storing the model
// information (domain, predicate extensions etc.)
function mkMdl(n,s) {
if(n=='domain') {
if(s.length==0) {
throw 'ERROR: domain must be nonempty';
}
if(!ck1Ext(s)) {
throw 'ERROR: domain must consist of a list of integers.';
}
DOMAIN = s.split(',').map(function (x) {return parseInt(x);});
return DOMAIN.sort();
}
if(n.length==1 && 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(n)>=0) {
if(s=='T') {
SENTENCES[n] = true;
return;
}
if(s=='F') {
SENTENCES[n] = false;
return;
}
if(s=='') {
PREDICATES1[n] = [];
PREDICATES2[n] = [];
return;
}
if(ck1Ext(s)) {
var ext = [];
if(s.length>0) {
ext = s.split(',').map(function (x) {return parseInt(x);})
}
if(!subset(ext,DOMAIN)) {
throw 'ERROR: extensions for 1-place predicates must be subsets of the domain.';
}
PREDICATES1[n] = ext;
return;
}
if(ck2Ext(s)) {
var ext = g2Ext(s);
for(var j=0;j<ext.length;j++) {
if(!subset(ext[j],DOMAIN)) {
throw 'ERROR: extensions of 2-place predicates must be pairs of domain elements.';
}
}
PREDICATES2[n] = ext;
return;
} else {
throw 'ERROR: the extension entered for '+n+' is not recognized.';
}
}
if(isC(n)) {
var ext = parseInt(s);
if(s<0) {
throw 'ERROR: the extension of the individual constant '+n+' must be a positive integer.';
}
if(!isin(ext,DOMAIN)) {
throw 'ERROR: the extension of the individual constant '+n+' must be an element of the domain.';
}
CONSTANTS[n] = ext;
} else {
throw 'ERROR: the symbol '+n+' in the model specification is not a recognized lexical item.';
}
}
// checks if string is a well-formed representation of a 1-place predicate extension
function ck1Ext(s) {
if(s.length==0) {return true;}
if(!isInt(s.charAt(0))) {return false;}
for(var i=0;i<s.length;i++) {
if(isInt(s.charAt(i))) {
continue;
} else if(s.charAt(i)==',' && isInt(s.charAt(i+1))) {
continue;
} else {return false;}
}
return true;
}
// checks if string is a well-formed representation of a 2-place predicate extension
function ck2Ext(s) {
if(s.length==0) {return true;}
if(!(s.charAt(0)=='(')) {return false;}
var stk = [','];
for(var i=0;i<s.length;i++) {
if(s.charAt(i)=='(' && stk[stk.length-1]==',') {
stk = ['('];
continue;
}
if(s.charAt(i)==',' && stk[stk.length-1]=='i' && stk[stk.length-2]=='(') {
stk.push(',');
continue;
}
if(s.charAt(i)==')' && stk[stk.length-1]=='i' && stk[stk.length-2]==',') {
stk = [];
continue;
}
if(s.charAt(i)==',' && stk.length==0) {
stk.push(',');
continue;
}
if(isInt(s.charAt(i))) {
if(stk[stk.length-1]=='i') {continue;} else {stk.push('i');}
} else {return false;}
}
if(stk.length==0) {return true;} else {return false;}
}
// produces an array of pairs from a string representation of a 2-place extension
function g2Ext(s) {
var out = [];
var sub = '';
while(s.length>0) {
var l = s.substring(1,s.indexOf(','));
var r = s.substring(s.indexOf(',')+1,s.indexOf(')'));
out.push([parseInt(l),parseInt(r)]);
if(s.substring(1,s.length).indexOf('(')==-1) {
break;
} else {
s = s.substring(s.indexOf(')')+2,s.length);
}
}
return out;
}
// determines if char x represents an int
function isInt(x) {
return parseInt(x)>=0;
}
// Clears the model from the global variables
function clearGlobals() {
DOMAIN = [];
CONSTANTS = {};
SENTENCES = {};
PREDICATES1 = {};
PREDICATES2 = {};
}
// Clears the model from the html interface
function clearModel() {
clearGlobals();
document.getElementById('mout').innerHTML = '';
document.getElementById('sout').innerHTML = '';
document.getElementById('d').value = '';
var e = document.getElementById('model');
var lis = e.getElementsByTagName('li').length;
for(var i=1;i<lis;i++) {
var x = document.getElementById(i+'l');
x.value = '';
document.getElementById(i+'r').value = '';
if(i==4) {
var span = document.getElementById('add4');
span.innerHTML = '';
var newbutton = document.createElement('input');
newbutton.type = 'button';
newbutton.value = '+';
newbutton.className = 'addbutton';
newbutton.onclick = function() {return addLine(5);};
span.appendChild(newbutton);
}
if(i>4) {
document.getElementById('model').removeChild(x.parentNode);
}
}
}
// Loads sample model
function loadSample() {
document.getElementById('d').value = '1,2,3';
document.getElementById('1l').value = 'R';
document.getElementById('1r').value = '(1,1),(1,2),(1,3),(2,2),(3,3)';
document.getElementById('2l').value = 'F';
document.getElementById('2r').value = '1,3';
document.getElementById('3l').value = 'a';
document.getElementById('3r').value = '1';
document.getElementById('4l').value = 'P';
document.getElementById('4r').value = 'F';
loadModel();
}
// Prints an error to the html element with the given id
function outputErr(err,id) {
var outStr = '<p style=\'color:red;\'>' + err + '</p>';
document.getElementById(id).innerHTML = outStr;
}
// Prints the loaded model to the html interface.
function printModel(m) {
var outStr = '<p><b>Loaded Model</b></p>';
outStr += '<ul style="list-style:none;padding:0px;"><li>Domain: '+JSON.stringify(DOMAIN)+'</li>';
var preds2 = Object.keys(PREDICATES2);
if(preds2.length>0) {
for(var i=0;i<preds2.length;i++) {
outStr += '<li>'+preds2[i]+': '+mkSeq(JSON.stringify(PREDICATES2[preds2[i]]))+'</li>';
}
}
var preds1 = Object.keys(PREDICATES1);
if(preds1.length>0) {
for(var i=0;i<preds1.length;i++) {
if(preds2.indexOf(preds1[i])>=0) {
continue;
} else {outStr += '<li>'+preds1[i]+': '+JSON.stringify(PREDICATES1[preds1[i]])+'</li>';}
}
}
var cons = Object.keys(CONSTANTS);
if(cons.length>0) {
for(var i=0;i<cons.length;i++) {
outStr += '<li>'+cons[i]+': '+CONSTANTS[cons[i]]+'</li>';
}
}
var sens = Object.keys(SENTENCES);
if(sens.length>0) {
for(var i=0;i<sens.length;i++) {
outStr += '<li>'+sens[i]+': '+SENTENCES[sens[i]]+'</li>';
}
}
document.getElementById('mout').innerHTML = outStr+'</ul>';
// Takes a JSON.stringify() representation of an array of arrays and changes square
// brackets into parentheses
function mkSeq(s) {
s = s.substring(1,s.length-1);
s = s.replace(/\[/g,'(');
s = s.replace(/\]/g,')');
return '['+s+']';
}
}
// Adds another line to the "model input" fields in the html interface
function addLine(n) {
var newli = document.createElement('li');
var newin1 = document.createElement('input');
newin1.type = 'text';
newin1.className = 'inL'
newin1.id = n+'l';
newli.appendChild(newin1);
var newin2 = document.createElement('input');
newin2.type = 'text';
newin2.className = 'inR'
newin2.id = n+'r';
newli.appendChild(newin1);
newli.appendChild(newin2);
var newspan = document.createElement('span');
newspan.id = 'add'+n;
var newbutton = document.createElement('input');
newbutton.type = 'button';
newbutton.value = '+';
newbutton.className = 'addbutton';
newbutton.onclick = function() {return addLine(n+1);};
newspan.appendChild(newbutton);
newli.appendChild(newspan);
document.getElementById('add'+(n-1)).innerHTML = "";
document.getElementById('model').appendChild(newli);
}