KgFacade.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package com.diagbot.facade;
  2. import com.diagbot.dto.BaseNodeDTO;
  3. import com.diagbot.dto.BaseNodeRSDTO;
  4. import com.diagbot.dto.CategorieDTO;
  5. import com.diagbot.dto.Categories;
  6. import com.diagbot.dto.GNodeDTO;
  7. import com.diagbot.dto.GraDTO;
  8. import com.diagbot.dto.GraphDTO;
  9. import com.diagbot.dto.GraphLabelDTO;
  10. import com.diagbot.dto.Link;
  11. import com.diagbot.dto.LinkDTO;
  12. import com.diagbot.dto.Node;
  13. import com.diagbot.dto.TreeDTO;
  14. import com.diagbot.dto.Tu;
  15. import com.diagbot.exception.CommonErrorCode;
  16. import com.diagbot.exception.CommonException;
  17. import com.diagbot.service.impl.KgServiceImpl;
  18. import com.diagbot.util.EntityUtil;
  19. import com.diagbot.util.FastJsonUtils;
  20. import com.diagbot.util.ListUtil;
  21. import com.diagbot.vo.KgQueryVO;
  22. import com.diagbot.vo.KgTreeVO;
  23. import org.springframework.stereotype.Component;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.LinkedHashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * @Description: 用户日志业务层
  31. * @author: gaodm
  32. * @time: 2018/8/6 9:11
  33. */
  34. @Component
  35. public class KgFacade extends KgServiceImpl {
  36. public GraphLabelDTO getGraphFac(KgQueryVO kgQueryVO) {
  37. GraphLabelDTO graphLabelDTO = new GraphLabelDTO();
  38. List<CategorieDTO> categories = new ArrayList<>();
  39. List<GNodeDTO> node = new ArrayList<>();
  40. List<LinkDTO> links = new ArrayList<>();
  41. List<GraphDTO> res = this.getGraph(kgQueryVO);
  42. if (ListUtil.isEmpty(res)) {
  43. throw new CommonException(CommonErrorCode.NOT_EXISTS);
  44. } else {
  45. Integer nodeId = 0;
  46. categories.add(new CategorieDTO("中心词"));
  47. categories.add(new CategorieDTO("关系"));
  48. Map<String, Integer> cMap = new HashMap<>();
  49. cMap.put("中心词", 0);
  50. cMap.put("关系", 1);
  51. GraphDTO graphDTO = res.get(0);
  52. GNodeDTO gNodeDTO
  53. = new GNodeDTO(0, graphDTO.getName(), nodeId, "circle", 50);
  54. nodeId++;
  55. node.add(gNodeDTO);
  56. if (ListUtil.isNotEmpty(graphDTO.getENodeRSDTOS())) {
  57. Integer rsId = 2;
  58. for (BaseNodeRSDTO baseNodeRSDTO : graphDTO.getENodeRSDTOS()) {
  59. if (null == cMap.get(baseNodeRSDTO.getRType())) {
  60. cMap.put(baseNodeRSDTO.getRType(), rsId);
  61. categories.add(new CategorieDTO(baseNodeRSDTO.getRType()));
  62. rsId++;
  63. }
  64. GNodeDTO nNodeDTO
  65. = new GNodeDTO(1, "", nodeId, "diamond", 10);
  66. node.add(nNodeDTO);
  67. links.add(new LinkDTO(gNodeDTO.getName(), nNodeDTO.getName(), baseNodeRSDTO.getRType()));
  68. nodeId++;
  69. if (ListUtil.isNotEmpty(baseNodeRSDTO.getENodeDTOS())) {
  70. for (BaseNodeDTO baseNodeDTO : baseNodeRSDTO.getENodeDTOS()) {
  71. GNodeDTO eNodeDTO
  72. = new GNodeDTO(cMap.get(baseNodeRSDTO.getRType()), baseNodeDTO.getName(), nodeId, random(), 28);
  73. nodeId++;
  74. node.add(eNodeDTO);
  75. links.add(new LinkDTO(nNodeDTO.getName(), eNodeDTO.getName(), ""));
  76. }
  77. }
  78. }
  79. }
  80. graphLabelDTO.setCategories(categories);
  81. graphLabelDTO.setNode(node);
  82. graphLabelDTO.setLinks(links);
  83. }
  84. return graphLabelDTO;
  85. }
  86. private String random() {
  87. //先随机产生一个下标再获取元素
  88. String random = "";
  89. String[] doc = { "circle", "diamond" };
  90. int index = (int) (Math.random() * doc.length);
  91. random = doc[index];
  92. return random;
  93. }
  94. public TreeDTO getTreeFac(KgTreeVO kgTreeVO) {
  95. TreeDTO treeDTO = new TreeDTO();
  96. if (kgTreeVO.getType() == 1 && kgTreeVO.getSubType() == 1) {
  97. treeDTO = FastJsonUtils.getJsonToBean(jsonStr, TreeDTO.class);
  98. } else {
  99. throw new CommonException(CommonErrorCode.PARAM_IS_ERROR);
  100. }
  101. return treeDTO;
  102. }
  103. private static final String jsonStr = "{\n" +
  104. " \"icdCode\": \"1\",\n" +
  105. " \"label\": null,\n" +
  106. " \"name\": \"疾病\",\n" +
  107. " \"snode\": [\n" +
  108. " {\n" +
  109. "\t\t\"icdCode\": \"A\",\n" +
  110. "\t\t\"label\": \"\",\n" +
  111. "\t\t\"name\": \"A\",\n" +
  112. "\t\t\"snode\": [\n" +
  113. "\t\t {\n" +
  114. "\t\t\t\"icdCode\": \"A1\",\n" +
  115. "\t\t\t\"label\": \"\",\n" +
  116. "\t\t\t\"name\": \"A1\",\n" +
  117. "\t\t\t\"snode\": [\n" +
  118. "\t\t\t null\n" +
  119. "\t\t\t]\n" +
  120. "\t\t },\n" +
  121. "\t\t {\n" +
  122. "\t\t\t\"icdCode\": \"A2\",\n" +
  123. "\t\t\t\"label\": \"\",\n" +
  124. "\t\t\t\"name\": \"A2\",\n" +
  125. "\t\t\t\"snode\": [\n" +
  126. "\t\t\t null\n" +
  127. "\t\t\t]\n" +
  128. "\t\t }\n" +
  129. "\t\t]\n" +
  130. "\t },\n" +
  131. "\t {\n" +
  132. "\t\t\"icdCode\": \"B\",\n" +
  133. "\t\t\"label\": \"\",\n" +
  134. "\t\t\"name\": \"B\",\n" +
  135. "\t\t\"snode\": [\n" +
  136. "\t\t null\n" +
  137. "\t\t]\n" +
  138. "\t }\n" +
  139. " ]\n" +
  140. "}";
  141. private void addNode(TreeDTO treeDTO) {
  142. }
  143. public GraDTO getTuFac(KgQueryVO kgQueryVO) {
  144. GraDTO graDTO = new GraDTO();
  145. List<Tu> tu = this.getTu(kgQueryVO);
  146. Map<String, List<Tu>> map = EntityUtil.makeEntityListMap(tu, "rel");
  147. List<Categories> categoriesList = new ArrayList<>();
  148. List<Node> nodeList = new ArrayList<>();
  149. List<Link> linksList = new ArrayList<>();
  150. // 添加第一个节点:中心节点
  151. Node node = new Node(0, kgQueryVO.getInputStr(), 0L, 50);
  152. nodeList.add(node);
  153. // 设置categories
  154. Map<String, Integer> relMap = new LinkedHashMap<>();
  155. int i = 0; // 设置category
  156. Categories c1 = new Categories("中心词");
  157. categoriesList.add(c1);
  158. relMap.put("中心词", i++);
  159. Categories c2 = new Categories("关系");
  160. categoriesList.add(c2);
  161. relMap.put("关系", i++);
  162. Long j = 1L; // 设置节点编号
  163. for (String key : map.keySet()) {
  164. Categories bean = new Categories(key);
  165. categoriesList.add(bean);
  166. relMap.put(key, i);
  167. // 添加关系节点
  168. Node nodeRel = new Node(1, "", j, 10);
  169. nodeList.add(nodeRel);
  170. // 添加中心节点与关系的关联
  171. Link linkRel = new Link(0L, j, key);
  172. linksList.add(linkRel);
  173. List<Tu> inner = map.get(key);
  174. for (Tu t : inner) {
  175. // 添加关系下的节点
  176. Node nodeLabel = new Node(i, t.getSubName(), ++j, 28);
  177. nodeList.add(nodeLabel);
  178. // 添加关系和节点的关联
  179. Link linkLabel = new Link(linkRel.getTarget(), j, "");
  180. linksList.add(linkLabel);
  181. }
  182. i++;
  183. j++;
  184. }
  185. graDTO.setCategoriesList(categoriesList);
  186. graDTO.setNodeList(nodeList);
  187. graDTO.setLinksList(linksList);
  188. return graDTO;
  189. }
  190. }