|
|
@@ -1,6 +1,8 @@
|
|
|
package com.example.TuringMachine;
|
|
|
|
|
|
|
|
|
+import com.example.TuringMachine.utils.SVGRenderer;
|
|
|
+
|
|
|
import java.io.Serializable;
|
|
|
import java.util.*;
|
|
|
|
|
|
@@ -47,7 +49,7 @@ public class TuringMachine implements Serializable {
|
|
|
}
|
|
|
|
|
|
public int step() {
|
|
|
- if(F.contains(q0)) return 0;
|
|
|
+ if (F.contains(state)) return 0;
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
for (int i = 0; i < heads.size(); i++) {
|
|
|
List<List<Character>> tape = tapes.get(i);
|
|
|
@@ -73,7 +75,7 @@ public class TuringMachine implements Serializable {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if(flag == 0) return -1;
|
|
|
+ if (flag == 0) return -1;
|
|
|
int k = 0;
|
|
|
for (int i = 0; i < heads.size(); i++) {
|
|
|
List<List<Character>> tape = tapes.get(i);
|
|
|
@@ -91,6 +93,81 @@ public class TuringMachine implements Serializable {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+ public int run() {
|
|
|
+ while (true) {
|
|
|
+ int result = this.step();
|
|
|
+ switch (result) {
|
|
|
+ case 0: {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ case 1: {
|
|
|
+ ;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case -1: {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ default: {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String Snapshoot() {
|
|
|
+ return "Snapshoot{" +
|
|
|
+ "tapes=" + tapes.toString() + "\n" +
|
|
|
+ "state=" + state + "\n" +
|
|
|
+ "head=" + heads.toString() + "\n" +
|
|
|
+ "id=" + id + "}";
|
|
|
+ }
|
|
|
+
|
|
|
+ public String show() {
|
|
|
+ int cx = 0;
|
|
|
+ int cy = 15;
|
|
|
+ int x = 60;
|
|
|
+ int y = 20;
|
|
|
+ int tx = 75;
|
|
|
+ int ty = 45;
|
|
|
+ int width = 40;
|
|
|
+ int height = 40;
|
|
|
+ //String rectStyle = "fill:white;stroke:pink;stroke-width:1;fill-opacity:0.1;";
|
|
|
+ HashMap<String, String> rectStyle = new HashMap<>();
|
|
|
+ rectStyle.put("fill", "white");
|
|
|
+ rectStyle.put("stroke", "pink");
|
|
|
+ rectStyle.put("stroke-width", "1");
|
|
|
+ //rectStyle.put("fill-opacity", "0.1");
|
|
|
+ SVGRenderer svgRenderer = new SVGRenderer();
|
|
|
+ int i = 0;
|
|
|
+ for (List<List<Character>> tape : tapes) {
|
|
|
+ int head = heads.get(i);
|
|
|
+ svgRenderer.addText("Tape"+i, cx, cy);
|
|
|
+ for (List<Character> track : tape) {
|
|
|
+ int j = 0;
|
|
|
+ svgRenderer.addText("Track"+j, cx, ty);
|
|
|
+ for (Character character : track) {
|
|
|
+ if (j == head) rectStyle.replace("fill", "yellow");
|
|
|
+ svgRenderer.addRect(x, y, width, height, rectStyle);
|
|
|
+ svgRenderer.addText(String.valueOf(character), tx, ty);
|
|
|
+ if (j == head) rectStyle.replace("fill", "white");
|
|
|
+ x += width;
|
|
|
+ tx += width;
|
|
|
+ j+=1;
|
|
|
+ }
|
|
|
+ y += height;
|
|
|
+ ty += height;
|
|
|
+ }
|
|
|
+ y += 60;
|
|
|
+ cy += 105;
|
|
|
+ ty += 60;
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ return svgRenderer.build();
|
|
|
+ }
|
|
|
+ //{\"id\":\"181250068\",\"Q\":[\"0\",\"1\",\"2\",\"3\",\"4\"],\"S\":[\"a\",\"b\"],\"G\":[\"a\",\"b\",\"_\"],\"q0\":\"0\",\"F\":[\"4\"],\"B\":\"_\",\"N\":\"1\",\"tapes\":[[[\"_\",\"_\",\"_\",\"_\",\"a\",\"a\",\"a\",\"b\",\"b\",\"b\",\"_\",\"_\"]]],\"delta\":[[\"0\",\"a\",\"_\",\"r\",\"1\"],[\"0\",\"_\",\"_\",\"r\",\"4\"],[\"1\",\"a\",\"a\",\"r\",\"1\"],[\"1\",\"b\",\"b\",\"r\",\"1\"],[\"1\",\"_\",\"_\",\"l\",\"2\"],[\"2\",\"b\",\"_\",\"l\",\"3\"],[\"3\",\"a\",\"a\",\"l\",\"3\"],[\"3\",\"b\",\"b\",\"l\",\"3\"],[\"3\",\"_\",\"_\",\"r\",\"0\"]]}
|
|
|
+ //
|
|
|
+
|
|
|
public List<List<List<Character>>> getTapes() {
|
|
|
return tapes;
|
|
|
}
|
|
|
@@ -186,14 +263,4 @@ public class TuringMachine implements Serializable {
|
|
|
public void setState(String state) {
|
|
|
this.state = state;
|
|
|
}
|
|
|
-
|
|
|
- public String Snapshoot() {
|
|
|
- return "Snapshoot{" +
|
|
|
- "tapes=" + tapes.toString() + "\n" +
|
|
|
- "state=" + state + "\n" +
|
|
|
- "head=" + heads.toString() + "\n" +
|
|
|
- "id=" + id + "}";
|
|
|
- }
|
|
|
- //{\"id\":\"181250068\",\"Q\":[\"0\",\"1\",\"2\",\"3\",\"4\"],\"S\":[\"a\",\"b\"],\"G\":[\"a\",\"b\",\"_\"],\"q0\":\"0\",\"F\":[\"4\"],\"B\":\"_\",\"N\":\"1\",\"tapes\":[[[\"_\",\"_\",\"_\",\"_\",\"a\",\"a\",\"a\",\"b\",\"b\",\"b\",\"_\",\"_\"]]],\"delta\":[[\"0\",\"a\",\"_\",\"r\",\"1\"],[\"0\",\"_\",\"_\",\"r\",\"4\"],[\"1\",\"a\",\"a\",\"r\",\"1\"],[\"1\",\"b\",\"b\",\"r\",\"1\"],[\"1\",\"_\",\"_\",\"l\",\"2\"],[\"2\",\"b\",\"_\",\"l\",\"3\"],[\"3\",\"a\",\"a\",\"l\",\"3\"],[\"3\",\"a\",\"b\",\"l\",\"3\"],[\"3\",\"_\",\"_\",\"r\",\"0\"]]}
|
|
|
- //
|
|
|
}
|