예제 #1
0
        public void TestAddProduction()
        {
            Production prod = new Production();
            prod.Label = "find-stack-of-two-blocks-to-the-left-of-a-red-block";
            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");

            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", z, "color", "red"));

            AssertCondition rhs = new AssertCondition("C4", x, "is", "on top");
            rhs.ConditionType = ConditionType.Assert;

            prod.AddConditionToRHS(rhs);

            Agenda agenda = new Agenda();
            agenda.ConflictResolutionStrategy = new SalienceResolver();
            agenda.AddProduction(prod);

            WME wme1 = new WME("W1");
            wme1.Fields[0] = "B1";
            wme1.Fields[1] = "on";
            wme1.Fields[2] = "B2";
            agenda.AddFact(wme1);

            WME wme2 = new WME("W2");
            wme2.Fields[0] = "B1";
            wme2.Fields[1] = "on";
            wme2.Fields[2] = "B3";
            agenda.AddFact(wme2);

            WME wme3 = new WME("W3");
            wme3.Fields[0] = "B1";
            wme3.Fields[1] = "color";
            wme3.Fields[2] = "red";
            agenda.AddFact(wme3);

            WME wme4 = new WME("W4");
            wme4.Fields[0] = "B2";
            wme4.Fields[1] = "on";
            wme4.Fields[2] = "table";
            agenda.AddFact(wme4);

            WME wme5 = new WME("W5");
            wme5.Fields[0] = "B2";
            wme5.Fields[1] = "left of";
            wme5.Fields[2] = "B3";
            agenda.AddFact(wme5);

            WME wme6 = new WME("W6");
            wme6.Fields[0] = "B2";
            wme6.Fields[1] = "color";
            wme6.Fields[2] = "blue";
            agenda.AddFact(wme6);

            WME wme7 = new WME("W7");
            wme7.Fields[0] = "B3";
            wme7.Fields[1] = "left of";
            wme7.Fields[2] = "B4";
            agenda.AddFact(wme7);

            WME wme8 = new WME("W8");
            wme8.Fields[0] = "B3";
            wme8.Fields[1] = "on";
            wme8.Fields[2] = "table";
            agenda.AddFact(wme8);

            WME wme9 = new WME("W9");
            wme9.Fields[0] = "B3";
            wme9.Fields[1] = "color";
            wme9.Fields[2] = "red";
            agenda.AddFact(wme9);

            agenda.Run();

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

            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Rule did not fire.");
            Assert.IsTrue(agenda.InferredFacts.Count == 1, "Bad");
        }
예제 #2
0
        public void NegativeConditionTest()
        {
            Production prod = new Production();
            prod.Label = "find-stack-of-two-blocks-to-the-left-of-a-red-block";
            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");
            //Variable c = new Variable("c");

            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            //prod.AddConditionToLHS(new condition("C3", z, "color", c));

            NegativeCondition neg = new NegativeCondition("C3", z, "color", "blue");
            neg.ConditionType = ConditionType.Negative;
            //neg.IsPositive = false;
            //neg.IsNegative = true;
            prod.AddConditionToLHS(neg);

            AssertCondition rhs = new AssertCondition("C4", x, "is", "on top");
            rhs.ConditionType = ConditionType.Assert;

            prod.AddConditionToRHS(rhs);

            Rete rete = new Rete();
            rete.AddProduction(prod);

            WME wme1 = new WME("W1");
            wme1.Fields[0] = "B1";
            wme1.Fields[1] = "on";
            wme1.Fields[2] = "B2";
            rete.AddWME(wme1);

            WME wme2 = new WME("W2");
            wme2.Fields[0] = "B1";
            wme2.Fields[1] = "on";
            wme2.Fields[2] = "B3";
            rete.AddWME(wme2);

            WME wme3 = new WME("W3");
            wme3.Fields[0] = "B1";
            wme3.Fields[1] = "color";
            wme3.Fields[2] = "red";
            rete.AddWME(wme3);

            WME wme4 = new WME("W4");
            wme4.Fields[0] = "B2";
            wme4.Fields[1] = "on";
            wme4.Fields[2] = "table";
            rete.AddWME(wme4);

            WME wme5 = new WME("W5");
            wme5.Fields[0] = "B2";
            wme5.Fields[1] = "left of";
            wme5.Fields[2] = "B3";
            rete.AddWME(wme5);

            WME wme6 = new WME("W6");
            wme6.Fields[0] = "B2";
            wme6.Fields[1] = "color";
            wme6.Fields[2] = "blue";
            rete.AddWME(wme6);

            WME wme7 = new WME("W7");
            wme7.Fields[0] = "B3";
            wme7.Fields[1] = "left of";
            wme7.Fields[2] = "B4";
            rete.AddWME(wme7);

            WME wme8 = new WME("W8");
            wme8.Fields[0] = "B3";
            wme8.Fields[1] = "on";
            wme8.Fields[2] = "table";
            rete.AddWME(wme8);

            WME wme9 = new WME("W9");
            wme9.Fields[0] = "B3";
            wme9.Fields[1] = "color";
            wme9.Fields[2] = "red";
            rete.AddWME(wme9);

            //WME wme10 = new WME("W10");
            //wme10.fields[0] = "B4";
            //wme10.fields[1] = "color";
            //wme10.fields[2] = "green";
            //rete.add_wme(wme10);

            //rete_node dummy = rete.DummyTopNode;

            NetworkPrinter printer = new NetworkPrinter();
            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\NegativeConditionTest.log", false))
            {
                writer.Write(printer.Output);
                writer.Flush();
            }

            //Assert.IsTrue(rete.RulesThatFired.Count == 1, "Rule did not fire.");
            Assert.IsTrue(prod.InferredFacts.Count == 1, "Wrong number of conclusions");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
예제 #3
0
        public void RemoveWME()
        {
            Production prod = new Production();
            prod.Label = "find-stack-of-two-blocks-to-the-left-of-a-red-block";
            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");

            prod.AddConditionToLHS(new PositiveCondition("C1", x, "on", y));
            prod.AddConditionToLHS(new PositiveCondition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", z, "color", "red"));
            AssertCondition rhs = new AssertCondition("C4", x, "is", "on top");
            rhs.ConditionType = ConditionType.Assert;

            prod.AddConditionToRHS(rhs);
            Rete rete = new Rete();

            rete.AddProduction(prod);

            WME wme1 = new WME("W1");
            wme1.Fields[0] = "B1";
            wme1.Fields[1] = "on";
            wme1.Fields[2] = "B2";
            rete.AddWME(wme1);

            WME wme2 = new WME("W2");
            wme2.Fields[0] = "B1";
            wme2.Fields[1] = "on";
            wme2.Fields[2] = "B3";
            rete.AddWME(wme2);

            WME wme3 = new WME("W3");
            wme3.Fields[0] = "B1";
            wme3.Fields[1] = "color";
            wme3.Fields[2] = "red";
            rete.AddWME(wme3);

            WME wme4 = new WME("W4");
            wme4.Fields[0] = "B2";
            wme4.Fields[1] = "on";
            wme4.Fields[2] = "table";
            rete.AddWME(wme4);

            WME wme5 = new WME("W5");
            wme5.Fields[0] = "B2";
            wme5.Fields[1] = "left of";
            wme5.Fields[2] = "B3";
            rete.AddWME(wme5);

            WME wme6 = new WME("W6");
            wme6.Fields[0] = "B2";
            wme6.Fields[1] = "color";
            wme6.Fields[2] = "blue";
            rete.AddWME(wme6);

            WME wme7 = new WME("W7");
            wme7.Fields[0] = "B3";
            wme7.Fields[1] = "left of";
            wme7.Fields[2] = "B4";
            rete.AddWME(wme7);

            WME wme8 = new WME("W8");
            wme8.Fields[0] = "B3";
            wme8.Fields[1] = "on";
            wme8.Fields[2] = "table";
            rete.AddWME(wme8);

            WME wme9 = new WME("W9");
            wme9.Fields[0] = "B3";
            wme9.Fields[1] = "color";
            wme9.Fields[2] = "red";
            rete.AddWME(wme9);

            Assert.IsTrue(prod.InferredFacts.Count == 1, "Wrong number of conclusions");

            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");

            rete.RemoveWME(wme9);

            Assert.IsTrue(prod.InferredFacts.Count == 0, "Wrong number of conclusions");

            Assert.IsTrue(rete.WorkingMemory.Count == 8, "Bad");
        }
예제 #4
0
        public void BasicMetaTest()
        {
            Agenda agenda = new Agenda();
            agenda.ConflictResolutionStrategy = new SalienceResolver();

            Production prod = new Production();
            prod.Label = "Gold Customer";
            Variable customer = new Variable("customer");
            Variable purchases = new Variable("purchases");
            //Variable y = new Variable("y");
            //Variable x = new Variable("x");

            prod.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            FunctionCondition gt = new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 10);
            gt.ConditionType = ConditionType.Function;
            prod.AddConditionToLHS(gt);
            AssertCondition rhs = new AssertCondition("R1", customer, "is", "gold");
            rhs.ConditionType = ConditionType.Assert;
            prod.AddConditionToRHS(rhs);
            agenda.AddProduction(prod);

            Production prod1 = new Production();
            prod1.Label = "Platinum Customer";

            prod1.AddConditionToLHS(new PositiveCondition("C1", customer, "purchases", purchases));
            FunctionCondition gt1 = new FunctionCondition("C2", purchases, new FuncTerm("funcGreaterThan", new funcGreaterThan()), 20);
            gt1.ConditionType = ConditionType.Function;
            prod1.AddConditionToLHS(gt1);
            AssertCondition rhs1 = new AssertCondition("R1", customer, "is", "Platinum");
            rhs1.ConditionType = ConditionType.Assert;
            prod1.AddConditionToRHS(rhs1);
            agenda.AddProduction(prod1);

            //production mut = new production();
            //mut.label = "mutex";
            //mut.AddConditionToLHS(new condition("C1", customer, "is", "Platinum"));
            //mut.AddConditionToLHS(new condition("C2", customer, "is", "gold"));

            //condition gtx = new condition("C3", y, "funcNotEquals", x);
            //gt1.ConditionType = ConditionType.Function;
            ////prod1.AddConditionToLHS(gtx);
            //condition rhsx = new condition("R1", customer, "is", "Platinum");
            //rhs1.ConditionType = ConditionType.Assert;
            //prod1.AddConditionToRHS(rhsx);
            //agenda.AddProduction(mut);

            //agenda.AddMutexNode(prod, new condition("R1", customer, "is", "gold"), prod1, new condition("R1", customer, "is", "Platinum"), new NotEquals());

            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\BasicOverrideTest.log", false);

            Assert.IsTrue(agenda.ActivatedRuleCount == 2, "Rule did not fire.");
            Assert.IsTrue(agenda.InferredFacts.Count == 12, "Bad");
        }