|
1 | 1 | import { animateNode, renderError, renderOut } from "./animateNode.js"; |
2 | 2 |
|
3 | 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); |
| 4 | + const steps = []; |
| 5 | + |
| 6 | + const verify = (afd, currentState, string) => { |
| 7 | + if (string.length <= 0) { |
| 8 | + return afd.finalStates.includes(currentState); |
| 9 | + } |
| 10 | + |
| 11 | + for (let i = 0; i < afd.transitions[currentState].length; i++) { |
| 12 | + const [nextState, symbol] = afd.transitions[currentState][i]; |
| 13 | + |
| 14 | + if (symbol === string[0]) { |
| 15 | + steps.push([currentState, symbol, nextState, string[0]]); |
| 16 | + |
| 17 | + if (verify(afd, nextState, string.slice(1))) { |
| 18 | + return true; |
| 19 | + } |
| 20 | + } else if (symbol === "λ") { |
| 21 | + steps.push([currentState, symbol, nextState, string[0]]); |
| 22 | + |
| 23 | + if (verify(afd, nextState, string)) { |
| 24 | + return true; |
79 | 25 | } |
80 | 26 | } |
81 | 27 | } |
82 | 28 |
|
83 | | - return nextStates; |
| 29 | + return false; |
84 | 30 | }; |
85 | 31 |
|
86 | | - let currentStates = [automata.initialState]; |
87 | | - |
88 | | - const verify = (states, symbol) => { |
89 | | - let i = 0; |
90 | | - let state = states[0]; |
| 32 | + const res = verify(automata, automata.initialState, string); |
91 | 33 |
|
92 | | - const interval = setInterval(() => { |
93 | | - animateNode( |
94 | | - paper, |
95 | | - graph, |
96 | | - state, |
97 | | - symbol, |
98 | | - automata.finalStates.includes(state) |
99 | | - ); |
| 34 | + let indexStep = 0; |
| 35 | + document.querySelector("#string-out").textContent = ""; |
100 | 36 |
|
101 | | - if (i >= string.length) { |
102 | | - clearInterval(interval); |
103 | | - return; |
| 37 | + const interval = setInterval(() => { |
| 38 | + if (indexStep >= steps.length) { |
| 39 | + if (!res) { |
| 40 | + renderOut("INVALID"); |
| 41 | + } else { |
| 42 | + renderOut("VALID"); |
104 | 43 | } |
105 | 44 |
|
106 | | - states = getNextStates(states, symbol); |
107 | | - |
108 | | - if (states.length <= 0) { |
109 | | - return; |
110 | | - } |
| 45 | + clearInterval(interval); |
| 46 | + return; |
| 47 | + } |
111 | 48 |
|
112 | | - console.log("Current: ", states); |
| 49 | + const step = steps[indexStep]; |
113 | 50 |
|
114 | | - i++; |
115 | | - symbol = string[i]; |
| 51 | + animateNode( |
| 52 | + paper, |
| 53 | + graph, |
| 54 | + step[0], |
| 55 | + step[1], |
| 56 | + automata.finalStates.includes(step[0]), |
| 57 | + step[2], |
| 58 | + ); |
116 | 59 |
|
117 | | - verify(states, symbol); |
118 | | - }, 1000); |
119 | | - }; |
| 60 | + document.querySelector("#string-out").textContent += step[3]; |
120 | 61 |
|
121 | | - verify(currentStates, string[0]); |
| 62 | + indexStep++; |
| 63 | + }, 1000); |
122 | 64 |
|
123 | | - return false; |
| 65 | + return res; |
124 | 66 | } |
125 | 67 |
|
126 | 68 | export { verifyAFND }; |
0 commit comments