[rules-dev] Locating objects by query/BC

Wolfgang Laun wolfgang.laun at gmail.com
Sat Jun 25 06:22:24 EDT 2011


// This rule prints all objects in WM.
rule findObject
when
    $o: Object()
then
    System.out.println( "Object: " + $o );
end

Happily I get
    Object: 42
    Object: a String object
    Object: org.drools.reteoo.InitialFactImpl at 4dde85f0

Now the query way:
    query object( Object o )
        o := Object()
    end
    rule findObjectByQuery
    when
        object( $a ; )
    then
        System.out.println( "Object by query: " + $a );
    end

Surprisingly, I get just one:
    Object by query: org.drools.reteoo.InitialFactImpl at 4dde85f0

A marvellous effect can be observed when the condition in rule
findObjectByQuery is modified to
    when
        Object()
        object( $a ; )
    then
I'd have guessed that the above result would appear repeatedly, once for
each Object in WM,
but that's not so. For N-1 facts the rule fires N*(N+1)/2 times, showing the
"initial object"
N times, the first inserted object N-1 times, the second one N-2 times and,
finally, the last one
once.
Object by query: 42
Object by query: org.drools.reteoo.InitialFactImpl at 4dde85f0
Object by query: a String object
Object by query: 100
Object by query: 42
Object by query: org.drools.reteoo.InitialFactImpl at 4dde85f0
Object by query: a String object
Object by query: org.drools.reteoo.InitialFactImpl at 4dde85f0
Object by query: a String object
Object by query: org.drools.reteoo.InitialFactImpl at 4dde85f0

-W
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-dev/attachments/20110625/c3c1ab32/attachment.html 


More information about the rules-dev mailing list