一、简介
Drools是一个易于访问企业策略、易于调整以及易于管理的开源业务规则引擎,符合业内标准,速度快、效率高。业务分析师或审核人员可以利用它轻松查看业务规则,从而检验是否已编码的规则执行所需的业务规则。
适用于风控、反欺诈规则匹配
二、语法
详见地址:http://www.drools.org.cn/
三、springboot整合drools
1、pom引包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhouzy.drools</groupId>
<artifactId>zzyDrools</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zzyDrools</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- drools 规则引擎 版本 -->
<drools.version>7.0.0.Final</drools.version>
<log4j2.version>2.5</log4j2.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<!-- 依赖项定义 -->
<dependencies>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>7.11.0.Final</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.11.0.Final</version>
</dependency>
<!-- Spring boot start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置资源文件
leave.drl
package rules;
dialect "java"
import com.zhouzy.drools.model.Leave;
import com.zhouzy.drools.service.DroolsService;
rule "leave1"
no-loop true
when
u : Leave(day < 1);
then
System.out.println("leave1规则触发了...");
u.setHit(1);
DroolsService droolsService = new DroolsService();
droolsService.hitRule("1001","leave1");
update(u);
end
rule "leave2"
no-loop true
when
u : Leave(day >= 3);
then
System.out.println("leave2规则触发了...");
u.setHit(1);
DroolsService droolsService = new DroolsService();
droolsService.hitRule("1001","leave2");
update(u);
end
rule "leave3"
no-loop true
when
u : Leave(day >= 3);
then
System.out.println("leave3规则触发了...");
u.setHit(1);
DroolsService droolsService = new DroolsService();
droolsService.hitRule("1001","leave3");
update(u);
end
kmodule.xml
注意里面的packages要跟leave.drl文件中的保持一致
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules" packages="rules">
<ksession name="ksession-rules"/>
</kbase>
</kmodule>
3、配置类
package com.zhouzy.drools.config;
import java.io.IOException;
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.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
@Configuration
public class KieSessionConfig {
//rule的存放位置
private static final String RULES_PATH = "rules/";
@Bean
@ConditionalOnMissingBean(KieFileSystem.class)
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for (Resource file : getRuleFiles()) {
String path = RULES_PATH + file.getFilename();
kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
}
return kieFileSystem;
}
/**
* 这里要引入 org.springframework.core.io.Resource 包
*
* @return
* @throws IOException
*/
private Resource[] getRuleFiles() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
}
@Bean
@ConditionalOnMissingBean(KieContainer.class)
public KieContainer kieContainer() throws IOException {
final KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
}
private KieServices getKieServices() {
return KieServices.Factory.get();
}
@Bean
@ConditionalOnMissingBean(KieBase.class)
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
@ConditionalOnMissingBean(KieSession.class)
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
@Bean
@ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)
public KModuleBeanFactoryPostProcessor kiePostProcessor() {
return new KModuleBeanFactoryPostProcessor();
}
}
4、对象类
Leave.java
package com.zhouzy.drools.model;
import java.io.Serializable;
public class Leave implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3438098591608217946L;
private String name;
private int day;
private int hit = 0; //0代表未命中,1代表命中
public Leave(){
}
public Leave(String name,int day){
this.name = name;
this.day = day;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
}
DroolsService.java
package com.zhouzy.drools.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DroolsService {
Logger log = LoggerFactory.getLogger(DroolsService.class);
public void hitRule(String id,String hitRule){
log.info("规则命中详情,id:{},hitRule:{}",id,hitRule);
}
}
5、测试类
package com.zhouzy.drools;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.zhouzy.drools.model.Leave;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DroolsApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DroolsTest {
Logger log = LoggerFactory.getLogger(DroolsTest.class);
@Autowired
private KieSession session;
@Test
public void droolsTest() {
Leave leave = new Leave();
session.insert(leave);//插入
int hitCount = session.fireAllRules();//执行规则
log.info("命中了:{}条规则",hitCount);
session.dispose();
}
}
测试结果: