-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumentation.html
More file actions
363 lines (339 loc) · 18.1 KB
/
documentation.html
File metadata and controls
363 lines (339 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Finite state machine (DEA) embedding Reber Grammar</title>
</head>
<body>
<nav class="navbar navbar-expand-lg sticky-top navbar-light bg-light">
<div class="container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="index.html">Zurück zur Startseite</a>
</li>
</div>
</div>
</nav>
<div class="container">
<div class="row mb-3">
<div class="col-2"></div>
<div class="col-8 mb-3">
<h1 class="h3">Realisierung einer Turingmaschine (TM)</h1>
<p>Autor: Andreas Rieger<br />Datum: 05.02.2022</p>
<h2 class="h4">Aufgabenstellung</h2>
<p>Simulation und Visualisierung einer Turingmaschine zur Entscheidung über die <a
href="https://willamette.edu/~gorr/classes/cs449/reber.html#top">Embedded Reber Grammar
(ERG)</a>.</p>
<h2 class="h4">Technische Dokumentation</h2>
<p>Die Anwendung basiert auf HTML, CSS und Javascript. </p>
<p>Zur Darstellung der Status als interaktives Diagramm wurde <a
href="https://gojs.net/latest/index.html">GoJS</a> implementiert. GoJS ermöglicht die schnelle
und automatische Erstellung von interaktiven Graphen durch die Nutzung von Log-Daten aus dem
Programmdurchlauf. Dieses Framework ist kompatibel mit den anderen. Es stehen verschiedene Layouts und Templates zur Verfügung.</p>
<p>Zur Visualisierung der Oberfläche wurde außerdem das Framework
<a href="https://getbootstrap.com/">Bootstrap</a> verwendet. Bootstrap liefert eine
einheitliche, gut strukturierte und responsive
Benutzeroberfläche, die sich an verschiedene Endgeräte anpassen lässt. Außerdem hält
das Framework viele vordefinierte und ansprechend gestaltete Komponenten zur Interaktion bereit.
</p>
<h2 class="h4">Fachliche Dokumentation</h2>
<p>Das Eingabewort kann manuell oder mit Hilfe eines Zufallsgenerators erstellt werden, wobei richtige
und potenziell falsche Buchstabenkombinationen ausgegeben werden.</p>
<pre>const randomSequence = async () => {
// To do: set min and max
const
n = sigma.length,
arr = [],
x = await randomInt(5, 10);
for (let i = 0; i < x; i++) {
arr.push(sigma[(0 + Math.floor(Math.random() * n)) % n]);
}
return arr;
};</pre>
<h3 class="h5">Anschließend wird die Überprüfung
des Eingabewortes und die Ausgabe des Ergebnisses initialisiert.</h3>
<pre>if (inputValue.length != 0) {
response = new Turingmachine(inputValue.toUpperCase());
accState = response["states"].length;
transitions = await transitionList(response["log"]);
counter = 0;
// init diagram
diagram = initDiagram(nodeData(response["states"]), linkData(response["states"]));
// init tape
initTapeOutput(response["word"]);
}</pre>
<h3 class="h5">
Die Turingmaschine wird als JavaScript-Klasse implementiert. Das übergebene Wort wird "auf das Band
geschrieben".</h3>
<pre>class Turingmachine {
constructor(word) {
// creating array from word
const tape = Array.from(word.trim());
// adding a blank to mark the end of word
tape.push(blank);
const firstChar = (element) => element != blank;
this.word = tape.slice(tape.findIndex(firstChar))
this.states = states;
this.accepted = false;
this.blank = blank;
this.log = [];
...
}
}
</pre>
<h3 class="h5">Die Konstruktor-Methode (rekursive Funktion) durchläuft die Eingabe auf dem Band und
testet diese
gegen die Maschinen-Konfiguration.</h3>
<pre>const operations = () => {
// making configuration details
// shorter and more readable
const read = tape[head];
// running through the configuration without errors
// while reading only valid chars from tape
if (typeof states[curState][read] !== "undefined") {
// making configuration details shorter and more readable
const write = states[curState][read][0];
const move = states[curState][read][1];
nextState = states[curState][read][2];
// Ignoring initial 'blanks' and
// moving the head to the first char without logging
if (curState == 0 && tape[head] == blank) {
head++;
operations();
}
// moving head to the right
else if (move == 'R') {
this.logResult(curState, head, read, write, move, nextState);
curState = nextState;
tape[head] = write;
head++;
operations();
}
// moving head to the left
else if (move == 'L') {
this.logResult(curState, head, read, write, move, nextState);
curState = nextState;
tape[head] = write;
head--;
operations();
}
// reaching end of tape, succeeding
else {
this.logResult(curState, head, read, write, move, nextState);
this.accepted = true;
}
}
// leaving the loop while reading unknown char from tape
else {
console.log(`unknown char ${read} at head pos. ${head} in state ${curState}`);
this.logResult(curState, head, read, read, 'N', nextState);
}
}
operations(); // starting program routine
}</pre>
<p class="fw-bold">Die Maschinenkonfiguration als Array aus Objekten:</p>
<pre>const states = [
// curState 0
{
[blank]: [blank, 'R', 0],
'B': [blank, 'R', 1]
},
// curState 1
{
'T': ['T', 'R', 2],
'P': ['P', 'R', 2]
},
// curState 2
{
'B': [blank, 'R', 3]
},
// curState 3
{
'T': [blank, 'R', 4],
'P': [blank, 'R', 6]
},
// curState 4
{
'S': [blank, 'R', 4],
'X': [blank, 'R', 5]
},
// curState 5
{
'X': [blank, 'R', 6],
'S': [blank, 'R', 8]
},
// curState 6
{
'T': [blank, 'R', 6],
'V': [blank, 'R', 7]
},
// curState 7
{
'P': [blank, 'R', 5],
'V': [blank, 'R', 8]
},
// curState 8
{
'E': [blank, 'R', 9]
},
// curState 9
{
'T': [blank, 'L', 10],
'P': [blank, 'L', 11]
},
// curState 10
{
[blank]: [blank, 'L', 10],
'T': [blank, 'R', 12]
},
// curState 11
{
[blank]: [blank, 'L', 11],
'P': [blank, 'R', 12]
},
// curState 12
{
[blank]: [blank, 'R', 12],
'E': [blank, 'R', 13]
},
// curState 13
{
[blank]: [blank, 'N', 13]
}
];</pre>
<h3 class="h5">Aus den Logs der Maschine werden die Daten für die Erstellung der Graphen generiert</h3>
<p class="fw-bold">Diagramm-Nodes:</p>
<pre>const nodeData = (states) => {
const arr = [];
const graphIds = [];
let graphId = null;
for (let i = 0, l = states.length; i < l; i++) {
if (i != graphId && !graphIds.includes(i)) {
if (i == 0) {
arr.push({ key: i, color: "green" });
graphIds.push(i);
}
if (!graphIds.includes(i)) {
arr.push({ key: i, color: "grey" });
graphIds.push(i);
}
graphId = i;
}
}
return arr;
};</pre>
<p class="fw-bold">Die Links zwischen den Nodes:</p>
<pre>const linkData = (states) => {
const arr = [];
for (let i = 0, l = states.length; i < l; i++) {
for (const key of Object.entries(states[i])) {
const label = `[${key[0]}, ${key[1][0]}, ${key[1][1]}]`;
const linkKey = i.toString() + key[1][2].toString();
arr.push({ from: i, to: key[1][2], key: linkKey, label: label });
}
}
return arr;
};</pre>
<h3 class="h5">Für die Visualisierung des Maschinenablaufs und der Zustandsgraphen wird ein Array aus
Objekten
erstellt.</h3>
<pre>const transitionList = async (log) => {
const arr = [];
for (const row of log) {
const
from = row["curState"],
to = row["nextState"],
key = `${row["curState"]}${row["nextState"]}`,
head = row["head"],
read = row["read"],
write = row["write"],
move = row["move"]
;
arr.push({ from: from, to: to, key: key, head: head, read: read, write: write, move: move });
}
return arr;
};</pre>
<h3 class="h5">Übergabe der Konfigurationsdaten an das Zustandsdiagramm und Initialisierung des
Diagramms:</h3>
<pre>function initDiagram(graphs, links) {
const myDiagram =
new go.Diagram("myDiagramDiv", // create a Diagram for a HTML Div element
{ "undoManager.isEnabled": true }, // enable undo & redo
);
// using the LayeredDigraphLayout layout
myDiagram.layout = new go.LayeredDigraphLayout({ columnSpacing: 60, layerSpacing: 35 });
// define a simple Node template
myDiagram.nodeTemplate =
new go.Node("Auto") // the Shape will automatically surround the TextBlock
.add( // add a Shape and a TextBlock to this "Auto" Panel
new go.Shape("Circle",
{ strokeWidth: 0, fill: "white" }) // no border; default fill is white
// Shape.fill is bound to Node.data.color
.bind("fill", "color"),
new go.TextBlock({ margin: 5, stroke: "#333" }) // some room around the text
// TextBlock.text is bound to Node.data.key
.bind("text", "key"));
// but use the default Link template, by not setting Diagram.linkTemplate
myDiagram.linkTemplate =
new go.Link({ curve: go.Link.Bezier, curviness: 20 })
.add(
new go.Shape(),
new go.Shape({ toArrow: "Standard" }),
new go.Panel("Auto", { segmentOffset: new go.Point(0, -25) })
.add(
new go.Shape("RoundedRectangle", { fill: "white" }),
new go.TextBlock({ background: "white", margin: 2 }).bind("text", "label")
)
)
;
// enabling the key property for graph links
myDiagram.linkKeyProperty = "key";
// create the model data that will be represented by Nodes and Links
myDiagram.model = new go.GraphLinksModel(
graphs,
links
);
myDiagram.model.isReadOnly = true;
return myDiagram;
}</pre>
<h3 class="h5">Übergabe der Konfigurationsdaten zur Darstellung des Eingabebands an das Frontend:</h3>
<pre>const initTapeOutput = word => {
const tapeWrap = document.getElementById("tapeOutput");
tapeWrap.innerHTML = "";
const output = document.createElement("p");
for (let i = 0, l = word.length; i < l; i++) {
const textWrap = document.createElement("span");
textWrap.setAttribute("id", "th" + i);
if (i == 0) {
textWrap.setAttribute("class", "bg-secondary text-white");
} else textWrap.setAttribute("class", "bg-light text-dark");
textWrap.classList.add("fs-1", "fw-bold", "font-monospace", "tape-element");
const text = document.createTextNode(word[i]);
textWrap.appendChild(text);
output.appendChild(textWrap);
}
tapeWrap.appendChild(output);
};</pre>
<p class="fw-bold">Nach dem Laden des Graphen-Diagramms und der Band-Darstellung kann die Simulation
gestartet werden.
Die Nutzer:innen haben dabei die Wahl zwischen der manuellen Steuerung in Einzelschritten oder der
automatischen Animation mit einstellbarer Geschwindigkeit.</p>
<p class="fw-bold">Viel Spass! <a href="index.html">Und los!</a></p>
</div>
<div class="col-2"></div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
</body>
</html>