spring boot + drools学习笔记(一)
程序员文章站
2022-05-28 12:21:58
...
引入依赖
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.14.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-spring</artifactId>
<version>7.14.0.Final</version>
</dependency>
配置
package com.wyc.drools;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
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 java.io.IOException;
/**
* @author: wyc
* @date: 2019/3/20
*/
@Configuration
public class DroolAutoConfiguration {
private static final String RULE_PATH = "rules/";
@Bean
@ConditionalOnMissingBean(KieFileSystem.class)
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for(Resource resource : getRuleFiles()){
kieFileSystem.write(ResourceFactory.newClassPathResource(RULE_PATH + resource.getFilename(),"UTF-8"));
}
return kieFileSystem;
}
@Bean
@ConditionalOnMissingBean(KieContainer.class)
public KieContainer kieContainer() throws IOException {
final KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(new KieModule() {
@Override
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
}
@Bean
@ConditionalOnMissingBean(KieBase.class)
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
@ConditionalOnMissingBean(KieSession.class)
public KieSession kieSession() throws IOException {
KieSession kieSession = kieContainer().newKieSession();
return kieSession;
}
private Resource[] getRuleFiles() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
return resourcePatternResolver.getResources("classpath*:" + RULE_PATH + "**/*.*");
}
public KieServices getKieServices(){
System.setProperty("drools.dateFotmat","yyyy-MM-dd");
return KieServices.Factory.get();
}
}
实体类
package com.wyc.drools;
/**
* @author: wyc
* @date: 2019/3/20
*/
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
语法规则实例
Hello world
规则
rule "helloworld"
when
eval(true)
then
System.err.println("hello,world!");
end
测试代码
@Test
public void testHelloWorld() {
kieSession.fireAllRules();
}
输出
hello,world!
条件筛选
规则
rule "user"
when
$u:User(name=="张三",age==18)
then
$u.setName("李四");
System.out.println($u.getName());
end
测试代码
@Test
public void testChangeUserName(){
User user = new User();
user.setAge(18);
user.setName("张三");
kieSession.insert(user);
kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("user"));
System.err.println("更改后的姓名:" + user.getName());
}
输出
李四
更改后的姓名:李四
自定义筛选器
规则
rule "customerRule1"
when
eval(true)
then
System.err.println("customerRule1");
end
rule "customerRule2"
when
eval(true)
then
System.err.println("customerRule2");
end
测试代码
@Test
public void testCustomerFilter(){
kieSession.fireAllRules(new CustomerFilter());
}
输出
customerRule1
customerRule2
contains
规则
rule "contains"
when
$s:String()
$u:User(name contains $s)
then
System.err.println("包含");
end
测试代码
@Test
public void testContains(){
User user = new User();
user.setName("张三");
//按照rule中的参数顺序依次insert
kieSession.insert("张三");
kieSession.insert(user);
kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("contains"));
}
输出
包含
memberOf
规则
rule "memberOf"
when
$list:List()
$u:User(name memberOf $list)
then
System.err.println("成员属于");
end
测试代码
@Test
public void testMemberOf(){
List<String> names = Stream.of("张三","李四").collect(Collectors.toList());
User user = new User();
user.setName("张三");
kieSession.insert(names);
kieSession.insert(user);
kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("memberOf"));
}
输出
成员属于
matches
规则
rule "matches"
when
// 必须使用.
$u:User(name matches "张.*")
then
System.err.println($u.getName());
end
测试代码
@Test
public void testMatches(){
User user = new User();
user.setName("张三");
kieSession.insert(user);
kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter("matches"));
}
输出
张三
上一篇: 原生态图片上传与加载
下一篇: VBnet 加载图片后如何删除图片文件
推荐阅读
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
X-Cart 学习笔记(一)了解和安装X-Cart
-
Nginx学习笔记(一)——入门
-
PHP 学习笔记 (一),php学习笔记_PHP教程
-
spring框架学习笔记1:搭建测试
-
HTML DOM(学习笔记一)_html/css_WEB-ITnose
-
韩顺平_PHP软件工程师玩转算法公开课(第一季)01_算法重要性_五子棋算法_汉诺塔_回溯算法_学习笔记_源代码图解_PPT文档整理
-
详解配置spring-boot-actuator时候遇到的一些小问题
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
-
详解Spring boot/Spring 统一错误处理方案的使用