Exemple #1
0
        public void PrintPyramidRot90(int levels)
        {
            // Left to Right
            for (var j = 0; j <= (levels * 2) - 1; j++)
            {
                // Expand Out
                if (j <= levels)
                {
                    for (int k = 0; k < j; k++)
                    {
                        OutputAdapter.Write("*");
                    }
                }
                // Contract In
                else
                {
                    for (int l = (levels * 2) - j; l > 0; l--)
                    {
                        OutputAdapter.Write("*");
                    }
                }

                OutputAdapter.WriteLine("");
            }
        }
        public void WhenTheApplicationIsStarted_ThenItShouldDrawTheInitialInterface()
        {
            // Arrange
            var screenOutput = new List <string>();

            var writer        = new Mock <IOutputWriter>();
            var outputAdapter = new OutputAdapter(writer.Object);

            writer.Setup(output => output.WriteLine(It.IsAny <string>())).Callback((string s) => screenOutput.Add(s));

            var ticTacToeEngine = new TicTacToeEngine();
            var game            = new Game(outputAdapter, ticTacToeEngine, _firstPlayer, _secondPlayer);

            // Act
            game.Start();

            // Assert
            Assert.Equal("Deep Blue is playing X, Garry Kasparov is playing O\n", screenOutput.ElementAt(0));
            Assert.Equal("     |     |      ", screenOutput.ElementAt(1));
            Assert.Equal("     |     |      ", screenOutput.ElementAt(2));
            Assert.Equal("_____|_____|_____ ", screenOutput.ElementAt(3));
            Assert.Equal("     |     |      ", screenOutput.ElementAt(4));
            Assert.Equal("     |     |      ", screenOutput.ElementAt(5));
            Assert.Equal("_____|_____|_____ ", screenOutput.ElementAt(6));
            Assert.Equal("     |     |      ", screenOutput.ElementAt(7));
            Assert.Equal("     |     |      ", screenOutput.ElementAt(8));
            Assert.Equal("     |     |      ", screenOutput.ElementAt(9));
            Assert.Equal("\nThe atmosphere is tense, press any key to start the game...", screenOutput.ElementAt(10));
            // Unusual for me to be checking view content like this, would normally use a tool like selenium in a web environment
            // Unusual for me to be checking multiple things with positioning, however it seems the logical choice in this instance
        }
Exemple #3
0
        /// <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);
        }
        private static void BindAndRunQuery <TPayload>(string queryName, CepStream <TPayload> queryStream, EventShape outputEventShape, List <string> inputFields, List <string> outputFields)
        {
            var inputConfig = new CsvInputConfig
            {
                InputFileName        = @"..\..\..\TollInput.txt",
                Delimiter            = new char[] { ',' },
                BufferSize           = 4096,
                CtiFrequency         = 1,
                CultureName          = "en-US",
                Fields               = inputFields,
                NonPayloadFieldCount = 2,
                StartTimePos         = 1,
                EndTimePos           = 2
            };

            // The adapter recognizes empty filename as a write to console.
            var outputConfig = new CsvOutputConfig
            {
                OutputFileName = string.Empty,
                Delimiter      = new string[] { "\t" },
                CultureName    = "en-US",
                Fields         = outputFields
            };

            // Note - Please change the instance name to the one you have chosen during installation
            using (var server = Server.Create("Default"))
            {
                Application application = server.CreateApplication("TollStationApp");

                // set up input and output adapters
                InputAdapter  inputAdapter  = application.CreateInputAdapter <CsvInputFactory>("input", "Csv Input Source");
                OutputAdapter outputAdapter = application.CreateOutputAdapter <CsvOutputFactory>("output", "Csv Output");

                // set up the query template
                QueryTemplate queryTemplate = application.CreateQueryTemplate("QueryTemplate", string.Empty, queryStream);

                // set up advance time settings to enqueue CTIs
                var advanceTimeGenerationSettings = new AdvanceTimeGenerationSettings(inputConfig.CtiFrequency, TimeSpan.Zero, true);
                var advanceTimeSettings           = new AdvanceTimeSettings(advanceTimeGenerationSettings, null, AdvanceTimePolicy.Adjust);

                // Bind query template to input and output streams
                QueryBinder queryBinder = new QueryBinder(queryTemplate);
                queryBinder.BindProducer <TollReading>("TollStream", inputAdapter, inputConfig, EventShape.Interval, advanceTimeSettings);
                queryBinder.AddConsumer("outputStream", outputAdapter, outputConfig, outputEventShape, StreamEventOrder.FullyOrdered);

                // Create a runnable query by binding the query template to the input stream of interval events,
                // and to an output stream of fully ordered point events (through an output adapter instantiated
                // from the output factory class)
                Query query = application.CreateQuery(queryName, "Hello Toll Query", queryBinder);

                RunQuery(query);
            }
        }
Exemple #5
0
        public void PrintPyramid(int levels)
        {
            var j = 0;

            while (j < levels)
            {
                j += 1;
                var spaces   = String.Join(" ", Enumerable.Repeat("", (levels - j) + 2));
                var asteriks = String.Join("", Enumerable.Repeat("*", (j * 2) - 1));
                OutputAdapter.WriteLine(spaces + asteriks);
            }
        }
Exemple #6
0
        /// <summary>
        /// Feeds specified neural network with data from InputAdapter and writes
        /// output using OutputAdapter </summary>
        /// <param name="neuralNet"> neural network </param>
        /// <param name="in"> input data source </param>
        /// <param name="out"> output data target   </param>
        public static void process(NeuralNetwork neuralNet, InputAdapter @in, OutputAdapter @out)
        {
            double[] input;
            while ((input = @in.readInput()) != null)
            {
                neuralNet.Input = input;
                neuralNet.calculate();
                double[] output = neuralNet.Output;
                @out.writeOutput(output);
            }

            @in.close();
            @out.close();
        }
Exemple #7
0
        /// <summary>
        /// Creates and binds a query with a single input stream.
        /// </summary>
        private static Query CreateQuery(Application application, CepStream <EventType> streamDefinition, Uri inputAddress, Uri outputAddress)
        {
            QueryTemplate template     = application.CreateQueryTemplate("QueryTemplate", null, streamDefinition);
            QueryBinder   binder       = new QueryBinder(template);
            InputAdapter  inputAdapter = application.CreateInputAdapter <WcfInputAdapterFactory>("InputAdapter", null);

            binder.BindProducer("Input", inputAdapter, inputAddress, EventShape.Point);
            OutputAdapter outputAdapter = application.CreateOutputAdapter <WcfOutputAdapterFactory>("OutputAdapter", null);

            binder.AddConsumer("Output", outputAdapter, outputAddress, EventShape.Point, StreamEventOrder.FullyOrdered);
            Query query = application.CreateQuery("Query", null, binder);

            return(query);
        }
        public void WhenPlayStarts_ThePlayShouldAlternateBetweenPlayers()
        {
            // Arrange
            var writer          = new Mock <IOutputWriter>();
            var outputAdapter   = new OutputAdapter(writer.Object);
            var ticTacToeEngine = new TicTacToeEngine();
            var game            = new Game(outputAdapter, ticTacToeEngine, _firstPlayer, _secondPlayer);

            // Act
            game.Start();

            // Assert
            var firstPlayerMoves = ticTacToeEngine.Moves.Where((item, index) => index % 2 == 0);

            Assert.True(firstPlayerMoves.SequenceEqual(_firstPlayer.Moves));

            var secondPlayerMoves = ticTacToeEngine.Moves.Where((item, index) => index % 2 != 0);

            Assert.True(secondPlayerMoves.SequenceEqual(_secondPlayer.Moves));
        }
Exemple #9
0
        public void PrintPyramid(int levels)
        {
            // Top to Bottom
            for (int i = 0; i <= levels; i++)
            {
                //spaces
                for (int k = 0; k < (levels - i) + 1; k++)
                {
                    OutputAdapter.Write(" ");
                }

                // asterik
                for (int j = 0; j < (i * 2) - 1; j++)
                {
                    OutputAdapter.Write("*");
                }

                OutputAdapter.WriteLine();
            }
        }
Exemple #10
0
        public void PrintPyramidRot180(int levels)
        {
            // Bottom to Top
            for (int i = levels; i > 0; i--)
            {
                // Spaces
                for (int k = 0; k < (levels - i) + 1; k++)
                {
                    OutputAdapter.Write(" ");
                }

                // Asteriks
                for (int j = 0; j < (i * 2) - 1; j++)
                {
                    OutputAdapter.Write("*");
                }

                OutputAdapter.WriteLine();
            }
        }
Exemple #11
0
        public void PrintPyramidRot270(int levels)
        {
            // Right to Left
            for (var j = 0; j <= (levels * 2) - 1; j++)
            {
                // Expand Out
                if (j <= levels)
                {
                    // Spaces
                    for (int kk = 0; kk < (levels - j); kk++)
                    {
                        OutputAdapter.Write(" ");
                    }
                    // Asteriks
                    for (int k = 0; k < j; k++)
                    {
                        OutputAdapter.Write("*");
                    }
                }
                // Retract In
                else
                {
                    // Spaces
                    for (int ll = 0; ll < j - levels; ll++)
                    {
                        OutputAdapter.Write(" ");
                    }
                    // Asteriks
                    for (int l = (levels * 2) - j; l > 0; l--)
                    {
                        OutputAdapter.Write("*");
                    }
                }

                OutputAdapter.WriteLine();
            }
        }
Exemple #12
0
        /// <summary>
        /// Example of using a user-defined filter
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createUserFilterExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream<StockQuote>.Create("input");
            var filteredCepStream = from e in input
                                    where UserDefinedFilter(e.FieldID)
                                    select e;
            var queryTemplate = application.CreateQueryTemplate("userFilterExampleTemplate", "Description...", filteredCepStream);

            var queryBinder = new QueryBinder(queryTemplate);
            queryBinder.BindProducer<StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer<StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("userFilterExampleQuery", "Description...", queryBinder);
            return query;
        }
Exemple #13
0
        /// <summary>
        /// Compare USD and SEK stock prices using given exchange rate. Example of a cross join.
        /// </summary>
        /// <param name="application"></param>
        /// <param name="ericUSDConfig"></param>
        /// <param name="ericSEKConfig"></param>
        /// <param name="USDSEKConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createCrossJoinExampleQuery(Application application, StockQuoteInputConfig ericUSDConfig, StockQuoteInputConfig ericSEKConfig, StockQuoteInputConfig USDSEKConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var ericUSDStream = CepStream <StockQuote> .Create("ericUSDStream");

            var ericSEKStream = CepStream <StockQuote> .Create("ericSEKStream");

            var USDSEKStream = CepStream <StockQuote> .Create("USDSEKStream");

            var ericRecalcCepStream = from eUSD in ericUSDStream
                                      from eXch in USDSEKStream // Cross join
                                      where eUSD.FieldID == "Close"
                                      select new StockQuote()
            {
                StockID = "ERIC-Recalc",
                FieldID = "Close",
                Value   = eUSD.Value * eXch.Value                         // Convert ERIC USD quote to SEK
            };

            var ericCompareCepStream = from eRecalc in ericRecalcCepStream
                                       from eSEK in ericSEKStream
                                       where eSEK.FieldID == "Close"
                                       select new StockQuote()
            {
                StockID = "ERIC-Compare",
                FieldID = "Diff",
                Value   = eSEK.Value - eRecalc.Value
            };
            var queryTemplate = application.CreateQueryTemplate("ericCompareTemplate", "Description...", ericCompareCepStream);

            var queryBinder = new QueryBinder(queryTemplate);

            queryBinder.BindProducer <StockQuote>("ericUSDStream", inputAdapter, ericUSDConfig, EventShape.Point);
            queryBinder.BindProducer <StockQuote>("ericSEKStream", inputAdapter, ericSEKConfig, EventShape.Point);
            queryBinder.BindProducer <StockQuote>("USDSEKStream", inputAdapter, USDSEKConfig, EventShape.Point);
            queryBinder.AddConsumer <StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("ericCompareQuery", "Description...", queryBinder);

            return(query);
        }
Exemple #14
0
        /// <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"))
            {
                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("TrafficJoinSample");

                    // Create query logic as a query template
                    Console.WriteLine("Registering LINQ query template");
                    QueryTemplate queryTemplate = CreateQueryTemplate(application);

                    // Register adapter factories, so that the engine will be able
                    // to instantiate adapters when the query starts.
                    Console.WriteLine("Registering Adapter Factories");
                    InputAdapter  csvInputAdapter  = application.CreateInputAdapter <TextFileReaderFactory>("CSV Input", "Reading tuples from a CSV file");
                    OutputAdapter csvOutputAdapter = application.CreateOutputAdapter <TextFileWriterFactory>("CSV Output", "Writing result events to a CSV file");

                    // bind query to event producers and consumers
                    QueryBinder queryBinder = BindQuery(csvInputAdapter, csvOutputAdapter, queryTemplate);

                    // Create bound query that can be run
                    Console.WriteLine("Registering bound query");
                    Query query = application.CreateQuery("TrafficSensorQuery", "Minute average count, filtered by location threshold", queryBinder);

                    Console.WriteLine("Start query");

                    // Start the 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/TrafficJoinSample/Query/TrafficSensorQuery")), Console.Out);

                    query.Stop();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Console.ReadLine();
                }
            }

            Console.WriteLine("\npress enter to exit application");
            Console.ReadLine();
        }
Exemple #15
0
        /// <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;
        }
Exemple #16
0
        /// <summary>
        /// Example of a grouping and calculation of averages for the groups
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createGroupApplyExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream <StockQuote> .Create("input");

            var ericUSDGroupCepStream = from e in input
                                        group e by e.FieldID into eGroup
                                        from w in eGroup.HoppingWindow(TimeSpan.FromDays(7), TimeSpan.FromDays(1), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                        select new StockQuote()
            {
                StockID = "ERIC 7-day avg",
                FieldID = eGroup.Key,
                Value   = w.Avg(e => e.Value)
            };
            var queryTemplate = application.CreateQueryTemplate("groupApplyExampleTemplate", "Description...", ericUSDGroupCepStream);

            var queryBinder = new QueryBinder(queryTemplate);

            queryBinder.BindProducer <StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer <StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("groupApplyExampleQuery", "Description...", queryBinder);

            return(query);
        }
Exemple #17
0
 public void RegisterAdapter(OutputAdapter adapter)
 {
     Adapter = adapter;
     RegisterAdapter(((AdapterSPI)adapter).EPServiceProvider);
 }
Exemple #18
0
        /// <summary>
        /// Example of detecting when stock price falls more than 10% in 7 days
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createBigLooserExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream <StockQuote> .Create("input");

            var bigLooserCepStream = (from e1 in input
                                      from e2 in input.ShiftEventTime(e => e.StartTime.AddDays(7))
                                      where e1.FieldID == "Close" && e2.FieldID == "Close"
                                      select new StockQuote()
            {
                StockID = "ERIC > 10% drop",
                FieldID = "Close",
                Value = (e1.Value - e2.Value) / e2.Value * 100
            }).Where(e => e.Value < -10);
            var queryTemplate = application.CreateQueryTemplate("bigLooserExampleTemplate", "Description...", bigLooserCepStream);

            var queryBinder = new QueryBinder(queryTemplate);

            queryBinder.BindProducer <StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer <StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("bigLooserExampleQuery", "Description...", queryBinder);

            return(query);
        }
Exemple #19
0
 public void PrintPyramidRot90(int levels)
 {
     OutputAdapter.WriteLine("COMINGSOON");
     //throw new NotImplementedException();
 }
Exemple #20
0
        /// <summary>
        /// Example of using a user-defined filter
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createUserFilterExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream <StockQuote> .Create("input");

            var filteredCepStream = from e in input
                                    where UserDefinedFilter(e.FieldID)
                                    select e;
            var queryTemplate = application.CreateQueryTemplate("userFilterExampleTemplate", "Description...", filteredCepStream);

            var queryBinder = new QueryBinder(queryTemplate);

            queryBinder.BindProducer <StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer <StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("userFilterExampleQuery", "Description...", queryBinder);

            return(query);
        }
Exemple #21
0
        /// <summary>
        /// Example of using a user-defined aggregate
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createStandardDeviationExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream <StockQuote> .Create("input");

            var stddevCepStream = from w in input.Where(e => e.FieldID == "Close")
                                  .HoppingWindow(TimeSpan.FromDays(7), TimeSpan.FromDays(1), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                  select new StockQuote()
            {
                StockID = "ERIC",
                FieldID = "7-day Stddev",
                Value   = w.StandardDeviation()
            };
            var queryTemplate = application.CreateQueryTemplate("standardDeviationExampleTemplate", "Description...", stddevCepStream);

            var queryBinder = new QueryBinder(queryTemplate);

            queryBinder.BindProducer <StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer <StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("standardDeviationExampleQuery", "Description...", queryBinder);

            return(query);
        }
Exemple #22
0
        /// <summary>
        /// Example of detecting when stock price falls more than 10% in 7 days
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createBigLooserExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream<StockQuote>.Create("input");
            var bigLooserCepStream = (from e1 in input
                                      from e2 in input.ShiftEventTime(e => e.StartTime.AddDays(7))
                                      where e1.FieldID == "Close" && e2.FieldID == "Close"
                                      select new StockQuote()
                                      {
                                          StockID = "ERIC > 10% drop",
                                          FieldID = "Close",
                                          Value = (e1.Value - e2.Value) / e2.Value * 100
                                      }).Where(e => e.Value < -10);
            var queryTemplate = application.CreateQueryTemplate("bigLooserExampleTemplate", "Description...", bigLooserCepStream);

            var queryBinder = new QueryBinder(queryTemplate);
            queryBinder.BindProducer<StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer<StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("bigLooserExampleQuery", "Description...", queryBinder);
            return query;
        }
Exemple #23
0
        /// <summary>
        /// Compare USD and SEK stock prices using given exchange rate. Example of a cross join.
        /// </summary>
        /// <param name="application"></param>
        /// <param name="ericUSDConfig"></param>
        /// <param name="ericSEKConfig"></param>
        /// <param name="USDSEKConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createCrossJoinExampleQuery(Application application, StockQuoteInputConfig ericUSDConfig, StockQuoteInputConfig ericSEKConfig, StockQuoteInputConfig USDSEKConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var ericUSDStream = CepStream<StockQuote>.Create("ericUSDStream");
            var ericSEKStream = CepStream<StockQuote>.Create("ericSEKStream");
            var USDSEKStream = CepStream<StockQuote>.Create("USDSEKStream");
            var ericRecalcCepStream = from eUSD in ericUSDStream
                                      from eXch in USDSEKStream // Cross join
                                      where eUSD.FieldID == "Close"
                                      select new StockQuote()
                                      {
                                          StockID = "ERIC-Recalc",
                                          FieldID = "Close",
                                          Value = eUSD.Value * eXch.Value // Convert ERIC USD quote to SEK
                                      };

            var ericCompareCepStream = from eRecalc in ericRecalcCepStream
                                       from eSEK in ericSEKStream
                                       where eSEK.FieldID == "Close"
                                       select new StockQuote()
                                       {
                                           StockID = "ERIC-Compare",
                                           FieldID = "Diff",
                                           Value = eSEK.Value - eRecalc.Value
                                       };
            var queryTemplate = application.CreateQueryTemplate("ericCompareTemplate", "Description...", ericCompareCepStream);

            var queryBinder = new QueryBinder(queryTemplate);
            queryBinder.BindProducer<StockQuote>("ericUSDStream", inputAdapter, ericUSDConfig, EventShape.Point);
            queryBinder.BindProducer<StockQuote>("ericSEKStream", inputAdapter, ericSEKConfig, EventShape.Point);
            queryBinder.BindProducer<StockQuote>("USDSEKStream", inputAdapter, USDSEKConfig, EventShape.Point);
            queryBinder.AddConsumer<StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("ericCompareQuery", "Description...", queryBinder);
            return query;
        }
Exemple #24
0
        /// <summary>
        /// Example of a grouping and calculation of averages for the groups
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createGroupApplyExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream<StockQuote>.Create("input");
            var ericUSDGroupCepStream = from e in input
                                        group e by e.FieldID into eGroup
                                        from w in eGroup.HoppingWindow(TimeSpan.FromDays(7), TimeSpan.FromDays(1), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                        select new StockQuote()
                                        {
                                            StockID = "ERIC 7-day avg",
                                            FieldID = eGroup.Key,
                                            Value = w.Avg(e => e.Value)
                                        };
            var queryTemplate = application.CreateQueryTemplate("groupApplyExampleTemplate", "Description...", ericUSDGroupCepStream);

            var queryBinder = new QueryBinder(queryTemplate);
            queryBinder.BindProducer<StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer<StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("groupApplyExampleQuery", "Description...", queryBinder);
            return query;
        }
Exemple #25
0
        /// <summary>
        /// Example of using a user-defined aggregate
        /// </summary>
        /// <param name="application"></param>
        /// <param name="inputConfig"></param>
        /// <param name="outputConfig"></param>
        /// <param name="inputAdapter"></param>
        /// <param name="outputAdapter"></param>
        /// <returns></returns>
        private static Query createStandardDeviationExampleQuery(Application application, StockQuoteInputConfig inputConfig, StockQuoteOutputConfig outputConfig, InputAdapter inputAdapter, OutputAdapter outputAdapter)
        {
            var input = CepStream<StockQuote>.Create("input");
            var stddevCepStream = from w in input.Where(e => e.FieldID == "Close")
                                                             .HoppingWindow(TimeSpan.FromDays(7), TimeSpan.FromDays(1), HoppingWindowOutputPolicy.ClipToWindowEnd)
                                  select new StockQuote()
                                  {
                                      StockID = "ERIC",
                                      FieldID = "7-day Stddev",
                                      Value = w.StandardDeviation()
                                  };
            var queryTemplate = application.CreateQueryTemplate("standardDeviationExampleTemplate", "Description...", stddevCepStream);

            var queryBinder = new QueryBinder(queryTemplate);
            queryBinder.BindProducer<StockQuote>("input", inputAdapter, inputConfig, EventShape.Point);
            queryBinder.AddConsumer<StockQuote>("output", outputAdapter, outputConfig, EventShape.Point, StreamEventOrder.ChainOrdered);

            var query = application.CreateQuery("standardDeviationExampleQuery", "Description...", queryBinder);
            return query;
        }