|
@ -2,8 +2,8 @@ package com.yihu.hos.system.service;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.hos.common.graph.BFSGraph;
|
|
|
import com.yihu.hos.common.graph.DGraph;
|
|
|
import com.yihu.hos.common.graph.DGraphImpl;
|
|
|
import com.yihu.hos.common.graph.IDGraph;
|
|
|
import com.yihu.hos.common.graph.Edge;
|
|
|
import com.yihu.hos.core.datatype.StringUtil;
|
|
|
import com.yihu.hos.system.dao.AppDao;
|
|
@ -17,7 +17,6 @@ import net.sf.json.JSONObject;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service("ProcessManager")
|
|
@ -101,34 +100,36 @@ public class ProcessManager {
|
|
|
" \"code\": \"crawler\",\n" +
|
|
|
" \"nodes\": {\n" +
|
|
|
" \"node_1\": {\n" +
|
|
|
" \"name\": \"quartz\",\n" +
|
|
|
" \"name\": \"Quartz\",\n" +
|
|
|
" \"value\": \"quartz://myGroup/myTimerName?cron=0/3 * * * * ?\",\n" +
|
|
|
" \"type\": \"service\"\n" +
|
|
|
" \"nodeType\": \"default\"\n" +
|
|
|
" },\n" +
|
|
|
" \"node_2\": {\n" +
|
|
|
" \"name\": \"Processor0\",\n" +
|
|
|
" \"value\": \"crawler.processor.Processor0\",\n" +
|
|
|
" \"type\": \"processor\"\n" +
|
|
|
" \"name\": \"DefaultHttpProcessor\",\n" +
|
|
|
" \"value\": \"com.yihu.hos.broker.common.processor.DefaultHttpProcessor\",\n" +
|
|
|
"\t\t\t\"param\": \"{\\\"jobId\\\":\\\"5ad5c11655d443c30155d477a6b10000\\\"}\",\n" +
|
|
|
" \"nodeType\": \"processor\"\n" +
|
|
|
" },\n" +
|
|
|
" \"node_3\": {\n" +
|
|
|
" \"name\": \"crawler0\",\n" +
|
|
|
" \"value\": \"http4://localhost:8088/crawler/patientList\",\n" +
|
|
|
" \"type\": \"service\"\n" +
|
|
|
" \"name\": \"Crawler0\",\n" +
|
|
|
" \"value\": \"http4://localhost:8088/crawler/patientList?method=post\",\n" +
|
|
|
" \"nodeType\": \"service\"\n" +
|
|
|
" },\n" +
|
|
|
" \"node_4\": {\n" +
|
|
|
" \"name\": \"multicast0\",\n" +
|
|
|
" \"value\": \"2\",\n" +
|
|
|
" \"type\": \"multicast\"\n" +
|
|
|
" \"name\": \"Circle\",\n" +
|
|
|
" \"value\": \"splitJsonArray\",\n" +
|
|
|
" \"nodeType\": \"circle\"\n" +
|
|
|
" },\n" +
|
|
|
" \"node_5\": {\n" +
|
|
|
" \"name\": \"crawle1r1\",\n" +
|
|
|
" \"value\": \"http4://localhost:8088/crawler/cralwer\",\n" +
|
|
|
" \"type\": \"service\"\n" +
|
|
|
" \"name\": \"HttpProcessor\",\n" +
|
|
|
" \"value\": \"com.yihu.hos.broker.common.processor.HttpProcessor\",\n" +
|
|
|
"\t\t\t\"param\": \"str\",\n" +
|
|
|
" \"nodeType\": \"processor\"\n" +
|
|
|
" },\n" +
|
|
|
" \"node_6\": {\n" +
|
|
|
" \"name\": \"crawler2\",\n" +
|
|
|
" \"value\": \"http4://localhost:8088/crawler/collect\",\n" +
|
|
|
" \"type\": \"service\"\n" +
|
|
|
" \"name\": \"Crawler2\",\n" +
|
|
|
" \"value\": \"http4://localhost:8088/crawler/collect?method=post\",\n" +
|
|
|
" \"nodeType\": \"service\"\n" +
|
|
|
" }\n" +
|
|
|
" },\n" +
|
|
|
" \"lines\": {\n" +
|
|
@ -140,7 +141,7 @@ public class ProcessManager {
|
|
|
" \"from\": \"node_2\",\n" +
|
|
|
" \"to\": \"node_3\"\n" +
|
|
|
" },\n" +
|
|
|
" \"line3\": {\n" +
|
|
|
" \"line_3\": {\n" +
|
|
|
" \"from\": \"node_3\",\n" +
|
|
|
" \"to\": \"node_4\"\n" +
|
|
|
" },\n" +
|
|
@ -149,7 +150,7 @@ public class ProcessManager {
|
|
|
" \"to\": \"node_5\"\n" +
|
|
|
" },\n" +
|
|
|
" \"line_5\": {\n" +
|
|
|
" \"from\": \"node_4\",\n" +
|
|
|
" \"from\": \"node_5\",\n" +
|
|
|
" \"to\": \"node_6\"\n" +
|
|
|
" }\n" +
|
|
|
" }\n" +
|
|
@ -172,11 +173,12 @@ public class ProcessManager {
|
|
|
//for the java code import processor class
|
|
|
Map<String, JsonNode> nodeMap = new HashMap<>();
|
|
|
Map<String, JsonNode> lineMap = new HashMap<>();
|
|
|
DGraph<String> mDG = new BFSGraph<String>();
|
|
|
IDGraph<String> mDG = new DGraphImpl<String>();
|
|
|
while (nodeIterator.hasNext()) {
|
|
|
Map.Entry<String, JsonNode> map = nodeIterator.next();
|
|
|
nodeMap.put(map.getKey(), map.getValue());
|
|
|
if (StringUtil.isEmpty(mDG.get(0))) {
|
|
|
String type = map.getValue().get("nodeType").asText();
|
|
|
if (StringUtil.isEmpty(root) && type.equals("default")) {
|
|
|
root = map.getKey();
|
|
|
}
|
|
|
mDG.add(map.getKey());
|
|
@ -189,164 +191,14 @@ public class ProcessManager {
|
|
|
String nodeNameTo = map.getValue().get("to").asText();
|
|
|
mDG.add(new Edge<>(nodeNameFrom, nodeNameTo, map.getKey()));
|
|
|
}
|
|
|
|
|
|
ProcessEditor editor = new ProcessEditor();
|
|
|
Iterator<String> nodeIt = mDG.iterator(root);
|
|
|
editor.setCode(code);
|
|
|
editor.setNodeIt(nodeIt);
|
|
|
editor.setmDG(mDG);
|
|
|
editor.setLineMap(lineMap);
|
|
|
editor.setNodeMap(nodeMap);
|
|
|
//generate the java code
|
|
|
return generate(code, root, lineMap, nodeMap, mDG);
|
|
|
}
|
|
|
|
|
|
public static String generate(String code, String root, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap, DGraph<String> mDG) throws IOException {
|
|
|
Boolean isFirstNodeFlg = true;
|
|
|
StringBuilder javaBuilder = new StringBuilder();
|
|
|
StringBuilder bodyBuilder = new StringBuilder();
|
|
|
StringBuilder packageBuilder = new StringBuilder();
|
|
|
String javaName = toUpperCaseFirstOne(code)+"Route";
|
|
|
|
|
|
packageBuilder.append("package "+code+".route;\n\n");
|
|
|
packageBuilder.append("import org.apache.camel.Exchange;\n");
|
|
|
packageBuilder.append("import org.apache.camel.builder.RouteBuilder;\n");
|
|
|
|
|
|
for (String key : nodeMap.keySet()) {
|
|
|
JsonNode node = nodeMap.get(key);
|
|
|
String type = node.get("type").asText();
|
|
|
if (type.equals("processor")) {
|
|
|
packageBuilder.append("import " + node.get("value").asText() + ";\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bodyBuilder.append("public class "+javaName+" extends RouteBuilder {\n");
|
|
|
bodyBuilder.append("public void configure() throws Exception {\n");
|
|
|
|
|
|
Iterator<String> it = mDG.iterator(root);
|
|
|
while(it.hasNext()) {
|
|
|
String nodeName = it.next();
|
|
|
JsonNode node = nodeMap.get(nodeName);
|
|
|
String type = node.get("type").asText();
|
|
|
String value = node.get("value").asText();
|
|
|
String name = node.get("name").asText();
|
|
|
if (isFirstNodeFlg) {
|
|
|
bodyBuilder.append("from(\"");
|
|
|
bodyBuilder.append(value + "\")");
|
|
|
bodyBuilder.append(".routeId(\""+code+"\")");
|
|
|
isFirstNodeFlg = false;
|
|
|
} else {
|
|
|
if (type.equals("processor")) {
|
|
|
JsonNode args = node.get("args");
|
|
|
if (args == null) {
|
|
|
bodyBuilder.append("\n.process(new "+name+"())");
|
|
|
} else {
|
|
|
String argStr = "";
|
|
|
String[] argArr = args.asText().split(",");
|
|
|
for (String arg : argArr) {
|
|
|
argStr += "\"" + arg + "\",";
|
|
|
}
|
|
|
argStr = StringUtil.substring(argStr, 0, argStr.length() - 1);
|
|
|
bodyBuilder.append("\n.process(new "+name+"("+argStr+"))");
|
|
|
}
|
|
|
} else if (type.equals("judgement")) {
|
|
|
judgement(bodyBuilder, value, getEdgeList(nodeName, mDG), it, lineMap, nodeMap);
|
|
|
} else if (type.equals("circle")) {
|
|
|
split(bodyBuilder, packageBuilder, value);
|
|
|
} else if (type.equals("aggregate")) {
|
|
|
aggregate(bodyBuilder, packageBuilder, value);
|
|
|
} else if (type.equals("multicast")) {
|
|
|
mulitcast(bodyBuilder, value, getEdgeList(nodeName, mDG), it, lineMap, nodeMap);
|
|
|
} else {
|
|
|
// bodyBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
bodyBuilder.append("\n.to(\"");
|
|
|
bodyBuilder.append(value + "\")");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
bodyBuilder.append(";");
|
|
|
bodyBuilder.append("\n}\n}");
|
|
|
javaBuilder.append(packageBuilder).append(bodyBuilder);
|
|
|
System.out.println(javaBuilder.toString());
|
|
|
// String packageFilePath = System.getProperty("user.dir");
|
|
|
//
|
|
|
// String filePath = packageFilePath + "/" + javaName + ".java";
|
|
|
// File file = new File(filePath);
|
|
|
//
|
|
|
// FileWriter fw = new FileWriter(file);
|
|
|
// fw.write(bodyBuilder.toString());
|
|
|
// fw.flush();
|
|
|
// fw.close();//这里只是产生一个JAVA文件,简单的IO操作
|
|
|
//
|
|
|
// //upload to mongo
|
|
|
// String newFileName;
|
|
|
// try {
|
|
|
// newFileName = GridFSUtil.uploadFile(filePath, file.getName(), null);
|
|
|
// if (!StringUtil.isEmpty(newFileName)) {
|
|
|
// return newFileName;
|
|
|
// }
|
|
|
// } catch (Exception e) {
|
|
|
// e.printStackTrace();
|
|
|
// }
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
//首字母转大写
|
|
|
public static String toUpperCaseFirstOne(String s) {
|
|
|
if(Character.isUpperCase(s.charAt(0)))
|
|
|
return s;
|
|
|
else
|
|
|
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
|
|
|
}
|
|
|
|
|
|
public static List<Edge<String>> getEdgeList(String nodeName, DGraph<String> mDG) {
|
|
|
return mDG.getEdgeList(nodeName);
|
|
|
}
|
|
|
|
|
|
public static void judgement(StringBuilder bodyBuilder, String value, List<Edge<String>> edgeList, Iterator<String> it, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
|
|
|
bodyBuilder.append("\n.choice()");
|
|
|
bodyBuilder.append("\n.when("+value+")");
|
|
|
String trueNodeName = "";
|
|
|
String falseNodeName = "";
|
|
|
for (Edge<String> edge : edgeList) {
|
|
|
it.next();
|
|
|
String nextNodeName = edge.getDest();
|
|
|
String nextLineName = edge.getName();
|
|
|
JsonNode nextLine = lineMap.get(nextLineName);
|
|
|
if (nextLine.get("value") != null && nextLine.get("value").asText().equals("correct")) {
|
|
|
trueNodeName = nextNodeName;
|
|
|
} else {
|
|
|
falseNodeName = nextNodeName;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
JsonNode node1 = nodeMap.get(trueNodeName);
|
|
|
JsonNode node2 = nodeMap.get(falseNodeName);
|
|
|
String firstValue = node1.get("value").asText();
|
|
|
String secondValue = node2.get("value").asText();
|
|
|
bodyBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
bodyBuilder.append("\n.to(\"");
|
|
|
bodyBuilder.append(firstValue + "\")");
|
|
|
bodyBuilder.append(".otherwise()");
|
|
|
bodyBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
|
|
|
bodyBuilder.append("\n.to(\"");
|
|
|
bodyBuilder.append(secondValue + "\")");
|
|
|
bodyBuilder.append("\n.end()");
|
|
|
}
|
|
|
|
|
|
public static void split(StringBuilder bodyBuilder, StringBuilder packageBuilder, String value) {
|
|
|
packageBuilder.append("import crawler.Split;\n");
|
|
|
bodyBuilder.append("\n.split().method(Split.class, \""+value+"\")");
|
|
|
}
|
|
|
|
|
|
public static void aggregate(StringBuilder bodyBuilder, StringBuilder packageBuilder, String value) {
|
|
|
packageBuilder.append("import crawler.Aggregate;\n");
|
|
|
bodyBuilder.append("\n.aggregate(header(\"test_correlation_key\"), new Aggregate()).completionSize(3)");
|
|
|
}
|
|
|
|
|
|
public static void mulitcast(StringBuilder bodyBuilder, String value, List<Edge<String>> edgeList, Iterator<String> it, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
|
|
|
|
|
|
String endpoints = "";
|
|
|
for (Edge<String> edge : edgeList) {
|
|
|
String nextNodeName = edge.getDest();
|
|
|
JsonNode node = nodeMap.get(nextNodeName);
|
|
|
endpoints += node.get("value") + ",";
|
|
|
it.next();
|
|
|
}
|
|
|
endpoints = StringUtil.substring(endpoints, 0, endpoints.length() - 1);
|
|
|
bodyBuilder.append("\n.multicast().stopOnException().to("+endpoints+").end()");
|
|
|
return editor.generate();
|
|
|
}
|
|
|
}
|