如何在侧流口水中定义变量并为其添加值 [英] How to define variable in side drools and add values to them

查看:94
本文介绍了如何在侧流口水中定义变量并为其添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在DRL文件中定义变量并向其添加值,这些文件可在规则之间用作静态资源。

How to define variable and add values to it inside DRL file that can be used between the rules as a static resource.

我尝试使用global关键字,但是在添加时向其中添加值时,不会像文档中提到的那样在Working Memory内部受到影响。就我而言,我不是从应用程序端添加它。

I tried to use global keyword but when add I add values to it i will not be effected inside Working Memory as it mentioned in documentation. And in my case i conn't add it from Application side.

示例:

 global java.util.List myList;

function java.util.List initMyList() {
 List list = new ArrayList();
 list.add(1);
 return list;
}

rule "initListRule"
salience 1001
 when
  eval(myList == null)
then
 myList = initMyList();
end

rule "checkIfListIsStillEmptyRule"
 salience 1000
when
 eval(myList != null)
then
 System.out.println("MyList is Not null");
end

由于全局变量未存储在唤醒内存中,因此myList始终为null不从应用程序一侧提供。有没有其他选择来定义变量并将其填充到DRL中?

as global are not stored in woking memory then myList will be always null as it is not provided from Application side. is there any alternative to define variables and fill them in DRL ?

推荐答案

DRL全局变量不会动态评估,因此您的规则 checkIfListIsStillEmptyRule将不会触发。

A DRL global is not evaluated dynamically and therefore your rule "checkIfListIsStillEmptyRule" will not fire.

您可以这样做

rule "initListFact"
when
    not List()
then
    insert( new ArrayList() );
end
rule "checkThatThereIsAnEmptyList"
when
    $list: List( size == 0 )
then
    modify( $list ){ add( "something" ) }
end

如果您不需要观察全局变量的变化您可以以很高的显着性来初始化它。它将作为资源提供,但是您不能基于规则状态使用规则条件。

If you don't need to observe changes of a global you can initialize it in a rule with very high salience. It will be available as a resource but you cannot base rule conditions on its state.

global list myList
rule "initListRule"
salience 9999999999
when
then
    myList = new ArrayList();
end

您可以使用多个全局变量:

You can use more than one global:

global List myListOne
global List myListTwo
rule "initListsRule"
salience 9999999999
when
then
    myListOne = new ArrayList();
    myListTwo = new ArrayList();
end

如果您需要对变化做出反应,就无法绕开事实。

If you need to react to changes, there's no way around facts.

declare NamedList
    name: String
    list: ArrayList
end
rule createListOne
when
    not NamedList( name == "one" )
then
    insert( new NamedList( "one", new ArrayList() ) );
end
rule "checkIfListOneIsStillEmpty"
when
    $nl: NamedList( name == "one", eval( list.size() == 0 ) )
then
    modify( $nl ){vgetList().add( "something" ) }
end

这篇关于如何在侧流口水中定义变量并为其添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆