Apama 10.15.3 | Developing Apama Applications | Developing Apama Applications in Java | Defining Event Expressions | Optimizing event expressions
 
Optimizing event expressions
When a developer creates an event expression, a substantial percentage of the computational overhead goes into parsing the event expression itself.
If you need to create several instances of an event expression where only literal values in event templates vary, this repeated parsing cost can be removed through the use of a prepared event expression.
Instead of writing,
EventExpression eventExpr1 = new EventExpression(
   "NewsItem(\"ACME\",*) -> Tick(\"ACME\",*)");
EventExpression eventExpr2 = new EventExpression(
   "NewsItem(\"EMCA\",*) -> Tick(\"EMCA\",*)");
eventExpr1.addMatchListener(matchListener1);
eventExpr2.addMatchListener(matchListener2);
you could write,
PreparedEventExpressionTemplate et
   = new PreparedEventExpressionTemplate(
     "NewsItem(?,*) -> Tick(?,*)");
 
PreparedEventExpression pex1=et.getInstance();
pex1.setString(0, "ACME");
pex1.setString(1, "ACME");
 
PreparedEventExpression pex2=et.getInstance();
pex2.setString(0, "EMCA");
pex2.setString(1, "EMCA");
 
pex1.addMatchListener(matchListener1);
pex2.addMatchListener(matchListener2);
The above example shows how instead of creating two very similar event expressions you can create a single prepared event expression template, and then customize multiple instances of it. The main advantage of the latter approach is the fact that the event expression was parsed in Java only once. With an example as simple as the ones above this would in fact hardly make any difference, but in Java code with hundreds of such event expressions the difference in performance can be significant.
As shown in the code snippet above, the procedure for creating listeners with prepared event expressions is slightly different from that of normal event expressions.
You must create a PreparedEventExpressionTemplate and define within that the event expression. The syntax for event expression definitions is the same as previously with the exception of the ? operator. This can be used instead of any literal value. The next step is to get an instance of a PreparedEventExpression, and then to set values for any literals replaced by ? in the prepared event expression template. Finally, you can create listeners on the PreparedEventExpression instances just as with normal event expressions.