public async Task <bool> RuleMatchesAsync(RuleExpression[] expressions, LogicalRuleOperator logicalOperator) { Guard.NotNull(expressions, nameof(expressions)); if (expressions.Length == 0) { return(true); } RuleExpressionGroup group; if (expressions.Length == 1 && expressions[0] is RuleExpressionGroup group2) { group = group2; } else { group = new RuleExpressionGroup { LogicalOperator = logicalOperator }; group.AddExpressions(expressions); } var context = new CartRuleContext(() => group.GetHashCode()) { Customer = _workContext.CurrentCustomer, Store = _storeContext.CurrentStore, WorkContext = _workContext }; var processor = GetProcessor(group); var result = await processor.MatchAsync(context, group); return(result); }
public bool RuleMatches(RuleExpression[] expressions, LogicalRuleOperator logicalOperator) { Guard.NotNull(expressions, nameof(expressions)); if (expressions.Length == 0) { return(true); } RuleExpressionGroup group; if (expressions.Length == 1 && expressions[0] is RuleExpressionGroup group2) { group = group2; } else { group = new RuleExpressionGroup { LogicalOperator = logicalOperator }; group.AddExpressions(expressions); } var context = new CartRuleContext { Customer = _services.WorkContext.CurrentCustomer, Store = _services.StoreContext.CurrentStore, WorkContext = _services.WorkContext }; var processor = GetProcessor(group); return(processor.Match(context, group)); }
public void GetSentence_Column_DataTypeLong() { RuleExpressionGroup gOuter = TestData_RuleExpressionGroups.RuleExpresssion_LogicalName_EndsWith_TypeOrCode_And_Max_lengh_is_gt_5(); string sentence = gOuter.GetSentence(); Assert.Equal("(([LogicalName][Ends with][code] OR [LogicalName][Ends with][type]) AND ([MaxLength][Is greater than][5]))", sentence); }
public void RunRuleAgainstObject_Column_EndsWithEmailVariants() { RuleExpressionGroup g = TestData_RuleExpressionGroups.LogicalNameEndsWithEmailVariants(); DataColumn c = new DataColumn(); c.ExtendedProperties["LogicalName"] = "person email"; Assert.True(g.EvaluateAgainstObject(c)); }
public void RunRuleAgainstObject_Column_DataTypeLong() { RuleExpressionGroup gOuter = TestData_RuleExpressionGroups.RuleExpresssion_LogicalName_EndsWith_TypeOrCode_And_Max_lengh_is_gt_5(); DataColumn c = new DataColumn(); c.ExtendedProperties["LogicalName"] = "Hearing Code"; c.MaxLength = 6; Assert.True(gOuter.EvaluateAgainstObject(c)); }
internal static RuleExpressionGroup LogicalNameEndsWithEmailVariants() { RuleExpressionGroup g = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); g.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "email address")); g.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "e-mail address")); g.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "e mail address")); g.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "email")); g.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "e-mail")); return(g); }
internal static Rule Rule079_A_field_that_contains_TSO() { Rule rule = new Rule("A field that contains a TSO ID should be a character datatype with a length of 5.", 79, "System.Data.DataColumn"); rule.Antecedent.RuleExpressions.Add(TestData_RuleExpressions.PhysicalName_EndsWith_Term("tsoid")); RuleExpressionGroup g = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); g.RuleExpressions.Add(new RuleExpression("Datatype", ComparisonConditionEnum.Contains, "char")); g.RuleExpressions.Add(new RuleExpression("DatatypeLength", ComparisonConditionEnum.EqualTo, 5)); rule.Consequent.RuleExpressionGroups.Add(g); return(rule); }
internal static Rule Rule057_email() { Rule r = new Rule("Email addresses ([email protected]) should end with email address.", 3, "System.Data.DataColumn"); r.Antecedent.RuleExpressionGroups.Add(TestData_RuleExpressionGroups.LogicalNameEndsWithEmailVariants()); RuleExpressionGroup gCon = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); gCon.RuleExpressions.Add(new RuleExpression(DataStructureProperty.DatatypeLength, ComparisonConditionEnum.GreaterThan, 50)); gCon.RuleExpressions.Add(new RuleExpression(DataStructureProperty.LogicalName, ComparisonConditionEnum.EndsWith, "Address")); r.Consequent.RuleExpressionGroups.Add(gCon); return(r); }
public static Rule Rule089_Character_dates() { Rule r = new Rule("All character dates should be of a textual datatype with a data length of 8.", 89, "System.Data.DataColumn"); r.Antecedent.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "Character Date")); RuleExpressionGroup g = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); g.RuleExpressions.Add(new RuleExpression("DatatypeLength", ComparisonConditionEnum.EqualTo, 8)); g.RuleExpressions.Add(new RuleExpression("Datatype", ComparisonConditionEnum.Contains, "char")); r.Consequent.RuleExpressionGroups.Add(g); return(r); }
internal static RuleExpressionGroup RuleExpresssion_LogicalName_EndsWith_TypeOrCode_And_Max_lengh_is_gt_5() { RuleExpressionGroup gInner1 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); gInner1.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "code")); gInner1.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "type")); RuleExpressionGroup gInner2 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); gInner2.RuleExpressions.Add(new RuleExpression("MaxLength", ComparisonConditionEnum.GreaterThan, 5)); RuleExpressionGroup gOuter = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); gOuter.RuleExpressionGroups.Add(gInner1); gOuter.RuleExpressionGroups.Add(gInner2); return(gOuter); }
public static Rule Rule019() { Rule r = new Rule("An Employer Identifcation Number / EIN is 9 characters long.", 19, "System.Data.DataColumn"); RuleExpressionGroup g1 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); g1.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "Employer Identification Number")); g1.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "employer id number")); g1.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "employer identity number")); r.Antecedent.RuleExpressionGroups.Add(g1); RuleExpressionGroup g2 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); g2.RuleExpressions.Add(new RuleExpression("DatatypeName", ComparisonConditionEnum.Contains, "char")); g2.RuleExpressions.Add(new RuleExpression("DatatypeLength", ComparisonConditionEnum.EqualTo, 9)); r.Consequent.RuleExpressionGroups.Add(g2); return(r); }
internal static Rule Rule091_long_codes() { Rule r = new Rule("Codes with unusually long datatype lengths.", 91, "System.Data.DataColumn"); RuleExpressionGroup gInner = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); gInner.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "code")); gInner.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.EndsWith, "type")); RuleExpressionGroup gOuter = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); gOuter.RuleExpressionGroups.Add(gInner); gOuter.RuleExpressions.Add(new RuleExpression("DatatypeLength", ComparisonConditionEnum.GreaterThan, 5)); r.Antecedent.RuleExpressionGroups.Add(gOuter); r.Consequent.RuleExpressions.Add(new RuleExpression("DatatypeLength", ComparisonConditionEnum.EqualTo, "a questionable length")); return(r); }
public override IRuleExpressionGroup VisitRuleSet(RuleSetEntity ruleSet) { var group = new RuleExpressionGroup { Id = ruleSet.Id, LogicalOperator = ruleSet.LogicalOperator, IsSubGroup = ruleSet.IsSubGroup, Value = ruleSet.Id, RawValue = ruleSet.Id.ToString(), Provider = this, Descriptor = new CartRuleDescriptor { RuleType = RuleType.Boolean, ProcessorType = typeof(CompositeRule) } }; return(group); }
internal static Rule Rule070_from_irs() { Rule r = new Rule("Data that comes from the Internal Revenue Service (IRS) should begin with IRS. Rule request by Brian P 9/20/2011", 70, "System.Data.DataColumn"); RuleExpressionGroup gAnt = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); gAnt.RuleExpressions.Add(new RuleExpression("Definition", ComparisonConditionEnum.Contains, "from irs")); gAnt.RuleExpressions.Add(new RuleExpression("Definition", ComparisonConditionEnum.Contains, "from internal revenue service")); gAnt.RuleExpressions.Add(new RuleExpression("Definition", ComparisonConditionEnum.Contains, "from the irs")); gAnt.RuleExpressions.Add(new RuleExpression("Definition", ComparisonConditionEnum.Contains, "from the internal revenue service")); r.Antecedent.RuleExpressionGroups.Add(gAnt); RuleExpressionGroup gCon = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); gCon.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.StartsWith, "IRS")); gCon.RuleExpressions.Add(new RuleExpression("LogicalName", ComparisonConditionEnum.StartsWith, "Internal Revenue Service")); r.Consequent.RuleExpressionGroups.Add(gCon); return(r); }
internal static Rule Rule001() { Rule r = new Rule("Datatype of TIMESTAMP or related is expected", 1, "System.Data.DataColumn"); RuleExpressionGroup gAnt = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); gAnt.RuleExpressions.Add(new RuleExpression(DataStructureProperty.LogicalName, ComparisonConditionEnum.EndsWith, "time stamp")); gAnt.RuleExpressions.Add(new RuleExpression(DataStructureProperty.LogicalName, ComparisonConditionEnum.EndsWith, "timestamp")); r.Antecedent.RuleExpressionGroups.Add(gAnt); RuleExpressionGroup gCon = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); gCon.RuleExpressions.Add(new RuleExpression(DataStructureProperty.Datatype, ComparisonConditionEnum.Contains, "timestamp")); gCon.RuleExpressions.Add(new RuleExpression(DataStructureProperty.Datatype, ComparisonConditionEnum.EqualTo, "datetime2")); gCon.RuleExpressions.Add(new RuleExpression(DataStructureProperty.Datatype, ComparisonConditionEnum.EqualTo, "datetime")); gCon.RuleExpressions.Add(new RuleExpression(DataStructureProperty.Datatype, ComparisonConditionEnum.EqualTo, "date")); r.Consequent.RuleExpressionGroups.Add(gCon); r.OutcomeLevel = OutcomeLevelEnum.Expected; return(r); }
internal static Rule Rule038_AddresslineLength() { Rule r = new Rule("Address line", 38, "System.Data.DataColumn"); RuleExpressionGroup g1 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); g1.RuleExpressions.Add(new RuleExpression("PhysicalName", ComparisonConditionEnum.EndsWith, "addrln")); g1.RuleExpressions.Add(new RuleExpression("PhysicalName", ComparisonConditionEnum.EndsWith, "addrln_1")); g1.RuleExpressions.Add(new RuleExpression("PhysicalName", ComparisonConditionEnum.EndsWith, "addrln_2")); g1.RuleExpressions.Add(new RuleExpression("PhysicalName", ComparisonConditionEnum.EndsWith, "addrln_3")); g1.RuleExpressions.Add(new RuleExpression("PhysicalName", ComparisonConditionEnum.EndsWith, "addrln_4")); g1.RuleExpressions.Add(new RuleExpression("PhysicalName", ComparisonConditionEnum.EndsWith, "addrln_5")); g1.RuleExpressions.Add(new RuleExpression("PhysicalName", ComparisonConditionEnum.EndsWith, "addrln_6")); r.Antecedent.RuleExpressionGroups.Add(g1); RuleExpressionGroup g2 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); g2.RuleExpressions.Add(new RuleExpression("DatatypeName", ComparisonConditionEnum.Contains, "char")); g2.RuleExpressions.Add(new RuleExpression("DatatypeLength", "=", 22)); r.Consequent.RuleExpressionGroups.Add(g2); return(r); }
public CompositeRule(RuleExpressionGroup group, CartRuleProvider cartRuleProvider) { _group = group; _cartRuleProvider = cartRuleProvider; }
public CompositeRule(RuleExpressionGroup group, CartRuleProvider cartRuleService) { _group = group; _cartRuleService = cartRuleService; }
public void ComplexNestedRuleExpressionGroupTest() { string pineappleString = "pineapple"; // starting with this complex condition // ((((eq1 OR eq2) OR (eq3 AND eq4 AND eq5)) AND eq6 AND eq7 AND (eq8 OR eq9)) OR eq10) // (eq1 OR eq2) // ==> g1 RuleExpressionGroup g1 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); RuleExpression eq1 = new RuleExpression("Length", "=", 5); RuleExpression eq2 = new RuleExpression("", "Contains", "apple"); g1.RuleExpressions.Add(eq1); g1.RuleExpressions.Add(eq2); bool g1Result = g1.EvaluateAgainstObject(pineappleString); Assert.True(g1Result); // 'pineapple' isn't 5 characters long, but it does contain 'apple'. Since this is an OR grouping, only one needs to be true // (eq3 AND eq4 AND eq5) // ==> g2 RuleExpressionGroup g2 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); RuleExpression eq3 = new RuleExpression("", "StartsWith", "pine"); RuleExpression eq4 = new RuleExpression("", "BeginsWith", "p"); RuleExpression eq5 = new RuleExpression("", "EndsWith", "ple"); g2.RuleExpressions.Add(eq3); g2.RuleExpressions.Add(eq4); g2.RuleExpressions.Add(eq5); bool g2Result = g2.EvaluateAgainstObject(pineappleString); Assert.True(g2Result); // 'pineapple' starts w/ 'pine', 'p' and ends w/ 'ple' // ((eq1 OR eq2) OR (eq3 AND eq4 AND eq5)) // (g1 OR g2) // ==> g3 RuleExpressionGroup g3 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); g3.RuleExpressionGroups.Add(g1); g3.RuleExpressionGroups.Add(g2); g3.EvaluateAgainstObject(pineappleString); bool g3Result = g3.EvaluateAgainstObject(pineappleString); Assert.True(g3Result); //(eq8 OR eq9) // ==> g4 RuleExpressionGroup g4 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); RuleExpression eq8 = new RuleExpression("Length", "=", 10); RuleExpression eq9 = new RuleExpression("Length", "is", 8); g4.RuleExpressions.Add(eq8); g4.RuleExpressions.Add(eq9); bool g4Result = g4.EvaluateAgainstObject(pineappleString); Assert.False(g4Result); //string.Length of 'pineapple' isn't 10 or 8 // (((eq1 OR eq2) OR (eq3 AND eq4 AND eq5)) AND eq6 AND eq7 AND (eq8 OR eq9)) // g3 AND eq6 AND eq7 and g4 // ==> g5 RuleExpressionGroup g5 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.And); RuleExpression eq6 = new RuleExpression("Length", "=", 9); RuleExpression eq7 = new RuleExpression("", "is", "pineapple"); g5.RuleExpressionGroups.Add(g3); //true g5.RuleExpressions.Add(eq6); //true g5.RuleExpressions.Add(eq7); //true g5.RuleExpressionGroups.Add(g4); //false bool g5Result = g5.EvaluateAgainstObject(pineappleString); Assert.False(g5Result); // ((((eq1 OR eq2) OR (eq3 AND eq4 AND eq5)) AND eq6 AND eq7 AND (eq8 OR eq9)) OR eq10) // g5 OR eq10 // ==> g6 RuleExpressionGroup g6 = new RuleExpressionGroup(RuleExpressionGroup.LogicOperatorEnum.Or); bool caseSensitive = false; RuleExpression eq10 = new RuleExpression("", "is", "PiNeAppLe", caseSensitive); g6.RuleExpressionGroups.Add(g5); // false g6.RuleExpressions.Add(eq10); // true bool g6Result = g6.EvaluateAgainstObject(pineappleString); Assert.True(g6Result); }