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 f8ba9ae..9e92241 100644 --- a/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java @@ -211,41 +211,37 @@ public class ModelServiceImpl implements ModelService { // 得到当前指标要求的存在关系(存在或不存在) 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) { + if (modelAtomicIndex == null && !OPERAND_TYPE_STRUCTURE.equals(ruleCondition.getIndexSource())) { log.error("原子指标不存在,跳出当前循环。原子指标ID:{}", ruleCondition.getAtomicIndexId()); return; } - ruleCondition.getOperandUnitList().forEach(operandUnit -> { - Operand left = operandUnit.getLeftOperand(); - Operand right = operandUnit.getRightOperand(); - // 定义原子指标结果共有属性 - ModelAtomicResult result = new ModelAtomicResult(); - result.setIndexId(modelIndex.getId()); - result.setCasePersonId(casePerson.getId()); - result.setCaseId(analyseCaseDTO.getCaseId()); - result.setAtomicId(modelAtomicIndex.getId()); - result.setAtomicResult(JudgeResultEnum.NOT_EXIST.getCode()); - ModelAtomicResult exist = modelAtomicResultMapper.selectByCaseIdAndAtomicId(caseId, casePerson.getId(), modelIndex.getId(), modelAtomicIndex.getId()); - if (exist != null) { - result.setId(exist.getId()); - } - switch (ruleCondition.getIndexSource()) { - case OPERAND_TYPE_MANUAL: - operandUnitResultSet.add(relationSymbol == manualIndexAnalysis(ruleCondition.getAtomicIndexId(), caseId)); - break; - case OPERAND_TYPE_DB: - operandUnitResultSet.add(relationSymbol == dbIndexAnalysis(caseId, modelAtomicIndex.getQueryLang(), result)); - break; - case OPERAND_TYPE_GRAPH: - operandUnitResultSet.add(relationSymbol == graphIndexAnalysis(casePerson.getName(), modelAtomicIndex, analyseCaseDTO, result)); - break; - case OPERAND_TYPE_STRUCTURE: - operandUnitResultSet.add(structureIndexAnalysis(operandUnit, modelAtomicIndex, atomicIndices, notePrompts, evidenceDirectories, caseEvidences, result)); - break; - default: - break; - } - }); + // 定义原子指标结果共有属性 + ModelAtomicResult result = new ModelAtomicResult(); + result.setIndexId(modelIndex.getId()); + result.setCasePersonId(casePerson.getId()); + result.setCaseId(analyseCaseDTO.getCaseId()); + result.setAtomicId(ruleCondition.getAtomicIndexId()); + result.setAtomicResult(JudgeResultEnum.NOT_EXIST.getCode()); + ModelAtomicResult exist = modelAtomicResultMapper.selectByCaseIdAndAtomicId(caseId, casePerson.getId(), modelIndex.getId(), ruleCondition.getAtomicIndexId()); + if (exist != null) { + result.setId(exist.getId()); + } + switch (ruleCondition.getIndexSource()) { + case OPERAND_TYPE_MANUAL: + operandUnitResultSet.add(relationSymbol == manualIndexAnalysis(ruleCondition.getAtomicIndexId(), caseId)); + break; + case OPERAND_TYPE_DB: + operandUnitResultSet.add(relationSymbol == dbIndexAnalysis(caseId, modelAtomicIndex.getQueryLang(), result)); + break; + case OPERAND_TYPE_GRAPH: + operandUnitResultSet.add(relationSymbol == graphIndexAnalysis(casePerson.getName(), modelAtomicIndex, analyseCaseDTO, result)); + break; + case OPERAND_TYPE_STRUCTURE: + ruleCondition.getOperandUnitList().forEach(operandUnit -> operandUnitResultSet.add(structureIndexAnalysis(operandUnit, ruleCondition.getAtomicIndexId(), atomicIndices, notePrompts, evidenceDirectories, caseEvidences, result))); + break; + default: + break; + } ruleConditionResultSet.add(CalculationUtil.calculateBooleanSet(operandUnitResultSet, ruleCondition.getLogic())); }); ruleConditionGroupResultList.add(CalculationUtil.calculateBooleanSet(ruleConditionResultSet, ruleConditionGroup.getRowLogic())); @@ -378,7 +374,7 @@ public class ModelServiceImpl implements ModelService { * 结构化查询指标分析 * * @param operandUnit 操作单元 - * @param modelAtomicIndex 原子指标 + * @param modelAtomicIndexId 原子指标ID * @param atomicIndices 原子指标集合 * @param notePrompts 提示词集合 * @param evidenceDirectories 证据目录集合 @@ -386,11 +382,16 @@ public class ModelServiceImpl implements ModelService { * @param atomicResult 原子指标结果 * @return 是否存在 */ - private boolean structureIndexAnalysis(OperandUnit operandUnit, ModelAtomicIndex modelAtomicIndex, List atomicIndices, List notePrompts, List evidenceDirectories, List caseEvidences, ModelAtomicResult atomicResult) { + private boolean structureIndexAnalysis(OperandUnit operandUnit, String modelAtomicIndexId, 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; + ModelAtomicIndex modelAtomicIndex = atomicIndices.stream().filter(atomicIndex -> atomicIndex.getId().equals(modelAtomicIndexId)).findAny().orElse(null); + if (modelAtomicIndex == null) { + log.error("原子指标不存在。原子指标ID:{}", modelAtomicIndexId); + return flag; + } List evidences = getEvidencesByPromptId(modelAtomicIndex.getPromptId(), caseEvidences, evidenceDirectories, notePrompts); ModelAtomicIndex rightModelAtomicIndex = null; if (OPERAND_TYPE_STRUCTURE.equals(right.getOperandType())) { diff --git a/src/main/java/com/supervision/utils/CalculationUtil.java b/src/main/java/com/supervision/utils/CalculationUtil.java index c9cd6a7..83b5afb 100644 --- a/src/main/java/com/supervision/utils/CalculationUtil.java +++ b/src/main/java/com/supervision/utils/CalculationUtil.java @@ -1,5 +1,7 @@ package com.supervision.utils; +import com.supervision.common.constant.IndexRuleConstants; +import lombok.extern.slf4j.Slf4j; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -9,6 +11,7 @@ import java.util.Set; import static com.supervision.common.constant.IndexRuleConstants.LOGIC_AND; import static com.supervision.common.constant.IndexRuleConstants.LOGIC_OR; +@Slf4j public class CalculationUtil { /** @@ -20,17 +23,43 @@ public class CalculationUtil { * @return 计算结果 */ public static boolean evaluateExpression(String leftOperand, String operator, String rightOperand) { - // 构建表达式 - String expression = leftOperand + " " + operator + " " + rightOperand; + String expression = switch (operator) { + case IndexRuleConstants.OPERATOR_GT -> // ">" + "#leftOperand > #rightOperand"; + case IndexRuleConstants.OPERATOR_GE -> // ">=" + "#leftOperand >= #rightOperand"; + case IndexRuleConstants.OPERATOR_LT -> // "<" + "#leftOperand < #rightOperand"; + case IndexRuleConstants.OPERATOR_LE -> // "<=" + "#leftOperand <= #rightOperand"; + case IndexRuleConstants.OPERATOR_EQ -> // "=" + "#leftOperand == #rightOperand"; + case IndexRuleConstants.OPERATOR_NE -> // "!=" + "#leftOperand != #rightOperand"; + case IndexRuleConstants.OPERATOR_EARLY -> // "早于" + "#leftOperand < #rightOperand"; + case IndexRuleConstants.OPERATOR_LATE -> // "晚于" + "#leftOperand > #rightOperand"; + case IndexRuleConstants.OPERATOR_CONTAIN -> // "包含" + "#leftOperand.contains(#rightOperand)"; + default -> throw new UnsupportedOperationException("不支持的操作符: " + operator); + }; + + // 判断操作符并构造相应的SpEL表达式 + + log.info("Expression: [{}]", expression); // 初始化SpEL解析器 ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); + context.setVariable("leftOperand", leftOperand); + context.setVariable("rightOperand", rightOperand); // 计算结果 return Boolean.TRUE.equals(parser.parseExpression(expression).getValue(context, Boolean.class)); } + /** * 计算布尔值集合的逻辑运算结果 * @@ -45,7 +74,7 @@ public class CalculationUtil { } // 判断是"且"还是"或" - String operator = ""; + String operator; if (LOGIC_AND.equals(logic)) { operator = " and "; } else if (LOGIC_OR.equals(logic)) { diff --git a/src/test/java/com/supervision/demo/CaseTest.java b/src/test/java/com/supervision/demo/CaseTest.java index c97e0ba..c895b5a 100644 --- a/src/test/java/com/supervision/demo/CaseTest.java +++ b/src/test/java/com/supervision/demo/CaseTest.java @@ -35,7 +35,7 @@ public class CaseTest { public void test() { long start = System.currentTimeMillis(); AnalyseCaseDTO analyseCaseDTO = new AnalyseCaseDTO(); - analyseCaseDTO.setCaseId("1831221360763416578"); + analyseCaseDTO.setCaseId("1852254296647204865"); modelService.analyseCaseNew(analyseCaseDTO); long end = System.currentTimeMillis(); log.info("耗时:{}ms", end - start);