public void GivenNidaAssessFurther_CorrectRuleSetExecuted() { using (var serviceLocatorFixture = new ServiceLocatorFixture()) { // Setup SetupServiceLocatorFixture(serviceLocatorFixture); IRuleEngineContext ruleEngineContext = null; var ruleEngineMock = new Mock <IRuleEngine <AssessmentInstance> > (); ruleEngineMock.Setup(re => re.ExecuteRules(It.IsAny <IRuleEngineContext> ())).Callback <IRuleEngineContext> (ctx => ruleEngineContext = ctx); var ruleEngineFactoryMock = new Mock <IRuleEngineFactory> (); ruleEngineFactoryMock.Setup(f => f.CreateRuleEngine(It.IsAny <AssessmentInstance> (), It.IsAny <IRuleCollection <AssessmentInstance> > ())) .Returns(ruleEngineMock.Object); var nidaWorkflowRuleCollection = serviceLocatorFixture.StructureMapContainer.GetInstance <NidaWorkflowRuleCollection> (); var nidaWorkflowEngine = new NidaWorkflowEngine(null, ruleEngineFactoryMock.Object, new MessageCollector()); var assessmentInstance = new AssessmentInstance(Guid.NewGuid(), Guid.NewGuid(), NidaAssessFurther.AssessmentCodedConcept.Name); nidaWorkflowEngine.Run(assessmentInstance); Assert.IsNotNull(ruleEngineContext); foreach (var rule in ruleEngineContext.RuleSelector.SelectRules(nidaWorkflowRuleCollection, ruleEngineContext)) { Assert.IsTrue(nidaWorkflowRuleCollection.NidaAssessFurtherRuleSet.Any(r => r.Name == rule.Name)); } } }
private void ExecuteElseThenClause(IRuleEngineContext context) { var skippingContext = context.WorkingMemory.GetContextObject <SkippingContext>(); Check.IsNotNull(skippingContext, "There was no skipping context in the working memeory"); SkippedItemDefinitions.ForEach(skippingContext.UnSkippedItemDefinitions.Add); }
public static RulesetBuilder <TEntity> Create(IRuleEngineContext context) { if (context == null) { throw new ArgumentNullException(nameof(context), "Cannot be null"); } return(new RulesetBuilder <TEntity>(context as RuleEngineContext.RuleEngineContext)); }
/// <summary> /// Initializes a new instance of the <see cref="RuleEngineContext<TSubject>"/> class. /// </summary> /// <param name="subject">Subject for the context.</param> /// <param name="ruleViolationReporter"><see cref="IRuleViolationReporter">RuleViolationReporter</see> of the context.</param> /// <param name="ruleSelector"><see cref="IRuleSelector">RuleSelector</see> of the context.</param> /// <param name="nameProvider"><see cref="INameProvider">NameProvider</see> of the context.</param> /// <param name="parentContext">Optional Parent RuleEngineContext.</param> public RuleEngineContext( TSubject subject, IRuleViolationReporter ruleViolationReporter, IRuleSelector ruleSelector, INameProvider nameProvider, IRuleEngineContext parentContext = null) : base(subject, ruleViolationReporter, ruleSelector, nameProvider, parentContext) { Subject = subject; }
/// <inheritdoc/> public bool ShouldRunRule(IRuleEngineContext ruleEngineContext) { var shouldRun = true; foreach (var shouldRunClause in _shouldRunClauses) { shouldRun &= shouldRunClause(ruleEngineContext); } return(shouldRun); }
/// <inheritdoc/> public override bool IsCompliant( object propertyValue, IRuleEngineContext ruleEngineContext ) { var isCompliant = true; if ( propertyValue != null ) { isCompliant = false; } return isCompliant; }
/// <inheritdoc/> public override bool IsCompliant(object propertyValue, IRuleEngineContext ruleEngineContext) { var isCompliant = true; if (propertyValue == null) { isCompliant = false; } return(isCompliant); }
/// <inheritdoc/> public override bool IsCompliant(object propertyValue, IRuleEngineContext ruleEngineContext) { var propertyRuleEngineContext = new RuleEngineContext <TProperty> ( ( TProperty )propertyValue, ruleEngineContext.RuleViolationReporter, _ruleSelector, ruleEngineContext.NameProvider, ruleEngineContext); propertyRuleEngineContext.WorkingMemory.AddContextObject(ruleEngineContext.Subject); var ruleEngine = new RuleEngine <TProperty> (_ruleCollection); var result = ruleEngine.ExecuteRules(propertyRuleEngineContext); return(!result.HasRuleViolation); }
/// <summary> /// Executes Rules using specified <see cref="IRuleEngineContext"/> /// </summary> /// <param name="ruleEngineContext">Context to use when executing rules.</param> /// <returns>A <see cref="RuleExecutionResult"/> containing the results of the execution pass.</returns> public RuleExecutionResult ExecuteRules(IRuleEngineContext ruleEngineContext) { var rules = ruleEngineContext.RuleSelector.SelectRules(_ruleCollection, ruleEngineContext) .Where(r => !r.IsDisabled) .OrderBy(r => r.Priority) .ToList(); rules.ForEach(rule => _ruleProcessor.Process(ruleEngineContext, rule)); var result = new RuleExecutionResult(ruleEngineContext.RuleViolationReporter); return(result); }
private void ExecuteThenClause(IRuleEngineContext ruleEngineContext) { var propertyValue = PropertyValueDelegate.DynamicInvoke(ruleEngineContext.Subject) as IEnumerable; if (propertyValue != null) { foreach (var childSubject in propertyValue) { var subContext = new RuleEngineContext( childSubject, ruleEngineContext.RuleViolationReporter, _ruleSelector, ruleEngineContext.NameProvider, ruleEngineContext); _executeRules(subContext); } } }
/// <inheritdoc/> public override bool IsCompliant(object propertyValue, IRuleEngineContext ruleEngineContext) { if (propertyValue != null && !(propertyValue is TProperty)) { throw new ArgumentException(string.Format("Property value is not of the correct type ({0}).", typeof(TProperty))); } Message = Message.FormatRuleEngineMessage(new Dictionary <string, string> { { "PropertyValue", propertyValue == null ? string.Empty : propertyValue.ToString() } }); var isCompliant = _predicate(( TProperty )propertyValue); return(isCompliant); }
/// <inheritdoc/> public override bool IsCompliant(object propertyValue, IRuleEngineContext ruleEngineContext) { if (propertyValue != null && !(propertyValue is TProperty)) { throw new ArgumentException(string.Format("Property value is not of the correct type ({0}).", typeof(TProperty))); } if (ruleEngineContext.Subject != null && !(ruleEngineContext.Subject is TSubject)) { throw new ArgumentException(string.Format("Subject in the rule engine context is not of the correct type ({0}).", typeof(TSubject))); } var isCompliant = _predicate((TProperty)propertyValue, (TSubject)(ruleEngineContext.Subject)); return(isCompliant); }
private void ExecuteElseThenClause(IRuleEngineContext context) { if (AutoValidate) { var failedConstraints = context.WorkingMemory.GetContextObject <List <IConstraint> > (Name); foreach (var constraint in failedConstraints) { if (!(constraint is IHandleAddingRuleViolations)) { var propertyName = _getPropertyNameFunc(context); var formatedMessage = constraint.Message.FormatRuleEngineMessage(propertyName); var ruleViolation = new RuleViolation(this, context.Subject, formatedMessage, _actualPropertyName); context.RuleViolationReporter.Report(ruleViolation); } } } }
/// <inheritdoc/> public void Process( IRuleEngineContext ruleEngineContext, IRule rule ) { Check.IsNotNull ( ruleEngineContext, "ruleEngineContext is required." ); Check.IsNotNull ( rule, "rule is required." ); if ( rule.ShouldRunRule ( ruleEngineContext ) ) { var whenClauseResult = rule.WhenClause ( ruleEngineContext ); if ( whenClauseResult ) { rule.ThenClauses.ForEach ( t => t ( ruleEngineContext ) ); } else { rule.ElseThenClauses.ForEach ( t => t ( ruleEngineContext ) ); } } }
/// <inheritdoc/> public override bool IsCompliant( object propertyValue, IRuleEngineContext ruleEngineContext ) { var isCompliant = true; if ( propertyValue is string ) { if ( string.IsNullOrWhiteSpace ( propertyValue.ToString () ) ) { isCompliant = false; } } else if ( propertyValue == null ) { isCompliant = false; } return isCompliant; }
/// <inheritdoc/> public void Process(IRuleEngineContext ruleEngineContext, IRule rule) { Check.IsNotNull(ruleEngineContext, "ruleEngineContext is required."); Check.IsNotNull(rule, "rule is required."); if (rule.ShouldRunRule(ruleEngineContext)) { var whenClauseResult = rule.WhenClause(ruleEngineContext); if (whenClauseResult) { rule.ThenClauses.ForEach(t => t(ruleEngineContext)); } else { rule.ElseThenClauses.ForEach(t => t(ruleEngineContext)); } } }
/// <inheritdoc/> public override bool IsCompliant(object propertyValue, IRuleEngineContext ruleEngineContext) { var isCompliant = true; if (propertyValue is string) { if (string.IsNullOrEmpty(propertyValue.ToString())) { isCompliant = false; } } else if (propertyValue == null) { isCompliant = false; } return(isCompliant); }
/// <inheritdoc/> public override bool IsCompliant(object propertyValue, IRuleEngineContext ruleEngineContext) { if (!(propertyValue is TProperty)) { throw new ArgumentException(string.Format("Property value is not of the correct type ({0}).", typeof(TProperty))); } var contextObject = _contextObjectName == null ? ruleEngineContext.WorkingMemory.GetContextObject <TProperty> () : ruleEngineContext.WorkingMemory.GetContextObject <TProperty> (_contextObjectName); Message = Message.FormatRuleEngineMessage( new Dictionary <string, string> { { "PropertyValue", propertyValue.ToString() }, { "CompareValue", contextObject.ToString() } }); return(_predicate(( TProperty )propertyValue, contextObject)); }
private bool ExecuteWhenClause(IRuleEngineContext context) { var whenClauseResult = true; var propertyValue = PropertyValueDelegate.DynamicInvoke(context.Subject); var failedConstraints = new List <IConstraint> (); context.WorkingMemory.AddContextObject(failedConstraints, Name); foreach (var constraint in Constraints) { var constraintIsCompliant = constraint.IsCompliant(propertyValue, context); if (!constraintIsCompliant) { whenClauseResult = false; failedConstraints.Add(constraint); } } return(whenClauseResult); }
private bool ExecuteWhenClause(IRuleEngineContext context) { var flag = true; var itemInstance = context.WorkingMemory.GetContextObject <ItemInstance> (_itemDefinitionCode); var contextObject = new List <IConstraint> (); if (_validNonResponseLookups != null) { var nonResponseValue = itemInstance.Value == null ? null : itemInstance.Value.ToString(); if (nonResponseValue != null) { flag = _validNonResponseLookups.Contains(nonResponseValue); } } if (_validNonResponseLookups == null || !flag) { //i => var propertyValue = itemInstance.Value == null || string.IsNullOrWhiteSpace(itemInstance.Value.ToString()) ? (TValueType)typeof(TValueType).GetDefault() : itemInstance.Value is TValueType ? itemInstance.Value : (TValueType)Convert.ChangeType(itemInstance.Value, typeof(TValueType)); foreach (var constraint in Constraints) { if (!constraint.IsCompliant(propertyValue, context)) { flag = false; contextObject.Add(constraint); } else { flag = true; } } } context.WorkingMemory.AddContextObject(contextObject, Name); return(flag); }
/// <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 }); }
/// <summary> /// Initializes a new instance of the <see cref="RuleEngineContext<TSubject>"/> class. /// </summary> /// <param name="subject">Subject for the context.</param> /// <param name="ruleViolationReporter"><see cref="IRuleViolationReporter">RuleViolationReporter</see> of the context.</param> /// <param name="nameProvider"><see cref="INameProvider">NameProvider</see> of the context.</param> /// <param name="parentContext">Optional Parent RuleEngineContext.</param> public RuleEngineContext( TSubject subject, IRuleViolationReporter ruleViolationReporter, INameProvider nameProvider, IRuleEngineContext parentContext = null) : this(subject, ruleViolationReporter, null, nameProvider, parentContext) { }
/// <summary> /// Initializes a new instance of the <see cref="RuleEngineContext<TSubject>"/> class. /// </summary> /// <param name="subject">Subject for the context.</param> /// <param name="ruleSelector"><see cref="IRuleSelector">RuleSelector</see> of the context.</param> /// <param name="parentContext">Optional Parent RuleEngineContext.</param> public RuleEngineContext(TSubject subject, IRuleSelector ruleSelector, IRuleEngineContext parentContext = null) : this(subject, new RuleViolationCollection(), ruleSelector, null, parentContext) { }
/// <summary> /// Initializes a new instance of the <see cref="RuleEngineContext<TSubject>"/> class. /// </summary> /// <param name="subject">Subject for the context.</param> /// <param name="nameProvider"><see cref="INameProvider">NameProvider</see> of the context.</param> /// <param name="parentContext">Optional Parent RuleEngineContext.</param> public RuleEngineContext(TSubject subject, INameProvider nameProvider, IRuleEngineContext parentContext = null) : this(subject, new RuleViolationCollection(), null, nameProvider, parentContext) { }
/// <inheritdoc/> public bool ShouldRunRule( IRuleEngineContext ruleEngineContext ) { var shouldRun = true; foreach ( var shouldRunClause in _shouldRunClauses ) { shouldRun &= shouldRunClause ( ruleEngineContext ); } return shouldRun; }
/// <inheritdoc/> public abstract bool IsCompliant( object propertyValue, IRuleEngineContext ruleEngineContext );
/// <summary> /// Initializes a new instance of the <see cref="RuleEngineContext<TSubject>"/> class. /// </summary> /// <param name="subject">Subject for the context.</param> /// <param name="ruleViolationReporter"><see cref="IRuleViolationReporter">RuleViolationReporter</see> of the context.</param> /// <param name="ruleSelector"><see cref="IRuleSelector">RuleSelector</see> of the context.</param> /// <param name="parentContext">Optional Parent RuleEngineContext.</param> public RuleEngineContext( TSubject subject, IRuleViolationReporter ruleViolationReporter, IRuleSelector ruleSelector, IRuleEngineContext parentContext = null) : this(subject, ruleViolationReporter, ruleSelector, null, parentContext) { }
public static GroupRuleSetBuilder <TEntity, TKey> Create <TKey>(IRuleEngineContext context) { return(new GroupRuleSetBuilder <TEntity, TKey>(context as RuleEngineContext.RuleEngineContext)); }
/// <inheritdoc/> public IEnumerable <IRule> SelectRules <TSubject> (IRuleCollection <TSubject> ruleCollection, IRuleEngineContext context) { return(ruleCollection); }
protected AbstractRuleBuilder(IRuleEngineContext context) { _properties = new Dictionary <PropertyRule <TEntity> .PropertyHolder, Func <TEntity, bool> >(); _tempPrpertyHolder = new List <TemporarypropertyHolder>(); _context = context as RuleEngineContext.RuleEngineContext; }
/// <inheritdoc/> public abstract bool IsCompliant(object propertyValue, IRuleEngineContext ruleEngineContext);
private static void TestGrouping(List <UserProfile> lst, IRuleEngineContext context) { Console.WriteLine("##########################################################################################"); foreach (var item in lst) { Console.WriteLine("Employee: " + item.FirstName + " Bonus: " + item.DoubleProp("Bonus") + " Age: " + item.Age + " Salary:" + item.UserProfileDetails.Salary + " Overtime:" + item.Overtime); //item.Age = 40; } Console.WriteLine("##########################################################################################"); Console.WriteLine(); var grp = GroupRuleSetBuilder <UserProfile, UserProfile> .Create(context) //.WithAggregateInfo<double>("AvgBonus", AggregateFunction.Average, "{Bonus}*{UserProfileDetails.Salary}") //.WithAggregateInfo<double>("AvgBonus", AggregateFunction.Average, "{Bonus}*{Age}") //.WithAggregateInfo<double>("AvgBonus", AggregateFunction.Average, "{Bonus}*{UserProfileDetails.Dynamic}") .WithAggregateInfo <double>("AvgBonus", AggregateFunction.Average, "{Bonus}") .WithAggregateInfo <int>("MaxAge", AggregateFunction.Max, "{Age}") //.WithAggregateInfo<int>("MaxAge", AggregateFunction.Max, "int({Age}*{AvgBonus})") .WithAggregateInfo <double>("MinSalary", AggregateFunction.Min, "{UserProfileDetails.Salary}") .WithAggregateInfo <int>("CountOfRecords", AggregateFunction.Count, "") //.WithWhere("{Overtime} >100") .WithPlaceHolder("SalaryCheck", "{AvgBonus} + {MaxSalary}") //.WithGroupingKey("{Age} + 1, {UserProfileDetails.Salary}") .WithGroupingKey("{UserProfileDetails.Dynamic}") .WithRuleSet("Bonus Check") .WithHaving("{MaxAge}<40") .WithRule("{Bonus} > {AvgBonus}") .SetProperty("BonusStatus", "Bonus is greater than Average Bonus") .SetAction(t => Console.WriteLine("Number of Records: " + t.GetPropertyOfType <int>("CountOfRecords"))) .Attach() .WithRule("{Bonus} < {AvgBonus}") .SetProperty("BonusStatus", "Bonus is less than Average Bonus") .SetAction(t => Console.WriteLine("Number of Records: " + t.GetPropertyOfType <int>("CountOfRecords"))) .Attach() .DefaultRule() .SetProperty("BonusStatus", "Bonus is equal to Average Bonus") .SetAction(t => Console.WriteLine("Number of Records: " + t.GetPropertyOfType <int>("CountOfRecords"))) .Attach() .WithRuleSet("Age Check") .WithRule("{Age} < {MaxAge}") .SetProperty("AgeStatus", "Age is less than Max Age") .SetAction(t => Console.WriteLine("Number of Records: " + t.GetPropertyOfType <int>("CountOfRecords"))) .Attach() .DefaultRule() .SetProperty("AgeStatus", "Age is equal to Max Age") .SetAction(t => Console.WriteLine("Number of Records: " + t.GetPropertyOfType <int>("CountOfRecords"))) .Attach() .WithRuleSet("Salary Check") .WithRule("{UserProfileDetails.Salary} > {MinSalary}") .SetProperty("SalaryStatus", "Salary is Greater than min salary") .SetAction(t => Console.WriteLine("Number of Records: " + t.GetPropertyOfType <int>("CountOfRecords"))) .Attach() .WithRule("{UserProfileDetails.Salary} = {MinSalary}") .SetProperty("SalaryStatus", "Salary is equal min Salary") .SetAction(t => Console.WriteLine("Number of Records: " + t.GetPropertyOfType <int>("CountOfRecords"))) .Attach() .Attach() .Compile(); grp.Execute(lst); foreach (var item in lst) { Console.WriteLine("Name:" + item.FirstName); Console.WriteLine("Overtime:" + item.Overtime); Console.WriteLine("--------------------------------------"); Console.WriteLine("Salary Status: " + item.StringProp("SalaryStatus") + "\r\nAge Status: " + item.StringProp("AgeStatus") + "\r\nBonus Status: " + item.StringProp("BonusStatus") + "\r\nMax Age: " + item.IntProp("MaxAge") ); Console.WriteLine(); } }
/// <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)))); }
/// <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> ()); }