[rules-users] setting different value in consequence ( RHS part) based on a conditional check

Welsh, Armand AWelsh at StateStreet.com
Mon Feb 6 19:45:19 EST 2012


As I suggested, if you wish to put conditional logic in the RHS of a rule, you would use a function.  I didn't provide an example using the function, because it did not appear to warrant the need for one, and because I consider this to be a very simplified version of the actual rule.

Again, the decision on how to solve the challenge is up to the developer.  Rules engines are not really designed to solve programmer's problems, they are designed to solve business problems.  And the use of functions to apply business logic obfuscates the code, making it less readable by a business analyst, and complicates the development of the code.  Placing the decision logic in the LHS keeps the rules simple, and thus more maintainable by a business analyst.

If only developers will ever look at the code, then I would question the use of drools at all, since many of the challenges we solve with drools can be coded more efficiently in straight JAVA code.  But again, the decision is always on the developers.

From: rules-users-bounces at lists.jboss.org [mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Wolfgang Laun
Sent: Thursday, January 26, 2012 10:52 AM
To: Rules Users List
Subject: Re: [rules-users] setting different value in consequence ( RHS part) based on a conditional check

Oh my, aren't we a wee bit too dogmatic? I've certainly been known as being a stickler to style and best practice and what not, but in this particular case I'd use a single rule and offload the earth-shaking decision between 'Y' and 'N' into a function:

rule x
when
   samplefact1( $status: status, state == "CA" )
then
   fact0.setField1(  yn( $status)  );
end

Cheers
-W

On 26 January 2012 18:25, Welsh, Armand <AWelsh at statestreet.com<mailto:AWelsh at statestreet.com>> wrote:
You cannot, under normal circumstances, place conditional login in the RHS.  This is by design.  Any conditional logic belongs in the LHS.  If you need to perform conditional logic in the RHS, this is usually an indicator that they rule is not written correctly.

Now, with that said, there are times when you specifically want to put a decision component in the RHS.  I would do this using a function.

In your case, it really would make sense to use two rules, to represent your one case, like this:


Rule 1
when
               samplefact1( status == "active", state == "CA" )

then
               Response fact0= new Response();
               fact0.setField1( "Y" );
               fact0.setName( "something " );

               insert(fact0 );
end
Rule 2
when
               samplefact1( status != "active", state == "CA" )

then
               Response fact0= new Response();
               fact0.setField1( "N" );
               fact0.setName( "something " );

               insert(fact0 );
end

These two rules are mutually exclusive, only one will fire, and you get the result you want.  It's really about changing the way you think about decision factors.  You can also achieve the same result like this:


Rule 1
when
               samplefact1(state == "CA" )

then
               Response fact0= new Response();
               fact0.setName( "something " );

               insert(fact0 );
end

Rule 2a
when
               samplefact1( status == "active", state == "CA" )
               $resp : Response ( field1 != "Y")

Then
       modify( $resp ) { setField1( "Y" )  };end

Rule 2b
when
               samplefact1( status != "active", state == "CA" )
               $resp : Response ( field1 == "Y")
then
       modify( $resp ) { setField1( "N" )  };
end

In this scenario, you are changing from hard data mapping to an event oriented rule.  Rule1 creates the basic Response fact, and Rules 2a&2b enrich the response fact based on conditions that may arise throughout the processing of the rules, independent of the initial creation step.  Just whenever you have a response fact that is not Y, set it to Y, and of course the inverse rule...  This requires more processing for the rules, but in a large rules based system this may be more in-line with what  you really want.

Armand

-----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 vadlam
Sent: Thursday, January 26, 2012 8:11 AM
To: rules-users at lists.jboss.org<mailto:rules-users at lists.jboss.org>
Subject: [rules-users] setting different value in consequence ( RHS part) based on a conditional check

Hi,

we have an existing BRL rule in Guvnor whereby we set the value of some
fields in RHS based on some value checks in the condition part. we have
created several of these rules already in a previous release.

In the next release, we have new requirements to set the value of an
existing field  in consequence to be of 2 different values based on a
specific conditional evaluation. the rest of the rule remains the same.

to clarify,

when
               samplefact1( status== "active" , state=="CA" )

           then

               Response fact0= new Response();
               fact0.setField1( "Y" );
               fact0.setName( "something " );

               insert(fact0 );
       end

we now have to change this to set Field1 value to Y or N based on some
updated condition for status field.

 lets say if status=="active" , Field1 has to be Y, but when status=closed,
then Field1 has to be N

Please keep in mind that the rest of the rule remains the same.

is there a way to set the value of Field1 to be Y or N within the
consequence part of same rule without having to create another rule .

will the use of functions or variables be of any help in this case?







--
View this message in context: http://drools.46999.n3.nabble.com/setting-different-value-in-consequence-RHS-part-based-on-a-conditional-check-tp3690826p3690826.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
_______________________________________________
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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120207/e89e0337/attachment.html 


More information about the rules-users mailing list