Apama 10.7.2 | Developing Apama Applications | Developing Apama Applications in Java | Defining Event Expressions | Defining advanced event expressions | Specifying the timer operators | Looking for event sequences within a set time
 
Looking for event sequences within a set time
Consider this earlier example:
// Code within the monitor class
public void onLoad() {
   EventExpression eventExpr = new EventExpression(
     "NewsItem(\"ACME\",*) -> Tick(\"ACME\",*)");
   eventExpr.addMatchListener(matchListener1);
}
 
// Code within the first (and only) Match Listener
// class – matchListener1
 
   public void match(MatchEvent event) {
     System.out.println("Detected a NewsItem followed"
       + " by an Tick event, both regarding the ACME company.");
   }
This will look for the event sequence of a news item about a company followed by a stock price tick about that company. Once improved this could be used to detect the beginning of a rise (or fall) in the value of shares of a company following the release of a relevant news headline.
However, unless a temporal constraint is put in place, the monitor is not going to be that pertinent, as it might trigger on an event sequence where the price change occurs weeks after the news item. That would clearly not be so useful to a trader, as the two events were most likely unrelated and hence not indicative of a possible trend.
If the event expression above is rewritten as follows,
EventExpression eventExpr = new EventExpression(
   "NewsItem(\"ACME\",*) -> Tick(\"ACME\",*) within(30.0)");
the Tick event would now need to occur within 30 seconds of NewsItem for the listener to trigger.
The within(float) operator is a postfix unary operator that can be applied to an event expression (the Tick event template in the above example). Think of it like a stopwatch. The clock starts ticking as soon as the event expression it is attached to becomes active, i.e. when the listener actually starts looking for it. If the stopwatch reaches the specified figure before the event expression evaluates to true the event expression becomes permanently false.
In the above code, the timer is only activated once a suitable NewsItem is encountered. Unless an adequate StockTick then occurs within 30 seconds and makes the expression evaluate to true, the timer will fire and fail the whole listener.
As already specified, the within operator can be applied to any event expression, hence A within(x), where A represents just an event template and x is a float value specifying a number in seconds, is perfectly valid.