A MultiExpressionEvaluator is used to evaluate multiple expressions in single
method call.
Multiple Expression/ExpressionListener pairs can be added
to a MultiExpressionEvaluator object. When the MultiExpressionEvaluator
object is evaluated, all the registed Expressions are evaluated and then the
associated ExpressionListener is invoked to inform it of the evaluation
result.
By evaluating multiple expressions at one time, some
optimizations can be made to reduce the number of computations normally
required to evaluate all the expressions.
When this class adds an
Expression it wrapps each node in the Expression's AST with a CacheExpression
object. Then each CacheExpression object (one for each node) is placed in the
cachedExpressions map. The cachedExpressions map allows us to find the sub
expressions that are common across two different expressions. When adding an
Expression in, if a sub Expression of the Expression is allready in the
cachedExpressions map, then instead of wrapping the sub expression in a new
CacheExpression object, we reuse the CacheExpression allready int the map.
To help illustrate what going on, lets try to give an exmample: If we
denote the AST of a Expression as follows:
[AST-Node-Type,Left-Node,Right-Node], then A expression like: "3*5+6" would
result in "[*,3,[+,5,6]]"
If the [*,3,[+,5,6]] expression is added to
the MultiExpressionEvaluator, it would really be converted to:
[c0,[*,3,[c1,[+,5,6]]]] where c0 and c1 represent the CacheExpression
expression objects that cache the results of the * and the + operation.
Constants and Property nodes are not cached.
If later on we add the
following expression [=,11,[+,5,6]] ("11=5+6") to the
MultiExpressionEvaluator it would be converted to: [c2,[=,11,[c1,[+,5,6]]]],
where c2 is a new CacheExpression object but c1 is the same CacheExpression
used in the previous expression.
When the expressions are evaluated, the
c1 CacheExpression object will only evaluate the [+,5,6] expression once and
cache the resulting value. Hence evauating the second expression costs less
because that [+,5,6] is not done 2 times.
Problems: - cacheing the
values introduces overhead. It may be possible to be smarter about WHICH
nodes in the AST are cached and which are not. - Current implementation is
not thread safe. This is because you need a way to invalidate all the cached
values so that the next evaluation re-evaluates the nodes. By going single
threaded, chache invalidation is done quickly by incrementing a 'view'
counter. When a CacheExpressionnotices it's last cached value was generated
in an old 'view', it invalidates its cached value.
$Date: 2005/08/27 03:52:36 $