NEsper Complex Event Processing: Pattern Matching
Saturday, October 11, 2014 Category : CEP, NEsper 0
This article discusses the basics of complex event processing and pattern matching in streaming data using NEsper (.NET).
What is Complex Event Processing (CEP)
Before discussing the details of how NEsper can be used for matching patterns in streaming data, first some information on what complex event processing is. In most of the applications created these days, the data is first stored (in a relational or non-relational database/datastore) and then queried later to get results. These results obtained from pre-stored data are a sort of snapshot for the instant at which the results are fetched. If the same query is run 10 seconds later, we may get different results since the underlying data may have changed by then.
Complex event processing turns that model around and says that instead of storing the data, store the queries. Data can then be made to pass through these queries (or rules) as and when it arrives and can be pushed in the processed form to all the interested parties (including a persistence engine).
CEP Uses
The processing of streaming data can be useful in many scenarios. For example, in stock market trading, we may have market data of a given ticker arriving at intermittent intervals. The price of a given ticker does not change every second but it may change any second. These changes can arrive as a stream and can then be used for calculating moving averages and other calculations can be performed on them to predict whether the ticker is on an upward trend or downward trend.
Another possible scenario can extend the previous scenario by joining the market data stream with the actual trading data (buy/sell) stream of a given user in a day to predict whether the total profit of the user is in an upward trend or a downward trend.
Another possible scenario can extend the previous scenario by joining the market data stream with the actual trading data (buy/sell) stream of a given user in a day to predict whether the total profit of the user is in an upward trend or a downward trend.
CEP Advantages
The first advantage of CEP is performance. Since the data is analyzed and processed as and when it arrives, operations are performed on small bits of data instead of on the entire data at once. Thus, the cost of performing operations is small.
The second advantage of CEP is real-time analysis. Since updates are pushed immediately to concerned parties and results updated, data is always fresh. Re-querying data is not required.
NEsper CEP
NEsper is the CLR based component for Esper for Java and provides stream processing capabilities for an event driven system. More information on ESper is available at:
http://esper.codehaus.org/about/esper/esper.html
http://esper.codehaus.org/about/esper/esper.html
NEsper Pattern Matching
The source code for this discussion is available at:
https://bitbucket.org/minihere/nesperbasics/src
Update: https://github.com/togglebrain/NEsper/tree/V1.0
This code sample contains a very basic UI:
The following entries are available in the drop-down, which represent the actions that a programmer-next-door performs every single day:
Based on the sequence in which the items on this list are selected, the prediction changes. For example, try selecting Scroll:
The text at the bottom changes to “programmer On a quest”.
Similarly, for a more complex sequence of events, if Minimize is selected, followed by Maximize, followed by Scroll, the prediction is that the programmer is bored:
If this is followed by Lock:
You get the idea.
While all this is going on, NEsper is remembering the previous data that came and adding on the new data to it to try and make sense of the rules given to it.
At the code level, it is pretty basic. For simplicity’s sake, all code has been kept in one file: Form1.cs. The form itself performs 3 important functions when the constructor fires:
The first function “InitializeEPLStatements” is the core here. It defines all the queries that we are storing in NEsper. These are the queries that run each time new data arrives. Let’s take a look at one of these:
"SELECT \"recharging\" FROM pattern [every a=Payload(PayloadText=\"Minimize\") -> every b=Payload(PayloadText=\"Maximize\") -> every c=Payload(PayloadText=\"Scroll\") -> every d=Payload(PayloadText=\"Coffee\")]"
The arrow symbol in the above query (->) indicates “followed by”. So, represented simply, this query is of the following format:
Event “a” (followed by) Event “b” (followed by) Event “c” (followed by) Event “d”
So if events occur in the order a->b->c->d, fire this query and select “recharging”. Event “a” itself means that object of type Payload arrives, and this object has property PayloadText set as “Minimize”. So, event “a” will not occur when Payload object arrives with any other PayloadText value.
In summary, the above query translates to:
Select “recharging”
When (first payload arrives with PayloadText=”Minimize”
Followed by payload arrives with PayloadText=”Maximize”
Followed by payload arrives with PayloadText=”Scroll”
Followed by payload arrives with PayloadText=”Coffee”)
Once these queries are initialized, the NEsper initialization code is fired, which is:
This code creates the NEsper default config, followed by adding an event type where the type of data expected in the event is of type Payload. Payload is a simple class containing only one string property. This code then creates a runtime for NEsper and creates a message handler class which should expose one Update method. If the Update method is removed from the definition of MessageHandler, the following error occurs:
So the Update method should be provided by whichever class is being passed in as a statement subscriber to NEsper.
Once the NEsper initialization is complete, the WireEPLStatements method wires the queries created earlier with the message handler created just now:
That’s it! More to be covered in upcoming posts.








