-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
302 lines (276 loc) · 7.8 KB
/
sketch.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
class State {
constructor(name, x, y) {
this.name = name;
this.x = x;
this.y = y;
this.transitions = [];
}
addTransition(toState, input) {
this.transitions.push({ to: toState, input: input });
}
}
let FSM1 = {
q0: new State("q0", 150, 200),
q1: new State("q1", 275, 125),
q2: new State("q2", 400, 200),
q3: new State("q3", 550, 125),
q4: new State("q4", 550, 250),
q5: new State("q5", 700, 125),
q6: new State("q6", 700, 250),
q7: new State("q7", 800, 200),
q8: new State("q8", 900, 200),
};
FSM1.q0.addTransition(FSM1.q1, "a");
FSM1.q0.addTransition(FSM1.q2, "b");
FSM1.q1.addTransition(FSM1.q2, "a");
FSM1.q1.addTransition(FSM1.q2, "b");
FSM1.q2.addTransition(FSM1.q4, "b");
FSM1.q2.addTransition(FSM1.q3, "a");
FSM1.q3.addTransition(FSM1.q4, "b");
FSM1.q4.addTransition(FSM1.q3, "a");
FSM1.q3.addTransition(FSM1.q5, "a");
FSM1.q4.addTransition(FSM1.q6, "b");
FSM1.q5.addTransition(FSM1.q4, "b");
FSM1.q6.addTransition(FSM1.q3, "a");
FSM1.q5.addTransition(FSM1.q7, "a");
FSM1.q6.addTransition(FSM1.q7, "b");
FSM1.q7.addTransition(FSM1.q8, "a");
FSM1.q7.addTransition(FSM1.q8, "b");
FSM1.q8.addTransition(FSM1.q8, "a");
FSM1.q8.addTransition(FSM1.q8, "8");
let FSM2 = {
q0: new State("q0", 150, 200),
q1: new State("q1", 250, 125),
q2: new State("q2", 250, 275),
q3: new State("q3", 325, 200),
q4: new State("q4", 525, 200),
q5: new State("q5", 650, 125),
q6: new State("q6", 650, 250),
q7: new State("q7", 800, 200),
q8: new State("q8", 900, 200),
};
FSM2.q0.addTransition(FSM2.q1, "1");
FSM2.q0.addTransition(FSM2.q2, "0");
FSM2.q1.addTransition(FSM2.q3, "0");
FSM2.q1.addTransition(FSM2.q4, "1");
FSM2.q2.addTransition(FSM2.q3, "0");
FSM2.q2.addTransition(FSM2.q4, "0");
FSM2.q3.addTransition(FSM2.q4, "0");
FSM2.q3.addTransition(FSM2.q4, "1");
FSM2.q4.addTransition(FSM2.q5, "1");
FSM2.q4.addTransition(FSM2.q6, "0");
FSM2.q5.addTransition(FSM2.q6, "0");
FSM2.q5.addTransition(FSM2.q7, "1");
FSM2.q6.addTransition(FSM2.q5, "1");
FSM2.q6.addTransition(FSM2.q7, "0");
FSM2.q7.addTransition(FSM2.q8, "0");
FSM2.q7.addTransition(FSM2.q8, "1");
FSM2.q8.addTransition(FSM2.q8, "1");
FSM2.q8.addTransition(FSM2.q8, "0");
let currentState = null;
let isDragging = false;
let selectedState = null;
let currentFSM = null;
let finalState = {
FSM1: FSM1.q8,
FSM2: FSM2.q8,
};
function setup() {
let canvas = createCanvas(1000, 400);
canvas.parent("myContainer");
textAlign(CENTER, CENTER);
textSize(20);
showFSM2();
}
function draw() {
background(220);
if (currentFSM) {
// Draw transitions as arrows
for (let stateName in currentFSM) {
let state = currentFSM[stateName];
for (let t of state.transitions) {
drawArrow(state, t.to, t.input);
}
}
// Draw states as ellipses
for (let stateName in currentFSM) {
let state = currentFSM[stateName];
fill(state === currentState ? color(0, 255, 0) : 200);
ellipse(state.x, state.y, 50, 50);
fill(0);
text(state.name, state.x, state.y);
}
}
if (currentFSM == FSM1) {
text("a", 200, 150);
text("b", 275, 185);
text("a,b", 350, 150);
text("a", 475, 150);
text("a", 560, 155);
text("a", 595, 150);
text("b", 595, 230);
text("b", 475, 215);
text("b", 540, 220);
text("b", 625, 240);
text("a", 625, 115);
text("a", 750, 150);
text("b", 750, 240);
text("a,b", 850, 190);
text("a,b", 950, 165);
} else if (currentFSM == FSM2) {
text("1", 190, 155);
text("0", 190, 245);
text("1,0", 405, 190);
text("1", 405, 155);
text("0", 405, 245);
text("0", 300, 160);
text("1", 300, 240);
text("0", 580, 235);
text("1", 580, 155);
text("0", 640, 220);
text("1", 660, 160);
text("1", 725, 150);
text("0", 725, 240);
text("1,0", 850, 190);
text("1,0", 950, 165);
}
}
function drawArrow(start, end, label) {
let angle = atan2(end.y - start.y, end.x - start.x);
let arrowSize = 10;
let curveDistance = 50; // Distance of the curve from the state
push();
translate(start.x, start.y);
rotate(angle);
if (start === end) {
// Loop back to the same state with a curve
let x1 = 0;
let y1 = -25;
let x2 = 20;
let y2 = -20;
let cx1 = x1 + curveDistance;
let cy1 = y1 - curveDistance;
let cx2 = x2 + curveDistance;
let cy2 = y2 - curveDistance;
noFill();
stroke(0);
strokeWeight(1);
bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2);
// Draw the arrowhead
fill(0);
triangle(
x2 - arrowSize,
y2,
x2,
y2 - arrowSize / 2,
x2,
y2 + arrowSize / 2
);
} else {
// Regular arrow
let lineLength = dist(0, 0, end.x - start.x, end.y - start.y) - 25;
// Line
line(0, 0, lineLength, 0);
// Arrowhead
fill(0);
triangle(
lineLength - arrowSize,
0,
lineLength - arrowSize,
arrowSize / 2,
lineLength,
0
);
triangle(
lineLength - arrowSize,
0,
lineLength - arrowSize,
-arrowSize / 2,
lineLength,
0
);
}
fill(0);
pop();
}
function showFSM1() {
currentState = FSM1.q0;
currentFSM = FSM1;
}
function showFSM2() {
currentState = FSM2.q0;
currentFSM = FSM2;
}
function validateInput(inputStr) {
const pattern = /^[ab]*$/;
return pattern.test(inputStr);
}
function validateInput2(inputStr) {
const pattern = /^[10]*$/;
return pattern.test(inputStr);
}
function simulate() {
let input5 = document.getElementById("input5").value;
document.getElementById("transition").innerHTML = "";
document.getElementById("valid").innerHTML = "";
if (currentFSM === FSM1) {
if (!validateInput(input5)) {
console.log("fuck no tis the wrong character");
document.getElementById("transition").innerHTML =
"Please only input a or b characters";
} else {
currentState = currentFSM.q0;
simulateInput(input5);
document.getElementById("transition").innerHTML = "";
}
} else {
if (!validateInput2(input5)) {
console.log("fuck no tis the wrong character");
document.getElementById("transition").innerHTML =
"Please only input 1 or 0 characters";
} else {
currentState = currentFSM.q0;
simulateInput(input5);
document.getElementById("transition").innerHTML = "";
}
}
}
function simulateInput(input) {
let i = 0;
let isValid = true;
let finalStates = finalState[currentFSM === FSM1 ? "FSM1" : "FSM2"];
let interval = setInterval(() => {
if (i >= input.length || !isValid) {
clearInterval(interval);
if (isValid && currentState === finalStates) {
document.getElementById("valid").innerHTML = "Input is valid!";
console.log("Input is valid!");
} else {
document.getElementById("valid").innerHTML = "Input is not valid!";
console.log("Input is not valid!");
}
return;
}
let key = input.charAt(i);
let validTransition = false;
for (let t of currentState.transitions) {
if (t.input === key) {
currentState = t.to;
validTransition = true;
document.getElementById("transition").innerHTML +=
" " + currentState.name + " " + t.input + " ---> ";
break;
}
}
if (!validTransition) {
console.log(
`No valid transition for input '${key}' from state '${currentState.name}'.`
);
isValid = false;
clearInterval(interval);
return;
}
console.log(`Input: ${key}, Current State: ${currentState.name}`);
i++;
}, 1000); // Adjust the delay (in milliseconds) between transitions here
}