/// <summary> /// Binds the query template to the specified adapters. /// </summary> /// <param name="inputAdapter">Input adapter metadata object to bind to all query template inputs.</param> /// <param name="outputAdapter">Input adapter metadata object to bind to the query template output.</param> /// <param name="queryTemplate">Query template to bind to adapters.</param> /// <returns>The query binder.</returns> private static QueryBinder BindQuery(InputAdapter inputAdapter, OutputAdapter outputAdapter, QueryTemplate queryTemplate) { // Create a query binder, wrapping the query template. QueryBinder queryBinder = new QueryBinder(queryTemplate); // Define the runtime configuration for both input adapters. var sensorInputConf = new TextFileReaderConfig { InputFileName = @"..\..\..\TrafficSensors.csv", Delimiter = ',', CtiFrequency = 9, CultureName = "en-US", InputFieldOrders = new Collection <string>() { "SensorId", "AverageSpeed", "VehicularCount" } }; var locationInputConf = new TextFileReaderConfig { InputFileName = @"..\..\..\TrafficSensorLocations.csv", Delimiter = ',', CtiFrequency = 100, CultureName = "en-US", InputFieldOrders = new Collection <string>() { "SensorId", "LocationId" } }; // Define the runtime configuration for the output adapter. // Specify an empty file name, which will just dump the output // events on the console. Also pass an event handle name to // synchronize the shutdown. var outputConf = new TextFileWriterConfig { OutputFileName = string.Empty, Delimiter = '\t' }; // Bind input adapters to query template's input streams, // applying runtime configuration. // In this example, the given input file for sensor input // contains interval events (each sensor reading has a start // and end time), while the location input is represented by // edge events (for each event, the end time is not known in // advance). queryBinder.BindProducer("sensorInput", inputAdapter, sensorInputConf, EventShape.Interval); queryBinder.BindProducer("locationInput", inputAdapter, locationInputConf, EventShape.Edge); // Bind output adapter to query, applying runtime // configuration. queryBinder.AddConsumer <TextFileWriterConfig>("queryresult", outputAdapter, outputConf, EventShape.Point, StreamEventOrder.FullyOrdered); return(queryBinder); }
/// <summary> /// Main program. /// </summary> internal static void Main() { Console.WriteLine("Creating CEP Server"); // Creating an embedded server. // // In order to connect to an existing server instead of creating a // new one, Server.Connect() can be used, with the appropriate // endpoint exposed by the remote server. // // NOTE: replace "Default" with the instance name you provided // during StreamInsight setup. using (Server server = Server.Create("Default")) { // Define the runtime configuration for input adapter var stockTicksInputConf = new TextFileReaderConfig { InputFileName = @"..\..\..\StockTicks.csv", Delimiter = ',', CtiFrequency = 9, CultureName = "en-US", InputFieldOrders = new Collection <string>() { "StockSymbol", "StockPrice", "PriceChange" } }; // Define the runtime configuration for the output adapter. // Specify an empty file name, which will just dump the output // events on the console. var outputConf = new TextFileWriterConfig { OutputFileName = string.Empty, Delimiter = '\t' }; try { // Create application in the server. The application will serve // as a container for actual CEP objects and queries. Console.WriteLine("Creating CEP Application"); Application application = server.CreateApplication("PatternDetectorSample"); // Create an input stream directly from the adapter, var stockStream = CepStream <StockTick> .Create("stockTicksInput", typeof(TextFileReaderFactory), stockTicksInputConf, EventShape.Point); // Execute pattern detection over the stream var patternResult = from w in stockStream.TumblingWindow(TimeSpan.FromHours(1), HoppingWindowOutputPolicy.ClipToWindowEnd) select w.AfaOperator <StockTick, RegisterType>(typeof(AfaEqualDownticksUpticks).AssemblyQualifiedName.ToString()); // Alternate example of using a count window for pattern detection: // var patternResult = from w in stockStream.CountByStartTimeWindow(4, CountWindowOutputPolicy.PointAlignToWindowEnd) // select w.AfaOperator<StockTick, RegisterType>(typeof(AfaEqualDownticksUpticks).AssemblyQualifiedName.ToString()); // Turn this logic into a query, outputting to the tracer output adapter. var query = patternResult.ToQuery( application, "EqualDownticksUpticksQuery", "Detecting equal number of downticks and upticks in a stock quote stream", typeof(TextFileWriterFactory), outputConf, EventShape.Point, StreamEventOrder.FullyOrdered); // Start the query Console.WriteLine("Starting query"); query.Start(); // Wait for the query to be suspended - that is the state // it will be in as soon as the output adapter stops due to // the end of the stream. DiagnosticView dv = server.GetDiagnosticView(query.Name); while ((string)dv[DiagnosticViewProperty.QueryState] == "Running") { // Sleep for 1s and check again Thread.Sleep(1000); dv = server.GetDiagnosticView(query.Name); } // Retrieve some diagnostic information from the CEP server // about the query. Console.WriteLine(string.Empty); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/EventManager")), Console.Out); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/PlanManager")), Console.Out); RetrieveDiagnostics(server.GetDiagnosticView(new Uri("cep:/Server/Application/PatternDetectorSample/Query/EqualDownticksUpticksQuery")), Console.Out); query.Stop(); } catch (Exception e) { Console.WriteLine(e); Console.ReadLine(); } } Console.WriteLine("\nPress enter to exit application"); Console.ReadLine(); }