微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot2整合Drools规则引擎及案例详解

这篇文章主要介绍了SpringBoot2整合Drools规则引擎及案例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、Drools引擎简介

1、基础简介Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效。具有易于访问企业策略、易于调整以及易于管理的特点,作为开源业务规则引擎,符合业内标准,速度快、效率高。2、规则语法(1)、演示drl文件格式package droolRule ; import org.slf4j.Logger import org.slf4j.LoggerFactory ; dialect "java" rule "paramcheck1" when then final Logger LOGGER = LoggerFactory.getLogger("param-check-one 规则引擎") ; LOGGER.info("参数"); end(2)、语法说明・ 文件格式可以 .drl、xml文件,也可以Java代码块硬编码;・ package规则文件中,package是必须定义的,必须放在规则文件第一行;・ import规则文件使用到的外部变量,可以是一个类,也可以是类中的可访问的静态方法;・ rule定义一个规则。paramcheck1规则名。规则通常包含三个部分:属性、条件、结果;二、整合SpringBoot框架1、项目结构SpringBoot2 整合 Drools规则引擎,实现高效的业务规则

2、核心依赖org.droolsdrools-core7.6.0.Finalorg.droolsdrools-compiler7.6.0.Finalorg.droolsdrools-templates7.6.0.Finalorg.kiekie-api7.6.0.Finalorg.kiekie-spring7.6.0.Final3、配置文件@Configuration public class RuleEngineConfig { private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineConfig.class) ; private static final String RULES_PATH = "droolRule/"; private final KieServices kieServices = KieServices.Factory.get(); @Bean public KieFileSystem kieFileSystem() throws IOException { KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] files = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*"); String path = null; for (Resource file : files) { path = RULES_PATH + file.getFilename(); LOGGER.info("path="+path); kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8")); } return kieFileSystem; } @Bean public KieContainer kieContainer() throws IOException { KieRepository kieRepository = kieServices.getRepository(); kieRepository.addKieModule(kieRepository::getDefaultReleaseId); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem()); kieBuilder.buildAll(); return kieServices.newKieContainer(kieRepository.getDefaultReleaseId()); } @Bean public KieBase kieBase() throws IOException { return kieContainer().getKieBase(); } @Bean public KieSession kieSession() throws IOException { return kieContainer().newKieSession(); } @Bean public KModulebeanfactoryPostProcessor kiePostProcessor() { return new KModulebeanfactoryPostProcessor(); } }这样环境整合就完成了。三、演示案例1、规则文件规则一dialect "java" rule "paramcheck1" salience 99 when queryParam : QueryParam(paramId != null && paramSign.equals("+")) resultParam : RuleResult() then final Logger LOGGER = LoggerFactory.getLogger("param-check-one 规则引擎") ; LOGGER.info("参数:getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineserviceImpl ruleEngineservice = new RuleEngineserviceImpl() ; ruleEngineservice.executeAddRule(queryParam); resultParam.setPostCodeResult(true); end规则二dialect "java" rule "paramcheck2" salience 88 when queryParam : QueryParam(paramId != null && paramSign.equals("-")) resultParam : RuleResult() then final Logger LOGGER = LoggerFactory.getLogger("param-check-two 规则引擎") ; LOGGER.info("参数:getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineserviceImpl ruleEngineservice = new RuleEngineserviceImpl() ; ruleEngineservice.executeRemoveRule(queryParam); resultParam.setPostCodeResult(true); end规则说明:A、salience 的值越大,越优先执行;B、规则流程:如果paramId不为null,参数标识是+号,执行添加规则,-号,执行移除规则操作。2、规则执行代码@Service public class RuleEngineserviceImpl implements RuleEngineservice { private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineserviceImpl.class) ; @Override public void executeAddRule(QueryParam param) { LOGGER.info("参数数据:"+param.getParamId()+";"+param.getParamSign()); ParamInfo paramInfo = new ParamInfo() ; paramInfo.setId(param.getParamId()); paramInfo.setParamSign(param.getParamSign()); paramInfo.setCreateTime(new Date()); paramInfo.setUpdateTime(new Date()); ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ; paramInfoService.insertParam(paramInfo); } @Override public void executeRemoveRule(QueryParam param) { LOGGER.info("参数数据:"+param.getParamId()+";"+param.getParamSign()); ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ; ParamInfo paramInfo = paramInfoService.selectById(param.getParamId()); if (paramInfo != null){ paramInfoService.removeById(param.getParamId()) ; } } }3、规则调用接口@RestController @RequestMapping("/rule") public class RuleController { @Resource private KieSession kieSession; @Resource private RuleEngineservice ruleEngineservice ; @RequestMapping("/param") public void param (){ QueryParam queryParam1 = new Queryparam() ; queryParam1.setParamId("1"); queryParam1.setParamSign("+"); QueryParam queryParam2 = new Queryparam() ; queryParam2.setParamId("2"); queryParam2.setParamSign("-"); // 入参 kieSession.insert(queryParam1) ; kieSession.insert(queryParam2) ; kieSession.insert(this.ruleEngineservice) ; // 返参 RuleResult resultParam = new RuleResult() ; kieSession.insert(resultParam) ; kieSession.fireAllRules() ; } }这样,完整的案例就结束了。四、源代码地址GitHub・地址GitEE・地址https://gitee.com/cicadasmile/middle-ware-parent

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐