当前位置: 首页 > news >正文

单页网站网址/免费com网站域名注册

单页网站网址,免费com网站域名注册,人力资源公司劳务派遣,政府门户网站建设取得文章目录 前言一、创建规则库二、SpringBootDrools程序1.Maven依赖2.application.yml3.Mapper.xml4.Drools配置类5.Service6.Contoller7.测试接口 前言 公司的技术方案想搭建Drools自定义规则库配合大模型进行数据的校验。本篇用来记录使用SpringBoot配合Drools开发Demo程序。…

文章目录

  • 前言
  • 一、创建规则库
  • 二、SpringBoot+Drools程序
    • 1.Maven依赖
    • 2.application.yml
    • 3.Mapper.xml
    • 4.Drools配置类
    • 5.Service
    • 6.Contoller
    • 7.测试接口


前言

公司的技术方案想搭建Drools+自定义规则库配合大模型进行数据的校验。本篇用来记录使用SpringBoot配合Drools开发Demo程序。

初步设计的技术方案为:使用数据库存储DRL文件,在程序启动时将所有的DRL文件加载到程序。接口传入数据时要带有想要做规则判断的DRL文件key值,支持多个key。面对规则库的数据有修改或增添时,理想方案是监听数据库的修改,然后将增量或修改加载到程序。也可以通过定时任务的方式定时全量重新加载,但就没办法实现实时生效。也可以暴露接口手动重新加载。

以上为初步的技术方案,很多地方还比较粗糙,如果有比较成熟的方案欢迎大家交流。


一、创建规则库

我这里用的MySql数据库
建表语句如下

DROP TABLE IF EXISTS `drools_rules`;
CREATE TABLE `drools_rules`  (`id` int(11) NOT NULL AUTO_INCREMENT,`rule_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,`rule_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,`version` int(11) NULL DEFAULT 1,`enabled` tinyint(1) NULL DEFAULT 1,`last_modified` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0),PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Compact;

主要就是将DRL文件以文本的格式存到数据库里
DRL例子如下:

rule "手术麻醉方式必填校验" @BusinessType("diagnosis_check")when$data: org.example.drools.domain.vo.ClinicalData(anesthesiaMethod == null || anesthesiaMethod.isEmpty())$result: org.example.drools.domain.vo.ValidationResult()then$result.addError("RULE_001", "手术记录必须包含麻醉方式");end

二、SpringBoot+Drools程序

1.Maven依赖

我这里Java用的11版本

		<!-- Drools --><dependency><groupId>org.drools</groupId><artifactId>drools-core</artifactId><version>7.69.0.Final</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-compiler</artifactId><version>7.69.0.Final</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-mvel</artifactId><version>7.69.0.Final</version> <!-- 确保版本与 drools-core 一致 --></dependency>

2.application.yml

server:port: 9528
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://XXXX:3306/drools_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: root
logging:level:org.springframework: INFOcom.example.drools: DEBUG
mybatis-plus:type-aliases-package: com.example.drools.domainmapper-locations: classpath*:mapper/**/*Mapper.xml

3.Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.drools.mapper.DroolsDemoMapper"><select id="findAllEnabledRules" resultType="org.example.drools.domain.entity.DroolsRules">SELECT *FROM drools_rules rWHERE r.enabled = 1ORDER BY r.version DESC</select>
</mapper>

获取数据库中所有生效的规则,按版本倒排

4.Drools配置类

package org.example.drools.config;import org.example.drools.domain.entity.DroolsRules;
import org.example.drools.mapper.DroolsDemoMapper;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.runtime.KieContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.List;/*** Drools 动态配置*/
@Configuration
public class DroolsConfig {//操作规则库的Mapper@Autowiredprivate DroolsDemoMapper droolsDemoMapper;@Beanpublic KieContainer kieContainer() {KieServices kieServices = KieServices.get();KieFileSystem kieFileSystem = kieServices.newKieFileSystem();reloadRules(kieFileSystem); // 初始化加载规则KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);kieBuilder.buildAll();KieModule kieModule = kieBuilder.getKieModule();return kieServices.newKieContainer(kieModule.getReleaseId());}// 动态重新加载规则public void reloadRules(KieFileSystem kieFileSystem) {List<DroolsRules> rules = droolsDemoMapper.findAllEnabledRules();rules.forEach(rule ->kieFileSystem.write(String.format("src/main/resources/%s.drl", rule.getRuleName()),rule.getRuleContent()));}
}

初始化加载所有规则到Drools的容器中

5.Service

package org.example.drools.service;import org.drools.core.base.RuleNameEqualsAgendaFilter;
import org.example.drools.config.DroolsConfig;
import org.example.drools.domain.vo.ClinicalData;
import org.example.drools.domain.vo.ValidationResult;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.definition.KiePackage;
import org.kie.api.definition.rule.Rule;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.AgendaFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;@Service
public class DroolsService {@Autowiredprivate KieContainer kieContainer;@Autowiredprivate DroolsConfig droolsConfig;private static Map<String, String> ruleMetadataMap = new HashMap<>();/*** 初始化元数据*/@PostConstructprivate void init() {KieBase kieBase = kieContainer.getKieBase();for (KiePackage pkg : kieBase.getKiePackages()) {for (Rule rule : pkg.getRules()) {org.drools.core.definitions.rule.impl.RuleImpl ruleImpl =(org.drools.core.definitions.rule.impl.RuleImpl) rule;// 提取元数据String businessType = (String) ruleImpl.getMetaData().get("BusinessType");ruleMetadataMap.put(rule.getName(), businessType);}}}public ValidationResult validate(ClinicalData request) {KieSession kieSession = kieContainer.newKieSession();ValidationResult result = new ValidationResult();//kieSession.setGlobal("errors", result.getErrors());kieSession.insert(request);kieSession.insert(result);AgendaFilter filter = activation -> {String ruleName = activation.getRule().getName();String businessType = ruleMetadataMap.get(ruleName);return request.getRuleTypes().contains(businessType);};kieSession.fireAllRules(filter);kieSession.dispose();return result;}}

当我们将所有的规则初始化加载到Drools的容器中后,如果直接使用接口参数执行规则判断,那么Drools将使用参数走过所有的规则判断,而实际的场景中我这些参数只想执行部分规则,那么就引出过滤规则的问题。

当时在处理这个问题的时候也是问了大模型的方案,当时大模型给出了一个比较麻烦的方案,就是在DRL文件中使用类似自定义注解的东西将DRL的一个业务编码写进去,具体写法可以看上面我展示的DRL文件例子,@BussinessType那个。然后后面就一直研究这个方案如何实现。

后面整理的时候发现其实有很简单的方法就能实现这个问题,我先说这个麻烦的方案是如何实现的,后面补充上简单的方案

麻烦的方案是:在将所有的规则加载到容器中后,遍历所有规则,将规则的name作为key,将DRL文件中的@BussinessType的字段取出作为value存在一个Map中,然后编写Drools的过滤器

 AgendaFilter filter = activation -> {String ruleName = activation.getRule().getName();String businessType = ruleMetadataMap.get(ruleName);return request.getRuleTypes().contains(businessType);};

简单的方案是:接口传参的时候直接传rule_name的集合,反正都是定死的,传name和bussinessType没区别,然后过滤器直接request.getRuleTypes().contains(ruleName)就行了。

6.Contoller

@RestController
@RequestMapping("/drools")
@Api(tags = "Drools测试 API", description = "提供Drools测试相关的 Rest API")
public class DroolsController {@Autowiredprivate DroolsService droolsService;@PostMapping("/test")public ValidationResult validateClinicalData(@RequestBody ClinicalData request) {return droolsService.validate(request);}
}
@Data
@ApiModel(value = "ClinicalData")
public class ClinicalData {@ApiModelProperty("麻醉方式")private String anesthesiaMethod;// 门(急)诊诊断编码(ICD-10)@ApiModelProperty("门(急)诊诊断编码")private String emergencyDiagnosisCode;@ApiModelProperty("规则类型集合")private Set<String> ruleTypes;public String getAnesthesiaMethod() {return anesthesiaMethod;}public void setAnesthesiaMethod(String anesthesiaMethod) {this.anesthesiaMethod = anesthesiaMethod;}public String getEmergencyDiagnosisCode() {return emergencyDiagnosisCode;}public void setEmergencyDiagnosisCode(String emergencyDiagnosisCode) {this.emergencyDiagnosisCode = emergencyDiagnosisCode;}public Set<String> getRuleTypes() {return ruleTypes;}public void setRuleTypes(Set<String> ruleTypes) {this.ruleTypes = ruleTypes;}
}

7.测试接口

在这里插入图片描述
附上结果返回的实体类结构以及我测试使用的两个DRL文件

@Data
public class ValidationResult {// 校验错误信息列表private List<String> errors = new ArrayList<>();// 校验通过标记public boolean isValid() {return errors.isEmpty();}// 添加带错误码的信息public void addError(String errorCode, String message) {errors.add(String.format("[%s] %s", errorCode, message));}public List<String> getErrors() {return errors;}public void setErrors(List<String> errors) {this.errors = errors;}
}
rule "手术麻醉方式必填校验" @BusinessType("diagnosis_check")when$data: org.example.drools.domain.vo.ClinicalData(anesthesiaMethod == null || anesthesiaMethod.isEmpty())$result: org.example.drools.domain.vo.ValidationResult()then$result.addError("RULE_001", "手术记录必须包含麻醉方式");end
function Boolean isValidDiagnosisCode(String code) {if (code == null || code.isEmpty()) return false;Character firstChar = Character.toUpperCase(code.charAt(0));return (firstChar >= 'A' && firstChar <= 'U') || firstChar == 'Z';
}
rule "诊断编码范围校验" @BusinessType("code_check")when$data: org.example.drools.domain.vo.ClinicalData(emergencyDiagnosisCode != null,!isValidDiagnosisCode(emergencyDiagnosisCode))$result: org.example.drools.domain.vo.ValidationResult()then$result.addError("RULE_002", "诊断编码各项编码范围应为:A~U开头和Z开头的编码");end
http://www.whsansanxincailiao.cn/news/31995948.html

相关文章:

  • 网站建设 网站开发 区别/百度自然搜索排名优化
  • 一个网站建设的成本/安卓aso
  • 搜狗seo优化/吉林seo外包
  • 滁州网站建设czesou/青岛关键词排名系统
  • 英文网站标题字体/seo软件安卓版
  • 武汉建网站/湘潭网站seo
  • 自助网站搭建系统/营销策略国内外文献综述
  • wordpress tab选项卡主题/湖南seo推广软件
  • 如何不花钱做网站/手机网站自助建站系统
  • 电脑做兼职找那个网站/网络营销的概述
  • 全新正版营销网站/搜索引擎营销的成功案例
  • wordpress用户链接/南通seo网站优化软件
  • asp.net做网站/seo专员工作容易学吗
  • 做网站分层技术/seo优化
  • 重庆专业网站建设公司哪家好/如何制作一个宣传网页
  • 蛋糕电子商务网站建设方案/网络营销都有哪些形式
  • 台湾网站建设/自媒体平台注册官网下载
  • 网站建设结束的售后服务/百度推广客户端app
  • 投票小程序制作/seo营销策略
  • 怎样用php做网站/成人再就业培训班
  • 在线做c语言题目的网站/公司网站制作
  • 有没有专门做奶粉的网站/深圳百度推广排名优化
  • 牟平建设企业网站/江东seo做关键词优化
  • vip视频网站如何做/专业软文平台
  • 江苏泰州网站建设/广东云浮疫情最新情况
  • 获取网站访客qq/宁波关键词优化平台
  • 广州酒店网站制作/优化神马网站关键词排名价格
  • 网站开发和美工的区别/seo优化与推广招聘
  • 小企业做网站有没有用/百度公司电话热线电话
  • 个人电脑做网站服务器/seo发贴软件