Apama 10.7.2 | Developing Apama Applications | Developing Apama Applications in EPL | Working with Streams and Stream Queries | Troubleshooting and stream query coding guidelines | Avoid duplication of stream source template expressions
 
Avoid duplication of stream source template expressions
When you are maintaining code, you might add a stream query whose streamExpr is an event template that is already used in a query elsewhere in the same monitor. However, duplicated stream source template expressions do not always produce the behavior you want. Consider the following two code fragments:
stream<float> means := from t in all Temperature()
   within 10.0
   select mean(t.temperature);
from t in all Temperature()
   from m in means select t-m as d {
      print "Difference from mean is " + d.toString();
   }
The first fragment behaves differently than this fragment:
stream<float> temperatures := all Temperature();
stream<float> means := from t in temperatures
   within 10.0
   select mean(t.temperature);
from t in temperatures
   from m in means
   select t-m as d {
      print "Difference from mean is " + d.toString();
   }
Of the two code fragments above, the second one has the desired behavior. The first example creates two event listeners, one for each all Temperature() clause. Each listener matches each incoming Temperature event, but the listeners trigger independently, one after the other. This means that there is no time when the second query has an item in each of its source streams. Consequently, the cross-join never produces any output.
In the second example, there is a single Temperature event listener that places matching events in the temperatures stream. The temperatures stream is the source stream for two queries. Now both source streams of the last query contain items at the same time and the query generates output.