indexRule数据结构调整

topo_dev
DESKTOP-DDTUS3E\yaxin 6 months ago
parent f7da9c67c0
commit 08d1535533

@ -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;
}

@ -16,4 +16,32 @@ public class RuleCondition {
*
*/
private List<OperandUnit> 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;
}

@ -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<ModelIndexA
return;
}
String judgeLogic = modelIndex.getJudgeLogic();
if (StrUtil.isEmpty(judgeLogic)){
log.info("saveByModelIndex: modelIndex judgeLogic 为空,不保存关联关系....modelIndex:{}", JSONUtil.toJsonStr(modelIndex));
return;
IndexRule indexRule = modelIndex.getIndexRule();
if (StrUtil.isNotEmpty(judgeLogic)){
List<String> 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<String> 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<String> atomicIndexIds = JudgeLogicUtil.pickAtomicIndexIds(judgeLogic);
atomicIndexIds.forEach(atomicIndexId -> {
ModelIndexAtomicRelation modelIndexAtomicRelation = new ModelIndexAtomicRelation();
modelIndexAtomicRelation.setModelIndexId(modelIndex.getId());
modelIndexAtomicRelation.setAtomicIndexId(atomicIndexId);
super.save(modelIndexAtomicRelation);
});
}
@Override

@ -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<ModelIndexMapper, ModelIn
.eq(ModelIndex::getDataStatus, "1");
if (StrUtil.isNotEmpty(modelIndex.getAtomicIndexName())){
if (StrUtil.isNotEmpty(modelIndex.getAtomicIndexName())) {
// 如果查询条件中有原子指标名称则根据原子指标名称过滤关联的指标id
List<ModelIndexAtomicRelationDTO> modelIndexAtomicDTOS = modelIndexAtomicRelationService.listByAtomicIndexName(modelIndex.getAtomicIndexName());
List<String> 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<ModelIndexMapper, ModelIn
//原子指标
String judgeLogic = index.getJudgeLogic();
List<String> ids = new ArrayList<>();
if (StringUtils.isNotEmpty(judgeLogic)) {
IndexRule indexRule = index.getIndexRule();
if (indexRule != null) {
Set<String> idSet = new HashSet<>();
List<RuleConditionGroup> ruleConditionGroupList = indexRule.getRuleConditionGroupList();
for (RuleConditionGroup ruleConditionGroup : ruleConditionGroupList) {
List<RuleCondition> ruleConditionList = ruleConditionGroup.getRuleConditionList();
for (RuleCondition ruleCondition : ruleConditionList) {
idSet.add(ruleCondition.getAtomicIndexId());
}
}
List<ModelAtomicIndex> atomicIndexList = modelAtomicIndexService.selectBatchIds(idSet.stream().toList());
index.setAtomicIndexList(atomicIndexList);
} else if (StringUtils.isNotEmpty(judgeLogic)) {
List<JudgeLogic> logic = JSONUtil.toList(judgeLogic, JudgeLogic.class);
for (JudgeLogic judge : logic) {
List<AtomicData> atomicData = judge.getAtomicData();
@ -190,7 +205,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
ModelAtomicIndex existIndex = modelAtomicIndexService.lambdaQuery().eq(ModelAtomicIndex::getName, modelAtomicIndex.getName())
.eq(ModelAtomicIndex::getDataStatus, DataStatus.AVAILABLE.getCode()).last("limit 1").one();
if (ObjectUtil.isNotEmpty(existIndex) && !StringUtils.equals(modelAtomicIndex.getId(), existIndex.getId())){
if (ObjectUtil.isNotEmpty(existIndex) && !StringUtils.equals(modelAtomicIndex.getId(), existIndex.getId())) {
throw new RuntimeException("已存在相同名称的原子指标");
}
@ -200,6 +215,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
/**
*
*
* @param modelAtomicIndex
*/
private void saveAtomicIndexPreDo(ModelAtomicIndex modelAtomicIndex) {
@ -208,7 +224,7 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
// 如果查询类型为数据查询,则校验查询语句
Assert.notEmpty(modelAtomicIndex.getQueryLang(), "查询语言不能为空");
Assert.isTrue(checkSql(modelAtomicIndex.getQueryLang()), "查询语句不合法");
}else if (StringUtils.equals(IndexRuleConstants.OPERAND_TYPE_LLM, modelAtomicIndex.getIndexSource())){
} else if (StringUtils.equals(IndexRuleConstants.OPERAND_TYPE_LLM, modelAtomicIndex.getIndexSource())) {
Assert.notEmpty(modelAtomicIndex.getPromptId(), "promptId不能为空");
NotePrompt notePrompt = notePromptService.getBaseMapper().selectById(modelAtomicIndex.getPromptId());
Assert.notNull(notePrompt, "提示词信息不存在");
@ -281,9 +297,9 @@ public class ModelIndexServiceImpl extends ServiceImpl<ModelIndexMapper, ModelIn
// 以原子指标为基准,组装数据
return modelAtomicIndexList.stream().flatMap(atomicIndex ->
modelIndexMapAtomic.get(atomicIndex.getId()).stream().map(modelIndex ->{
Map<String, List<ModelAtomicResult>> 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<String, List<ModelAtomicResult>> 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<ModelIndexMapper, ModelIn
}
@Override
public Map<String,List<ValueCalculateScopeDTO>> listAtomicIndexAttributeScope(List<String> atomicIndexIds) {
public Map<String, List<ValueCalculateScopeDTO>> listAtomicIndexAttributeScope(List<String> atomicIndexIds) {
if (CollUtil.isEmpty(atomicIndexIds)){
if (CollUtil.isEmpty(atomicIndexIds)) {
return Map.of();
}
Map<String, List<ValueCalculateScopeDTO>> result = atomicIndexIds.stream().collect(Collectors.toMap(i -> i, i->List.of()));
Map<String, List<ValueCalculateScopeDTO>> result = atomicIndexIds.stream().collect(Collectors.toMap(i -> i, i -> List.of()));
List<ModelAtomicIndex> atomicIndexList = modelAtomicIndexService.listByIds(atomicIndexIds);
if (CollUtil.isEmpty(atomicIndexList)){
if (CollUtil.isEmpty(atomicIndexList)) {
return result;
}
List<String> 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<ModelIndexMapper, ModelIn
for (Map.Entry<String, List<ValueCalculateScopeDTO>> entry : result.entrySet()) {
List<ValueCalculateScopeDTO> 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<ModelIndexMapper, ModelIn
private List<ValueCalculateScopeDTO> makeScopes(String atomicIndexId, List<ModelAtomicIndex> atomicIndexList, List<NotePrompt> notePrompts, Map<String, String> 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<NotePromptExtractAttributesDto> extractAttributes = notePromptDTO.getExtractAttributes();

@ -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<EvidenceDirectory> evidenceDirectories = evidenceDirectoryService.list(new LambdaQueryWrapper<EvidenceDirectory>().eq(EvidenceDirectory::getCaseId, caseId));
// 查出证据信息
List<CaseEvidence> caseEvidences = caseEvidenceService.list(new LambdaQueryWrapper<CaseEvidence>().eq(CaseEvidence::getCaseId, caseId));
log.info("开始计算指标结果");
// 遍历指标集合,处理每个指标的判断逻辑,得出结果
modelIndices.forEach(modelIndex -> {
log.info("开始计算指标ID:{},指标名称:{}", modelIndex.getId(), modelIndex.getName());
IndexRule indexRule = modelIndex.getIndexRule();
Set<Boolean> ruleConditionGroupResultList = new HashSet<>();
if (indexRule != null) {
@ -205,15 +208,16 @@ public class ModelServiceImpl implements ModelService {
Set<Boolean> ruleConditionResultSet = new HashSet<>();
ruleConditionGroup.getRuleConditionList().forEach(ruleCondition -> {
Set<Boolean> 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<ResultDTO> 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<ModelAtomicIndex> atomicIndices, List<NotePrompt> notePrompts, List<EvidenceDirectory> evidenceDirectories, List<CaseEvidence> caseEvidences, ModelAtomicResult atomicResult) {
private boolean structureIndexAnalysis(OperandUnit operandUnit, ModelAtomicIndex modelAtomicIndex, List<ModelAtomicIndex> atomicIndices, List<NotePrompt> notePrompts, List<EvidenceDirectory> evidenceDirectories, List<CaseEvidence> caseEvidences, ModelAtomicResult atomicResult) {
Operand left = operandUnit.getLeftOperand();
String operator = operandUnit.getOperator();
Operand right = operandUnit.getRightOperand();
boolean flag = false;
List<CaseEvidence> 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<CaseEvidence> 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<CaseEvidence> evidences) {
private Double getSumFromEvidences(String propertyKey, List<CaseEvidence> evidences) {
AtomicReference<Double> sum = new AtomicReference<>(0.0);
if (evidences != null && !evidences.isEmpty()) {
evidences.forEach(e -> {
List<NotePromptExtractAttributesDto> 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()));
}

@ -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);

Loading…
Cancel
Save