Skip to content

Commit

Permalink
Merge pull request #5 from MauricioDDS/main
Browse files Browse the repository at this point in the history
Progress made for turing machine simulation
  • Loading branch information
byandrev authored Dec 9, 2023
2 parents 5201c86 + 158b239 commit c42d6fb
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 90 deletions.
22 changes: 1 addition & 21 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ <h1 class="select-none">Automata Simulation</h1>

<select
class="change-simulator"
id="changeSim"
onchange="changeSimulator()">
id="btn-changeSim">
<option value="Automata-Simulator">AFD Simulator</option>
<option value="Turing-Machine-Simulator">TM Simulator</option>
</select>
Expand Down Expand Up @@ -321,25 +320,6 @@ <h3 class="text-2xl font-bold mb-2">Collaborate</h3>
</div>
</div>
</div>
<script>
// Load Turing Machine Simulator
function changeSimulator() {
var select = document.getElementById("changeSim");
if (select) {
var options = select.options[select.selectedIndex].value;
if (options === "Automata-Simulator") {
console.log("xd1");

}
else if(options === "Turing-Machine-Simulator"){
console.log("xd2");

}
} else {
console.error("Element not found");
}
}
</script>
<script src="src/main.js" type="module"></script>
</body>
</html>
96 changes: 75 additions & 21 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import MicroModal from "micromodal";
import Split from "split.js";
// import { verifyAFD } from "./afd.js";
import { verifyAFND } from "./afnd.js";
import { verifyTM } from "./tm.js";
import { renderError, renderOut, renderOutString } from "./animateNode.js";
import { clearAutomata, createAutomata } from "./automata.js";
import { createMachine, clearMachine } from "./turingMachine.js";
import { startDragTools } from "./dragTools.js";
import { initGraph } from "./graph.js";
import { CircleShape, FILL_NODE_FINAL } from "./shapes.js";
Expand All @@ -16,8 +18,11 @@ const inputLabel = document.querySelector("#input-label-name");
const inputState = document.querySelector("#input-state-name");
const btnClearAll = document.querySelector("#btn-clear-all");
const btnDownload = document.querySelector("#btn-download");
const btnChangeSim = document.querySelector("#btn-changeSim");

const automata = createAutomata();
const machine = createMachine();
let mode = 1;

function run() {
const data = graph.toJSON();
Expand All @@ -28,6 +33,7 @@ function run() {
const string = inputString.value;
const statesArr = [];
const transitions = {};
const moveSet = {};

// clear errors
renderError(null);
Expand All @@ -47,25 +53,53 @@ function run() {
}
});

data.cells.forEach((el) => {
if (el.type === "Link") {
alphabet.push(...el.labels[0].attrs.text.text.split(","));

el.labels[0].attrs.text.text.split(",").forEach((symbol) => {
if (transitions[states[el.source.id].text].length >= 0) {
transitions[states[el.source.id].text].push([
states[el.target.id].text,
symbol,
]);
} else {
transitions[states[el.source.id].text] = [
[states[el.target.id].text, symbol],
];
}
});
}
});
if (mode === 1) {

data.cells.forEach((el) => {
if (el.type === "Link") {
alphabet.push(...el.labels[0].attrs.text.text.split(","));

el.labels[0].attrs.text.text.split(",").forEach((symbol) => {
if (transitions[states[el.source.id].text].length >= 0) {
transitions[states[el.source.id].text].push([
states[el.target.id].text,
symbol,
]);
} else {
transitions[states[el.source.id].text] = [
[states[el.target.id].text, symbol],
];
}
});
}
});
}
else {
data.cells.forEach((el) => {
if (el.type === "Link") {
alphabet.push(...el.labels[0].attrs.text.text.split("/")[0]);
el.labels[0].attrs.text.text.split(",").forEach((symbol) => {
let write = el.labels[0].attrs.text.text.split("/")[1];
let move = el.labels[0].attrs.text.text.split("/")[2];
if (transitions[states[el.source.id].text].length >= 0) {
transitions[states[el.source.id].text].push([
states[el.target.id].text,
symbol[0],
]);
moveSet[states[el.source.id].text].push([
write, move
]);
} else {
transitions[states[el.source.id].text] = [
[states[el.target.id].text, symbol[0]],
];
moveSet[states[el.source.id].text] = [write, move]
}
});
}

});
}
Object.values(states).forEach((state) => statesArr.push(state.text));

if (statesArr.length <= 0) {
Expand All @@ -78,20 +112,35 @@ function run() {
return;
}


if(mode === 1){
automata.alphabet = alphabet;
automata.initialState = "q0";
automata.states = statesArr;
automata.finalStates = finalStates;
automata.transitions = transitions;

console.log(automata);
}
else{
machine.alphabet = alphabet;
machine.initialState = "q0";
machine.states = statesArr;
machine.finalStates = finalStates;
machine.transitions = transitions;
machine.moveSet = moveSet;
console.log(machine);
}

renderOut("Loading ...");
renderOutString(string);
// verifyAFD(paper, graph, automata, string);

const res = verifyAFND(paper, graph, automata, string);
console.log(res);
if(mode === 1){
const res = verifyAFND(paper, graph, automata, string);
console.log(res);
} else{
const res = verifyTM(paper, graph, machine, string);
}
}

function changeLabelName() {
Expand Down Expand Up @@ -172,3 +221,8 @@ btnClearAll.addEventListener("click", () => {
// Download png
btnDownload.addEventListener("click", download);

btnChangeSim.addEventListener("change", () => {
clearAutomata(automata);
graph.clear();
mode *= -1;
});
57 changes: 9 additions & 48 deletions src/tm.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,13 @@
import { animateNode, renderError, renderOut } from "./animateNode.js";
import { animateNode, renderError, renderOut } from "/src/animateNode.js";
import { verifyAFND } from "/src/afnd.js?t=1701900312070";

function verifyAFD(paper, graph, automata, string) {
let i = 0;
let state = automata.initialState;
let symbol = string[i];
function verifyTM(paper, graph, machine, string) {

const interval = setInterval(() => {
animateNode(
paper,
graph,
state,
symbol,
automata.finalStates.includes(state)
);

if (i >= string.length) {
clearInterval(interval);

if (automata.finalStates.includes(state)) {
renderOut("VALID");
return;
}

renderOut("INVALID");
renderError("I do not end in a final state");
return;
}

document
.querySelector("#string-out")
.querySelector(`span:nth-child(${i + 1})`).className =
"symbol text-blue-500 font-bold symbol-active";

const isState = automata.transitions.find(
(el) => el.state === state && el.symbol.includes(symbol)
);

if (!isState) {
clearInterval(interval);

renderOut("INVALID");
return;
}

i++;
state = isState.nextState;
symbol = string[i];
}, 1000);
const steps = [];

verifyAFND(paper, graph, machine, string);
//escribir
}

export { verifyAFD };
export { verifyTM };

29 changes: 29 additions & 0 deletions src/turingMachine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function createMachine() {
const machine = {
states: [],
alphabet: [],
transitions: [],
moveSet: [],
initialState: "",
finalStates: [],
};
return machine;
}

function clearMachine(automata) {
machine.states = [];
machine.alphabet = [];
machine.transitions = [];
moveSet = [];
machine.initialState = "";
machine.finalStates = [];
return machines;
}

function createTMTransition(automata, state, symbol, nextState) {
machine.transitions.push({ state, symbol, nextState });
return machine;
}

export { createMachine, createTMTransition, clearMachine };

0 comments on commit c42d6fb

Please sign in to comment.