Can I use the contains statement in the collection assigned to a variable?

I would appreciate it if someone could explain to me why this is illegal:

rule "some rule name"

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

Where:

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 plugin error is not very useful. It provides the following error:

[ERR 102] Line 23:16 The unused input "contains" in the rule "some rule name"

The error appears in the line with "bset contains ..."

I looked through the Drools documentation, as well as the book I have, and did not find any examples to be very revealing in this regard.

+4
source share
1 answer

'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

, ,

+6

All Articles