From 08d1535533a5ddb721f6310c983a6a792158c21f Mon Sep 17 00:00:00 2001 From: "DESKTOP-DDTUS3E\\yaxin" Date: Fri, 1 Nov 2024 15:04:31 +0800 Subject: [PATCH] =?UTF-8?q?indexRule=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../police/dto/indexRule/Operand.java | 13 +---- .../police/dto/indexRule/RuleCondition.java | 28 ++++++++++ .../ModelIndexAtomicRelationServiceImpl.java | 35 +++++++++---- .../service/impl/ModelIndexServiceImpl.java | 52 ++++++++++++------- .../police/service/impl/ModelServiceImpl.java | 41 +++++++++------ .../java/com/supervision/demo/CaseTest.java | 19 ++++--- 6 files changed, 123 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/supervision/police/dto/indexRule/Operand.java b/src/main/java/com/supervision/police/dto/indexRule/Operand.java index 55f8805..57f431d 100644 --- a/src/main/java/com/supervision/police/dto/indexRule/Operand.java +++ b/src/main/java/com/supervision/police/dto/indexRule/Operand.java @@ -2,14 +2,11 @@ package com.supervision.police.dto.indexRule; import lombok.Data; -import java.util.HashSet; -import java.util.Set; - /** * 操作数 */ @Data -public class Operand{ +public class Operand { /** * 原子指标id @@ -33,7 +30,6 @@ public class Operand{ private String valueType; /** - * * 操作数类型 */ private String operandType; @@ -43,11 +39,4 @@ public class Operand{ * 0:合计 1:数量 2:任一 */ private String aggregateType; - - /** - * 原子指标逻辑 - * 3 存在 - * 4 不存在 - */ - private String relationalSymbol; } diff --git a/src/main/java/com/supervision/police/dto/indexRule/RuleCondition.java b/src/main/java/com/supervision/police/dto/indexRule/RuleCondition.java index 8017b3b..8d11fee 100644 --- a/src/main/java/com/supervision/police/dto/indexRule/RuleCondition.java +++ b/src/main/java/com/supervision/police/dto/indexRule/RuleCondition.java @@ -16,4 +16,32 @@ public class RuleCondition { * 条件内操作单元 */ private List operandUnitList = new ArrayList<>(); + + /** + * 指标来源 1人工定义 2数据库查询 3图谱生成 4大模型 5:结构化推理 + */ + private String indexSource; + + /** + * 原子指标id + */ + private String atomicIndexId; + + /** + * 值 + */ + private String value; + + + /** + * 操作符 > >= < <= = != 早于 晚于 包含 + */ + private String operator; + + /** + * 原子指标逻辑 + * 3 存在 + * 4 不存在 + */ + private String relationalSymbol; } diff --git a/src/main/java/com/supervision/police/service/impl/ModelIndexAtomicRelationServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ModelIndexAtomicRelationServiceImpl.java index e9d77c8..75274e4 100644 --- a/src/main/java/com/supervision/police/service/impl/ModelIndexAtomicRelationServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ModelIndexAtomicRelationServiceImpl.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.supervision.police.domain.ModelIndex; import com.supervision.police.domain.ModelIndexAtomicRelation; import com.supervision.police.dto.ModelIndexAtomicRelationDTO; + import com.supervision.police.dto.indexRule.IndexRule; import com.supervision.police.service.ModelIndexAtomicRelationService; import com.supervision.police.mapper.ModelIndexAtomicRelationMapper; import com.supervision.utils.JudgeLogicUtil; @@ -14,8 +15,10 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; /** * @author Administrator @@ -45,17 +48,29 @@ public class ModelIndexAtomicRelationServiceImpl extends ServiceImpl atomicIndexIds = JudgeLogicUtil.pickAtomicIndexIds(judgeLogic); + atomicIndexIds.forEach(atomicIndexId -> { + ModelIndexAtomicRelation modelIndexAtomicRelation = new ModelIndexAtomicRelation(); + modelIndexAtomicRelation.setModelIndexId(modelIndex.getId()); + modelIndexAtomicRelation.setAtomicIndexId(atomicIndexId); + super.save(modelIndexAtomicRelation); + }); + }else if(Objects.nonNull(indexRule)){ + Set ids = new HashSet<>(); + indexRule.getRuleConditionGroupList().forEach(ruleConditionGroup -> { + ruleConditionGroup.getRuleConditionList().forEach(ruleCondition -> { + ids.add(ruleCondition.getAtomicIndexId()); + }); + }); + ids.forEach(atomicIndexId -> { + ModelIndexAtomicRelation modelIndexAtomicRelation = new ModelIndexAtomicRelation(); + modelIndexAtomicRelation.setModelIndexId(modelIndex.getId()); + modelIndexAtomicRelation.setAtomicIndexId(atomicIndexId); + super.save(modelIndexAtomicRelation); + }); } - List atomicIndexIds = JudgeLogicUtil.pickAtomicIndexIds(judgeLogic); - atomicIndexIds.forEach(atomicIndexId -> { - ModelIndexAtomicRelation modelIndexAtomicRelation = new ModelIndexAtomicRelation(); - modelIndexAtomicRelation.setModelIndexId(modelIndex.getId()); - modelIndexAtomicRelation.setAtomicIndexId(atomicIndexId); - super.save(modelIndexAtomicRelation); - }); } @Override diff --git a/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java index 25a6aae..20f9084 100644 --- a/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ModelIndexServiceImpl.java @@ -21,6 +21,9 @@ import com.supervision.common.utils.StringUtils; import com.supervision.constant.DataStatus; import com.supervision.police.domain.*; import com.supervision.police.dto.*; +import com.supervision.police.dto.indexRule.IndexRule; +import com.supervision.police.dto.indexRule.RuleCondition; +import com.supervision.police.dto.indexRule.RuleConditionGroup; import com.supervision.police.mapper.CasePersonMapper; import com.supervision.police.mapper.ModelAtomicResultMapper; import com.supervision.police.mapper.ModelIndexMapper; @@ -86,14 +89,14 @@ public class ModelIndexServiceImpl extends ServiceImpl modelIndexAtomicDTOS = modelIndexAtomicRelationService.listByAtomicIndexName(modelIndex.getAtomicIndexName()); List indexIdList = modelIndexAtomicDTOS.stream().map(ModelIndexAtomicRelationDTO::getModelIndexId).distinct().toList(); - if (CollUtil.isEmpty(indexIdList)){ + if (CollUtil.isEmpty(indexIdList)) { return R.ok(IPages.buildDataMap(iPage)); } - wrapper.in(CollUtil.isNotEmpty(indexIdList),ModelIndex::getId, indexIdList); + wrapper.in(CollUtil.isNotEmpty(indexIdList), ModelIndex::getId, indexIdList); } wrapper.orderBy(true, false, ModelIndex::getUpdateTime); @@ -110,7 +113,19 @@ public class ModelIndexServiceImpl extends ServiceImpl ids = new ArrayList<>(); - if (StringUtils.isNotEmpty(judgeLogic)) { + IndexRule indexRule = index.getIndexRule(); + if (indexRule != null) { + Set idSet = new HashSet<>(); + List ruleConditionGroupList = indexRule.getRuleConditionGroupList(); + for (RuleConditionGroup ruleConditionGroup : ruleConditionGroupList) { + List ruleConditionList = ruleConditionGroup.getRuleConditionList(); + for (RuleCondition ruleCondition : ruleConditionList) { + idSet.add(ruleCondition.getAtomicIndexId()); + } + } + List atomicIndexList = modelAtomicIndexService.selectBatchIds(idSet.stream().toList()); + index.setAtomicIndexList(atomicIndexList); + } else if (StringUtils.isNotEmpty(judgeLogic)) { List logic = JSONUtil.toList(judgeLogic, JudgeLogic.class); for (JudgeLogic judge : logic) { List atomicData = judge.getAtomicData(); @@ -190,7 +205,7 @@ public class ModelIndexServiceImpl extends ServiceImpl - modelIndexMapAtomic.get(atomicIndex.getId()).stream().map(modelIndex ->{ - Map> map = modelAtomicResultGroup.getOrDefault(modelIndex.getId(),new HashMap<>(1)); - return new CaseAtomicIndexDTO(atomicIndex, modelIndex,CollUtil.getFirst(map.get(atomicIndex.getId()))); + modelIndexMapAtomic.get(atomicIndex.getId()).stream().map(modelIndex -> { + Map> map = modelAtomicResultGroup.getOrDefault(modelIndex.getId(), new HashMap<>(1)); + return new CaseAtomicIndexDTO(atomicIndex, modelIndex, CollUtil.getFirst(map.get(atomicIndex.getId()))); }) ).toList(); } @@ -351,18 +367,18 @@ public class ModelIndexServiceImpl extends ServiceImpl> listAtomicIndexAttributeScope(List atomicIndexIds) { + public Map> listAtomicIndexAttributeScope(List atomicIndexIds) { - if (CollUtil.isEmpty(atomicIndexIds)){ + if (CollUtil.isEmpty(atomicIndexIds)) { return Map.of(); } - Map> result = atomicIndexIds.stream().collect(Collectors.toMap(i -> i, i->List.of())); + Map> result = atomicIndexIds.stream().collect(Collectors.toMap(i -> i, i -> List.of())); List atomicIndexList = modelAtomicIndexService.listByIds(atomicIndexIds); - if (CollUtil.isEmpty(atomicIndexList)){ + if (CollUtil.isEmpty(atomicIndexList)) { return result; } List promptIds = atomicIndexList.stream().map(ModelAtomicIndex::getPromptId).filter(StrUtil::isNotEmpty).toList(); - if (CollUtil.isEmpty(promptIds)){ + if (CollUtil.isEmpty(promptIds)) { return result; } @@ -371,7 +387,7 @@ public class ModelIndexServiceImpl extends ServiceImpl> entry : result.entrySet()) { List scopeDTOList = makeScopes(entry.getKey(), atomicIndexList, notePrompts, dicMap); - if (scopeDTOList != null){ + if (scopeDTOList != null) { entry.setValue(scopeDTOList); } } @@ -380,15 +396,15 @@ public class ModelIndexServiceImpl extends ServiceImpl makeScopes(String atomicIndexId, List atomicIndexList, List notePrompts, Map dicMap) { ModelAtomicIndex modelAtomicIndex = atomicIndexList.stream().filter(i -> StrUtil.equals(atomicIndexId, i.getId())).findFirst().orElse(null); - if (null == modelAtomicIndex || StrUtil.isEmpty(modelAtomicIndex.getPromptId())){ + if (null == modelAtomicIndex || StrUtil.isEmpty(modelAtomicIndex.getPromptId())) { return null; } String properties = modelAtomicIndex.getProperties(); - if (StrUtil.isEmpty(properties)){ + if (StrUtil.isEmpty(properties)) { return null; } NotePrompt notePromptDTO = notePrompts.stream().filter(i -> StrUtil.equals(modelAtomicIndex.getPromptId(), i.getId())).findFirst().orElse(null); - if (null == notePromptDTO){ + if (null == notePromptDTO) { return null; } List extractAttributes = notePromptDTO.getExtractAttributes(); diff --git a/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java index 6d50d22..f8ba9ae 100644 --- a/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java @@ -21,6 +21,7 @@ import com.supervision.police.dto.caseScore.CaseScoreDetailBuilder; import com.supervision.police.dto.caseScore.CaseScoreDetailDTO; import com.supervision.police.dto.indexRule.IndexRule; import com.supervision.police.dto.indexRule.Operand; +import com.supervision.police.dto.indexRule.OperandUnit; import com.supervision.police.mapper.*; import com.supervision.police.mybatis.RowSqlMapper; import com.supervision.police.service.*; @@ -196,8 +197,10 @@ public class ModelServiceImpl implements ModelService { List evidenceDirectories = evidenceDirectoryService.list(new LambdaQueryWrapper().eq(EvidenceDirectory::getCaseId, caseId)); // 查出证据信息 List caseEvidences = caseEvidenceService.list(new LambdaQueryWrapper().eq(CaseEvidence::getCaseId, caseId)); + log.info("开始计算指标结果"); // 遍历指标集合,处理每个指标的判断逻辑,得出结果 modelIndices.forEach(modelIndex -> { + log.info("开始计算指标ID:{},指标名称:{}", modelIndex.getId(), modelIndex.getName()); IndexRule indexRule = modelIndex.getIndexRule(); Set ruleConditionGroupResultList = new HashSet<>(); if (indexRule != null) { @@ -205,15 +208,16 @@ public class ModelServiceImpl implements ModelService { Set ruleConditionResultSet = new HashSet<>(); ruleConditionGroup.getRuleConditionList().forEach(ruleCondition -> { Set operandUnitResultSet = new HashSet<>(); + // 得到当前指标要求的存在关系(存在或不存在) + boolean relationSymbol = IndexRuleConstants.EVALUATE_RESULT_EXIST.equals(ruleCondition.getRelationalSymbol()); + ModelAtomicIndex modelAtomicIndex = atomicIndices.stream().filter(atomicIndex -> atomicIndex.getId().equals(ruleCondition.getAtomicIndexId())).findAny().orElse(null); + if (modelAtomicIndex == null) { + log.error("原子指标不存在,跳出当前循环。原子指标ID:{}", ruleCondition.getAtomicIndexId()); + return; + } ruleCondition.getOperandUnitList().forEach(operandUnit -> { Operand left = operandUnit.getLeftOperand(); - boolean relationSymbol = IndexRuleConstants.EVALUATE_RESULT_EXIST.equals(left.getRelationalSymbol()); Operand right = operandUnit.getRightOperand(); - ModelAtomicIndex modelAtomicIndex = atomicIndices.stream().filter(atomicIndex -> atomicIndex.getId().equals(left.getAtomicIndexId())).findAny().orElse(null); - if (modelAtomicIndex == null) { - log.error("原子指标不存在,跳出当前循环。原子指标ID:{}", left.getAtomicIndexId()); - return; - } // 定义原子指标结果共有属性 ModelAtomicResult result = new ModelAtomicResult(); result.setIndexId(modelIndex.getId()); @@ -225,9 +229,9 @@ public class ModelServiceImpl implements ModelService { if (exist != null) { result.setId(exist.getId()); } - switch (left.getOperandType()) { + switch (ruleCondition.getIndexSource()) { case OPERAND_TYPE_MANUAL: - operandUnitResultSet.add(relationSymbol == manualIndexAnalysis(left.getAtomicIndexId(), caseId)); + operandUnitResultSet.add(relationSymbol == manualIndexAnalysis(ruleCondition.getAtomicIndexId(), caseId)); break; case OPERAND_TYPE_DB: operandUnitResultSet.add(relationSymbol == dbIndexAnalysis(caseId, modelAtomicIndex.getQueryLang(), result)); @@ -236,7 +240,7 @@ public class ModelServiceImpl implements ModelService { operandUnitResultSet.add(relationSymbol == graphIndexAnalysis(casePerson.getName(), modelAtomicIndex, analyseCaseDTO, result)); break; case OPERAND_TYPE_STRUCTURE: - operandUnitResultSet.add(structureIndexAnalysis(left, right, operandUnit.getOperator(), modelAtomicIndex, atomicIndices, notePrompts, evidenceDirectories, caseEvidences, result)); + operandUnitResultSet.add(structureIndexAnalysis(operandUnit, modelAtomicIndex, atomicIndices, notePrompts, evidenceDirectories, caseEvidences, result)); break; default: break; @@ -341,11 +345,12 @@ public class ModelServiceImpl implements ModelService { params.put("lawActor", casePersonName); // 案号 params.put("caseId", analyseCaseDTO.getCaseId()); - Result run = null; + Result run; try { run = session.run(modelAtomicIndex.getQueryLang(), params); } catch (Exception e) { log.error("图数据库查询出现错误,查询语句{},参数{}", modelAtomicIndex.getQueryLang(), JSONUtil.toJsonStr(params), e); + return flag; } List res = Neo4jUtils.getResultDTOList(run); if (!res.isEmpty()) { @@ -372,8 +377,7 @@ public class ModelServiceImpl implements ModelService { /** * 结构化查询指标分析 * - * @param left 左操作数 - * @param right 右操作数 + * @param operandUnit 操作单元 * @param modelAtomicIndex 原子指标 * @param atomicIndices 原子指标集合 * @param notePrompts 提示词集合 @@ -382,7 +386,10 @@ public class ModelServiceImpl implements ModelService { * @param atomicResult 原子指标结果 * @return 是否存在 */ - private boolean structureIndexAnalysis(Operand left, Operand right, String operator, ModelAtomicIndex modelAtomicIndex, List atomicIndices, List notePrompts, List evidenceDirectories, List caseEvidences, ModelAtomicResult atomicResult) { + private boolean structureIndexAnalysis(OperandUnit operandUnit, ModelAtomicIndex modelAtomicIndex, List atomicIndices, List notePrompts, List evidenceDirectories, List caseEvidences, ModelAtomicResult atomicResult) { + Operand left = operandUnit.getLeftOperand(); + String operator = operandUnit.getOperator(); + Operand right = operandUnit.getRightOperand(); boolean flag = false; List evidences = getEvidencesByPromptId(modelAtomicIndex.getPromptId(), caseEvidences, evidenceDirectories, notePrompts); ModelAtomicIndex rightModelAtomicIndex = null; @@ -400,14 +407,14 @@ public class ModelServiceImpl implements ModelService { case AGGREGATE_TYPE_SUM: if (OPERAND_TYPE_STRUCTURE.equals(right.getOperandType())) { List rightEvidences = getEvidencesByPromptId(rightModelAtomicIndex.getPromptId(), caseEvidences, evidenceDirectories, notePrompts); - if (CalculationUtil.evaluateExpression(String.valueOf(getSumFromEvidences(left, evidences)), operator, String.valueOf(getSumFromEvidences(right, rightEvidences)))) { + if (CalculationUtil.evaluateExpression(String.valueOf(getSumFromEvidences(left.getPropertyKey(), evidences)), operator, String.valueOf(getSumFromEvidences(right.getPropertyKey(), rightEvidences)))) { atomicResult.setEvidenceId(evidences.stream().map(CaseEvidence::getId).collect(Collectors.joining(","))); atomicResult.setAtomicResult(JudgeResultEnum.EXIST.getCode()); modelAtomicResultService.saveOrUpdate(atomicResult); return true; } } else if (OPERAND_TYPE_VALUE.equals(right.getOperandType())) { - if (CalculationUtil.evaluateExpression(String.valueOf(getSumFromEvidences(left, evidences)), operator, right.getValue())) { + if (CalculationUtil.evaluateExpression(String.valueOf(getSumFromEvidences(left.getPropertyKey(), evidences)), operator, right.getValue())) { atomicResult.setEvidenceId(evidences.stream().map(CaseEvidence::getId).collect(Collectors.joining(","))); atomicResult.setAtomicResult(JudgeResultEnum.EXIST.getCode()); modelAtomicResultService.saveOrUpdate(atomicResult); @@ -464,14 +471,14 @@ public class ModelServiceImpl implements ModelService { return flag; } - private Double getSumFromEvidences(Operand operand, List evidences) { + private Double getSumFromEvidences(String propertyKey, List evidences) { AtomicReference sum = new AtomicReference<>(0.0); if (evidences != null && !evidences.isEmpty()) { evidences.forEach(e -> { List properties = e.getProperty(); if (properties != null && !properties.isEmpty()) { properties.forEach(p -> { - if (p.getAttrName().equals(operand.getPropertyKey())) { + if (p.getAttrName().equals(propertyKey)) { if (StringUtils.isNotEmpty(p.getAttrValue())) { sum.updateAndGet(v -> v + NumberUtil.parseDouble(p.getAttrValue())); } diff --git a/src/test/java/com/supervision/demo/CaseTest.java b/src/test/java/com/supervision/demo/CaseTest.java index 4f5d68f..c97e0ba 100644 --- a/src/test/java/com/supervision/demo/CaseTest.java +++ b/src/test/java/com/supervision/demo/CaseTest.java @@ -1,11 +1,14 @@ package com.supervision.demo; import cn.hutool.json.JSONUtil; +import com.supervision.common.constant.IndexRuleConstants; import com.supervision.police.domain.ModelAtomicIndex; import com.supervision.police.domain.ModelIndex; import com.supervision.police.dto.AnalyseCaseDTO; import com.supervision.police.dto.JudgeLogic; -import com.supervision.police.dto.indexRule.*; +import com.supervision.police.dto.indexRule.IndexRule; +import com.supervision.police.dto.indexRule.RuleCondition; +import com.supervision.police.dto.indexRule.RuleConditionGroup; import com.supervision.police.service.ModelAtomicIndexService; import com.supervision.police.service.ModelIndexService; import com.supervision.police.service.ModelService; @@ -58,15 +61,15 @@ public class CaseTest { } judgeLogic.getAtomicData().forEach(atomicData -> { RuleCondition ruleCondition = new RuleCondition(); - OperandUnit operandUnit = new OperandUnit(); - Operand left = new Operand(); - left.setAtomicIndexId(atomicData.getAtomicIndex()); - left.setRelationalSymbol(atomicData.getRelationalSymbol()); + ruleCondition.setAtomicIndexId(atomicData.getAtomicIndex()); + ruleCondition.setRelationalSymbol(atomicData.getRelationalSymbol()); modelAtomicIndices.stream().filter(modelAtomicIndex -> modelAtomicIndex.getId().equals(atomicData.getAtomicIndex())).findFirst().ifPresent(modelAtomicIndex -> { - left.setOperandType(modelAtomicIndex.getIndexSource()); + if (modelAtomicIndex.getIndexSource().equals(IndexRuleConstants.OPERAND_TYPE_GRAPH)) { + ruleCondition.setOperator(">"); + ruleCondition.setValue("0"); + } + ruleCondition.setIndexSource(modelAtomicIndex.getIndexSource()); }); - operandUnit.setLeftOperand(left); - ruleCondition.setOperandUnitList(List.of(operandUnit)); ruleConditions.add(ruleCondition); }); ruleConditionGroup.setRuleConditionList(ruleConditions);