Ejemplo n.º 1
0
        public void string2()
        {
            //init variables
            string modelname = "1.xml";

            changed = false;

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "/root/person/firstname/text()", typeof(string), modelname);
            f.Changed        += Changed;
            f.ModelLookup    += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable   = true;

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();

            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.AreEqual("Joe", (string)f.Value);

            //change the model, dont update this xpath expression.
            changed = false;
            model1["person"]["firstname"].InnerText = "bob";
            f.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.AreEqual("bob", (string)f.Value);
        }
Ejemplo n.º 2
0
 private void CreateFact1()
 {
     f1 = new RuleEngine.Evidence.Fact("f1", 1, 2d, typeof(double));
     f1.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
     f1.IsEvaluatable   = true;
     f1.Evaluate();
 }
Ejemplo n.º 3
0
        public void string6()
        {
            string modelname = "1.xml";

            changed = false;

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "/root/person/firstname/text()", typeof(string), modelname);
            f.Changed        += Changed;
            f.ModelLookup    += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable   = true;

            //init model
            XmlDocument doc = new XmlDocument();

            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            //clone
            RuleEngine.Evidence.Fact a = (RuleEngine.Evidence.Fact)f.Clone();
            a.Changed        += Changed;
            a.ModelLookup    += ModelLookup;
            a.EvidenceLookup += EvidenceLookup;
            a.IsEvaluatable   = true;

            model1["person"]["firstname"].InnerText = "Bob";
            a.Evaluate();

            Assert.True((string)f.Value == "Joe");
            Assert.True((string)a.Value == "Bob");
            Assert.True(changed == true);
        }
Ejemplo n.º 4
0
 private void CreateFact1()
 {
     f1 = new RuleEngine.Evidence.Fact("f1", 1,2d, typeof(double));
     f1.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
     f1.IsEvaluatable = true;
     f1.Evaluate();
 }
Ejemplo n.º 5
0
 private void CreateFact2()
 {
     f2 = new RuleEngine.Evidence.Fact("f2", 1, 3d, typeof(double));
     f2.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
     f2.IsEvaluatable   = true;
     f2.Evaluate();
 }
Ejemplo n.º 6
0
        public void string1()
        {
            //init variables
            string modelname = "1.xml";

            changed = false;

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "/root/person/firstname/text()", typeof(string), modelname);
            f.Changed        += Changed;
            f.ModelLookup    += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable   = true;
            f.Evaluate();

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();

            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.Equal(true, changed);
            Assert.Equal("Joe", (string)f.Value);
        }
Ejemplo n.º 7
0
        public void string7()
        {
            //init variables
            string modelname = "1.xml";

            changed = false;

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "Joe", typeof(string));
            f.Changed        += Changed;
            f.ModelLookup    += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable   = true;

            //init model
            XmlDocument doc = new XmlDocument();

            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            //clone
            RuleEngine.Evidence.Fact a = (RuleEngine.Evidence.Fact)f.Clone();
            a.Changed        += Changed;
            a.ModelLookup    += ModelLookup;
            a.EvidenceLookup += EvidenceLookup;
            a.IsEvaluatable   = true;

            a.Value = "Bob";
            a.Evaluate();
            f.Evaluate();

            Assert.IsTrue((string)f.Value == "Joe");
            Assert.IsTrue((string)a.Value == "Bob");
            Assert.IsTrue(changed == true);
        }
Ejemplo n.º 8
0
        private static void IndividualFactTest()
        {
            DateTime end;
            DateTime start;
            TimeSpan diff;
            Xml x;
            int total;

            ROM rom = new ROM();

            //set up out facts
            Fact F1 = new Fact("F1", 1, new Naked("1", typeof(double)), typeof(double));
            Fact F2 = new Fact("F2", 1, new Naked("1", typeof(double)), typeof(double));
            Fact F3 = new Fact("F3", 1, new Naked(null, typeof(double)), typeof(double));
            rom.AddEvidence(F1);
            rom.AddEvidence(F2);
            rom.AddEvidence(F3);

            //set up our assignments
            ActionExpression A1 = new ActionExpression("1", "F1", "2", 2);
            ActionExpression A2 = new ActionExpression("2", "F2", "2", 2);
            rom.AddEvidence(A1);
            rom.AddEvidence(A2);

            List<EvidenceSpecifier> list = new List<EvidenceSpecifier>();
            list.Add(new EvidenceSpecifier(true, "1"));
            list.Add(new EvidenceSpecifier(true, "2"));

            //create a rule
            Rule R1 = new Rule("R1", "1==1", list, 500, true);

            rom.AddEvidence(R1);
            rom.Evaluate();

            Console.WriteLine("Starting Test:" + DateTime.Now);
            total = 1000;
            start = DateTime.Now;
            for (int counter = 0; counter < total; counter++)
            {
                //cause rules to evaluate
                rom.Evaluate();
            }
            end = DateTime.Now;
            diff = end - start;
            Console.WriteLine("Total ms: " + diff.TotalMilliseconds);
            Console.WriteLine("milliseconds per rule: " + (diff.TotalMilliseconds / (total * 8d))); //eight rules per run
        }
Ejemplo n.º 9
0
        public void TestCloneFunctionality()
        {
            RuleEngine.ROM rom = new RuleEngine.ROM();

            //facts and rules available to these text cases
            RuleEngine.Evidence.Fact f1 = new RuleEngine.Evidence.Fact("f1", 1, 2d, typeof(double));
            RuleEngine.Evidence.Fact f2 = new RuleEngine.Evidence.Fact("f2", 1, 4d, typeof(double));

            RuleEngine.Evidence.Actions.ActionExpression a1 = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f1", "3", 1);
            RuleEngine.Evidence.Actions.ActionExpression a2 = new RuleEngine.Evidence.Actions.ActionExpression("a2", "f2", "3", 1);
            RuleEngine.Evidence.Actions.ActionExecute    a3 = new RuleEngine.Evidence.Actions.ActionExecute("a3", "R2", 1);

            System.Collections.Generic.List <RuleEngine.Evidence.EvidenceSpecifier> actionList = new System.Collections.Generic.List <RuleEngine.Evidence.EvidenceSpecifier>();
            actionList.Add(new RuleEngine.Evidence.EvidenceSpecifier(true, "a1"));
            actionList.Add(new RuleEngine.Evidence.EvidenceSpecifier(true, "a2"));
            actionList.Add(new RuleEngine.Evidence.EvidenceSpecifier(true, "a3"));

            RuleEngine.Evidence.Rule r2 = new RuleEngine.Evidence.Rule("R2", "f1!=f2", actionList, 1, false);

            rom.AddEvidence(f1);
            rom.AddEvidence(f2);
            rom.AddEvidence(a1);
            rom.AddEvidence(a2);
            rom.AddEvidence(a3);
            rom.AddEvidence(r2);

            rom.AddDependentFact("f1", "R2");
            rom.AddDependentFact("f2", "R2");

            f1.IsEvaluatable = true;
            f2.IsEvaluatable = true;
            a1.IsEvaluatable = true;
            a2.IsEvaluatable = true;
            a3.IsEvaluatable = true;
            r2.IsEvaluatable = true;

            RuleEngine.ROM rom2 = (RuleEngine.ROM)rom.Clone();
            rom2.Evaluate();

            Assert.AreEqual("3", rom2["f1"].Value.ToString());
            Assert.AreEqual("3", rom2["f2"].Value.ToString());
            Assert.AreNotEqual(rom["f1"].Value.ToString(), rom2["f1"].Value.ToString());
        }
Ejemplo n.º 10
0
        public void string4()
        {
            //init variables
            string modelname1 = "1.xml";
            string modelname2 = "2.xml";

            changed = false;

            RuleEngine.Evidence.Fact b = new RuleEngine.Evidence.Fact("f1", 1, "/root/address", typeof(XmlNode), modelname1);
            b.Changed        += Changed;
            b.ModelLookup    += ModelLookup;
            b.EvidenceLookup += EvidenceLookup;
            b.IsEvaluatable   = true;
            b.Evaluate();

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "@pobox", typeof(bool), modelname2);
            f.Changed        += Changed;
            f.ModelLookup    += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable   = true;
            f.Evaluate();

            //init model
            XmlDocument doc = new XmlDocument();

            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname1);
            model1 = doc.DocumentElement;
            model2 = doc["root"]["person"]["address"];
            f.Evaluate();
            b.Evaluate();

            Assert.Equal(true, changed);
            Assert.True(b.Value is XmlNode);

            //change the model, update this xpath expression.
            changed = false;
            f.Value = true;

            Assert.Equal(true, changed);
            Assert.True(Boolean.Parse(model2.Attributes["pobox"].Value));
            Assert.True(Boolean.Parse(model1["person"]["address"].Attributes["pobox"].Value));
        }
Ejemplo n.º 11
0
        public void string1()
        {
            //init variables
            string modelname = "1.xml";
            changed = false;

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "/root/person/firstname/text()", typeof(string), modelname);
            f.Changed += Changed;
            f.ModelLookup += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable = true;

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.AreEqual("Joe", (string)f.Value);
        }
Ejemplo n.º 12
0
 private void CreateFact2()
 {
     f2 = new RuleEngine.Evidence.Fact("f2", 1, 3d, typeof(double));
     f2.EvidenceLookup += new EvidenceLookupHandler(EvidenceLookup);
     f2.IsEvaluatable = true;
     f2.Evaluate();
 }
Ejemplo n.º 13
0
        public void double1()
        {
            //init variables
            string modelname = "1.xml";
            bool changed = false;
            XmlNode model1 = null;
            XmlNode model2 = null;
            RuleEngine.Evidence.Fact f1 = new RuleEngine.Evidence.Fact("f1", 1, 2d, typeof(double));
            RuleEngine.Evidence.Fact f2 = new RuleEngine.Evidence.Fact("f2", 1, 4d, typeof(double));
            #region delegates
            f1.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f1.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f1.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            #region delegates
            f2.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f2.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f2.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f1.IsEvaluatable = true;
            f2.IsEvaluatable = true;
            f1.Evaluate();
            f2.Evaluate();

            RuleEngine.Evidence.Actions.ActionExpression f = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f1", "3", 1);
            #region delegates
            f.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                    return model1;
                else if (args.Key == "2.xml")
                    return model2;
                else
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
            };
            f.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return f1;
                }
                else if (args.Key == "f2")
                {
                    return f2;
                }
                else
                    throw new Exception("Unknown evidence");
            };
            #endregion
            f.IsEvaluatable = true;

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.IsTrue(f1.Value is double);
            Assert.AreEqual(3, (double)f1.Value);
        }
Ejemplo n.º 14
0
        public void double3()
        {
            //init variables
            string  modelname = "1.xml";
            bool    changed   = false;
            XmlNode model1    = null;
            XmlNode model2    = null;

            RuleEngine.Evidence.Fact f1 = new RuleEngine.Evidence.Fact("f1", 1, 2d, typeof(double));
            RuleEngine.Evidence.Fact f2 = new RuleEngine.Evidence.Fact("f2", 1, 4d, typeof(double));
            #region delegates
            f1.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f1.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                {
                    return(model1);
                }
                else if (args.Key == "2.xml")
                {
                    return(model2);
                }
                else
                {
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
                }
            };
            f1.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return(f1);
                }
                else if (args.Key == "f2")
                {
                    return(f2);
                }
                else
                {
                    throw new Exception("Unknown evidence");
                }
            };
            #endregion
            #region delegates
            f2.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f2.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                {
                    return(model1);
                }
                else if (args.Key == "2.xml")
                {
                    return(model2);
                }
                else
                {
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
                }
            };
            f2.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return(f1);
                }
                else if (args.Key == "f2")
                {
                    return(f2);
                }
                else
                {
                    throw new Exception("Unknown evidence");
                }
            };
            #endregion
            f1.IsEvaluatable = true;
            f2.IsEvaluatable = true;
            f1.Evaluate();
            f2.Evaluate();

            RuleEngine.Evidence.Actions.ActionExpression f = new RuleEngine.Evidence.Actions.ActionExpression("a1", "f1", "f2+1", 1);
            #region delegates
            f.Changed += delegate(object source, ChangedArgs args)
            {
                changed = true;
            };
            f.ModelLookup += delegate(object source, ModelLookupArgs args)
            {
                if (args.Key == "1.xml")
                {
                    return(model1);
                }
                else if (args.Key == "2.xml")
                {
                    return(model2);
                }
                else
                {
                    throw new Exception("Couldnt find model: " + ((ModelLookupArgs)args).Key);
                }
            };
            f.EvidenceLookup += delegate(object source, EvidenceLookupArgs args)
            {
                if (args.Key == "f1")
                {
                    return(f1);
                }
                else if (args.Key == "f2")
                {
                    return(f2);
                }
                else
                {
                    throw new Exception("Unknown evidence");
                }
            };
            #endregion
            f.IsEvaluatable = true;

            //init model
            changed = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            Assert.Equal(true, changed);
            Assert.True(f1.Value is double);
            Assert.Equal(5, (double)f1.Value);
        }
Ejemplo n.º 15
0
        private static void LoadFacts(ROM rom, XmlDocument document)
        {
            XmlNodeList facts = document.SelectNodes("RuleEngine/Facts//Fact");

            foreach (XmlNode factNode in facts)
            {
                string id = factNode.Attributes["id"].Value;
                string type = factNode.Attributes["type"].Value;
                string desc = factNode.Attributes["desc"].Value;
                string modelId = factNode.Attributes["modelId"].Value;
                Type valueType = null;

                //ensure same rule wont be entered twice
                if (rom[id]!=null)
                    throw new Exception("Fact has already been loaded: " + id);

                //determine priority
                int priority = 500;
                if (factNode.Attributes["priority"]!=null)
                    priority = Int32.Parse(factNode.Attributes["priority"].Value);

                IFact fact = null;
                if (factNode["xpath"].InnerText != String.Empty)
                {
                    //determine xpath
                    string xpath = factNode["xpath"].InnerText;

                    //determine value type
                    switch (type) //deterrmine the type of value returned by xpath
                    {
                        case "double":
                            valueType = typeof(double);
                            break;
                        case "boolean":
                            valueType = typeof(bool);
                            break;
                        case "string":
                            valueType = typeof(string);
                            break;
                        default:
                            throw new Exception("Invalid type: " + id + " " + type);
                    }

                    //determine value
                    Xml x = new Xml(xpath, valueType, modelId);

                    //create fact and add it to the rom
                    fact = new Fact(id, priority, xpath, valueType, modelId);
                }
                else
                {
            /*
                    //determine value type
                    switch (type) //deterrmine the type of value returned by xpath
                    {
                        case "double":
                            valueType = typeof(double);
                            break;
                        case "boolean":
                            valueType = typeof(bool);
                            break;
                        case "string":
                            valueType = typeof(string);
                            break;
                        default:
                            throw new Exception("Invalid type: " + id + " " + type);
                    }

                    //determine value
                    Naked x = new Naked(value, valueType);

                    //create fact and add it to the rom
                    fact = new Fact(id, priority, valueType,);
             */
                }

                rom.AddEvidence(fact);
            }
        }
Ejemplo n.º 16
0
        public void TestEvaluateFunctionality()
        {
            //get a datareader stream from the xml file
            XmlReaderSettings xrs = new XmlReaderSettings();

            xrs.ConformanceLevel             = ConformanceLevel.Document;
            xrs.IgnoreComments               = true;
            xrs.IgnoreProcessingInstructions = true;
            xrs.IgnoreWhitespace             = true;
            Stream    s      = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Expression\ExpressionEvaluator.xml").BaseStream;
            XmlReader reader = XmlReader.Create(s, xrs);

            //advance the reader to the first test element
            string comment   = "";
            string condition = "";
            string result    = "";

            while (!reader.EOF)
            {
                Debug.Flush();

                if (reader.IsStartElement("Tests"))
                {
                    reader.Read();
                    continue;
                }

                if (reader.IsStartElement("Test"))
                {
                    comment   = "";
                    condition = "";
                    result    = "";
                    reader.Read();
                    continue;
                }

                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                    case "Comment":
                        comment = reader.ReadElementString();
                        break;

                    case "Condition":
                        condition = reader.ReadElementString();
                        break;

                    case "Result":
                        result = reader.ReadElementString();
                        break;
                    }
                    continue;
                }

                //run the test on test end element
                if (reader.Name == "Test" && !reader.IsStartElement())
                {
                    Debug.WriteLine("");
                    Debug.Write("Test Case: ");
                    Debug.WriteLine(comment);
                    Debug.WriteLine(condition);
                    Debug.WriteLine(result);

                    //facts and rules available to these text cases
                    RuleEngine.Evidence.Fact f1   = new RuleEngine.Evidence.Fact("F1", 1, 2d, typeof(double));
                    RuleEngine.Evidence.Fact f2   = new RuleEngine.Evidence.Fact("F2", 1, 4d, typeof(double));
                    RuleEngine.Evidence.Fact s1   = new RuleEngine.Evidence.Fact("String", 1, "String", typeof(string));
                    RuleEngine.Evidence.Fact in1  = new RuleEngine.Evidence.Fact("In", 1, 2d, typeof(double));
                    RuleEngine.Evidence.Fact out1 = new RuleEngine.Evidence.Fact("Out", 1, null, typeof(double));
                    f1.IsEvaluatable = true;
                    f2.IsEvaluatable = true;
                    s1.IsEvaluatable = true;

                    RuleEngine.Evidence.ExpressionEvaluator e = new RuleEngine.Evidence.ExpressionEvaluator();
                    e.Parse(condition);
                    e.InfixToPostfix();
                    e.GetEvidence += delegate(object source, EvidenceLookupArgs args)
                    {
                        if (args.Key == "F1")
                        {
                            return(f1);
                        }
                        else if (args.Key == "F2")
                        {
                            return(f2);
                        }
                        else if (args.Key == "String")
                        {
                            return(s1);
                        }
                        else if (args.Key == "In")
                        {
                            return(in1);
                        }
                        else if (args.Key == "Out")
                        {
                            return(out1);
                        }
                        else
                        {
                            throw new Exception("Unknown evidence: " + args.Key);
                        }
                    };
                    RuleEngine.Evidence.ExpressionEvaluator.Symbol r = e.Evaluate();

                    //throw exception up stack if an error was present
                    Assert.IsFalse(r.type == RuleEngine.Evidence.ExpressionEvaluator.Type.Invalid && result != "Invalid");
                    Assert.AreEqual(result, r.value.Value.ToString().ToLower());

                    reader.Read();
                    continue;
                }

                //junk gos here
                reader.Read();
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        //[System.Diagnostics.DebuggerHidden]
        public override object Clone()
        {
            Fact f = (Fact)base.Clone();

            return(f);
        }
Ejemplo n.º 18
0
        public void string7()
        {
            //init variables
            string modelname = "1.xml";
            changed = false;

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "Joe", typeof(string));
            f.Changed += Changed;
            f.ModelLookup += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable = true;

            //init model
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname);
            model1 = doc.DocumentElement;
            f.Evaluate();

            //clone
            RuleEngine.Evidence.Fact a = (RuleEngine.Evidence.Fact)f.Clone();
            a.Changed += Changed;
            a.ModelLookup += ModelLookup;
            a.EvidenceLookup += EvidenceLookup;
            a.IsEvaluatable = true;

            a.Value = "Bob";
            a.Evaluate();
            f.Evaluate();

            Assert.IsTrue((string)f.Value == "Joe");
            Assert.IsTrue((string)a.Value == "Bob");
            Assert.IsTrue(changed == true);
        }
Ejemplo n.º 19
0
        public void string4()
        {
            //init variables
            string modelname1 = "1.xml";
            string modelname2 = "2.xml";
            changed = false;

            RuleEngine.Evidence.Fact b = new RuleEngine.Evidence.Fact("f1", 1, "/root/address", typeof(XmlNode), modelname1);
            b.Changed += Changed;
            b.ModelLookup += ModelLookup;
            b.EvidenceLookup += EvidenceLookup;
            b.IsEvaluatable = true;
            b.Evaluate();

            RuleEngine.Evidence.Fact f = new RuleEngine.Evidence.Fact("f1", 1, "@pobox", typeof(bool), modelname2);
            f.Changed += Changed;
            f.ModelLookup += ModelLookup;
            f.EvidenceLookup += EvidenceLookup;
            f.IsEvaluatable = true;
            f.Evaluate();

            //init model
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Fact\" + modelname1);
            model1 = doc.DocumentElement;
            model2 = doc["root"]["person"]["address"];
            f.Evaluate();
            b.Evaluate();

            Assert.AreEqual(true, changed);
            Assert.IsTrue(b.Value is XmlNode);

            //change the model, update this xpath expression.
            changed = false;
            f.Value = true;

            Assert.AreEqual(true, changed);
            Assert.IsTrue(Boolean.Parse(model2.Attributes["pobox"].Value));
            Assert.IsTrue(Boolean.Parse(model1["person"]["address"].Attributes["pobox"].Value));
        }
Ejemplo n.º 20
0
        public void TestEvaluateFunctionality()
        {
            //get a datareader stream from the xml file
            XmlReaderSettings xrs = new XmlReaderSettings();
            xrs.ConformanceLevel = ConformanceLevel.Document;
            xrs.IgnoreComments = true;
            xrs.IgnoreProcessingInstructions = true;
            xrs.IgnoreWhitespace = true;
            Stream s = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\Expression\ExpressionEvaluator.xml").BaseStream;
            XmlReader reader = XmlReader.Create(s, xrs);

            //advance the reader to the first test element
            string comment="";
            string condition="";
            string result="";

            while (!reader.EOF)
            {
                Debug.Flush();

                if (reader.IsStartElement("Tests"))
                {
                    reader.Read();
                    continue;
                }

                if (reader.IsStartElement("Test"))
                {
                    comment = "";
                    condition = "";
                    result = "";
                    reader.Read();
                    continue;
                }

                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                        case "Comment":
                            comment = reader.ReadElementString();
                            break;
                        case "Condition":
                            condition = reader.ReadElementString();
                            break;
                        case "Result":
                            result = reader.ReadElementString();
                            break;
                    }
                    continue;
                }

                //run the test on test end element
                if (reader.Name == "Test" && !reader.IsStartElement())
                {
                    Debug.WriteLine("");
                    Debug.Write("Test Case: ");
                    Debug.WriteLine(comment);
                    Debug.WriteLine(condition);
                    Debug.WriteLine(result);

                    //facts and rules available to these text cases
                    RuleEngine.Evidence.Fact f1 = new RuleEngine.Evidence.Fact("F1", 1, 2d, typeof(double));
                    RuleEngine.Evidence.Fact f2 = new RuleEngine.Evidence.Fact("F2", 1, 4d, typeof(double));
                    RuleEngine.Evidence.Fact s1 = new RuleEngine.Evidence.Fact("String", 1, "String", typeof(string));
                    RuleEngine.Evidence.Fact in1 = new RuleEngine.Evidence.Fact("In", 1, 2d, typeof(double));
                    RuleEngine.Evidence.Fact out1 = new RuleEngine.Evidence.Fact("Out", 1, null, typeof(double));
                    f1.IsEvaluatable = true;
                    f2.IsEvaluatable = true;
                    s1.IsEvaluatable = true;

                    RuleEngine.Evidence.ExpressionEvaluator e = new RuleEngine.Evidence.ExpressionEvaluator();
                    e.Parse(condition);
                    e.InfixToPostfix();
                    e.GetEvidence += delegate(object source, EvidenceLookupArgs args)
                        {
                            if (args.Key == "F1")
                            {
                                return f1;
                            }
                            else if (args.Key == "F2")
                            {
                                return f2;
                            }
                            else if (args.Key == "String")
                            {
                                return s1;
                            }
                            else if (args.Key == "In")
                            {
                                return in1;
                            }
                            else if (args.Key == "Out")
                            {
                                return out1;
                            }
                            else
                                throw new Exception("Unknown evidence: " + args.Key);
                        };
                    RuleEngine.Evidence.ExpressionEvaluator.Symbol r = e.Evaluate();

                    //throw exception up stack if an error was present
                    Assert.IsFalse(r.type == RuleEngine.Evidence.ExpressionEvaluator.Type.Invalid && result != "Invalid");
                    Assert.AreEqual(result, r.value.Value.ToString().ToLower());

                    reader.Read();
                    continue;
                }

                //junk gos here
                reader.Read();
            }
        }