Skip to content

Commit c42d6fb

Browse files
authored
Merge pull request #5 from MauricioDDS/main
Progress made for turing machine simulation
2 parents 5201c86 + 158b239 commit c42d6fb

File tree

4 files changed

+114
-90
lines changed

4 files changed

+114
-90
lines changed

index.html

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ <h1 class="select-none">Automata Simulation</h1>
8686

8787
<select
8888
class="change-simulator"
89-
id="changeSim"
90-
onchange="changeSimulator()">
89+
id="btn-changeSim">
9190
<option value="Automata-Simulator">AFD Simulator</option>
9291
<option value="Turing-Machine-Simulator">TM Simulator</option>
9392
</select>
@@ -321,25 +320,6 @@ <h3 class="text-2xl font-bold mb-2">Collaborate</h3>
321320
</div>
322321
</div>
323322
</div>
324-
<script>
325-
// Load Turing Machine Simulator
326-
function changeSimulator() {
327-
var select = document.getElementById("changeSim");
328-
if (select) {
329-
var options = select.options[select.selectedIndex].value;
330-
if (options === "Automata-Simulator") {
331-
console.log("xd1");
332-
333-
}
334-
else if(options === "Turing-Machine-Simulator"){
335-
console.log("xd2");
336-
337-
}
338-
} else {
339-
console.error("Element not found");
340-
}
341-
}
342-
</script>
343323
<script src="src/main.js" type="module"></script>
344324
</body>
345325
</html>

src/main.js

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import MicroModal from "micromodal";
22
import Split from "split.js";
33
// import { verifyAFD } from "./afd.js";
44
import { verifyAFND } from "./afnd.js";
5+
import { verifyTM } from "./tm.js";
56
import { renderError, renderOut, renderOutString } from "./animateNode.js";
67
import { clearAutomata, createAutomata } from "./automata.js";
8+
import { createMachine, clearMachine } from "./turingMachine.js";
79
import { startDragTools } from "./dragTools.js";
810
import { initGraph } from "./graph.js";
911
import { CircleShape, FILL_NODE_FINAL } from "./shapes.js";
@@ -16,8 +18,11 @@ const inputLabel = document.querySelector("#input-label-name");
1618
const inputState = document.querySelector("#input-state-name");
1719
const btnClearAll = document.querySelector("#btn-clear-all");
1820
const btnDownload = document.querySelector("#btn-download");
21+
const btnChangeSim = document.querySelector("#btn-changeSim");
1922

2023
const automata = createAutomata();
24+
const machine = createMachine();
25+
let mode = 1;
2126

2227
function run() {
2328
const data = graph.toJSON();
@@ -28,6 +33,7 @@ function run() {
2833
const string = inputString.value;
2934
const statesArr = [];
3035
const transitions = {};
36+
const moveSet = {};
3137

3238
// clear errors
3339
renderError(null);
@@ -47,25 +53,53 @@ function run() {
4753
}
4854
});
4955

50-
data.cells.forEach((el) => {
51-
if (el.type === "Link") {
52-
alphabet.push(...el.labels[0].attrs.text.text.split(","));
53-
54-
el.labels[0].attrs.text.text.split(",").forEach((symbol) => {
55-
if (transitions[states[el.source.id].text].length >= 0) {
56-
transitions[states[el.source.id].text].push([
57-
states[el.target.id].text,
58-
symbol,
59-
]);
60-
} else {
61-
transitions[states[el.source.id].text] = [
62-
[states[el.target.id].text, symbol],
63-
];
64-
}
65-
});
66-
}
67-
});
56+
if (mode === 1) {
57+
58+
data.cells.forEach((el) => {
59+
if (el.type === "Link") {
60+
alphabet.push(...el.labels[0].attrs.text.text.split(","));
61+
62+
el.labels[0].attrs.text.text.split(",").forEach((symbol) => {
63+
if (transitions[states[el.source.id].text].length >= 0) {
64+
transitions[states[el.source.id].text].push([
65+
states[el.target.id].text,
66+
symbol,
67+
]);
68+
} else {
69+
transitions[states[el.source.id].text] = [
70+
[states[el.target.id].text, symbol],
71+
];
72+
}
73+
});
74+
}
75+
});
76+
}
77+
else {
78+
data.cells.forEach((el) => {
79+
if (el.type === "Link") {
80+
alphabet.push(...el.labels[0].attrs.text.text.split("/")[0]);
81+
el.labels[0].attrs.text.text.split(",").forEach((symbol) => {
82+
let write = el.labels[0].attrs.text.text.split("/")[1];
83+
let move = el.labels[0].attrs.text.text.split("/")[2];
84+
if (transitions[states[el.source.id].text].length >= 0) {
85+
transitions[states[el.source.id].text].push([
86+
states[el.target.id].text,
87+
symbol[0],
88+
]);
89+
moveSet[states[el.source.id].text].push([
90+
write, move
91+
]);
92+
} else {
93+
transitions[states[el.source.id].text] = [
94+
[states[el.target.id].text, symbol[0]],
95+
];
96+
moveSet[states[el.source.id].text] = [write, move]
97+
}
98+
});
99+
}
68100

101+
});
102+
}
69103
Object.values(states).forEach((state) => statesArr.push(state.text));
70104

71105
if (statesArr.length <= 0) {
@@ -78,20 +112,35 @@ function run() {
78112
return;
79113
}
80114

115+
116+
if(mode === 1){
81117
automata.alphabet = alphabet;
82118
automata.initialState = "q0";
83119
automata.states = statesArr;
84120
automata.finalStates = finalStates;
85121
automata.transitions = transitions;
86-
87122
console.log(automata);
123+
}
124+
else{
125+
machine.alphabet = alphabet;
126+
machine.initialState = "q0";
127+
machine.states = statesArr;
128+
machine.finalStates = finalStates;
129+
machine.transitions = transitions;
130+
machine.moveSet = moveSet;
131+
console.log(machine);
132+
}
88133

89134
renderOut("Loading ...");
90135
renderOutString(string);
91136
// verifyAFD(paper, graph, automata, string);
92137

93-
const res = verifyAFND(paper, graph, automata, string);
94-
console.log(res);
138+
if(mode === 1){
139+
const res = verifyAFND(paper, graph, automata, string);
140+
console.log(res);
141+
} else{
142+
const res = verifyTM(paper, graph, machine, string);
143+
}
95144
}
96145

97146
function changeLabelName() {
@@ -172,3 +221,8 @@ btnClearAll.addEventListener("click", () => {
172221
// Download png
173222
btnDownload.addEventListener("click", download);
174223

224+
btnChangeSim.addEventListener("change", () => {
225+
clearAutomata(automata);
226+
graph.clear();
227+
mode *= -1;
228+
});

src/tm.js

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,13 @@
1-
import { animateNode, renderError, renderOut } from "./animateNode.js";
1+
import { animateNode, renderError, renderOut } from "/src/animateNode.js";
2+
import { verifyAFND } from "/src/afnd.js?t=1701900312070";
23

3-
function verifyAFD(paper, graph, automata, string) {
4-
let i = 0;
5-
let state = automata.initialState;
6-
let symbol = string[i];
4+
function verifyTM(paper, graph, machine, string) {
75

8-
const interval = setInterval(() => {
9-
animateNode(
10-
paper,
11-
graph,
12-
state,
13-
symbol,
14-
automata.finalStates.includes(state)
15-
);
16-
17-
if (i >= string.length) {
18-
clearInterval(interval);
19-
20-
if (automata.finalStates.includes(state)) {
21-
renderOut("VALID");
22-
return;
23-
}
24-
25-
renderOut("INVALID");
26-
renderError("I do not end in a final state");
27-
return;
28-
}
29-
30-
document
31-
.querySelector("#string-out")
32-
.querySelector(`span:nth-child(${i + 1})`).className =
33-
"symbol text-blue-500 font-bold symbol-active";
34-
35-
const isState = automata.transitions.find(
36-
(el) => el.state === state && el.symbol.includes(symbol)
37-
);
38-
39-
if (!isState) {
40-
clearInterval(interval);
41-
42-
renderOut("INVALID");
43-
return;
44-
}
45-
46-
i++;
47-
state = isState.nextState;
48-
symbol = string[i];
49-
}, 1000);
6+
const steps = [];
7+
8+
verifyAFND(paper, graph, machine, string);
9+
//escribir
5010
}
5111

52-
export { verifyAFD };
12+
export { verifyTM };
13+

src/turingMachine.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function createMachine() {
2+
const machine = {
3+
states: [],
4+
alphabet: [],
5+
transitions: [],
6+
moveSet: [],
7+
initialState: "",
8+
finalStates: [],
9+
};
10+
return machine;
11+
}
12+
13+
function clearMachine(automata) {
14+
machine.states = [];
15+
machine.alphabet = [];
16+
machine.transitions = [];
17+
moveSet = [];
18+
machine.initialState = "";
19+
machine.finalStates = [];
20+
return machines;
21+
}
22+
23+
function createTMTransition(automata, state, symbol, nextState) {
24+
machine.transitions.push({ state, symbol, nextState });
25+
return machine;
26+
}
27+
28+
export { createMachine, createTMTransition, clearMachine };
29+

0 commit comments

Comments
 (0)