public void SimpleRuleLoaderPersonEvaluateFoundInExpression() { Person person1 = new Person() { Name = "Mathias", Age = 36, Children = 2 }; Person person2 = new Person() { Name = "Anna", Age = 35, Children = 2 }; Person person3 = new Person() { Name = "Emil", Age = 4, Children = 0 }; List <Person> persons = new List <Person>(); persons.Add(person1); persons.Add(person2); persons.Add(person3); RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); var ruleFunc = ruleEngine.CompileRule <Person>("Name", Operator.FoundIn, persons); var result = ruleFunc(person1); Rule <Person> rule = new Rule <Person>("Name", Operator.NotFoundIn, persons); var ruleFuncRule = ruleEngine.CompileRule <Person>(rule); var ruleFuncRuleResult = ruleFuncRule(person3); Assert.AreEqual(result, true); Assert.AreEqual(ruleFuncRuleResult, false); }
public void SimpleRuleLoaderPersonValidateRulesCount() { Person person1 = new Person() { Name = "Mathias", Birthdate = new DateTime(1976, 5, 9) }; Person person2 = new Person() { Name = "Anna", Birthdate = new DateTime(2001, 4, 4) }; Rule rule1 = new Rule("Birthdate", Operator.GreaterThanOrEqual, new DateTime(2000, 1, 1)); RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); var ruleFunc = ruleEngine.CompileRule <Person>(rule1); RuleValidator ruleValidator = new RuleValidator(); // calculates the count of persons that match rule1 var resultSingleRule = ruleValidator.ValidateRuleCount(new[] { person1, person2 }, ruleFunc); // one person ("Anna") matches the Birthdate rule Assert.AreEqual(resultSingleRule, 1); Rule rule2 = new Rule("Name", Operator.Equal, "Anna"); var combinedRulesFunc = ruleEngine.CombineRules <Person>(new Rule[] { rule1, rule2 }); // calculates the count of persons that match rule1 and rule2 var resultMultibleRule = ruleValidator.ValidateRulesCount(new[] { person1, person2 }, combinedRulesFunc); // one person ("Anna") matches the Birthdate rule and the Name rule Assert.AreEqual(resultMultibleRule, 1); }
public void SimpleRuleLoaderPersonValidateRulesAny() { //Person person1 = new Person() { // Name = "Mathias", Birthdate = new DateTime(1976,5,9) }; //Person person2 = new Person() { // Name = "Anna", Birthdate = new DateTime(2001,4,4) }; //RuleLoader ruleLoader = new RuleLoader(); //// new Rule("Age", Operator.LessThanOrEqual, 50); //Rule rule1 = ruleLoader.Load(2); //Rule rule2 = ruleLoader.Load(3); //// new Rule("Children", Operator.GreaterThan, 0); ////Rule rule2 = ruleLoader.Load(3); //RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); //var ruleFuncs = ruleEngine.CombineRules<Person>( // new Rule[] { rule1, rule2 }); //RuleValidator ruleValidator = new RuleValidator(); //var result = ruleValidator.ValidateRulesAny( // new[] { person1, person2 }, ruleFuncs); //Assert.AreEqual(result, true); var rule = new Rule("Age", Operator.LessThanOrEqual, 50); Person person = new Person() { Name = "Mathias", Age = 36, Children = 2, Married = true }; RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); var ruleFunc = ruleEngine.CompileRule <Person>(rule); var result = ruleFunc(person); Assert.AreEqual(result, true); }
public void SimpleExpressionEvaluatorRules() { Person person1 = new Person() { Name = "Mathias", Age = 36, Children = 2, Married = true, Birthdate = new DateTime(1976, 5, 9) }; Person person2 = new Person() { Name = "Anna", Age = 32, Children = 2, Married = false, Birthdate = new DateTime(2002, 2, 2) }; RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); RuleValidator ruleValidator = new RuleValidator(); var counter = ruleValidator.ValidateExpressionRulesCount( new Person[] { person1, person2 }, new Rule[] { new Rule("Birthdate < '2001-09-05'") }); Assert.AreEqual(counter, 1); }
public void SimpleRuleLoaderPersonTest() { Person person = new Person() { Name = "Mathias", Age = 36, Children = 2, Married = true }; RuleLoader ruleLoader = new RuleLoader(); // new Rule("Age", Operator.LessThanOrEqual, 50); Rule rule = ruleLoader.Load(2); RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); var ruleFunc = ruleEngine.CompileRule <Person>(rule); var result = ruleFunc(person); Assert.AreEqual(result, true); }
public void SimpleRuleLoaderPersonAddressValidateRulesAll() { Person person = new Person() { Name = "Mathias", Age = 36, Children = 2, Married = true }; Adresse adresseOne = new Adresse() { Street = "Teststreet1", Plz = 3030, City = "New York", ActiveState = true }; Adresse adresseTwo = new Adresse() { Street = "Teststreet2", Plz = 1010, City = "London", ActiveState = false }; Adresse adresseThree = new Adresse() { Street = "Teststreet3", Plz = 2020, City = "Paris", ActiveState = false }; person.Adresses_.Add(adresseOne); person.Adresses_.Add(adresseTwo); person.Adresses_.Add(adresseThree); RuleLoader ruleLoader = new RuleLoader(); // new Rule("City", Operator.Equal, "New York"); Rule firstAdressRule = ruleLoader.Load(4); //return new Rule("City", Operator.Equal, "New York"); // new Rule("ActiveState", Operator.Equal, true); Rule secondAdressRule = ruleLoader.Load(5); //new Rule("ActiveState", Operator.Equal, true); RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); var firstAdressRuleFunc = ruleEngine.CompileRule <Adresse>(firstAdressRule); var secondAdressRuleFunc = ruleEngine.CompileRule <Adresse>(secondAdressRule); RuleValidator ruleValidator = new RuleValidator(); bool result = ruleValidator. ValidateValuesAny(person.Adresses_, firstAdressRuleFunc); Assert.AreEqual(result, true); result = ruleValidator. ValidateValuesAny(person.Adresses_, secondAdressRuleFunc); Assert.AreEqual(result, true); }
public Cart(IRule discountRule, IsolationMode appDomainMode = IsolationMode.Shared, IsolationMode runtimeMode = IsolationMode.Shared, IsolationMode scopeMode = IsolationMode.Shared) { if (discountRule == null) { throw new ArgumentNullException( "discountRule"); } _appDomainMode = appDomainMode; _runtimeMode = runtimeMode; _scopeMode = scopeMode; #if PASS_BY_VALUE if (_appDomainMode == IsolationMode.Private) { discountRule.ExpectedReturnValueNames = new[] { "cart" } } ; #endif #if USE_SAFE_ENGINE _ruleEngine = new RuleEngine.RuleEngine(_appDomainMode); _ruleHandle = _ruleEngine.InsertRule( discountRule, _runtimeMode); #else _ruleEngine = new UnsafeRuleEngine(); _ruleHandle = _ruleEngine.InsertRule( discountRule); #endif _ruleParameters = new Dictionary <string, object> { { "cart", this } }; }
public void SimpleRuleLoaderPersonValidateRulesAll() { Person person1 = new Person() { Name = "Mathias", Age = 36, Children = 2, Married = true }; Person person2 = new Person() { Name = "Anna", Age = 32, Children = 2, Married = false }; RuleLoader ruleLoader = new RuleLoader(); // new Rule("Age", Operator.LessThanOrEqual, 50); Rule rule1 = ruleLoader.Load(2); // new Rule("Married", Operator.Equal, true); Rule rule2 = ruleLoader.Load(8); RuleEngine.RuleEngine ruleEngine = new RuleEngine.RuleEngine(); var ruleFuncs = ruleEngine.CombineRules <Person>( new Rule[] { rule1, rule2 }); RuleValidator ruleValidator = new RuleValidator(); var result = ruleValidator.ValidateRulesAll(new[] { person1, person2 }, ruleFuncs); Assert.AreEqual(result, false); }
public ValidationHelper(IUnitOfWork unitOfWork) { _engine = new RuleEngine.RuleEngine(); _entityService = new RuleService(unitOfWork); }