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:
float d;
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 : d {
      print "Difference from mean is " + d.toString();
   }
The first fragment behaves differently than this fragment:
float d;
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 : 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.
Copyright © Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its Subsidiaries and or/its Affiliates and/or their licensors.
Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.