123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package com.diagbot.facade;
- import com.diagbot.dto.BaseNodeDTO;
- import com.diagbot.dto.BaseNodeRSDTO;
- import com.diagbot.dto.CategorieDTO;
- import com.diagbot.dto.Categories;
- import com.diagbot.dto.GNodeDTO;
- import com.diagbot.dto.GraDTO;
- import com.diagbot.dto.GraphDTO;
- import com.diagbot.dto.GraphLabelDTO;
- import com.diagbot.dto.Link;
- import com.diagbot.dto.LinkDTO;
- import com.diagbot.dto.Node;
- import com.diagbot.dto.TreeDTO;
- import com.diagbot.dto.Tu;
- import com.diagbot.exception.CommonErrorCode;
- import com.diagbot.exception.CommonException;
- import com.diagbot.service.impl.KgServiceImpl;
- import com.diagbot.util.EntityUtil;
- import com.diagbot.util.FastJsonUtils;
- import com.diagbot.util.ListUtil;
- import com.diagbot.vo.KgQueryVO;
- import com.diagbot.vo.KgTreeVO;
- import org.springframework.stereotype.Component;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * @Description: 用户日志业务层
- * @author: gaodm
- * @time: 2018/8/6 9:11
- */
- @Component
- public class KgFacade extends KgServiceImpl {
- public GraphLabelDTO getGraphFac(KgQueryVO kgQueryVO) {
- GraphLabelDTO graphLabelDTO = new GraphLabelDTO();
- List<CategorieDTO> categories = new ArrayList<>();
- List<GNodeDTO> node = new ArrayList<>();
- List<LinkDTO> links = new ArrayList<>();
- List<GraphDTO> res = this.getGraph(kgQueryVO);
- if (ListUtil.isEmpty(res)) {
- throw new CommonException(CommonErrorCode.NOT_EXISTS);
- } else {
- Integer nodeId = 0;
- categories.add(new CategorieDTO("中心词"));
- categories.add(new CategorieDTO("关系"));
- Map<String, Integer> cMap = new HashMap<>();
- cMap.put("中心词", 0);
- cMap.put("关系", 1);
- GraphDTO graphDTO = res.get(0);
- GNodeDTO gNodeDTO
- = new GNodeDTO(0, graphDTO.getName(), nodeId, "circle", 50);
- nodeId++;
- node.add(gNodeDTO);
- if (ListUtil.isNotEmpty(graphDTO.getENodeRSDTOS())) {
- Integer rsId = 2;
- for (BaseNodeRSDTO baseNodeRSDTO : graphDTO.getENodeRSDTOS()) {
- if (null == cMap.get(baseNodeRSDTO.getRType())) {
- cMap.put(baseNodeRSDTO.getRType(), rsId);
- categories.add(new CategorieDTO(baseNodeRSDTO.getRType()));
- rsId++;
- }
- GNodeDTO nNodeDTO
- = new GNodeDTO(1, "", nodeId, "diamond", 10);
- node.add(nNodeDTO);
- links.add(new LinkDTO(gNodeDTO.getName(), nNodeDTO.getName(), baseNodeRSDTO.getRType()));
- nodeId++;
- if (ListUtil.isNotEmpty(baseNodeRSDTO.getENodeDTOS())) {
- for (BaseNodeDTO baseNodeDTO : baseNodeRSDTO.getENodeDTOS()) {
- GNodeDTO eNodeDTO
- = new GNodeDTO(cMap.get(baseNodeRSDTO.getRType()), baseNodeDTO.getName(), nodeId, random(), 28);
- nodeId++;
- node.add(eNodeDTO);
- links.add(new LinkDTO(nNodeDTO.getName(), eNodeDTO.getName(), ""));
- }
- }
- }
- }
- graphLabelDTO.setCategories(categories);
- graphLabelDTO.setNode(node);
- graphLabelDTO.setLinks(links);
- }
- return graphLabelDTO;
- }
- private String random() {
- //先随机产生一个下标再获取元素
- String random = "";
- String[] doc = { "circle", "diamond" };
- int index = (int) (Math.random() * doc.length);
- random = doc[index];
- return random;
- }
- public TreeDTO getTreeFac(KgTreeVO kgTreeVO) {
- TreeDTO treeDTO = new TreeDTO();
- if (kgTreeVO.getType() == 1 && kgTreeVO.getSubType() == 1) {
- treeDTO = FastJsonUtils.getJsonToBean(jsonStr, TreeDTO.class);
- } else {
- throw new CommonException(CommonErrorCode.PARAM_IS_ERROR);
- }
- return treeDTO;
- }
- private static final String jsonStr = "{\n" +
- " \"icdCode\": \"1\",\n" +
- " \"label\": null,\n" +
- " \"name\": \"疾病\",\n" +
- " \"snode\": [\n" +
- " {\n" +
- "\t\t\"icdCode\": \"A\",\n" +
- "\t\t\"label\": \"\",\n" +
- "\t\t\"name\": \"A\",\n" +
- "\t\t\"snode\": [\n" +
- "\t\t {\n" +
- "\t\t\t\"icdCode\": \"A1\",\n" +
- "\t\t\t\"label\": \"\",\n" +
- "\t\t\t\"name\": \"A1\",\n" +
- "\t\t\t\"snode\": [\n" +
- "\t\t\t null\n" +
- "\t\t\t]\n" +
- "\t\t },\n" +
- "\t\t {\n" +
- "\t\t\t\"icdCode\": \"A2\",\n" +
- "\t\t\t\"label\": \"\",\n" +
- "\t\t\t\"name\": \"A2\",\n" +
- "\t\t\t\"snode\": [\n" +
- "\t\t\t null\n" +
- "\t\t\t]\n" +
- "\t\t }\n" +
- "\t\t]\n" +
- "\t },\n" +
- "\t {\n" +
- "\t\t\"icdCode\": \"B\",\n" +
- "\t\t\"label\": \"\",\n" +
- "\t\t\"name\": \"B\",\n" +
- "\t\t\"snode\": [\n" +
- "\t\t null\n" +
- "\t\t]\n" +
- "\t }\n" +
- " ]\n" +
- "}";
- private void addNode(TreeDTO treeDTO) {
- }
- public GraDTO getTuFac(KgQueryVO kgQueryVO) {
- GraDTO graDTO = new GraDTO();
- List<Tu> tu = this.getTu(kgQueryVO);
- Map<String, List<Tu>> map = EntityUtil.makeEntityListMap(tu, "rel");
- List<Categories> categoriesList = new ArrayList<>();
- List<Node> nodeList = new ArrayList<>();
- List<Link> linksList = new ArrayList<>();
- // 添加第一个节点:中心节点
- Node node = new Node(0, kgQueryVO.getInputStr(), 0L, 50);
- nodeList.add(node);
- // 设置categories
- Map<String, Integer> relMap = new LinkedHashMap<>();
- int i = 0; // 设置category
- Categories c1 = new Categories("中心词");
- categoriesList.add(c1);
- relMap.put("中心词", i++);
- Categories c2 = new Categories("关系");
- categoriesList.add(c2);
- relMap.put("关系", i++);
- Long j = 1L; // 设置节点编号
- for (String key : map.keySet()) {
- Categories bean = new Categories(key);
- categoriesList.add(bean);
- relMap.put(key, i);
- // 添加关系节点
- Node nodeRel = new Node(1, "", j, 10);
- nodeList.add(nodeRel);
- // 添加中心节点与关系的关联
- Link linkRel = new Link(0L, j, key);
- linksList.add(linkRel);
- List<Tu> inner = map.get(key);
- for (Tu t : inner) {
- // 添加关系下的节点
- Node nodeLabel = new Node(i, t.getSubName(), ++j, 28);
- nodeList.add(nodeLabel);
- // 添加关系和节点的关联
- Link linkLabel = new Link(linkRel.getTarget(), j, "");
- linksList.add(linkLabel);
- }
- i++;
- j++;
- }
- graDTO.setCategoriesList(categoriesList);
- graDTO.setNodeList(nodeList);
- graDTO.setLinksList(linksList);
- return graDTO;
- }
- }
|