Apama 10.5.3 | Developing Apama Applications | Developing Apama Applications in EPL | Working with Streams and Stream Queries | Using dynamic expressions in stream queries | Examples of using dynamic expressions in stream queries | Example of altering a threshold
 
Example of altering a threshold
The following code fragment shows part of a monitor that accepts requests from external entities to monitor the value of the trades for a given symbol. After you create a monitor like this, an external entity can, at any time, change the thresholds at which the monitor recognizes the trade as a high value trade.
monitor CountHighValueTicks {
   float threshold;
   action onload() {
      on all CountHighValueTicksRequest() as r spawn
         monitorHighValueTicks (r);
         // Simplified. Assumes no duplicate requests.
   }
   action monitorHighValueTicks(CountHighValueTicksRequest r) {
      threshold := r.threshold;
      stream<Tick> filtered := from t in all Ticks(symbol=v.symbol)
                               where t.price*t.volume > threshold
                               select t;
      from t in filtered within 60.0 every 60.0 select count() as c {
         print "Count of high value trades in previous minute: " +
            c.toString();
      }
      on all CountHighValueTicksRequestUpdate(symbol=r.symbol) as u {
         threshold := u.threshold ; }
   }
}
This example uses two queries. The first query filters out any ticks with values below the threshold. The second query accumulates the high-value ticks received in the last minute and outputs the count of high-value ticks in that period. This could have been written as a single query with the filtering performed after the window operation. For example:
from t in all Ticks(symbol=v.symbol) within 60.0 every 60.0
   where t.price*t.volume > threshold select count();
However this query's window contains all of the low value ticks received in the last 60 seconds, as well as the high value ticks. This is not an optimal use of memory resources. Hence the two query approach is preferred.
Alternatively, you can specify an embedded query to amalgamate the two queries into a single statement:
from t in
   (from t2 in ticks where t2.price*t2.volume > threshold select t2 )
   within 60.0 every 60.0
   select count() as c { ... }
The parentheses around the embedded query are optional.