Drools:在规则的LHS中匹配数组中的本地字符串 [英] Drools: Match local string from array in LHS of rule

查看:452
本文介绍了Drools:在规则的LHS中匹配数组中的本地字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建规则,以检查规则LHS中的字符串列表中是否包含字符串。要比较的字符串列表是恒定的,并且在创建规​​则时就已知道,但是在许多相似的规则中有所不同。

I am trying to create rules in which I check if a string is in a list of strings in the LHS of my rule. The list of strings to compare to is constant and known at the time of rule creation, but differs among many similar rules.

一个示例规则就是这样(我的方式正在写它们):

An example rule is as such (the way I am writing them now):

rule "ruleX"
when
    $a: MyObject() 
then
    List<String> list = new ArrayList<String>();
    list.add("ABC");
    list.add("DEF");

    if (list.contains($a.getString()) {
        //do stuff
    }
end

这将不会利用PHREAK引擎中存在的一些优化,因为RHS中有一些工作可以用来区分通过LHS的规则。

This will not take advantage of the some of the optimizations present in the PHREAK engine, because there is work done in the RHS that could be used to distinguish rules via the LHS.

在许多类似规则中,列表的内容各不相同。是否有更好的方法可以做到这一点?局域网数据库调用会更快。

The contents of "list" vary over many of these similar rules. Is there a better way to do this? I can't imagine that a LAN database call would be faster.

推荐答案

这可以简单地重写为:

rule "ruleX"
when
    MyObject( string in ("ABC", "DEF") ) 
then
    //do stuff
end

请注意,之后的列表在也可以包含(以前)绑定变量。

Note that the list after in could also contain (previously) bound variables.

如果您在集合中有值,也可以编写

If you have the values in a collection, you can also write

rule "ruleY"
when
    Data( $los: listOfStrings )
    MyObject( string memberOf $los ) 
then
    //do stuff
end
rule "ruleY"
when
    MyObject( $s: string ) 
    Data( listOfStrings contains $s )
then
    //do stuff
end

如果列表可以从某处提取(例如,

This might be preferable if the lists can be extracted from somewhere (e.g. a DB) and wrapped into suitable facts, to be inserted up front.

请注意,技术细节在Drools参考手册中有详细记录。

Note that the technical details are well documented in the Drools reference manual.

这篇关于Drools:在规则的LHS中匹配数组中的本地字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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