예제 #1
0
        /// <inheritdoc/>
        public void WithRuleCollection <T> (IRuleCollection <T> ruleCollection, IRuleSelector ruleSelector = null)
        {
            _ruleSelector = ruleSelector;
            var ruleEngine = new RuleEngine <T> (ruleCollection);

            _executeRules = ctx => ruleEngine.ExecuteRules(ctx);
        }
예제 #2
0
        private async Task Refresh(AppInfo info, IDictionary <ConfigType, IDictionary <string, string> > configs, bool recoverFromLocal)
        {
            var builders = configs.Select(c =>
            {
                var builder = new ZkTreeBuilder(info, c.Key, this._setting.ZookeeperBasePath, this._setting.ClientInfo.IgnoreCase);
                IRuleCollection <Rule> rule = null;
                if (this._ruleDictionary.ContainsKey(c.Key))
                {
                    rule = this._ruleDictionary[c.Key];
                }
                foreach (var kv in c.Value)
                {
                    builder.GetOrAddZnodeName(kv.Key);
                    rule?.For(kv.Key);
                    this._fallBackPolicy.Execute(() =>
                    {
                        this.TryNoticeChanged(c.Key, kv.Key, kv.Value);
                    }, new Dictionary <string, object>
                    {
                        { "Action", "TryNoticeChangedOnRefresh" },
                        { "ConfigType", c.Key },
                        { kv.Key, kv.Value }
                    });
                }
                return(builder);
            }).ToArray();

            await this.AddWatcher(builders).ConfigureAwait(false);
        }
예제 #3
0
 /// <summary>
 /// Adds rules from another collection to this collection.
 /// </summary>
 /// <param name="ruleCollection">RuleCollection to get rules from.</param>
 public void AddRules(IRuleCollection <TSubject> ruleCollection)
 {
     foreach (var rule in ruleCollection)
     {
         AddRule(rule, false);
     }
 }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RuleEngine&lt;TSubject&gt;"/> class.
        /// </summary>
        /// <param name="ruleCollection"><see cref="IRuleCollection{TSubject}">Rule Collection</see> containing the list of rules that will be executed.</param>
        /// <param name="ruleProcessor"><see cref="IRuleProcessor">Rule Processor</see> to use to process each rule when executed.</param>
        public RuleEngine(IRuleCollection <TSubject> ruleCollection, IRuleProcessor ruleProcessor)
        {
            Check.IsNotNull(ruleCollection, "ruleCollection is required.");
            Check.IsNotNull(ruleProcessor, "ruleProcessor is required.");

            _ruleCollection = ruleCollection;
            _ruleProcessor  = ruleProcessor;
        }
        /// <inheritdoc/>
        public IEnumerable <IRule> SelectRules <TSubject> (IRuleCollection <TSubject> ruleCollection, IRuleEngineContext context)
        {
            object parameter = Equals(Parameter, typeof(TParameter).GetDefault())
                                   ? context.WorkingMemory.GetContextObject <TParameter> ()
                                   : Parameter;

            return(ruleCollection.Where(r => SelectRule(r, _memberNameFunc(( TParameter )parameter))));
        }
 /// <summary>
 /// Adds a WhenClause to the rule that checks the context object against a rule collection that returns true if a rule violation occurs from rule collection.
 /// </summary>
 /// <typeparam name="TContext">The type of the context.</typeparam>
 /// <typeparam name="TSubject">The type of the subject.</typeparam>
 /// <typeparam name="TContextObject">The type of the context object.</typeparam>
 /// <param name="ruleBuilder">The rule builder.</param>
 /// <param name="ruleCollection">The rule collection.</param>
 /// <param name="ruleSelector">The rule selector.</param>
 /// <returns>
 /// A <see cref="IRuleBuilder{TContext,TSubject}"/>
 /// </returns>
 public static IRuleBuilder <TContext, TSubject> WithCollection <TContext, TSubject, TContextObject> (
     this IContextObjectProviderRuleBuilder <TContext, TSubject, TContextObject> ruleBuilder,
     IRuleCollection <TContextObject> ruleCollection,
     IRuleSelector ruleSelector = null)
     where TContext : RuleEngineContext <TSubject>
 {
     ruleBuilder.Constrain(new RuleCollectionConstraint <TContextObject> (ruleCollection, ruleSelector));
     return(ruleBuilder);
 }
예제 #7
0
 public Engine(IRuleCollection <T> rules, IResultsFormatter resultFormatter = null)
 {
     this.rules     = rules;
     this.formatter = resultFormatter;
     if (formatter == null)
     {
         formatter = new NoopFormatter();
     }
 }
예제 #8
0
 /// <summary>
 /// Adds an <see cref="InlineConstraint{TProperty}"/> to a rule.
 /// </summary>
 /// <typeparam name="TSubject">Type of subject of the rule.</typeparam>
 /// <typeparam name="TContext">Type of <see cref="IRuleEngineContext"/> of the rule.</typeparam>
 /// <typeparam name="TProperty">Type of property of the subject of the rule.</typeparam>
 /// <param name="propertyRuleBuilder"><see cref="IPropertyRuleBuilder{TContext,TSubject,TProperty}"/> currently configuring the rule.</param>
 /// <param name="ruleCollection">Rule Collection for property.</param>
 /// <param name="ruleSelector">Optional Rule Selector for <paramref name="ruleCollection"/></param>
 /// <returns>A <see cref="IPropertyRuleBuilder{TContext,TSubject,TProperty}"/>.</returns>
 public static IPropertyRuleBuilder <TContext, TSubject, TProperty> ConstrainWithCollection <TSubject, TContext, TProperty> (
     this IPropertyRuleBuilder <TContext, TSubject, TProperty> propertyRuleBuilder,
     IRuleCollection <TProperty> ruleCollection,
     IRuleSelector ruleSelector = null)
     where TContext : RuleEngineContext <TSubject>
 {
     propertyRuleBuilder.Constrain(new RuleCollectionConstraint <TProperty> (ruleCollection, ruleSelector));
     return(propertyRuleBuilder);
 }
예제 #9
0
 public ParsedArguments(
     ISolution solution,
     IRuleCollection rules,
     ViolationReportFormat reportFormat,
     [CanBeNull] string reportOutputFile,
     ISolutionInspectorConfiguration configuration)
 {
     Solution         = solution;
     Rules            = rules;
     ReportFormat     = reportFormat;
     ReportOutputFile = reportOutputFile;
     Configuration    = configuration;
 }
예제 #10
0
        private IEnumerable <IRuleViolation> GetRuleViolations(ISolution solution, IRuleCollection rules)
        {
            var ruleViolations         = new List <IRuleViolation>();
            int previousViolationCount = 0;

            s_logger.Info($"Checking for solution rule violations in solution '{solution.FullPath}'...");
            foreach (var solutionRule in rules.SolutionRules)
            {
                ruleViolations.AddRange(solutionRule.Evaluate(solution));
            }
            s_logger.Info(
                $"Finished checking for solution rule violations in solution '{solution.FullPath}': " +
                $"Found {ruleViolations.Count - previousViolationCount} violations.");

            foreach (var project in solution.Projects)
            {
                s_logger.Info($"Checking for project rule violations in project '{project.FullPath}'...");
                previousViolationCount = ruleViolations.Count;
                foreach (var projectRule in rules.ProjectRules)
                {
                    ruleViolations.AddRange(projectRule.Evaluate(project));
                }
                s_logger.Info(
                    $"Finished checking for project rule violations in project '{project.FullPath}': " +
                    $"Found {ruleViolations.Count - previousViolationCount} violations.");
            }

            foreach (var project in solution.Projects)
            {
                s_logger.Info($"Checking for project item rule violations in project '{project.FullPath}'...");
                previousViolationCount = ruleViolations.Count;

                foreach (var projectItem in project.ProjectItems)
                {
                    s_logger.Debug($"Checking for project item rule violations in project item '{projectItem.FullPath}'...");
                    foreach (var projectItemRule in rules.ProjectItemRules)
                    {
                        ruleViolations.AddRange(projectItemRule.Evaluate(projectItem));
                    }
                }

                s_logger.Info(
                    $"Finished checking for project item rule violations in project '{project.FullPath}': " +
                    $"Found {ruleViolations.Count - previousViolationCount} violations.");
            }

            s_logger.Info(
                $"Finished checking for violations in solution '{solution.FullPath}': " +
                $"Found {ruleViolations.Count} total violations.");
            return(ruleViolations);
        }
        /// <summary>
        /// Select a <see cref="IRule"/> from <paramref name="rules"/>
        /// </summary>
        /// <param name="rules">
        /// <see cref="IRule"/> collection to select from
        /// </param>
        /// <returns>
        /// Select <see cref="IRule"/> instance
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="rules"/> is a null reference
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="rules"/> is empty
        /// </exception>
        public override IRule Select(IRuleCollection rules)
        {
            if (rules==null)
                throw new ArgumentNullException("rules");
            if (rules.Count==0)
                throw new ArgumentException("rules is empty");

            // get rules weight
            double totalWeight = GetTotalWeight(rules);
            // get index inside the wieght
            double index = this.Random.NextDouble()*totalWeight;
            // find which rules is targeted
            return GetRuleByWeightIndex(index,rules);
        }
        /// <inheritdoc/>
        public IEnumerable <IRule> SelectRules <TSubject> (IRuleCollection <TSubject> ruleCollection, IRuleEngineContext context)
        {
            IEnumerable <IRule> rulesToRun = _ruleSet;

            if (rulesToRun == null)
            {
                var propertyInfo = ruleCollection.GetType().GetProperty(_ruleSetName);
                if (propertyInfo != null)
                {
                    rulesToRun = propertyInfo.GetValue(ruleCollection, null) as IEnumerable <IRule>;
                }
            }

            return(rulesToRun ?? Enumerable.Empty <IRule> ());
        }
        /// <summary>
        /// Select a <see cref="IRule"/> from <paramref name="rules"/>
        /// </summary>
        /// <param name="rules">
        /// <see cref="IRule"/> collection to select from
        /// </param>
        /// <returns>
        /// Select <see cref="IRule"/> instance
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="rules"/> is a null reference
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="rules"/> is empty
        /// </exception>
        public virtual IRule Select(IRuleCollection rules)
        {
            if (rules==null)
                throw new ArgumentNullException("rules");
            int index = random.Next(0, rules.Count);

            int i =0;
            foreach(IRule r in rules)
            {
                if (i==index)
                    return r;
                ++i;
            }
            throw new Exception();
        }
        /// <summary>
        /// Select a <see cref="IRule"/> from <paramref name="rules"/>
        /// </summary>
        /// <param name="rules">
        /// <see cref="IRule"/> collection to select from
        /// </param>
        /// <returns>
        /// Select <see cref="IRule"/> instance
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="rules"/> is a null reference
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="rules"/> is empty
        /// </exception>
        public IRule Select(IRuleCollection rules)
        {
            if (rules==null)
                throw new ArgumentNullException("rules");
            if (rules.Count==0)
                throw new ArgumentException("rules is empty");

            this.index = Math.Max(0, (this.index++) % rules.Count);
            int i =0;
            foreach(IRule r in rules)
            {
                if (i==index)
                    return r;
                ++i;
            }
            throw new Exception();
        }
예제 #15
0
        /// <summary>
        /// Select a <see cref="IRule"/> from <paramref name="rules"/>
        /// </summary>
        /// <param name="rules">
        /// <see cref="IRule"/> collection to select from
        /// </param>
        /// <returns>
        /// Select <see cref="IRule"/> instance
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="rules"/> is a null reference
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="rules"/> is empty
        /// </exception>
        public override IRule Select(IRuleCollection rules)
        {
            if (rules == null)
            {
                throw new ArgumentNullException("rules");
            }
            if (rules.Count == 0)
            {
                throw new ArgumentException("rules is empty");
            }

            // get rules weight
            double totalWeight = GetTotalWeight(rules);
            // get index inside the wieght
            double index = this.Random.NextDouble() * totalWeight;

            // find which rules is targeted
            return(GetRuleByWeightIndex(index, rules));
        }
예제 #16
0
        /// <summary>
        /// Select a <see cref="IRule"/> from <paramref name="rules"/>
        /// </summary>
        /// <param name="rules">
        /// <see cref="IRule"/> collection to select from
        /// </param>
        /// <returns>
        /// Select <see cref="IRule"/> instance
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="rules"/> is a null reference
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="rules"/> is empty
        /// </exception>
        public virtual IRule Select(IRuleCollection rules)
        {
            if (rules == null)
            {
                throw new ArgumentNullException("rules");
            }
            int index = random.Next(0, rules.Count);

            int i = 0;

            foreach (IRule r in rules)
            {
                if (i == index)
                {
                    return(r);
                }
                ++i;
            }
            throw new Exception();
        }
예제 #17
0
        /// <summary>
        /// Select a <see cref="IRule"/> from <paramref name="rules"/>
        /// </summary>
        /// <param name="rules">
        /// <see cref="IRule"/> collection to select from
        /// </param>
        /// <returns>
        /// Select <see cref="IRule"/> instance
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="rules"/> is a null reference
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="rules"/> is empty
        /// </exception>
        public IRule Select(IRuleCollection rules)
        {
            if (rules == null)
            {
                throw new ArgumentNullException("rules");
            }
            if (rules.Count == 0)
            {
                throw new ArgumentException("rules is empty");
            }

            this.index = Math.Max(0, (this.index++) % rules.Count);
            int i = 0;

            foreach (IRule r in rules)
            {
                if (i == index)
                {
                    return(r);
                }
                ++i;
            }
            throw new Exception();
        }
예제 #18
0
 public RuleCommand(IRuleCollection rules, ILogger <RuleCommand> logger)
 {
     _rules  = rules;
     _logger = logger;
 }
예제 #19
0
 public void SetUp()
 {
     collection = new DefaultRuleCollection();
 }
예제 #20
0
 public FizzBuzzManagement(IRuleCollection collection)
 {
     this.collection = collection;
 }
예제 #21
0
 /// <inheritdoc/>
 public ICollectionPropertyRuleBuilder <TContext, TSubject, TProperty> WithRuleCollection(
     IRuleCollection <TProperty> ruleCollection, IRuleSelector ruleSelector = null)
 {
     _collectionPropertyRule.WithRuleCollection(ruleCollection, ruleSelector);
     return(this);
 }
예제 #22
0
 public PolygonAnalyser(IRuleCollection <IControlPolygonRule> rules)
 {
     _rules = new List <IControlPolygonRule>(rules.Rules);
 }
예제 #23
0
 public InputAnalyser(IRuleCollection <IInputRule> rules)
 {
     _rules = new List <IInputRule>(rules.Rules);
 }
예제 #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RuleEngine&lt;TSubject&gt;"/> class.
 /// </summary>
 /// <param name="ruleCollection"><see cref="IRuleCollection{TSubject}">Rule Collection</see> containing the list of rules that will be executed.</param>
 public RuleEngine(IRuleCollection <TSubject> ruleCollection)
     : this(ruleCollection, new RuleProcessor())
 {
 }
예제 #25
0
 /// <inheritdoc/>
 public IEnumerable <IRule> SelectRules <TSubject> (IRuleCollection <TSubject> ruleCollection, IRuleEngineContext context)
 {
     return(ruleCollection);
 }
예제 #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RuleCollectionConstraint&lt;TProperty&gt;"/> class.
 /// </summary>
 /// <param name="ruleCollection">Rule Collection property must satisfy.</param>
 /// <param name="ruleSelector">Optional Rule selector for selecting rules in <paramref name="ruleCollection"/></param>
 public RuleCollectionConstraint(IRuleCollection <TProperty> ruleCollection, IRuleSelector ruleSelector = null)
     : base(null)
 {
     _ruleCollection = ruleCollection;
     _ruleSelector   = ruleSelector;
 }
예제 #27
0
 /// <summary>
 /// Gets the list of <see cref="T:Pillar.FluentRuleEngine.IRule">Rules</see> to run.
 /// </summary>
 /// <typeparam name="TSubject">Type of subject for <paramref name="ruleCollection">rule collection</paramref></typeparam><param name="ruleCollection">The rule collection.</param><param name="context">The context.</param>
 /// <returns>
 /// List of rules to run.
 /// </returns>
 public IEnumerable <IRule> SelectRules <TSubject> (IRuleCollection <TSubject> ruleCollection, IRuleEngineContext context)
 {
     return(new List <IRule> {
         _skippingRule
     });
 }