2 Commits f6eca993aa ... a77944a21d

Author SHA1 Message Date
  pkun a77944a21d rm target 5 years ago
  pkun 8a1fcd19d2 done svg yes! 5 years ago

+ 1 - 1
.idea/misc.xml

@@ -10,7 +10,7 @@
       </list>
     </option>
   </component>
-  <component name="ProjectRootManager">
+  <component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/out" />
   </component>
 </project>

+ 24 - 8
turingMachine-service/src/main/java/com/example/TuringMachine/TMService.java

@@ -12,7 +12,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
-import javax.crypto.spec.PSource;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -25,7 +24,8 @@ import java.util.List;
 public class TMService {
 
     private final static String PATTERN_SOURCE_TYPE_UNKNOWN = "无法解析参数类型";
-    private final static String ERROR = "迁移函数编写有误,本次单步未找到能够执行的迁移函数且不在终止态";
+    private final static String RUNTIME_ERROR = "迁移函数编写有误,本次单步未找到能够执行的迁移函数且不在终止态";
+    private final static String TM_ERROR = "迁移函数导致纸带越界";
 
     @Autowired
     private TMInterpreter interpreter;
@@ -61,7 +61,7 @@ public class TMService {
         // render options
         List<TypeMap<IRType, TargetType>> renderOptions = new ArrayList<>();
         // 渲染图灵机
-        renderOptions.add(new TypeMap<>(IRType.TM, TargetType.SHOW));
+        renderOptions.add(new TypeMap<>(IRType.ID, TargetType.SHOW));
         serviceInfo.setRenderOptions(renderOptions);
 
         return serviceInfo;
@@ -93,14 +93,20 @@ public class TMService {
             try {
                 switch (param.getTarget()) {
                     case TM: {
-                        JSONObject jsonObject = JSONObject.parseObject(param.getData());
                         return ResponseUtils.build(map.get(JSONObject.parseObject(param.getData()).getInteger("id")).Snapshoot());
                     }
                     case STEP: {
                         TuringMachine tm = map.get(JSONObject.parseObject(param.getData()).getInteger("id"));
                         int result = tm.step();
-                        if(result==-1) return ResponseUtils.build(ERROR);
-                        if(result==0) return ResponseUtils.build("HALT");
+                        if (result == -1) return ResponseUtils.build(RUNTIME_ERROR);
+                        if (result == 0) return ResponseUtils.build("HALT");
+                        return ResponseUtils.build(tm.Snapshoot());
+                    }
+                    case RUN: {
+                        TuringMachine tm = map.get(JSONObject.parseObject(param.getData()).getInteger("id"));
+                        int result = tm.run();
+                        if (result == -1) return ResponseUtils.build(RUNTIME_ERROR);
+                        if (result == 0) return ResponseUtils.build(tm.Snapshoot());
                         return ResponseUtils.build(tm.Snapshoot());
                     }
                     default: {
@@ -109,13 +115,23 @@ public class TMService {
                 }
             } catch (Exception e) {
                 e.printStackTrace();
-                return ResponseUtils.error("interpreter error");
+                return ResponseUtils.error(TM_ERROR);
             }
         }
     }
 
     public ResponseEntity<String> render(RenderParam param) {
-        return null;
+        try {
+            int n = JSONObject.parseObject(param.getData()).getInteger("id");
+            if (!map.containsKey(n)) return ResponseUtils.error("请先使用pattern->IR注册一个图灵机");
+            else{
+                TuringMachine tm = map.get(n);
+                return ResponseUtils.build(tm.show());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return ResponseUtils.error("render error");
+        }
     }
 
 

+ 79 - 12
turingMachine-service/src/main/java/com/example/TuringMachine/TuringMachine.java

@@ -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\"]]}
-    //
 }

+ 4 - 4
turingMachine-service/src/main/java/com/example/TuringMachine/entity/RenderParam.java

@@ -7,10 +7,10 @@ import com.example.TuringMachine.type.TargetType;
 public class RenderParam {
 
     private IRType type;
-    private Object data;
+    private String data;
     private TargetType target;
 
-    public RenderParam(IRType type, Object data, TargetType target) {
+    public RenderParam(IRType type, String data, TargetType target) {
         this.type = type;
         this.data = data;
         this.target = target;
@@ -24,11 +24,11 @@ public class RenderParam {
         this.type = type;
     }
 
-    public Object getData() {
+    public String getData() {
         return data;
     }
 
-    public void setData(Object data) {
+    public void setData(String data) {
         this.data = data;
     }
 

+ 1 - 0
turingMachine-service/src/main/java/com/example/TuringMachine/type/IRType.java

@@ -5,6 +5,7 @@ package com.example.TuringMachine.type;
  */
 public enum IRType implements IType {
     TM("图灵机"),
+    ID("用户ID"),
     STEP("单步"),
     RUN("执行到底");
 

+ 11 - 3
turingMachine-service/src/main/java/com/example/TuringMachine/utils/SVGRenderer.java

@@ -3,6 +3,7 @@ package com.example.TuringMachine.utils;
 import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 
 @Component
@@ -16,15 +17,22 @@ public class SVGRenderer {
         return this;
     }
 
-    public SVGRenderer addRect(int x, int y, int width, int height, String style) {
-        String rect = String.format("<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" style=\"%s\"/>", x, y, width, height, style);
+    public SVGRenderer addRect(int x, int y, int width, int height, HashMap<String, String> style) {
+        StringBuilder stringBuilder = new StringBuilder();
+        for(String key:style.keySet()){
+            stringBuilder.append(key);
+            stringBuilder.append(":");
+            stringBuilder.append(style.get(key));
+            stringBuilder.append(";");
+        }
+        String rect = String.format("<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" style=\"%s\"/>", x, y, width, height, stringBuilder.toString());
         items.add(rect);
         return this;
     }
 
     public String build() {
         StringBuilder res = new StringBuilder();
-        res.append("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
+        res.append("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"  style=\"height: 1000px;width: 1000px;\">");
         for (String item : items) {
             res.append(item);
         }