Skip to content

Commit 1e3bb44

Browse files
committed
afnd :(
1 parent 7e6d28b commit 1e3bb44

File tree

2 files changed

+153
-6
lines changed

2 files changed

+153
-6
lines changed

src/afnd.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { animateNode, renderError, renderOut } from "./animateNode.js";
2+
3+
function verifyAFND(paper, graph, automata, string) {
4+
// function epsilonClosure(states) {
5+
// const epsilonClosureStates = new Set(states);
6+
// const stack = Array.from(states);
7+
8+
// while (stack.length > 0) {
9+
// const state = stack.pop();
10+
11+
// if (automata.transitions[state] && automata.transitions[state]["ε"]) {
12+
// for (const nextState of automata.transitions[state]["ε"]) {
13+
// if (!epsilonClosureStates.has(nextState)) {
14+
// epsilonClosureStates.add(nextState);
15+
// stack.push(nextState);
16+
// }
17+
// }
18+
// }
19+
// }
20+
21+
// return epsilonClosureStates;
22+
// }
23+
24+
// function move(states, symbol) {
25+
// const nextStates = new Set();
26+
27+
// for (const state of states) {
28+
// setTimeout(() => {
29+
// animateNode(
30+
// paper,
31+
// graph,
32+
// state,
33+
// symbol,
34+
// automata.finalStates.includes(state)
35+
// );
36+
37+
// if (
38+
// automata.transitions[state] &&
39+
// automata.transitions[state][symbol]
40+
// ) {
41+
// for (const nextState of automata.transitions[state][symbol]) {
42+
// nextStates.add(nextState);
43+
// }
44+
// }
45+
// }, 1000);
46+
// }
47+
// return nextStates;
48+
// }
49+
50+
// let i = 0;
51+
// let currentStates = epsilonClosure(new Set([automata.initialState]));
52+
// let symbol = string[i];
53+
54+
// const interval = setInterval(() => {
55+
// currentStates = epsilonClosure(move(currentStates, symbol));
56+
57+
// if (i >= string.length) {
58+
// for (const state of currentStates) {
59+
// if (automata.finalStates.includes(state)) {
60+
// console.log("YES");
61+
// return true;
62+
// }
63+
// }
64+
65+
// clearInterval(interval);
66+
// }
67+
68+
// i++;
69+
// symbol = string[i];
70+
// }, 1000);
71+
72+
const getNextStates = (states, symbol) => {
73+
const nextStates = new Set();
74+
75+
for (const state of states) {
76+
if (automata.transitions[state] && automata.transitions[state][symbol]) {
77+
for (const nextState of automata.transitions[state][symbol]) {
78+
nextStates.add(nextState);
79+
}
80+
}
81+
}
82+
83+
return nextStates;
84+
};
85+
86+
let currentStates = [automata.initialState];
87+
88+
const verify = (states, symbol) => {
89+
let i = 0;
90+
let state = states[0];
91+
92+
const interval = setInterval(() => {
93+
animateNode(
94+
paper,
95+
graph,
96+
state,
97+
symbol,
98+
automata.finalStates.includes(state)
99+
);
100+
101+
if (i >= string.length) {
102+
clearInterval(interval);
103+
return;
104+
}
105+
106+
states = getNextStates(states, symbol);
107+
108+
if (states.length <= 0) {
109+
return;
110+
}
111+
112+
console.log("Current: ", states);
113+
114+
i++;
115+
symbol = string[i];
116+
117+
verify(states, symbol);
118+
}, 1000);
119+
};
120+
121+
verify(currentStates, string[0]);
122+
123+
return false;
124+
}
125+
126+
export { verifyAFND };

src/main.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { startDragTools } from "./dragTools.js";
77
import { initGraph } from "./graph.js";
88
import { CircleShape, FILL_NODE_FINAL } from "./shapes.js";
99
import download from "./utils/download.js";
10+
import { verifyAFND } from "./afnd.js";
1011

1112
const { graph, paper } = initGraph();
1213
const inputString = document.querySelector("#input-string");
@@ -26,7 +27,7 @@ function run() {
2627
const alphabet = [];
2728
const string = inputString.value;
2829
const statesArr = [];
29-
const transitions = [];
30+
const transitions = {};
3031

3132
// clear errors
3233
renderError(null);
@@ -38,6 +39,8 @@ function run() {
3839
id: el.attributes.id,
3940
};
4041

42+
transitions[el.attributes.attrs.label.text] = {};
43+
4144
if (el.attributes.attrs.body.fill === FILL_NODE_FINAL) {
4245
finalStates.push(el.attributes.attrs.label.text);
4346
}
@@ -48,11 +51,24 @@ function run() {
4851
if (el.type === "Link") {
4952
alphabet.push(...el.labels[0].attrs.text.text.split(","));
5053

51-
transitions.push({
52-
state: states[el.source.id].text,
53-
symbol: el.labels[0].attrs.text.text.split(",") || "transition",
54-
nextState: states[el.target.id].text,
54+
el.labels[0].attrs.text.text.split(",").forEach((symbol) => {
55+
if (transitions[states[el.source.id].text][symbol]) {
56+
transitions[states[el.source.id].text][symbol] = [
57+
...transitions[states[el.source.id].text][symbol],
58+
states[el.target.id].text,
59+
];
60+
} else {
61+
transitions[states[el.source.id].text][symbol] = [
62+
states[el.target.id].text,
63+
];
64+
}
5565
});
66+
67+
// transitions[state] = {
68+
// state: states[el.source.id].text,
69+
// symbol: el.labels[0].attrs.text.text.split(",") || "transition",
70+
// nextState: states[el.target.id].text,
71+
// };
5672
}
5773
});
5874

@@ -74,9 +90,14 @@ function run() {
7490
automata.finalStates = finalStates;
7591
automata.transitions = transitions;
7692

93+
console.log(automata);
94+
7795
renderOut("Loading ...");
7896
renderOutString(string);
79-
verifyAFD(paper, graph, automata, string);
97+
// verifyAFD(paper, graph, automata, string);
98+
99+
const res = verifyAFND(paper, graph, automata, string);
100+
console.log(res);
80101
}
81102

82103
function changeLabelName() {

0 commit comments

Comments
 (0)