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

时间:2013-11-19 04:19:58

标签: drools

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

rule "some rule name"

when 
    $a : A($bset : bset)
    $bset contains B(x == "hello")
then
    //do something
end

其中:

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
}
}

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

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

错误出现在“bset contains ...”

的行上

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

1 个答案:

答案 0 :(得分: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

另:

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

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

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

或:

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

希望它有所帮助,

相关问题