Using stream variables
Because streams are values in EPL, you can pass stream references between the code elements within a monitor. This is useful when writing services. A common service (that is, a service used by two or more monitors) is normally implemented using a service event. This event contains the logic to implement the service or to access an external service. A stream can be used as part of the interface to the service: the stream and stream query specification is encapsulated within the service event code and a reference to the stream created by this code is returned, from the service action to the client monitor code, as the return value of an action call. This is illustrated in the following code example.
01. event Temperature { string sensorId; float temperature; }
02. event TemperatureRange { string sensorId; float minTemperature; float maxTemperature; }
01. using com.apama.aggregates.max; using com.apama.aggregates.min;
02. event TemperatureRangeService {
03. float period;
04. action init( string id, float _period ) returns stream<TemperatureRange> {
05. period := _period;
06. return
07. from t in all Temperature(sensorId=id) within period every period
08. select TemperatureRange(id,min(t.temperature),max(t.temperature));
09. }
10. }
11. monitor UsesTemperatureRangeService {
12. action onload() {
13. TemperatureRangeService service := new TemperatureRangeService;
14. stream<TemperatureRange> ranges := service.init("S001",60.0);
15. TemperatureRange range;
16. from r in ranges select r : range { print range.toString(); }
17. }