Reputation: 429
Recently I'm working with drools and I want to make some special checks on some objects. I need to use functions in the when
section of the rule, but I'm getting an error. Example:
function boolean newFunction(int a){
if(a>0)
return true;
else
return false;
}
rule "new rule"
salience 100
dialect "mvel"
when
eval(newFunction(1))
then
System.out.println("OK");
end
The error i get always is:
unable to resolve method using strict-mode: java.lang.Object.newFunction(java.lang.Integer)
Is there no support on drools for functions in when
section?
Thanks!
Upvotes: 9
Views: 22488
Reputation: 55
I'm not certain why you are getting the error you are getting, but I was using the following for some debugging yesterday in a query (an LHS so I'm guessing same as a rule)
function boolean say(Object s) {
Debug.log("Say %s\n", s);
return true;
}
the query just did eval(say($object))
to help me see whether it ever got invoked. I'm running a snapshot of 6.1 from a week or so ago. Maybe try getting it working (doing anything) just taking Object and work from there - it might be Number or Integer is where you end up on the argument rather than int?
Upvotes: 0
Reputation: 30007
The short answer is: no.
This is because the facts need to be in the working memory.
What you can do, is to have a rule that takes all types of a certain class from the working memory, applies a function in the then section and inserts that new value in the working memory.
This answer, originally posted on 2012, is not more relevant as newer versions of drools do support functions on the when
clause.
Upvotes: 12
Reputation: 886
Along the lines of the selected answer above, after some experimentation I found that it is possible to create an external java method, whose class can be imported into the rules file, and wrapped in a MVEL function wrapper (Boolean) which can then can be called from the LHS as a parameter to an eval statement.
[External Java POJO_Class.myMethod]
import com.mypackage.POJO_Class;
function Boolean myFunctionName() {
POJO_Class myClass = new POJO_Class();
return myClass.myMethod(Parameters);
}
rule "Test Rule"
when
eval ( myFunctionName(parameters) )
then
end
Upvotes: 2
Reputation: 221
Very likely an MVEL or an integration bug - function call adapters did not box/unbox primitive types. I see the question is quite old, but the problem has been since fixed (tested with 6.3.0-SNAPSHOT). For older versions, I'd try using boxed types : function boolean newFunction( Integer a ) ...
Upvotes: 3