Apama 10.15.0 | Developing Apama Applications | Developing Apama Applications in Java | Defining Event Expressions | Obtaining matching events
 
Obtaining matching events
An event template provides a definition against which several event instances could match. Once a listener triggers, sometimes it is necessary to get hold of the actual event that matched the template.
This can be achieved through event tagging.
If you are familiar with EPL, event tagging in JMon is similar in principle to variable coassignment in EPL. For this reason the term coassigned is sometimes used to refer to event tagging.
Consider this revised Simple monitor:
import com.apama.jmon.*;
 
public class Simple implements Monitor, MatchListener {
 
   /**
   * No argument constructor used by the jmon framework on
   * application loading
   */
   public Simple() {}
 
   /**
   * Implementation of the Monitor interface onLoad method. Sets up
   * a single event expression looking for all stock trade events
   * with a trade price of greater than 10.0. This class instantiation
   * is added as a match listener to the event expression.
   */
   public void onLoad() {
     EventExpression eventExpr = new EventExpression("Tick(*, >10.0):t");
     eventExpr.addMatchListener(this);
   }
 
   /**
   * Implementation of the MatchListener interface match method.
   * Extracts the tick event that caused the event expression to
   * trigger and emits the event onto the default channel
   */
   public void match(MatchEvent event) {
     Tick tick = (Tick)event.getMatchingEvents().get("t");
     System.out.println("Event details: " + tick.name
       + " "   + tick.price);
     tick.emit();
   }
}
Note the revised event expression
Tick(*, >10.0):t
This specifies that when a suitable Tick event is detected, it must be recorded with the t tag. This allows a developer to get hold of the actual event that matched the event expression within the registered match listener's match method.
Once the eventExpr listener detects a suitable event it will trigger and call match, passing to it a MatchEvent object. This object embeds within it all the individual event instances that together caused the event expression to be satisfied and were tagged.
In this example our event expression still consists of a single event template, and since this is tagged, then the MatchEvent object will contain the single Tick event that triggered the eventExpr listener. This will be tagged as t.
A MatchEvent object has two methods:
*HashMap getMatchingEvents() - Get the set of tagged Events that caused the match. This method returns a Map of the tagged Event objects that hold the values that matched the source EventExpression.
*Event getMatchingEvent(String key) - Get one of the tagged Events that caused the match. This method returns the tagged Event object that matched in the source EventExpression.
For complete class and method signatures, refer to the API Reference for Java (Javadoc).
The lines:
Tick tick = (Tick)event.getMatchingEvents().get("t");
or
Tick tick = event.getMatchingEvent("t");
show how the tagged event can be extracted by using the tag as a key.