Difference between revisions of "Northwind Esper: Alerts"

From RifidiWiki

Jump to: navigation, search
(New page: ===Alert: Package Moved Backwards!=== At this point, we can track the items that can be seen at the various read zones in our scenario. Now we need to implement the functionality that wi...)
(No difference)

Revision as of 19:10, 24 November 2009

Alert: Package Moved Backwards!

At this point, we can track the items that can be seen at the various read zones in our scenario. Now we need to implement the functionality that will fire an alert when an item moves backwards (that is from the weigh station to the dock door).

The Code

	/**
	 * A method that sets up business alerts
	 */
	private void setUpAlerts(){
// 1		Create  a window for alert messages
		statements.add(esperService.getProvider().getEPAdministrator().createEPL(
			"create window alerts.win:length(20) (alert_type int, tag_ID String)"));

// 2		create a window for item leaving the weighstation 
		statements.add(esperService.getProvider().getEPAdministrator().createEPL(
			"create window weighstation_recent.std:unique(tag_ID) (tag_ID String)"));
		
// 3		Insert items into weightstation_recent once they leave weighstation
		statements.add(esperService.getProvider().getEPAdministrator().createEPL(
			"insert rstream into weighstation_recent select tag_ID from weighstation"));
		
// 4		whenever an item is seen at the weighstation and then seen at the dockdoor, insert a new item into the alerts window
		statements.add(esperService.getProvider().getEPAdministrator().createEPL(
			"on pattern[every-distinct(tag.tag_ID) tag=weighstation_recent -> dockdoor(tag_ID = tag.tag_ID)] " +
			"insert into alerts " +
			"select 1 as alert_type, tag_ID as tag_ID " +
				"from weighstation_recent where tag_ID = tag.tag_ID"));
	}

The following code is an addition to the setupListeners() method that was previously defined:

		// Create a listener to handle Alerts
		StatementAwareUpdateListener alertListener = new StatementAwareUpdateListener() {
			@Override
			public void update(EventBean[] arrivals, EventBean[] departures,
					EPStatement arg2, EPServiceProvider arg3) {
				if (arrivals != null) {
					for (EventBean bean : arrivals) {
						int alertType = (Integer) bean.get("alert_type");
						String id = (String) bean.get("tag_ID");
						switch (alertType) {
						case 1:
							System.out.println("Package moved backwards: " + id );
							break;

						}
					}
				}
			}
		};
		
		//Create a query that is triggered on insert and remove events from Weigh Station Window 
		EPStatement queryAlert= esperService.getProvider().getEPAdministrator().createEPL(
				"select * from alerts");
		queryAlert.addListener(alertListener);
		statements.add(queryAlert);

Explanation of the Code

  1. First we need to create a window that will hold alerts.
    1. Using the win:length() view, the window will only keep track of the last 20 alerts
    2. This window is defined to hold two pieces of information per entry: an int that is the alert type, and a string that is the tag ID
  2. Next we create a window that will hold tags that have departed from the weigh station
  3. This statement inserts items that have left the weighstation window into the weighstation_recent window
  4. Finally we get to define the rule! This rule simply looks for the pattern of an event entering the weighstation_recent window followed by an event entering the dockdoor window which have the same ID. Once this occurs, it makes a new entry in the alerts window.
    1. We use the every-distinct operator to ensure that we will only match the same tag once in the pattern.
    2. We also insert '1' as the alert type. We will assume that that event type corresponds to a 'tag moved backwards' alert.