Search in sources :

Example 31 with ASTReference

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

the class JexlASTHelperTest method testNormalizeLiteral.

@Test
public void testNormalizeLiteral() throws Throwable {
    LcNoDiacriticsType normalizer = new LcNoDiacriticsType();
    ASTJexlScript script = JexlASTHelper.parseJexlQuery("F == 'aSTrInG'");
    if (log.isDebugEnabled()) {
        PrintingVisitor.printQuery(script);
    }
    JexlNode literal = script.jjtGetChild(0).jjtGetChild(1).jjtGetChild(0);
    ASTReference ref = JexlASTHelper.normalizeLiteral(literal, normalizer);
    Assert.assertEquals("astring", ref.jjtGetChild(0).image);
}
Also used : LcNoDiacriticsType(datawave.data.type.LcNoDiacriticsType) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference) Test(org.junit.Test)

Example 32 with ASTReference

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

the class DistributeAndedNodesVisitor method visit.

/**
 * Checks each of the child nodes, and determines how the anded nodes should be applied.
 *
 * @param node
 *            The node that we will be distributing the anded nodes into
 * @param data
 *            The nodes which we will be distributing into the root node
 * @return An updated script with the anded nodes distributed throughout
 */
@Override
public Object visit(ASTAndNode node, Object data) {
    DistributeAndedNodesVisitor.DistAndData parentData = (DistributeAndedNodesVisitor.DistAndData) data;
    if (initialNode == null || initialNode instanceof ASTReference || initialNode instanceof ASTReferenceExpression)
        initialNode = node;
    // if this node is one of the anded nodes, or a whindex
    // comprised of one of the anded nodes, halt recursion
    List<JexlNode> usedAndedNodes = usesAndedNodes(node);
    if (!usedAndedNodes.isEmpty()) {
        parentData.usedAndedNodes.addAll(usedAndedNodes);
        return node;
    }
    // this logic is dependent upon identifying whindex nodes by their address
    if (whindexNodes.containsKey(node)) {
        return node;
    }
    // check each child node to see how many of the desired andedNodes are present
    List<JexlNode> rebuiltChildren = new ArrayList<>();
    for (JexlNode child : JexlNodes.children(node)) {
        DistributeAndedNodesVisitor.DistAndData foundData = new DistributeAndedNodesVisitor.DistAndData();
        rebuiltChildren.add((JexlNode) child.jjtAccept(this, foundData));
        parentData.usedAndedNodes.addAll(foundData.usedAndedNodes);
    }
    // are some anded nodes missing, and is this the initial node?
    if (!parentData.usedAndedNodes.containsAll(andedNodes) && node.equals(initialNode)) {
        // 'and' with the missing anded nodes, and return
        List<JexlNode> nodes = andedNodes.stream().filter(andedNode -> !parentData.usedAndedNodes.contains(andedNode)).map(RebuildingVisitor::copy).collect(Collectors.toList());
        nodes.add(node);
        // this is probably unnecessary, but to be safe, let's set it
        parentData.usedAndedNodes.addAll(andedNodes);
        return WhindexVisitor.createUnwrappedAndNode(nodes);
    }
    return WhindexVisitor.createUnwrappedAndNode(rebuiltChildren);
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ArrayList(java.util.ArrayList) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference)

Example 33 with ASTReference

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

the class TreeFlatteningRebuilder method flattenTree.

/**
 * Given a stack of nodes, representing the post order traversal of a JexlNode, iteratively flattens the AND and OR nodes of the tree.
 *
 * @param postOrderStack
 *            the post order traversal representation of the tree
 * @return the flattened tree
 */
private JexlNode flattenTree(Deque<JexlNode> postOrderStack) {
    Deque<JexlNode> parentStack = new LinkedList<>();
    Deque<List<JexlNode>> childrenStack = new LinkedList<>();
    JexlNode newNode = null;
    // now that we have the post order traversal, we can operate on the nodes...
    while (!postOrderStack.isEmpty()) {
        JexlNode node = postOrderStack.pop();
        boolean hasChildren = node.jjtGetNumChildren() > 0;
        // if this is a reference node, flatten it
        if (hasChildren && node instanceof ASTReference) {
            newNode = flattenReference((ASTReference) JexlNodes.children(parentStack.pop(), childrenStack.pop().toArray(new JexlNode[0])));
        } else // if this is an AND or OR node, flatten it
        if (hasChildren && (node instanceof ASTOrNode || node instanceof ASTAndNode)) {
            newNode = flattenAndOrNode(JexlNodes.children(parentStack.pop(), childrenStack.pop().toArray(new JexlNode[0])));
        } else // if this is a node with children, assign the children
        if (hasChildren && node == parentStack.peek()) {
            newNode = JexlNodes.children(parentStack.pop(), childrenStack.pop().toArray(new JexlNode[0]));
        } else // if this is a leaf node, just keep it
        {
            newNode = node;
        }
        // if we still have nodes to evaluate
        if (!postOrderStack.isEmpty()) {
            // otherwise, add this node to the existing parent's list of children
            if (node.jjtGetParent() != parentStack.peek()) {
                parentStack.push(node.jjtGetParent());
                childrenStack.push(Lists.newArrayList(newNode));
            } else {
                childrenStack.peek().add(newNode);
            }
        }
    }
    return newNode;
}
Also used : ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Aggregations

ASTReference (org.apache.commons.jexl2.parser.ASTReference)28 JexlNode (org.apache.commons.jexl2.parser.JexlNode)17 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)11 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)8 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)7 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)6 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)6 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)5 ArrayList (java.util.ArrayList)4 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)4 ASTNRNode (org.apache.commons.jexl2.parser.ASTNRNode)4 ASTIdentifierAccess (org.apache.commons.jexl3.parser.ASTIdentifierAccess)4 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)3 List (java.util.List)3 ASTAssignment (org.apache.commons.jexl2.parser.ASTAssignment)3 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)3 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)3 ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)3 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)3 ASTReference (org.apache.commons.jexl3.parser.ASTReference)3