Search in sources :

Example 1 with ASTFalseNode

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

the class JexlASTHelper method getLiteralValue.

/**
 * Fetch the literal off of the grandchild. Throws an exception if there is no literal
 *
 * @param node
 * @return
 * @throws NoSuchElementException
 */
@SuppressWarnings("rawtypes")
public static Object getLiteralValue(JexlNode node) throws NoSuchElementException {
    Object literal = getLiteral(node);
    // If the grandchild and its image is non-null and equal to the any-field identifier
    if (literal instanceof JexlNode.Literal) {
        return ((JexlNode.Literal) literal).getLiteral();
    } else if (literal instanceof ASTTrueNode) {
        return true;
    } else if (literal instanceof ASTFalseNode) {
        return false;
    } else if (literal instanceof ASTNullLiteral) {
        return null;
    }
    NotFoundQueryException qe = new NotFoundQueryException(DatawaveErrorCode.LITERAL_MISSING);
    throw (NoSuchElementException) (new NoSuchElementException().initCause(qe));
}
Also used : ASTFalseNode(org.apache.commons.jexl2.parser.ASTFalseNode) ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral) ASTNullLiteral(org.apache.commons.jexl2.parser.ASTNullLiteral) Literal(org.apache.commons.jexl2.parser.JexlNode.Literal) ASTTrueNode(org.apache.commons.jexl2.parser.ASTTrueNode) ASTNullLiteral(org.apache.commons.jexl2.parser.ASTNullLiteral) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with ASTFalseNode

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

the class JexlNodeFactory method shallowCopy.

/**
 * A shallow copy of the given JexlNode, creates a new node of the same type with the same parent and image. Children are not copied
 *
 * @param original
 * @return
 */
public static JexlNode shallowCopy(JexlNode original) {
    if (null == original) {
        throw new IllegalArgumentException();
    }
    JexlNode copy;
    Class<?> clz = original.getClass();
    if (ASTAndNode.class.isAssignableFrom(clz)) {
        copy = new ASTAndNode(ParserTreeConstants.JJTANDNODE);
    } else if (ASTBitwiseAndNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseAndNode(ParserTreeConstants.JJTBITWISEANDNODE);
    } else if (ASTBitwiseComplNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseComplNode(ParserTreeConstants.JJTBITWISECOMPLNODE);
    } else if (ASTBitwiseOrNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseOrNode(ParserTreeConstants.JJTBITWISEORNODE);
    } else if (ASTBitwiseXorNode.class.isAssignableFrom(clz)) {
        copy = new ASTBitwiseXorNode(ParserTreeConstants.JJTBITWISEXORNODE);
    } else if (ASTEmptyFunction.class.isAssignableFrom(clz)) {
        copy = new ASTEmptyFunction(ParserTreeConstants.JJTEMPTYFUNCTION);
    } else if (ASTEQNode.class.isAssignableFrom(clz)) {
        copy = new ASTEQNode(ParserTreeConstants.JJTEQNODE);
    } else if (ASTERNode.class.isAssignableFrom(clz)) {
        copy = new ASTERNode(ParserTreeConstants.JJTERNODE);
    } else if (ASTFalseNode.class.isAssignableFrom(clz)) {
        copy = new ASTFalseNode(ParserTreeConstants.JJTFALSENODE);
    } else if (ASTGENode.class.isAssignableFrom(clz)) {
        copy = new ASTGENode(ParserTreeConstants.JJTGENODE);
    } else if (ASTGTNode.class.isAssignableFrom(clz)) {
        copy = new ASTGTNode(ParserTreeConstants.JJTGTNODE);
    } else if (ASTIdentifier.class.isAssignableFrom(clz)) {
        copy = new ASTIdentifier(ParserTreeConstants.JJTIDENTIFIER);
    } else if (ASTLENode.class.isAssignableFrom(clz)) {
        copy = new ASTLENode(ParserTreeConstants.JJTLENODE);
    } else if (ASTLTNode.class.isAssignableFrom(clz)) {
        copy = new ASTLTNode(ParserTreeConstants.JJTLTNODE);
    } else if (ASTNENode.class.isAssignableFrom(clz)) {
        copy = new ASTNENode(ParserTreeConstants.JJTNENODE);
    } else if (ASTNRNode.class.isAssignableFrom(clz)) {
        copy = new ASTNRNode(ParserTreeConstants.JJTNRNODE);
    } else if (ASTNotNode.class.isAssignableFrom(clz)) {
        copy = new ASTNotNode(ParserTreeConstants.JJTNOTNODE);
    } else if (ASTNullLiteral.class.isAssignableFrom(clz)) {
        copy = new ASTNullLiteral(ParserTreeConstants.JJTNULLLITERAL);
    } else if (ASTNumberLiteral.class.isAssignableFrom(clz)) {
        copy = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
        JexlNodes.setLiteral((ASTNumberLiteral) copy, ((ASTNumberLiteral) original).getLiteral());
    } else if (ASTOrNode.class.isAssignableFrom(clz)) {
        copy = new ASTOrNode(ParserTreeConstants.JJTORNODE);
    } else if (ASTStringLiteral.class.isAssignableFrom(clz)) {
        copy = new ASTStringLiteral(ParserTreeConstants.JJTSTRINGLITERAL);
        JexlNodes.setLiteral((ASTStringLiteral) copy, ((ASTStringLiteral) original).getLiteral());
    } else if (ASTTrueNode.class.isAssignableFrom(clz)) {
        copy = new ASTTrueNode(ParserTreeConstants.JJTTRUENODE);
    } else if (ASTReferenceExpression.class.isAssignableFrom(clz)) {
        copy = new ASTReferenceExpression(ParserTreeConstants.JJTREFERENCEEXPRESSION);
    } else if (ASTReference.class.isAssignableFrom(clz)) {
        copy = new ASTReference(ParserTreeConstants.JJTREFERENCE);
    } else if (ASTAdditiveNode.class.isAssignableFrom(clz)) {
        copy = new ASTAdditiveNode(ParserTreeConstants.JJTADDITIVENODE);
    } else if (ASTMethodNode.class.isAssignableFrom(clz)) {
        copy = new ASTMethodNode(ParserTreeConstants.JJTMETHODNODE);
    } else if (ASTFunctionNode.class.isAssignableFrom(clz)) {
        copy = new ASTFunctionNode(ParserTreeConstants.JJTFUNCTIONNODE);
    } else if (ASTMulNode.class.isAssignableFrom(clz)) {
        copy = new ASTMulNode(ParserTreeConstants.JJTMULNODE);
    } else if (ASTAssignment.class.isAssignableFrom(clz)) {
        copy = new ASTAssignment(ParserTreeConstants.JJTASSIGNMENT);
    } else {
        throw new UnsupportedOperationException();
    }
    copy.jjtSetParent(original.jjtGetParent());
    copy.image = original.image;
    return copy;
}
Also used : ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTNENode(org.apache.commons.jexl2.parser.ASTNENode) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral) ASTMulNode(org.apache.commons.jexl2.parser.ASTMulNode) ASTBitwiseComplNode(org.apache.commons.jexl2.parser.ASTBitwiseComplNode) ASTMethodNode(org.apache.commons.jexl2.parser.ASTMethodNode) ASTBitwiseXorNode(org.apache.commons.jexl2.parser.ASTBitwiseXorNode) ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) ASTBitwiseAndNode(org.apache.commons.jexl2.parser.ASTBitwiseAndNode) ASTNotNode(org.apache.commons.jexl2.parser.ASTNotNode) ASTGENode(org.apache.commons.jexl2.parser.ASTGENode) ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) ASTAdditiveNode(org.apache.commons.jexl2.parser.ASTAdditiveNode) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) ASTTrueNode(org.apache.commons.jexl2.parser.ASTTrueNode) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) ASTAssignment(org.apache.commons.jexl2.parser.ASTAssignment) ASTGTNode(org.apache.commons.jexl2.parser.ASTGTNode) ASTNullLiteral(org.apache.commons.jexl2.parser.ASTNullLiteral) ASTLTNode(org.apache.commons.jexl2.parser.ASTLTNode) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTBitwiseOrNode(org.apache.commons.jexl2.parser.ASTBitwiseOrNode) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) ASTLENode(org.apache.commons.jexl2.parser.ASTLENode) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) ASTFalseNode(org.apache.commons.jexl2.parser.ASTFalseNode) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) ExceededTermThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode) ASTEmptyFunction(org.apache.commons.jexl2.parser.ASTEmptyFunction) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Example 3 with ASTFalseNode

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

the class GeoWavePruningVisitor method visit.

@Override
public Object visit(ASTEQNode node, Object data) {
    if (data instanceof Multimap) {
        Multimap<String, Geometry> fieldToGeometryMap = (Multimap<String, Geometry>) data;
        String field = JexlASTHelper.getIdentifier(node);
        Collection<Geometry> queryGeometries = fieldToGeometryMap.get(field);
        if (queryGeometries != null && !queryGeometries.isEmpty()) {
            String value = (String) JexlASTHelper.getLiteralValue(node);
            if (value != null) {
                Geometry nodeGeometry = GeoWaveUtils.positionToGeometry(value);
                // if the node geometry doesn't intersect the query geometry, get rid of this node
                if (fieldToGeometryMap.get(field).stream().noneMatch(nodeGeometry::intersects)) {
                    if (prunedTerms != null) {
                        prunedTerms.put(field, value);
                    }
                    return new ASTFalseNode(ParserTreeConstants.JJTFALSENODE);
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) ASTFalseNode(org.apache.commons.jexl2.parser.ASTFalseNode)

Example 4 with ASTFalseNode

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

the class QueryPruningVisitor method visit.

@Override
public Object visit(ASTAndNode node, Object data) {
    // do not process query property markers
    if (QueryPropertyMarker.findInstance(node).isAnyType()) {
        return TruthState.UNKNOWN;
    }
    // grab the node string before recursion so the original is intact
    String originalString = null;
    if (rewrite && debugPrune) {
        originalString = JexlStringBuildingVisitor.buildQuery(node);
    }
    Set<TruthState> states = new HashSet<>();
    for (int i = node.jjtGetNumChildren() - 1; i >= 0; i--) {
        JexlNode child = node.jjtGetChild(i);
        String originalChild = null;
        if (rewrite && debugPrune) {
            originalChild = JexlStringBuildingVisitor.buildQuery(child);
        }
        TruthState state = (TruthState) (child.jjtAccept(this, data));
        if (state == TruthState.FALSE) {
            // short circuit
            replaceAndAssign(node, originalString, new ASTFalseNode(ParserTreeConstants.JJTFALSENODE));
            return TruthState.FALSE;
        } else if (state == TruthState.TRUE) {
            // drop this node from the AND it will always be satisfied
            replaceAndAssign(child, null, null);
            if (debugPrune) {
                log.debug("Pruning " + originalChild + " from " + originalString);
            }
        }
        states.add(state);
    }
    // the node is either UNKNOWN or TRUE at this point since FALSE breaks evaluation early
    if (states.contains(TruthState.UNKNOWN)) {
        return TruthState.UNKNOWN;
    } else {
        replaceAndAssign(node, originalString, new ASTTrueNode(ParserTreeConstants.JJTTRUENODE));
        return TruthState.TRUE;
    }
}
Also used : ASTFalseNode(org.apache.commons.jexl2.parser.ASTFalseNode) ASTTrueNode(org.apache.commons.jexl2.parser.ASTTrueNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) HashSet(java.util.HashSet)

Example 5 with ASTFalseNode

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

the class QueryPruningVisitor method visit.

@Override
public Object visit(ASTOrNode node, Object data) {
    // grab the node string before recursion so the original is intact
    String originalString = null;
    if (rewrite && debugPrune) {
        originalString = JexlStringBuildingVisitor.buildQuery(node);
    }
    Set<TruthState> states = new HashSet<>();
    for (int i = node.jjtGetNumChildren() - 1; i >= 0; i--) {
        JexlNode child = node.jjtGetChild(i);
        String originalChild = null;
        if (rewrite && debugPrune) {
            originalChild = JexlStringBuildingVisitor.buildQuery(child);
        }
        TruthState state = (TruthState) (child.jjtAccept(this, data));
        if (state == TruthState.TRUE) {
            // short circuit
            replaceAndAssign(node, originalString, new ASTTrueNode(ParserTreeConstants.JJTTRUENODE));
            return TruthState.TRUE;
        } else if (state == TruthState.FALSE) {
            // drop this node from the OR it can never be satisfied
            replaceAndAssign(child, null, null);
            if (debugPrune) {
                log.debug("Pruning " + originalChild + " from " + originalString);
            }
        }
        states.add(state);
    }
    // the node is either UNKNOWN or FALSE at this point since TRUE breaks evaluation early
    if (states.contains(TruthState.UNKNOWN)) {
        return TruthState.UNKNOWN;
    } else {
        replaceAndAssign(node, originalString, new ASTFalseNode(ParserTreeConstants.JJTFALSENODE));
        return TruthState.FALSE;
    }
}
Also used : ASTFalseNode(org.apache.commons.jexl2.parser.ASTFalseNode) ASTTrueNode(org.apache.commons.jexl2.parser.ASTTrueNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) HashSet(java.util.HashSet)

Aggregations

ASTFalseNode (org.apache.commons.jexl2.parser.ASTFalseNode)9 ASTTrueNode (org.apache.commons.jexl2.parser.ASTTrueNode)7 JexlNode (org.apache.commons.jexl2.parser.JexlNode)5 ExceededTermThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededTermThresholdMarkerJexlNode)3 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)3 ASTIdentifier (org.apache.commons.jexl2.parser.ASTIdentifier)3 HashSet (java.util.HashSet)2 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)2 ASTAssignment (org.apache.commons.jexl2.parser.ASTAssignment)2 ASTEQNode (org.apache.commons.jexl2.parser.ASTEQNode)2 ASTERNode (org.apache.commons.jexl2.parser.ASTERNode)2 ASTGENode (org.apache.commons.jexl2.parser.ASTGENode)2 ASTGTNode (org.apache.commons.jexl2.parser.ASTGTNode)2 ASTLENode (org.apache.commons.jexl2.parser.ASTLENode)2 ASTLTNode (org.apache.commons.jexl2.parser.ASTLTNode)2 ASTNENode (org.apache.commons.jexl2.parser.ASTNENode)2 ASTNRNode (org.apache.commons.jexl2.parser.ASTNRNode)2 ASTNullLiteral (org.apache.commons.jexl2.parser.ASTNullLiteral)2 ASTNumberLiteral (org.apache.commons.jexl2.parser.ASTNumberLiteral)2 ASTOrNode (org.apache.commons.jexl2.parser.ASTOrNode)2