规则引擎-drools-3.2-drl文件构成-rule部分-属性Attribute

news2024/10/6 23:15:59

文章目录

  • drl文件构成-rule部分
  • rule示例
    • rule name
    • Attribute
      • 全部属性说明
      • no-loop 和 lock-on-active
      • activation-group 和 agenda-group

drl文件构成-rule部分

drl文件构成,位于官网的第5章位置,也是drools作为规则引擎应用的最核心部分。
其中rule模块,包括属性(Attribute - rule)、条件(Condition - when)、结果(Action - then)是5.1.7、5.1.8、5.1.9 三小节部分内容。

  1. Rule Language Reference
    官网链接:https://docs.drools.org/7.73.0.Final/drools-docs/html_single/index.html#_droolslanguagereferencechapter
    在这里插入图片描述

rule示例

rule "rule_name"
    // Attribute
    // Attribute
    when
        // Conditions
    then
        // Actions
end

rule name

其中,rule_name 是全局唯一的,意思是在kie的工作内存中需要保持唯一。所以,安全起见,在整个项目中这个rule_name保持唯一就好了。因为是字符串类型,所以,约束较少,可以使用空格、中文等。不过对于实际应用中,一般会给出有规律的命名,比如同一种类型的规则,使用相同的前缀或后缀(这样也是在后期使用时,可以利用kie的api,可以明确执行约定的规则。)

Attribute

全部属性说明

位于rule和when之间的部分,是Attribute。属性一般作用于该条规则(rule),如执行优先级、触发时间限制等。
在这里插入图片描述
每个属性在官网中都有明确的描述

Attribute Value
@ salience
An integer defining the priority of the rule. Rules with a higher salience value are given higher priority when ordered in the activation queue.
含义:salience 后面的数字越大,优先级越高
’Example: salience 10
@ enabled
A Boolean value. When the option is selected, the rule is enabled. When the option is not selected, the rule is disabled.
含义:该规则是否启用
Example: enabled true
@ date-effective
A string containing a date and time definition. The rule can be activated only if the current date and time is after a date-effective attribute.
含义:该规则从指定的日期开始生效,代码增加:System.setProperty(“drools.dateformat”, “yyyy-MM-dd”); 可以指定日期格式
Example: date-effective “4-Sep-2018”
@ date-expires
A string containing a date and time definition. The rule cannot be activated if the current date and time is after the date-expires attribute.
含义:该规则在指定日期之前生效 ,代码增加:System.setProperty(“drools.dateformat”, “yyyy-MM-dd”); 可以指定日期格式
Example: date-expires “4-Oct-2018”
@ no-loop
A Boolean value. When the option is selected, the rule cannot be reactivated (looped) if a consequence of the rule re-triggers a previously met condition. When the condition is not selected, the rule can be looped in these circumstances.
含义:该规则结果改变造成的再次满足该规则条件,不会被触发。但其他规则结果改变造成的满足条件,会再次触发该规则。
Example: no-loop true
@ agenda-group
A string identifying an agenda group to which you want to assign the rule. Agenda groups allow you to partition the agenda to provide more execution control over groups of rules. Only rules in an agenda group that has acquired a focus are able to be activated.
Example: agenda-group “GroupName”
@ activation-group
A string identifying an activation (or XOR) group to which you want to assign the rule. In activation groups, only one rule can be activated. The first rule to fire will cancel all pending activations of all rules in the activation group.
Example: activation-group “GroupName”
@ duration
A long integer value defining the duration of time in milliseconds after which the rule can be activated, if the rule conditions are still met.
Example: duration 10000
@ timer
A string identifying either int (interval) or cron timer definitions for scheduling the rule.
含义:定时器,类似于java的定时器,定义了规则允许触发的频次
Example: timer ( cron:* 0/15 * * * ? ) (every 15 minutes)
@ calendar
A Quartz calendar definition for scheduling the rule.
含义:类似于定时器,语法不同,定义了规则允许触发的时间段
Example:
// Exclude non-business hours
calendars “* * 0-7,18-23 ? * *”
// Weekdays only, as registered in the KIE session
calendars “weekday”
@ auto-focus
A Boolean value, applicable only to rules within agenda groups. When the option is selected, the next time the rule is activated, a focus is automatically given to the agenda group to which the rule is assigned.
含义:作用于 agenda groups,分组内只要有一个规则标记了 auto-focus,则名称相同的 agenda groups 分组会被触发规则。
Example: auto-focus true
@ lock-on-active
A Boolean value, applicable only to rules within rule flow groups or agenda groups. When the option is selected, the next time the ruleflow group for the rule becomes active or the agenda group for the rule receives a focus, the rule cannot be activated again until the ruleflow group is no longer active or the agenda group loses the focus. This is a stronger version of the no-loop attribute, because the activation of a matching rule is discarded regardless of the origin of the update (not only by the rule itself). This attribute is ideal for calculation rules where you have a number of rules that modify a fact and you do not want any rule re-matching and firing again.
含义:无论规则结果是否修改,该规则至多触发一次。
Example: lock-on-active true
@ ruleflow-group
A string identifying a rule flow group. In rule flow groups, rules can fire only when the group is activated by the associated rule flow.
含义:规则流
Example: ruleflow-group “GroupName”
@ dialect
A string identifying either JAVA or MVEL as the language to be used for code expressions in the rule. By default, the rule uses the dialect specified at the package level. Any dialect specified here overrides the package dialect setting for the rule.
含义:规则语言, JAVA or MVEL,默认是java
Example: dialect “JAVA”
When you use Drools without the executable model, the dialect “JAVA” rule consequences support only Java 5 syntax. For more information about executable models, see Executable rule models.

以上属性基本都是常用属性。业务场景基本覆盖的到。
便于阅读,拿出几个初学者需要会“百度一下”的属性简单介绍一下。

no-loop 和 lock-on-active

no-loop:官网定义:如果规则的结果再次触发先前满足的条件,则无法重新激活(循环)规则。
说明这个属性只能管自己的结果改变(modify、update)造成的再次触发先前满足的条件。其他规则的结果造成的,它管不了。
lock-on-active:官网定义:仅适用于 rule flow groups 或 agenda groups. 这两种分组。这是一个更强大的无循环属性版本,因为无论更新的来源如何(而不仅仅是规则本身),匹配规则的激活都会被丢弃。
先不管适用啥分组,后面这句话的意思是 no-loop的一个升级版,无论是被谁的结果条件改变造成的,该规则都最多被触发一次。

总结一句话,如果只是使用no-loop,在不了解这两个属性区别之前,会造成死循环。

文字比较抽象,举例说明:

test方法:

  @Test
    public void testLoop() {
        // 获取services
        KieServices kieServices = KieServices.Factory.get();
        // 获取container
        KieContainer container = kieServices.getKieClasspathContainer();
        // 获取session
        KieSession kieSession = container.newKieSession();

        Map<String, Object> globalMap = new HashMap<>();

        kieSession.setGlobal("globalMap", globalMap);

        Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("age", 30);
        conditionMap.put("testKey", "test");

        kieSession.insert(conditionMap);
//        kieSession.fireAllRules();
        kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("map_rule_test"));
        System.out.println(globalMap.get("testKey"));
        System.out.println(conditionMap.get("userScore"));
        System.out.println(conditionMap.get("sex"));
        kieSession.dispose();
    }

map_rule.drl

package rules;

import java.util.Map
global java.util.Map globalMap

rule "map_rule_test2"
//    no-loop true
//    lock-on-active
    when
        $map:Map($map["userScore"]<600)
    then
        modify($map){put("userScore", 500)};
        System.out.println("触发了规则:map_rule_test2");
end

rule "map_rule_test1"
//    no-loop true
//    lock-on-active
    when
        $map:Map()
        Object($map["age"]>20, $map["testKey"]=="test")
    then
        modify($map){put("userScore", 300),put("sex","女")};
        globalMap.put("testKey","testValue");
        System.out.println("触发了规则:map_rule_test1");
end

  1. 其中 map_rule_test2和map_rule_test1 分别注释了
    // no-loop true
    // lock-on-active

这种情况下,执行结果:只打印 触发了规则:map_rule_test2
在这里插入图片描述
因为 规则结果部分 modify($map){put(“userScore”, 500)}; 对map进行了修改,当没有 no-loop true 这个属性时,会触发再次执行该规则,条件得到满足,所以会继续执行。从而造成死循环,且test1不会被执行到。
由于规则执行死循环,test代码中的打印也不会触发到。

当 map_rule_test1 增加了属性 no-loop true 和 lock-on-active ,对结果无影响。

  1. map_rule_test1 和 map_rule_test2 的 no-loop true 和 lock-on-active 两个属性分别注释和放开的执行结果如下截图。
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

可以看到 当 两个规则,都加了 no-loop true 时,仍然会造成死循环。但任意一个加上 lock-on-active true 属性的时候,则可以终止死循环。当然执行结果是不同的。因为map的初始值 score属性为null,因此当test2规则加了属性的时候,由于test1的结果修改了属性,不会再次触发test2,所以执行结果没有触发test2这个规则。

activation-group 和 agenda-group

activation-group :该属性将若干个规则划分成一个组,统一命名。在执行的时候,具有相同activation-group 属性的规则中只要有一个被执行,其它的规则都不再执行。同时可以用类似salience之类属性来实现组内规则的执行优先级。
也就是说n个规则,具有相同的属性 activation-group ,但最多只有 1 个规则会被触发,当多个规则满足条件是,默认是按照drl书写的顺序第一个被触发,但可以使用 salience 属性来约定优先级。

agenda-group :若干个agenda-group分组,每个分组内有n个规则,在执行的时候,被focus的分组,组内的所有规则都可以被触发。

举例说明:
测试方法:

 @Test
    public void testGroup() {
        // 获取services
        KieServices kieServices = KieServices.Factory.get();
        // 获取container
        KieContainer container = kieServices.getKieClasspathContainer();
        // 获取session
        KieSession kieSession = container.newKieSession();
        // 注意这行,是在代码中指定focus的分组。
//        kieSession.getAgenda().getAgendaGroup("agenda002").setFocus();
        // 触发规则
//        kieSession.fireAllRules();
        int ruleCount = kieSession.fireAllRules(new RuleNameStartsWithAgendaFilter("rule-group"));
        System.out.println("执行了规则数量:" + ruleCount);
        kieSession.dispose();
    }

group_test.drl文件

package rules;

import java.util.Date

rule "rule-group-1"
    salience 10
    activation-group "testgroup"
    when

    then
        System.out.println("activation-rule-1 .. ");
end

rule "rule-group-2"
    salience 20
    activation-group "testgroup"
    when

    then
        System.out.println("activation-rule-2 .. ");
end

rule "rule-group-3"
    agenda-group "agenda001"
    when

    then
        System.out.println("agenda001-rule-3 .. ");
end

rule "rule-group-4"
    agenda-group "agenda001"
    auto-focus
    when

    then
        System.out.println("agenda001-rule-4 .. ");
end

rule "rule-group-5"
    agenda-group "agenda002"
    when

    then
        System.out.println("agenda002-rule-5 .. ");
end

rule "rule-group-6"
    agenda-group "agenda002"
    when

    then
        System.out.println("agenda002-rule-6 .. ");
end

drl文件定义了 6 个规则,其中 activation-group “testgroup” 属性的2个、 agenda-group "agenda001"属性的2个、agenda-group "agenda002"属性的2个。
rule-group-1、rule-group-2 属性为 activation-group “testgroup” ,最多只有其中一个能执行,例子中,使用 salience 属性,将 rule-group-2 的优先级提高了。因此 rule-group-1 是不会执行到的。

agenda-group “agenda001” 因为有 auto-focus 属性,因此 rule-group-3、rule-group-4会被执行
rule-group-5、rule-group-6则不会被执行。
执行结果截图如下:
在这里插入图片描述

但代码中加入 kieSession.getAgenda().getAgendaGroup(“agenda002”).setFocus();时,
rule-group-2、 rule-group-3、rule-group-4、rule-group-5、rule-group-6,都会被执行。
执行结果截图如下:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/194502.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

谷歌浏览器无法翻译此网页,解决方法?(谷歌浏览器无法翻译成中文,谷歌翻译,最新方法)

谷歌浏览器自带的翻译功能,对我们来说用处很大,但有的时候突然就会变成“无法翻译此网页”,针对此问题这里提供几种解决方案(翻译插件),如下: 方法1: 蓝奏云文件https://wwot.lanzouw.com/iFc7d0hmrtpg 访问密码:slee 方法2: 脚本之家

分布式事务的4种模式

分布式事务理论基础 解决分布式事务&#xff0c;也有相应的规范和协议。分布式事务相关的协议有2PC、3PC。 由于三阶段提交协议3PC非常难实现&#xff0c;目前市面主流的分布式事务解决方案都是2PC协议。 有些文章分析2PC时&#xff0c;几乎都会用TCC两阶段的例子&#xff0…

Java开发基础入门之Java基础中的Stack类及其常用方法

一、Stack类 1.Stack是Vector的一个子类&#xff0c;它实现标准的后进先出堆栈。Stack只定义了创建空堆栈的默认构造方法。 Stack() 2.Stack类里面主要实现的有以下的几个方法&#xff1a; (1)boolean empty( )方法是判断堆栈是否为空。 (2)Object peek( )方法是返回栈顶端…

一夜爆火的现象级产品ChatGPT,是AI突破还是昙花乍现?

导语 | 编写代码、翻译小说、参加考试……2022 年末&#xff0c;人工智能聊天机器人 ChatGPT 风靡全网。自 2016 年 AlphaGo 击败围棋世界冠军李世石后&#xff0c;ChatGPT 再次掀起了人工智能发展应用的高潮。它将会给我们带来哪些影响&#xff1f;人工智能的颠覆性应用是否即…

MyBatis 二级缓存整合Redis【学习记录】

二级缓存整合Redis 上篇文章介绍了MyBatis自带的二级缓存&#xff0c;但是这个缓存是单服务器工作&#xff0c;无法实现分布式缓存。那么什么是分布式缓存呢&#xff1f;假设现在有两个服务器1和2&#xff0c;用户访问的时候访问了服务器1&#xff0c;查询后的缓存就会放在服务…

酒店预订订单的分析与建模【决策树、xgboost】

酒店预订订单的分析与建模【决策树、xgboost】 本项目包含 1.数据处理 2.数据探索性分析 3.网格搜索对决策树、xgboost进行模型参数调优 4.基于五折交叉验证的决策树、xgboost模型预测 专栏和往期项目 &#x1f449;往期文章可以关注我的专栏 下巴同学的数据加油小站 会不…

《神经网络与深度学习》 邱希鹏 学习笔记(二)

正则化 所有损害优化的方法都是正则化。增加优化约束&#xff0c;干扰优化过程 优化约束包括 L1/L2约束&#xff0c;数据增强 干扰优化包括 随机梯度下降 权重衰减 提前停止 在上式中 y(n)为样本n&#xff0c;其展开形式为y^{(n)}为样本n&#xff0c;其展开形式为y(n)为样本…

【Axure教程】低代码可视化编辑器

低代码是一组数字技术工具平台&#xff0c;基于图形化拖拽、参数化配置等更为高效的方式&#xff0c;通过少量代码或不用代码实现数字化转型中的场景应用创新。例如在业务系统中&#xff0c;如果企业新增了一项业务&#xff0c;以往往往需要对系统继续开发和升级&#xff0c;但…

【Python学习笔记】9. Python3 解释器

前言 Linux/Unix的系统上&#xff0c;一般默认的 python 版本为 2.x&#xff0c;我们可以将 python3.x 安装在 /usr/local/python3 目录中。 Python3 解释器 Linux/Unix的系统上&#xff0c;一般默认的 python 版本为 2.x&#xff0c;我们可以将 python3.x 安装在 /usr/loca…

IDEA 常用插件跟配置提升开发效率

工欲善其事必先利其器 安装好 IntelliJ IDEA 后&#xff0c;进行如下的初始化操作&#xff0c;工作效率提升50倍。 一、插件 1. Codota 代码智能提示插件 只要打出首字母就能联想出一整条语句&#xff0c;这也太智能了&#xff0c;还显示了每条语句使用频率。原因是它学习了…

最全的视频转换器工具清单,这18款免费视频格式转换器记得收藏

审查和比较具有功能和定价的最佳视频转换器软件。从这个顶级付费和免费在线视频转换器工具列表中选择&#xff0c;以快速轻松地转换任何视频&#xff1a; 什么是视频转换器&#xff1f; 视频转换工具允许您将视频从一种格式转换为另一种格式。第一个商业上成功的视频格式是 Q…

11.1-股票基金历年收益率计算

文章目录1. 计算目标2. 关键问题3. 获取交易日历4. 逻辑编写1. 计算目标 我们想知道&#xff0c;一只股票标的&#xff0c;在之前的几年中&#xff0c;每一年的年化收益率是多少&#xff1f; 如果将每年的年化收益率进行求和汇总&#xff0c;截止到今年&#xff0c;总共年化收…

五、Mybatis详细教程

Mybatis概述 1 Mybatis概念 MyBatis 是一款优秀的持久层框架&#xff0c;用于简化 JDBC 开发 MyBatis 本是 Apache 的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code&#xff0c;并且改名为MyBatis 。2013年11月迁移到Github 官网&am…

Qt OpenGL(三十五)——Qt OpenGL 核心模式-点云(斯坦福兔子)

提示:本系列文章的索引目录在下面文章的链接里(点击下面可以跳转查看): Qt OpenGL 核心模式版本文章目录 Qt OpenGL(三十五)——Qt OpenGL 核心模式-点云(斯坦福兔子) 一、场景 我们在平时的项目中,有的时候会遇到,激光雷达等这些设置采集的数据集,不管是在机器人、…

【微服务】分布式缓存Redis

分布式缓存Redis基于Redis集群解决单机Redis存在的问题1.Redis持久化1.1.RDB持久化1.1.1.执行时机1.1.2.RDB原理1.1.3.小结1.2.AOF持久化1.2.1.AOF原理1.2.2.AOF配置1.2.3.AOF文件重写1.3.RDB与AOF对比2.Redis主从2.1.搭建主从架构2.2.主从数据同步原理2.2.1.全量同步2.2.2.增量…

UVM实战(张强)--- UART实例代码详细注解

目录一、整体的设计结构图二、各个组件代码详解2.1 DUT2.2 my_driver2.3 my_transaction2.4 my_env2.5 my_monitor2.6 my_agent2.7 my_model2.8 my_scoreboard2.9 my_sequencer2.10 base_test2.11 my_case02.12 my_case1一、整体的设计结构图 各个模块的基础介绍&#xff1a; &…

Spring核心——面向切面编程(AOP)

Spring核心——AOP&#xff08;Aspect-oriented programming&#xff09;一、概念二、作用三、AOP核心概念1.连接点&#xff08;JoinPoint&#xff09;2.切入点&#xff08;Pointcut&#xff09;3.通知&#xff08;Advice&#xff09;4.通知类5.切面&#xff08;Aspect&#xf…

c语言 结构体 动态内存 动态内存管理 模拟实现atoi 找单身狗 文件操作程序编译和链接 预处理 交换奇偶位 offsetof宏的实现 习题

结构体大小 【题目名称】 在32位系统环境&#xff0c;编译选项为4字节对齐&#xff0c;那么sizeof(A)和sizeof(B)是&#xff08; C &#xff09; 对齐数是取其较小值 struct A {int a;short b;int c;char d; }; struct B {int a;short b;char c;int d; };【题目内容】 A. 1…

小程序项目学习--第五章:项目实战一

第五章&#xff1a;项目实战一、 01_(了解)音乐小程序的项目介绍 坑关于Vant Weapp中组件引入未找到的解决方案 [ pages/main-music/main-music.json 文件内容错误] pages/main-music/main-music.json: [“usingComponents”][“van-search”]: “vant/weapp/search/index”…

阿里云们扎堆集结,数据库黄金时代到了?

配图来自Canva可画 作为全球数一数二的信息产业大国&#xff0c;我国在信息技术软硬件底层标准、架构、产品以及生态体系方面&#xff0c;长期被外商“卡脖子”&#xff0c;其中数据库市场更是长期被甲骨文等外商公司所占据。 近年来伴随着信创产业的高速发展&#xff0c;国内…