Apama 10.15.0 | Developing Apama Applications | Developing Apama Applications in EPL | Working with Streams and Stream Queries | Defining stream queries | Simple example of a stream network
 
Simple example of a stream network
Sometimes a single from statement is all that is required to achieve your goal. For example, to obtain a volume-weighted average price (VWAP) for a stock, you can add the following from statement to a monitor:
from t in all Tick(symbol="APMA")
   within period
   select wavg(t.price,t.volume) as vwap {
      processNewVwap(vwap); }
Often, however, you want to use the output from one query as the input to another query. For example, here is an extract from the Statistical Arbitrage demo, which is available from the Welcome page:
spreads :=
from a in all com.apama.demo.marketdata.Depth(symbol=order.Instrument_1)
retain 1
from b in all com.apama.demo.marketdata.Depth(symbol=order.Instrument_2)
retain 1
select (a.midPrices[0] - b.midPrices[0]);
stream<MeanSd> meanSds :=
from s in spreads within 20.0 select MeanSd( mean(s), stddev(s) );
stream<integer> comparison :=
from s in spreads from m in meanSds select
compareSpreadAndBands(s, m.mean, m.sd, order.Std_Dev_Multiplier);
stream<integer> prevComparison :=
from c in comparison retain 1 select rstream c;
from c in comparison from p in prevComparison
where c!=p select c as instruction {
if state = WAIT_FOR_SPREAD and instruction = HOLD {
monitorState();
}
if state = MONITOR and instruction != HOLD {
waitForOrders(instruction);
}
}
When queries are connected like this, the set of connected queries is referred to as a stream network.
A stream network is strictly within a monitor instance. Routing an event takes that event entirely out of the stream network since the event would not be received in the same network activation even if it is received by the same monitor. Spawning a monitor makes any stream variables point to inert streams, so it is not possible to refer to a stream network from a different monitor instance.