Permalink
Browse files

new file for general js stuff and doc for test

  • Loading branch information...
1 parent 2dff890 commit 4e26e8771785e5d70056fd16b98bd8a4b7235adf @tychon committed Mar 23, 2012
Showing with 122 additions and 65 deletions.
  1. +25 −50 index.html
  2. +23 −14 simulation.js
  3. +12 −1 style.css
  4. +25 −0 test.js
  5. +37 −0 universal.js
View
@@ -2,22 +2,24 @@
<head>
<title>Turing machine</title>
<link rel="stylesheet" type="text/css" href="style.css">
+ <script language="JavaScript" src="universal.js"></script>
<script language="JavaScript" src="compiler.js"></script>
<script language="JavaScript" src="simulation.js"></script>
<script language="JavaScript" src="test.js"></script>
<script language="JavaScript">
addEventListener("load", initialize, false);
-var tape_canvas, tape_ccontext, machine, sim = null
+var tape_canvas, tape_ccontext, machine, sim = null, test
, guiEnabled = true
function initialize() {
- tape_ccontext = document.getElementById('tape_canvas').getContext('2d');
- tape_canvas = tape_ccontext.canvas;
- tape_ccontext.save();
+ tape_ccontext = document.getElementById('tape_canvas').getContext('2d')
+ tape_canvas = tape_ccontext.canvas
+ tape_ccontext.save()
- compile();
- resetSimulation();
- openDescription();
+ compile()
+ resetSimulation()
+ resetTest()
+ openDescription()
}
function openDescription() {
@@ -39,7 +41,7 @@
document.getElementById('simulation_button').style.backgroundColor = 'lightblue'
document.getElementById('test_button').style.backgroundColor = 'white'
- resetSimulation();
+ resetSimulation()
}
function openTest() {
document.getElementById('description').style.display = 'none'
@@ -49,47 +51,10 @@
document.getElementById('description_button').style.backgroundColor = 'white'
document.getElementById('simulation_button').style.backgroundColor = 'white'
document.getElementById('test_button').style.backgroundColor = 'lightblue'
+
+ resetTest()
}
-function setCharAt(str,index,chr) {
- if(index > str.length-1) return str;
- return str.substr(0,index) + chr + str.substr(index+1);
-}
-function setToString(array) {
- var out = '{'
- array.forEach(function (val) {out += val+', '})
- return out.substring(0, out.length-2) + '}'
-}
-function arrayToString(array) {
- var out = ''
- array.forEach(function (val) {out += val+', '})
- return out.substring(0, out.length-2)
-}
-function arrayEqu(a1, a2) {
- if (a1.length != a2.length) return false
- for (var i = 0; i < a1.length; a1 ++) if (a1[i] != a2[i]) return false
- return true
-}
-function findNextTransition() {
- var t, diff
- for (var i = 0; i < machine.transitions.length; i++) {
- t = machine.transitions[i]
- if (t.origin != sim.state) continue
- diff = false
- for (var j = 0; j < t.read.length; j++) {
- if (t.read[j] != sim.tapes[j].content[sim.tapes[j].head_pos]) {
- diff = true
- break
- }
- }
- if (diff) continue
-
- return {index: i, transition: t}
- }
- return null
-}
-
-
function disableGui(mode) {
guiEnabled = false
var els = getElementsByClassName("button")
@@ -151,7 +116,7 @@
<div class="descr_type">
Type:<br>
<input id="one_tape" type="radio" name="type_group" onclick="descrChanged()" checked />one tape<br>
- <input id="multitrack" type="radio" name="type_group" onclick="descrChanged()" />multi-track<br>
+ <span style="display: none"><input id="multitrack" type="radio" name="type_group" onclick="descrChanged()" />multi-track<br></span>
<input id="multitape" type="radio" name="type_group" onclick="descrChanged()" />multi-tape<br>
<span id="span_tape_count" style="visibility: hidden">Number of tapes: <input id="tape_count" type="text" value="2" size="2" onkeyup="descrChanged()" /></span>
</div>
@@ -206,7 +171,7 @@
<div id="simulation" class="tab" style="display: none">
- Input: <input id="sim_input" type="text" value="1001101" onkeyup="if(guiEnabled) resetSimulation()"/>
+ Input: <input id="sim_input" type="text" value="1001101" onkeyup="if(guiEnabled) resetSimulation()" />
<!--<a href="javascript:startSimulation()" class="simButton">Start</a>-->
<a href="javascript:if(guiEnabled) resetSimulation()" class="button">Reset</a><br>
<div class="tip">(Characters not in the alphabet are filtered out.)<span id="multi_char_symbols_tip"><br>(The Symbols have to be separated by spaces!)</span></div>
@@ -226,7 +191,17 @@
</div>
- <div id="test" class="tab" style="display:none">TODO</div>
+ <div id="test" class="tab" style="display:none">
+ <div class="test_infobox">
+ Number of words: <strong id="test_num_of_words">?</strong><br>
+ <div id="test_info"></div>
+ </div>
+ Word length: <input id="test_input" type="text" onkeyup="if(guiEnabled) resetTest()" size="3" value="3" /> symbols.<br>
+ <input id="test_break_checkbox" type="checkbox" onclick="if(guiEnabled) resetTest()" checked/>Break simulation after <input id="test_break_num" type="text" size="4" onkeyup="if(guiEnabled) resetTest()"> steps and assume that the word is rejected.<br>
+ <a href="javascript:if(guiEnabled) startTest()" class="button" >START</a> <span class="tip">(BE CAREFUL: You can't stop a running test.)</span>
+ <div id="test_result"></div>
+ <div style="clear: both"><!-- block float--></div>
+ </div>
</div>
</td><td class="layout_machine"><div id="machine"></div></td></tr></tbody></table>
<!-- load / save -->
View
@@ -39,6 +39,9 @@ sim = {
function resetSimulation() {
if (! machine) return // no compiled machine, should not occurr
+ if (machine.multi_character_symbols) document.getElementById('multi_char_symbols_tip').style.display = 'inline'
+ else document.getElementById('multi_char_symbols_tip').style.display = 'none'
+
sim = {
info: 'start',
infotext: '',
@@ -64,20 +67,7 @@ function resetSimulation() {
// load input
var input = document.getElementById('sim_input').value
- , filtered = []
- if (machine.multi_character_symbols) {
- document.getElementById('multi_char_symbols_tip').style.display = 'inline'
- var symbols = input.split(/\s+/)
- symbols.forEach(function (sym) {
- if (machine.alphabet.indexOf(sym) != -1) filtered.push(sym)
- })
- } else {
- document.getElementById('multi_char_symbols_tip').style.display = 'none'
- input = input.replace(new RegExp('[^'+machine.alphabet.join('')+']', 'g'), '')
- for (var i = 0; i < input.length; i++)
- if (machine.alphabet.indexOf(input[i]) != -1) filtered.push(input[i])
- }
- if (filtered.length == '') filtered = [machine.blank_symbol]
+ , filtered = filterInput(input)
sim.input = filtered
sim.tapes[0].content = filtered
@@ -89,6 +79,25 @@ function resetSimulation() {
sim.configurations.push(generateConfiguration())
updateInfoField()
}
+/* Filters all characters out of the input that are not in the alphabet
+ * and returns the input as a string array.
+ */
+function filterInput(input) {
+ var filtered = []
+ if (machine.multi_character_symbols) {
+ var symbols = input.split(/\s+/)
+ symbols.forEach(function (sym) {
+ if (machine.alphabet.indexOf(sym) != -1) filtered.push(sym)
+ })
+ } else {
+ for (var i = 0; i < input.length; i++) {
+ if (machine.alphabet.indexOf(input[i]) != -1) filtered.push(input[i])
+ }
+ }
+
+ if (filtered.length == '') filtered = [machine.blank_symbol]
+ return filtered
+}
function loadResizedCanvas() {
var BOX_SIZE = +document.getElementById('tape_size_slider').value
, container = document.getElementById('canvas_container')
View
@@ -17,8 +17,9 @@ table {
}
a.button, a.button:hover, a.button:visited {
+ display: inline-block;
+ margin: 0px;
padding: 5px;
- margin: 5px;
border: 1px solid black;
text-decoration: none;
background-color: white;
@@ -31,6 +32,7 @@ a.button:hover {
a.tabButton, a.tabButton:hover, a.tabButton:visited {
margin-left: -10px;
padding-bottom: 0px;
+ padding-right: 10px;
border-radius: 0px;
font-size: larger;
}
@@ -199,3 +201,12 @@ table.conf {
display: inline;
}
+/* ################################# */
+/* Test */
+div.test_infobox {
+ float: right;
+ margin: 5px;
+ border: 1px solid black;
+ border-radius: 5px;
+ padding: 5px;
+}
View
25 test.js
@@ -1,4 +1,29 @@
/* Functions used for testing the TM.
+
+test = {
+ input : ([string])
+ total_words : (int) // number of words
+ steps_avg : (float)
+ memory_avg : (float)
+ results : ([{
+ result : (string) // "accepted"/"rejected" , [result-word]
+ steps : (int)
+ max_storage_occ : (int)
+ }])
+}
*/
+function resetTest() {
+ test = {
+ input: filterInput(document.getElementById('test_input').value),
+ steps_avg: 0,
+ memory_avg: 0
+ }
+}
+
+function startTest() {
+ disableGui()
+
+ enableGui()
+}
View
@@ -0,0 +1,37 @@
+/* Some functions used by all the different features.
+ */
+
+function setToString(array) {
+ var out = '{'
+ array.forEach(function (val) {out += val+', '})
+ return out.substring(0, out.length-2) + '}'
+}
+function arrayToString(array) {
+ var out = ''
+ array.forEach(function (val) {out += val+', '})
+ return out.substring(0, out.length-2)
+}
+function arrayEqu(a1, a2) {
+ if (a1.length != a2.length) return false
+ for (var i = 0; i < a1.length; a1 ++) if (a1[i] != a2[i]) return false
+ return true
+}
+function findNextTransition() {
+ var t, diff
+ for (var i = 0; i < machine.transitions.length; i++) {
+ t = machine.transitions[i]
+ if (t.origin != sim.state) continue
+ diff = false
+ for (var j = 0; j < t.read.length; j++) {
+ if (t.read[j] != sim.tapes[j].content[sim.tapes[j].head_pos]) {
+ diff = true
+ break
+ }
+ }
+ if (diff) continue
+
+ return {index: i, transition: t}
+ }
+ return null
+}
+

0 comments on commit 4e26e87

Please sign in to comment.