Drools Function in DRL files - Java @ Desk

Wednesday, July 2, 2014

Drools Function in DRL files

Drools Function in DRL files

Function in a DRL file is similar to a java function.

Inside a function, a plain java code can be easily written. Functions in a DRL file can be used for both action(when) and consequence(then) part.

When used in a when part of a rule, a function should return a boolean value i.e. true or false. If a function is returning any other object then the evaluation to true or false need to be taken care in the rule. For example, if a function is returning String, in that case in the when part, its required to use

eval(returnedString == "XYZ")

In case of then part of a rule, a function can return any object.

Advantages of using function :
1) Easy to write java code and maintain.
2) If the function is getting used in multiple DRL files, then no need to write the function in all DRL. The function is required in only one DRL file that is being loaded 1st in the knowledge session
3) Can implement complex logic

Function syntax

function RETURN_TYPE functionName() {
 return RETURN_TYPE; // return to be used in case other than void
}


Function usage :

The function checks if the user has taken Personal Loan from any bank.

function boolean isPersonalLoan() {
 return true;
}

rule "funtionTest"
when
 eval(isPersonalLoan())
then
 System.out.println("Function retured true");
end








No comments:

Post a Comment