Ejemplo n.º 1
0
        private void DebugBoolPredicate(BoolPredicate predicate)
        {
            //Console.WriteLine($"Bool Predicate Id: {predicate.Id.ToString()}");
            //Console.WriteLine($"Bool Interval: {predicate.Interval.A.ToString()}:{predicate.Interval.B.ToString()}");
            //Console.WriteLine($"Bool Operator: {predicate.Boolean}");

            if (predicate.Left is not null)
            {
                if (predicate.Left is BoolPredicate)
                {
                    //Console.WriteLine($"Bool Predicate Id: {predicate.Id.ToString()} Left Is Bool, evaluating...");
                    DebugBoolPredicate(predicate.Left as BoolPredicate);
                }
                else if (predicate.Left is Predicate && predicate.Right is Predicate)
                {
                    DebugPredicate(predicate.Left as Predicate);
                    Console.WriteLine($"Bool Operator: {predicate.Boolean}");
                    DebugPredicate(predicate.Right as Predicate);
                }
                else
                {
                    DebugPredicate(predicate.Left as Predicate);
                }
            }

            if (predicate.Right is not null)
            {
                if (predicate.Right is BoolPredicate)
                {
                    //Console.WriteLine($"Bool Predicate Id: {predicate.Id.ToString()} Right Is Bool, evaluating...");
                    DebugBoolPredicate(predicate.Right as BoolPredicate);
                }
                else
                {
                    Console.WriteLine($"Bool Operator: {predicate.Boolean}");
                    DebugPredicate(predicate.Right as Predicate);
                }
            }
        }
Ejemplo n.º 2
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);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }