Пример #1
0
        public void GiveBestDiscount()
        {
            Agenda agenda = new Agenda();
            agenda.ConflictResolutionStrategy = new SalienceResolver();

            Variable customer = new Variable("customer");
            Variable purchases = new Variable("purchases");
            Variable discount = new Variable("discount");

            Production SilverCustomer = new Production("Silver Customer");
            SilverCustomer.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            SilverCustomer.AddConditionToLHS(new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 10));
            SilverCustomer.AddConditionToRHS(new AssertCondition("R1", customer, "is", "silver"));
            agenda.AddProduction(SilverCustomer);

            Production GoldCustomer = new Production("Gold Customer");
            GoldCustomer.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            GoldCustomer.AddConditionToLHS(new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 25));
            GoldCustomer.AddConditionToRHS(new AssertCondition("R1", customer, "is", "gold"));
            agenda.AddProduction(GoldCustomer);

            Production PlatinumCustomer = new Production("Platinum Customer");
            PlatinumCustomer.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            PlatinumCustomer.AddConditionToLHS(new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 50));
            PlatinumCustomer.AddConditionToRHS(new AssertCondition("R1", customer, "is", "platinum"));
            agenda.AddProduction(PlatinumCustomer);

            Production SilverDiscount = new Production("Silver Discount");
            SilverDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "is", "silver"));
            SilverDiscount.AddConditionToRHS(new AssertCondition("R1", customer, "give discount", 5));
            agenda.AddProduction(SilverDiscount);

            Production GoldDiscount = new Production("Gold Discount");
            GoldDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "is", "gold"));
            GoldDiscount.AddConditionToRHS(new AssertCondition("R1", customer, "give discount", 10));
            agenda.AddProduction(GoldDiscount);

            Production PlatinumDiscount = new Production("Platinum Discount");
            PlatinumDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "is", "platinum"));
            PlatinumDiscount.AddConditionToRHS(new AssertCondition("R1", customer, "give discount", 15));
            agenda.AddProduction(PlatinumDiscount);

            Mutex BestDiscount = new Mutex("Best Discount");
            //BestDiscount.AddAggregator(new Max(), customer, new StringTerm("give discount"), discount);
            BestDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "give discount", discount));
            //BestDiscount.AddConditionToLHS(new condition("C2", ConditionType.Function, customer, "funcMax", discount));
            BestDiscount.AddConditionToRHS(new InvokeCondition("R1", customer, "Customer.GiveDiscount", discount));
            //BestDiscount.AddEvaluator(new Max(), customer, new StringTerm("give discount"), discount);
            BestDiscount.Evaluator = new Max(customer, new StringTerm("give discount"), discount);
            agenda.AddMutex(BestDiscount);

            WME wme1 = new WME("W1");
            wme1.Fields[0] = "joe";
            wme1.Fields[1] = "purchases";
            wme1.Fields[2] = "1";
            agenda.AddFact(wme1);

            WME wme2 = new WME("W2");
            wme2.Fields[0] = "ted";
            wme2.Fields[1] = "purchases";
            wme2.Fields[2] = "10";
            agenda.AddFact(wme2);

            WME wme3 = new WME("W3");
            wme3.Fields[0] = "ed";
            wme3.Fields[1] = "purchases";
            wme3.Fields[2] = "11";
            agenda.AddFact(wme3);

            WME wme4 = new WME("W4");
            wme4.Fields[0] = "phil";
            wme4.Fields[1] = "purchases";
            wme4.Fields[2] = "18";
            agenda.AddFact(wme4);

            WME wme5 = new WME("W5");
            wme5.Fields[0] = "mary";
            wme5.Fields[1] = "purchases";
            wme5.Fields[2] = "22";
            agenda.AddFact(wme5);

            WME wme6 = new WME("W6");
            wme6.Fields[0] = "jane";
            wme6.Fields[1] = "purchases";
            wme6.Fields[2] = "25";
            agenda.AddFact(wme6);

            WME wme7 = new WME("W7");
            wme7.Fields[0] = "fred";
            wme7.Fields[1] = "purchases";
            wme7.Fields[2] = "50";
            agenda.AddFact(wme7);

            WME wme8 = new WME("W8");
            wme8.Fields[0] = "harry";
            wme8.Fields[1] = "purchases";
            wme8.Fields[2] = "55";
            agenda.AddFact(wme8);

            WME wme9 = new WME("W9");
            wme9.Fields[0] = "jon";
            wme9.Fields[1] = "purchases";
            wme9.Fields[2] = "99";
            agenda.AddFact(wme9);

            agenda.Run();

            agenda.VisualizeNetworkToFile(@"C:\Temp\GiveBestDiscount.log", false);

            Assert.IsTrue(agenda.ActivatedRuleCount == 5, "Rule did not fire.");
            Assert.IsTrue(agenda.InferredFacts.Count == 21, "Bad");
            Assert.IsTrue(BestDiscount.InferredFacts.Count == 7, "Bad");
        }
Пример #2
0
        public void GiveBestDiscount()
        {
            Agenda agenda = new Agenda();

            agenda.ConflictResolutionStrategy = new SalienceResolver();


            Variable customer  = new Variable("customer");
            Variable purchases = new Variable("purchases");
            Variable discount  = new Variable("discount");

            Production SilverCustomer = new Production("Silver Customer");

            SilverCustomer.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            SilverCustomer.AddConditionToLHS(new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 10));
            SilverCustomer.AddConditionToRHS(new AssertCondition("R1", customer, "is", "silver"));
            agenda.AddProduction(SilverCustomer);

            Production GoldCustomer = new Production("Gold Customer");

            GoldCustomer.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            GoldCustomer.AddConditionToLHS(new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 25));
            GoldCustomer.AddConditionToRHS(new AssertCondition("R1", customer, "is", "gold"));
            agenda.AddProduction(GoldCustomer);

            Production PlatinumCustomer = new Production("Platinum Customer");

            PlatinumCustomer.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            PlatinumCustomer.AddConditionToLHS(new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 50));
            PlatinumCustomer.AddConditionToRHS(new AssertCondition("R1", customer, "is", "platinum"));
            agenda.AddProduction(PlatinumCustomer);

            Production SilverDiscount = new Production("Silver Discount");

            SilverDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "is", "silver"));
            SilverDiscount.AddConditionToRHS(new AssertCondition("R1", customer, "give discount", 5));
            agenda.AddProduction(SilverDiscount);

            Production GoldDiscount = new Production("Gold Discount");

            GoldDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "is", "gold"));
            GoldDiscount.AddConditionToRHS(new AssertCondition("R1", customer, "give discount", 10));
            agenda.AddProduction(GoldDiscount);

            Production PlatinumDiscount = new Production("Platinum Discount");

            PlatinumDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "is", "platinum"));
            PlatinumDiscount.AddConditionToRHS(new AssertCondition("R1", customer, "give discount", 15));
            agenda.AddProduction(PlatinumDiscount);

            Mutex BestDiscount = new Mutex("Best Discount");

            //BestDiscount.AddAggregator(new Max(), customer, new StringTerm("give discount"), discount);
            BestDiscount.AddConditionToLHS(new PositiveCondition("C1", customer, "give discount", discount));
            //BestDiscount.AddConditionToLHS(new condition("C2", ConditionType.Function, customer, "funcMax", discount));
            BestDiscount.AddConditionToRHS(new InvokeCondition("R1", customer, "Customer.GiveDiscount", discount));
            //BestDiscount.AddEvaluator(new Max(), customer, new StringTerm("give discount"), discount);
            BestDiscount.Evaluator = new Max(customer, new StringTerm("give discount"), discount);
            agenda.AddMutex(BestDiscount);

            WME wme1 = new WME("W1");

            wme1.Fields[0] = "joe";
            wme1.Fields[1] = "purchases";
            wme1.Fields[2] = "1";
            agenda.AddFact(wme1);

            WME wme2 = new WME("W2");

            wme2.Fields[0] = "ted";
            wme2.Fields[1] = "purchases";
            wme2.Fields[2] = "10";
            agenda.AddFact(wme2);

            WME wme3 = new WME("W3");

            wme3.Fields[0] = "ed";
            wme3.Fields[1] = "purchases";
            wme3.Fields[2] = "11";
            agenda.AddFact(wme3);

            WME wme4 = new WME("W4");

            wme4.Fields[0] = "phil";
            wme4.Fields[1] = "purchases";
            wme4.Fields[2] = "18";
            agenda.AddFact(wme4);

            WME wme5 = new WME("W5");

            wme5.Fields[0] = "mary";
            wme5.Fields[1] = "purchases";
            wme5.Fields[2] = "22";
            agenda.AddFact(wme5);

            WME wme6 = new WME("W6");

            wme6.Fields[0] = "jane";
            wme6.Fields[1] = "purchases";
            wme6.Fields[2] = "25";
            agenda.AddFact(wme6);

            WME wme7 = new WME("W7");

            wme7.Fields[0] = "fred";
            wme7.Fields[1] = "purchases";
            wme7.Fields[2] = "50";
            agenda.AddFact(wme7);

            WME wme8 = new WME("W8");

            wme8.Fields[0] = "harry";
            wme8.Fields[1] = "purchases";
            wme8.Fields[2] = "55";
            agenda.AddFact(wme8);

            WME wme9 = new WME("W9");

            wme9.Fields[0] = "jon";
            wme9.Fields[1] = "purchases";
            wme9.Fields[2] = "99";
            agenda.AddFact(wme9);

            agenda.Run();

            agenda.VisualizeNetworkToFile(@"C:\Temp\GiveBestDiscount.log", false);

            Assert.IsTrue(agenda.ActivatedRuleCount == 5, "Rule did not fire.");
            Assert.IsTrue(agenda.InferredFacts.Count == 21, "Bad");
            Assert.IsTrue(BestDiscount.InferredFacts.Count == 7, "Bad");
        }