java代码
@Test
public void testjblx() throws Exception {
Resource dis = ResourceFactory.newClassPathResource("rules/testdrl/testjblx.drl", TestTemplate01.class);
ObjectDataCompiler converter = new ObjectDataCompiler();
Collection cfl = new ArrayList();
KieHelper helper = new KieHelper();
helper.addResource(dis,ResourceType.DRL);
KieSession ksession = helper.build().newKieSession();
FactHandle f = ksession.insert(list);
ksession.fireAllRules(new RuleNameEndsWithAgendaFilter("bbb"));
ksession.delete(f);
FactHandle fs = ksession.insert(list);
int i = ksession.fireAllRules(new RuleNameEndsWithAgendaFilter("aaa")); // 指定 具体规则以bbb结尾的规则
System.out.println( " " + i + "次");
ksession.dispose();
}
rule aaa
when
$list:ArrayList();
$listint:Integer(intValue > 485) from $list
then
System.out.println($listint.longValue());
end
rule bbb
when
$list:ArrayList();
$listchar:String(toString =="张400") from $list
then
System.out.println($listchar.toString());
end
package com.drools.web.jars;
import org.kie.api.runtime.rule.AgendaFilter;
import org.kie.api.runtime.rule.Match;
import java.util.Set;
/**
* Created by kangz on 2016/9/7.
*/
public class CustomAgendaFilter implements AgendaFilter {
private final Set ruleNamesThatAreAllowedToFire;//传入的rule name
public CustomAgendaFilter(Set ruleNamesThatAreAllowedToFire) {
this.ruleNamesThatAreAllowedToFire = ruleNamesThatAreAllowedToFire;
}
@Override
public boolean accept(Match match) {
return ruleNamesThatAreAllowedToFire.contains(match.getRule().getName());
}
}
实现代码:
@Test
public void namesRules(){
//KieSession kieSession= newKisessions();
Resource dis = ResourceFactory.newClassPathResource("rules/testdrl/StatelessRulesName.drl", TestTemplate01.class);
KieHelper helper = new KieHelper();
helper.addResource(dis, ResourceType.DRL);
KieSession kieSessionieSession=helper.build().newKieSession();
Set set=new HashSet();
set.add("name的使用1");
set.add("name的使用3");
set.add("name的使用5");
kieSessionieSession.fireAllRules(new CustomAgendaFilter(set));
}
rule "name的使用1"
dialect "mvel"
when
then
System.out.println("name的使用1正在使用");
end
rule "name的使用2"
dialect "mvel"
when
forall(p:Person(name=="张三") Person(this==p,age==10));
then
System.out.println("name的使用2正在使用");
end
rule "name的使用3"
when
forall(p:Person(name=="张三") Person(this==p,age==10));
then
System.out.println("name的使用3正在使用");
end
rule "name的使用4"
dialect "mvel"
when
forall(p:Person(name=="张三") Person(this==p,age==10));
then
System.out.println("name的使用4正在使用");
end
rule "name的使用5"
dialect "mvel"
when
forall(p:Person(name=="张三") Person(this==p,age==10));
then
System.out.println("name的使用5正在使用");
end
@Test
public void newRuleNameEqualsAgendaFilter(){
Resource dis = ResourceFactory.newClassPathResource("rules/testdrl/StatelessRulesName.drl", TestTemplate01.class);
KieHelper helper = new KieHelper();
helper.addResource(dis, ResourceType.DRL);
KieSession ksession = helper.build().newKieSession();
int i =ksession.fireAllRules(new RuleNameEqualsAgendaFilter("name的使用6"));//根据规则名精确执行所有匹配的规则
System.out.println("共执行了 "+i);
}
@Test
public void newRuleNameStartsWithAgendaFilter(){
Resource dis = ResourceFactory.newClassPathResource("rules/testdrl/StatelessRulesName.drl", TestTemplate01.class);
KieHelper helper = new KieHelper();
helper.addResource(dis, ResourceType.DRL);
KieSession ksession = helper.build().newKieSession();
int i =ksession.fireAllRules(new RuleNameStartsWithAgendaFilter("name"));//根据规则名开头执行所有匹配的规则
System.out.println("共执行了 "+i);
}
@Test
public void newRuleNameEndsWithAgendaFilter(){
Resource dis = ResourceFactory.newClassPathResource("rules/testdrl/StatelessRulesName.drl", TestTemplate01.class);
KieHelper helper = new KieHelper();
helper.addResource(dis, ResourceType.DRL);
KieSession ksession = helper.build().newKieSession();
int i =ksession.fireAllRules(new RuleNameEndsWithAgendaFilter("的使用6"));//根据规则结尾执行所有匹配的规则
System.out.println("共执行了 "+i);
}
@Test
public void namesRulesregular(){
//KieSession kieSession= newKisessions();
Resource dis = ResourceFactory.newClassPathResource("rules/testdrl/StatelessRulesName.drl", TestTemplate01.class);
KieHelper helper = new KieHelper();
helper.addResource(dis, ResourceType.DRL);
KieSession kieSessionieSession=helper.build().newKieSession();
int i= kieSessionieSession.fireAllRules(new RuleNameMatchesAgendaFilter("\\w{0,5}[\\u4e00-\\u9fa5]{0,10}\\d"));
System.out.println("共执行了 "+i);
}
下面是小编的微信转帐二维码,小编再次谢谢读者的支持,小编会更努力的
----请看下方↓↓↓↓↓↓↓
百度搜索 Drools从入门到精通:可下载开源全套Drools教程
深度Drools教程不段更新中:
更多Drools实战陆续发布中………
扫描下方二维码关注公众号 ↓↓↓↓↓↓↓↓↓↓