• 【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下的规则将不会被执行。

  • 相关阅读:
    C#实现清理系统内存
    WinForm 程序加管理员权限
    DataGridView 绑定List集合后实现自定义排序
    swfupload提示“错误302”的解决方法
    C# WinForm捕获全局异常
    C# WinForm应用程序降低系统内存占用方法
    清除webBrowser 缓存和Cookie的解决方案
    ThInkPHP加密和解密cookie(登录操作)
    prestashop 首页轮播幻灯片图片修改
    网页内容分享到微信
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/9439165.html
Copyright © 2020-2023  润新知