1

I am trying to migrate an existing ILOG JRules based application to Drools based project. In ILOG we have local variables (and variable set) which can be used to calculate and store intermediate values within rules (formulas).

Is there an equivalent of local variables in Drools? I know we have Globals in Drools but the documentation suggest to use Globals only as read only constants and to pass data between Java calling class to rules:

It is strongly discouraged to set or change a global value from inside your rules. We recommend to you always set the value from your application using the working memory interface.

One approach would be to write a custom java class Variable and declare all variables within it and use it in drools rules. But that would need a java code change everytime we want to add a new variable.

Is there any feasible method of defining and using variables in Drools which can be defined and altered within rules?

Viral Patel
  • 8,506
  • 3
  • 32
  • 29

1 Answers1

1

I don't see anything wrong in using globals to keep some kind of calculation or state between your rules. I think what Drools' documentation is trying to say (in a very complicated and unclear way) is that you should never use globals in the RHS of the rules because Drools doesn't know when the value of a global changes. If you do use globals in your constraints, then make sure you never change their values. Having saud that, I think that using globals in the RHS of your rules is harmless. If you are not comfortable with this idea, then you could also insert your "Calculation" object as a fact in your session. Rules can match against this fact, bind a variable to it and then interact with it in the RHS.

Hope it helps,

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31