Drools rule error "Unexpected global"

Kaizar Laxmidhar picture Kaizar Laxmidhar · Feb 12, 2014 · Viewed 7.6k times · Source

I have business rules implemented in Drools, while executing I get java RuntimeException

Unexpected global [myService]
org.drools.common.AbstractWorkingMemory.setGlobal(AbstractWorkingMemory.java:588)

What could be the cause?

Rule:

rule "Tax Rule"
  when
    calculateTax : calculateTax(
        objOne : objOne,
        objTwo : objTwo,
        objThree : objThree
    );

    objFour : objFour();
    System.out.println("debug");

  then
    ...
end

Answer

laune picture laune · Feb 13, 2014

To declare and set a global in your DRL, you need to declare it and initialize it:

// DRL file
global Service myService

// Java application
StatefulKnowledgeSession session = ...
session.setGlobal("myService", new Service() );

Failure to declare the global in the DRL file or a mismatch of the global's name and the first argument in the setGlobal call result in the error message as posted.