Search code examples
droolsrulesrule-engine

Drools- Unable to get rule to fire when I access a global I set using setGlobal in the KieSession


I have a very simple test application that inserts a global in a KieSession. I have a ProductFinder object that stores fictitious categories. I set this object as a global in the session. I set it using the KieSession like this: Actually, the rules fire when I create one kieSession with identical code but reading different data. Why would the same kieContainer pointing to the same files run at different times, fire the rules in one case and not fire them in another? kieSession.setGlobal("pf", productFinder);

Here are the basic classes for ProductFinder and Category:

 class ProductFinder {
    Category produce
    Category cosmetics
 }

 class Category{
  Long  code
 }

I then proceed to write a simple rule that tries to access the global category like so:

 global ProductFinder pf;

 rule "find category Produce "

  when

   $produce : Category(  ) from  pf.produce

   then
      System.out.println("Found product category with info "+$produce ) ;
   end

Unfortunately, the rule is not fired. I know the global ProductFinder has been set in the global, because if I comment out the line where I set the global, Drools throws an exception complaining that it can find the global.

Please help.


Solution

  • My bad, I did not call fireAllRules() on the session.