Drools7规则属性之agenda-group

1,067 阅读1分钟

规则文件

package rules.rulesHello.agenda;

rule "rule01"
    agenda-group "agenda-group-1"
    salience 9
    when
        eval(true)
    then
        System.out.println("rule01 with agenda-group-1 ");
end

rule "rule02"
    agenda-group "agenda-group-1"
    salience 10
    when
        eval(true)
    then
        System.out.println("rule02 with agenda-group-1 ");
end

rule "rule03"
    agenda-group "agenda-group-2"
    auto-focus true
    when
        eval(true)
    then
        System.out.println("rule03 with agenda-group-2 ");
end

rule "rule04"
    when
        eval(true)
    then
        System.out.println("rule04 without agenda group ");
end

测试代码

        KieServices kss = KieServices.Factory.get();
        KieContainer kc = kss.newKieClasspathContainer();
        KieSession session = kc.newKieSession("agenda01");
        AgendaGroup agendaGroup = session.getAgenda().getAgendaGroup("agenda-group-1");
        agendaGroup.setFocus();
//        agendaGroup.clear();
        int count = session.fireAllRules();
        session.dispose();
执行结果:
rule03 with agenda-group-2 
rule02 with agenda-group-1 
rule01 with agenda-group-1 
rule04 without agenda group 

agenda-group的结构就是一个栈,默认没有设置agend-group的情况下,所有规则均在一个名称为MAIN的agenda-group中,当通过 auto-focus设置为true则会自动添加到agenda-group栈上,而通过session.getAgenda().getAgendaGroup("agenda-group-1")的方式会将它继续添加到栈中。从栈顶以此执行,执行完就会被移除继续执行下一个,直至MAINgroup.