Home > Enterprise >  Drools Rules : Can a static method be called in the when section of a drool file
Drools Rules : Can a static method be called in the when section of a drool file

Time:09-11

I am trying to write a drools rule that would check if a student has an average of more than 5 at a discipline and i get this error whenever i try to run a static method in the when clause. Moreover, i couldn't find in the drools documentation if it's possible to call a static method in the when section.

This is the rule :

rule "student with at least two grades and average less than 5" salience 8
    when
    $student : Student(grades.size() >= 2)
         $list : ArrayList() from eval(computeAverageGrade($student))
         $value : Double() from $list
         Boolean(booleanValue() == true) from $value < 5.0
    then
     System.out.println(2);
    student.setValid(false)
end

This is the method's header:

 public static List<Double> computeAverageGrade(Student student)

and this is the error i am getting :

Caused by: org.mvel2.PropertyAccessException: [Error: null pointer or function not found: eval]
[Near : {... eval(computeAverageGrade($stud ....}]
             ^
[Line: 1, Column: 1]

CodePudding user response:

Try to import the static method in the Drool file:

E.g. import static ClassName.methodName:

Please reference: How to import Java static method into Drools file?

CodePudding user response:

Solved it by replacing ArrayList with Object() and removing eval from the static method call.

found it here : https://docs.drools.org/7.73.0.Final/drools-docs/html_single/index.html#drl-rules-WHEN-con_drl-rules

  • Related