本文源碼: "GitHub·點這裡" || "GitEE·點這裡" 一、Drools引擎簡介 1、基礎簡介 Drools是一個基於java的規則引擎,開源的,可以將複雜多變的規則從硬編碼中解放出來,以規則腳本的形式存放在文件中,使得規則的變更不需要修正代碼重啟機器就可以立即線上上環境生效。具有易於訪 ...
本文源碼:GitHub·點這裡 || GitEE·點這裡
一、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、項目結構
2、核心依賴
<!--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>
3、配置文件
@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·地址
https://github.com/cicadasmile/middle-ware-parent
GitEE·地址
https://gitee.com/cicadasmile/middle-ware-parent