java.lang.Object
org.activemq.filter.MultiExpressionEvaluator
- public class MultiExpressionEvaluator
- extends java.lang.Object
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.
- Version:
- $Revision: 1.1.1.1 $ $Date: 2005/03/11 21:14:23 $
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
rootExpressions
java.util.HashMap rootExpressions
cachedExpressions
java.util.HashMap cachedExpressions
view
int view
MultiExpressionEvaluator
public MultiExpressionEvaluator()
addExpressionListner
public void addExpressionListner(Expression selector,
MultiExpressionEvaluator.ExpressionListener c)
- Adds an ExpressionListener to a given expression. When evaluate is
called, the ExpressionListener will be provided the results of the
Expression applied to the evaluated message.
removeEventListner
public boolean removeEventListner(java.lang.String selector,
MultiExpressionEvaluator.ExpressionListener c)
- Removes an ExpressionListener from receiving the results of
a given evaluation.
addToCache
private MultiExpressionEvaluator.CacheExpression addToCache(Expression expr)
- Finds the CacheExpression that has been associated
with an expression. If it is the first time the
Expression is being added to the Cache, a new
CacheExpression is created and associated with
the expression.
This method updates the reference counters on the
CacheExpression to know when it is no longer needed.
removeFromCache
private void removeFromCache(MultiExpressionEvaluator.CacheExpression cn)
- Removes an expression from the cache. Updates the
reference counters on the CacheExpression object. When
the refernce counter goes to zero, the entry
int the Expression to CacheExpression map is removed.
evaluate
public void evaluate(javax.jms.Message message)
- Evaluates the message against all the Expressions added to
this object. The added ExpressionListeners are notified
of the result of the evaluation.