Beispiel #1
0
        public ImmEvaluation Evaluate(IpMaturityMatrix imm, IEvaluationContext?context = null)
        {
            context ??= new Context();

            var  ruleEvaluations      = new List <RuleEvaluation>();
            long totalScore           = 0;
            long maximumPossibleScore = 0;

            foreach (var rule in imm.Rules.Where(r => !r.OptOut))
            {
                var ruleDefinition = this.ruleDefinitionRepository.GetDefinitionFor(rule);
                if (ruleDefinition != null)
                {
                    var calculator = this.ruleCalculatorFactory.Create(ruleDefinition.DataType);

                    var ruleEvaluation = new RuleEvaluation(rule, calculator.Percentage(rule, context), calculator.Score(rule, context));
                    ruleEvaluations.Add(ruleEvaluation);

                    totalScore           += ruleEvaluation.Score;
                    maximumPossibleScore += GetMaximumScoreForRule(ruleDefinition, rule);
                }
            }

            return(new ImmEvaluation(ruleEvaluations, totalScore, maximumPossibleScore));
        }
 private static void Add(RuleEvaluation failure, IList<Result> failures) => 
     failures.Add(new Result
 {
     Message = failure.Message,
     Mitigation = failure.Result<CommandReference>()
                         .IfNotNull()
                         .Then(r => r.ToString())
                         .ElseDefault()
 });
Beispiel #3
0
 private static void Add(RuleEvaluation failure, IList <Result> failures) =>
 failures.Add(new Result
 {
     Message    = failure.Message,
     Mitigation = failure.Result <CommandReference>()
                  .IfNotNull()
                  .Then(r => r.ToString())
                  .ElseDefault()
 });
Beispiel #4
0
        public void start()
        {
            //Reading incoming stream
            var getReqThread = Task.Run(() => readInputStream());

            //Reading Rules from file
            var getRuleThread = Task.Run(() => readRulesFromFile());

            Task.WhenAll(getReqThread, getRuleThread);
            //Pass through Rule Evaluation
            RuleEvaluation eval = new RuleEvaluation();

            eval.Evaluate(getReqThread.Result, getRuleThread.Result);
        }
        public void SetCurrentScope(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var term = new FuzzyTerm("Term", new MembershipFunction());
            var var = new FuzzyVariable("Variable", null, term);

            var value = new FuzzyValue(var, new Dictionary<FuzzyTerm, double> { { term, 0.2 } });
            var scope = new Scope(value);
            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = scope };

            Assert.AreEqual(scope, sut.InputScope);
        }
        /// <summary>
        ///   Gets the message for the specified <see cref="RuleEvaluation" /> .
        /// </summary>
        /// <param name="evaluation"> The <see cref="RuleEvaluation" /> . </param>
        /// <returns> A validation failure message <see cref="string" /> . </returns>
        public string GetMessage(RuleEvaluation evaluation)
        {
            var template = evaluation.MessageTemplate;

            if (!string.IsNullOrEmpty(template))
            {
                return(evaluation.Parameters != null
                           ? MessageGenerator.Detokenize(template, evaluation.Parameters)
                           : template);
            }

            var failure = evaluation as FailedEvaluation;

            return(failure != null
                       ? failure.ErrorCode
                       : string.Empty);
        }
        public async Task <HttpResponseMessage> GitHubImmByRule(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "imm/github/{org}/{project}/rule/{ruleId}")] HttpRequest request,
            string org,
            string project,
            string ruleId)
        {
            var ruleIdAsGuid = Guid.Parse(ruleId);

            (string rulesObjectName, string projectObjectName) = this.GetGitHubBranchOrObjectNames(request);
            (IRuleDefinitionRepository ruleDefinitions, IpMaturityMatrix imm) =
                await this.GetImmRulesFromGitHubAsync(org, project, rulesObjectName, projectObjectName).ConfigureAwait(false);

            var            evaluationEngine = new EvaluationEngine(ruleDefinitions);
            RuleEvaluation result           = evaluationEngine.Evaluate(imm).RuleEvaluations.First(x => x.RuleAssertion.Id == ruleIdAsGuid);

            return(this.CreateUncacheResponse(new ByteArrayContent(Encoding.ASCII.GetBytes(BadgePainter.DrawSVG(WebUtility.HtmlEncode(result.RuleAssertion.Name !), $"{result.Percentage}%", GetColourSchemeForPercentage(result.Percentage), Style.Flat))), "image/svg+xml"));
        }
Beispiel #8
0
        private void ctrl_cbRuleEvaluation_SelectedValueChanged(object sender, EventArgs e)
        {
            RuleEvaluation eval = (RuleEvaluation)Enum.Parse(typeof(RuleEvaluation), ctrl_cbRuleEvaluation.EditValue.ToString());

            if (FormEditState == FormEditStates.Adding || FormEditState == FormEditStates.Editing)
            {
                ctrl_cboTargetEntityType.Visible  = (eval == (RuleEvaluation.TargetOnly | RuleEvaluation.Both));
                ctrl_frEdTargetEntityRule.Visible = (eval == (RuleEvaluation.TargetOnly | RuleEvaluation.Both));

                ctrl_cboSourceEntityType.Visible  = (eval == (RuleEvaluation.SourceOnly | RuleEvaluation.Both));
                ctrl_frEdSourceEntityRule.Visible = (eval == (RuleEvaluation.SourceOnly | RuleEvaluation.Both));
            }
            switch (eval)
            {
            case RuleEvaluation.SourceOnly:
                lo_TargetEntity.Visibility       = LayoutVisibility.Never;
                lo_TargetCriteriaTree.Visibility = LayoutVisibility.Never;
                lo_SourceEntity.Visibility       = LayoutVisibility.Always;
                lo_SourceCriteriaTree.Visibility = LayoutVisibility.Always;
                break;

            case RuleEvaluation.TargetOnly:
                lo_TargetEntity.Visibility       = LayoutVisibility.Always;
                lo_TargetCriteriaTree.Visibility = LayoutVisibility.Always;
                lo_SourceEntity.Visibility       = LayoutVisibility.Never;
                lo_SourceCriteriaTree.Visibility = LayoutVisibility.Never;
                break;

            case RuleEvaluation.Both:
                lo_TargetEntity.Visibility       = LayoutVisibility.Always;
                lo_TargetCriteriaTree.Visibility = LayoutVisibility.Always;
                lo_SourceEntity.Visibility       = LayoutVisibility.Always;
                lo_SourceCriteriaTree.Visibility = LayoutVisibility.Always;
                //dxValidator.GetValidationRule(ctrl_frEdTargetEntityRule);
                break;

            default:
                break;
            }
        }
        public void Visit_Value_Expression_Fails_Variable_Not_Found(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA = new FuzzyTerm("Term A", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA);
            var termB = new FuzzyTerm("Term B", new MembershipFunction());
            var varB = new FuzzyVariable("Variable B", null);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA, 0.2 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) };

            Assert.Throws<UnresolvedVariableException>(() => sut.Visit(new ValueExpression(varB, termB)));
        }
        public void Apply_Many_Implication_With_Value_Override(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA1 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termA2 = new FuzzyTerm("TermA1", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA1, termA2);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.2 }, { termA2, 0.6 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) };

            var impl1 = new FuzzyImplication(new AndExpression(new ValueExpression(varA, termA1), new ValueExpression(varA, termA2)), new ValueExpression(varA, termA1));

            var result = sut.Apply(new Scope(value), new[] { impl1 });

            var val = evaluationStrategy.And(0.2, 0.6);

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(varA, result[0].AssociatedVariable);
            Assert.AreEqual(2, result[0].Values.Count);
            // A = A1 got overridden:
            Assert.AreEqual(val, result[0].Values[termA1]);
            // A = A2 stays the same:
            Assert.AreEqual(0.6, result[0].Values[termA2]);
        }
        public void Apply_Many_Implication_With_Value_Merging(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA1 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termA2 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termB1 = new FuzzyTerm("TermB1", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA1, termA2);
            var varB = new FuzzyVariable("Variable B", null, termB1);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.5 }, { termA2, 0.6 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) };

            var impl1 = new FuzzyImplication(new ValueExpression(varA, termA1), new ValueExpression(varB, termB1));
            var impl2 = new FuzzyImplication(new ValueExpression(varA, termA2), new ValueExpression(varB, termB1));

            var result = sut.Apply(new Scope(value), new[] { impl1, impl2 });

            var mergedValue = merger.Apply(new FuzzyValue[]
            {
                new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB1, 0.5 } }),
                new FuzzyValue(varB, new Dictionary<FuzzyTerm, double> { { termB1, 0.6 } })
            });

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(varA, result[0].AssociatedVariable);
            Assert.AreEqual(2, result[0].Values.Count);
            Assert.AreEqual(0.5, result[0].Values[termA1]);
            Assert.AreEqual(0.6, result[0].Values[termA2]);


            Assert.AreEqual(varB, result[1].AssociatedVariable);
            Assert.AreEqual(1, result[1].Values.Count);
            Assert.AreEqual(mergedValue[0].Values[termB1], result[1].Values[termB1]);
        }
        public void Apply_Many_Implication(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA1 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termA2 = new FuzzyTerm("TermA1", new MembershipFunction());
            var termB1 = new FuzzyTerm("TermB1", new MembershipFunction());
            var termB2 = new FuzzyTerm("TermB2", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA1, termA2);
            var varB = new FuzzyVariable("Variable B", null, termB1, termB2);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA1, 0.4 }, { termA2, 0.6 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger);

            var impl1 = new FuzzyImplication(new ValueExpression(varA, termA1), new ValueExpression(varB, termB1));
            var impl2 = new FuzzyImplication(new ValueExpression(varA, termA2), new ValueExpression(varB, termB2));

            var result = sut.Apply(new Scope(value), new[] {impl1, impl2}).ToList();

            Assert.AreEqual(2, result.Count);
            var valA = result.Find(val => val.AssociatedVariable.Equals(varA));
            Assert.IsNotNull(valA);
            Assert.AreEqual(2, valA.Values.Count);
            Assert.AreEqual(0.4, valA.Values[termA1]);
            Assert.AreEqual(0.6, valA.Values[termA2]);

            var valB = result.Find(val => val.AssociatedVariable.Equals(varB));
            Assert.IsNotNull(valB);
            Assert.AreEqual(2, valB.Values.Count);
            Assert.AreEqual(0.4, valB.Values[termB1]);
            Assert.AreEqual(0.6, valB.Values[termB2]);
        }
        public void Apply_One_Implication(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA = new FuzzyTerm("TermA", new MembershipFunction());
            var termB = new FuzzyTerm("TermB", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA);
            var varB = new FuzzyVariable("Variable B", null, termB);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA, 0.4 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger);

            var result = sut.Apply(new Scope(value), new[] {new FuzzyImplication(new ValueExpression(varA, termA), new ValueExpression(varB, termB))});

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(varA, result[0].AssociatedVariable);
            Assert.AreEqual(1, result[0].Values.Count);
            Assert.AreEqual(0.4, result[0].Values[termA]);
            Assert.AreEqual(varB, result[1].AssociatedVariable);
            Assert.AreEqual(1, result[1].Values.Count);
            Assert.AreEqual(0.4, result[1].Values[termB]);
        }
        public void Visit_Implication_Expression(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var termA = new FuzzyTerm("TermA", new MembershipFunction());
            var termB = new FuzzyTerm("TermB", new MembershipFunction());
            var varA = new FuzzyVariable("Variable A", null, termA);
            var varB = new FuzzyVariable("Variable B", null, termB);

            var value = new FuzzyValue(varA, new Dictionary<FuzzyTerm, double> { { termA, 0.4 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) };

            var result = sut.Visit(new FuzzyImplication(new ValueExpression(varA, termA), new ValueExpression(varB, termB)));

            Assert.AreEqual(0.4, result);
        }
        public void Visit_Or_Expression(
            [Values("Algebraic", "Drastic", "Einstein", "Hamacher", "Lukasiewicz", "MinMax")] string evalStrategyName,
            [Values("Average", "Max", "Min", "Sum")] string mergeStrategy)
        {
            var evaluationStrategy = createEvaluationStrategy(evalStrategyName);
            var merger = createFuzzyValueMerger(mergeStrategy);

            var term1 = new FuzzyTerm("Term1", new MembershipFunction());
            var term2 = new FuzzyTerm("Term2", new MembershipFunction());
            var var = new FuzzyVariable("Variable", null, term1, term2);

            var value = new FuzzyValue(var, new Dictionary<FuzzyTerm, double> { { term1, 0.2 }, { term2, 0.8 } });

            var sut = new RuleEvaluation(evaluationStrategy, merger) { InputScope = new Scope(value) };

            var result = sut.Visit(new OrExpression(new ValueExpression(var, term1), new ValueExpression(var, term2)));

            Assert.AreEqual(evaluationStrategy.Or(value.Values[term1], value.Values[term2]), result);
        }
Beispiel #16
0
        /// <summary>
        /// Validates the entity against rules set for the specific entity against itself.
        /// </summary>
        /// <param name="entityRules">This can be a list of all the rules or an already filtered list, the rules will be checked again.</param>
        /// <param name="theEntity">The entity that is being used to look up rules against its own type and its specific record</param>
        /// <param name="ruleApplicator">The event or process the event is applied to.</param>
        /// <returns>Returns the rule that was matched or null if nothing matched</returns>
        public IJarsRule EvaluateEntityOwnRules(List <IJarsRule> entityRules, IEntityBase theEntity, RuleRunsOn ruleApplicator, RuleEvaluation ruleEvaluation = RuleEvaluation.Both)
        {
            var fullFilterList = new List <IJarsRule>();

            if (ruleEvaluation == RuleEvaluation.Both)
            {
                fullFilterList = entityRules.FindAll(r =>
                                                     r.SourceTypeName == theEntity.GetType().Name&&
                                                     r.TargetTypeName == theEntity.GetType().Name&&
                                                     r.RuleEvaluation == ruleEvaluation &&
                                                     r.RuleRunsOn.Contains(ruleApplicator.ToString()));
            }

            if (ruleEvaluation == RuleEvaluation.SourceOnly)
            {
                fullFilterList = entityRules.FindAll(r =>
                                                     r.SourceTypeName == theEntity.GetType().Name&&
                                                     r.RuleEvaluation == ruleEvaluation &&
                                                     r.RuleRunsOn.Contains(ruleApplicator.ToString()));
            }

            if (ruleEvaluation == RuleEvaluation.TargetOnly)
            {
                fullFilterList = entityRules.FindAll(r =>
                                                     r.TargetTypeName == theEntity.GetType().Name&&
                                                     r.RuleEvaluation == ruleEvaluation &&
                                                     r.RuleRunsOn.Contains(ruleApplicator.ToString()));
            }
            //does the entity type meet the its own conditions?
            //var entityFilterList = fullFilterList.FindAll(rules => rules.TargetEntityId == $"{theEntity.Id}");
            if (!fullFilterList.Any())
            {
                return(null);
            }

            DataTable entityTable = theEntity.ConvertToDataTable();

            foreach (var rule in fullFilterList)
            {
                if (ruleEvaluation == RuleEvaluation.Both)
                {
                    if (!EvaluateRule(entityTable, null, rule))
                    {
                        return(rule);
                    }
                    if (!EvaluateRule(null, entityTable, rule))
                    {
                        return(rule);
                    }
                }
                else
                {
                    if (!EvaluateRule(entityTable, null, rule))
                    {
                        return(rule);
                    }
                }
            }
            return(null);
        }