[rules-users] Why Using "from" Always Return A New Fact?

Swindells, Thomas TSwindells at nds.com
Wed Jan 19 05:27:03 EST 2011


In reality it doesn't return any fact, it returns each of the values currently in the list held by the bound fact ($p).
Each time the rule is re-evaluated (due to $p changing) it re-iterates over the list again (as it doesn't know if the list has changed or not). Drools effectively doesn't know anything about the $a's returned from the list nor does it keep track of them. Hence when re-evaluating the rule it can't know if it is the same set of values that matches last time or a different set of values.

Thomas

> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org [mailto:rules-users-
> bounces at lists.jboss.org] On Behalf Of hyjshanghai
> Sent: 19 January 2011 10:21
> To: rules-users at lists.jboss.org
> Subject: [rules-users] Why Using "from" Always Return A New Fact?
>
>
> I am trying to understand why care should be taken to use "from" and
> "lock-on-active" together, as is described in "Section 4.8.3.8. Conditional
> Element from" of
> http://downloads.jboss.com/drools/docs/5.1.1.34858.FINAL/drools-
> expert/html/ch04.html
> the Drools Reference Document .
>
> It says "the use of from returns a new fact for all intents and purposes
> each time it is evaluated." Why is it true?
>
> Below is the full related-text copied from the Drools Reference Document for
> your reference:
> === BEGIN ===
> You must take caution, however, when using from, especially in conjunction
> with the lock-on-active rule attribute as it may produce unexpected results.
> Consider the example provided earlier, but now slightly modified as follows:
>
> rule "Assign people in North Carolina (NC) to sales region 1"
> ruleflow-group "test"
> lock-on-active true
> when
>     $p : Person( )
>     $a : Address( state == "NC") from $p.address
> then
>     modify ($p) {} #Assign person to sales region 1 in a modify block
> end
>
> rule "Apply a discount to people in the city of Raleigh"
> ruleflow-group "test"
> lock-on-active true
> when
>     $p : Person( )
>     $a : Address( city == "Raleigh") from $p.address
> then
>     modify ($p) {} #Apply discount to person in a modify block
> end
>
> In the above example, persons in Raleigh, NC should be assigned to sales
> region 1 and receive a discount; i.e., you would expect both rules to
> activate and fire. Instead you will find that only the second rule fires.
>
> If you were to turn on the audit log, you would also see that when the
> second rule fires, it deactivates the first rule. Since the rule attribute
> lock-on-active prevents a rule from creating new activations when a set of
> facts change, the first rule fails to reactivate. Though the set of facts
> have not changed, the use of from returns a new fact for all intents and
> purposes each time it is evaluated.
> === END ===
>
> --
> View this message in context: http://drools-java-rules-
> engine.46999.n3.nabble.com/Why-Using-from-Always-Return-A-New-Fact-
> tp2286393p2286393.html
> Sent from the Drools - User mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users


**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************




More information about the rules-users mailing list