Drools高级属性之global全局变量

Drools高级属性之global全局变量

定义

在我们编写drools的规则文件的时候,有些时候需要用到全局变量,那么这个该怎么实现呢?有哪些注意事项呢?

官方定义为

Global variables in DRL files typically provide data or services for the rules, such as application services used in rule consequences, and return data from rules, such as logs or values added in rule consequences. You set the global value in the working memory of the Drools engine through a KIE session configuration or REST operation, declare the global variable above the rules in the DRL file, and then use it in an action (then) part of the rule. For multiple global variables, use separate lines in the DRL file.

在这里插入图片描述

特点

全局变量的特点

  1. 使用global来定义全局变量,它可以为规则提供数据和服务。
  2. 全局变量并不会被写入到工作内存中,因此我们不可放到规则的约束条件中,即when的后面,除非这个全局变量是常量。
  3. 如果不同的包中存在相同标识符的常量,那么我们的常量必须是相同的类型,以便可以引用到相同的值。官网原文: If you declare global variables with the same identifier in multiple packages, then you must set all the packages with the same type so that they all reference the same global value.
  4. 尽量不要使用全局变量在规则之间传递参数,如果需要在规则之间传递参数,我们应该向工作内存中插入Fact对象。

示例

global关键字可以用来在规则文件中定义全局变量,它可以让应用程序中的对象在规则文件中都能够被访问。可以用来为规则文件提供数据或服务,从而贯穿整个规则文件的执行过程。该关键字的使用方式在下面的例子中会做介绍,我们编写一个规则文件,文件名为globalrule.drl,具体代码如下:

package rules.globals

import java.util.List;
import java.util.ArrayList;
import com.ppl.demo.entity.Account;

//此处定义一个全局计算数
global java.lang.Integer num;
//这里定义了一个普通的javaBean
global Account account;
//这里定义了一个简单的List集合
global List<String> list;

rule "rules-global-1"

	    when
        then
           //计算只在当前规则中有效
           num=num+100;
           System.out.println("这是规则rules-global-1的用户balance:"+account.getBalance());
           list.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Drools中,我们可以将复杂的function直接写在全局变量中。下面是一个示例: ``` package com.example.drools; import java.util.List; global java.util.function.Predicate<Integer> isMultipleOfThree; rule "Print Numbers" when $numbers: List(size > 0) then for (Integer number : $numbers) { if (isMultipleOfThree.test(number)) { System.out.println(number + " is a multiple of three."); } else { System.out.println(number + " is not a multiple of three."); } } end ``` 在这个规则文件中,我们定义了一个全局变量`isMultipleOfThree`,它是一个`Predicate<Integer>`,用于判断一个整数是否是3的倍数。在`Print Numbers`规则中,我们将列表中的每个数传递给`isMultipleOfThree`,并根据返回值打印出相应的信息。 我们可以在DroolsJava API中设置这个全局变量,例如: ``` KieServices kieServices = KieServices.Factory.get(); KieFileSystem kfs = kieServices.newKieFileSystem(); // 加载规则文件 kfs.write(ResourceFactory.newClassPathResource("rules.drl")); KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll(); KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId()); // 设置全局变量 kieContainer.getKieSessionConfiguration().setOption(ClockTypeOption.get("pseudo")); kieContainer.getKieSessionConfiguration().setOption(TimerJobFactoryOption.get("trackable")); KieSession kieSession = kieContainer.newKieSession(); // 设置全局变量 kieSession.setGlobal("isMultipleOfThree", new Predicate<Integer>() { @Override public boolean test(Integer number) { return number % 3 == 0; } }); List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); kieSession.insert(numbers); kieSession.fireAllRules(); ``` 在这个示例中,我们将一个`Predicate`对象作为全局变量`isMultipleOfThree`传递给Drools的会话。在规则中,我们使用`isMultipleOfThree.test(number)`来判断一个数是否是3的倍数。 需要注意的是,如果我们使用全局变量的方式来定义复杂function,那么这些function只能使用Java标准库中的类和方法,不能使用Drools中的API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值