Beispiel #1
0
        private static ExprNode Unwind(ExprNode node)
        {
            var isOr = node is ExprOrNode;
            var isAnd = node is ExprAndNode;
            if (!isOr && !isAnd) {
                return node;
            }

            var needsUnwind = false;
            foreach (var child in node.ChildNodes) {
                if (child is ExprOrNode && isOr || child is ExprAndNode && isAnd) {
                    needsUnwind = true;
                    break;
                }
            }

            if (!needsUnwind) {
                return node;
            }

            if (isOr) {
                var unwoundX = new ExprOrNode();
                foreach (var child in node.ChildNodes) {
                    if (child is ExprOrNode) {
                        foreach (var orChild in child.ChildNodes) {
                            var unwoundChild = Unwind(orChild);
                            if (unwoundChild is ExprOrNode) {
                                unwoundX.AddChildNodes(Arrays.AsList(unwoundChild.ChildNodes));
                            }
                            else {
                                unwoundX.AddChildNode(unwoundChild);
                            }
                        }
                    }
                    else {
                        unwoundX.AddChildNode(Unwind(child));
                    }
                }

                return unwoundX;
            }

            ExprAndNode unwound = new ExprAndNodeImpl();
            foreach (var child in node.ChildNodes) {
                if (child is ExprAndNode) {
                    foreach (var andChild in child.ChildNodes) {
                        var unwoundChild = Unwind(andChild);
                        if (unwoundChild is ExprAndNode) {
                            unwound.AddChildNodes(Arrays.AsList(unwoundChild.ChildNodes));
                        }
                        else {
                            unwound.AddChildNode(unwoundChild);
                        }
                    }
                }
                else {
                    unwound.AddChildNode(Unwind(child));
                }
            }

            return unwound;
        }