Пример #1
0
        /// <summary>
        /// Evaluate The source entity against the target entity type.
        /// This is for rules that are set against an entity type rather than a specific entity.
        /// This does not include self evaluating.
        /// swapping the source and target will do the reverse check.
        /// </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="sourceEntity">The entity that acts as the source for the rule</param>
        /// <param name="targetEntity">The entity that acts as the target for the rule.</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 EvaluateSourceAgainstTargetType(List <IJarsRule> entityRules, IEntityBase sourceEntity, IEntityBase targetEntity, RuleRunsOn ruleApplicator)
        {
            var fullTargetFilterList = entityRules.FindAll(r =>
                                                           r.TargetTypeName == targetEntity.GetType().Name&&
                                                           r.SourceTypeName == sourceEntity.GetType().Name&&
                                                           r.RuleRunsOn.Contains(ruleApplicator.ToString()) &&
                                                           !r.TargetCriteriaString.Contains($"[{nameof(targetEntity.Id)}] = {targetEntity.Id}"));
            //does the source rules meet the target conditions?
            var specificFilterList = fullTargetFilterList.FindAll(rules => string.IsNullOrEmpty(rules.TargetCriteriaString));

            if (!specificFilterList.Any())
            {
                return(null);
            }

            DataTable sourceTable = sourceEntity.ConvertToDataTable();
            DataTable targetTable = sourceEntity.ConvertToDataTable();

            foreach (var rule in specificFilterList)
            {
                if (!EvaluateRule(sourceTable, targetTable, rule))
                {
                    return(rule);
                }
            }
            return(null);
        }
Пример #2
0
        /// <summary>
        /// Evaluate The source entity against the target entity only.
        /// This does not include self evaluating.
        /// swapping the source and target will do the reverse check.
        /// </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="sourceEntity">The entity that acts as the source for the rule</param>
        /// <param name="targetEntity">The entity that acts as the target for the rule.</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 EvaluateSourceAgainstTarget(List <IJarsRule> entityRules, IEntityBase sourceEntity, IEntityBase targetEntity, RuleRunsOn ruleApplicator)
        {
            var fullTargetFilterList = entityRules.FindAll(r =>
                                                           r.SourceTypeName == sourceEntity.GetType().Name&&
                                                           r.TargetTypeName == targetEntity.GetType().Name&&
                                                           r.TargetCriteriaString.Contains($"[{nameof(targetEntity.Id)}] = {targetEntity.Id}") &&
                                                           r.RuleRunsOn.Contains(ruleApplicator.ToString()));

            if (!fullTargetFilterList.Any())
            {
                return(null);
            }

            //get the first matching rule
            DataTable sourceTable = sourceEntity.ConvertToDataTable();
            DataTable targetTable = targetEntity.ConvertToDataTable();

            foreach (var rule in fullTargetFilterList)
            {
                //test the source table
                if (!EvaluateRule(sourceTable, targetTable, rule))
                {
                    return(rule);
                }
            }
            return(null);
        }
Пример #3
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);
        }