Example #1
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());
        }
Example #2
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
        }
Example #3
0
        public void TestHarness()
        {
            //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 + @"\..\..\Compiler\Compiler.xml").BaseStream;
            XmlReader reader = XmlReader.Create(s, xrs);

            string    comment;
            string    ruleset;
            ArrayList model = null;

            RuleEngine.ROM rom = null;

            bool inTest = false;

            while (!reader.EOF)
            {
                if (reader.IsStartElement("test"))
                {
                    Debug.WriteLine("START TEST");
                    inTest = true;
                    model  = new ArrayList();
                    reader.Read();
                }

                else if (inTest && reader.Name == "comment")
                {
                    comment = reader.ReadElementContentAsString();
                    Debug.WriteLine(comment);
                }

                else if (inTest && reader.Name == "ruleset")
                {
                    ruleset = reader.ReadElementContentAsString();
                    Debug.WriteLine(ruleset);
                    XmlDocument doc = new XmlDocument();
                    doc.Load(ruleset);
                    rom = RuleEngine.Compiler.Compiler.Compile(doc);
                }

                else if (inTest && reader.Name == "model")
                {
                    string      mid = reader.GetAttribute("modelId");
                    string      m   = reader.ReadElementContentAsString();
                    XmlDocument mod = new XmlDocument();
                    mod.Load(m);
                    model.Add(mod);
                    rom.AddModel(mid, mod);
                }

                else if (inTest && reader.Name == "evaluate")
                {
                    //evaluate
                    Debug.WriteLine("Evaluate");
                    rom.Evaluate();
                    reader.Read();
                }

                else if (inTest && reader.Name == "assign")
                {
                    Debug.WriteLine("Assign");
                    string mid = reader.GetAttribute("factId");
                    string m   = reader.ReadElementContentAsString();

                    object value;
                    //determine value type
                    switch (rom[mid].ValueType.ToString()) //deterrmine the type of value returned by xpath
                    {
                    case "System.Double":
                        value = Double.Parse(m);
                        break;

                    case "System.Boolean":
                        value = Boolean.Parse(m);
                        break;

                    case "System.String":
                        value = m;
                        break;

                    default:
                        throw new Exception("Invalid type: " + m);
                    }
                    rom[mid].Value = value;
                }

                else if (inTest && reader.Name == "result")
                {
                    Debug.WriteLine("Assert");
                    string mid = reader.GetAttribute("factId");
                    string m   = reader.ReadElementContentAsString();

                    Assert.Equal(m, rom[mid].Value.ToString());
                }

                else if (inTest && reader.Name == "test")
                {
                    rom     = null;
                    model   = null;
                    comment = null;
                    ruleset = null;
                    inTest  = false;
                    reader.Read();
                    Debug.WriteLine("END TEST");
                }
                else
                {
                    reader.Read();
                }
            }
        }