-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution.js
334 lines (309 loc) · 12.6 KB
/
execution.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
/***********************************************
*
* Code for the execution section below
*
**********************************************/
var transitionIndex = 0;
var possibleTransitions = [];
var pastStates = []
/***********************************************
*
* Guided loop functions start
*
**********************************************/
function initializeExecution() {
let initialState = createInitialStateFromAtoms();
let printer = document.getElementById('statePrinter')
printer.innerHTML += '<font style="color: darkred; font-weight:800;">Starting new execution.</font> \n';
pastStates.unshift(initialState);
getTransitions(initialState);
populateTransitionSelector(possibleTransitions)
printState(initialState)
getFilter(initialState, filters)
updateFilterState(initialState)
}
function transisitionSelected() {
var transitionSelector = document.getElementById('transitionViewer');
if (transitionSelector.selectedIndex != -1) {
transition = possibleTransitions[transitionSelector.selectedIndex]
let nextState = transition.state;
pastStates.push(nextState)
printTransition(transition)
getTransitions(nextState);
printState(nextState)
populateTransitionSelector(possibleTransitions);
getFilter(nextState, filters)
updateFilterState(nextState)
}
}
function populateTransitionSelector(transitions) {
let selector = document.getElementById('transitionViewer');
selector.options.length = 0
for (let transition of possibleTransitions) {
let option = document.createElement('option');
let optionText = transition.transition.name + " ";
let usedID = []
for (let arg of transition.transition.arguments) {
if (usedID.indexOf(arg.id) <= -1) {
optionText += arg.arg + ' '
usedID.push(arg.id)
}
}
option.innerHTML = optionText;
selector.appendChild(option);
}
}
/***********************************************
*
* Guided loop functions end
*
**********************************************/
function unguidedExecutionLoop(numAuto) {
if (numAuto === undefined)
numAuto = document.getElementById("numAuto").value
while (possibleTransitions.length != 0 && numAuto != 0) {
let nextTransition = possibleTransitions[Math.floor((Math.random() * possibleTransitions.length))];
numAuto--
//let nextState = getNextState(nextTransition);
let nextState = nextTransition.state;
printTransition(nextTransition)
getTransitions(nextState)
populateTransitionSelector(possibleTransitions);
printState(nextState)
}
}
function getNextState(stateTransition) {
let transition = stateTransition.transition;
let nextState = stateTransition.state;
for (let atom of transition.effects) {
let newGroundPred = { name: atom.name, arguments: [] }
for (let i = 0; i < atom.arguments.length; i++) {
let atomArg = getTransitionArg(atom.arguments[i].arg, atom.arguments);
newGroundPred.arguments.push(atomArg)
}
nextState.push(newGroundPred)
}
for (let pred of transition.fixedConditions) {
let predToRemove = { name: pred.name, arguments: [] }
for (let arg of pred.arguments) {
let transArg = getTransitionArgument(arg.arg, transition.arguments)
predToRemove.arguments.push(transArg.arg)
}
for (let atom of nextState) {
let match = atom.name == predToRemove.name;
if (match) {
for (let i = 0; i < atom.arguments.length; i++)
match = match && atom.arguments[i] == predToRemove.arguments[i]
}
if (match) {
let index = nextState.indexOf(atom)
nextState.splice(index, 1)
break;
}
}
}
return nextState
}
function getTransitions(currentState) {
possibleTransitions = []
for (let rule of finalRules) {
let transition = { id: rule.name, name: rule.name, fixedConditions: [], remainingConditions: [], effects: [], arguments: [] };
for (let pred of rule.conditions) {
let dupPred = { name: pred.name, arguments: [] };
for (let arg of pred.arguments) {
dupPred.arguments.push({ arg: arg.arg, type: arg.type, variable: arg.variable });
if (!getTransitionArg(arg.arg, transition.arguments))
transition.arguments.push({ id: arg.arg, arg: arg.arg, type: arg.type, fixed: !arg.variable});
}
transition.remainingConditions.push(dupPred);
}
for (let pred of rule.effects) {
let dupPred = { name: pred.name, arguments: []};
for (let arg of pred.arguments) {
dupPred.arguments.push({ arg: arg.arg, type: arg.type, variable: arg.variable });
if (!getTransitionArg(arg.arg, transition.arguments)){
let variable = typeof(arg.variable) == 'boolean' ? !arg.variable : arg.variable;
transition.arguments.push({ id: arg.arg, arg: arg.arg, type: arg.type, fixed: variable});
}
}
transition.effects.push(dupPred);
}
lockTransition(transition, currentState);
}
}
/**
* recursively fixes the variables in the transition
*/
function lockTransition(transition, currentState) {
//if no remaining conditions to fix, then the transition is possible
if (transition.remainingConditions.length == 0) {
for (let pred of transition.effects) {
for (let arg of pred.arguments){
let argument = getTransitionArgument(arg.arg, transition.arguments)
if (argument.fixed == 'expr'){
let expression = evaluate(argument.arg, transition.arguments)
argument.arg = expression;
}
}
}
for (let pred of transition.effects) {
if(isNumPred(pred.name))
continue;
let dupPred = { name: pred.name, arguments: [] };
for (let arg of pred.arguments){
dupPred.arguments.push(getTransitionArgument(arg.arg, transition.arguments))
}
currentState.push(dupPred)
}
possibleTransitions.push({ transition: transition, state: currentState })
return;
}
//otherwise, try to fix an element of the remaining conditions
let currentCondition = transition.remainingConditions.pop();
if (!isNumPred(currentCondition.name)) {
transition.fixedConditions.push(currentCondition);
for (let atom of currentState) {
let valid = true;
if (atom.name == currentCondition.name) {
for (let i = 0; i < atom.arguments.length; i++) {
let arg = getTransitionArgument(currentCondition.arguments[i].arg, transition.arguments);
valid = valid && ((atom.arguments[i].arg == arg.arg) || !arg.fixed)
}
if (valid) {
let newTransition = duplicateTransition(transition);
let newState = currentState.slice(0);
newState.splice(newState.indexOf(atom), 1);
for (let i = 0; i < atom.arguments.length; i++) {
let arg = getTransitionArgument(currentCondition.arguments[i].arg, newTransition.arguments);
arg.arg = atom.arguments[i].arg;
arg.fixed = true;
arg.type = atom.arguments[i].type;
}
lockTransition(newTransition, newState);
}
}
}
} else{
let valid = true
for (let arg of currentCondition.arguments){
valid = valid && getTransitionArgument(arg.arg, transition.arguments).fixed
}
if(!valid){
transition.remainingConditions.unshift(currentCondition)
lockTransition(transition, currentState)
} else {
let argArray = [];
for (let i=0; i<currentCondition.arguments.length; i++) {
argArray.push(getTransitionArgument(currentCondition.arguments[i].arg, transition.arguments))
}
let result = evaluateNumberPred(currentCondition.name, argArray[0].arg, argArray[1].arg)
if(result)
lockTransition(transition, currentState)
}
}
}
/**
* duplicates an existing transition
*/
function duplicateTransition(transition) {
let newTransition = { id: transition.name, name: transition.name, fixedConditions: [], remainingConditions: [], effects: [], arguments: [] };
//duplicate remaining conditions
for (let pred of transition.remainingConditions) {
let dupPred = { name: pred.name, arguments: [] };
for (let arg of pred.arguments)
dupPred.arguments.push({ arg: arg.arg, type: arg.type, variable: arg.variable });
newTransition.remainingConditions.push(dupPred);
}
//duplicate fixed conditions
for (let pred of transition.fixedConditions) {
let dupPred = { name: pred.name, arguments: [] };
for (let arg of pred.arguments)
dupPred.arguments.push({ arg: arg.arg, type: arg.type, variable: arg.variable });
newTransition.fixedConditions.push(dupPred);
}
//duplicate effects
for (let pred of transition.effects) {
let dupPred = { name: pred.name, arguments: [] };
for (let arg of pred.arguments)
dupPred.arguments.push({ arg: arg.arg, type: arg.type, variable: arg.variable });
newTransition.effects.push(dupPred);
}
//duplicate arguments
for (let arg of transition.arguments) {
let dupArg = { id: arg.id, arg: arg.arg, type: arg.type, fixed: arg.fixed };
newTransition.arguments.push(dupArg);
}
return newTransition;
}
function getTransitionArgument(id, arguments) {
for (let arg of arguments)
if (arg.id == id)
return arg;
return false;
}
function getTransitionArg(id, arguments) {
for (let arg of arguments)
if (arg.arg == id)
return arg;
return false;
}
/**
* returns true if the predicate contains a variable, false otherwise
*/
function predContainsVar(predicate) {
let containsVar = false;
for (let arg of predicate)
containsVar = containsVar || arg.variable;
return containsVar;
}
function createInitialStateFromAtoms() {
let initialState = [];
for (let atom of finalAtoms) {
let groundPred = { name: atom.name, arguments: [] }
for (let arg of atom.arguments)
groundPred.arguments.push(arg);
initialState.push(groundPred);
}
return initialState;
}
function printState(currentState) {
let printer = document.getElementById('statePrinter')
let existingStates = printer.innerHTML;
let newState = "";
for (let atom of currentState) {
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>'
existingStates += newState
existingStates += "\n\n";
printer.innerHTML = existingStates
printer.scrollTop = printer.scrollHeight
}
function printTransition(transition) {
let printer = document.getElementById('statePrinter')
let existingStates = printer.innerHTML;
let newState = '<font style="font-weight:800;">Firing rule:</font> ' + transition.transition.name;
newState += '<font style="font-weight:800;">\nConditions:</font> '
for (let atom of transition.transition.fixedConditions) {
newState += '(' + atom.name + ' '
for (let arg of atom.arguments)
newState += getTransitionArgument(arg.arg, transition.transition.arguments).arg + ' '
newState = newState.substring(0, newState.length - 1) + ')'
}
newState += '<font style="font-weight:800;">\nEffects:</font> '
for (let atom of transition.transition.effects) {
newState += '(' + atom.name + ' '
for (let arg of atom.arguments)
newState += getTransitionArgument(arg.arg, transition.transition.arguments).arg + ' '
newState = newState.substring(0, newState.length - 1) + ')'
}
newState += '<font style="font-weight:800;">\nRule fired</font>'
existingStates += newState
existingStates += "\n\n";
printer.innerHTML = existingStates
}