Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效。具有易于访问企业策略、易于调整以及易于管理的特点,作为开源业务规则引擎,符合业内标准,速度快、效率高。
(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)、语法说明
SpringBoot2 整合 Drools规则引擎,实现高效的业务规则
<!--drools规则引擎--> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-templates</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>7.6.0.Final</version> </dependency> <dependency> <groupId>org.kie</groupId> <artifactId>kie-spring</artifactId> <version>7.6.0.Final</version> </dependency>
@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(); } }
这样环境整合就完成了。
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,参数标识是+号,执行添加规则,-号,执行移除规则操作。
@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()) ; } } }
@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() ; } }
这样,完整的案例就结束了。
https://gitee.com/cicadasmile/middle-ware-parent
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本网站。
您可能感兴趣的文章:
- Springboot集成规则引擎Drools方式
- SpringBoot整合Drools规则引擎动态生成业务规则的实现
- Spring Boot+Drools规则引擎整合详解
- Java基于drools做规则校验的实现