/// <inheritdoc/>
        public override FactRuleCollection GetRules()
        {
            var rules = new FactRuleCollection();

            rules.AddRange(UiSettingsRules());
            return(rules);
        }
Beispiel #2
0
        public void Initialize()
        {
            UserDB = new List <User>
            {
                new User {
                    Id = 1, Email = "*****@*****.**", Name = "John Cornero"
                },
                new User {
                    Id = 2, Email = "*****@*****.**", Name = "Liza Famez"
                },
                new User {
                    Id = 3, Email = "*****@*****.**", Name = "Judy Gonzalez"
                },
            };

            MovieDB = new List <Movie>
            {
                new Movie {
                    Id = 1, Name = "My Hero Academia: Heroes Rising", Cost = 11,
                },
                new Movie {
                    Id = 2, Name = "Bad Boys for Life", Cost = 12,
                },
                new Movie {
                    Id = 3, Name = "Bloodshot", Cost = 13,
                },
            };

            DiscountDB = new List <Discount>
            {
                new Discount {
                    Id = 1, MovieDiscount = 5, MovieId = 1, UserId = 3
                },
                new Discount {
                    Id = 2, MovieDiscount = 4, MovieId = 2, UserId = 2
                },
                new Discount {
                    Id = 3, MovieDiscount = 3, MovieId = 3, UserId = 1
                },
            };

            Rules = new FactRuleCollection
            {
                // If we have a user, then we can find out his email.
                (UserFact fact) => new UserEmailFact(fact.Value.Email),

                // If we have an email, then we can recognize the user.
                (UserEmailFact fact) => new UserFact(UserDB.Single(user => user.Email == fact.Value)),

                // If we have a movie ID, you can find a movie.
                (MovieIdFact fact) => new MovieFact(MovieDB.Single(movie => movie.Id == fact.Value)),

                // If we have a movie, you can find a movie ID.
                (MovieFact fact) => new MovieIdFact(fact.Value.Id),

                // If we have a user and a movie, then we can add a user discount amount.
                (UserFact userFact, MovieFact movieFact) =>
                {
                    int result = 0;

                    var dicounts = DiscountDB.Where(dicount => dicount.UserId == userFact.Value.Id && dicount.MovieId == movieFact.Value.Id).ToList();

                    if (dicounts != null)
                    {
                        foreach (var dicount in dicounts)
                        {
                            result += dicount.MovieDiscount;
                        }

                        if (result > movieFact.Value.Cost)
                        {
                            result = movieFact.Value.Cost;
                        }
                    }

                    return(new MovieDiscountFact(result));
                },

                // If we have a movie and a discount size, then we can calculate the cost of the movie.
                (MovieFact movieFact, MovieDiscountFact movieDiscountFact) => new MoviePurchasePriceFact(movieFact.Value.Cost - movieDiscountFact.Value),

                // Let's add a rule to demonstrate asynchronous communication
                async(MovieFact movieFact, MovieDiscountFact movieDiscountFact) =>
                {
                    // Simulate work
                    await Task.Delay(200);

                    return(new MoviePurchasePriceFactAsync(movieFact.Value.Cost - movieDiscountFact.Value));
                },
            };

            Factory = new FactFactory();
            Factory.Rules.AddRange(Rules);
        }