<p>We use dedicated Java classes named after the kind of constant they represent, like Slack in your case or RetirementAge, LegalAgeThreshold. Most of the time, they contain a single property "value".</p>
<p>This makes it easy to use Drools patterns to match them, plus we get rather strong type checking during rules compilation AKA packaging. We only need around a dozen constants in our rules and their values rarely change (like once every few months). The amount and type of constants are even more static: add/remove one constant per year on average.</p>
<p>Best regards</p>
<p>Ansgar</p>
<div class="gmail_quote">Am 18.02.2013 20:52 schrieb "Willem van Asperen" <<a href="mailto:willem@van.asperen.org">willem@van.asperen.org</a>>:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
Hi All,<br>
<br>
I have a bunch of rules that evaluate using some constant variable
(say "slackTicks"). Currently, in my rules, I write down the value
of this "slack" each and every time I use it, for example:<br>
<br>
<code>TransferSchedule( tick > currentTick - 10d )</code><br>
<br>
where 10 is the slack.<br>
<br>
A solution would be to insert constant facts:<br>
<br>
<code>Constant( key = "slack", $slackTicks : value )</code><code><br>
</code><code>TransferSchedule( tick > currentTick - $slackTicks )</code><br>
<br>
Or inject a global properties object:<br>
<br>
<code>global Properties constants</code><code><br>
</code><code><br>
</code><code>$slackTicks :
Double.valueOf(constants.getPropertyValue("model.slack", "0"))</code><br>
<br>
Or, the last one I can think of: a public final static on the fact
class:<br>
<br>
<code>TransferSchedule( tick > currentTick -
TransferSchedule.SLACK_TICKS )</code><code><br>
</code><br>
These all cross the boundary between Java and Drools. In my Java
code I have to insert the Constant facts or inject the global. When
my drools developers think of some new ways to create the rules, I
run the risk of having to go in and modify the .properties file or,
worse, change the Java code.<br>
<br>
To be able to define a "constant" would be a clean solution:<br>
<br>
<code>declare</code><code> constant double SLACK_TICKS = 10</code><code>;<br>
</code><code>
</code><code><br>
...<br>
</code><code>
TransferSchedule( tick > currentTick - SLACK_TICKS )</code><code><br>
...<br>
</code><br>
This separates the rules from the Java code and give the rules
developer all freedom to do whatever they want to make constant.<br>
<br>
Ah, just thought of another way to do it. You can insert the facts
from within Drools itself:<br>
<br>
<code>declare Constant</code><code><br>
</code><code> key : String</code><code><br>
</code><code> value : Object</code><code><br>
</code><code>end</code><code><br>
</code><code><br>
</code><code>rule initialize</code><code><br>
</code><code> salience 100000</code><code><br>
</code><code> when</code><code><br>
</code><code> // always</code><code><br>
</code><code> then</code><code><br>
</code><code> insert( new Constant( "slack", 10d ) );</code><code><br>
</code><code> //... other constants go here</code><code><br>
</code><code>end</code><code><br>
</code><br>
I will still have to "capture" the constants that I need in every
rule with a Constants( key == "...", $c : value ) premise, though...
And you would need to add some more Constant classes to cater to
contain type safety...<br>
<br>
What's your thought? What strategy do you use?<br>
<br>
Regards,<br>
Willem<br>
</div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br></blockquote></div>