echo编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075) 交流学习。
Drools的实现并不难,关键是了解他的语法即可
入门案例
- 创建一个简单的SpringBoot项目
- 选择依赖的时候,添加一个web依赖即可
- 在新项目的pom.xml中添加drools的相关依赖
<properties>
<java.version>1.8</java.version>
<drools.version>7.14.0.Final</drools.version>
</properties>
<dependencies>
<!-- drools依赖 -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
<!-- 决策表 -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${drools.version}</version>
</dependency>
<!-- 模板 -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-templates</artifactId>
<version>${drools.version}</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>${drools.version}</version>
</dependency>
</dependencies>
- 创建Kie相关 Bean 使其被spring管理
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.Resource;
import java.io.IOException;
/**
* @author tang.sl
* @date 2021-01-05 09:37
*/
@Configuration
public class KiaSessionConfig {
private static final String RULES_PATH = "rules/";
@Bean
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for (Resource file : getRuleFiles()) {
kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
}
return kieFileSystem;
}
private Resource[] getRuleFiles() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
final Resource[] resources = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
return resources;
}
@Bean
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
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
}
到目前为止,我们的基本准备工作就已经完成了,我们可以创建实际的案例来使用Drools了。
- 根据前面的需求定义一个实体类
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private String age;
private String address;
private String phone;
private String sex;
}
- 创建drl规则文件
根据前几篇文章描述的内容,我们来设计一个规则文件。文件放在resources目录下面,在该目录下面创建一个rules统一存放后面所有的rule文件
package com.echo.drools.dto
import com.echo.drools.dto.User
dialect "java"
rule "address eq beijing"
when
$user: User(address == "北京")
then
System.out.println($user.getName() + "是个北漂");
end
rule "address eq shanghai"
when
$user: User(address == "上海")
then
System.out.println($user.getName() + "是个上漂");
end
- 然后我们就可以来测试使用这个规则
import com.echo.drools.dto.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DroolsApplicationTests {
@Autowired
private KieSession session;
@Test
void contextLoads() {
User user = new User();
user.setAddress("北京");
user.setAge("20");
user.setName("echo");
user.setPhone("136123456");
user.setSex("1");
// 插入用户
session.insert(user);
// 执行规则
session.fireAllRules();
}
/**
* 记得执行完成之后释放资源
*/
@AfterEach
public void runDispose() {
session.dispose();
}
}
- 多次测试
结果和我们的符合我们原有的预期,比如上面的实例,最终会在控制台输出“echo是北漂”。
注意:我们这里仅仅写了两个规则,并且也没有写对应的业务代码,这个后面再讲,仅仅只是一个入门展示案例。不要多纠结为什么没有业务代码