|
|
@@ -9,14 +9,13 @@ 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.type.*;
|
|
|
import com.example.lambda.service.web.typedata.IRAtom;
|
|
|
+import com.example.lambda.service.web.utils.ResponseUtils;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
@@ -36,14 +35,11 @@ public class LambdaService {
|
|
|
return interpreter != null ? "Interpreter is available(" + interpreter + ")" : "Interpreter is unavailable";
|
|
|
}
|
|
|
|
|
|
- private List<TypeInfo> createTypeInfos(IType[] types) {
|
|
|
- List<TypeInfo> infos = new ArrayList<>();
|
|
|
- for (IType type : types) {
|
|
|
- infos.add(new TypeInfo(type));
|
|
|
- }
|
|
|
- return infos;
|
|
|
- }
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 获取服务列表
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public ServiceInfo info() {
|
|
|
ServiceInfo info = new ServiceInfo();
|
|
|
// add types
|
|
|
@@ -66,10 +62,24 @@ public class LambdaService {
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
- public ServiceResponse pattern(PatternParam param) {
|
|
|
+ private List<TypeInfo> createTypeInfos(IType[] types) {
|
|
|
+ List<TypeInfo> infos = new ArrayList<>();
|
|
|
+ for (IType type : types) {
|
|
|
+ infos.add(new TypeInfo(type));
|
|
|
+ }
|
|
|
+ return infos;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析服务
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResponseEntity<String> pattern(PatternParam param) {
|
|
|
if (param.getType() != SourceType.PATTERN) {
|
|
|
// 参数类型不存在
|
|
|
- return ServiceResponse1.error(PATTERN_SOURCE_TYPE_UNKNOWN);
|
|
|
+ return ResponseUtils.error(PATTERN_SOURCE_TYPE_UNKNOWN);
|
|
|
}
|
|
|
String pattern = param.getData();
|
|
|
try {
|
|
|
@@ -77,22 +87,24 @@ public class LambdaService {
|
|
|
// Result res = interpreter.solveErrorStub(pattern);
|
|
|
if (res.getState() == InterpreterState.ERROR) {
|
|
|
// 解析失败
|
|
|
- return ServiceResponse1.error(res.getMsg());
|
|
|
+ return ResponseUtils.error(res.getMsg());
|
|
|
}
|
|
|
+ System.out.println(res.getAst());
|
|
|
switch (param.getTarget()) {
|
|
|
case ATOM:
|
|
|
- List<Object> atoms = extractAtoms(res.getAst());
|
|
|
- return ServiceResponse1.success(atoms);
|
|
|
+ IRAtom irAtom = new IRAtom("\\1.\\2.2", extractAtoms(res.getAst()));
|
|
|
+ return ResponseUtils.build(irAtom);
|
|
|
case AST:
|
|
|
default:
|
|
|
- return ServiceResponse1.success(res.getAst());
|
|
|
+ return ResponseUtils.build(res.getAst());
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- return ServiceResponse1.error("interpreter error");
|
|
|
+ e.printStackTrace();
|
|
|
+ return ResponseUtils.error("interpreter error");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private List<Object> extractAtoms(Ast ast) {
|
|
|
+ private List<Atom> extractAtoms(Ast ast) {
|
|
|
Set<Atom> atoms = new HashSet<>();
|
|
|
Queue<Ast> Q = new LinkedList<>();
|
|
|
Q.add(ast);
|
|
|
@@ -111,12 +123,35 @@ public class LambdaService {
|
|
|
return new ArrayList<>(atoms);
|
|
|
}
|
|
|
|
|
|
- public ServiceResponse render(RenderParam param) {
|
|
|
- IRAtom data = new ObjectMapper().convertValue(param.getData(), IRAtom.class);
|
|
|
+ /**
|
|
|
+ * 渲染服务
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResponseEntity<String> render(RenderParam param) {
|
|
|
+ String el = "";
|
|
|
+ switch (param.getType()) {
|
|
|
+ case ATOM:
|
|
|
+ el = renderAtom(new ObjectMapper().convertValue(param.getData(), IRAtom.class));
|
|
|
+ break;
|
|
|
+ case AST:
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return ResponseUtils.build(el);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return ResponseUtils.error("json serialize error");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String renderAtom(IRAtom data) {
|
|
|
// build atom mapper
|
|
|
Map<Integer, String> atoms = new HashMap<>();
|
|
|
- for (AtomInfo info : data.getAtoms()) {
|
|
|
- atoms.put(info.getId(), info.getName());
|
|
|
+ for (Atom atom : data.getAtoms()) {
|
|
|
+ atoms.put(atom.getId(), atom.getName());
|
|
|
}
|
|
|
// System.out.println(atoms);
|
|
|
// reformat pattern
|
|
|
@@ -133,6 +168,6 @@ public class LambdaService {
|
|
|
renderer.addText(text, x, y);
|
|
|
x += 8;
|
|
|
}
|
|
|
- return ServiceResponse1.success(renderer.build());
|
|
|
+ return renderer.build();
|
|
|
}
|
|
|
}
|