Exemple #1
0
        public void TestCompund_ILLegal()
        {
            // cant buy more than 10 prods and cant buy more than 1 of item 2
            cart.AddProduct(store, 1, 7, false);
            cart.AddProduct(store, 2, 2, false);
            PurchaseBasket basket = cart.GetBasket(store);

            store.ActiveStore = false;
            PurchasePolicy        purchaseplcMinItemsAtBasket = new BasketPurchasePolicy(minItems: 12, pre: preConditionsDict[CommonStr.PurchasePreCondition.MinItemsAtBasket]);
            PurchasePolicy        purchaseplcMaxItemAtBasket  = new BasketPurchasePolicy(maxItems: 10, pre: preConditionsDict[CommonStr.PurchasePreCondition.MaxItemsAtBasket]);
            CompundPurchasePolicy compund = new CompundPurchasePolicy(CommonStr.PurchaseMergeTypes.AND, null);

            compund.add(purchaseplcMinItemsAtBasket);
            compund.add(purchaseplcMaxItemAtBasket);
            bool eligiblePurchase = compund.IsEligiblePurchase(basket);

            Assert.AreEqual(false, eligiblePurchase);
        }
        public static PurchasePolicy Parse(string text)
        {
            bool simplePolicy = prefixes.Any(prefix => text.StartsWith(prefix));

            if (simplePolicy)
            {
                if (productPurchasePolicyMinRegex.IsMatch(text)) // (p_min:precondition:productId:minAmount)
                {
                    string[] constructs   = text.Split(':');
                    int      precondition = Convert.ToInt32(constructs[1]);
                    int      productId    = Convert.ToInt32(constructs[2]);
                    int      minAmount    = Convert.ToInt32(constructs[3]);
                    return(new ProductPurchasePolicy(new PurchasePreCondition(precondition), productId, minAmount));
                }
                else if (productPurchasePolicyMaxRegex.IsMatch(text)) // (p_max:precondition:productId:maxAmount)
                {
                    string[] constructs   = text.Split(':');
                    int      precondition = Convert.ToInt32(constructs[1]);
                    int      productId    = Convert.ToInt32(constructs[2]);
                    int      maxAmount    = Convert.ToInt32(constructs[3]);
                    return(new ProductPurchasePolicy(maxAmount, new PurchasePreCondition(precondition), productId));
                }
                else if (basketPurchasePolicyRegex.IsMatch(text)) // (b:precondition)
                {
                    string[] constructs   = text.Split(':');
                    int      precondition = Convert.ToInt32(constructs[1]);
                    return(new BasketPurchasePolicy(new PurchasePreCondition(precondition)));
                }
                else if (basketPurchasePolicyMaxBasketRegex.IsMatch(text)) // (b_maxBasket:precondition:maxBasketPrice)
                {
                    string[] constructs     = text.Split(':');
                    int      precondition   = Convert.ToInt32(constructs[1]);
                    double   maxBasketPrice = Convert.ToDouble(constructs[2]);
                    return(new BasketPurchasePolicy(new PurchasePreCondition(precondition), maxBasketPrice));
                }
                else if (basketPurchasePolicyMinBasketRegex.IsMatch(text)) // (b_minBasket:precondition:minBasketPrice)
                {
                    string[] constructs     = text.Split(':');
                    int      precondition   = Convert.ToInt32(constructs[1]);
                    double   minBasketPrice = Convert.ToDouble(constructs[2]);
                    return(new BasketPurchasePolicy(minBasketPrice, new PurchasePreCondition(precondition)));
                }
                else if (basketPurchasePolicyMaxItemsRegex.IsMatch(text)) // (b_maxItems:precondition:maxItems)
                {
                    string[] constructs   = text.Split(':');
                    int      precondition = Convert.ToInt32(constructs[1]);
                    int      maxItems     = Convert.ToInt32(constructs[2]);
                    return(new BasketPurchasePolicy(new PurchasePreCondition(precondition), maxItems));
                }
                else if (basketPurchasePolicyMinItemsRegex.IsMatch(text)) // (b_minItems:precondition:minItems)
                {
                    string[] constructs   = text.Split(':');
                    int      precondition = Convert.ToInt32(constructs[1]);
                    int      minItems     = Convert.ToInt32(constructs[2]);
                    return(new BasketPurchasePolicy(minItems, new PurchasePreCondition(precondition)));
                }
                else if (systemPurchasePolicyRegex.IsMatch(text)) // (s:preCondition:storeID)
                {
                    string[] constructs   = text.Split(':');
                    int      preCondition = Convert.ToInt32(constructs[1]);
                    int      storeID      = Convert.ToInt32(constructs[2]);
                    return(new SystemPurchasePolicy(new PurchasePreCondition(preCondition), storeID));
                }
                else if (userPurchasePolicyRegex.IsMatch(text)) // (u:precondition)
                {
                    string[] constructs   = text.Split(':');
                    int      precondition = Convert.ToInt32(constructs[1]);
                    //string username = constructs[2];
                    return(new UserPurchasePolicy(new PurchasePreCondition(precondition)));
                }
            }
            else // compound purchase policy
            {
                int      counter    = 0;
                string   curr       = "";
                string[] constructs = Regex.Split(text, "(XOR|OR|AND)");
                if (constructs.Length <= 1)
                {
                    return(new ProductPurchasePolicy(new PurchasePreCondition(-1), -1, -1));
                }
                string opStr = constructs[1];
                int    op    = opStr == "XOR" ? 0 : opStr == "OR" ? 1 : opStr == "AND" ? 2 : -1;
                if (op == -1)
                {
                    return(new ProductPurchasePolicy(new PurchasePreCondition(-3), -3, -3));
                }
                string        restText = text.Substring(opStr.Length + 2, text.Length - 1 - opStr.Length - 2);
                List <string> policies = new List <string>();
                for (int i = 0; i < restText.Length; i++)
                {
                    if (restText[i] == '(')
                    {
                        if (counter == 0) // shift from 0 to 1 -> new element
                        {
                            curr = "";
                        }
                        counter++;
                    }
                    else if (restText[i] == ')')
                    {
                        if (counter == 1) // shift from 1 to 0 -> finish the current element
                        {
                            curr += restText[i];
                            policies.Add(curr);
                            curr = "";
                        }
                        counter--;
                    }
                    else if (restText[i] == ' ' && counter == 0)
                    {
                        if (!policies.Contains(curr) && curr != "")
                        {
                            policies.Add(curr);
                            curr = "";
                        }
                    }
                    if (!(counter == 0 || curr == ")"))
                    {
                        curr += restText[i];
                    }
                    if (counter == 0 && restText[i] != ')' && restText[i] != '(' && restText[i] != ' ')
                    {
                        curr += restText[i];
                    }
                }
                if (curr != "")
                {
                    policies.Add(curr);
                }
                if (counter != 0)
                {
                    return(new ProductPurchasePolicy(new PurchasePreCondition(-1), -1, -1));
                }

                List <PurchasePolicy> children = new List <PurchasePolicy>();

                foreach (string policy in policies)
                {
                    PurchasePolicy purchasePolicy = Parse(policy);
                    if (CheckPurchasePolicy(purchasePolicy) == false) // this indicates an error!
                    {
                        return(new ProductPurchasePolicy(new PurchasePreCondition(-2), -2, -2));
                    }
                    else
                    {
                        children.Add(purchasePolicy);
                    }
                }
                PurchasePolicy compoundPolicy = new CompundPurchasePolicy(op, children);
                return(compoundPolicy);
            }
            return(new ProductPurchasePolicy(new PurchasePreCondition(-4), -4, -4));
        }