Пример #1
0
        private int CanApply(Rule r)
        {
            int maxLevel = -1;

            foreach (IFact f in r.Premises)
            {
                IFact foundFact = fDB.Search(f.Name);
                if (foundFact == null)
                {
                    if (f.Question != null)
                    {
                        foundFact = FactFactory.Fact(f, this);
                        fDB.AddFact(foundFact);
                        maxLevel = Math.Max(maxLevel, 0);
                    }
                    else
                    {
                        return(-1);
                    }
                }

                if (!foundFact.Value.Equals(f.Value))
                {
                    return(-1);
                }
                else
                {
                    maxLevel = Math.Max(maxLevel, foundFact.Level);
                }
            }

            return(maxLevel);
        }
Пример #2
0
    public void AddRule(string ruleStr)
    {
        // Séparation nom : règle
        String[] splitName = ruleStr.Split(new String[] { " : " },
                                           StringSplitOptions.RemoveEmptyEntries);
        if (splitName.Length == 2)
        {
            String name = splitName[0];
            // Séparation premisses THEN conclusion
            String[] splitPremConcl = splitName[1].Split(new String[]
                                                         { "IF ", ", ", " THEN " }, StringSplitOptions.RemoveEmptyEntries);
            if (splitPremConcl.Length == 2)
            {
                // Lecture des premisses
                List <IFact> premises    = new List <IFact>();
                String[]     premisesStr = splitPremConcl[0].Split(new String[] { " AND " },
                                                                   StringSplitOptions.RemoveEmptyEntries);
                foreach (String prem in premisesStr)
                {
                    IFact premise = FactFactory.Fact(prem);
                    premises.Add(premise);
                }

                // Lecture de la conclusion
                String conclusionStr = splitPremConcl[1].Trim();
                IFact  conclusion    = FactFactory.Fact(conclusionStr);

                // Création de la règle et ajout
                rDB.AddRule(new Rule(name, premises, conclusion));
            }
        }
    }
Пример #3
0
    private int CanApply(Rule r)
    {
        int maxlevel = -1;

        // On vérifie si chaque prémises est vraie
        foreach (IFact f in r.Premises)
        {
            IFact foundFact = fDB.Search(f.Name());
            if (foundFact == null)
            {
                // Ce fait n'existe pas dans la base actuellement
                if (f.Question() != null)
                {
                    // On le demande à l'utilisateur
                    // et on l'ajoute en base
                    foundFact = FactFactory.Fact(f, this);
                    maxlevel  = Math.Max(maxlevel, 0);
                }
                else
                {
                    // On sait que la régle ne s'applique pas
                    return(-1);
                }
            }

            // On a un fait en base, on vérfie sa valeur
            if (!foundFact.Value().Equals(f.Value()))
            {
                // Elle ne correspond pas
                return(-1);
            }
            else
            {
                // Elle correspond
                maxlevel = Math.Max(maxlevel, foundFact.Level());
            }
        }

        return(maxlevel);
    }
Пример #4
0
        public void AddRule(string ruleStr)
        {
            string[] splitName = ruleStr.Split(new string[] { " : " }, StringSplitOptions.RemoveEmptyEntries);
            if (splitName.Length == 2)
            {
                string   name           = splitName[0];
                string[] splitPremConcl = splitName[1].Split(new string[] { "IF", " THEN " }, StringSplitOptions.RemoveEmptyEntries);
                if (splitPremConcl.Length == 2)
                {
                    List <IFact> premises    = new List <IFact>();
                    string[]     premisesStr = splitPremConcl[0].Split(new string[] { " AND " }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string prem in premisesStr)
                    {
                        IFact premise = FactFactory.Fact(prem);
                        premises.Add(premise);
                    }

                    string conclStr   = splitPremConcl[1].Trim();
                    IFact  conclusion = FactFactory.Fact(conclStr);

                    rDB.Add(new Rule(name, premises, conclusion));
                }
            }
        }
Пример #5
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);
        }