forked from tripperroc/malv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFA.js
executable file
·274 lines (214 loc) · 6.43 KB
/
DFA.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
/**
*
*/
// ---------- VARS -----------
var Turing = false;
// List of states
var Qstates = [];
// Start State
var Qzero = null;
// Accept States
var FStates = [];
// input
// step state information
var currentState;
var prevState;
var boxInput = "";
var error = "";
var animate = false; // this boolean will be toggled for running the DFA in check or debug mode.
var alerts = true; // this boolean will turn on and off alerts because the annoy me.
// -------- FUNCTIONS --------
// s is an index in the input string, newInput is if it hasn't been proceessed before
function step(s,newInput){
// get next state from currentState with next character
nextState = getNextState( currentState, inputList[s] );
if( nextState == null ){
alert("Failure, no transition found");
if(newInput){setAcceptedForInput(AcceptedForInput.NOTACCEPTED);}
return AcceptedForInput.NOTACCEPTED;
}
// reset states
prevState = currentState;
currentState = nextState;
nextState = null;
}
function isAccepted(input,newInput){
// if machine ends in accept state
if( $.inArray(currentState, FStates) != -1 ){
alert("Machine completed in accept " + currentState.label + " State for string " + input);
if(newInput){setAcceptedForInput(AcceptedForInput.ACCEPTED);}
return AcceptedForInput.ACCEPTED;
}
alert("Not accepted, \n finished in state " + currentState.label + " for string " + input);
if(newInput){setAcceptedForInput(AcceptedForInput.NOTACCEPTED);}
return AcceptedForInput.NOTACCEPTED;
}
function readInput(input,newInput){
// Check valid machine state
if(newInput){displayInputs(input,true);}
if( !checkValidMachine() ){
alert("Invalid Machine state: " + error);
if(newInput){setAcceptedForInput(AcceptedForInput.IMPOSSIBLE);}
return AcceptedForInput.IMPOSSIBLE;
}
// DEBUG
//console.log(input);
// set up initial states
currentState = Qzero;
prevState = null;
nextState = null;
inputList = input.split("");
// step through input
for( s in inputList ){
if(step(s,newInput)==AcceptedForInput.NOTACCEPTED){
return AcceptedForInput.NOTACCEPTED;
}
}
// return if it was accepted or not
return isAccepted(input,newInput);
}
function checkValidMachine(){
// set up error message
error = "";
var isValid = true;
if(Qzero == null){
isValid = false;
error += "\n No Start State set";
}
if(FStates[0] == null){
isValid = false;
error += "\n No Accept States set";
}
return isValid;
// FIX THIS
}
var animatedInput = 0; // index for reading input
function readInputAnimated(input){
// set up recursive loop
setTimeout(function(){
ctx.fillStyle="#B7AA86";
ctx.fillRect(0,0,c.width,c.height);
// draws the machine, needs to be done because we stopped the update
drawMachine();
drawReadingCharacters(animatedInput);
drawHighlighted(currentState.x,currentState.y,currentState.radius+3);
if(animatedInput == inputList.length){ // at the end of the input list
if( $.inArray(currentState, FStates) != -1 ){
alert("Machine completed in accept State");
setAcceptedForInput(AcceptedForInput.ACCEPTED);
animating = false; // updates can start drawing again
return;
}
else{
alert("Not accepted, \n finished in state" + currentState.label);
setAcceptedForInput(AcceptedForInput.NOTACCEPTED);
animating = false; // updates can start drawing again
return;
}
}
// step with current input
step(animatedInput,false);
animatedInput++;
// calls readInputAnimated, replaces for loop
requestAnimationFrame(readInputAnimated); // basic recursion
},1000); // updates every once every 1 second
}
// Draws the current machine state
function drawMachine(){
for(var i=0; i<Qstates.length; i++){
Qstates[i].display();
}
if( drawingTran == true && clickedState != null && pm == PlacementMode.TRANSITION ){
line(clickedState.x, clickedState.y, mouseX, mouseY, ctx);
ctx.fillText(lastKeyCode,mouseX,mouseY);
}
}
// steps through machine
function debugInput(){
animatedInput = 0;
animating = true; // stops updating
input = document.getElementById('input').value;
displayInputs(input,true);
// DEBUG
//console.log(input);
if( !checkValidMachine() ){
alert("Invalid Machine state: " + error);
animating = false;
setAcceptedForInput(AcceptedForInput.IMPOSSIBLE);
return false;
}
// set up states
currentState = Qzero;
prevState = null;
nextState = null;
inputList = input.split("");
boxInput = document.getElementById('input').value;
// start animating through input
readInputAnimated(boxInput);
}
// just checks input and tells user of accept or reject
function checkInput(){
boxInput = document.getElementById('input').value;
console.log(boxInput);
readInput(boxInput,true);
}
// draws an ellipse
function drawHighlighted(X,Y,R){
ctx.strokeWidth = 1;
ctx.strokeStyle = '#00FF00';
ctx.clear;
ellipse(X,Y,R);
}
function setSelectedAsStart(){
if( selectedState != null ){
clearAccepted();
Qzero = selectedState;
//toggle this for display
}
}
// toggles accept on and off
function setSelectedAsAccept(){
if( selectedState != null ){
clearAccepted();
if( $.inArray(selectedState, FStates) != -1){
i = FStates.indexOf(selectedState);
if(i != -1){
FStates.splice(i,1);
}
}
else{
FStates.push(selectedState);
//toggle this for display
selectedState.isAccept = !selectedState.isAccept;
console.log(selectedState.label + " is accept? " +selectedState.isAccept);
}
}
}
function getNextState( current, inputChar){
// Find the state at the end of the transition that matches the input Character
// return that state ( or NULL )
next = current.transitions[inputChar];
return next;
}
function clearSelectedState(){
if( selectedState == null){
return;
}
var index = Qstates.indexOf(selectedState);
Qstates.splice(index,1);
selectedState = null;
}
function clearSelectedTransitions(){
if( selectedState == null){
return;
}
selectedState.transitions = {};
}
// takes array of all previous inputs and rechecks them with a new machine
function checkAllPreviousInput(){ // array should all be strings
clearAccepted();
alerts = false;
for(i in checkedInputs){
didAccept[i] = readInput(checkedInputs[i],false);
}
}