/// <summary> /// Load Node from XmlNode /// </summary> /// <param name="node"></param> /// <returns></returns> public static RuleSet loadFromXmlNode(XmlNode node) { RuleSet root = new RuleSet(new TruePredicate()); if (node.Attributes["id"] != null) root.id = node.Attributes["id"].Value; if (node.Attributes["defaultScore"] != null) root.fdefaultscore = node.Attributes["defaultScore"].Value; if (node.Attributes["recordCount"] != null) root.recordCount = Convert.ToDecimal(node.Attributes["recordCount"].Value, CultureInfo.InvariantCulture); root.ruleSelectionMethods = new List<RuleSelectionMethod>(); root.scoreDistributions = new List<ScoreDistribution>(); foreach(XmlNode item in node.ChildNodes) { if ("extension".Equals(item.Name.ToLowerInvariant())) { // TODO : implement extension //root.Nodes.Add(Node.loadFromXmlNode(item)); } else if ("ruleselectionmethod".Equals(item.Name.ToLowerInvariant())) { root.RuleSelectionMethods.Add(RuleSelectionMethod.loadFromXmlNode(item)); } else if ("scoredistribution".Equals(item.Name.ToLowerInvariant())) { root.ScoreDistributions.Add(ScoreDistribution.loadFromXmlNode(item)); } else if ("simplerule".Equals(item.Name.ToLowerInvariant())) { root.Rules.Add(SimpleRule.loadFromXmlNode(item)); } else throw new NotImplementedException(); } return root; }
/// <summary> /// Scoring with rule set model /// </summary> /// <param name="root">Parent node</param> /// <param name="criterionStr">Criterion</param> /// <param name="dict">Values</param> /// <param name="res" >Result to return</param> /// <returns></returns> public static ScoreResult Evaluate(RuleSet root, String criterionStr, Dictionary<string, object> dict, ScoreResult res) { // HACK res.Value = root.fdefaultscore; res.Confidence = 1.0M; // Both the ordering of keys and values is significant OrderedDictionary/*<String, SimpleRule>*/ firedRules = new OrderedDictionary();//<String, SimpleRule> // Test childs foreach(Rule rule in root.Rules) { collectFiredRules(firedRules, rule, dict); if (firedRules.Count > 0) foreach (String key in firedRules.Keys) { res.Value = key; res.Confidence = 1.0M; //((SimpleRule)firedRules(key)).. return res; } } // For now we return the first /*foreach (String key in firedRules.Keys) { res.Value = key; res.Confidence = 1.0M; //((SimpleRule)firedRules(key)).. return res; }*/ // Test childs /*foreach(Rule rule in root.Rules) { PredicateResult resRule = rule.Evaluate(dict); if (resRule == PredicateResult.True) { //res.Nodes.Add(child); res.Value = rule.Score; foreach(ScoreDistribution sco in rule.ScoreDistributions) { if (sco.Value.Equals(rule.Score)) { if (sco.Confidence != null) res.Confidence = Decimal.Parse(sco.Confidence, NumberStyles.Any, CultureInfo.InvariantCulture); } } return Evaluate(child, missingvalueStr, noTrueChildStr, dict, res); } if (resRule == PredicateResult.Unknown) throw new NotImplementedException();*/ /*else if (childPredicate == PredicateResult.Unknown) { // Unknow value lead to act with missingvalueStr switch(missingvalueStr) { case MissingValueStrategy.LastPrediction: return res; case MissingValueStrategy.NullPrediction: res.Value = null; return res; case MissingValueStrategy.WeightedConfidence: Dictionary<string, decimal> conf = CalculateConfidence(root, dict); string max_conf = null; foreach(string key in conf.Keys) { if (max_conf == null) max_conf = key; if (conf[key] > conf[max_conf]) max_conf = key; } res.Value = max_conf; res.Confidence = conf[max_conf]; return res; case MissingValueStrategy.AggregateNodes: return res; default: throw new NotImplementedException(); } } }*/ // All child nodes are false /*if (root.Nodes.Count > 0) if (noTrueChildStr == NoTrueChildStrategy.ReturnNullPrediction) { res.Value = null; }*/ return res; }