Merge branch 'master' into dev

# Conflicts:
#	src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java
jinan_dev
yaxin 5 months ago
commit 9f216fa844

@ -272,6 +272,7 @@ public class Neo4jServiceImpl implements Neo4jService {
Map<String, Object> map = new HashMap<>();
List<WebRelDTO> list = new ArrayList<>();
List<Map<String, String>> nodes = new ArrayList<>();
List<Map<String, String>> labelColorMap = graphReqVO.getNodeLabelColorMap();
try {
Session session = driver.session();
StringBuilder relQuery = new StringBuilder("MATCH (n)-[rel]->(m) " +
@ -293,11 +294,17 @@ public class Neo4jServiceImpl implements Neo4jService {
params.put("relTypes", graphReqVO.getRelTypes());
relQuery.append("AND ANY(type IN $relTypes WHERE type = type(rel)) ");
}
relQuery.append("RETURN id(rel) as id, n.name as source, id(n) as sourceId, n.name as sourceName, type(rel) as relName, m.name as target, id(m) as targetId, m.name as targetName");
relQuery.append("RETURN id(rel) as id, n.name as source, id(n) as sourceId, n.name as sourceName, type(rel) as relName, m.name as target, id(m) as targetId, m.name as targetName, labels(n) as sourceLabels, labels(m) as targetLabels");
log.info("relQuery:{}", relQuery);
Result run = session.run(relQuery.toString(), params);
while (run.hasNext()) {
Record record = run.next();
List<String> sourceLabels = record.get("sourceLabels").asList(Value::asString);
List<String> targetLabels = record.get("targetLabels").asList(Value::asString);
if (sourceLabels == null || sourceLabels.isEmpty() || targetLabels == null || targetLabels.isEmpty()) {
log.info("节点标签为空,跳过");
continue;
}
// 组织边
long sourceId = record.get("sourceId").asLong();
String relName = record.get("relName").asString();
@ -307,12 +314,10 @@ public class Neo4jServiceImpl implements Neo4jService {
// 组织节点
Map<String, String> sourceNodeMap = new HashMap<>();
sourceNodeMap.put("name", record.get("sourceName").asString());
sourceNodeMap.put("id", String.valueOf(sourceId));
nodes.add(sourceNodeMap);
setNodeIdAndColor(nodes, labelColorMap, sourceLabels, sourceId, sourceNodeMap, graphReqVO);
Map<String, String> targetNodeMap = new HashMap<>();
targetNodeMap.put("name", record.get("targetName").asString());
targetNodeMap.put("id", String.valueOf(targetId));
nodes.add(targetNodeMap);
setNodeIdAndColor(nodes, labelColorMap, targetLabels, targetId, targetNodeMap, graphReqVO);
}
} catch (Exception e) {
log.error("查询失败", e);
@ -328,20 +333,50 @@ public class Neo4jServiceImpl implements Neo4jService {
// 节点和关系合并
Pair<List<WebRelDTO>, List<Map<String, String>>> pair = mergeRecord(distinctNodes, list);
// 将返回结果List<Map<String, String>>转为Map<String, Object>并将其中key为color的键值对包一层itemStyle将键值对放在里面
List<Map<String, Object>> nodesList = new ArrayList<>();
for (Map<String, String> node : pair.getValue()) {
Map<String, String> itemStyle = new HashMap<>();
itemStyle.put("color", node.get("color"));
Map<String, Object> nodeMap = new HashMap<>();
nodeMap.put("name", node.get("name"));
nodeMap.put("id", node.get("id"));
nodeMap.put("itemStyle", itemStyle);
nodesList.add(nodeMap);
}
map.put("list", pair.getKey());
map.put("nodes", pair.getValue());
map.put("nodes", nodesList);
return R.ok(map);
}
private void setNodeIdAndColor(List<Map<String, String>> nodes, List<Map<String, String>> labelColorMap, List<String> labels, long id, Map<String, String> nodeMap, GraphReqVO graphReqVO) {
nodeMap.put("id", String.valueOf(id));
for (Map<String, String> labelColor : labelColorMap) {
if (labels.contains(labelColor.get("name"))) {
if (graphReqVO.getNodeLabels().isEmpty() && graphReqVO.getRelTypes().isEmpty() && StringUtils.isEmpty(graphReqVO.getQueryStr())) {
nodeMap.put("color", labelColor.get("lightColor"));
} else {
nodeMap.put("color", labelColor.get("color"));
}
break;
}
}
if (!nodeMap.containsKey("color")) {
log.error("节点【{}】没有颜色,随机生成", nodeMap.get("name"));
nodeMap.put("color", Neo4jUtils.getRandomColorPair()[0]);
}
nodes.add(nodeMap);
}
public Pair<List<WebRelDTO>, List<Map<String, String>>> mergeRecord(List<Map<String, String>> nodes, List<WebRelDTO> relDTOS) {
Map<String, NodeMapRecord> nodeRecordMap = electNodeRecord(nodes);
return Pair.of(mergerWebRel(relDTOS, nodeRecordMap), mergeNode(nodes, nodeRecordMap));
return Pair.of(mergerWebRel(relDTOS, nodeRecordMap), mergeNode(nodes, nodeRecordMap));
}
@Override
public R<?> getNodeAndRelationListByCaseId(String picType, String caseId) {
Map<String, Set<String>> map = new HashMap<>();
Map<String, Object> map = new HashMap<>();
Set<String> nodeLabels = new HashSet<>();
Set<String> relTypes = new HashSet<>();
try (Session session = driver.session()) {
@ -368,7 +403,18 @@ public class Neo4jServiceImpl implements Neo4jService {
}
log.info("查询到的节点类型{}个:{}", nodeLabels.size(), nodeLabels);
log.info("查询到的关系类型{}个:{}", relTypes.size(), relTypes);
map.put("nodeLabels", nodeLabels);
// 将nodeLabels重新组装成List<Map<String, Object>>nodeLabels中的值作为key:name的值另外创建一个key:itemStyle值是Map<String, String>一个键值对是color+随机颜色另一个是lightColor+随机颜色
List<Map<String, Object>> nodeLabelsList = nodeLabels.stream().map(label -> {
Map<String, Object> nodeLabelMap = new HashMap<>();
Map<String, String> itemStyle = new HashMap<>();
String[] colors = Neo4jUtils.getRandomColorPair();
itemStyle.put("color", colors[0]);
itemStyle.put("lightColor", colors[1]);
nodeLabelMap.put("name", label);
nodeLabelMap.put("itemStyle", itemStyle);
return nodeLabelMap;
}).toList();
map.put("nodeLabels", nodeLabelsList);
map.put("relTypes", relTypes);
return R.ok(map);
}
@ -415,6 +461,7 @@ public class Neo4jServiceImpl implements Neo4jService {
return nodes.stream().map(map -> {
Map<String, String> nodeMap = new HashMap<>();
nodeMap.put("name", map.get("name"));
nodeMap.put("color", map.get("color"));
NodeMapRecord nodeMapRecord = nodeRecordMap.get(map.get("name"));
if (null == nodeMapRecord) {
log.warn("mergeNode:节点信息异常nodeRecordMap中不存在节点名称为{}的NodeMapRecord", map.get("name"));
@ -583,7 +630,7 @@ public class Neo4jServiceImpl implements Neo4jService {
}
@Override
public List<Record> executeCypher(String cypher,Map<String, Object> parameters) {
public List<Record> executeCypher(String cypher, Map<String, Object> parameters) {
return this.driver.session().run(cypher, parameters).list();
}
}

@ -14,11 +14,26 @@ import org.neo4j.driver.internal.value.StringValue;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
public class Neo4jUtils {
public static final String NODE_RETURN = " RETURN id(n) as id, n.name as name, n.nodeType as nodeType, n.recordId as recordId, n.caseId as caseId, n.picType as picType";
public static final String REL_RETURN = " RETURN id(rel) as id, a.name as source, id(a) as sourceId, type(rel) as name, b.name as target, id(b) as targetId";
private static final String[][] COLORS = {
{"#0083C7", "#7FC0E2"},
{"#09C6C0", "#83E2DF"},
{"#2BCCFF", "#94E5FF"},
{"#3763FF", "#9BB1FF"},
{"#4274AF", "#A0B9D6"},
{"#469C3D", "#A2CD9D"},
{"#55CBAD", "#A9E4D5"},
{"#75A0C8", "#B9CFE3"},
{"#8D8BFF", "#C5C4FF"},
{"#C76BFF", "#E2B5FF"},
{"#D2955D", "#E8C9AD"},
{"#FF8F51", "#FFC6A7"}
};
public static CaseNode getOneNode(Result run) {
CaseNode node = null;
@ -117,4 +132,15 @@ public class Neo4jUtils {
}
/**
*
* @return [, ]
*/
public static String[] getRandomColorPair() {
Random random = new Random();
// 随机选择一个颜色组
int index = random.nextInt(COLORS.length);
return COLORS[index];
}
}

@ -21,17 +21,11 @@ public class ModelController {
private final ModelService modelService;
@Value("${fu-hsi-config.case-analysis-method}")
private String caseAnalysisMethod;
@PostMapping("/analyseCase")
@Operation(summary = "分析指标")
public R<?> analyseCase(@RequestBody AnalyseCaseDTO analyseCaseDTO) {
if (METHOD_NEW.equals(caseAnalysisMethod)) {
return modelService.analyseCaseNew(analyseCaseDTO);
} else {
return modelService.analyseCase(analyseCaseDTO);
}
return modelService.analyseCase(analyseCaseDTO);
}
@GetMapping("/caseScoreDetail")

@ -11,7 +11,6 @@ public interface ModelService {
R<?> analyseCase(AnalyseCaseDTO analyseCaseDTO);
R<?> analyseCaseNew(AnalyseCaseDTO analyseCaseDTO);
/**
*

@ -104,84 +104,6 @@ public class ModelServiceImpl implements ModelService {
@Override
@Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
public R<?> analyseCase(AnalyseCaseDTO analyseCaseDTO) {
ModelCase modelCase = modelCaseMapper.selectById(analyseCaseDTO.getCaseId());
// 获取案件行为人ID
CasePerson casePerson = casePersonMapper.selectOne(new LambdaQueryWrapper<CasePerson>()
.eq(CasePerson::getCaseId, analyseCaseDTO.getCaseId())
.eq(CasePerson::getCaseActorFlag, 1)
.eq(CasePerson::getRoleCode, "1")
.eq(StrUtil.isNotEmpty(analyseCaseDTO.getLawActorName()), CasePerson::getName, analyseCaseDTO.getLawActorName()));
if (ObjectUtil.isEmpty(casePerson)) {
throw new RuntimeException("未找到的行为人" + analyseCaseDTO.getLawActorName());
}
// 首先将原先的值进行赋值,设置到pre_result
modelIndexResultMapper.updatePreResult(analyseCaseDTO.getCaseId());
//原子指标
List<ModelAtomicIndex> atomicIndices = modelAtomicIndexMapper.selectByCaseType(modelCase.getCaseType());
// 存放原子指标的结果,key:原子指标ID,value:(key大指标ID,value:结果)
Map<String, Map<String, String>> atomicResultMap = new HashMap<>();
for (ModelAtomicIndex atomicIndex : atomicIndices) {
//原子指标结果
ModelAtomicResult result = new ModelAtomicResult();
result.setCasePersonId(casePerson.getId());
result.setCaseId(analyseCaseDTO.getCaseId());
result.setAtomicId(atomicIndex.getId());
//查询语句
String ql = atomicIndex.getQueryLang();
//原子指标结果表
try {
//查询图谱 index_source: 1人工定义 2数据库查询 3图谱生成 4大模型
if ("1".equals(atomicIndex.getIndexSource())) {
// list
manuallyDefinedCase(analyseCaseDTO, result, atomicIndex);
} else if ("2".equals(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) {
//
analyseDataBaseCase(analyseCaseDTO, result, ql, casePerson.getName());
} else if ("3".equals(atomicIndex.getIndexSource()) && StringUtils.isNotEmpty(ql)) {
// 使用知识图谱进行计算
analyseGraphCase(analyseCaseDTO, result, ql, casePerson.getName());
} else if ("4".equals(atomicIndex.getIndexSource())) {
//
} else if ("5".equals(atomicIndex.getIndexSource())) {
// todo: 结构化推理
//analyseStructuredInference(analyseCaseDTO, result);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
// 根据原子指标ID查model_index_atomic_relation表所有的指标ID
List<ModelIndexAtomicRelation> relationList = modelIndexAtomicRelationService.lambdaQuery().eq(ModelIndexAtomicRelation::getAtomicIndexId, atomicIndex.getId()).list();
for (ModelIndexAtomicRelation relation : relationList) {
// 保存或更新原子指标结果表
ModelAtomicResult exist = modelAtomicResultMapper.selectByCaseIdAndAtomicId(analyseCaseDTO.getCaseId(), casePerson.getId(), relation.getModelIndexId(), atomicIndex.getId());
result.setIndexId(relation.getModelIndexId());
if (exist == null) {
result.setId(null);
modelAtomicResultMapper.insert(result);
} else {
result.setId(exist.getId());
modelAtomicResultMapper.updateById(result);
}
Map<String, String> indexMap = atomicResultMap.computeIfAbsent(
result.getAtomicId(),
k -> new HashMap<>() // 如果不存在则创建一个新的 HashMap
);
indexMap.put(relation.getModelIndexId(), result.getAtomicResult());
}
}
// 最终计算得分
calculateFinalScore(analyseCaseDTO, modelCase, atomicResultMap);
caseStatusManageService.whenAnalyseCaseSuccess(analyseCaseDTO.getCaseId(), modelCase.getTotalScore());
// 计算完成之后,把所有的笔录上传到模型
difyApiUtil.syncCaseFileToDifyKnowledgeBase(modelCase, modelCaseService.listCaseFileIds(analyseCaseDTO.getCaseId()));
return R.ok();
}
@Override
public R<?> analyseCaseNew(AnalyseCaseDTO analyseCaseDTO) {
log.info("数据准备阶段");
String caseId = analyseCaseDTO.getCaseId();
Map<String, Integer> typeScoreMap = new HashMap<>();

@ -2,7 +2,9 @@ package com.supervision.police.vo;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
@ -24,9 +26,11 @@ public class GraphReqVO {
/**
* (
*/
private List<String> nodeLabels;
private List<String> nodeLabels = new ArrayList<>();
/**
*
*/
private List<String> relTypes;
private List<String> relTypes = new ArrayList<>();
private List<Map<String, String>> nodeLabelColorMap = new ArrayList<>();
}

@ -36,7 +36,7 @@ public class CaseTest {
long start = System.currentTimeMillis();
AnalyseCaseDTO analyseCaseDTO = new AnalyseCaseDTO();
analyseCaseDTO.setCaseId("1852254296647204865");
modelService.analyseCaseNew(analyseCaseDTO);
modelService.analyseCase(analyseCaseDTO);
long end = System.currentTimeMillis();
log.info("耗时:{}ms", end - start);
}

Loading…
Cancel
Save