Drools规则左侧的“ this”等效关键字

问题描述

给定一个配置类Config,其中包含属性excludeShifts(这是Shift类的列表),是否存在像“ this”这样的关键字表示匹配过程中的当前对象?例如,

rule "Match but not if excluded"
  when
     $config : Config(...)  // additional matching criteria deleted

     $shift : Shift(this not memberOf $config.excludeShifts,...     // additional criteria deleted
                    )
     ... // additional criteria deleted
  then
     ...
end

我认识到在功能上,我可以使用以下命令来实现此匹配:

   $shift : Shift()
   $config : Config(excludeShifts not contains $shift
                   )

解决方法

this是一个公认的关键字,其使用方式与您指示的方式相同,用作对要评估的当前对象的引用。

一些例子:

// An even integer in working memory
$even: Integer( this % 2 == 0 )

// A string with a specific value
$animal: String(this in ("cat","dog","fish"))

// A value from a map
Map( $value: this['key'] )

您的示例与您编写的示例一样有效。

rule "Match but not if excluded"
when
  $config : Config($excluded: excludedShifts)
  $shift : Shift(this not memberOf $excluded)
then
end

我拨通了documentation,但看不到是否曾经this被用作关键字(尽管要搜索它有点困难。)在一些示例中使用了它。 ,例如,在forall运算符示例代码中。正如Esteban在评论中所建议的那样,欢迎您打开错误报告或提交文档修订以纠正这种疏忽。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...