[rules-users] Not looping the rule when more then 1 valid match is found

Edson Tirelli tirelli at post.com
Mon Jan 26 09:15:28 EST 2009


    It depends on what addResult() does and if you need the actual values of
A() and B(). Forward chaining engines are "data driven" and as so, changes
to your data (facts) are what cause the engine to cancel activations.
    So, if you don't need the actual values of A and B, use "exists":

exists( A( $c : code) and B(parentCode == $c) )

     Although, since addResult() uses them as parameters, I guess it is not
the case. So, does addResult() changes your facts in any way? Lets say
addResult() creates a Association fact between A and B, you could write:

when
    $a : A( $c : code )
    $b : B( parentCode == $c )
    not( Association( <you can constraint something here if you want> ) )
then

    Or if A and B have an attribute that is set by addResult()

when
    $a : A( $c : code )
    $b : B( parentCode == $c )
    not( A( resultAdded == true ) and B( resultAdded == true ) )
then

    So, in the end, rules in forward chaining are about taking actions
triggered by a given data state. Write rules that react to the expected
state and change the state accordingly.

    Oh, obviously you can always use fire limit or halt to "pause" the
execution if that is what you want too.

session.fireAllRules( 1 ); // fires only one rule and returns control to the
application

    Or:

when
    $a : A( $c : code )
    $b : B( parentCode == $c )
then
    addResult( $a, $b );
    drools.halt();
end

    After firing the above rule, stop firing rules and return the control to
the application. Above situations will only pause the engine. All
activations that were not fired will remain in the agenda waiting for the
next call to fireAllRules() to be fired.

    []s
    Edson


2009/1/26 Maxim Veksler <maxim.veksler at gmail.com>

> Hello group,
>
> Assuming I have the rule:
>
> rule "try not to loop"
>     when
>         $a : A($code : code)
>         $b : B(parentCode == $code)
>     then
>         addResult($a, $b);
> end
>
>
> and the classes:
>
> public class A {
> int code;
> }
>
> public class B {
> int parentCode;
> }
>
>
>
> I insert into the WM 4 objects :
> A(code = 1), A(code = 2);
> B(parentCode=1), B(parentCode = 2);
>
> I would like that after calling fireAllRules() only 1 match will be
> returned. I have no preference regarding which one, in the current situation
> 2 matches are returned.
>
> Anyone could suggest a method to enforce this ?
>
>
> Thank you,
> Maxim.
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>


-- 
 Edson Tirelli
 JBoss Drools Core Development
 JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090126/7be16277/attachment.html 


More information about the rules-users mailing list