예제 #1
0
        public void Test2()
        {
            CustomerFactory       factory   = new CustomerFactory(new ObjectContext(CreateDataStore()));
            ObjectList <Customer> customers = factory.Find("Status.Name = {0}", "Active");

            Assert.IsNotNull(customers.Count >= 2, "Should not be at least 2.");

            Agenda agenda = new Agenda();

            agenda.ConflictResolutionStrategy = new SalienceResolver();
            agenda.LoadRulesFromAssemblies    = false;
            agenda.AddObjects(customers);

            Production WhatsMyName = new Production("Whats My Name");

            WhatsMyName.Salience = 1;
            Variable customer_var  = new Variable("Customer");
            Variable customer_name = new Variable("Name");

            WhatsMyName.AddConditionToLHS(new PositiveCondition("C2", customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("C3", customer_name, new FuncTerm("funcEquals", new funcEquals()), "Joe Blow"));
            WhatsMyName.AddConditionToRHS(new InvokeCondition("R1", customer_var, "Shout", customer_name));
            WhatsMyName.AddConditionToRHS(new SetCondition("R2", customer_var, "Remarks", "Hello from Expergent."));
            agenda.AddProduction(WhatsMyName);

            Production WhatsMyName1 = new Production();

            WhatsMyName1.Salience = 10;
            WhatsMyName1.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            WhatsMyName1.AddConditionToRHS(new InvokeCondition(customer_var, "Shout", "Squid"));
            WhatsMyName1.AddConditionToRHS(new SetCondition(customer_var, "Remarks", "Squid Text"));
            agenda.AddProduction(WhatsMyName1);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 22, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 6, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 2, "Bad");
            Assert.IsTrue(customers.FindFirst("Name = {0}", "Joe Blow").Result != null && customers.FindFirst("Name = {0}", "Joe Blow").Result.Contains("Squid"), "Did not invoke method.");
            Assert.IsTrue(customers.FindFirst("Name = {0}", "Joe Blow").Remarks != null && customers.FindFirst("Name = {0}", "Joe Blow").Remarks.Contains("Squid Text"), "Did not invoke method.");
        }
예제 #2
0
        public void TimeStampConflictResolver()
        {
            CustomerFactory factory  = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer        customer = factory.FindFirst("Name = {0}", "Joe Blow");

            Assert.IsNotNull(customer, "SHould have found customer.");
            Agenda agenda = new Agenda();

            agenda.ConflictResolutionStrategy = new TimeStampConflictResolver();
            agenda.LoadRulesFromAssemblies    = false;
            agenda.AddObject(customer);

            Production WhatsMyName = new Production("Sets 'Default Text'.");

            WhatsMyName.Salience = 1;
            Variable customer_var  = new Variable("$Customer");
            Variable customer_name = new Variable("$Customer.Name");

            WhatsMyName.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("Joe Blow", new FuncTerm("funcEquals", new funcEquals()), customer_name));
            WhatsMyName.AddConditionToRHS(new InvokeCondition(customer_var, "Shout", customer_name));
            WhatsMyName.AddConditionToRHS(new SetCondition(customer_var, "Remarks", "Default Text."));
            agenda.AddProduction(WhatsMyName);

            Production WhatsMyName1 = new Production("Sets 'Squid Text'.");

            WhatsMyName1.Salience = 1;
            WhatsMyName1.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            WhatsMyName1.AddConditionToRHS(new InvokeCondition(customer_var, "Shout", "Squid"));
            WhatsMyName1.AddConditionToRHS(new SetCondition(customer_var, "Remarks", "Squid Text"));
            WhatsMyName1.AddConditionToRHS(new AssertCondition(customer_var, "Poop", "Deck"));
            agenda.AddProduction(WhatsMyName1);

            agenda.Run();

            Assert.IsTrue(agenda.TotalFacts == 11);
            Assert.IsTrue(agenda.InferredFacts.Count == 1);
            Assert.IsTrue(agenda.ActivatedRuleCount == 2);
            Assert.IsTrue(agenda.ActionsTaken.Count == 4);
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0);
            Assert.IsTrue(customer.Remarks.Equals("Squid Text"), "Should be 'Default Text.'.");
        }
예제 #3
0
        public List<Production> GetProductions()
        {
            List<Production> list = new List<Production>();

            Production WhatsMyName = new Production("WhatsMyName");
            WhatsMyName.Salience = 1;
            Variable customer_var = new Variable("Customer");
            Variable customer_name = new Variable("Customer.Name");
            Variable site_status = new Variable("Customer.Status");
            Variable site_status_name = new Variable("Customer.Status.Name");

            WhatsMyName.AddConditionToLHS(new PositiveCondition("C1", customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F1", customer_name, new FuncTerm("funcEquals", new funcEquals()), "Joe Blow"));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C2", customer_var, "$Customer.Status", site_status));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C3", site_status, "$Customer.Status.Name", site_status_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F2", site_status_name, new FuncTerm("funcEquals", new funcEquals()), "Active"));
            WhatsMyName.AddConditionToRHS(new SetCondition(customer_var, "Remarks", customer_name));

            list.Add(WhatsMyName);

            return list;
        }
예제 #4
0
        public void DigDeep()
        {
            CustomerFactory factory  = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer        customer = factory.FindFirst("Name = {0}", "Joe Blow");

            Agenda agenda = new Agenda();

            agenda.ConflictResolutionStrategy = new TimeStampConflictResolver();
            agenda.LoadRulesFromAssemblies    = false;
            agenda.AddObject(customer);

            Production WhatsMyName = new Production("WhatsMyName");

            WhatsMyName.Salience = 1;
            Variable customer_var     = new Variable("Customer");
            Variable customer_name    = new Variable("Customer.Name");
            Variable site_status      = new Variable("Customer.Status");
            Variable site_status_name = new Variable("Customer.Status.Name");

            WhatsMyName.AddConditionToLHS(new PositiveCondition("C1", customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F1", customer_name, new FuncTerm("funcEquals", new funcEquals()), "Joe Blow"));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C2", customer_var, "$Customer.Status", site_status));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C3", site_status, "$Customer.Status.Name", site_status_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F2", site_status_name, new FuncTerm("funcEquals", new funcEquals()), "Active"));
            WhatsMyName.AddConditionToRHS(new SetCondition(customer_var, "Remarks", customer_name));
            agenda.AddProduction(WhatsMyName);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11);
            Assert.IsTrue(agenda.InferredFacts.Count == 0);
            Assert.IsTrue(agenda.ActivatedRuleCount == 1);
            Assert.IsTrue(agenda.ActionsTaken.Count == 1);
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0);
            Assert.IsTrue(customer.Remarks.Equals("Joe Blow"), "Should be 'Default Text.'.");
        }
예제 #5
0
        public List <Production> GetProductions()
        {
            List <Production> list = new List <Production>();

            Production WhatsMyName = new Production("WhatsMyName");

            WhatsMyName.Salience = 1;
            Variable customer_var     = new Variable("Customer");
            Variable customer_name    = new Variable("Customer.Name");
            Variable site_status      = new Variable("Customer.Status");
            Variable site_status_name = new Variable("Customer.Status.Name");

            WhatsMyName.AddConditionToLHS(new PositiveCondition("C1", customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F1", customer_name, new FuncTerm("funcEquals", new funcEquals()), "Joe Blow"));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C2", customer_var, "$Customer.Status", site_status));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C3", site_status, "$Customer.Status.Name", site_status_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F2", site_status_name, new FuncTerm("funcEquals", new funcEquals()), "Active"));
            WhatsMyName.AddConditionToRHS(new SetCondition(customer_var, "Remarks", customer_name));

            list.Add(WhatsMyName);

            return(list);
        }
예제 #6
0
        public void CollectionTest()
        {
            CustomerFactory factory  = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer        customer = factory.FindFirst("Name = {0}", "Joe Blow");

            IList <WME> factlist = customer.GenerateFactsForRootObject();

            Assert.IsTrue(factlist.Count == 11, "Wrong count.");

            Agenda agenda = new Agenda();

            agenda.LoadRulesFromAssemblies = false;

            Variable customer_var = new Variable("Customer");
            Variable orders       = new Variable("Orders");
            Variable order        = new Variable("Order");
            Variable orderItems   = new Variable("OrderItems");
            Variable orderItem    = new Variable("OrderItem");
            Variable product      = new Variable("Product");
            //Variable comment = new Variable("Comment");
            Variable description = new Variable("Description");

            Production mostSimple = new Production("CollectionTest");

            mostSimple.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Orders", orders));
            mostSimple.AddConditionToLHS(new PositiveCondition(orders, "$Customer.Orders.Order", order));
            mostSimple.AddConditionToLHS(new PositiveCondition(order, "$Customer.Orders.Order.OrderItems", orderItems));
            mostSimple.AddConditionToLHS(new PositiveCondition(orderItems, "$Customer.Orders.Order.OrderItems.OrderItem", orderItem));
            mostSimple.AddConditionToLHS(new PositiveCondition(orderItem, "$Customer.Orders.Order.OrderItems.OrderItem.Product", product));
            mostSimple.AddConditionToLHS(new PositiveCondition(product, "$Customer.Orders.Order.OrderItems.OrderItem.Product.Description", description));
            mostSimple.AddConditionToLHS(new FunctionCondition(description, new FuncTerm("funcEquals", new funcEquals()), "Troll Food"));
            mostSimple.AddConditionToRHS(new InvokeCondition("shout", customer_var, "Shout", "Yipee"));
            mostSimple.AddConditionToRHS(new AssertCondition("Wow", customer_var, "eats", description));
            agenda.AddProduction(mostSimple);

            agenda.AddFacts(factlist);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 1, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
        }
예제 #7
0
        public void FirstAggTest()
        {
            CustomerFactory factory  = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer        customer = factory.FindFirst("Name = {0}", "Joe Blow");

            IList <WME> factlist = customer.GenerateFactsForRootObject();

            Assert.IsTrue(factlist.Count == 11, "Wrong count.");

            Agenda agenda = new Agenda();

            agenda.LoadRulesFromAssemblies = false;

            Variable customer_var = new Variable("Customer");
            Variable orders       = new Variable("Orders");
            Variable order        = new Variable("Order");
            Variable val          = new Variable("?Value");

            Aggregator ag = new Aggregator("Count of customer orders.");

            ag.GroupBy            = customer_var;
            ag.AggregatorFunction = new Count("$Customer.Orders.Order", "$Customer.Orders.Count");
            ag.AddConditionToLHS(new AND(customer_var, "$Customer.Orders", orders));
            ag.AddConditionToLHS(new AND(orders, "$Customer.Orders.Order", order));
            agenda.AddAggregator(ag);

            Production mostSimple = new Production("If Big Spender");

            mostSimple.AddConditionToLHS(new AND(customer_var, "$Customer.Orders.Count", val));
            mostSimple.AddConditionToRHS(new INVOKE("shout", customer_var, "Shout", val));
            agenda.AddProduction(mostSimple);

            agenda.AddFacts(factlist);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 1, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
        }
예제 #8
0
        public void TestOfMax()
        {
            CustomerFactory       factory   = new CustomerFactory(new ObjectContext(CreateDataStore()));
            ObjectList <Customer> customers = factory.FindAllObjects();

            Agenda agenda = new Agenda();

            agenda.LoadRulesFromAssemblies = false;
            agenda.AddObjects(customers);

            Variable customer_var = new Variable("Customer");
            Variable orders       = new Variable("Orders");
            Variable order        = new Variable("Order");
            Variable orderamount  = new Variable("OrderAmount");
            Variable max          = new Variable("OrderAmountMaximum");

            Aggregator ag = new Aggregator("Maximum of customer order OrderAmount.");

            ag.GroupBy            = customer_var;
            ag.AggregatorFunction = new Maximum("$Customer.Orders.Order.OrderAmount");
            ag.AddConditionToLHS(new AND(customer_var, "$Customer.Orders", orders));
            ag.AddConditionToLHS(new AND(orders, "$Customer.Orders.Order", order));
            ag.AddConditionToLHS(new AND(order, "$Customer.Orders.Order.OrderAmount", orderamount));
            agenda.AddAggregator(ag);

            Production mostSimple = new Production("If Big Spender");

            mostSimple.AddConditionToLHS(new AND(customer_var, "Maximum", max));
            mostSimple.AddConditionToRHS(new INVOKE("shout", customer_var, "Shout", max));
            agenda.AddProduction(mostSimple);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 22, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 2, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            foreach (Customer customer in customers)
            {
                Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
            }
        }
예제 #9
0
        public void CollectionTest()
        {
            CustomerFactory factory = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer customer = factory.FindFirst("Name = {0}", "Joe Blow");

            IList<WME> factlist = customer.GenerateFactsForRootObject();
            Assert.IsTrue(factlist.Count == 11, "Wrong count.");

            Agenda agenda = new Agenda();
            agenda.LoadRulesFromAssemblies = false;

            Variable customer_var = new Variable("Customer");
            Variable orders = new Variable("Orders");
            Variable order = new Variable("Order");
            Variable orderItems = new Variable("OrderItems");
            Variable orderItem = new Variable("OrderItem");
            Variable product = new Variable("Product");
            //Variable comment = new Variable("Comment");
            Variable description = new Variable("Description");

            Production mostSimple = new Production("CollectionTest");
            mostSimple.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Orders", orders));
            mostSimple.AddConditionToLHS(new PositiveCondition(orders, "$Customer.Orders.Order", order));
            mostSimple.AddConditionToLHS(new PositiveCondition(order, "$Customer.Orders.Order.OrderItems", orderItems));
            mostSimple.AddConditionToLHS(new PositiveCondition(orderItems, "$Customer.Orders.Order.OrderItems.OrderItem", orderItem));
            mostSimple.AddConditionToLHS(new PositiveCondition(orderItem, "$Customer.Orders.Order.OrderItems.OrderItem.Product", product));
            mostSimple.AddConditionToLHS(new PositiveCondition(product, "$Customer.Orders.Order.OrderItems.OrderItem.Product.Description", description));
            mostSimple.AddConditionToLHS(new FunctionCondition(description, new FuncTerm("funcEquals", new funcEquals()), "Troll Food"));
            mostSimple.AddConditionToRHS(new InvokeCondition("shout", customer_var, "Shout", "Yipee"));
            mostSimple.AddConditionToRHS(new AssertCondition("Wow", customer_var, "eats", description));
            agenda.AddProduction(mostSimple);

            agenda.AddFacts(factlist);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 1, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
        }
예제 #10
0
        public void FirstAggTest()
        {
            CustomerFactory factory = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer customer = factory.FindFirst("Name = {0}", "Joe Blow");

            IList<WME> factlist = customer.GenerateFactsForRootObject();
            Assert.IsTrue(factlist.Count == 11, "Wrong count.");

            Agenda agenda = new Agenda();
            agenda.LoadRulesFromAssemblies = false;

            Variable customer_var = new Variable("Customer");
            Variable orders = new Variable("Orders");
            Variable order = new Variable("Order");
            Variable val = new Variable("?Value");

            Aggregator ag = new Aggregator("Count of customer orders.");
            ag.GroupBy = customer_var;
            ag.AggregatorFunction = new Count("$Customer.Orders.Order", "$Customer.Orders.Count");
            ag.AddConditionToLHS(new AND(customer_var, "$Customer.Orders", orders));
            ag.AddConditionToLHS(new AND(orders, "$Customer.Orders.Order", order));
            agenda.AddAggregator(ag);

            Production mostSimple = new Production("If Big Spender");
            mostSimple.AddConditionToLHS(new AND(customer_var, "$Customer.Orders.Count", val));
            mostSimple.AddConditionToRHS(new INVOKE("shout", customer_var, "Shout", val));
            agenda.AddProduction(mostSimple);

            agenda.AddFacts(factlist);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 1, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
        }
예제 #11
0
        public void FirstTest()
        {
            CustomerFactory factory  = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer        customer = factory.FindFirst("Status.Name = {0}", "Active");

            Assert.IsNotNull(customer, "Should not be null.");

            IList <WME> factlist = customer.GenerateFactsForRootObject();

            Assert.IsTrue(factlist.Count == 11, "Wrong count.");

            Agenda agenda = new Agenda();

            agenda.LoadRulesFromAssemblies = false;

            Variable customer_var  = new Variable("Customer");
            Variable customer_name = new Variable("Customer.Name");

            Production mostSimple = new Production("Most Simple");

            mostSimple.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            mostSimple.AddConditionToRHS(new InvokeCondition("shout", customer_var, "Shout", customer_name));
            agenda.AddProduction(mostSimple);

            agenda.AddFacts(factlist);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 1, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
        }
예제 #12
0
        public void RetractRule()
        {
            Agenda agenda = new Agenda();

            agenda.ConflictResolutionStrategy = new SalienceResolver();

            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");

            Production prod = new Production("find-stack-of-two-blocks-to-the-left-of-a-red-block");

            prod.Salience = 1;

            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"));
            prod.AddConditionToRHS(new AssertCondition("R1", x, "is", "on top"));
            prod.AddConditionToRHS(new AssertCondition("R2", "mary", "looks", "fine"));
            agenda.AddProduction(prod);


            Production dependentProd = new Production("Dependent Production");

            dependentProd.Salience = 55;
            dependentProd.AddConditionToLHS(new AND("mary", "looks", "fine"));
            dependentProd.AddConditionToRHS(new ASSERT("should", "not", "see"));
            agenda.AddProduction(dependentProd);

            Production retractProd = new Production("Retract Production");

            retractProd.Salience = 50;
            retractProd.AddConditionToLHS(new AND(x, "is", "on top"));
            retractProd.AddConditionToRHS(new RETRACT("mary", "looks", "fine"));
            agenda.AddProduction(retractProd);

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

            Assert.IsTrue(agenda.ActivatedRuleCount == 2, "Rule did not fire.");
            Assert.IsTrue(agenda.NotActivatedRuleCount == 1, "Rule should not have fired.");
            Assert.IsTrue(agenda.InferredFacts.Count == 1, "Bad");
        }
예제 #13
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");
        }
예제 #14
0
        public void TimeStampConflictResolver()
        {
            CustomerFactory factory = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer customer = factory.FindFirst("Name = {0}", "Joe Blow");
            Assert.IsNotNull(customer, "SHould have found customer.");
            Agenda agenda = new Agenda();
            agenda.ConflictResolutionStrategy = new TimeStampConflictResolver();
            agenda.LoadRulesFromAssemblies = false;
            agenda.AddObject(customer);

            Production WhatsMyName = new Production("Sets 'Default Text'.");
            WhatsMyName.Salience = 1;
            Variable customer_var = new Variable("$Customer");
            Variable customer_name = new Variable("$Customer.Name");
            WhatsMyName.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("Joe Blow", new FuncTerm("funcEquals", new funcEquals()), customer_name));
            WhatsMyName.AddConditionToRHS(new InvokeCondition(customer_var, "Shout", customer_name));
            WhatsMyName.AddConditionToRHS(new SetCondition(customer_var, "Remarks", "Default Text."));
            agenda.AddProduction(WhatsMyName);

            Production WhatsMyName1 = new Production("Sets 'Squid Text'.");
            WhatsMyName1.Salience = 1;
            WhatsMyName1.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            WhatsMyName1.AddConditionToRHS(new InvokeCondition(customer_var, "Shout", "Squid"));
            WhatsMyName1.AddConditionToRHS(new SetCondition(customer_var, "Remarks", "Squid Text"));
            WhatsMyName1.AddConditionToRHS(new AssertCondition(customer_var, "Poop", "Deck"));
            agenda.AddProduction(WhatsMyName1);

            agenda.Run();

            Assert.IsTrue(agenda.TotalFacts == 11);
            Assert.IsTrue(agenda.InferredFacts.Count == 1);
            Assert.IsTrue(agenda.ActivatedRuleCount == 2);
            Assert.IsTrue(agenda.ActionsTaken.Count == 4);
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0);
            Assert.IsTrue(customer.Remarks.Equals("Squid Text"), "Should be 'Default Text.'.");
        }
예제 #15
0
        public void FirstTest()
        {
            CustomerFactory factory = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer customer = factory.FindFirst("Status.Name = {0}", "Active");

            Assert.IsNotNull(customer, "Should not be null.");

            IList<WME> factlist = customer.GenerateFactsForRootObject();
            Assert.IsTrue(factlist.Count == 11, "Wrong count.");

            Agenda agenda = new Agenda();
            agenda.LoadRulesFromAssemblies = false;

            Variable customer_var = new Variable("Customer");
            Variable customer_name = new Variable("Customer.Name");

            Production mostSimple = new Production("Most Simple");
            mostSimple.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            mostSimple.AddConditionToRHS(new InvokeCondition("shout", customer_var, "Shout", customer_name));
            agenda.AddProduction(mostSimple);

            agenda.AddFacts(factlist);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 1, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
        }
예제 #16
0
        public void BasicOverrideTest()
        {
            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");

            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");

            rhs.ConditionType = ConditionType.Assert;
            prod1.AddConditionToRHS(rhs1);
            agenda.AddProduction(prod1);

            Override me = new Override();

            me.Winner = "Platinum Customer";
            me.Loser  = "Gold Customer";

            agenda.AddOverride(me);

            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 == 1, "Rule did not fire.");
            Assert.IsTrue(agenda.InferredFacts.Count == 5, "Bad");
        }
예제 #17
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");
        }
예제 #18
0
        public void ListTest2()
        {
            Production prod = new Production();

            prod.Label = "ListTest";
            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");
            Variable l = new Variable("l");
            Variable v = new Variable("v");

            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"));
            prod.AddConditionToLHS(new PositiveCondition("C4", x, "has virtue", l));
            prod.AddConditionToLHS(new PositiveCondition("C5", "virtues", "virtueList", v));
            prod.AddConditionToLHS(new FunctionCondition("C6", l, new FuncTerm("isInList", new isInList()), v));

            AssertCondition rhs = new AssertCondition("R1", x, "is", "good");

            rhs.ConditionType = ConditionType.Assert;

            prod.AddConditionToRHS(rhs);

            Agenda agenda = new Agenda();

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


            WME v1 = new WME("V1", "B1", "has virtue", "Calm");

            agenda.AddFact(v1);

            WME v2 = new WME("V2", "virtues", "virtueList", new string[] { "Calm", "Cool", "Collected" });

            agenda.AddFact(v2);

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

            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Rule did not fire.");
            Assert.IsTrue(agenda.InferredFacts.Count == 1, "Bad");
        }
예제 #19
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"));
            prod.AddConditionToRHS(new AssertCondition("C4", x, "is", "on top"));

            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);

            NetworkPrinter printer = new NetworkPrinter();

            rete.DummyTopNode.Accept(printer);

            using (StreamWriter writer = new StreamWriter(@"C:\Temp\TestAddProduction - 1.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");
        }
예제 #20
0
        public void GrandParents()
        {
            Rete rete = new Rete();

            Production prod = new Production();

            prod.Label = "Whose your daddy";

            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");

            prod.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod.AddConditionToRHS(new AssertCondition(y, "child", x));

            rete.AddProduction(prod);

            Production prod1 = new Production();

            prod1.Label = "Whose your grand daddy";

            prod1.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod1.AddConditionToLHS(new PositiveCondition(y, "parent", z));
            prod1.AddConditionToRHS(new AssertCondition(x, "gparent", z));

            rete.AddProduction(prod1);

            Production prod2 = new Production();

            prod2.Label = "Whose your grand child";

            prod2.AddConditionToLHS(new PositiveCondition(x, "gparent", y));
            prod2.AddConditionToRHS(new AssertCondition(y, "gchild", x));

            rete.AddProduction(prod2);

            Production prod3 = new Production();

            prod3.Label = "Whose your great grand daddy";

            prod3.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod3.AddConditionToLHS(new PositiveCondition(y, "gparent", z));
            prod3.AddConditionToRHS(new AssertCondition(x, "ggparent", z));

            rete.AddProduction(prod3);

            List <WME> wmes = new List <WME>();

            wmes.Add(new WME("george", "gchild", "kendall"));
            wmes.Add(new WME("kendall", "parent", "immanuel"));
            wmes.Add(new WME("louis", "gparent", "richard"));
            wmes.Add(new WME("jacques", "parent", "jean-francois"));
            wmes.Add(new WME("friedrich", "parent", "louis"));
            wmes.Add(new WME("louis", "gparent", "willard"));
            wmes.Add(new WME("willard", "gchild", "ludwig"));
            wmes.Add(new WME("miles", "parent", "ludwig"));
            wmes.Add(new WME("ludwig", "gchild", "noam"));
            wmes.Add(new WME("miles", "gparent", "wilhelm"));
            wmes.Add(new WME("friedrich", "parent", "jean-francois"));
            wmes.Add(new WME("bijan", "gchild", "george"));
            wmes.Add(new WME("wilhelm", "gparent", "bertrand"));
            wmes.Add(new WME("alan", "parent", "bijan"));
            wmes.Add(new WME("kendall", "parent", "gottlob"));
            wmes.Add(new WME("david", "gchild", "bertrand"));
            wmes.Add(new WME("alan", "gchild", "henry"));
            wmes.Add(new WME("kendall", "gparent", "louis"));
            wmes.Add(new WME("willard", "gchild", "willard"));
            wmes.Add(new WME("richard", "gchild", "pat"));
            wmes.Add(new WME("miles", "parent", "friedrich"));
            wmes.Add(new WME("noam", "gparent", "ludwig"));
            wmes.Add(new WME("jean-francois", "gchild", "bertrand"));
            wmes.Add(new WME("louis", "parent", "bijan"));
            wmes.Add(new WME("willard", "parent", "kendall"));
            wmes.Add(new WME("gottlob", "gparent", "kendall"));
            wmes.Add(new WME("immanuel", "gchild", "wilhelm"));
            wmes.Add(new WME("george", "parent", "drew"));
            wmes.Add(new WME("friedrich", "gchild", "david"));
            wmes.Add(new WME("gottlob", "gchild", "bertrand"));
            wmes.Add(new WME("wilhelm", "parent", "ludwig"));
            wmes.Add(new WME("henry", "gchild", "willard"));
            wmes.Add(new WME("alan", "gchild", "richard"));
            wmes.Add(new WME("george", "gchild", "miles"));
            wmes.Add(new WME("george", "gparent", "willard"));
            wmes.Add(new WME("alasdair", "gparent", "willard"));
            wmes.Add(new WME("willard", "gparent", "immanuel"));
            wmes.Add(new WME("jacques", "gparent", "george"));
            wmes.Add(new WME("henry", "gchild", "rudolf"));
            wmes.Add(new WME("wilhelm", "gparent", "miles"));
            wmes.Add(new WME("noam", "gparent", "jean-francois"));
            wmes.Add(new WME("pat", "gchild", "friedrich"));
            wmes.Add(new WME("rudolf", "gchild", "david"));
            wmes.Add(new WME("john", "gchild", "stanley"));
            wmes.Add(new WME("jacques", "gchild", "bijan"));
            wmes.Add(new WME("george", "parent", "miles"));
            wmes.Add(new WME("louis", "parent", "drew"));
            wmes.Add(new WME("rudolf", "parent", "jacques"));
            wmes.Add(new WME("bertrand", "gparent", "ludwig"));
            wmes.Add(new WME("gottlob", "parent", "john"));
            wmes.Add(new WME("miles", "gchild", "miles"));
            wmes.Add(new WME("bijan", "gchild", "noam"));
            wmes.Add(new WME("jean-francois", "gparent", "george"));
            wmes.Add(new WME("miles", "gparent", "henry"));
            wmes.Add(new WME("kendall", "gchild", "ludwig"));
            wmes.Add(new WME("louis", "gparent", "david"));
            wmes.Add(new WME("noam", "parent", "george"));
            wmes.Add(new WME("pat", "parent", "richard"));
            wmes.Add(new WME("george", "parent", "ludwig"));
            wmes.Add(new WME("george", "gparent", "alan"));
            wmes.Add(new WME("pat", "gparent", "louis"));
            wmes.Add(new WME("david", "parent", "friedrich"));
            wmes.Add(new WME("wilhelm", "gchild", "alasdair"));
            wmes.Add(new WME("pat", "gchild", "bertrand"));
            wmes.Add(new WME("noam", "parent", "stanley"));
            wmes.Add(new WME("friedrich", "gparent", "bijan"));
            wmes.Add(new WME("willard", "gchild", "bertrand"));
            wmes.Add(new WME("kendall", "gparent", "noam"));
            wmes.Add(new WME("kendall", "gparent", "george"));
            wmes.Add(new WME("drew", "parent", "gottlob"));
            wmes.Add(new WME("david", "parent", "kendall"));
            wmes.Add(new WME("alan", "gparent", "gottlob"));
            wmes.Add(new WME("richard", "parent", "jacques"));
            wmes.Add(new WME("alasdair", "gchild", "david"));
            wmes.Add(new WME("richard", "parent", "bijan"));
            wmes.Add(new WME("jean-francois", "gparent", "alan"));
            wmes.Add(new WME("jacques", "gchild", "louis"));
            wmes.Add(new WME("miles", "gchild", "jean-francois"));
            wmes.Add(new WME("pat", "parent", "miles"));
            wmes.Add(new WME("kendall", "parent", "gottlob"));
            wmes.Add(new WME("gottlob", "parent", "willard"));
            wmes.Add(new WME("jean-francois", "parent", "willard"));
            wmes.Add(new WME("stanley", "gchild", "ludwig"));
            wmes.Add(new WME("john", "gchild", "miles"));
            wmes.Add(new WME("john", "parent", "jean-francois"));
            wmes.Add(new WME("alan", "gchild", "louis"));
            wmes.Add(new WME("stanley", "gchild", "wilhelm"));
            wmes.Add(new WME("drew", "gparent", "miles"));
            wmes.Add(new WME("jacques", "parent", "immanuel"));
            wmes.Add(new WME("david", "gparent", "willard"));
            wmes.Add(new WME("miles", "parent", "alasdair"));
            wmes.Add(new WME("immanuel", "gparent", "ludwig"));
            wmes.Add(new WME("kendall", "gparent", "henry"));
            wmes.Add(new WME("immanuel", "gparent", "ludwig"));
            wmes.Add(new WME("miles", "gchild", "john"));
            wmes.Add(new WME("louis", "gchild", "alan"));
            wmes.Add(new WME("willard", "parent", "alan"));
            wmes.Add(new WME("kendall", "parent", "ludwig"));
            wmes.Add(new WME("george", "gchild", "alasdair"));
            wmes.Add(new WME("richard", "gparent", "bertrand"));

            int i = 1;

            foreach (WME wme in wmes)
            {
                wme.Label = "W" + i++;
                rete.AddWME(wme);
            }

            NetworkPrinter printer = new NetworkPrinter();

            rete.DummyTopNode.Accept(printer);

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

            Assert.IsTrue(rete.WorkingMemory.Count == 100, "Bad");
            Assert.IsTrue(prod.InferredFacts.Count == 33, "Wrong number of conclusions");
            Assert.IsTrue(prod1.InferredFacts.Count == 42, "Wrong number of conclusions");
            Assert.IsTrue(prod2.InferredFacts.Count == 29, "Wrong number of conclusions");
            Assert.IsTrue(prod3.InferredFacts.Count == 38, "Wrong number of conclusions");
        }
예제 #21
0
        public void MultiBuiltinTest()
        {
            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 condition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", x, "color", c));

            FunctionCondition funCond = new FunctionCondition("F4", c, new FuncTerm("funcNotEquals", new funcNotEquals()), new StringTerm("blue"));

            funCond.ConditionType = ConditionType.Function;
            prod.AddConditionToLHS(funCond);

            prod.AddConditionToRHS(new AssertCondition("C4", x, "is", c));

            Rete rete = new Rete();

            rete.AddProduction(prod);

            NetworkPrinter printer = new NetworkPrinter();

            rete.DummyTopNode.Accept(printer);

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

            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);

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

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

            //Assert.IsTrue(rete.RulesThatFired.Count == 1, "Rule did not fire.");
            Assert.IsTrue(prod.InferredFacts.Count == 2, "Wrong number of InferredFacts");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
예제 #22
0
        public void SecondAggTest()
        {
            CustomerFactory factory = new CustomerFactory(new ObjectContext(CreateDataStore()));
            ObjectList<Customer> customers = factory.FindAllObjects();

            Agenda agenda = new Agenda();
            agenda.LoadRulesFromAssemblies = false;
            agenda.AddObjects(customers);

            Variable customer_var = new Variable("Customer");
            Variable orders = new Variable("Orders");
            Variable order = new Variable("Order");
            Variable count = new Variable("OrderCount");

            Aggregator ag = new Aggregator("Count of customer orders.");
            ag.GroupBy = customer_var;
            ag.AggregatorFunction = new Count("$Customer.Orders.Order", "Customer.Orders.Count");
            ag.AddConditionToLHS(new AND(customer_var, "$Customer.Orders", orders));
            ag.AddConditionToLHS(new AND(orders, "$Customer.Orders.Order", order));
            agenda.AddAggregator(ag);

            Production mostSimple = new Production("If Big Spender");
            mostSimple.AddConditionToLHS(new AND(customer_var, "Customer.Orders.Count", count));
            mostSimple.AddConditionToRHS(new INVOKE("shout", customer_var, "Shout", count));
            agenda.AddProduction(mostSimple);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 22, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 2, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 1, "Bad");
            foreach (Customer customer in customers)
            {
                Assert.IsTrue(customer.Result != null && customer.Result.StartsWith("Shout"), "Did not invoke method.");
            }
        }
예제 #23
0
        public void Test4pointsTo1()
        {
            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"));
            prod.AddConditionToLHS(new PositiveCondition("C4", x, "has", "car"));
            prod.AddConditionToLHS(new PositiveCondition("C5", x, "wants", z));
            prod.AddConditionToRHS(new AssertCondition("C5", x, "gets", z));
            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] = "B2";
            wme10.Fields[1] = "has";
            wme10.Fields[2] = "bike";
            rete.AddWME(wme10);

            WME wme11 = new WME("W11");
            wme11.Fields[0] = "B1";
            wme11.Fields[1] = "has";
            wme11.Fields[2] = "car";
            rete.AddWME(wme11);

            WME wme12 = new WME("W12");
            wme12.Fields[0] = "B1";
            wme12.Fields[1] = "wants";
            wme12.Fields[2] = "B3";
            rete.AddWME(wme12);

            //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 == 12, "Bad");
        }
예제 #24
0
        public void ReteWithHash()
        {
            Production prod = new Production();
            prod.Label = "ReteWithHash";
            Variable a = new Variable("a");
            Variable b = new Variable("b");
            Variable c = new Variable("c");
            Variable d = new Variable("d");
            Variable s = new Variable("s");

            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"));
            prod.AddConditionToLHS(new PositiveCondition("C4", a, "color", "maize"));
            prod.AddConditionToLHS(new PositiveCondition("C5", b, "color", "blue"));
            prod.AddConditionToLHS(new PositiveCondition("C6", c, "color", "green"));
            prod.AddConditionToLHS(new PositiveCondition("C7", d, "color", "white"));
            prod.AddConditionToLHS(new PositiveCondition("C8", s, "on", "table"));
            prod.AddConditionToLHS(new PositiveCondition("C9", z, a, b));
            prod.AddConditionToLHS(new PositiveCondition("C10", a, "left of", d));

            prod.AddConditionToRHS(new AssertCondition("C4", x, "is", "on top"));

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

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

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

            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);

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

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

            Assert.IsTrue(prod.InferredFacts.Count == 0, "Wrong number of conclusions");
        }
예제 #25
0
        public void GrandParents()
        {
            Rete rete = new Rete();

            Production prod = new Production();
            prod.Label = "Whose your daddy";

            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");
            prod.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod.AddConditionToRHS(new AssertCondition(y, "child", x));

            rete.AddProduction(prod);

            Production prod1 = new Production();
            prod1.Label = "Whose your grand daddy";

            prod1.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod1.AddConditionToLHS(new PositiveCondition(y, "parent", z));
            prod1.AddConditionToRHS(new AssertCondition(x, "gparent", z));

            rete.AddProduction(prod1);

            Production prod2 = new Production();
            prod2.Label = "Whose your grand child";

            prod2.AddConditionToLHS(new PositiveCondition(x, "gparent", y));
            prod2.AddConditionToRHS(new AssertCondition(y, "gchild", x));

            rete.AddProduction(prod2);

            Production prod3 = new Production();
            prod3.Label = "Whose your great grand daddy";

            prod3.AddConditionToLHS(new PositiveCondition(x, "parent", y));
            prod3.AddConditionToLHS(new PositiveCondition(y, "gparent", z));
            prod3.AddConditionToRHS(new AssertCondition(x, "ggparent", z));

            rete.AddProduction(prod3);

            List<WME> wmes = new List<WME>();

            wmes.Add(new WME("george", "gchild", "kendall"));
            wmes.Add(new WME("kendall", "parent", "immanuel"));
            wmes.Add(new WME("louis", "gparent", "richard"));
            wmes.Add(new WME("jacques", "parent", "jean-francois"));
            wmes.Add(new WME("friedrich", "parent", "louis"));
            wmes.Add(new WME("louis", "gparent", "willard"));
            wmes.Add(new WME("willard", "gchild", "ludwig"));
            wmes.Add(new WME("miles", "parent", "ludwig"));
            wmes.Add(new WME("ludwig", "gchild", "noam"));
            wmes.Add(new WME("miles", "gparent", "wilhelm"));
            wmes.Add(new WME("friedrich", "parent", "jean-francois"));
            wmes.Add(new WME("bijan", "gchild", "george"));
            wmes.Add(new WME("wilhelm", "gparent", "bertrand"));
            wmes.Add(new WME("alan", "parent", "bijan"));
            wmes.Add(new WME("kendall", "parent", "gottlob"));
            wmes.Add(new WME("david", "gchild", "bertrand"));
            wmes.Add(new WME("alan", "gchild", "henry"));
            wmes.Add(new WME("kendall", "gparent", "louis"));
            wmes.Add(new WME("willard", "gchild", "willard"));
            wmes.Add(new WME("richard", "gchild", "pat"));
            wmes.Add(new WME("miles", "parent", "friedrich"));
            wmes.Add(new WME("noam", "gparent", "ludwig"));
            wmes.Add(new WME("jean-francois", "gchild", "bertrand"));
            wmes.Add(new WME("louis", "parent", "bijan"));
            wmes.Add(new WME("willard", "parent", "kendall"));
            wmes.Add(new WME("gottlob", "gparent", "kendall"));
            wmes.Add(new WME("immanuel", "gchild", "wilhelm"));
            wmes.Add(new WME("george", "parent", "drew"));
            wmes.Add(new WME("friedrich", "gchild", "david"));
            wmes.Add(new WME("gottlob", "gchild", "bertrand"));
            wmes.Add(new WME("wilhelm", "parent", "ludwig"));
            wmes.Add(new WME("henry", "gchild", "willard"));
            wmes.Add(new WME("alan", "gchild", "richard"));
            wmes.Add(new WME("george", "gchild", "miles"));
            wmes.Add(new WME("george", "gparent", "willard"));
            wmes.Add(new WME("alasdair", "gparent", "willard"));
            wmes.Add(new WME("willard", "gparent", "immanuel"));
            wmes.Add(new WME("jacques", "gparent", "george"));
            wmes.Add(new WME("henry", "gchild", "rudolf"));
            wmes.Add(new WME("wilhelm", "gparent", "miles"));
            wmes.Add(new WME("noam", "gparent", "jean-francois"));
            wmes.Add(new WME("pat", "gchild", "friedrich"));
            wmes.Add(new WME("rudolf", "gchild", "david"));
            wmes.Add(new WME("john", "gchild", "stanley"));
            wmes.Add(new WME("jacques", "gchild", "bijan"));
            wmes.Add(new WME("george", "parent", "miles"));
            wmes.Add(new WME("louis", "parent", "drew"));
            wmes.Add(new WME("rudolf", "parent", "jacques"));
            wmes.Add(new WME("bertrand", "gparent", "ludwig"));
            wmes.Add(new WME("gottlob", "parent", "john"));
            wmes.Add(new WME("miles", "gchild", "miles"));
            wmes.Add(new WME("bijan", "gchild", "noam"));
            wmes.Add(new WME("jean-francois", "gparent", "george"));
            wmes.Add(new WME("miles", "gparent", "henry"));
            wmes.Add(new WME("kendall", "gchild", "ludwig"));
            wmes.Add(new WME("louis", "gparent", "david"));
            wmes.Add(new WME("noam", "parent", "george"));
            wmes.Add(new WME("pat", "parent", "richard"));
            wmes.Add(new WME("george", "parent", "ludwig"));
            wmes.Add(new WME("george", "gparent", "alan"));
            wmes.Add(new WME("pat", "gparent", "louis"));
            wmes.Add(new WME("david", "parent", "friedrich"));
            wmes.Add(new WME("wilhelm", "gchild", "alasdair"));
            wmes.Add(new WME("pat", "gchild", "bertrand"));
            wmes.Add(new WME("noam", "parent", "stanley"));
            wmes.Add(new WME("friedrich", "gparent", "bijan"));
            wmes.Add(new WME("willard", "gchild", "bertrand"));
            wmes.Add(new WME("kendall", "gparent", "noam"));
            wmes.Add(new WME("kendall", "gparent", "george"));
            wmes.Add(new WME("drew", "parent", "gottlob"));
            wmes.Add(new WME("david", "parent", "kendall"));
            wmes.Add(new WME("alan", "gparent", "gottlob"));
            wmes.Add(new WME("richard", "parent", "jacques"));
            wmes.Add(new WME("alasdair", "gchild", "david"));
            wmes.Add(new WME("richard", "parent", "bijan"));
            wmes.Add(new WME("jean-francois", "gparent", "alan"));
            wmes.Add(new WME("jacques", "gchild", "louis"));
            wmes.Add(new WME("miles", "gchild", "jean-francois"));
            wmes.Add(new WME("pat", "parent", "miles"));
            wmes.Add(new WME("kendall", "parent", "gottlob"));
            wmes.Add(new WME("gottlob", "parent", "willard"));
            wmes.Add(new WME("jean-francois", "parent", "willard"));
            wmes.Add(new WME("stanley", "gchild", "ludwig"));
            wmes.Add(new WME("john", "gchild", "miles"));
            wmes.Add(new WME("john", "parent", "jean-francois"));
            wmes.Add(new WME("alan", "gchild", "louis"));
            wmes.Add(new WME("stanley", "gchild", "wilhelm"));
            wmes.Add(new WME("drew", "gparent", "miles"));
            wmes.Add(new WME("jacques", "parent", "immanuel"));
            wmes.Add(new WME("david", "gparent", "willard"));
            wmes.Add(new WME("miles", "parent", "alasdair"));
            wmes.Add(new WME("immanuel", "gparent", "ludwig"));
            wmes.Add(new WME("kendall", "gparent", "henry"));
            wmes.Add(new WME("immanuel", "gparent", "ludwig"));
            wmes.Add(new WME("miles", "gchild", "john"));
            wmes.Add(new WME("louis", "gchild", "alan"));
            wmes.Add(new WME("willard", "parent", "alan"));
            wmes.Add(new WME("kendall", "parent", "ludwig"));
            wmes.Add(new WME("george", "gchild", "alasdair"));
            wmes.Add(new WME("richard", "gparent", "bertrand"));

            int i = 1;
            foreach (WME wme in wmes)
            {
                wme.Label = "W" + i++;
                rete.AddWME(wme);
            }

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

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

            Assert.IsTrue(rete.WorkingMemory.Count == 100, "Bad");
            Assert.IsTrue(prod.InferredFacts.Count == 33, "Wrong number of conclusions");
            Assert.IsTrue(prod1.InferredFacts.Count == 42, "Wrong number of conclusions");
            Assert.IsTrue(prod2.InferredFacts.Count == 29, "Wrong number of conclusions");
            Assert.IsTrue(prod3.InferredFacts.Count == 38, "Wrong number of conclusions");
        }
예제 #26
0
        public void EvaluatorTest()
        {
            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", "red"));

            PositiveCondition noteq = new PositiveCondition("NOT BLUE", x, "color", "blue");
            noteq.Evaluator = EvaluatorManager.Instance.Evaluators["NotEquals"];
            prod.AddConditionToLHS(noteq);

            prod.AddConditionToRHS(new AssertCondition("C5", x, "calls", z));

            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);

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

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

            Assert.IsTrue(prod.InferredFacts.Count == 2, "Wrong number of conclusions");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
예제 #27
0
        public void DigDeep()
        {
            CustomerFactory factory = new CustomerFactory(new ObjectContext(CreateDataStore()));
            Customer customer = factory.FindFirst("Name = {0}", "Joe Blow");

            Agenda agenda = new Agenda();
            agenda.ConflictResolutionStrategy = new TimeStampConflictResolver();
            agenda.LoadRulesFromAssemblies = false;
            agenda.AddObject(customer);

            Production WhatsMyName = new Production("WhatsMyName");
            WhatsMyName.Salience = 1;
            Variable customer_var = new Variable("Customer");
            Variable customer_name = new Variable("Customer.Name");
            Variable site_status = new Variable("Customer.Status");
            Variable site_status_name = new Variable("Customer.Status.Name");

            WhatsMyName.AddConditionToLHS(new PositiveCondition("C1", customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F1", customer_name, new FuncTerm("funcEquals", new funcEquals()), "Joe Blow"));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C2", customer_var, "$Customer.Status", site_status));
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C3", site_status, "$Customer.Status.Name", site_status_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("F2", site_status_name, new FuncTerm("funcEquals", new funcEquals()), "Active"));
            WhatsMyName.AddConditionToRHS(new SetCondition(customer_var, "Remarks", customer_name));
            agenda.AddProduction(WhatsMyName);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 11);
            Assert.IsTrue(agenda.InferredFacts.Count == 0);
            Assert.IsTrue(agenda.ActivatedRuleCount == 1);
            Assert.IsTrue(agenda.ActionsTaken.Count == 1);
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0);
            Assert.IsTrue(customer.Remarks.Equals("Joe Blow"), "Should be 'Default Text.'.");
        }
예제 #28
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");
        }
예제 #29
0
        public void RemoveProduction()
        {
            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.RemoveProduction(prod);

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

            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
예제 #30
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");
        }
예제 #31
0
        public void Test4pointsTo1()
        {
            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"));
            prod.AddConditionToLHS(new PositiveCondition("C4", x, "has", "car"));
            prod.AddConditionToLHS(new PositiveCondition("C5", x, "wants", z));
            prod.AddConditionToRHS(new AssertCondition("C5", x, "gets", z));
            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] = "B2";
            wme10.Fields[1] = "has";
            wme10.Fields[2] = "bike";
            rete.AddWME(wme10);

            WME wme11 = new WME("W11");

            wme11.Fields[0] = "B1";
            wme11.Fields[1] = "has";
            wme11.Fields[2] = "car";
            rete.AddWME(wme11);

            WME wme12 = new WME("W12");

            wme12.Fields[0] = "B1";
            wme12.Fields[1] = "wants";
            wme12.Fields[2] = "B3";
            rete.AddWME(wme12);

            //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 == 12, "Bad");
        }
예제 #32
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");
        }
예제 #33
0
        public void MultiBuiltinTest()
        {
            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 condition("C2", y, "left of", z));
            prod.AddConditionToLHS(new PositiveCondition("C3", x, "color", c));

            FunctionCondition funCond = new FunctionCondition("F4", c, new FuncTerm("funcNotEquals", new funcNotEquals()), new StringTerm("blue"));
            funCond.ConditionType = ConditionType.Function;
            prod.AddConditionToLHS(funCond);

            prod.AddConditionToRHS(new AssertCondition("C4", x, "is", c));

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

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

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

            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);

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

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

            //Assert.IsTrue(rete.RulesThatFired.Count == 1, "Rule did not fire.");
            Assert.IsTrue(prod.InferredFacts.Count == 2, "Wrong number of InferredFacts");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }
예제 #34
0
        public void Test2()
        {
            CustomerFactory factory = new CustomerFactory(new ObjectContext(CreateDataStore()));
            ObjectList<Customer> customers = factory.Find("Status.Name = {0}", "Active");

            Assert.IsNotNull(customers.Count >= 2, "Should not be at least 2.");

            Agenda agenda = new Agenda();
            agenda.ConflictResolutionStrategy = new SalienceResolver();
            agenda.LoadRulesFromAssemblies = false;
            agenda.AddObjects(customers);

            Production WhatsMyName = new Production("Whats My Name");
            WhatsMyName.Salience = 1;
            Variable customer_var = new Variable("Customer");
            Variable customer_name = new Variable("Name");
            WhatsMyName.AddConditionToLHS(new PositiveCondition("C2", customer_var, "$Customer.Name", customer_name));
            WhatsMyName.AddConditionToLHS(new FunctionCondition("C3", customer_name, new FuncTerm("funcEquals", new funcEquals()), "Joe Blow"));
            WhatsMyName.AddConditionToRHS(new InvokeCondition("R1", customer_var, "Shout", customer_name));
            WhatsMyName.AddConditionToRHS(new SetCondition("R2", customer_var, "Remarks", "Hello from Expergent."));
            agenda.AddProduction(WhatsMyName);

            Production WhatsMyName1 = new Production();
            WhatsMyName1.Salience = 10;
            WhatsMyName1.AddConditionToLHS(new PositiveCondition(customer_var, "$Customer.Name", customer_name));
            WhatsMyName1.AddConditionToRHS(new InvokeCondition(customer_var, "Shout", "Squid"));
            WhatsMyName1.AddConditionToRHS(new SetCondition(customer_var, "Remarks", "Squid Text"));
            agenda.AddProduction(WhatsMyName1);

            agenda.Run();

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

            Assert.IsTrue(agenda.TotalFacts == 22, "Bad");
            Assert.IsTrue(agenda.ActionsTaken.Count == 6, "Bad");
            Assert.IsTrue(agenda.ActionsSkipped.Count == 0, "Bad");
            Assert.IsTrue(agenda.ActivatedRuleCount == 2, "Bad");
            Assert.IsTrue(customers.FindFirst("Name = {0}", "Joe Blow").Result != null && customers.FindFirst("Name = {0}", "Joe Blow").Result.Contains("Squid"), "Did not invoke method.");
            Assert.IsTrue(customers.FindFirst("Name = {0}", "Joe Blow").Remarks != null && customers.FindFirst("Name = {0}", "Joe Blow").Remarks.Contains("Squid Text"), "Did not invoke method.");
        }
예제 #35
0
        public void RetractRule()
        {
            Agenda agenda = new Agenda();
            agenda.ConflictResolutionStrategy = new SalienceResolver();

            Variable x = new Variable("x");
            Variable y = new Variable("y");
            Variable z = new Variable("z");

            Production prod = new Production("find-stack-of-two-blocks-to-the-left-of-a-red-block");
            prod.Salience = 1;

            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"));
            prod.AddConditionToRHS(new AssertCondition("R1", x, "is", "on top"));
            prod.AddConditionToRHS(new AssertCondition("R2", "mary", "looks", "fine"));
            agenda.AddProduction(prod);

            Production dependentProd = new Production("Dependent Production");
            dependentProd.Salience = 55;
            dependentProd.AddConditionToLHS(new AND("mary", "looks", "fine"));
            dependentProd.AddConditionToRHS(new ASSERT("should", "not", "see"));
            agenda.AddProduction(dependentProd);

            Production retractProd = new Production("Retract Production");
            retractProd.Salience = 50;
            retractProd.AddConditionToLHS(new AND(x, "is", "on top"));
            retractProd.AddConditionToRHS(new RETRACT("mary", "looks", "fine"));
            agenda.AddProduction(retractProd);

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

            Assert.IsTrue(agenda.ActivatedRuleCount == 2, "Rule did not fire.");
            Assert.IsTrue(agenda.NotActivatedRuleCount == 1, "Rule should not have fired.");
            Assert.IsTrue(agenda.InferredFacts.Count == 1, "Bad");
        }
예제 #36
0
        public void ReteWithHash()
        {
            Production prod = new Production();

            prod.Label = "ReteWithHash";
            Variable a = new Variable("a");
            Variable b = new Variable("b");
            Variable c = new Variable("c");
            Variable d = new Variable("d");
            Variable s = new Variable("s");

            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"));
            prod.AddConditionToLHS(new PositiveCondition("C4", a, "color", "maize"));
            prod.AddConditionToLHS(new PositiveCondition("C5", b, "color", "blue"));
            prod.AddConditionToLHS(new PositiveCondition("C6", c, "color", "green"));
            prod.AddConditionToLHS(new PositiveCondition("C7", d, "color", "white"));
            prod.AddConditionToLHS(new PositiveCondition("C8", s, "on", "table"));
            prod.AddConditionToLHS(new PositiveCondition("C9", z, a, b));
            prod.AddConditionToLHS(new PositiveCondition("C10", a, "left of", d));

            prod.AddConditionToRHS(new AssertCondition("C4", x, "is", "on top"));

            Rete rete = new Rete();

            rete.AddProduction(prod);

            NetworkPrinter printer = new NetworkPrinter();

            rete.DummyTopNode.Accept(printer);

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

            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);

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

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

            Assert.IsTrue(prod.InferredFacts.Count == 0, "Wrong number of conclusions");
        }
예제 #37
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");
        }
예제 #38
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");
        }
예제 #39
0
        public void EvaluatorTest()
        {
            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", "red"));

            PositiveCondition noteq = new PositiveCondition("NOT BLUE", x, "color", "blue");

            noteq.Evaluator = EvaluatorManager.Instance.Evaluators["NotEquals"];
            prod.AddConditionToLHS(noteq);

            prod.AddConditionToRHS(new AssertCondition("C5", x, "calls", z));

            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);

            NetworkPrinter printer = new NetworkPrinter();

            rete.DummyTopNode.Accept(printer);

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

            Assert.IsTrue(prod.InferredFacts.Count == 2, "Wrong number of conclusions");
            Assert.IsTrue(rete.WorkingMemory.Count == 9, "Bad");
        }