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));
        }
Beispiel #2
0
        public bool Match(CartRuleContext context, RuleExpression expression)
        {
            var list = expression.Value as List <T>;

            if (list == null || list.Count == 0)
            {
                return(true);
            }

            var value = GetValue(context);

            if (object.Equals(value, default(T)))
            {
                return(false);
            }
            if (expression.Operator == RuleOperator.In)
            {
                return(list.Contains(value));
            }
            if (expression.Operator == RuleOperator.NotIn)
            {
                return(!list.Contains(value));
            }

            throw new InvalidRuleOperatorException(expression);
        }
Beispiel #3
0
        public bool Match(CartRuleContext context, RuleExpression expression)
        {
            bool match = false;

            foreach (var expr in _group.Expressions.Cast <RuleExpression>())
            {
                var descriptor = expr.Descriptor as CartRuleDescriptor;
                if (descriptor == null)
                {
                    continue;
                }

                var processor = _cartRuleService.GetProcessor(expr);

                match = processor.Match(context, expr);

                if (!match && _group.LogicalOperator == LogicalRuleOperator.And)
                {
                    break;
                }

                if (match && _group.LogicalOperator == LogicalRuleOperator.Or)
                {
                    break;
                }
            }

            return(match);
        }
        public virtual bool Match(CartRuleContext context, RuleExpression expression)
        {
            var list = expression.Value as List <T>;

            if (!(list?.Any() ?? false))
            {
                return(true);
            }

            var values = GetValues(context);

            if (values == null)
            {
                var value = GetValue(context);

                if (object.Equals(value, default(T)))
                {
                    return(false);
                }

                if (expression.Operator == RuleOperator.In)
                {
                    return(list.Contains(value, _comparer));
                }
                if (expression.Operator == RuleOperator.NotIn)
                {
                    return(!list.Contains(value, _comparer));
                }
            }
            else
            {
                if (expression.Operator == RuleOperator.IsEqualTo)
                {
                    return(!list.Except(values).Any());
                }
                else if (expression.Operator == RuleOperator.IsNotEqualTo)
                {
                    return(list.Except(values).Any());
                }
                else if (expression.Operator == RuleOperator.Contains)
                {
                    // FALSE for list { 0,1,2,3 } and values { 3,2,1 }
                    return(list.All(x => values.Contains(x)));
                }
                else if (expression.Operator == RuleOperator.NotContains)
                {
                    return(list.All(x => !values.Contains(x)));
                }
                else if (expression.Operator == RuleOperator.In)
                {
                    return(values.Any(x => list.Contains(x)));
                }
                else if (expression.Operator == RuleOperator.NotIn)
                {
                    return(values.Any(x => !list.Contains(x)));
                }
                else if (expression.Operator == RuleOperator.AllIn)
                {
                    // TRUE for list { 0,1,2,3 } and values { 3,2,1 }
                    return(values.All(x => list.Contains(x)));
                }
                else if (expression.Operator == RuleOperator.NotAllIn)
                {
                    return(values.All(x => !list.Contains(x)));
                }
            }

            throw new InvalidRuleOperatorException(expression);
        }
 protected virtual IEnumerable <T> GetValues(CartRuleContext context) => default;
 protected virtual T GetValue(CartRuleContext context) => default;
Beispiel #7
0
 protected abstract T GetValue(CartRuleContext context);
Beispiel #8
0
 protected virtual T GetValue(CartRuleContext context)
 {
     return(default);