Exemplo n.º 1
0
        private BoolPredicate GetBooleanPredicate(d.Predicate predicate)
        {
            foreach (var item in _bucket.Predicates)
            {
                if (item is BoolPredicate)
                {
                    var foo = item as BoolPredicate;

                    if (foo.Left is Predicate)
                    {
                        if (foo.Left.Interval.A == predicate.Interval.A && foo.Left.Interval.B == predicate.Interval.B)
                        {
                            return(foo);
                        }
                    }

                    if (foo.Right is Predicate)
                    {
                        if (foo.Right.Interval.A == predicate.Interval.A && foo.Right.Interval.B == predicate.Interval.B)
                        {
                            return(foo);
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        private d.Predicate GetDrumPredicate(TSqlParser.PredicateContext context)
        {
            var predicateInterval = context.SourceInterval;
            var bucketInterval    = new Interval {
                A = predicateInterval.a, B = predicateInterval.b
            };

            var newPredicate = new d.Predicate(GetNextPredicateId());

            newPredicate.SetInterval(bucketInterval);
            newPredicate.Text = GetWhiteSpaceFromCurrentContext(context);

            return(newPredicate);
        }
Exemplo n.º 3
0
        private d.Predicate GetDrumPredicate(TSqlParser.Search_conditionContext context)
        {
            var predicate = context.predicate();

            if (predicate is not null)
            {
                var predicateInterval = predicate.SourceInterval;
                var bucketInterval    = new Interval {
                    A = predicateInterval.a, B = predicateInterval.b
                };

                var newPredicate = new d.Predicate(GetNextPredicateId());
                newPredicate.SetInterval(bucketInterval);
                newPredicate.Text = GetWhiteSpaceFromCurrentContext(predicate);

                return(newPredicate);
            }

            return(null);
        }
Exemplo n.º 4
0
        public override void EnterSearch_condition([NotNull] TSqlParser.Search_conditionContext context)
        {
            string debug    = context.GetText();
            var    interval = context.SourceInterval;

            Console.WriteLine("EnterSearch_condition:");
            Console.WriteLine($"Text: {debug}");
            Console.WriteLine($"Interval: {interval.ToString()}");

            var predicate = context.predicate();

            if (predicate != null)
            {
                string predicateText     = predicate.GetText();
                var    predicateInterval = predicate.SourceInterval;
                var    bucketInterval    = new Interval {
                    A = predicateInterval.a, B = predicateInterval.b
                };

                d.Predicate newPredicate = null;

                if (!HasPredicate(bucketInterval))
                {
                    if (PredicateHasBool(context))
                    {
                        // we need to determine if we need to add a boolean predicate
                        // or regular predicate
                        WalkTree(context);
                    }
                    else
                    {
                        // just add the predicate
                        newPredicate = new d.Predicate(GetNextPredicateId());
                        newPredicate.SetInterval(bucketInterval);
                        AddPredicate(newPredicate);
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void WalkTree(RuleContext context)
        {
            bool hasBoolean = false;

            d.Predicate drumPredicate = null;
            string      boolText      = string.Empty;

            if (context is TSqlParser.Search_conditionContext)
            {
                string debug           = context.GetText();
                var    searchContext   = context as TSqlParser.Search_conditionContext;
                var    searchPredicate = searchContext.predicate();
                if (searchPredicate is not null)
                {
                    // do something with the predicate
                    drumPredicate = GetDrumPredicate(searchPredicate);
                }
                else
                {
                    foreach (var c in searchContext.children)
                    {
                        if (c is TSqlParser.Search_conditionContext)
                        {
                            var x = c as TSqlParser.Search_conditionContext;
                            var p = x.predicate();
                            if (p is not null)
                            {
                                drumPredicate = GetDrumPredicate(p);
                            }
                        }
                    }
                }
            }

            if (context.Parent.ChildCount > 0)
            {
                if (context.Parent is TSqlParser.Search_conditionContext)
                {
                    var parent = context.Parent as TSqlParser.Search_conditionContext;

                    bool anyChildrenHaveBool = parent.children.Any(child => string.Equals(child.GetText(), "AND", StringComparison.OrdinalIgnoreCase) || string.Equals(child.GetText(), "OR", StringComparison.OrdinalIgnoreCase));

                    if (anyChildrenHaveBool)
                    {
                        foreach (var child in parent.children)
                        {
                            var childText = child.GetText();
                            if (childText == "AND" || childText == "OR")
                            {
                                hasBoolean = true;
                                boolText   = childText;
                            }
                        }

                        foreach (var child in parent.children)
                        {
                            var text = child.GetText();
                            if (!string.Equals("AND", text, StringComparison.OrdinalIgnoreCase) && !string.Equals("OR", text, StringComparison.OrdinalIgnoreCase))
                            {
                                if (child is Search_conditionContext)
                                {
                                    var c          = child as Search_conditionContext;
                                    var dPredicate = GetDrumPredicate(c);

                                    if (dPredicate is null)
                                    {
                                        // do something
                                        var childInterval  = child.SourceInterval;
                                        var dChildInterval = new Interval {
                                            A = childInterval.a, B = childInterval.b
                                        };

                                        var textCharacters = text.ToCharArray();
                                        foreach (var ch in textCharacters)
                                        {
                                            if (ch == Char.Parse("("))
                                            {
                                                dChildInterval.A += 1;
                                            }

                                            /*
                                             * if (ch == Char.Parse(")"))
                                             * {
                                             *  dChildInterval.B -= 1;
                                             * }
                                             */
                                        }

                                        if (text.EndsWith(")"))
                                        {
                                            dChildInterval.B -= 1;
                                        }

                                        var foo = dChildInterval;

                                        if (HasPredicate(dChildInterval))
                                        {
                                            // we need to point this predicate to the previous boolean interval
                                            var existingBoolean = GetBooleanPredicate(dChildInterval);
                                            if (existingBoolean is not null)
                                            {
                                                if (drumPredicate is not null)
                                                {
                                                    var booleanPredicate = new BoolPredicate(GetNextPredicateId());
                                                    booleanPredicate.Boolean = boolText;
                                                    booleanPredicate.Left    = existingBoolean;
                                                    booleanPredicate.Right   = drumPredicate;
                                                    AddPredicate(booleanPredicate);
                                                    return;
                                                }
                                            }
                                            else
                                            {
                                                return;
                                            }
                                        }
                                    }

                                    // we need to check to see if there is already a boolean in the bucket
                                    // for this predicate

                                    if (drumPredicate is not null)
                                    {
                                        if (HasBooleanPredicate(drumPredicate))
                                        {
                                            var existingBoolean = GetBooleanPredicate(drumPredicate);
                                            existingBoolean.Right = dPredicate;
                                        }
                                        else
                                        {
                                            var booleanPredicate = new BoolPredicate(GetNextPredicateId());
                                            booleanPredicate.Boolean = boolText;
                                            booleanPredicate.Left    = drumPredicate;
                                            AddPredicate(booleanPredicate);
                                        }
                                    }
                                }
                            }
                        }

                        return;
                    }

                    // if we don't have any bools, we need to scan the child parent just in case
                    // see if we're caught in between parenthesis

                    bool anyChildrenHaveParen = parent.children.Any(child => child.GetText().StartsWith("(") || child.GetText().StartsWith(""));
                    if (anyChildrenHaveParen)
                    {
                        foreach (var child in parent.children)
                        {
                            var childText = child.GetText();
                            if (childText.StartsWith("(") || childText.StartsWith(")"))
                            {
                                continue;
                            }
                            else
                            {
                                if (child is TSqlParser.Search_conditionContext)
                                {
                                    var recursiveChild = child as TSqlParser.Search_conditionContext;

                                    string debug            = recursiveChild.Parent.GetText();
                                    string debugGrandParent = recursiveChild.Parent.Parent.GetText();

                                    var grandParent = recursiveChild.Parent.Parent;

                                    if (grandParent is TSqlParser.Search_conditionContext)
                                    {
                                        var gp = grandParent as TSqlParser.Search_conditionContext;
                                        foreach (var grandChild in gp.children)
                                        {
                                            if (grandChild is TSqlParser.Search_conditionContext)
                                            {
                                                var gc = grandChild as TSqlParser.Search_conditionContext;
                                                WalkTree(gc);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }