drools

Drools - Getting exception while trying to hold the method retun values


I am new to the drools. I am trying to call a static method from a drl file and trying to hold the value in a reference variable for further processing, not sure why I am always getting the exception, need help

dialect  "mvel"

rule "Calculate values"
    
when
        $p : InputDTO()
        
$section : SectionsDTO(sectionName == "XX") from $p.sectionsList
        
$subSection : SubSectionsDTO(subSectionName == "YY") from $section.subSectionsList
        
$question : QuestionDTO(questionId == "12") from $subSection.questionsList
        
$questionOption : OptionsDTO($key : key, $selected : selected) from $question.questionnaireOptions
        
if($key == "yes" && $selected == true) do[yes]  
else if($key == "no" && $selected == true) do[no]
    
    then
        then[yes]
        $calc : Utility.checkBalances(reportConfig, $key); //Returns double values (for example - 2.0)

    then[no]
        System.out.println("no");
end

Exception I am getting:

text=Unable to Analyse Expression $calc : Utility.checkBalances(reportConfig, $key);

I have checked the return value of method by printing the result and it is returning correct values.

NOTE: I am planning to update the $calc values from my next rules. How I can hold the Utility.checkBalances(reportConfig, $key) return value in such a way so that from the next rule fire I can check and update it?


Solution

  • The right hand side of your rule ("then" clause) should be in Java.

    then
      double $calc = Utility.checkBalances(reportConfig, $key);
    

    If you plan on using $calc in subsequent rules you'll of course want to insert it into working memory.