@@ -2,8 +2,10 @@ import MicroModal from "micromodal";
22import Split from "split.js" ;
33// import { verifyAFD } from "./afd.js";
44import { verifyAFND } from "./afnd.js" ;
5+ import { verifyTM } from "./tm.js" ;
56import { renderError , renderOut , renderOutString } from "./animateNode.js" ;
67import { clearAutomata , createAutomata } from "./automata.js" ;
8+ import { createMachine , clearMachine } from "./turingMachine.js" ;
79import { startDragTools } from "./dragTools.js" ;
810import { initGraph } from "./graph.js" ;
911import { CircleShape , FILL_NODE_FINAL } from "./shapes.js" ;
@@ -16,8 +18,11 @@ const inputLabel = document.querySelector("#input-label-name");
1618const inputState = document . querySelector ( "#input-state-name" ) ;
1719const btnClearAll = document . querySelector ( "#btn-clear-all" ) ;
1820const btnDownload = document . querySelector ( "#btn-download" ) ;
21+ const btnChangeSim = document . querySelector ( "#btn-changeSim" ) ;
1922
2023const automata = createAutomata ( ) ;
24+ const machine = createMachine ( ) ;
25+ let mode = 1 ;
2126
2227function 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
97146function changeLabelName ( ) {
@@ -172,3 +221,8 @@ btnClearAll.addEventListener("click", () => {
172221// Download png
173222btnDownload . addEventListener ( "click" , download ) ;
174223
224+ btnChangeSim . addEventListener ( "change" , ( ) => {
225+ clearAutomata ( automata ) ;
226+ graph . clear ( ) ;
227+ mode *= - 1 ;
228+ } ) ;
0 commit comments