Make everything as simple as possible, but not simpler. -Albert Einstein 

Home Esper Save Money on Gas with Esper: (CEP) Engine
formats

Save Money on Gas with Esper: (CEP) Engine

Published on May 22, 2012 by in Esper

Summary: In this post we utilize Esper, a Complex Event Processing  (CEP) engine, to monitor a fictitious data stream of gas prices to determine when and where to buy gas.  Event processing with Esper involves ‘real time’ tracking, analyzing, and responding to streams of information.    These data streams could be sales orders, Twitter feeds, stock market feeds, weather reports, traffic reports, or in our use case, gas prices. 

Prerequisites: If you would like to obtain this article’s complete sample, it may be obtained from our GitHub repository.  All samples are Maven based java projects.

Let’s Get Started! Before we can begin utilizing Esper, we will have to model our data.   In this sample, we will create a GasEvent and Store class.  As you can see, GasEvent has three properties: store, grade,  and price.  

Listing 1: GasEvent.java

package techbysample.esper4.sample1.model;

/**
 *
 * @author TechbySample.com
 *
 */
public class GasEvent {

	private Store store;
	private String grade;
	private double price;

	public Store getStore() {
		return store;
	}
	public void setStore(Store store) {
		this.store = store;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}

}

Listing 2: Store.java

package techbysample.esper4.sample1.model;

/**
 *
 * @author TechbySample.com
 *
 */
public class Store {

	private String storeName;
	private String zipCode;

	public String getStoreName() {
		return storeName;
	}
	public void setStoreName(String storeName) {
		this.storeName = storeName;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
}

Lets Get Started! (cont): Next, we will need to register our event class, GasEvent with the Esper (CEP) engine.

Reference Listing 3 : TestGasEvent.java  for the following:

In line 34, we create a new Configuration object.

In line 35, we register our GasEvent with Configuration object.

In line 36, we create a new EPServiceProvider using our Configuration object.

In lines 38-82, we create 5 fictious GasEvents.  This information will be utilized to generate and monitor our random stream of gas prices.

Listing 3 : TestGasEvent.java

package techbysample.esper4.sample1;

import java.util.Random;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import techbysample.esper4.sample1.listener.GasEventListener;
import techbysample.esper4.sample1.model.GasEvent;
import techbysample.esper4.sample1.model.Store;

import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;

/**
 *
 * @author TechBySample.com
 *
 */

public class TestGasEvent{

	private Configuration cepConfig = null;
	private EPServiceProvider epService= null;

	private GasEvent[] gasEvents = new GasEvent[20];

	@Before
	public void initialize() throws Exception {

		cepConfig = new Configuration();
		cepConfig.addEventType("GasEvent",GasEvent.class.getName());
		epService = EPServiceProviderManager.getProvider("myCEPEngine",cepConfig);

		Store store1 = new Store();
		store1.setStoreName("Exxon");
		store1.setZipCode("77056");

		Store store2 = new Store();
		store2.setStoreName("Shell");
		store2.setZipCode("77406");

		Store store3 = new Store();
		store3.setStoreName("Chevron");
		store3.setZipCode("77059");

		Store store4 = new Store();
		store4.setStoreName("Citgo");
		store4.setZipCode("77406");

		GasEvent gasEvent1 = new GasEvent();;
		gasEvent1.setPrice(2.50);
		gasEvent1.setGrade("Regular");
		gasEvent1.setStore(store1);
		gasEvents[0]=gasEvent1;

		GasEvent gasEvent2 = new GasEvent();;
		gasEvent2.setPrice(3.50);
		gasEvent2.setGrade("Premium");
		gasEvent2.setStore(store2);
		gasEvents[1]=gasEvent2;

		GasEvent gasEvent3 = new GasEvent();;
		gasEvent3.setPrice(2.79);
		gasEvent3.setGrade("Mid");
		gasEvent3.setStore(store3);
		gasEvents[2]=gasEvent3;

		GasEvent gasEvent4 = new GasEvent();;
		gasEvent4.setPrice(2.60);
		gasEvent4.setGrade("Regular");
		gasEvent4.setStore(store4);
		gasEvents[3]=gasEvent4;

		GasEvent gasEvent5 = new GasEvent();;
		gasEvent5.setPrice(3.50);
		gasEvent5.setGrade("Premium");
		gasEvent5.setStore(store4);
		gasEvents[4]=gasEvent5;

	}

	@Test
    public void testGasEvent()
	{
		try{
			String expression = "select * from GasEvent(grade='Regular') having price < 2.80 and store.zipCode in ('77406')";
			EPStatement statement = epService.getEPAdministrator().createEPL(expression);

			GasEventListener listener = new GasEventListener();
			statement.addListener(listener);

			for (int i=0; i			{
			   Random random = new Random();
			   int eventIndex = random.nextInt(5);
			   epService.getEPRuntime().sendEvent(gasEvents[eventIndex]);
			   Thread.sleep(3000);
			}

			}
			catch (Exception e)
			{
				System.out.println(e);
			}
	}

    @After
	public void cleanup()
	{
    	cepConfig = null;
		epService = null;
	}
}

Esper Query Language: Esper provides a rich ‘SQL like’ syntax called Event Query Language (EQL)(EQL)  enables you to express complex event conditions and correlation that may span time windows.  In our sample project, we are only interested in stores that offer ‘Regular’ grade gasoline  for less than or equal to  ‘$2.80 per gallon’.  In addition, we would like the  store to have a ’77406′ zip codeThe EQL expression for this rule would look like this:

select * from GasEvent(grade=’Regular’) having price < 2.80 and store.zipCode in (‘77406’)

In lines 90-91, we utilize a (EQL) expression to create an EPStatement object. The Esper engine will then filter the data it receives, and fire events whenever that data satisfies the condition defined by the (EQL) statement.

In lines 93-94, we create and register our custom listener object, GasEventListener with the EPstatement. GasEvenListener implements UpdateListener, and prints out details about the gas price whenever our rule is triggered.

Unit Testing Esper:

From the ‘esper4-sample1′ project directory, type:

mvn -Dtest=TestGasEvent test

You should see a result similar to the following output:
——————————————————-
T E S T S
——————————————————-
Running techbysample.esper4.sample1.TestGasEvent
Let’s buy some gas! @
Store Name: Citgo
Store Zip: 77406
Gas Grade: Regular
Gas Price: 2.6

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 19.921 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Resources:
Esper web site
Official TechBySample GitHub repository

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 

Leave a Reply

Your email address will not be published. Required fields are marked *


*

© Techbysample.com, all rights reserved.