Example #1
0
        /// <summary>
        /// Create/Update an And Rule for the rule results
        /// </summary>
        /// <param name="ruleResults"></param>
        /// <param name="existingRule"></param>
        /// <param name="negateResult"></param>
        /// <returns></returns>
        public static Rule AndRule(this List <RuleResult> ruleResults, Rule existingRule = null, bool negateResult = false)
        {
            if (ruleResults.Count < 2)
            {
                throw new ArgumentOutOfRangeException(nameof(ruleResults),
                                                      $"{nameof(ruleResults)} does not meet the requirements for the minimum number of members");
            }

            var refValues = ruleResults.Select(rr => (IEntity)(TypeKey)rr);

            var refValueIds = new RuleCollect
            {
                RuleResultId = existingRule?.ReferenceValues.RuleResultId ?? GuidHelpers.NewTimeUuid(),
                EntityIds    = ImmutableList.CreateRange(refValues)
            };

            var lastChanged = ruleResults.OrderByDescending(rr => rr.LastChanged).First().LastChanged;

            if (existingRule == null)
            {
                existingRule = new Rule
                {
                    RuleId = GuidHelpers.NewTimeUuid()
                };
            }

            existingRule.RuleName        = $"Test all rule results are {!negateResult}";
            existingRule.RuleType        = RuleType.And;
            existingRule.LastChanged     = lastChanged;
            existingRule.LastExecuted    = lastChanged;
            existingRule.NegateResult    = negateResult;
            existingRule.ReferenceValues = refValueIds;

            return(existingRule);
        }
Example #2
0
        /// <summary>
        /// Create a RulePrescription (implicitly defining a RuleResult) from an ITypeKey.
        /// This can be used as the Entities value within an IRuleProcessing derived object
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="existingRule"></param>
        /// <returns></returns>
        public static T RulePrescription <T>(this IEntity entity, Rule existingRule = null) where T : IRulePrescription, new()
        {
            var refValue = new T {
                RuleResultId = existingRule?.ReferenceValues.RuleResultId ?? GuidHelpers.NewTimeUuid(), EntityIds = ImmutableList.Create(entity)
            };

            return(refValue);
        }
Example #3
0
        /// <summary>
        /// Create an OperandKey from an enumerable of Entity Ids
        /// </summary>
        /// <param name="sourceValueIds"></param>
        /// <param name="entType"></param>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public static OperandKey OperandKey(this IEnumerable <Guid> sourceValueIds, EntityType entType, Guid?entityId = null)
        {
            if (!entityId.HasValue || entityId == Guid.Empty)
            {
                entityId = GuidHelpers.NewTimeUuid();
            }

            var opKey = new OperandKey {
                EntType = entType, EntityId = entityId.Value, SourceEntityIds = ImmutableArray.CreateRange(sourceValueIds?.ToArray() ?? new Guid[0])
            };

            return(opKey);
        }
Example #4
0
        /// <summary>
        /// Create an OperandKey from a single Value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="entType"></param>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public static OperandKey OperandKey(this Value value, EntityType entType, Guid?entityId = null)
        {
            if (!entityId.HasValue || entityId == Guid.Empty)
            {
                entityId = GuidHelpers.NewTimeUuid();
            }

            var opKey = new OperandKey {
                EntType = entType, EntityId = entityId.Value, SourceEntityIds = ImmutableArray.Create(value.EntityId)
            };

            return(opKey);
        }
Example #5
0
        /// <summary>
        /// Create a RulePrescription (implicitly defining a RuleResult) from a Processable Entity
        /// This can be used as the Entities value within an IRuleProcessing derived object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TU"></typeparam>
        /// <param name="entity"></param>
        /// <param name="existingRule"></param>
        /// <returns></returns>
        public static TU RulePrescription <T, TU>(this T entity, Rule existingRule = null) where T : IEntity where TU : IRulePrescription, new()
        {
            if (!entity.IsProcessable())
            {
                throw new ArgumentOutOfRangeException(nameof(entity), "RulePrescription helper creator is only for Processable entity types");
            }

            var entTypeKey = new TypeKey {
                EntityId = entity.EntityId, EntType = entity.EntType, EntTags = entity.EntTags, LastChanged = entity.LastChanged
            };
            var refValue = new TU {
                RuleResultId = existingRule?.ReferenceValues.RuleResultId ?? GuidHelpers.NewTimeUuid(), EntityIds = ImmutableList.Create((IEntity)entTypeKey)
            };

            return(refValue);
        }
Example #6
0
        /// <summary>
        /// Create a HasMeaningfulValue Rule for the value
        /// </summary>
        /// <param name="value"></param>
        /// <param name="existingRule"></param>
        /// <param name="negateResult"></param>
        /// <returns></returns>
        public static Rule HasMeaningfulValueRule(this Value value, Rule existingRule = null, bool negateResult = false)
        {
            if (existingRule == null)
            {
                existingRule = new Rule
                {
                    RuleId = GuidHelpers.NewTimeUuid()
                };
            }

            existingRule.RuleName        = $"Test for meaningful value of Value {(TypeKey)value}";
            existingRule.RuleType        = RuleType.HasMeaningfulValue;
            existingRule.LastChanged     = value.LastChanged;
            existingRule.LastExecuted    = value.LastChanged;
            existingRule.NegateResult    = negateResult;
            existingRule.ReferenceValues = value.RulePrescription <RuleUnary>(existingRule);

            return(existingRule);
        }
Example #7
0
        /// <summary>
        /// Create an Exists Rule for the value
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="existingRule"></param>
        /// <param name="negateResult"></param>
        /// <returns></returns>
        public static Rule ExistsRule(this IEntity entity, Rule existingRule = null, bool negateResult = true)
        {
            var nText     = negateResult ? "non-" : "";
            var ruleName  = $"Test for {nText}existence of {entity.EntType.ToString()} {(TypeKey)entity}";
            var refValues = (IRulePrescription)entity.RulePrescription <RuleUnary>();

            if (existingRule == null)
            {
                existingRule = new Rule
                {
                    RuleId = GuidHelpers.NewTimeUuid()
                };
            }

            existingRule.RuleName        = ruleName;
            existingRule.RuleType        = RuleType.Exists;
            existingRule.LastChanged     = entity.LastChanged;
            existingRule.LastExecuted    = entity.LastChanged;
            existingRule.NegateResult    = negateResult;
            existingRule.ReferenceValues = refValues;

            return(existingRule);
        }