Example #1
0
        public static Rule LoadXml(XElement X)
        {
            List <Expression> t;
            var IfNode = X.Descendants("If").First();

            if (IfNode.Elements() != null && IfNode.Elements().Count() > 0)
            {
                t = (from x in X.Descendants("If").First().Elements()
                     select Expression.LoadXml(x)).ToList();
            }
            else
            {
                t = new List <Expression>();
            }
            // var t = (t1 == null || t1.Count() == 0) ? new List<Expression>() : t1.ToList();
            if (IfNode.Attribute("Text") != null)
            {
                t.Insert(0, Expression.ParseString(IfNode.Attribute("Text").Value));
            }
            Expression _if;

            if (t.Count == 1)
            {
                _if = t[0];
            }
            else
            {
                _if = new ExpressionAnd(t);
            }
            var s = (from x in X.Descendants("Then").First().Elements()
                     select Action.LoadXml(x)).ToList();
            Action _then;

            if (s.Count == 1)
            {
                _then = s[0];
            }
            else
            {
                _then = new CombinedAction(s);
            }
            var R = new Rule(_if, _then);

            if (X.Attribute("Priority") != null)
            {
                R.Priority = int.Parse(X.Attribute("Priority").Value);
            }
            if (X.Attribute("RuleSet") != null)
            {
                R.RuleSet = X.Attribute("RuleSet").Value;
            }
            return(R);
        }
Example #2
0
        public static Expression ParseExpressionsSequence(string expressionsSequence)
        {
            Expression expression = null;

            expression = ParseAtomicExpression(expressionsSequence);

            if (expression == null)
            {
                int   firstOpeningBracePosition = expressionsSequence.IndexOf("(");
                int[] closingBracesPositions    = BracketedConfigProcessor.AllIndexesOf(expressionsSequence, ")");
                if (firstOpeningBracePosition == -1 || closingBracesPositions.Length != 0)
                {
                    if (closingBracesPositions.Length > 0)
                    {
                        for (int tryingClosingBraceIndex = 0;
                             tryingClosingBraceIndex != closingBracesPositions.Length;
                             tryingClosingBraceIndex++)
                        {
                            int possibleExpression1Start  = firstOpeningBracePosition + 1;
                            int possibleExpression1End    = closingBracesPositions[tryingClosingBraceIndex];
                            int possibleExpression1Length = possibleExpression1End - possibleExpression1Start;

                            Expression expression1 = null;
                            if (possibleExpression1Length >= 0)
                            {
                                string possibleExpression1Substring =
                                    expressionsSequence.Substring(possibleExpression1Start, possibleExpression1Length);
                                expression1 = ParseExpressionsSequence(possibleExpression1Substring);
                            }

                            if (expression1 != null)
                            {
                                string restOfString = expressionsSequence.Substring(possibleExpression1End);

                                if (!Regex.IsMatch(restOfString, @"^\s*\)\s*$"))
                                {
                                    if (Regex.IsMatch(restOfString, @"^\s*\)\s*(and|AND).+$"))
                                    {
                                        string possibleExpression2Substring =
                                            restOfString.Substring(restOfString.IndexOf("and") + "and".Length);
                                        Expression expression2 = ParseExpressionsSequence(possibleExpression2Substring);
                                        if (expression1 != null && expression2 != null)
                                        {
                                            expression = new ExpressionAnd(new List <Expression> {
                                                expression1, expression2
                                            });
                                        }
                                    }
                                    else if (Regex.IsMatch(restOfString, @"^\s*\)\s*(or|OR).+$"))
                                    {
                                        string possibleExpression2Substring =
                                            restOfString.Substring(restOfString.IndexOf("or") + "or".Length);
                                        Expression expression2 = ParseExpressionsSequence(possibleExpression2Substring);
                                        if (expression1 != null && expression2 != null)
                                        {
                                            expression = new ExpressionOr(new List <Expression> {
                                                expression1, expression2
                                            });
                                        }
                                    }
                                }
                                else
                                {
                                    expression = expression1;
                                }

                                if (expression != null)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(expression);
        }