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

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 文档以及我拥有的一本书,但没有发现这些示例在这方面非常具有说明性。

最佳答案

'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 对象也是你 session 中的事实,你可以这样写:
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

希望能帮助到你,

关于drools - 我不能在分配给变量的集合上使用 contains 运算符吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20063145/

10-13 04:33