2

How do I set a global in Drools 4 from within a rule? I want to set a boolean if a rule fires so that it can read it from another rule with a lower salience.

Tarski
  • 5,360
  • 4
  • 38
  • 47

3 Answers3

1

Assuming you're using Drools 5.x, you can do this in your rule consequence:

kcontext.getKnowledgeRuntime().setGlobal(name, value);

Kris Verlaenen
  • 2,918
  • 1
  • 15
  • 5
0

You might be able to do something like:

drools.getWorkingMemory().setGlobal... (similar to above, what kris said).

Michael Neale
  • 19,248
  • 19
  • 77
  • 109
0

I wouldn't use a global here. You are inferring a new fact from other facts, you can just 'insert' the new fact and have the other rule fire on that.

rule "some rule"
when
    //…
then
    insert(new MyNewFact())
end

There's no guarantee that Drools will re-evaluate your rules in response to the insertion of a (new) global and your other rule might not fire. It will, however, re-evaluate in response to facts being inserted (or retracted, or modified).

Francisco Canedo
  • 1,980
  • 2
  • 13
  • 16