Drools DRL alternatives of eval is Boolean check - Java @ Desk

Monday, December 2, 2013

Drools DRL alternatives of eval is Boolean check

Drools DRL alternatives of eval is Boolean check

In DRL, the boolean check is implemented using the eval() function. Eval is used in following ways :
1) eval can have a method call returning a boolean value
2) eval can perform equality/non-equality operations on fields.

But the disadvantage of using eval is it hits performance in a bad way. Rule file having lot many eval blocks runs slower.

In order to remove the eval, we can make use of following things:
1) java.lang.Boolean class
2) from keyword

Consider the DRL file below
DRL file:

 package com.sample  
 import com.sample.PojoDRLListIteration;  
 import java.util.ArrayList;  
 import java.util.List;  

 // This rule iterates through the list using the from  
 // keyword and checks for value equality with each String  
 // object from list  
 rule "Rule List Contains check with iteration"  
 salience 50  
   when  
     pojoDRLListIteration : PojoDRLListIteration($listOfString : listOfString)  
     value : String() from $listOfString  
     // eval(value == "India")
     Boolean(booleanValue == true) from value == "India"  
   then  
     System.out.println("List contains country India");  
 end  


Have a look at the commented part in the rule which compares String field value with the String "India" and we are passing the in the eval block.
Instead of that, have a look at the next line where we compare
1) value == "India"
2) Using the from keyword from which we are fetching what the condition is returning.
3) Since the equality check returns the Boolean value i.e. true or false, we apply the Boolean class check for field booleanValue == true.






No comments:

Post a Comment