|
|
@@ -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());
|
|
|
+ }
|
|
|
}
|