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

hyjshanghai hyjshanghai at gmail.com
Wed Jan 19 05:21:26 EST 2011


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.



More information about the rules-users mailing list