我可以不在分配给变量的集合上使用包含运算符吗?

bri*_*cks 4 drools

如果有人能向我解释为什么这是非法的,我将不胜感激:

rule "some rule name"

when 
    $a : A($bset : bset)
    $bset contains B(x == "hello")
then
    //do something
end
Run Code Online (Sandbox Code Playgroud)

哪里:

public class A {
private Set<B> bset = new HashSet<B>();
//getters and setters for bset
//toString() and hashCode for A

public static class B {
private String x
//getters and setters for x
//toString() and hashCode() for B
}
}
Run Code Online (Sandbox Code Playgroud)

来自Drools eclipse插件的错误不是很有帮助.它提供以下错误:

[ERR 102]第23:16行在规则"某些规则名称"中输入'包含'不匹配

该错误出现在"bset contains ..."的行上

我已经搜索了Drools文档,以及我所拥有的一本书,并且没有发现这些例子在这方面非常具有说明性.

Est*_*rti 6

'contains'是必须在模式中使用的运算符.$bset contains B(x == "hello")在这种情况下,它不是有效的模式.有几种方法可以实现您的目标.这是其中之一:

rule "some rule name"
when 
    $a: A($bset : bset)
    $b: B(x == "hello") from $bset
then
    //you will have one activation for each of the B objects matching 
    //the second pattern
end
Run Code Online (Sandbox Code Playgroud)

另一个:

rule "some rule name"
when 
    $a: A($bset : bset)
    exists (B(x == "hello") from $bset)
then
    //you will have one activation no matter how many B objects match 
    //the second pattern ( you must have at least one of course)
end
Run Code Online (Sandbox Code Playgroud)

如果你想看看如何使用contains操作,如果B对象也是你的会话中的事实,你可以写这样的东西:

rule "some rule name"
when 
    $b: B(x == "hello")
    $a: A(bset contains $b)
then
    //multiple activations
end
Run Code Online (Sandbox Code Playgroud)

要么:

rule "some rule name"
when 
    $b: B(x == "hello")
    exists( A(bset contains $b) )
then
    //single activation
end
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你,