Search in sources :

Example 1 with SimpleNode

use of org.apache.commons.jexl2.parser.SimpleNode in project datawave by NationalSecurityAgency.

the class QueryJexl method createNormalizedExpression.

// =================================
// private methods
private Expression createNormalizedExpression(final String query) {
    try {
        ASTJexlScript script = JexlASTHelper.parseJexlQuery(query);
        Deque<SimpleNode> nodes = new LinkedList<>();
        normalizeScript(script, nodes);
        return new NormalizedExpression(jEngine, query, script);
    } catch (TokenMgrError | org.apache.commons.jexl2.parser.ParseException pe) {
        throw new AssertionError(pe);
    }
}
Also used : ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) TokenMgrError(org.apache.commons.jexl2.parser.TokenMgrError) ParseException(java.text.ParseException) LinkedList(java.util.LinkedList) SimpleNode(org.apache.commons.jexl2.parser.SimpleNode)

Example 2 with SimpleNode

use of org.apache.commons.jexl2.parser.SimpleNode in project datawave by NationalSecurityAgency.

the class TreeEqualityVisitor method listChildren.

private List<SimpleNode> listChildren(SimpleNode node) {
    List<SimpleNode> list = new ArrayList<>();
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        list.add(node.jjtGetChild(i));
    }
    boolean changed = true;
    List<SimpleNode> newList = new ArrayList<>();
    while (changed) {
        changed = false;
        for (SimpleNode child : list) {
            // note the isAssignableFrom is to handle QueryPropertyMarker nodes
            if (((child.getClass().equals(ASTReference.class) || ASTReference.class.isAssignableFrom(child.getClass())) && (child.jjtGetNumChildren() == 1)) || (child.getClass().equals(ASTReferenceExpression.class) && (child.jjtGetNumChildren() == 1)) || (child.getClass().equals(ASTOrNode.class) && ((child.jjtGetNumChildren() == 1) || node.getClass().equals(ASTOrNode.class))) || (child.getClass().equals(ASTAndNode.class) && ((child.jjtGetNumChildren() == 1) || node.getClass().equals(ASTAndNode.class)))) {
                for (int j = 0; j < child.jjtGetNumChildren(); j++) {
                    newList.add(child.jjtGetChild(j));
                }
                changed = true;
            } else {
                newList.add(child);
            }
        }
        List<SimpleNode> temp = newList;
        newList = list;
        list = temp;
        newList.clear();
    }
    return list;
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ArrayList(java.util.ArrayList) ASTReference(org.apache.commons.jexl2.parser.ASTReference) SimpleNode(org.apache.commons.jexl2.parser.SimpleNode) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Example 3 with SimpleNode

use of org.apache.commons.jexl2.parser.SimpleNode in project datawave by NationalSecurityAgency.

the class TreeEqualityVisitor method visitEquality.

/**
 * Accept the visitor on all this node's children.
 *
 * @param node1
 * @param node2
 * @return result of visit
 */
private Object visitEquality(SimpleNode node1, SimpleNode node2) {
    if (!equal) {
        return "Already not equal";
    } else if (!node1.getClass().equals(node2.getClass())) {
        equal = false;
        return "Classes differ: " + node1.getClass().getSimpleName() + " vs " + node2.getClass().getSimpleName();
    } else if (!equal(node1.jjtGetValue(), node2.jjtGetValue())) {
        equal = false;
        return ("Node values differ: " + node1.jjtGetValue() + " vs " + node2.jjtGetValue());
    } else if (node1 instanceof JexlNode && !equal(((JexlNode) node1).image, ((JexlNode) node2).image)) {
        equal = false;
        return ("Node images differ: " + ((JexlNode) node1).image + " vs " + ((JexlNode) node2).image);
    } else if (node1.jjtGetNumChildren() > 0 || node2.jjtGetNumChildren() > 0) {
        List<SimpleNode> list1 = listChildren(node1);
        List<SimpleNode> list2 = listChildren(node2);
        if (list1.size() != list2.size()) {
            if (log.isDebugEnabled()) {
                log.debug("not equal " + list1.size() + " " + list2.size());
            }
            equal = false;
            return ("Num children differ: " + list1 + " vs " + list2);
        }
        Object reason = null;
        // start visiting recursively to find equality
        for (SimpleNode child1 : list1) {
            // compare the list1 node to each node in list2 until we find a match
            for (int i = 0; i < list2.size(); i++) {
                SimpleNode child2 = list2.get(i);
                equal = true;
                reason = child1.jjtAccept(this, child2);
                if (equal) {
                    // equal may be made false by child
                    // found a match, break;
                    list2.remove(i);
                    break;
                }
            }
            // if we get here with !equal, then we never found a match for a node...break out
            if (!equal) {
                return "Did not find a matching child for " + child1 + " in " + list2 + ": " + reason;
            }
        }
    }
    return null;
}
Also used : JexlNode(org.apache.commons.jexl2.parser.JexlNode) SimpleNode(org.apache.commons.jexl2.parser.SimpleNode)

Example 4 with SimpleNode

use of org.apache.commons.jexl2.parser.SimpleNode in project datawave by NationalSecurityAgency.

the class NodeTypeCountVisitorTest method testSimpleNode.

@Test
public void testSimpleNode() {
    NodeTypeCount count = (NodeTypeCount) new SimpleNode(ParserTreeConstants.JJTREFERENCE).jjtAccept(new NodeTypeCountVisitor(), null);
    assertEquals(1, count.getTotal(SimpleNode.class));
}
Also used : NodeTypeCount(datawave.query.jexl.NodeTypeCount) SimpleNode(org.apache.commons.jexl2.parser.SimpleNode) Test(org.junit.Test)

Example 5 with SimpleNode

use of org.apache.commons.jexl2.parser.SimpleNode in project datawave by NationalSecurityAgency.

the class QueryJexl method normalizeScript.

/**
 * Normalizes all of the {@link ASTIdentifier}, {@link ASTStringLiteral}, and {@link ASTNumberLiteral} entries that exist in a {@link ASTJexlScript}.
 *
 * @param node
 *            current node for normalization
 * @param nodes
 *            queue of nodes used as a stack for normalization
 */
private void normalizeScript(final SimpleNode node, final Deque<SimpleNode> nodes) {
    int num = node.jjtGetNumChildren();
    for (int n = 0; n < num; n++) {
        SimpleNode child = node.jjtGetChild(n);
        if (0 < child.jjtGetNumChildren()) {
            if (!(child instanceof ASTReference || child instanceof ASTReferenceExpression)) {
                // this may be an op node (e.g. ASTEQNode) or some other node
                nodes.addFirst(child);
            }
            // recursive processing of child nodes
            normalizeScript(child, nodes);
        } else {
            // the order for the identifier and literal nodes may vary
            if (child instanceof ASTIdentifier) {
                ASTIdentifier id = (ASTIdentifier) child;
                // change identifier to lower case
                id.image = id.image.toLowerCase();
                // check for string or numeric literal on node stack
                SimpleNode entry = nodes.removeFirst();
                Assert.assertNotNull(entry);
                if (entry instanceof ASTStringLiteral || entry instanceof ASTNumberLiteral) {
                    // exp is "value OP field"
                    // remove op node
                    SimpleNode opNode = nodes.removeFirst();
                    normalizeField(id.image, (JexlNode) entry, opNode);
                } else {
                    // push entry back on stack and add identifier
                    nodes.addFirst(entry);
                    nodes.addFirst(child);
                }
            } else if (child instanceof ASTStringLiteral || child instanceof ASTNumberLiteral) {
                // check for identifier on node stack
                SimpleNode entry = nodes.removeFirst();
                Assert.assertNotNull(entry);
                if (entry instanceof ASTIdentifier) {
                    // exp is "field OP value"
                    SimpleNode opNode = nodes.removeFirst();
                    normalizeField(((JexlNode) entry).image, (JexlNode) child, opNode);
                } else {
                    // push entry back on stack and add literal
                    nodes.addFirst(entry);
                    nodes.addFirst(child);
                }
            }
        }
    }
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference) SimpleNode(org.apache.commons.jexl2.parser.SimpleNode) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Aggregations

SimpleNode (org.apache.commons.jexl2.parser.SimpleNode)5 ASTReference (org.apache.commons.jexl2.parser.ASTReference)2 ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)2 JexlNode (org.apache.commons.jexl2.parser.JexlNode)2 NodeTypeCount (datawave.query.jexl.NodeTypeCount)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)1 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)1 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)1 ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)1 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)1 TokenMgrError (org.apache.commons.jexl2.parser.TokenMgrError)1 Test (org.junit.Test)1