This documentation is for WSO2 Complex Event Processor 2.0.1. View documentation for the latest release.

Patterns

from [every] <stream> -> [every] <stream> ... <stream> within <time>
insert into <stream-name> <attribute-name> {<attribute-name>}

1. Pattern processing is based on one or more input streams.
2. Pattern matches events or conditions about events from input streams against a series of happen before/after relationships.
3. The input event streams of the query should be referenced in order to uniquely identify events of those streams. e1=Stream1[prize >= 20] is an example of a reference.
4. Any event in the output stream is a collection of events received from the input streams, and they satisfy the specified pattern.
5. For a pattern, the output attribute should be named using the ‘as’ keyword, and it will be used as the output attribute name in the output stream.

Following example show a simple pattern query.

If an event arrival at Stream1 with price >= 20 is followed by an event arrival at Stream2 having price >= 1st event’s price, an output event will be triggered via StockQuote stream. The event will have two attributes; 1st event’s symbol, and 2nd event’s price.

from e1=Stream1[price >= 20] -> e2=Stream2[price >= e1.price]
insert into StockQuote e1.symbol as symbol, e2.price as price

Every and Within Keywords

Without “every” keyword, the query will only run once. If you have the “every” enclosing a pattern, then the query runs for every occurrence of that pattern.
Furthermore, If “within <time>” is used, Siddhi triggers only the patterns where the first and the last events constituting to the pattern have arrived within the given time period. In the following example, a1 and b1 should be within 3000 msec as specified.

For every infoStock event having action == "buy" following an confirmOrder event having command =="OK", the StockExchangeStream event will be matched when its price is > infoStock event’s price.

from every (a1 = infoStock[action == "buy"]
       -> a2 = confirmOrder[command == "OK"] )
       -> b1 = StockExchangeStream [price > infoStock.price]
within 3000
insert into StockQuote
      a1.action as action, b1.price as price

Logical Operations

You can combine streams in patterns using logical OR and AND.
1. and - occurrence of two events in any order
2. or - occurrence of an event from either of the steams in any order

Following example shows a sample query. It waits till the ‘buy’ action form both OrderStock1 and OrderStock2 before matching the prices in StockExchangeStream.

For every OrderStock1 event with action == "buy" and OrderStock2 event with action == "buy", the StockExchangeStream will be matched for events having price > 70 followed by events having price > 75. 

from every a1 = OrderStock1[action == "buy"] and
                a2 = OrderStock2[action == "buy"] ->
                b1 = StockExchangeStream[price > 70] ->
                b2 = StockExchangeStream[price > 75]
insert into StockQuote
                a1.action as action, b1.price as priceA, b2.price as priceB

Counting patterns

You can count the number of event occurrences of the same event stream with the minimum and maximum limits. For example, <1:4> means 1 to 4 events, <2:> means 2 or more, and [3] means exactly 3 events.

For every two or more infoStock events, the StockExchangeStream will be matched for three events having price > 70 followed by one to four events having price > 75.

from every a1 = infoStock[action == "buy"]<2:> ->
                b1 = StockExchangeStream[price > 70]<3> ->
                b2 = StockExchangeStream[price > 75]<1:4>
insert into StockQuote
               a1[0].action as action, b1.price as priceA, b2[2].price as priceB


When referring to the resuts events matching the count pattern, square brackets should be used to access a specific occurrence of that event. In the above example, a1[0] will refer the 1st event of the many events that have matched the pattern and arrived via the ‘infoStock’ stream.

Copyright © WSO2 Inc. 2005-2014