示例#1
0
        public static IRuleDefinition Build(DecisionRule decisionRule)
        {
            var builder = new NRules.RuleModel.Builders.RuleBuilder();

            builder.Name(decisionRule.RuleName);
            var propInfo = GetRuleProperties(decisionRule);

            foreach (var prop in propInfo)
            {
                //var name = prop.Name;
                //var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                //var value = prop.GetValue(decisionRule);
                //object safeValue = (value == null) ? null : Convert.ChangeType(value, type);
                //builder.Property(name,safeValue);
                builder.Property(prop.Name, prop.GetValue(decisionRule));
            }

            var autoAuthorizationPattern = builder.LeftHandSide()
                                           .Pattern(typeof(DecisionRuleRequest), "autoAuthContract");
            Expression <Func <DecisionRuleRequest, bool> > autoAuthExpression = autoAuthContract =>
                                                                                autoAuthContract.ClientId == decisionRule.ClientId &&
                                                                                autoAuthContract.LobId == decisionRule.LobId &&
                                                                                autoAuthContract.AuthTypeId == decisionRule.AuthTypeId;

            autoAuthExpression = autoAuthExpression
                                 .AppendRules(decisionRule);

            autoAuthorizationPattern.Condition(autoAuthExpression);
            return(builder.Build());
        }
 public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
 {
     if (rule.MaxUnits.HasValue)
     {
         return(autoAuthContract => autoAuthContract.RequestedAmount <= rule.MaxUnits);
     }
     return(null);
 }
 public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
 {
     if (rule.VendorProviderSpecialityCodes != null && rule.VendorProviderSpecialityCodes.Any())
     {
         return(autoAuthContract => rule.VendorProviderSpecialityCodes.Contains(autoAuthContract.VendorProviderSpecialityCode));
     }
     return(null);
 }
 public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
 {
     if (rule.DiagnosisCodes != null && rule.DiagnosisCodes.Any())
     {
         return(autoAuthContract => rule.DiagnosisCodes.Contains(autoAuthContract.DiagnosisCode));
     }
     return(null);
 }
 public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
 {
     if (rule.ReviewTypeId.HasValue)
     {
         return(autoAuthContract => autoAuthContract.ReviewTypeId == rule.ReviewTypeId);
     }
     return(null);
 }
 public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
 {
     if (!string.IsNullOrWhiteSpace(rule.Gender))
     {
         return(autoAuthContract => autoAuthContract.Gender == rule.Gender);
     }
     return(null);
 }
 public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
 {
     if (!string.IsNullOrWhiteSpace(rule.PlaceOfService))
     {
         return
             (autoAuthContract =>
              string.Equals(autoAuthContract.PlaceOfService, rule.PlaceOfService,
                            StringComparison.CurrentCultureIgnoreCase));
     }
     return(null);
 }
        public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
        {
            if (rule.AgeLowerBound.HasValue && rule.AgeUpperBound.HasValue)
            {
                return
                    (autoAuthContract =>
                     autoAuthContract.Age > rule.AgeLowerBound.Value &&
                     autoAuthContract.Age < rule.AgeUpperBound.Value);
            }

            return(null);
        }
示例#9
0
        public void IsInconsistentTest()
        {
            var a1 = new Antecedent(0, ComparisonKind.Equal, 1.0);
            var a2 = new Antecedent(0, ComparisonKind.Equal, 1.0);
            var a3 = new Antecedent(0, ComparisonKind.Equal, 0.0);

            var rule1 = new DecisionRule(0, a1);
            var rule2 = new DecisionRule(0, a2);
            var rule3 = new DecisionRule(0, a3);

            bool a12 = rule1.IsInconsistentWith(rule2);
            bool a13 = rule1.IsInconsistentWith(rule3);

            Assert.IsFalse(a12);
            Assert.IsFalse(a13);
        }
        public void IsInconsistentTest()
        {
            var a1 = new Antecedent(0, ComparisonKind.Equal, 1.0);
            var a2 = new Antecedent(0, ComparisonKind.Equal, 1.0);
            var a3 = new Antecedent(0, ComparisonKind.Equal, 0.0);

            var rule1 = new DecisionRule(0, a1);
            var rule2 = new DecisionRule(0, a2);
            var rule3 = new DecisionRule(0, a3);

            bool a12 = rule1.IsInconsistentWith(rule2);
            bool a13 = rule1.IsInconsistentWith(rule3);

            Assert.IsFalse(a12);
            Assert.IsFalse(a13);
        }
 public Expression <Func <DecisionRuleRequest, bool> > Build(DecisionRule rule)
 {
     if (!string.IsNullOrWhiteSpace(rule.RequestingProviderSpecialityCode))
     {
         Expression <Func <DecisionRuleRequest, bool> > requestProviderSpecialityExpression =
             autoAuthContract =>
             string.Equals(autoAuthContract.RequestingProviderSpecialityCode,
                           rule.RequestingProviderSpecialityCode, StringComparison.CurrentCultureIgnoreCase);
         if (rule.IsRequestProviderInNetwork.HasValue)
         {
             requestProviderSpecialityExpression =
                 requestProviderSpecialityExpression.And(
                     autoAuthContract =>
                     autoAuthContract.IsRequestProviderInNetwork.HasValue &&
                     autoAuthContract.IsRequestProviderInNetwork.Value);
         }
         return(requestProviderSpecialityExpression);
     }
     return(null);
 }
示例#12
0
        /// <summary>
        /// Generate the decision rules based on the specisied FP-tree and the minimal
        /// confidence.
        /// </summary>
        /// <param name="tree">FP-tree</param>
        /// <param name="minConfidence">The minimal confidence</param>
        /// <returns></returns>
        public List <DecisionRule <T> > GenerateRuleSet(FpTree <T> tree, double minConfidence)
        {
            FpGrowth(tree, new List <T>());

            var decisionRules = new List <DecisionRule <T> >();

            foreach (var frequentItemSet in FrequentItemSets.Keys)
            {
                if (frequentItemSet.ItemSet.Count < 2)
                {
                    continue;
                }

                var subSets = EnumerableHelper.GetSubsets(frequentItemSet.ItemSet);

                foreach (var t in subSets)
                {
                    var leftSide = new FrequentItemSet <T>(t);
                    for (var j = 0; j < subSets.Count; j++)
                    {
                        var rightSide = new FrequentItemSet <T>(subSets[j]);
                        if (rightSide.ItemSet.Count != 1 || !FrequentItemSet <T> .SetsSeparated(rightSide, leftSide))
                        {
                            continue;
                        }

                        if (FrequentItemSets.ContainsKey(leftSide))
                        {
                            var confidence = (double)FrequentItemSets[frequentItemSet] / FrequentItemSets[leftSide];
                            if (confidence >= minConfidence)
                            {
                                var rule = new DecisionRule <T>(leftSide.ItemSet, rightSide.ItemSet, FrequentItemSets[frequentItemSet], confidence);
                                decisionRules.Add(rule);
                            }
                        }
                    }
                }
            }
            return(decisionRules);
        }
        public override void Run(ExecutionSettings executionSettings, bool printRules)
        {
            builder = new MsDataBuilder();
            var data = builder.BuildInstance(executionSettings);

            var frequentSets = data.Elements.Keys.Select(element => new List <int> {
                element
            }).AsParallel().ToList();

            frequentSets = frequentSets.Where(set => set.IsFrequent(data.Transactions, executionSettings.MinSup)).AsParallel().ToList();
            var frequentItemSets = frequentSets.AsParallel().ToDictionary(set => new FrequentItemSet <int>(set),
                                                                          set => set.GetSupport(data.Transactions));
            List <List <int> > candidates;

            while ((candidates = GenerateCandidates(frequentSets)).Count > 0)
            {
                //! sprawdź czy któryś podzbiór k-1 elementowy kadydatów nie jest w frequentSets => wywal go!

                // leave only these sets which are frequent
                candidates =
                    candidates.Where(set => set.IsFrequentParallel(data.Transactions, executionSettings.MinSup)).AsParallel().ToList();

                if (candidates.Count > 0)
                {
                    frequentSets = candidates;
                    foreach (var candidate in candidates)
                    {
                        frequentItemSets.Add(new FrequentItemSet <int>(candidate), candidate.GetSupportParallel(data.Transactions));
                    }
                }
                else
                {
                    // we don't have any more candidates
                    break;
                }
            }

            //here we should do something with the candidates
            var decisionRules = new List <DecisionRule <int> >();

            foreach (var frequentSet in frequentSets)
            {
                var subSets = EnumerableHelper.GetSubsets(frequentSet);

                foreach (var t in subSets)
                {
                    var leftSide = new FrequentItemSet <int>(t);
                    for (var j = 0; j < subSets.Count; j++)
                    {
                        var rightSide = new FrequentItemSet <int>(subSets[j]);
                        if (rightSide.ItemSet.Count != 1 || !FrequentItemSet <int> .SetsSeparated(rightSide, leftSide))
                        {
                            continue;
                        }

                        if (!frequentItemSets.ContainsKey(leftSide))
                        {
                            continue;
                        }
                        var confidence = (double)frequentItemSets[new FrequentItemSet <int>(frequentSet)] / frequentItemSets[leftSide];
                        if (confidence >= executionSettings.MinConf)
                        {
                            var rule = new DecisionRule <int>(leftSide.ItemSet, rightSide.ItemSet, frequentItemSets[new FrequentItemSet <int>(frequentSet)], confidence);
                            decisionRules.Add(rule);
                        }
                    }
                }
            }

            if (!printRules)
            {
                return;
            }

            var result = PrintRules(decisionRules, executionSettings.DataSourcePath, executionSettings.MinSup, executionSettings.MinConf, data.Transactions.Keys.Count, data.Elements);

            Console.WriteLine(result);
        }
示例#14
0
        public override void Run(ExecutionSettings executionSettings, bool printRules)
        {
            builder = new MsDataBuilder();
            var data                = builder.BuildInstance(executionSettings);
            var elementsList        = data.Elements.Keys.ToList();
            var transactionsList    = data.Transactions.Keys.ToList();
            var bitmapWrapper       = PrepareBitmapWrapper(data, elementsList, transactionsList);
            var elementsFrequencies = CalculateElementsFrequencies(bitmapWrapper);
            var frequentSets        = elementsList
                                      .Where(e => elementsFrequencies[elementsList.IndexOf(e)] >= executionSettings.MinSup * transactionsList.Count)
                                      .Select(element => new List <int> {
                element
            })
                                      .ToList();
            var frequentItemSets = frequentSets.ToDictionary(set => new FrequentItemSet <int>(set),
                                                             set => elementsFrequencies[elementsList.IndexOf(set[0])]);
            List <List <int> > candidates;

            if (frequentSets.Count == 0)
            {
                return;
            }

            var bitmapTransposed = new Bitmap(transactionsList.Count, frequentSets.Count);
            var newElementsList  = new List <int>(frequentSets.Count);
            var jj = 0;

            foreach (var set in frequentSets)
            {
                newElementsList.Add(set[0]);

                for (var i = 0; i < transactionsList.Count; i++)
                {
                    var pixel = bitmapWrapper.Bitmap.GetPixel(elementsList.IndexOf(set[0]), i);

                    bitmapTransposed.SetPixel(i, jj, pixel);
                }
                jj++;
            }
            var newBitmapWrapper = BitmapWrapper.ConvertBitmap(bitmapTransposed);

            while ((candidates = GenerateCandidates(frequentSets)).Count > 0)
            {
                // 1. tranlate into elements Id's
                foreach (var candidate in candidates)
                {
                    for (var i = 0; i < candidate.Count; i++)
                    {
                        candidate[i] = newElementsList.IndexOf(candidate[i]);
                    }
                }

                // 2. execute CUDA counting
                candidates = GetFrequentSets(candidates, executionSettings.MinSup, newBitmapWrapper, transactionsList.Count);

                // 3. translate back from elements Id's
                foreach (var candidate in candidates)
                {
                    for (var i = 0; i < candidate.Count; i++)
                    {
                        candidate[i] = newElementsList[candidate[i]];
                    }
                }

                if (candidates.Count > 0)
                {
                    var sw = new Stopwatch();
                    sw.Start();
                    frequentSets = candidates;
                    foreach (var candidate in candidates)
                    {
                        frequentItemSets.Add(new FrequentItemSet <int>(candidate), candidate.GetSupport(data.Transactions));
                    }
                    sw.Stop();
                    //Console.WriteLine("CAND: {0}", sw.ElapsedMilliseconds);
                }
                else
                {
                    // we don't have any more candidates
                    break;
                }
            }

            //here we should do something with the candidates
            var decisionRules = new List <DecisionRule <int> >();

            foreach (var frequentSet in frequentSets)
            {
                var subSets = EnumerableHelper.GetSubsets(frequentSet);

                foreach (var t in subSets)
                {
                    var leftSide = new FrequentItemSet <int>(t);
                    for (var j = 0; j < subSets.Count; j++)
                    {
                        var rightSide = new FrequentItemSet <int>(subSets[j]);
                        if (rightSide.ItemSet.Count != 1 || !FrequentItemSet <int> .SetsSeparated(rightSide, leftSide))
                        {
                            continue;
                        }

                        if (frequentItemSets.ContainsKey(leftSide))
                        {
                            var confidence = (double)frequentItemSets[new FrequentItemSet <int>(frequentSet)] / frequentItemSets[leftSide];
                            if (confidence >= executionSettings.MinConf)
                            {
                                var rule = new DecisionRule <int>(leftSide.ItemSet, rightSide.ItemSet, frequentItemSets[new FrequentItemSet <int>(frequentSet)], confidence);
                                decisionRules.Add(rule);
                            }
                        }
                    }
                }
            }

            if (!printRules)
            {
                return;
            }

            var result = PrintRules(decisionRules, executionSettings.DataSourcePath, executionSettings.MinSup, executionSettings.MinConf, data.Transactions.Keys.Count, data.Elements);

            Console.WriteLine(result);
        }
示例#15
0
        public override void Run(ExecutionSettings executionSettings)
        {
            builder = new MsDataBuilder();
            var data = builder.BuildInstance(executionSettings);

            var frequentSets = data.Elements.Keys.Select(element => new List<int> { element }).ToList();

            frequentSets = frequentSets.Where(set => set.IsFrequent(data.Transactions, executionSettings.MinSup)).ToList();
            var frequentItemSets = frequentSets.ToDictionary(set => new FrequentItemSet<int>(set),
                                                             set => set.GetSupport(data.Transactions));
            List<List<int>> candidates;

            while ((candidates = GenerateCandidates(frequentSets)).Count > 0)
            {
                //! sprawdź czy któryś podzbiór k-1 elementowy kadydatów nie jest w frequentSets => wywal go!

                // leave only these sets which are frequent
                candidates =
                    candidates.Where(set => set.IsFrequent(data.Transactions, executionSettings.MinSup)).ToList();

                if (candidates.Count > 0)
                {
                    frequentSets = candidates;
                    foreach (var candidate in candidates)
                    {
                        frequentItemSets.Add(new FrequentItemSet<int>(candidate), candidate.GetSupport(data.Transactions));
                    }
                }
                else
                {
                    // we don't have any more candidates
                    break;
                }
            }

            //here we should do something with the candidates
            var decisionRules = new List<DecisionRule<int>>();

            foreach (var frequentSet in frequentSets)
            {
                var subSets = EnumerableHelper.GetSubsets(frequentSet);

                foreach (var t in subSets)
                {
                    var leftSide = new FrequentItemSet<int>(t);
                    for (var j = 0; j < subSets.Count; j++)
                    {
                        var rightSide = new FrequentItemSet<int>(subSets[j]);
                        if (rightSide.ItemSet.Count != 1 || !FrequentItemSet<int>.SetsSeparated(rightSide, leftSide))
                        {
                            continue;
                        }

                        if (frequentItemSets.ContainsKey(leftSide))
                        {
                            var confidence = (double)frequentItemSets[new FrequentItemSet<int>(frequentSet)] / frequentItemSets[leftSide];
                            if (confidence >= executionSettings.MinConf)
                            {
                                var rule = new DecisionRule<int>(leftSide.ItemSet, rightSide.ItemSet, frequentItemSets[new FrequentItemSet<int>(frequentSet)], confidence);
                                decisionRules.Add(rule);
                            }
                        }
                    }
                }
            }

            var result = PrintRules(decisionRules, executionSettings.DataSourcePath, executionSettings.MinSup, executionSettings.MinConf, data.Transactions.Keys.Count, data.Elements);
            Console.WriteLine(result);
        }
        public static Expression <Func <DecisionRuleRequest, bool> > AppendRules(
            this Expression <Func <DecisionRuleRequest, bool> > autoAuthorizationExpression, DecisionRule rule)
        {
            var authorizationRuleBuilderType = typeof(IDecisionEngineRule);

            _autoAuthorizationRuleBuilders = _autoAuthorizationRuleBuilders ?? Assembly
                                             .GetExecutingAssembly()
                                             .GetTypes()
                                             .Where(
                ab =>
                authorizationRuleBuilderType.IsAssignableFrom(ab) &&
                ab.Name != typeof(IDecisionEngineRule).Name)
                                             .Select(t => Activator.CreateInstance(t) as IDecisionEngineRule).ToList();

            return(_autoAuthorizationRuleBuilders
                   .Select(autoAuthorizationRuleBuilder => autoAuthorizationRuleBuilder.Build(rule))
                   .Aggregate(autoAuthorizationExpression,
                              (current, ruleExpression) => ruleExpression == null ? current : current.And(ruleExpression)));
        }