Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools functions

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 whensection?

Thanks!

like image 480
Fulgencio Jara Avatar asked Sep 26 '12 15:09

Fulgencio Jara


2 Answers

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.

edit

This answer, originally posted on 2012, is not more relevant as newer versions of drools do support functions on the when clause.

like image 141
Augusto Avatar answered Nov 05 '22 05:11

Augusto


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 ) ...

like image 44
Davide Sottara Avatar answered Nov 05 '22 03:11

Davide Sottara