Пример #1
0
        /// <summary>
        /// Parses conditions from the node.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="conditions">Conditions list to add new conditions to.</param>
        /// <param name="negative">Whether the conditions should be negated.</param>
        /// <param name="config">Rewriter configuration</param>
        protected void ParseConditions(XmlNode node, IList <IRewriteCondition> conditions, bool negative, IRewriterConfiguration config)
        {
            if (config == null)
            {
                return;
            }

            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (conditions == null)
            {
                throw new ArgumentNullException(nameof(conditions));
            }

            // Parse attribute-based conditions.
            config.ConditionParserPipeline.ForEach(parser =>
            {
                var condition = parser.Parse(node);

                if (condition == null)
                {
                    return;
                }

                if (negative)
                {
                    condition = new NegativeCondition(condition);
                }

                conditions.Add(condition);
            });

            // Now, process the nested <and> conditions.
            var childNode = node.FirstChild;

            while (childNode != null)
            {
                if (childNode.NodeType == XmlNodeType.Element)
                {
                    if (childNode.LocalName == Constants.ElementAnd)
                    {
                        this.ParseConditions(childNode, conditions, negative, config);

                        var childNode2 = childNode.NextSibling;
                        node.RemoveChild(childNode);
                        childNode = childNode2;
                        continue;
                    }
                }

                childNode = childNode.NextSibling;
            }
        }
Пример #2
0
        /// <summary>
        /// Parses conditions from the node.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="conditions">Conditions list to add new conditions to.</param>
        /// <param name="negative">Whether the conditions should be negated.</param>
        /// <param name="config">Rewriter configuration</param>
        protected void ParseConditions(XmlNode node, IList conditions, bool negative, object config)
        {
            if (config == null)
            {
                return;
            }
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (conditions == null)
            {
                throw new ArgumentNullException("conditions");
            }

            RewriterConfiguration rewriterConfig = config as RewriterConfiguration;

            // Parse attribute-based conditions.
            foreach (IRewriteConditionParser parser in rewriterConfig.ConditionParserPipeline)
            {
                IRewriteCondition condition = parser.Parse(node);
                if (condition != null)
                {
                    if (negative)
                    {
                        condition = new NegativeCondition(condition);
                    }

                    conditions.Add(condition);
                }
            }

            // Now, process the nested <and> conditions.
            XmlNode childNode = node.FirstChild;

            while (childNode != null)
            {
                if (childNode.NodeType == XmlNodeType.Element)
                {
                    if (childNode.LocalName == Constants.ElementAnd)
                    {
                        ParseConditions(childNode, conditions, negative, config);

                        XmlNode childNode2 = childNode.NextSibling;
                        node.RemoveChild(childNode);
                        childNode = childNode2;
                        continue;
                    }
                }

                childNode = childNode.NextSibling;
            }
        }
Пример #3
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");
        }
Пример #4
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");
        }