Пример #1
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.");
            }
        }