Exemplo n.º 1
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
        }
Exemplo n.º 2
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());
        }
Exemplo n.º 3
0
        private static void FactRelationship(ROM rom, XmlDocument doc)
        {
            //go though each chainable rule and determine who they are dependent on
            foreach(IEvidence evidence in rom.Evidence.Values)
            {
                if (!(evidence is IRule))
                    continue;

                IRule rule = (IRule)evidence;

                if (!rule.isChainable)
                    continue;

                foreach (string depFacts in rule.DependentEvidence)
                {
                    rom.AddDependentFact(depFacts, rule.ID);
                }
            }
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
 private static void LoadActions(ROM rom, XmlDocument document)
 {
 }
Exemplo n.º 6
0
 private static ROM CreateRom()
 {
     ROM rom = new ROM();
     return rom;
 }
Exemplo n.º 7
0
        private static void LoadRules(ROM rom, XmlDocument document)
        {
            XmlNodeList rules = document.SelectNodes("RuleEngine/Rules//Rule");

            foreach (XmlNode ruleNode in rules)
            {
                string id = ruleNode.Attributes["id"].Value;
                bool inference = false;
                if (ruleNode.Attributes["chainable"] != null)
                    inference = Boolean.Parse(ruleNode.Attributes["chainable"].Value);
                int priority = 500;
                if (ruleNode.Attributes["priority"] != null)
                    priority = Int32.Parse(ruleNode.Attributes["priority"].Value);

                //ensure this has not already been entered
                if (rom[id] != null)
                    throw new Exception("Rule Id's must be unique: " + id);

                //expression
                string condition = ruleNode["Condition"].InnerText;

                //actions
                int actionCounter = 0;
                List<EvidenceSpecifier> actions = new List<EvidenceSpecifier>();
                XmlNodeList actionList;
                #region Evaluate
                actionList = ruleNode.SelectNodes("Actions//Evaluate");
                foreach (XmlNode a in actionList)
                {
                    try
                    {
                        string actionOperatingName = a.Attributes["factId"].Value;
                        string aValue = a.InnerText;
                        bool result = true;
                        int actionPriority = 500;
                        if (a.Attributes["priority"] != null)
                            actionPriority = Int32.Parse(a.Attributes["priority"].Value);
                        if (a.Attributes["result"] != null)
                            result = Boolean.Parse(a.Attributes["result"].Value);

                        string actionId = id + "-" + actionOperatingName + "-" + actionCounter++;
                        rom.AddEvidence(new ActionExpression(actionId, actionOperatingName, aValue, actionPriority));
                        actions.Add(new EvidenceSpecifier(result, actionId));
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Invalid action: " + a.OuterXml, e);
                    }
                }
                #endregion
                #region Execute
                actionList = ruleNode.SelectNodes("Actions//Execute");
                foreach (XmlNode a in actionList)
                {
                    try
                    {
                        string actionOperatingName = a.Attributes["factId"].Value;
                        bool result = true;
                        int actionPriority = 500;
                        if (a.Attributes["priority"] != null)
                            actionPriority = Int32.Parse(a.Attributes["priority"].Value);
                        if (a.Attributes["result"] != null)
                            result = Boolean.Parse(a.Attributes["result"].Value);

                        string actionId = id + "-" + actionOperatingName + "-" + actionCounter++;
                        rom.AddEvidence(new ActionExecute(actionId, actionOperatingName, actionPriority));
                        actions.Add(new EvidenceSpecifier(result, actionId));
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Invalid action: " + a.OuterXml, e);
                    }
                }
                #endregion

                //now create the rule
                IRule r = new Rule(id, condition, actions, priority, inference);
                rom.AddEvidence(r);
            }
        }
Exemplo n.º 8
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();
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            ROM rom = new ROM();
            rom.callback = new Dictionary<string, Delegate>(this.callback);

            rom.dependentEvidence = new Dictionary<string, List<string>>();
            foreach (string key in this.dependentEvidence.Keys)
            {
                rom.dependentEvidence.Add(key, new List<string>(this.dependentEvidence[key]));
            }

            rom.evidenceCollection = new Dictionary<string, IEvidence>();
            foreach (string key in this.evidenceCollection.Keys)
            {
                IEvidence evidence = (IEvidence)this.evidenceCollection[key].Clone();
                rom.evidenceCollection.Add(key, evidence);
            }

            rom.models = new Dictionary<string, XmlDocument>(this.models);

            return rom;
        }