Using a fact as a guard to block alternatives is tricky. For one thing, you
cannot use a single "global" fact. (When would you reset this fact to the
initial state so that the whole mechanism can work again for the next fact
set to be processed by this rule set?) So, you might consider inserting a
flag fact along with one of the facts playing the key role in all of the
conditions. But what if the participating facts change, and this "if set"
should be activated again? How would you reset or recreate the flag fact?
Also, the precedence implied by the ordering of the else branches would have
to be expressed in the rules, using salience.
So, the solution Greg has outlined is the one to pursue. As a general rule,
keep in mind that
if( cond ){
...;
} else {
assert( ! cond );
...;
}
and
if( cond1){ if( cond2 ){ assert( cond1 && cond2 );...;
Thus, you can collect the precondition for each if branch that is to become
a consequence of a rule, and the corresponding precondition is to be
implemented as the left hand side.
-W
On 6/16/09, Greg Barton <greg_barton(a)yahoo.com> wrote:
Note to drools devs: I didn't put the original poster up to this. :)
Globals should be considered immutable, so shouldn't be used in this way.
You could do it with an object asserted in working memory, though.
Just as good, though, you can use this kind of pattern to replicate else
logic:
rule "EverythingMatches"
when
Condition0
Condition1
Condition2
then
...
end
rule "ElseAtCondition2"
when
Condition0
Condition1
!Condition2
then
...
end
rule "ElseAtCondition1"
when
Condition0
!Condition1
then
...
end
rule "ElseAtCondition0"
when
!Condition0
then
...
end
--- On Mon, 6/15/09, Scott Reed <sreed(a)avacoda.com> wrote:
> From: Scott Reed <sreed(a)avacoda.com>
> Subject: Re: [rules-users] Help -- Conditional Logic in DRL
> To: "Rules Users List" <rules-users(a)lists.jboss.org>
> Date: Monday, June 15, 2009, 10:19 PM
> Looks like 5 rules with a global flag
> might work:
>
> global boolean flag = false;
>
> rule "name1"
> when
> (condition1 == true )
> (flag == false)
> then
> consequence1();
> flag = true;
> end
>
> ...
>
> rule "name5"
> when
> (condition5 == true )
> (flag == false)
> then
> consequence5();
> flag = true;
> end
>
> Does that work for you?
>
> Vishy [6/15/2009 11:06 PM] wrote:
> > Hi All,
> >
> > I am new to drools...and am trying to figure out as to
> how I can
> > represent the following logic in a DRL file.
> >
> > if (condition1) {
> >
> > if (condition2) {
> >
> > } else if (condition3) {
> >
> > } else if (condition 4) {
> >
> > } else if (condition 5) {
> >
> > }
> >
> > }
> >
> > In DSL, a typical rule is represented as follows:
> >
> > rule "name"
> >
> > when
> > condition is true
> > then
> > consequence
> >
> > end
> >
> > How can I represent the above mentioned if - else
> logic in DSL? Or is
> > it not possible?
> >
> > If it is not, can you please suggest any alternative?
> >
> > Thanks much!!
> > _______________________________________________
> > rules-users mailing list
> > rules-users(a)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
>
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users