Browse Source

complete render & fix pattern/render proxy param problems

superfree 5 years ago
parent
commit
abbbb47000

+ 34 - 0
lambda-service/src/main/java/com/example/lambda/service/utils/SVGRenderer.java

@@ -0,0 +1,34 @@
+package com.example.lambda.service.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Component
+public class SVGRenderer {
+
+    private List<String> items = new ArrayList();
+
+    public SVGRenderer addText(String text, int x, int y) {
+        String el = String.format("<text x=\"%d\" y=\"%d\">%s</text>", x, y, text);
+        items.add(el);
+        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);
+        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\">");
+        for (String item : items) {
+            res.append(item);
+        }
+        res.append("</svg>");
+        return res.toString();
+    }
+}

+ 11 - 8
lambda-service/src/main/java/com/example/lambda/service/web/LambdaController.java

@@ -1,8 +1,6 @@
 package com.example.lambda.service.web;
 
-import com.example.lambda.service.web.entity.PatternParam;
-import com.example.lambda.service.web.entity.ServiceInfo;
-import com.example.lambda.service.web.entity.ServiceResponse;
+import com.example.lambda.service.web.entity.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -31,11 +29,16 @@ public class LambdaController {
     }
 
     @PostMapping("/pattern")
-    public ServiceResponse pattern(@RequestBody PatternParam data) {
-        System.out.println("pattern: " + data.toString());
-        ServiceResponse response = lambdaService.pattern(data);
-        System.out.println(response);
-        System.out.println(response.getData());
+    public ServiceResponse pattern(@RequestBody PatternParam param) {
+        System.out.println("pattern: " + param.getData().toString());
+        ServiceResponse response = lambdaService.pattern(param);
+        return response;
+    }
+
+    @PostMapping("/render")
+    public ServiceResponse render(@RequestBody RenderParam param) {
+        System.out.println("render: " + param.getData().toString());
+        ServiceResponse response = lambdaService.render(param);
         return response;
     }
 

+ 36 - 5
lambda-service/src/main/java/com/example/lambda/service/web/LambdaService.java

@@ -7,11 +7,15 @@ import com.example.lambda.service.interpreter.InterpreterState;
 import com.example.lambda.service.interpreter.LambdaInterpreter;
 import com.example.lambda.service.interpreter.Result;
 import com.example.lambda.service.interpreter.type.Atom;
+import com.example.lambda.service.utils.SVGRenderer;
 import com.example.lambda.service.web.entity.*;
 import com.example.lambda.service.web.type.IRType;
 import com.example.lambda.service.web.type.IType;
 import com.example.lambda.service.web.type.SourceType;
 import com.example.lambda.service.web.type.TargetType;
+import com.example.lambda.service.web.typedata.AtomInfo;
+import com.example.lambda.service.web.typedata.IRAtom;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -25,6 +29,9 @@ public class LambdaService {
     @Autowired
     private LambdaInterpreter interpreter;
 
+    @Autowired
+    private SVGRenderer svgRenderer;
+
     public String interpreterStatus() {
         return interpreter != null ? "Interpreter is available(" + interpreter + ")" : "Interpreter is unavailable";
     }
@@ -62,7 +69,7 @@ public class LambdaService {
     public ServiceResponse pattern(PatternParam param) {
         if (param.getType() != SourceType.PATTERN) {
             // 参数类型不存在
-            return ServiceResponse.error(PATTERN_SOURCE_TYPE_UNKNOWN);
+            return ServiceResponse1.error(PATTERN_SOURCE_TYPE_UNKNOWN);
         }
         String pattern = param.getData();
         try {
@@ -70,18 +77,18 @@ public class LambdaService {
 //            Result res = interpreter.solveErrorStub(pattern);
             if (res.getState() == InterpreterState.ERROR) {
                 // 解析失败
-                return ServiceResponse.error(res.getMsg());
+                return ServiceResponse1.error(res.getMsg());
             }
             switch (param.getTarget()) {
                 case ATOM:
                     List<Object> atoms = extractAtoms(res.getAst());
-                    return ServiceResponse.success(atoms);
+                    return ServiceResponse1.success(atoms);
                 case AST:
                 default:
-                    return ServiceResponse.success(res.getAst());
+                    return ServiceResponse1.success(res.getAst());
             }
         } catch (Exception e) {
-            return ServiceResponse.error("interpreter error");
+            return ServiceResponse1.error("interpreter error");
         }
     }
 
@@ -104,4 +111,28 @@ public class LambdaService {
         return new ArrayList<>(atoms);
     }
 
+    public ServiceResponse render(RenderParam param) {
+        IRAtom data = new ObjectMapper().convertValue(param.getData(), IRAtom.class);
+        // build atom mapper
+        Map<Integer, String> atoms = new HashMap<>();
+        for (AtomInfo info : data.getAtoms()) {
+            atoms.put(info.getId(), info.getName());
+        }
+//        System.out.println(atoms);
+        // reformat pattern
+        SVGRenderer renderer = new SVGRenderer();
+        int x = 10, y = 20;
+        for (char c : data.getPattern().toCharArray()) {
+            String text;
+            if (Character.isDigit(c)) {
+                renderer.addRect(x, y - 16, 8, 20, "fill:yellow");
+                text = atoms.get(c - '0');
+            } else {
+                text = String.valueOf(c);
+            }
+            renderer.addText(text, x, y);
+            x += 8;
+        }
+        return ServiceResponse1.success(renderer.build());
+    }
 }

+ 1 - 69
lambda-service/src/main/java/com/example/lambda/service/web/entity/ServiceResponse.java

@@ -1,72 +1,4 @@
 package com.example.lambda.service.web.entity;
 
-import com.example.lambda.service.web.type.ServiceResponseType;
-
-import java.util.List;
-
-public class ServiceResponse {
-
-    private ServiceResponseType status;
-    private String msg;
-    private Object data;
-    private List<Object> list;
-
-    public ServiceResponse(ServiceResponseType status, String msg, Object data, List<Object> list) {
-        this.status = status;
-        this.msg = msg;
-        this.data = data;
-        this.list = list;
-    }
-
-    public static ServiceResponse success() {
-        return new ServiceResponse(ServiceResponseType.SUCCESS, "success", null, null);
-    }
-
-    public static ServiceResponse success(Object data) {
-        return new ServiceResponse(ServiceResponseType.SUCCESS, "success", data, null);
-    }
-
-    public static ServiceResponse success(List<Object> list) {
-        return new ServiceResponse(ServiceResponseType.SUCCESS, "success", list, list);
-    }
-
-    public static ServiceResponse error() {
-        return new ServiceResponse(ServiceResponseType.ERROR, "unknown error", null, null);
-    }
-
-    public static ServiceResponse error(String msg) {
-        return new ServiceResponse(ServiceResponseType.ERROR, msg, null, null);
-    }
-
-    public ServiceResponseType getStatus() {
-        return status;
-    }
-
-    public void setStatus(ServiceResponseType status) {
-        this.status = status;
-    }
-
-    public String getMsg() {
-        return msg;
-    }
-
-    public void setMsg(String msg) {
-        this.msg = msg;
-    }
-
-    public Object getData() {
-        return data;
-    }
-
-    public void setData(Object data) {
-        this.data = data;
-    }
-
-    public List<Object> getList() {
-        return list;
-    }
-
-    public void setList(List<Object> list) {
-        this.list = list;
-    }
+public interface ServiceResponse {
 }

+ 72 - 0
lambda-service/src/main/java/com/example/lambda/service/web/entity/ServiceResponse1.java

@@ -0,0 +1,72 @@
+package com.example.lambda.service.web.entity;
+
+import com.example.lambda.service.web.type.ServiceResponseType;
+
+import java.util.List;
+
+public class ServiceResponse1 implements ServiceResponse {
+
+    private ServiceResponseType status;
+    private String msg;
+    private Object data;   // { list: data }
+    private List<Object> list; // []
+
+    public ServiceResponse1(ServiceResponseType status, String msg, Object data, List<Object> list) {
+        this.status = status;
+        this.msg = msg;
+        this.data = data;
+        this.list = list;
+    }
+
+    public static ServiceResponse1 success() {
+        return new ServiceResponse1(ServiceResponseType.SUCCESS, "success", null, null);
+    }
+
+    public static ServiceResponse1 success(Object data) {
+        return new ServiceResponse1(ServiceResponseType.SUCCESS, "success", data, null);
+    }
+
+    public static ServiceResponse1 success(List<Object> list) {
+        return new ServiceResponse1(ServiceResponseType.SUCCESS, "success", null, list);
+    }
+
+    public static ServiceResponse1 error() {
+        return new ServiceResponse1(ServiceResponseType.ERROR, "unknown error", null, null);
+    }
+
+    public static ServiceResponse1 error(String msg) {
+        return new ServiceResponse1(ServiceResponseType.ERROR, msg, null, null);
+    }
+
+    public ServiceResponseType getStatus() {
+        return status;
+    }
+
+    public void setStatus(ServiceResponseType status) {
+        this.status = status;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    public Object getData() {
+        return data;
+    }
+
+    public void setData(Object data) {
+        this.data = data;
+    }
+
+    public List<Object> getList() {
+        return list;
+    }
+
+    public void setList(List<Object> list) {
+        this.list = list;
+    }
+}

+ 35 - 0
lambda-service/src/main/java/com/example/lambda/service/web/entity/ServiceResponse2.java

@@ -0,0 +1,35 @@
+package com.example.lambda.service.web.entity;
+
+import com.example.lambda.service.web.type.ServiceResponseType;
+
+import java.util.List;
+
+public class ServiceResponse2 implements ServiceResponse {
+    private ServiceResponseType status;
+    private String msg;
+    private List<Object> data;   // { list: data }
+
+    public ServiceResponseType getStatus() {
+        return status;
+    }
+
+    public void setStatus(ServiceResponseType status) {
+        this.status = status;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    public List<Object> getData() {
+        return data;
+    }
+
+    public void setData(List<Object> data) {
+        this.data = data;
+    }
+}

+ 22 - 0
lambda-service/src/main/java/com/example/lambda/service/web/typedata/AtomInfo.java

@@ -0,0 +1,22 @@
+package com.example.lambda.service.web.typedata;
+
+public class AtomInfo {
+    private Integer id;
+    private String name;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 24 - 0
lambda-service/src/main/java/com/example/lambda/service/web/typedata/IRAtom.java

@@ -0,0 +1,24 @@
+package com.example.lambda.service.web.typedata;
+
+import java.util.List;
+
+public class IRAtom {
+    private String pattern;
+    private List<AtomInfo> atoms;
+
+    public String getPattern() {
+        return pattern;
+    }
+
+    public void setPattern(String pattern) {
+        this.pattern = pattern;
+    }
+
+    public List<AtomInfo> getAtoms() {
+        return atoms;
+    }
+
+    public void setAtoms(List<AtomInfo> atoms) {
+        this.atoms = atoms;
+    }
+}

+ 1 - 0
subject-center/src/main/java/com/example/subject/center/SubjectCenterApplication.java

@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
+import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 import org.springframework.context.annotation.Bean;
 import org.springframework.web.client.RestTemplate;
 

+ 17 - 8
subject-center/src/main/java/com/example/subject/center/SubjectCenterController.java

@@ -1,10 +1,13 @@
 package com.example.subject.center;
 
 import com.example.subject.center.entity.*;
+import com.example.subject.center.utils.RequestUtils;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.List;
 
 @RestController
@@ -16,10 +19,10 @@ public class SubjectCenterController {
     @Autowired
     private SubjectCenterService subjectCenterService;
 
-    @GetMapping("/")
-    public String info() {
-        return "Default route from Subject Center at " + port;
-    }
+//    @GetMapping("/")
+//    public String info() {
+//        return "Default route from Subject Center at " + port;
+//    }
 
     @GetMapping("/services")
     public List<ServiceInfo> services() {
@@ -31,14 +34,20 @@ public class SubjectCenterController {
     }
 
     @PostMapping("/{serviceName}/pattern")
-    public Response pattern(@PathVariable String serviceName, @RequestBody PatternParam param) {
-        ServiceResponse response = subjectCenterService.patternProxy(getServiceId(serviceName), param);
+    public Response pattern(HttpServletRequest request, @PathVariable String serviceName) {
+        String body = RequestUtils.getBody(request);
+//        System.out.printf("[render proxy]: service=%s\n", serviceName);
+        System.out.printf("[pattern proxy]: service=%s , body=%s\n", serviceName, body);
+        ServiceResponse response = subjectCenterService.patternProxy(getServiceId(serviceName), body);
         return solveServiceResponse(response);
     }
 
     @PostMapping("/{serviceName}/render")
-    public Response render(@PathVariable String serviceName, @RequestBody RenderParam param) {
-        ServiceResponse response = subjectCenterService.renderProxy(getServiceId(serviceName), param);
+    public Response render(HttpServletRequest request, @PathVariable String serviceName) {
+        String body = RequestUtils.getBody(request);
+//        System.out.printf("[render proxy]: service=%s\n", serviceName);
+        System.out.printf("[render proxy]: service=%s , body=%s\n", serviceName, body);
+        ServiceResponse response = subjectCenterService.renderProxy(getServiceId(serviceName), body);
         return solveServiceResponse(response);
     }
 

+ 2 - 2
subject-center/src/main/java/com/example/subject/center/SubjectCenterService.java

@@ -11,7 +11,7 @@ public interface SubjectCenterService {
 
     List<ServiceInfo> getServiceList();
 
-    ServiceResponse patternProxy(String serviceId, PatternParam patternParam);
+    ServiceResponse patternProxy(String serviceId, String body);
 
-    ServiceResponse renderProxy(String serviceId, RenderParam renderParam);
+    ServiceResponse renderProxy(String serviceId, String body);
 }

+ 14 - 9
subject-center/src/main/java/com/example/subject/center/SubjectCenterServiceImpl.java

@@ -3,6 +3,9 @@ package com.example.subject.center;
 import com.example.subject.center.entity.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
@@ -34,22 +37,24 @@ public class SubjectCenterServiceImpl implements SubjectCenterService {
         return serviceInfos;
     }
 
-    private ServiceResponse serviceProxy(String serviceId, String method, Object data) {
+    private ServiceResponse serviceProxy(String serviceId, String method, String body) {
+        // build url
         String url = "http://" + serviceId + "/" + method;
         System.out.println("[" + method + " proxy]: " + url);
-        return restTemplate.postForObject(url, data, ServiceResponse.class);
+        // build entity
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        HttpEntity<String> httpEntity = new HttpEntity<String>(body, headers);
+        return restTemplate.postForObject(url, httpEntity, ServiceResponse.class);
     }
 
     private static final String PROXY_METHOD_PATTERN = "pattern";
     private static final String PROXY_METHOD_RENDER = "render";
 
     @Override
-    public ServiceResponse patternProxy(String serviceId, PatternParam param) {
+    public ServiceResponse patternProxy(String serviceId, String body) {
         try {
-            ServiceResponse response = serviceProxy(serviceId, PROXY_METHOD_PATTERN, param);
-            System.out.println(response.getData());
-            System.out.println(response.getList());
-            return response;
+            return serviceProxy(serviceId, PROXY_METHOD_PATTERN, body);
         } catch (Exception e) {
             e.printStackTrace();
             return ServiceResponse.error();
@@ -57,9 +62,9 @@ public class SubjectCenterServiceImpl implements SubjectCenterService {
     }
 
     @Override
-    public ServiceResponse renderProxy(String serviceId, RenderParam param) {
+    public ServiceResponse renderProxy(String serviceId, String body) {
         try {
-            return serviceProxy(serviceId, PROXY_METHOD_RENDER, param);
+            return serviceProxy(serviceId, PROXY_METHOD_RENDER, body);
         } catch (Exception e) {
             return ServiceResponse.error();
         }

+ 51 - 0
subject-center/src/main/java/com/example/subject/center/utils/RequestUtils.java

@@ -0,0 +1,51 @@
+package com.example.subject.center.utils;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+public class RequestUtils {
+
+    public static String getBody(HttpServletRequest request) {
+        StringBuilder builder = new StringBuilder();
+        BufferedReader reader = null;
+        InputStream in = null;
+        try {
+            in = request.getInputStream();
+            if (in != null) {
+//                System.out.println("has input");
+                reader = new BufferedReader(new InputStreamReader(in));
+                char[] charBuffer = new char[1024];
+                int bytesRead = -1;
+                while ((bytesRead = reader.read(charBuffer)) > 0) {
+//                    System.out.println(bytesRead); // input length
+                    builder.append(charBuffer, 0, bytesRead);
+                }
+            } else {
+//                System.out.println("no input");
+                builder.append("");
+            }
+        } catch (IOException ex) {
+            ex.printStackTrace();
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        String body = builder.toString();
+        return body;
+    }
+}

+ 0 - 2
subject-center/src/main/resources/application.yml

@@ -2,8 +2,6 @@ server:
   port: 8801
 
 eureka:
-  instance:
-    appname: subject-center
   client:
     register-with-eureka: false
     service-url: