Search code examples
drools

Drools cannot resolve a global variable


i'm trying to add String items to a global variable $items. But i get an error "$items cannot be resolved"

Below is snippet of my drl rule. What am i missing?

code snippet


Solution

  • you can try this

    First: in drl file

    global java.util.HashSet items
    
    rule "rule 1"
        when
        then
            items.add("Item 1");
            System.err.println(items);
    end
    

    Second: in java file

    KieSession kieSession = ...
    kieSession.setGlobal("items",new HashSet<>());