Search in sources :

Example 16 with JexlNode

use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.

the class TreeFlatteningRebuilder method getAndOrLeaves.

private List<JexlNode> getAndOrLeaves(JexlNode node) {
    LinkedList<JexlNode> children = new LinkedList<>();
    LinkedList<JexlNode> stack = new LinkedList<>();
    stack.push(node);
    while (!stack.isEmpty()) {
        JexlNode currNode = stack.pop();
        // @formatter:off
        if (currNode == node || (node.getClass().isInstance(currNode) && (currNode instanceof ASTOrNode || (currNode instanceof ASTAndNode && !isBoundedRange((ASTAndNode) currNode))))) {
            // @formatter:on
            for (JexlNode child : children(currNode)) {
                stack.push(child);
            }
        } else {
            children.push(currNode);
        }
    }
    return children;
}
Also used : ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) LinkedList(java.util.LinkedList) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Example 17 with JexlNode

use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.

the class TreeFlatteningRebuilder method flattenTree.

/**
 * Given a JexlNode, creates a copy of that node which has had it's AND and OR nodes flattened.
 *
 * @param rootNode
 *            the node to flatten
 * @param <T>
 * @return the flattened copy
 */
public <T extends JexlNode> T flattenTree(T rootNode) {
    Deque<JexlNode> postOrderStack = new LinkedList<>();
    // iteratively copy the root node, and create the post order traversal stack
    copyTree(rootNode, postOrderStack);
    // use the copied post order traversal stack to iteratively flatten the tree
    JexlNode newRoot = flattenTree(postOrderStack);
    return (T) newRoot;
}
Also used : JexlNode(org.apache.commons.jexl2.parser.JexlNode) LinkedList(java.util.LinkedList)

Example 18 with JexlNode

use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.

the class TreeFlatteningRebuilder method rebuildNode.

/**
 * Returns a copy of the passed in node.
 *
 * If the original node has children, those exact children (not copies) will be added to the copied node. However, the parentage of those child nodes will
 * be left as-is.
 *
 * If the original node has no children, we will simply use the RebuildingVisitor to copy the node.
 *
 * @param node
 *            the node to copy
 * @return the copied node
 */
private JexlNode rebuildNode(JexlNode node) {
    JexlNode newNode;
    if (node.jjtGetNumChildren() == 0) {
        newNode = RebuildingVisitor.copy(node);
    } else {
        newNode = JexlNodes.newInstanceOfType(node);
        newNode.image = node.image;
        JexlNodes.ensureCapacity(newNode, node.jjtGetNumChildren());
        int nodeIdx = 0;
        for (JexlNode child : children(node)) newNode.jjtAddChild(child, nodeIdx++);
    }
    return newNode;
}
Also used : JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 19 with JexlNode

use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.

the class TreeWrappingRebuildingVisitor method visit.

@Override
public Object visit(ASTOrNode node, Object data) {
    ASTOrNode newNode = new ASTOrNode(ParserTreeConstants.JJTORNODE);
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        JexlNode child = (JexlNode) node.jjtGetChild(i).jjtAccept(this, data);
        newNode.jjtAddChild(child, newNode.jjtGetNumChildren());
        child.jjtSetParent(newNode);
    }
    return JexlNodeFactory.wrap(newNode);
}
Also used : ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 20 with JexlNode

use of org.apache.commons.jexl3.parser.JexlNode in project datawave by NationalSecurityAgency.

the class UniqueExpressionTermsVisitor method getUniqueChildren.

private List<JexlNode> getUniqueChildren(List<JexlNode> nodes) {
    Set<String> childKeys = new HashSet<>();
    List<JexlNode> unique = new ArrayList<>();
    for (JexlNode node : nodes) {
        String childKey = JexlStringBuildingVisitor.buildQueryWithoutParse(TreeFlatteningRebuildingVisitor.flatten(node), true);
        if (childKeys.add(childKey)) {
            unique.add(node);
        } else {
            this.duplicates++;
        }
    }
    return unique;
}
Also used : ArrayList(java.util.ArrayList) JexlNode(org.apache.commons.jexl2.parser.JexlNode) HashSet(java.util.HashSet)

Aggregations

JexlNode (org.apache.commons.jexl2.parser.JexlNode)327 Test (org.junit.Test)124 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)63 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)56 ArrayList (java.util.ArrayList)50 JexlNode (org.apache.commons.jexl3.parser.JexlNode)38 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)34 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)33 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)28 ExceededOrThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededOrThresholdMarkerJexlNode)25 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)25 JexlException (org.apache.commons.jexl3.JexlException)24 HashSet (java.util.HashSet)22 ASTReference (org.apache.commons.jexl2.parser.ASTReference)19 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)18 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)16 IndexHoleMarkerJexlNode (datawave.query.jexl.nodes.IndexHoleMarkerJexlNode)15 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)15 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)14 JexlArgumentDescriptor (datawave.query.jexl.functions.arguments.JexlArgumentDescriptor)12