/// <inheritdoc/> public void WithRuleCollection <T> (IRuleCollection <T> ruleCollection, IRuleSelector ruleSelector = null) { _ruleSelector = ruleSelector; var ruleEngine = new RuleEngine <T> (ruleCollection); _executeRules = ctx => ruleEngine.ExecuteRules(ctx); }
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); }
/// <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); } }
/// <summary> /// Initializes a new instance of the <see cref="RuleEngine<TSubject>"/> 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); }
public Engine(IRuleCollection <T> rules, IResultsFormatter resultFormatter = null) { this.rules = rules; this.formatter = resultFormatter; if (formatter == null) { formatter = new NoopFormatter(); } }
/// <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); }
public ParsedArguments( ISolution solution, IRuleCollection rules, ViolationReportFormat reportFormat, [CanBeNull] string reportOutputFile, ISolutionInspectorConfiguration configuration) { Solution = solution; Rules = rules; ReportFormat = reportFormat; ReportOutputFile = reportOutputFile; Configuration = configuration; }
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(); }
/// <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)); }
/// <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(); }
public RuleCommand(IRuleCollection rules, ILogger <RuleCommand> logger) { _rules = rules; _logger = logger; }
public void SetUp() { collection = new DefaultRuleCollection(); }
public FizzBuzzManagement(IRuleCollection collection) { this.collection = collection; }
/// <inheritdoc/> public ICollectionPropertyRuleBuilder <TContext, TSubject, TProperty> WithRuleCollection( IRuleCollection <TProperty> ruleCollection, IRuleSelector ruleSelector = null) { _collectionPropertyRule.WithRuleCollection(ruleCollection, ruleSelector); return(this); }
public PolygonAnalyser(IRuleCollection <IControlPolygonRule> rules) { _rules = new List <IControlPolygonRule>(rules.Rules); }
public InputAnalyser(IRuleCollection <IInputRule> rules) { _rules = new List <IInputRule>(rules.Rules); }
/// <summary> /// Initializes a new instance of the <see cref="RuleEngine<TSubject>"/> 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()) { }
/// <inheritdoc/> public IEnumerable <IRule> SelectRules <TSubject> (IRuleCollection <TSubject> ruleCollection, IRuleEngineContext context) { return(ruleCollection); }
/// <summary> /// Initializes a new instance of the <see cref="RuleCollectionConstraint<TProperty>"/> 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; }
/// <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 }); }