-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
409 lines (381 loc) · 11.2 KB
/
filter.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/********************************
*
* Formats the filter and filters the state
*
* Input: stateCopy = the current execution state
*
* Output: filteredState = {t: boolean (true if match exists, false otherwise)
* S: stateCopy\M where M is a subset of stateCopy which matches the filter}
*
********************************/
function depricatedRunFilter(stateCopy, filters) {
let formattedFilter = getFilter({ op: '', p1: '', p2: '' }, filters)
return filteredState = quotient({ t: true, S: stateCopy }, formattedFilter);
}
/*****************************
*
* Recursively constructs filter from user interface.
* Input: filter = {op: + (or), x (and), or p (unary predicate)
* p1: left child
* p2: right child}
* filters : list of filters from the UI
*
* Output: filter = {op: + (or), x (and), or p (unary predicate)
* p1: left child
* p2: right child}
*
****************************/
function depricatedGetFilter(filter, filters) {
if (filters.length == 1) {
filter = { op: 'p', p1: filters.pop() }
}
else if (filters.length > 1) {
filter.p1 = { op: 'p', p1: filters.pop() }
filter.p2 = getFilter({ op: '', p1: '', p2: '' }, filters);
}
return filter;
}
/*
* Quotient function takes a multiset S and the tree structure of a filter pattern and attempts
* to find a match M which satisfies the pattern in S, returning S\M.
*
* Input: delta = {t:boolean, S: multiset (implemented as an array)}
* filterTree = {op: operation type, p1: left child nodes, p2: right child nodes}
*
* Output: delta' = {t: True if successful, False otherwise,
* S: delta.S if delta'.t = false, otherwise delta.S\M for some match M}
*/
function depricatedQuotient(delta, filterTree) {
//if a recursive call returns false, or the filterTree is empty, return delta
if (!delta.t || filterTree == '')
return delta
//if the highest level operator in the tree is an or
if (filterTree.op == '+') {
//recurse on left and right child trees
let delta1 = quotient(delta, filterTree.p1)
let delta2 = quotient(delta, filterTree.p2)
//returns false if both fail, true otherwise. S is the multiset intersection of delta1.S and delta2.S.
//note if both fail, delta1.S = delta2.S = delta.S
return { t: delta1.t || delta2.t, S: intersection(delta1.S, delta2.S) }
}
//if the highest level operator in the tree is an and
else if (filterTree.op == 'x') {
//recurse on left child tree
let delta1 = quotient(delta, filterTree.p1)
//if left child has a match, continue
if (delta1.t) {
//recurse on right child, with delta.S\M1 where M1 is the match for the left child
let delta2 = quotient(delta1, filterTree.p2)
//if right child has a match
if (delta2.t)
//return success
return delta2
}
//otherwise return failure
return { t: false, S: delta.S }
}
//if no operator is present, this tree node is a predicate (left child is a leaf node)
else if (filterTree.op == 'p') {
//if delta.S contains the predicate
if (delta.S.indexOf(filterTree.p1) >= 0) {
//we don't want to actually modify delta.S so we duplicate
let temp = delta.S.slice(0)
//remove the predicate from the duplicated delta.S
temp.splice(temp.indexOf(filterTree.p1), 1)
//return success
return { t: true, S: temp }
}
//otherwise return failure
else
return { t: false, S: delta.S }
}
}
/*
* Finds the intersection of two multiset arrays.
*
* Input: arr1, arr2.
*
* Output: returnArr, the multiset intersection of arr1 and arr2.
* ({x^m| x^m1 in arr1, x^m2 in arr2, m=min(m1, m2)})
*/
function intersection(arr1, arr2) {
//duplicate the arrays
let work1 = arr1.slice(0)
let work2 = arr2.slice(0)
//create return array
let returnArr = []
//iterate through the arrays
for (let x of work1) {
//if both arrays contain the element
if (work2.indexOf(x) >= 0) {
//remove it from the temporary array
work2.splice(work2.indexOf(x), 1)
//and include it in the return array
returnArr.push(x)
}
}
return returnArr
}
var newStates = [];
function getFilter(state, filters) {
newStates = [];
for (let filter of Object.keys(filters)){
filter = filters[filter]
getANDFilter(state, filter)
}
}
function getFilterState(currentState, filteredState){
let dupeState = currentState.slice(0)
for (let atom of filteredState) {
for (let i = 0; i<dupeState.length; i++){
if (atom == dupeState[i]){
dupeState.splice(i,1)
break;
}
}
}
return dupeState;
}
function updateFilterState(currentState){
let i = 0;
let printer = document.getElementById("filterPrinter")
if(newStates.length == 0){
printer.innerHTML = "";
return;
}
let intState = newStates[0].slice(0);
while (i+1<newStates.length){
intState = intersection(intState,newStates[++i])
}
let filteredState = getFilterState(currentState, intState);
let newState = "";
for (let atom of filteredState) {
newState += '(' + atom.name + ' '
for (let arg of atom.arguments)
newState += arg.arg + ' '
newState = newState.substring(0, newState.length - 1) + ')'
}
newState += '<font style="font-weight:800;">\nEnd of State</font>'
if (possibleTransitions.length == 0)
newState += '<font style="font-weight:800;">\nQuiescence</font>'
printer.innerHTML = newState
printer.scrollTop = printer.scrollHeight
}
function getANDFilter(currentState, filters) {
let andFilter = { id: "andFilter", name: "filter.name", fixedPredicates: [], remainingPredicates: [], arguments: [] };
for (let pred of filters) {
let dupPred = { name: pred.name, arguments: [], hidden: pred.hidden};
for (let arg of pred.arguments) {
dupPred.arguments.push({ arg: arg.arg, variable: arg.variable });
if (!getTransitionArg(arg.arg, andFilter.arguments))
andFilter.arguments.push({ id: arg.arg, arg: arg.arg, type: arg.type, fixed: !arg.variable });
}
andFilter.remainingPredicates.push(dupPred);
}
lockFilter(andFilter, currentState, []);
}
//Tries to lock the filter given the current state
function lockFilter(filter, currentState, hiddenPredicates) {
//if no remaining conditions to fix, then the transition is possible
if (filter.remainingPredicates.length == 0) {
newStates.push(currentState.concat(hiddenPredicates))
return;
}
//otherwise, try to fix an element of the remaining conditions
let currentPredicate = filter.remainingPredicates.pop();
filter.fixedPredicates.push(currentPredicate);
for (let atom of currentState) {
let valid = true;
if (atom.name == currentPredicate.name) {
for (let i = 0; i < atom.arguments.length; i++) {
let arg = getTransitionArgument(currentPredicate.arguments[i].arg, filter.arguments);
valid = valid && ((atom.arguments[i].arg == arg.arg) || !arg.fixed)
}
if (valid) {
let newFilter = duplicateFilter(filter);
let newState = currentState.slice(0);
newState.splice(newState.indexOf(atom), 1);
for (let i = 0; i < atom.arguments.length; i++) {
let arg = getTransitionArgument(currentPredicate.arguments[i].arg, newFilter.arguments);
arg.arg = atom.arguments[i].arg;
arg.fixed = true;
arg.type = atom.arguments[i].type;
}
if (currentPredicate.hidden == 'hide')
hiddenPredicates.push(atom)
lockFilter(newFilter, newState, hiddenPredicates);
}
}
}
}
/**
* duplicates an existing transition
*/
function duplicateFilter(filter) {
let newFilter = { id: filter.id, name: filter.name, fixedPredicates: [], remainingPredicates: [], arguments: [] };
//duplicate remaining conditions
for (let pred of filter.remainingPredicates) {
let dupPred = { name: pred.name, arguments: [], hidden: pred.hidden };
for (let arg of pred.arguments)
dupPred.arguments.push({ arg: arg.arg, variable: arg.variable });
newFilter.remainingPredicates.push(dupPred);
}
//duplicate fixed conditions
for (let pred of filter.fixedPredicates) {
let dupPred = { name: pred.name, arguments: [], hidden: pred.hidden};
for (let arg of pred.arguments)
dupPred.arguments.push({ arg: arg.arg, variable: arg.variable });
newFilter.fixedPredicates.push(dupPred);
}
//duplicate arguments
for (let arg of filter.arguments) {
let dupArg = { id: arg.id, arg: arg.arg, type: arg.type, fixed: arg.fixed };
newFilter.arguments.push(dupArg);
}
return newFilter;
}
testSet = ['a', 'b', 'b', 'c', 'd', 'a']
testPattern1 = {
op: 'x',
p1: {
op: 'p',
p1: 'd'
},
p2: {
op: 'p',
p1: 'c'
}
}
testPattern2 = {
op: 'x',
p1: {
op: 'p',
p1: 'e'
},
p2: {
op: 'p',
p1: 'c'
}
}
testPattern3 = {
op: 'x',
p1: {
op: 'p',
p1: 'a'
},
p2: {
op: 'p',
p1: 'e'
}
}
testPattern4 = {
op: 'x',
p1: {
op: 'p',
p1: 'c'
},
p2: {
op: 'p',
p1: 'c'
}
}
testPattern5 = {
op: '+',
p1: {
op: 'x',
p1: {
op: 'p',
p1: 'a'
},
p2: {
op: 'p',
p1: 'b'
}
},
p2: {
op: 'x',
p1: {
op: 'p',
p1: 'b'
},
p2: {
op: 'p',
p1: 'c'
}
}
}
testPattern6 = {
op: '+',
p1: {
op: 'x',
p1: {
op: 'p',
p1: 'a'
},
p2: {
op: 'p',
p1: 'b'
}
},
p2: {
op: 'x',
p1: {
op: 'p',
p1: 'e'
},
p2: {
op: 'p',
p1: 'c'
}
}
}
testPattern7 = {
op: '+',
p1: {
op: 'x',
p1: {
op: 'p',
p1: 'e'
},
p2: {
op: 'p',
p1: 'b'
}
},
p2: {
op: 'x',
p1: {
op: 'p',
p1: 'a'
},
p2: {
op: 'p',
p1: 'c'
}
}
}
testPattern8 = {
op: '+',
p1: {
op: 'x',
p1: {
op: 'p',
p1: 'e'
},
p2: {
op: 'p',
p1: 'b'
}
},
p2: {
op: 'x',
p1: {
op: 'p',
p1: 'e'
},
p2: {
op: 'p',
p1: 'c'
}
}
}