Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var list = GetRandomList();

            foreach (var round in list)
            {
                Console.WriteLine(round);
            }
            Console.WriteLine("***********");

            //var filtered = list.Where(round => round.Steps > 5 && round.Views < 6);

            var builder = new FilteredPropertyBuilder(typeof(Round));

            var filteredFields = new List <IFilteredProperty>();


            var properties = typeof(Round).GetProperties();

            foreach (var property in properties)
            {
                filteredFields.Add(builder.Build(property.Name));
            }

            var dynamicFilter = new DynamicFilter <Round>();
            var count         = 0;

            while (true)
            {
                var prediction = new Prediction();

                if (count != 0)
                {
                    var logicalOperators = Enum.GetValues(typeof(LogicalOperators));
                    for (int i = 0; i < logicalOperators.Length; ++i)
                    {
                        Console.WriteLine($"{i}: {logicalOperators.GetValue(i)}");
                    }
                    Console.Write("Choose field index or -1 to exit: ");
                    var indexOfFieldOperator = ReadObject <int>();
                    if (indexOfFieldOperator == -1)
                    {
                        break;
                    }
                    prediction.Operator = (LogicalOperators)logicalOperators.GetValue(indexOfFieldOperator);
                }

                Console.WriteLine();
                for (int i = 0; i < filteredFields.Count; i++)
                {
                    Console.WriteLine($"{i}: {filteredFields[i].FieldName}");
                }
                Console.Write("Choose field index: ");

                var indexOfField = ReadObject <int>();

                var filteredField = filteredFields[indexOfField];
                prediction.PropertyName = filteredField.FieldName;

                Console.WriteLine();
                for (int i = 0; i < filteredField.AvailableActions.Count; i++)
                {
                    Console.WriteLine($"{i}: {filteredField.AvailableActions[i]}");
                }
                Console.Write("Choose action index: ");

                var indexOfAction = ReadObject <int>();
                prediction.CompareAction = filteredField.AvailableActions[indexOfAction];

                Console.WriteLine();
                if (prediction.NeedRight())
                {
                    Console.Write("Set right part: ");
                    var rightPart = ReadObject(filteredField.FieldType);
                    prediction.RightValue = rightPart;
                }
                dynamicFilter.AddPredict(prediction);
                ++count;
            }

            var filtered = dynamicFilter.Filter(list);

            Console.WriteLine("Lambda: ");
            Console.WriteLine(dynamicFilter.GetLambda());

            foreach (var round in filtered)
            {
                Console.WriteLine(round);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            List <Player> players = new List <Player>();

            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "Server=(localdb)\\mssqllocaldb;" +
                                        "Database=aspnet-NumberGuesserWeb;" +
                                        "Trusted_Connection=True;MultipleActiveResultSets=true";
                conn.Open();
                SqlCommand command = new SqlCommand("SELECT * FROM dbo.AspNetUsers", conn);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        players.Add(new Player
                        {
                            Login    = (string)reader[1],
                            Score    = (int)reader[17],
                            Wins     = (int)reader[18],
                            Loses    = (int)reader[16],
                            Birthday = (DateTime)reader[19]
                        });
                    }
                }
                // use the connection here
            }
            foreach (var player in players)
            {
                Console.WriteLine(player);
            }
            Console.WriteLine("**************");

            var builder        = new FilteredPropertyBuilder(typeof(Player));
            var chainCreator   = new ChainCreator(builder);
            var filteredFields = new List <IFilteredProperty>();

            filteredFields.Add(builder.Build(nameof(Player.Login)));
            filteredFields.Add(builder.Build(nameof(Player.Score)));
            filteredFields.Add(builder.Build(nameof(Player.Wins)));
            filteredFields.Add(builder.Build(nameof(Player.Loses)));
            filteredFields.Add(builder.Build(nameof(Player.Birthday)));

            var dynamicFilter = new DynamicFilter <Player>();

            while (true)
            {
                var prediction = new Prediction();
                for (int i = 0; i < filteredFields.Count; i++)
                {
                    Console.WriteLine($"{i}) {filteredFields[i].FieldName}");
                }
                Console.Write("Choose field (index) or -1 to end: ");
                var indexOfField = ReadObj <int>();
                if (indexOfField == -1)
                {
                    break;
                }
                var filteredField = filteredFields[indexOfField];
                var complex       = filteredField as IComplexFilteredProperty;
                if (complex != null)
                {
                    var properties = complex.GetProperties();
                    for (var i = 0; i < properties.Count; i++)
                    {
                        Console.WriteLine($"{i}) {properties[i].Name}");
                    }
                    Console.Write("Choose property (index): or -1 to continue ");
                    var index = ReadObj <int>();
                    if (index != -1)
                    {
                        chainCreator.CreateChain(prediction, filteredField, properties[index]);
                    }
                }

                prediction.PropertyName = filteredField.FieldName;

                for (int i = 0; i < filteredField.AvailableActions.Count; i++)
                {
                    Console.WriteLine($"{i}) {filteredField.AvailableActions[i]}");
                }
                Console.Write("Choose action (index): ");
                var indexOfAction = ReadObj <int>();
                prediction.CompareAction = filteredField.AvailableActions[indexOfAction];

                if (prediction.NeedRight())
                {
                    Console.Write("Set right part: ");
                    var rightPart = ReadObj(filteredField.FieldType);
                    prediction.RightValue = rightPart;
                }

                dynamicFilter.AddPredict(prediction);
            }

            Console.WriteLine($"lambda: {dynamicFilter.GetLambda()}");

            var filtered = dynamicFilter.Filter(players);

            foreach (var round in filtered)
            {
                Console.WriteLine(round);
            }

            Console.Read();
        }