【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.2 agenda-group

转载:https://blog.csdn.net/wo541075754/article/details/75332720

agenda-group

规则的调用与执行是通过StatelessKieSession或KieSession来实现的,一般的顺序是创建一个StatelessKieSession或KieSession,将各种经过编译的规则添加到session当中,然后将规则当中可能用到的Global对象和Fact对象插入到Session当中,最后调用fireAllRules 方法来触发、执行规则。

在没有调用fireAllRules方法之前,所有的规则及插入的Fact对象都存放在一个Agenda表的对象当中,这个Agenda表中每一个规则及与其匹配相关业务数据叫做Activation,在调用fireAllRules方法后,这些Activation会依次执行,执行顺序在没有设置相关控制顺序属性时(比如salience属性),它的执行顺序是随机的。

Agenda Group是用来在Agenda基础上对规则进行再次分组,可通过为规则添加agenda-group属性来实现。agenda-group属性的值是一个字符串,通过这个字符串,可以将规则分为若干个Agenda Group。引擎在调用设置了agenda-group属性的规则时需要显示的指定某个Agenda Group得到Focus(焦点),否则将不执行该Agenda Group当中的规则。

规则代码:

package com.rules

 rule "test agenda-group"

    agenda-group "abc"
    when
    then
        System.out.println("规则test agenda-group 被触发");
    end
rule otherRule

    when
    then
        System.out.println("其他规则被触发");
    end
View Code

调用代码:

KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
KieSession kSession = kieContainer.newKieSession("ksession-rule");

kSession.getAgenda().getAgendaGroup("abc").setFocus();
kSession.fireAllRules();
kSession.dispose();
View Code

执行以上代码,打印结果为:

规则test agenda-group 被触发
其他规则被触发
View Code

如果将代码kSession.getAgenda().getAgendaGroup(“abc”).setFocus()注释掉,则只会打印出:

其他规则被触发
View Code

很显然,如果不设置指定AgendaGroup获得焦点,则该AgendaGroup下的规则将不会被执行。

猜你喜欢

转载自www.cnblogs.com/shangxiaofei/p/9439165.html
4.2