Question

I need to implement a rule engine using ESPER
For this I have to prepare query for rules (if there is any other optimized way, please suggest). Rules must be declarable as well as modifiable at run time.
Also I will have to create a UI to define rules.
Please suggest any better and optimized way of doing this.
An example:
Example
Some more rules can be defined at run time.

Was it helpful?

Solution

There is very little context to go by, but a simple solution is to simply keep the latest state of each Order and each Price and implement each rule as an engine subscription. Your engine could be initialized with the following EPL statements:

/* Minimal schema-s */
create schema LiveOrder (user string, orderid string, quantity, double, symbol string);
create schema LivePrice (symbol string, price double);

/* create two windows to store the latest order by orderid, and latest price by symbol */
create window LiveOrders.std:unique(orderid) as select * from LiveOrder;
create window LivePrices.std:unique(symbol) as select * from LivePrice;

/* insert data into the windows when data arrives */
insert into LiveOrders select * from LiveOrder;
insert into LivePrices select * from LivePrice;

At this point you will have all order and prices stored, so they can be easily "joined" for different rules. If a user requires an alert when he places and order with quantity > 100, you simply create the following EPL statement and attach a listener to it which would send the alert:

select * from LiveOrders where user='U1' and quantity > 100;

To create an alert if an order amount for any symbol exceed 10000, you do the same with this EPL:

select LiveOrders.symbol as symbol, LiveOrders.quantity*LivePrices as total from LiveOrders
inner join LivePrices on LiveOrders.symbol=LivePrices.symbol
where LiveOrders.quantity*LivePrices > 10000

Whenever the alert is no longer necessary, you simply remove the listener and destroy the EPL statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top