Exemplo n.º 1
0
        private void ValidateConditionTree(Solution solution, Condition condition, List <ParserErrorInfo> errors, bool postResolving)
        {
            switch (condition.Type)
            {
            case 0:
            case 1:
                using (List <Condition> .Enumerator enumerator = ((CompoundCondition)condition).Children.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        Condition condition2 = enumerator.Current;
                        this.ValidateConditionTree(solution, condition2, errors, postResolving);
                    }
                    return;
                }
                break;

            case 2:
                break;

            case 3:
            {
                LeafCondition leaf = (LeafCondition)condition;
                this.ValidatePredicate(solution, leaf, errors, postResolving);
                return;
            }

            default:
                throw new InvalidOperationException("Invalid condition type");
            }
            this.ValidateConditionTree(solution, ((NegationCondition)condition).Child, errors, postResolving);
        }
        private bool IsLeftToRightCondition(Condition condition)
        {
            LeafCondition leaf      = (LeafCondition)condition;
            RelOp         operation = leaf.Operation;

            return(operation == RelOp.GreaterOrEqualThan ||
                   operation == RelOp.GreaterThan);
        }
Exemplo n.º 3
0
        public void setUp()
        {
            admin        = new User(0, "admin", "123456", true, true);
            basket_admin = admin.Basket;
            user         = new User(1, null, null, false, false);
            basket_user  = user.Basket;

            store = new Store(-1, "store");

            p1   = new Product(0, "first", "", "", 5000);
            p2   = new Product(1, "second", "", "", 5000);
            p3   = new Product(2, "third", "", "", 5000);
            p4   = new Product(3, "fourth", "", "", 5000);
            pis1 = new ProductInStore(10000000, store, p1);
            pis2 = new ProductInStore(10000000, store, p2);
            pis3 = new ProductInStore(10000000, store, p3);
            pis4 = new ProductInStore(10000000, store, p4);
            store.Products.Add(p1.Id, pis1);
            store.Products.Add(p2.Id, pis2);
            store.Products.Add(p3.Id, pis3);
            store.Products.Add(p4.Id, pis4);
            sys = new TradingSystem(null, null);
            sys.StoreCounter   = 1;
            sys.ProductCounter = 4;
            sys.UserCounter    = 2;
            sys.Stores.Add(store.Id, store);
            sys.Users.Add(admin.Id, admin);
            sys.Users.Add(user.Id, user);

            Dictionary <int, KeyValuePair <ProductInStore, int> > discountProduct = new Dictionary <int, KeyValuePair <ProductInStore, int> >();

            discountProduct.Add(p1.Id, new KeyValuePair <ProductInStore, int>(pis1, 0));
            rd_min = new RevealedDiscount(sys.DiscountPolicyCounter++, 0.2, discountProduct, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication);
            rd_mid = new RevealedDiscount(sys.DiscountPolicyCounter++, 0.25, discountProduct, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication);
            rd_max = new RevealedDiscount(sys.DiscountPolicyCounter++, 0.3, discountProduct, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication);


            rd_mid_without = new RevealedDiscount(sys.DiscountPolicyCounter++, 0.35, discountProduct, new DateTime(2222, 1, 1), DuplicatePolicy.WithoutMultiplication);
            rd_max_without = new RevealedDiscount(sys.DiscountPolicyCounter++, 1, discountProduct, new DateTime(2222, 1, 1), DuplicatePolicy.WithoutMultiplication);

            Dictionary <int, KeyValuePair <ProductInStore, int> > relatedProduct = new Dictionary <int, KeyValuePair <ProductInStore, int> >();

            relatedProduct.Add(p1.Id, new KeyValuePair <ProductInStore, int>(pis1, 2));
            lc1 = new LeafCondition(relatedProduct, 0, 0, null, new DateTime(), DuplicatePolicy.WithMultiplication);

            relatedProduct = new Dictionary <int, KeyValuePair <ProductInStore, int> >();
            relatedProduct.Add(p1.Id, new KeyValuePair <ProductInStore, int>(pis2, 1));
            lc2 = new LeafCondition(relatedProduct, 0, 0, null, new DateTime(), DuplicatePolicy.WithMultiplication);

            Dictionary <int, ProductInStore> ProductPrecentage = new Dictionary <int, ProductInStore>();

            ProductPrecentage.Add(pis1.Product.Id, pis1);
            ProductPrecentage.Add(pis2.Product.Id, pis2);

            lc = new LogicalCondition(0, 0.5, ProductPrecentage, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication, LogicalConnections.and);
            lc.addChild(0, lc1);
            lc.addChild(1, lc2);
        }
        public void sutup()
        {
            p1    = new Product(0, "p1", null, null, 20);
            store = new Store(7, "a");
            pis1  = new ProductInStore(20, store, p1);

            p2   = new Product(1, "p2", null, null, 20);
            pis2 = new ProductInStore(20, store, p2);


            Dictionary <int, KeyValuePair <ProductInStore, int> > productList = new Dictionary <int, KeyValuePair <ProductInStore, int> >();

            productList.Add(p1.Id, new KeyValuePair <ProductInStore, int>(pis1, 1));
            rd = new RevealedDiscount(1, 0.5, productList, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication);


            Dictionary <int, KeyValuePair <ProductInStore, int> > relatedProducts = new Dictionary <int, KeyValuePair <ProductInStore, int> >();

            relatedProducts.Add(p1.Id, new KeyValuePair <ProductInStore, int>(pis1, 1));
            relatedProducts.Add(p2.Id, new KeyValuePair <ProductInStore, int>(pis2, 2));
            Dictionary <int, ProductInStore> discountproducts = new Dictionary <int, ProductInStore>();

            discountproducts.Add(p1.Id, pis1);
            discountproducts.Add(p2.Id, pis2);
            lc = new LeafCondition(relatedProducts, 10, 0.5, discountproducts, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication);
            relatedProducts = new Dictionary <int, KeyValuePair <ProductInStore, int> >();
            relatedProducts.Add(p1.Id, new KeyValuePair <ProductInStore, int>(pis1, 1));
            lc1 = new LeafCondition(relatedProducts, 10, 0.5, discountproducts, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication);

            relatedProducts = new Dictionary <int, KeyValuePair <ProductInStore, int> >();
            relatedProducts.Add(p2.Id, new KeyValuePair <ProductInStore, int>(pis2, 2));
            lc2 = new LeafCondition(relatedProducts, 10, 0.5, discountproducts, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication);

            logcAnd = new LogicalCondition(10, 0.5, discountproducts, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication, LogicalConnections.and);
            logcAnd.addChild(0, lc1);
            logcAnd.addChild(1, lc2);

            logcOr = new LogicalCondition(10, 0.5, discountproducts, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication, LogicalConnections.or);
            logcOr.addChild(0, lc1);
            logcOr.addChild(1, lc2);

            logcXor = new LogicalCondition(10, 0.5, discountproducts, new DateTime(2222, 1, 1), DuplicatePolicy.WithMultiplication, LogicalConnections.xor);
            logcXor.addChild(0, lc1);
            logcXor.addChild(1, lc2);
        }
Exemplo n.º 5
0
        private static string PredicateToBodyQueryString(LeafCondition leafCond)
        {
            if (!AqsParser.PropertyKeywordMap.ContainsKey(leafCond.PropertyName))
            {
                return(null);
            }
            PropertyKeyword?propertyKeyword = new PropertyKeyword?(AqsParser.PropertyKeywordMap[leafCond.PropertyName]);

            if (propertyKeyword == null)
            {
                return(null);
            }
            if (propertyKeyword.Value == PropertyKeyword.All || propertyKeyword.Value == PropertyKeyword.Body)
            {
                return((string)leafCond.Value);
            }
            return(null);
        }
Exemplo n.º 6
0
        private bool TryExtractConstantConditionWithIndex(Relation relation, Condition condition, out LeafCondition result)
        {
            result = null;

            if (condition is AndCondition andCondition)
            {
                if (TryExtractConstantConditionWithIndex(relation, andCondition.Left, out result))
                {
                    return(true);
                }
                else if (TryExtractConstantConditionWithIndex(relation, andCondition.Right, out result))
                {
                    return(true);
                }

                return(false);
            }
            else if (condition is LeafCondition leaf &&
                     leaf.Operation == Compiler.Common.RelOp.Equals)
            {
                foreach (Index index in (relation as TableDefinition).Indexes)
                {
                    if (leaf.Column == (relation as TableDefinition).GetAttributeByName(index.Column))
                    {
                        result = new LeafCondition()
                        {
                            Column    = leaf.Column,
                            Operation = leaf.Operation,
                            Value     = leaf.Value
                        };

                        leaf.AlwaysTrue = true;

                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 7
0
        private QueryFilter PredicateToQueryFilter(LeafCondition leafCond)
        {
            if (!AqsParser.PropertyKeywordMap.ContainsKey(leafCond.PropertyName))
            {
                return(null);
            }
            PropertyKeyword?propertyKeyword = new PropertyKeyword?(AqsParser.PropertyKeywordMap[leafCond.PropertyName]);

            if (propertyKeyword == null)
            {
                return(null);
            }
            if (!this.AllowedKeywords.Contains(propertyKeyword.Value))
            {
                return(null);
            }
            if (leafCond.Operation == 2)
            {
                QueryFilter queryFilter = this.filterBuilderMap[propertyKeyword.Value](1, leafCond.Value);
                if (queryFilter != null)
                {
                    return(new NotFilter(queryFilter));
                }
            }
            else
            {
                if (leafCond.Operation != 10)
                {
                    return(this.filterBuilderMap[propertyKeyword.Value](leafCond.Operation, leafCond.Value));
                }
                QueryFilter queryFilter2 = this.filterBuilderMap[propertyKeyword.Value](9, leafCond.Value);
                if (queryFilter2 != null)
                {
                    return(new NotFilter(queryFilter2));
                }
            }
            return(null);
        }
Exemplo n.º 8
0
        private void ValidatePredicateOperator(Solution solution, LeafCondition leaf, List <ParserErrorInfo> errors)
        {
            if (leaf.Operation == null)
            {
                return;
            }
            TokenInfo tokenInfo = (leaf.OperationTermInfo == null) ? null : new TokenInfo(solution.Tokens[leaf.OperationTermInfo.FirstToken]);

            if (tokenInfo == null)
            {
                int num = -1;
                if (leaf.PropertyTermInfo != null)
                {
                    num = leaf.PropertyTermInfo.FirstToken + leaf.PropertyTermInfo.Length;
                }
                int num2 = -1;
                if (leaf.ValueTermInfo != null)
                {
                    num2 = leaf.ValueTermInfo.FirstToken - 1;
                }
                if (num != -1 && num2 != -1 && num <= num2)
                {
                    tokenInfo = new TokenInfo(solution.Tokens[num2]);
                }
                else if (leaf.ValueTermInfo != null)
                {
                    tokenInfo = new TokenInfo(solution.Tokens[leaf.ValueTermInfo.FirstToken]);
                }
                else if (leaf.PropertyTermInfo != null)
                {
                    tokenInfo = new TokenInfo(solution.Tokens[leaf.PropertyTermInfo.FirstToken]);
                }
            }
            PropertyKeyword?propertyKeyword = null;

            if (leaf.PropertyTermInfo != null && AqsParser.PropertyKeywordMap.ContainsKey(leaf.PropertyName))
            {
                propertyKeyword = new PropertyKeyword?(AqsParser.PropertyKeywordMap[leaf.PropertyName]);
            }
            if (propertyKeyword == PropertyKeyword.Sent || propertyKeyword == PropertyKeyword.Received || propertyKeyword == PropertyKeyword.Importance || propertyKeyword == PropertyKeyword.Size)
            {
                switch (leaf.Operation)
                {
                case 1:
                case 3:
                case 4:
                case 5:
                case 6:
                    break;

                default:
                    errors.Add(new ParserErrorInfo(ParserErrorCode.InvalidOperator, tokenInfo));
                    return;
                }
            }
            else
            {
                switch (leaf.Operation)
                {
                case 1:
                case 7:
                case 11:
                case 12:
                case 13:
                    return;

                case 8:
                case 9:
                    errors.Add(new ParserErrorInfo(ParserErrorCode.SuffixMatchNotSupported, tokenInfo));
                    return;
                }
                errors.Add(new ParserErrorInfo(ParserErrorCode.InvalidOperator, tokenInfo));
            }
        }
Exemplo n.º 9
0
        private void ValidatePredicate(Solution solution, LeafCondition leaf, List <ParserErrorInfo> errors, bool postResolving)
        {
            this.ValidatePredicateOperator(solution, leaf, errors);
            TokenInfo       errorToken = (leaf.PropertyTermInfo == null) ? null : new TokenInfo(solution.Tokens[leaf.PropertyTermInfo.FirstToken]);
            PropertyKeyword propertyKeyword;

            if (postResolving)
            {
                if (!AqsParser.PropertyKeywordMap.TryGetValue(leaf.PropertyName, out propertyKeyword))
                {
                    propertyKeyword = PropertyKeyword.All;
                }
            }
            else if (leaf.PropertyTermInfo != null)
            {
                if (!AqsParser.PropertyKeywordMap.TryGetValue(leaf.PropertyName, out propertyKeyword))
                {
                    errors.Add(new ParserErrorInfo(ParserErrorCode.InvalidPropertyKey, errorToken));
                    return;
                }
                if (!this.AllowedKeywords.Contains(propertyKeyword))
                {
                    errors.Add(new ParserErrorInfo(ParserErrorCode.InvalidPropertyKey, errorToken));
                    return;
                }
            }
            else
            {
                propertyKeyword = PropertyKeyword.All;
                if (leaf.ValueTermInfo == null)
                {
                    errors.Add(new ParserErrorInfo(ParserErrorCode.UnexpectedToken));
                    return;
                }
            }
            if (!postResolving && leaf.ValueTermInfo == null)
            {
                ParserErrorCode parserErrorCode  = ParserErrorCode.MissingPropertyValue;
                PropertyKeyword propertyKeyword2 = propertyKeyword;
                switch (propertyKeyword2)
                {
                case PropertyKeyword.Sent:
                case PropertyKeyword.Received:
                    parserErrorCode = ParserErrorCode.InvalidDateTimeFormat;
                    break;

                default:
                    if (propertyKeyword2 == PropertyKeyword.Kind)
                    {
                        parserErrorCode = ParserErrorCode.InvalidKindFormat;
                    }
                    break;
                }
                if (parserErrorCode != ParserErrorCode.MissingPropertyValue)
                {
                    int num = leaf.PropertyTermInfo.FirstToken + leaf.PropertyTermInfo.Length + 1;
                    if (leaf.OperationTermInfo != null)
                    {
                        num += leaf.PropertyTermInfo.Length;
                    }
                    if (num < solution.Tokens.Count)
                    {
                        errorToken = new TokenInfo(solution.Tokens[num]);
                    }
                }
                errors.Add(new ParserErrorInfo(parserErrorCode, errorToken));
                return;
            }
            if (leaf.ValueTermInfo != null)
            {
                int firstChar = solution.Tokens[leaf.ValueTermInfo.FirstToken].FirstChar;
                errorToken = new TokenInfo(firstChar, leaf.ValueTermInfo.Text.Length);
            }
            if (propertyKeyword == PropertyKeyword.Kind)
            {
                if (!AqsParser.KindKeywordMap.ContainsKey((string)leaf.Value))
                {
                    errors.Add(new ParserErrorInfo(ParserErrorCode.InvalidKindFormat, errorToken));
                    return;
                }
            }
            else
            {
                if (propertyKeyword == PropertyKeyword.Sent || propertyKeyword == PropertyKeyword.Received)
                {
                    if (postResolving || !(leaf.Value is string[]) || ((string[])leaf.Value).Length != 2)
                    {
                        return;
                    }
                    using (Condition condition = solution.Resolve(leaf, 0, (DateTime)ExDateTime.Now))
                    {
                        LeafCondition leafCondition  = (LeafCondition)((CompoundCondition)condition).Children[0];
                        LeafCondition leafCondition2 = (LeafCondition)((CompoundCondition)condition).Children[1];
                        ExDateTime    t  = (ExDateTime)((DateTime)leafCondition.Value);
                        ExDateTime    t2 = (ExDateTime)((DateTime)leafCondition2.Value);
                        if (t > t2)
                        {
                            errors.Add(new ParserErrorInfo(ParserErrorCode.InvalidDateTimeRange, errorToken));
                        }
                        return;
                    }
                }
                if (postResolving && leaf.Value is string)
                {
                    string text = (string)leaf.Value;
                    if (!AqsParser.ContainsAlphanumericChars(text))
                    {
                        errors.Add(new ParserErrorInfo(ParserErrorCode.UnexpectedToken, errorToken));
                    }
                    int num2 = text.IndexOf('*');
                    if (num2 >= 0)
                    {
                        string s = text.Substring(0, num2);
                        if (!AqsParser.ContainsAlphanumericChars(s))
                        {
                            errors.Add(new ParserErrorInfo(ParserErrorCode.SuffixMatchNotSupported, errorToken));
                        }
                    }
                }
            }
        }