2010/8/27 Patricia Bogoevici <patriciabogoevici(a)yahoo.com>
Thx for your answer, Wolfgang.
While your advice makes sense, it does not help me. I cannot avoid using
OR. I use Drools for validation and I have some cases when I need to use OR
between conditions for evaluating an invalid state.
Well, I just wrote a substantial number of rules for data validation, and I
never felt the need to use "or". Care to post an example where you think you
can't avoid it?
Also, the solution you proposed works fine when the rule works with
one
object type only. If there are more than 1, it does not work.
Right, but or-ing two facts of entirely different types produces a rather
limited CE: you cannot, for instance, bind a LHS variable to such a CE. This
means that, later on and on the RHS, you don't know which of these actually
matched.
A workaround I see, would be to separate each of the condition from
OR,
into separate rule. But that adds the overhead of managing multiple rules.
True, and this is one (perhaps the only one) point in favour of "or".
In Expert, 5.1 manual, there is an example of how to chain expressions in
DSL, using OR. They mention mapping OR being sensible, but I wish there were
more details about that sensible thing.
--quote from the manual:
"
Example 4.77. Chaining DSL Expressions
There is a person called Bob who is happy
Or
There is a person called Mike who is sad
Of course this assumes that "Or" is mapped to the "or" conditional
element
(which is a sensible thing to do).
"
--end quote
Heh :)
What the author is trying to say here is that only a moron would map "Or" to
"and".
So, my question was:
Is any solution to chain conditions using eval, without having the rule
firing multiple times? From your answer, it seems not, and it seems that
will not be possible due to some Rete-level refactoring or optimization
(hope I got your message correct, I do not know how RETE works). Is that
true?
Yes. - As for Rete, you could read
this<http://www.jessrules.com/jess/docs/71/rete.html>.
The syntax of the rules is a little different, but you should get an idea
about the general principle and where optimization kicks in.
I believe, I can live with this limitation (rule fires multiple times).
You could avoid this, technically, by adding additional CEs, but if you can
live with it, it may not be worth the effort.
I only hope that there is no other implication, that I am not aware
of
right now.
I don't think there is.
-W
Thanks,
Patricia
--- On *Fri, 8/27/10, Wolfgang Laun <wolfgang.laun(a)gmail.com>* wrote:
From: Wolfgang Laun <wolfgang.laun(a)gmail.com>
Subject: Re: [rules-users] Drools Guvnor: Rule fires multiple times when
using eval and OR
To: "Rules Users List" <rules-users(a)lists.jboss.org>
Date: Friday, August 27, 2010, 3:32 AM
My advice is: Try to avoid the Conditional Element "or". From the
"Expert" manual: "The engine actually has no understanding of the
Conditional Element 'or',..." So, if the engine doesn't
"understand"
it, how can we? ;-)
More seriously now, there is general consent that a CE "or" should
ultimately result in two (or more) disjunct (sub-)rules, firing
independently. But this clear strategy is apparently countered (you
could even say marred) by some Rete-level refactoring or optimization.
Anyway, in your case, a "healthy" solution is to write
rule "any blank"
when
Asset( eval( StringUtil.isBlank(name) || StringUtil.isBlank(tag) ))
then
//...
end
-W
2010/8/27 Patricia Bogoevici
<patriciabogoevici@yahoo.com<http://mc/compose?to=patriciabogoevici@yahoo.com>
>
>
> Hi all,
>
> I am using Drools Guvnor 5.1. I created a rule that uses eval, and OR for
conditions. When I tested the rule, I noticed in the log, that actually the
rule fired twice.
> The error seemed to be caused by eval and or combination. When I re-wrote
the rule to not use eval, it fired only once. Also, I re-wrote the rule to
use eval, but AND for conditions, again, the rule fired once as expected.
> I wonder if there is any other rule attribute besides no-loop, and
lock-on-active that I can use to not have the rule firing twice. Or if there
is any way to make the rule not firing twice.The main problem for me, is
that after the rule is executed, there are 2 objects created.
>
> Below is the rule test scenario, and audit log:
>
>
> Rule with eval and OR, rule fires twice
> rule "test_rule_1"
> lock-on-active true
> no-loop true
> dialect "mvel"
> when
> Asset(eval(StringUtils.isBlank(name)))
> or
> Asset(eval(StringUtils.isBlank(tag)))
> then
> Assetfact0 = new Asset();
> fact0.setStatus( "INVALID" );
> insert(fact0 );
> end
>
> Audit log:
> OBJECT ASSERTED value:Asset( model=MODEL, status=null, tag=null,
classification=null, name=null, serial=null ) factId: 1
> FIRING rule: [test_rule_1] activationId:test_rule_1 [1] declarations:
> OBJECT ASSERTED value:Asset( model=null, status=INVALID, tag=null,
classification=null, name=null, serial=null ) factId: 2
> FIRING rule: [test_rule_1] activationId:test_rule_1 [1] declarations:
> OBJECT ASSERTED value:Asset( model=null, status=INVALID, tag=null,
classification=null, name=null, serial=null ) factId: 3
>
> Rule with eval and AND, rule fires only once:
> rule "test_rule_2"
> lock-on-active true
> no-loop true
> dialect "mvel"
> when
> Asset(eval(StringUtils.isBlank(name)))
> Asset(eval(StringUtils.isBlank(tag)))
> then
> Assetfact0 = new Asset();
> fact0.setStatus( "INVALID" );
> insert(fact0 );
> end
>
> Audit log:
> OBJECT ASSERTED value:Asset( model=MODEL, status=null, tag=null,
classification=null, name=null, serial=null ) factId: 1
> FIRING rule: [test_rule_2] activationId:test_rule_2 [1] declarations:
> OBJECT ASSERTED value:Asset( model=null, status=INVALID, tag=null,
classification=null, name=null, serial=null ) factId: 2
>
> Rule with simple string validation instead of eval. The rule fires only
once.
>
> rule "test_rule_3"
> lock-on-active true
> no-loop true
> dialect "mvel"
> when
> Asset(name=="''")
> or
> Asset(tag=="'''")
> then
> Assetfact0 = new Asset();
> fact0.setStatus( "INVALID" );
> insert(fact0 );
> end
>
>
> Audit log:
> OBJECT ASSERTED value:Asset( model=MODEL, status=null, tag=null,
classification=null, name='', serial=null ) factId: 1
> FIRING rule: [test_rule_3] activationId: test_rule_3 [1] declarations:
> OBJECT ASSERTED value:Asset( model=null, status=INVALID, tag=null,
classification=null, name=null, serial=null ) factId: 2
>
>
> Thanks,
> Patricia
>
>
> _______________________________________________
> rules-users mailing list
> rules-users@lists.jboss.org<http://mc/compose?to=rules-users@lists.jboss.org>
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org<http://mc/compose?to=rules-users@lists.jboss.org>
https://lists.jboss.org/mailman/listinfo/rules-users
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users