CEP code fragment: Leapfrog queueing in Apama monitorscript

February 7th, 2008

The idea is to respond to incoming price events, but always process the latest event, skipping intermediate events. Here’s the way you can express that in Apama’s monitorscript language.

You can stage processing of an external event by caching an internal-version of the event, and enqueuing a ‘process-this-price’ event (putting it at the back of the queue). As more 3Y prices come in, they overwrite the cached event, and enqueue more ‘process-this-price’ events. When the ‘process-this-price’ events are reached, the latest price will be removed from the cache and the internal-version routed as the ‘real’ price event. Subsequent ‘process-this-price’ events result in no events because the 3Y was already removed from the cache. Here’s the code:


// Monitor-scoped events
dictionary <string,lab49.external.MarketPrice> marketPriceCache;
lab49.external.MarketPrice epx;
lab49.internal.ProcessPriceQueue ppq;

on all lab49.external.MarketPrice() :epx {
// Overwrite the temp-cache with the latest version of the event
marketPriceCache[mktPrice.instrument] :=
lab49.internal.MarketPrice(epx.market, epx.instrument, epx.bidPrice, epx.bidSize, epx.askPrice, epx.askSize);
enqueue( ProcessPriceQueue(epx.instrument) );
}

on all lab49.internal.ProcessPriceQueue() :ppq {
// Take head off queue.
if (marketPriceCache.hasKey(ppq.id)) then
{
lab49.internal.MarketPrice px := marketPriceCache[ppq.id];
marketPriceCache.remove(ppq.id);
route px;
}
}

One Response to “CEP code fragment: Leapfrog queueing in Apama monitorscript”

  1. Marco on CEP»Blog Archive » Apama Monitorscript Example Says:

    [...] To quote: http://blog.lab49.com/archives/1747: [...]