引入依赖
org.drools
drools-core
7.14.0.Final
org.kie
kie-spring
7.14.0.Final
配置
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 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"));
}
输出
张三