From c1cf77375241523756315bab089fc85f47ef9b05 Mon Sep 17 00:00:00 2001 From: MauricioDDs Date: Wed, 6 Dec 2023 20:55:21 -0500 Subject: [PATCH] avances en maquina de turing --- index.html | 22 +--------- src/main.js | 96 ++++++++++++++++++++++++++++++++++---------- src/tm.js | 57 +++++--------------------- src/turingMachine.js | 29 +++++++++++++ 4 files changed, 114 insertions(+), 90 deletions(-) create mode 100644 src/turingMachine.js diff --git a/index.html b/index.html index 70db358..40a397f 100644 --- a/index.html +++ b/index.html @@ -86,8 +86,7 @@

Automata Simulation

@@ -321,25 +320,6 @@

Collaborate

- diff --git a/src/main.js b/src/main.js index 32ac0fb..f72aeb3 100644 --- a/src/main.js +++ b/src/main.js @@ -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"; @@ -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(); @@ -28,6 +33,7 @@ function run() { const string = inputString.value; const statesArr = []; const transitions = {}; + const moveSet = {}; // clear errors renderError(null); @@ -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) { @@ -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() { @@ -172,3 +221,8 @@ btnClearAll.addEventListener("click", () => { // Download png btnDownload.addEventListener("click", download); +btnChangeSim.addEventListener("change", () => { + clearAutomata(automata); + graph.clear(); + mode *= -1; +}); \ No newline at end of file diff --git a/src/tm.js b/src/tm.js index 00211bc..f7acb24 100644 --- a/src/tm.js +++ b/src/tm.js @@ -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 }; + diff --git a/src/turingMachine.js b/src/turingMachine.js new file mode 100644 index 0000000..980f6dc --- /dev/null +++ b/src/turingMachine.js @@ -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 }; + \ No newline at end of file