Memory consumption reduction
To reduce memory consumption of the rule engine in sequential
mode, you can use the select and dynamicselect
keywords,
the dynamicselect
keyword followed by a rule domain,
or remove rule metadata.
The memory consumed when a rule engine uses sequential processing can be significantly reduced if the executable objects created to realize the condition part and the action part are discarded.
Use of select and dynamicselect keywords
Using
the select
and dynamicselect
keywords,
you can select the rules belonging to a rule task. These keywords
operate on IlrRule objects and
return a Boolean value to indicate whether a rule is selected. For
example:
body = select(?rule) {
if (?rule.getName().startsWith("R1.") return true;
else return false;
}
From the select
and dynamicselect
keywords,
the rule engine iterates on the rules of the ruleset and calls the
selector for each of them. When the total number of rules is high
and the selected number of rules is low, the loop is likely to consume
much memory. To overcome this expensive memory usage, you can write
the select
and dynamicselect
functions
with no arguments. When the functions take no arguments, the body
of the selector iterates on the rules and returns an array of IlrRule objects.
For example:
body = select() {
IlrRule[] rules = getRuleset().getAllRules();
// do some selection...
return rules;
}
By using the select
and dynamicselect
keywords,
together with an indexing mechanism to find the rules efficiently,
you can eliminate long loops that consume expensive memory.
Use of dynamicselect followed by a rule domain
A dynamicselect
keyword
can be followed by a domain, initiated by the in
keyword
(see in in the IRL Reference for the full reference
of the in
keyword). Here is an example of how to
use the dynamicselect
with the in
keyword:
body = dynamicselect(?rule) {
if (?rule.getName().startsWith("R1.") return true;
else return false;
} in EXPRESSION;
Here, in EXPRESSION
is optional.
The EXPRESSION
element must have the type IlrRule[] and
is generally a method call. The rules of EXPRESSION
are
compiled once. The dynamicselect
statement itself
selects the rules among the rules of EXPRESSION
.
When
the selector is defined with an argument, instead of iterating on
the entire ruleset to find which rules compose the rule task body,
it iterates only on the rules of the domain. The domain represents
a superset of the rules that compose the body of the rule task. If
the rule task is defined with the algorithm property equal to sequential
, Java™ bytecode is generated only
for the rules of this superset. At each computation of the actual
set of rules of the rule task body, that is, at each execution of
the rule task, the significant bytecode generated for the superset
is activated for this execution.
The rule domain provides two benefits:
-
The number of rules on which you iterate is clearly defined.
-
The bytecode is only generated once, despite the dynamic property of the body.
Because EXPRESSION
is computed only
once, it must not depend on rule-engine-specific parameters. In other
words, EXPRESSION
should only depend on static rule
properties.
Removal of rule metadata
For the case where every dynamicselect
is
provided with a domain, the bytecode can be prepared before a rule
engine is created. After a rule engine has been created, the rule
metadata of the prepared bytecode is no longer necessary to the sequential
execution mode and can be removed.
For the the rule metadata to be removed, you can use these methods after the ruleset has been passed:
-
IlrRule#removeMetadata()
-
IlrRuleset.IlrRuleset#removeMetadata(boolean,%20boolean)
-
IlrRuleset.IlrRuleset#removeMetadata()