@@ -2,8 +2,10 @@ import MicroModal from "micromodal";
2
2
import Split from "split.js" ;
3
3
// import { verifyAFD } from "./afd.js";
4
4
import { verifyAFND } from "./afnd.js" ;
5
+ import { verifyTM } from "./tm.js" ;
5
6
import { renderError , renderOut , renderOutString } from "./animateNode.js" ;
6
7
import { clearAutomata , createAutomata } from "./automata.js" ;
8
+ import { createMachine , clearMachine } from "./turingMachine.js" ;
7
9
import { startDragTools } from "./dragTools.js" ;
8
10
import { initGraph } from "./graph.js" ;
9
11
import { CircleShape , FILL_NODE_FINAL } from "./shapes.js" ;
@@ -16,8 +18,11 @@ const inputLabel = document.querySelector("#input-label-name");
16
18
const inputState = document . querySelector ( "#input-state-name" ) ;
17
19
const btnClearAll = document . querySelector ( "#btn-clear-all" ) ;
18
20
const btnDownload = document . querySelector ( "#btn-download" ) ;
21
+ const btnChangeSim = document . querySelector ( "#btn-changeSim" ) ;
19
22
20
23
const automata = createAutomata ( ) ;
24
+ const machine = createMachine ( ) ;
25
+ let mode = 1 ;
21
26
22
27
function run ( ) {
23
28
const data = graph . toJSON ( ) ;
@@ -28,6 +33,7 @@ function run() {
28
33
const string = inputString . value ;
29
34
const statesArr = [ ] ;
30
35
const transitions = { } ;
36
+ const moveSet = { } ;
31
37
32
38
// clear errors
33
39
renderError ( null ) ;
@@ -47,25 +53,53 @@ function run() {
47
53
}
48
54
} ) ;
49
55
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
+ }
68
100
101
+ } ) ;
102
+ }
69
103
Object . values ( states ) . forEach ( ( state ) => statesArr . push ( state . text ) ) ;
70
104
71
105
if ( statesArr . length <= 0 ) {
@@ -78,20 +112,35 @@ function run() {
78
112
return ;
79
113
}
80
114
115
+
116
+ if ( mode === 1 ) {
81
117
automata . alphabet = alphabet ;
82
118
automata . initialState = "q0" ;
83
119
automata . states = statesArr ;
84
120
automata . finalStates = finalStates ;
85
121
automata . transitions = transitions ;
86
-
87
122
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
+ }
88
133
89
134
renderOut ( "Loading ..." ) ;
90
135
renderOutString ( string ) ;
91
136
// verifyAFD(paper, graph, automata, string);
92
137
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
+ }
95
144
}
96
145
97
146
function changeLabelName ( ) {
@@ -172,3 +221,8 @@ btnClearAll.addEventListener("click", () => {
172
221
// Download png
173
222
btnDownload . addEventListener ( "click" , download ) ;
174
223
224
+ btnChangeSim . addEventListener ( "change" , ( ) => {
225
+ clearAutomata ( automata ) ;
226
+ graph . clear ( ) ;
227
+ mode *= - 1 ;
228
+ } ) ;
0 commit comments