[rules-users] Rules with from always loops (Drools 4.0.7)

Edson Tirelli tirelli at post.com
Fri Feb 13 09:47:41 EST 2009


   Przemyslaw,

   There are no plans to change the behavior for 5.0. In 5.1 with a feature
that we call "true modify" we may be able to improve the situation.

   Let me explain it in a better way: the "from" expression is a black box
for the engine. You can call *any* code in that expression and a common case
is to use the expression to pull data from a database on-demand. Since it is
a black box, the engine assumes that every time the expression is
re-evaluated you are bringing new data into the engine and as so it creates
new fact handles for it. A modify in most Rete engines is a retract+assert
and it is like this in drools 4-5 too. So, when you modify the fact that is
written before the "from" in LHS, it will retract all facts previously
created by "from", re-evaluate it, and create new fact handles for them.

   Since no-loop and lock-on-active use the fact handles to determine when a
fact is equals to another, you get this side effects.

   A way to work around the problem is have your data in the working memory,
reducing the usage of from. Another way to work around the problem is if you
can write the patterns that use from before the patterns that are modified
by your rules. E.g:

Rule X
when
    $a : A()
    $b : B() from somewhere
    $c : C()
    $d : D()
then
    // ...
end

   If $a is modified by any rule or by the application, the whole LHS of the
above rule is re-evaluated. If $c is modified, only $c and $d are
reevaluated, and so on. Placing the facts that you modify frequently "after"
any pattern that uses "from" is a way to prevent this behavior. It is not
always possible, I know, but it helps with this side-effect whenever
possible.

   []s
   Edson



2009/2/13 Przemysław Różycki <P.Rozycki at amg.net.pl>

> Edson, Michal,
>
> thank you for your answers. If I understand well, Drools can't recognize if
> the $fact2 has been changed by the current rule or not.
> Anyway, I found such a behaviur as a bit confusing, so my second question
> is, is it a desirable behaviour? And is it going to be changed or left as
> is?
>
> Best regards
> Przemyslaw
>
> Edson Tirelli pisze:
>
>>
>>    Michal is correct. Every time a "from" expression is executed, Drools
>> does not know if a result for the expression is new or not and it creates a
>> new fact handle, bypassing the no-loop.
>>
>>    []s
>>    Edson
>>
>> 2009/2/13 Michal Bali <michalbali at gmail.com <mailto:michalbali at gmail.com
>> >>
>>
>>    This is what I think is going on:
>>    when you call update($fact1) it will first retract $fact1 which in
>>    turn retracts $fact2, the $fact1 is then inserted again which cause
>>    another 'from' evaluation and a NEW $fact2 is pulled -> no-loop
>>    doesn't work because Drools sees it as different data
>>    I am just guessing here ...
>>
>>
>>    On Fri, Feb 13, 2009 at 9:12 AM, Przemysław Różycki
>>    <P.Rozycki at amg.net.pl <mailto:P.Rozycki at amg.net.pl>> wrote:
>>
>>        Hello,
>>
>>        Doesn't really anyone have any logical explanation of why such a
>>        rule loops?
>>
>>        To remind the rule:
>>
>>
>>        rule "Hello World"
>>               no-loop
>>               when
>>                       $fact1 : MyFactBean1(  )
>>                       $fact2 : MyFactBean2(  ) from $fact1
>>               then
>>                       System.out.println("Fired!!!");
>>                       update($fact1);
>>        end
>>
>>        If I don't use 'from' everything is fine.
>>        I really didn't find in a documentation, that 'from' causes any
>>        special behaviour of the rule looping.
>>
>>
>>        Best regards,
>>        --        Przemysław Różycki
>>        AMG.net, A Bull Group Company
>>        ul. Łąkowa 29
>>        90-554 Łódź
>>        www.amg.net.pl <http://www.amg.net.pl>
>>
>>
>>
>>
>>        Przemysław Różycki pisze:
>>
>>            Thanks for answer, Vikrant, but I don't think so. I have
>>            always used no-loop without any additions and it worked
>>            (when I didn't use from). BTW, if you comment a condition
>>            with from, then the rule does not loop. I also checked with
>>            "no-loop true" and result is the same.
>>
>>            Best regards,
>>            Przemek
>>
>>            Vikrant Yagnick pisze:
>>
>>                I have to quickly recheck the documentation by I think
>>                you have to write "no-loop true". Just writing no-loop
>>                defaults to false I guess.
>>
>>                -----Original Message-----
>>                From: rules-users-bounces at lists.jboss.org
>>                <mailto:rules-users-bounces at lists.jboss.org>
>>                [mailto:rules-users-bounces at lists.jboss.org
>>                <mailto:rules-users-bounces at lists.jboss.org>] On Behalf
>>                Of Przemyslaw Rózycki
>>                Sent: Tuesday, February 10, 2009 3:24 PM
>>                To: Rules Users List
>>                Subject: [rules-users] Rules with from always loops
>>                (Drools 4.0.7)
>>
>>                Hello,
>>
>>                why does such a rule always loop? Even if no-loop is used?
>>
>>                rule "Hello World"
>>                       no-loop
>>                       when
>>                               $fact1 : MyFactBean1(  )
>>                               $fact2 : MyFactBean2(  ) from $fact1
>>                       then
>>                               System.out.println("Fired!!!");
>>                               update($fact1);
>>                end
>>
>>                The full code of my test application and drl is attached.
>>
>>                Best regards,
>>                --                Przemysław Różycki
>>                AMG.net, A Bull Group Company
>>                ul. Łąkowa 29
>>                90-554 Łódź
>>                www.amg.net.pl <http://www.amg.net.pl>
>>
>>
>>                MASTEK LTD.
>>                Mastek is in NASSCOM's 'India Top 20' Software Service
>>                Exporters List.
>>                In the US, we're called MAJESCOMASTEK
>>
>>
>>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>                Opinions expressed in this e-mail are those of the
>>                individual and not that of Mastek Limited, unless
>>                specifically indicated to that effect. Mastek Limited
>>                does not accept any responsibility or liability for it.
>>                This e-mail and attachments (if any) transmitted with it
>>                are confidential and/or privileged and solely for the
>>                use of the intended person or entity to which it is
>>                addressed. Any review, re-transmission, dissemination or
>>                other use of or taking of any action in reliance upon
>>                this information by persons or entities other than the
>>                intended recipient is prohibited. This e-mail and its
>>                attachments have been scanned for the presence of
>>                computer viruses. It is the responsibility of the
>>                recipient to run the virus check on e-mails and
>>                attachments before opening them. If you have received
>>                this e-mail in error, kindly delete this e-mail from
>>                desktop and server.
>>
>>  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>>
>>                _______________________________________________
>>                rules-users mailing list
>>                rules-users at lists.jboss.org
>>                <mailto:rules-users at lists.jboss.org>
>>                https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>            _______________________________________________
>>            rules-users mailing list
>>            rules-users at lists.jboss.org <mailto:
>> rules-users at lists.jboss.org>
>>            https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>
>>        _______________________________________________
>>        rules-users mailing list
>>        rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>>        https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>
>>    _______________________________________________
>>    rules-users mailing list
>>    rules-users at lists.jboss.org <mailto: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 <http://www.jboss.com>
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
> _______________________________________________
> 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/20090213/f153909c/attachment.html 


More information about the rules-users mailing list