ProcessManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package com.yihu.hos.system.service;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.hos.common.graph.BFSGraph;
  5. import com.yihu.hos.common.graph.DGraph;
  6. import com.yihu.hos.common.graph.Edge;
  7. import com.yihu.hos.core.datatype.StringUtil;
  8. import com.yihu.hos.system.dao.AppDao;
  9. import com.yihu.hos.system.dao.AppServiceDao;
  10. import com.yihu.hos.system.dao.FlowProcessDao;
  11. import com.yihu.hos.system.dao.ProcessorDao;
  12. import com.yihu.hos.system.model.SystemApp;
  13. import com.yihu.hos.system.model.SystemServiceEndpoint;
  14. import com.yihu.hos.system.model.SystemServiceFlowProcess;
  15. import com.yihu.hos.system.model.SystemServiceFlowProcessor;
  16. import com.yihu.hos.web.framework.model.Result;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.io.IOException;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. @Service("ProcessManager")
  25. public class ProcessManager {
  26. public static final String BEAN_ID = "ProcessManager";
  27. @Resource(name = AppDao.BEAN_ID)
  28. private AppDao appDao;
  29. @Resource(name = AppServiceDao.BEAN_ID)
  30. private AppServiceDao appServiceDao;
  31. @Resource(name = ProcessorDao.BEAN_ID)
  32. private ProcessorDao processorDao;
  33. @Resource(name = FlowProcessDao.BEAN_ID)
  34. private FlowProcessDao processDao;
  35. private ObjectMapper objectMapper = new ObjectMapper();
  36. public Result getAllApp() throws Exception {
  37. String hql = "from SystemApp";
  38. List<SystemApp> appList = appDao.getEntityList(SystemApp.class, hql);
  39. String result = objectMapper.writeValueAsString(appList);
  40. return Result.success(result);
  41. }
  42. public Result getAllAppService() throws Exception {
  43. String hql = "from SystemServiceEndpoint";
  44. List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getEntityList(SystemServiceEndpoint.class, hql);
  45. String result = objectMapper.writeValueAsString(serviceEndpointList);
  46. return Result.success(result);
  47. }
  48. public Result getAppServiceByAppId(String appId) throws Exception {
  49. String hql = "from SystemServiceEndpoint where appId = "+appId+"";
  50. List<SystemServiceEndpoint> serviceEndpointList = appServiceDao.getEntityList(SystemServiceEndpoint.class, hql);
  51. String result = objectMapper.writeValueAsString(serviceEndpointList);
  52. return Result.success(result);
  53. }
  54. public Result getAllProcessor() throws Exception {
  55. String hql = "from SystemServiceFlowProcessor";
  56. List<SystemServiceFlowProcessor> processorList = processorDao.getEntityList(SystemServiceFlowProcessor.class, hql);
  57. String result = objectMapper.writeValueAsString(processorList);
  58. return Result.success(result);
  59. }
  60. public void saveProcess(String code, String name, String fileName, String positionJsonStr) throws Exception {
  61. SystemServiceFlowProcess process = new SystemServiceFlowProcess();
  62. process.setCode(code);
  63. process.setName(name);
  64. process.setResult(positionJsonStr);
  65. process.setFileName(fileName);
  66. processDao.saveEntity(process);
  67. }
  68. public static void main(String[] args) throws Exception {
  69. String flowJsonStr = "{\n" +
  70. " \"code\": \"cralwer\",\n" +
  71. " \"nodes\": {\n" +
  72. " \"node_1\": {\n" +
  73. " \"name\": \"quartz\",\n" +
  74. " \"value\": \"quartz://myGroup/myTimerName?cron=0/3 * * * * ?\",\n" +
  75. " \"type\": \"service\"\n" +
  76. " },\n" +
  77. " \"node_2\": {\n" +
  78. " \"name\": \"CrawlerProcessor0\",\n" +
  79. " \"value\": \"crawler.processor.CrawlerProcessor0\",\n" +
  80. " \"type\": \"processor\"\n" +
  81. " },\n" +
  82. " \"node_3\": {\n" +
  83. " \"name\": \"crawler\",\n" +
  84. " \"value\": \"body().isEqualTo(false)\",\n" +
  85. " \"type\": \"judgement\"\n" +
  86. " },\n" +
  87. " \"node_4\": {\n" +
  88. " \"name\": \"crawler\",\n" +
  89. " \"value\": \"http4://localhost:8088/crawler/collect\",\n" +
  90. " \"type\": \"service\"\n" +
  91. " },\n" +
  92. " \"node_5\": {\n" +
  93. " \"name\": \"crawler\",\n" +
  94. " \"value\": \"http4://localhost:8088/crawler/patientList\",\n" +
  95. " \"type\": \"service\"\n" +
  96. " }\n" +
  97. " },\n" +
  98. " \"lines\": {\n" +
  99. " \"line_1\": {\n" +
  100. " \"from\": \"node_1\",\n" +
  101. " \"to\": \"node_2\"\n" +
  102. " },\n" +
  103. " \"line_2\": {\n" +
  104. " \"from\": \"node_2\",\n" +
  105. " \"to\": \"node_3\"\n" +
  106. " },\n" +
  107. " \"line_3\": {\n" +
  108. " \"from\": \"node_3\",\n" +
  109. " \"to\": \"node_4\",\n" +
  110. " \"value\": \"correct\"\n" +
  111. " },\n" +
  112. " \"line_4\": {\n" +
  113. " \"from\": \"node_3\",\n" +
  114. " \"to\": \"node_5\"\n" +
  115. " }\n" +
  116. " }\n" +
  117. "}";
  118. formatJson(flowJsonStr);
  119. }
  120. public static String formatJson(String flowJsonStr) throws Exception {
  121. String root = "";
  122. ObjectMapper objectMapper = new ObjectMapper();
  123. JsonNode flowJson = objectMapper.readValue(flowJsonStr, JsonNode.class);
  124. String code = flowJson.get("code").asText();
  125. //sort flow by lines
  126. JsonNode lines = flowJson.get("lines");
  127. Iterator<Map.Entry<String, JsonNode>> lineIterator = lines.fields();
  128. //get nodeMap by nodes
  129. JsonNode nodes = flowJson.get("nodes");
  130. Iterator<Map.Entry<String, JsonNode>> nodeIterator = nodes.fields();
  131. //for the java code import processor class
  132. Map<String, JsonNode> nodeMap = new HashMap<>();
  133. Map<String, JsonNode> lineMap = new HashMap<>();
  134. DGraph<String> mDG = new BFSGraph<String>();
  135. while (nodeIterator.hasNext()) {
  136. Map.Entry<String, JsonNode> map = nodeIterator.next();
  137. nodeMap.put(map.getKey(), map.getValue());
  138. if (StringUtil.isEmpty(mDG.get(0))) {
  139. root = map.getKey();
  140. }
  141. mDG.add(map.getKey());
  142. }
  143. while (lineIterator.hasNext()) {
  144. Map.Entry<String, JsonNode> map = lineIterator.next();
  145. lineMap.put(map.getKey(), map.getValue());
  146. String nodeNameFrom = map.getValue().get("from").asText();
  147. String nodeNameTo = map.getValue().get("to").asText();
  148. mDG.add(new Edge<>(nodeNameFrom, nodeNameTo, map.getKey()));
  149. }
  150. //generate the java code
  151. return generate(code, root, lineMap, nodeMap, mDG);
  152. }
  153. public static String generate(String code, String root, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap, DGraph<String> mDG) throws IOException {
  154. Boolean isFirstNodeFlg = true;
  155. StringBuilder javaBuilder = new StringBuilder();
  156. String javaName = toUpperCaseFirstOne(code)+"Route";
  157. javaBuilder.append("package "+code+".route;\n\n");
  158. javaBuilder.append("import org.apache.camel.Exchange;\n");
  159. javaBuilder.append("import org.apache.camel.builder.RouteBuilder;\n");
  160. javaBuilder.append("import crawler.SplitUtil;\n");
  161. for (String key : nodeMap.keySet()) {
  162. JsonNode node = nodeMap.get(key);
  163. String type = node.get("type").asText();
  164. if (type.equals("processor")) {
  165. javaBuilder.append("import " + node.get("value").asText() + ";\n");
  166. }
  167. }
  168. javaBuilder.append("public class "+javaName+" extends RouteBuilder {\n");
  169. javaBuilder.append("public void configure() throws Exception {\n");
  170. Iterator<String> it = mDG.iterator(root);
  171. while(it.hasNext()) {
  172. String nodeName = it.next();
  173. JsonNode node = nodeMap.get(nodeName);
  174. String type = node.get("type").asText();
  175. String value = node.get("value").asText();
  176. String name = node.get("name").asText();
  177. if (isFirstNodeFlg) {
  178. javaBuilder.append("from(\"");
  179. javaBuilder.append(value + "\")");
  180. javaBuilder.append(".routeId(\""+code+"\")");
  181. isFirstNodeFlg = false;
  182. } else {
  183. if (type.equals("processor")) {
  184. JsonNode args = node.get("args");
  185. if (args == null) {
  186. javaBuilder.append("\n.process(\"new "+name+"())");
  187. } else {
  188. String argStr = "";
  189. String[] argArr = args.asText().split(",");
  190. for (String arg : argArr) {
  191. argStr += "\"" + arg + "\",";
  192. }
  193. argStr = StringUtil.substring(argStr, 0, argStr.length() - 1);
  194. javaBuilder.append("\n.process(\"new "+name+"("+argStr+"))");
  195. }
  196. } else if (type.equals("judgement")) {
  197. judgement(javaBuilder, value, nodeName, mDG, it, lineMap, nodeMap);
  198. } else if (type.equals("circle")) {
  199. split(javaBuilder, value);
  200. } else if (type.equals("aggregate")) {
  201. aggregate(javaBuilder);
  202. } else if (type.equals("multicast")) {
  203. mulitcast(javaBuilder, value, nodeName, mDG, it, lineMap, nodeMap);
  204. } else {
  205. javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
  206. javaBuilder.append("\n.to(\"");
  207. javaBuilder.append(value + "\")");
  208. }
  209. }
  210. }
  211. javaBuilder.append("\n}\n}");
  212. System.out.println(javaBuilder.toString());
  213. // String packageFilePath = System.getProperty("user.dir");
  214. //
  215. // String filePath = packageFilePath + "/" + javaName + ".java";
  216. // File file = new File(filePath);
  217. //
  218. // FileWriter fw = new FileWriter(file);
  219. // fw.write(javaBuilder.toString());
  220. // fw.flush();
  221. // fw.close();//这里只是产生一个JAVA文件,简单的IO操作
  222. //
  223. // //upload to mongo
  224. // String newFileName;
  225. // try {
  226. // newFileName = GridFSUtil.uploadFile(filePath, file.getName(), null);
  227. // if (!StringUtil.isEmpty(newFileName)) {
  228. // return newFileName;
  229. // }
  230. // } catch (Exception e) {
  231. // e.printStackTrace();
  232. // }
  233. return "";
  234. }
  235. //首字母转大写
  236. public static String toUpperCaseFirstOne(String s) {
  237. if(Character.isUpperCase(s.charAt(0)))
  238. return s;
  239. else
  240. return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
  241. }
  242. public static void judgement(StringBuilder javaBuilder, String value, String nodeName, DGraph<String> mDG, Iterator<String> it, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
  243. javaBuilder.append("\n.when("+value+")");
  244. List<Edge<String>> edgeList = mDG.getEdgeList(nodeName);
  245. String trueNodeName = "";
  246. String falseNodeName = "";
  247. for (Edge<String> edge : edgeList) {
  248. it.next();
  249. String nextNodeName = edge.getDest();
  250. String nextLineName = edge.getName();
  251. JsonNode nextLine = lineMap.get(nextLineName);
  252. if (nextLine.get("value") != null &&nextLine.get("value").asText().equals("correct")) {
  253. trueNodeName = nextNodeName;
  254. } else {
  255. falseNodeName = nextNodeName;
  256. }
  257. }
  258. JsonNode node1 = nodeMap.get(trueNodeName);
  259. JsonNode node2 = nodeMap.get(falseNodeName);
  260. String firstValue = node1.get("value").asText();
  261. String secondValue = node2.get("value").asText();
  262. javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
  263. javaBuilder.append("\n.to(\"");
  264. javaBuilder.append(firstValue + "\")");
  265. javaBuilder.append(".otherwise()");
  266. javaBuilder.append("\n.setHeader(Exchange.HTTP_METHOD, constant(\"POST\"))");
  267. javaBuilder.append("\n.to(\"");
  268. javaBuilder.append(secondValue + "\")");
  269. }
  270. public static void split(StringBuilder javaBuilder, String value) {
  271. javaBuilder.append("\n.split().method(Split.class, \""+value+"\")");
  272. }
  273. public static void aggregate(StringBuilder javaBuilder) {
  274. javaBuilder.append("\n.aggregate(header(\"test_correlation_key\"), new Aggregate()).completionSize(3)");
  275. }
  276. public static void mulitcast(StringBuilder javaBuilder, String value, String nodeName, DGraph<String> mDG, Iterator<String> it, Map<String, JsonNode> lineMap, Map<String, JsonNode> nodeMap) {
  277. List<Edge<String>> edgeList = mDG.getEdgeList(nodeName);
  278. String endpoints = "";
  279. for (Edge<String> edge : edgeList) {
  280. String nextNodeName = edge.getDest();
  281. JsonNode node = nodeMap.get(nextNodeName);
  282. endpoints += "\"" + node.get("value") + "\",";
  283. it.next();
  284. }
  285. endpoints = StringUtil.substring(endpoints, 0, endpoints.length() - 1);
  286. javaBuilder.append("\n.multicast().stopOnException().to("+endpoints+").end()");
  287. }
  288. }