Search in sources :

Example 11 with ASTReferenceExpression

use of org.apache.commons.jexl2.parser.ASTReferenceExpression 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)

Example 12 with ASTReferenceExpression

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

the class JexlNodeFactory method createExpression.

/**
 * Creates a reference expression fro a child node
 *
 * @param childContainer
 * @param wrappingContainer
 * @return
 */
public static JexlNode createExpression(JexlNode childContainer, JexlNode wrappingContainer) {
    ASTReference ref = new ASTReference(ParserTreeConstants.JJTREFERENCE);
    ASTReferenceExpression exp = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION);
    for (int i = 0; i < childContainer.jjtGetNumChildren(); i++) {
        JexlNode child = childContainer.jjtGetChild(i);
        child.jjtSetParent(ref);
        exp.jjtAddChild(child, i);
        i++;
    }
    exp.jjtSetParent(ref);
    ref.jjtAddChild(exp, 0);
    JexlNode newWrapper = shallowCopy(wrappingContainer);
    ref.jjtSetParent(newWrapper);
    newWrapper.jjtAddChild(ref, 0);
    return newWrapper;
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ExceededTermThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference)

Example 13 with ASTReferenceExpression

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

the class JexlNodeFactory method wrap.

/**
 * Wrap an ASTAndNode or ASTOrNode in parenthesis if it has more than one child. Will return itself if wrapping is unnecessary.
 *
 * @param toWrap
 * @return
 */
public static JexlNode wrap(JexlNode toWrap) {
    if ((toWrap instanceof ASTAndNode || toWrap instanceof ASTOrNode) && toWrap.jjtGetNumChildren() > 1) {
        ASTReference reference = new ASTReference(ParserTreeConstants.JJTREFERENCE);
        ASTReferenceExpression parens = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION);
        parens.jjtAddChild(toWrap, 0);
        toWrap.jjtSetParent(parens);
        reference.jjtAddChild(parens, 0);
        parens.jjtSetParent(reference);
        return reference;
    }
    return toWrap;
}
Also used : ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Example 14 with ASTReferenceExpression

use of org.apache.commons.jexl2.parser.ASTReferenceExpression 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 15 with ASTReferenceExpression

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

the class QueryPropertyMarker method setupSource.

protected void setupSource(JexlNode source) {
    this.jjtSetParent(source.jjtGetParent());
    // create the assignment using the label wrapped in an expression
    JexlNode refNode1 = JexlNodeFactory.createExpression(JexlNodeFactory.createAssignment(getLabel(), true));
    // wrap the source in an expression, but only if needed
    JexlNode refNode2 = JexlNodeFactory.createExpression(source);
    // wrap the assignment and source in an AND node
    JexlNode andNode = JexlNodeFactory.createUnwrappedAndNode(Arrays.asList(refNode1, refNode2));
    // wrap the and node with an expression (see JexlNodeFactory.createAndNode)
    ASTReferenceExpression refExpNode1 = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION);
    andNode.jjtSetParent(refExpNode1);
    refExpNode1.jjtAddChild(andNode, 0);
    // and make a child of this
    refExpNode1.jjtSetParent(this);
    this.jjtAddChild(refExpNode1, 0);
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Aggregations

ASTReferenceExpression (org.apache.commons.jexl2.parser.ASTReferenceExpression)15 JexlNode (org.apache.commons.jexl2.parser.JexlNode)12 ASTReference (org.apache.commons.jexl2.parser.ASTReference)11 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)7 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)6 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)3 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)2 ArrayList (java.util.ArrayList)2 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)2 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)2 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)2 ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)2 ASTStringLiteral (org.apache.commons.jexl2.parser.ASTStringLiteral)2 IpAddressType (datawave.data.type.IpAddressType)1 Type (datawave.data.type.Type)1 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)1 ContainerType (datawave.query.jexl.JexlNodeFactory.ContainerType)1 LiteralRange (datawave.query.jexl.LiteralRange)1 BoundedRange (datawave.query.jexl.nodes.BoundedRange)1 QueryPropertyMarker (datawave.query.jexl.nodes.QueryPropertyMarker)1