public static CepStream<CsvReading2> ReadFile(Application myApp) { // =============== Read data from CSV file ======================== var sensorInputConf = new TextFileReaderConfig { InputFileName = @"..\..\..\dataset.csv",// Location of the file where the data exist Delimiter = ',', CtiFrequency = 99, // CTI frequency depending on the Sampling Rate. CultureName = "el-GR", InputFieldOrders = new Collection<string>() { "SensorID", "SampleRate", "Value", "Type", "initTime", "count" } }; // Parameters for creating the Input Stream from the file. var atgs = new AdvanceTimeGenerationSettings(99, TimeSpan.FromSeconds(0), true); var ats = new AdvanceTimeSettings(atgs, null, AdvanceTimePolicy.Adjust); // I need an application ID to attach the input stream var myStream = CepStream<CsvReading>.Create( myApp, "input stream", typeof(TextFileReaderFactory), sensorInputConf, EventShape.Point, ats); // Get the channel. CAUTION: SensorID??? var valueStream1 = from e in myStream where e.SensorID=="EEG1" select e; var valueStream2 = from e in myStream where e.SensorID == "EEG2" select e; // =============== End of reading data from CSV file ======================== var stream = from e1 in valueStream1 from e2 in valueStream2 select new CsvReading2 { SensorID = e1.SensorID, Value1 = e1.Value, Value2 =e2.Value, initTime = e1.initTime }; return stream; // Or this one //var alphaStream = from e in valueStream.HoppingWindow<double>(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1)) // select e.alphaAgg<double>(a => a); //var detectionStream = from e in alphaStream // where e > AlphaDetection.alphaThreshold // select e; // ===================== END of Alpha Rhythm Detection ===================== }
/// <summary> /// Initializes a new instance of the TextFilePointInput class. /// </summary> /// <param name="configInfo">Configuration structure provided by the Factory. If /// the InputFieldOrders member is null, the default mapping will be applied.</param> /// <param name="eventType">Represents the actual event type that will be passed into this /// constructor from the query binding via the adapter factory class.</param> public TextFilePointInput(TextFileReaderConfig configInfo, CepEventType eventType) { this.streamReader = new StreamReader(configInfo.InputFileName); this.cultureInfo = new CultureInfo(configInfo.CultureName); this.delimiter = configInfo.Delimiter; this.bindtimeEventType = eventType; // Check whether all fields are contained in the config's field-to-column mapping. if (configInfo.InputFieldOrders != null && configInfo.InputFieldOrders.Count != eventType.Fields.Count) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "The configuration element InputFieldOrders should have {0} elements, but it has {1} elements", eventType.Fields.Count, configInfo.InputFieldOrders.Count)); } // Create mapping from input file field ordinal to engine type field ordinal // (the engine orders the members of the type's struct or class lexicographically) this.inputOrdinalToCepOrdinal = new Dictionary <int, int>(); for (int i = 0; i < eventType.Fields.Count; i++) { if (configInfo.InputFieldOrders != null) { CepEventTypeField engineField; if (!eventType.Fields.TryGetValue(configInfo.InputFieldOrders[i], out engineField)) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "Event type {0} doesn't have an input field named '{1}'", eventType.ShortName, configInfo.InputFieldOrders[i])); } this.inputOrdinalToCepOrdinal.Add(i, engineField.Ordinal); } else { // Use default mapping: lexicographic ordering. this.inputOrdinalToCepOrdinal.Add(i, i); } } this.consoleTracer = new System.Diagnostics.ConsoleTraceListener(); }
/// <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(); }
/// <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> /// Initializes a new instance of the TextFileEdgeInput class. /// </summary> /// <param name="configInfo">Configuration structure provided by the Factory. If /// the InputFieldOrders member is null, the default mapping will be applied.</param> /// <param name="eventType">Represents the actual event type that will be passed into this /// constructor from the query binding via the adapter factory class.</param> public TextFileEdgeInput(TextFileReaderConfig configInfo, CepEventType eventType) { this.streamReader = new StreamReader(configInfo.InputFileName); this.cultureInfo = new CultureInfo(configInfo.CultureName); this.delimiter = configInfo.Delimiter; this.bindtimeEventType = eventType; // check whether all fields are contained in the config's field-to-column mapping if (configInfo.InputFieldOrders != null && configInfo.InputFieldOrders.Count != eventType.Fields.Count) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "The configuration element InputFieldOrders should have {0} elements, but it has {1} elements", eventType.Fields.Count, configInfo.InputFieldOrders.Count)); } // Create mapping from input field ordinal to engine field ordinal this.inputOrdinalToCepOrdinal = new Dictionary<int, int>(); for (int i = 0; i < eventType.Fields.Count; i++) { if (configInfo.InputFieldOrders != null) { CepEventTypeField engineField; if (!eventType.Fields.TryGetValue(configInfo.InputFieldOrders[i], out engineField)) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "Event type {0} doesn't have an input field named '{1}'", eventType.ShortName, configInfo.InputFieldOrders[i])); } this.inputOrdinalToCepOrdinal.Add(i, engineField.Ordinal); } else { // Use default mapping: lexicographic ordering. this.inputOrdinalToCepOrdinal.Add(i, i); } } this.consoleTracer = new System.Diagnostics.ConsoleTraceListener(); }