/// <summary> /// Contains the query logic in form of a query template. /// </summary> /// <param name="application">Application to host the query template.</param> /// <returns>The new query template object.</returns> private static QueryTemplate CreateQueryTemplate(Application application) { // Create stream objects as basis for query template // specification. The specified names will be used when binding // the query template's inputs to event producers. CepStream <SensorReading> sensorStream = CepStream <SensorReading> .Create("sensorInput"); CepStream <LocationData> locationStream = CepStream <LocationData> .Create("locationInput"); // Extend duration of each sensor reading, so that they fall in // a one-minute sliding window. Group by sensor ID and calculate the // average vehicular count per group within each window. // Include the grouping key in the aggregation result. var avgCount = from oneMinReading in sensorStream.AlterEventDuration(e => TimeSpan.FromMinutes(1)) group oneMinReading by oneMinReading.SensorId into oneGroup from eventWindow in oneGroup.SnapshotWindow(SnapshotWindowOutputPolicy.Clip) select new { avgCount = eventWindow.Avg(e => e.VehicularCount), SensorId = oneGroup.Key }; // Join sensors and locations. Moreover, filter the count // result by a threshold, which is looked up based on the // sensor location through a user-defined function. var joined = from averageEvent in avgCount join locationData in locationStream on averageEvent.SensorId equals locationData.SensorId where averageEvent.avgCount > UserFunctions.LocationCountThreshold(locationData.LocationId) select new { SensorId = locationData.SensorId, LocationID = locationData.LocationId, VehicularCount = averageEvent.avgCount }; return(application.CreateQueryTemplate("SampleQueryTemplate", string.Empty, joined)); }