Can I not use contains operator on a collection assigned to a variable?

drools

Solution

'contains' is an operator that must be used inside a pattern. `$bset contains B(x == "hello")` is not a valid pattern in this case. There are a couple of ways to achieve what you are trying to do. This is one of them:

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

Another:

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

If you want to see how `contains` operation is used, and if the B objects are also facts in your session, you can write something like this:

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

or:

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

Hope it helps,

Problem

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 } } ``` The error from the Drools eclipse plugin is not very helpful. It provides the following error: [ERR 102] Line 23:16 mismatched input 'contains' in rule "some rule name" The error appears on the line with "bset contains..." I have searched through the Drools documentation, as well as a book that I have, and have not found the examples to be very illustrative in this regard.

Original source