Search in sources :

Example 11 with JexlArgumentDescriptor

use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.

the class FunctionNormalizationRebuildingVisitor method normalize.

public static JexlNode normalize(ASTFunctionNode function, Multimap<String, Type<?>> allNormalizers, MetadataHelper helper, Set<String> datatypeFilter) {
    Preconditions.checkNotNull(function);
    Preconditions.checkNotNull(allNormalizers);
    Preconditions.checkNotNull(helper);
    JexlArgumentDescriptor descriptor = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(function);
    List<ASTFunctionNode> functions = Lists.newArrayList();
    // create the distinct normalization lists.
    List<List<Type<?>>> lists = getNormalizerListsForArgs(function, allNormalizers, descriptor, helper, datatypeFilter);
    // now for each unique list of normalizers, lets normalize the function arguments
    for (List<Type<?>> list : lists) {
        FunctionNormalizationRebuildingVisitor visitor = new FunctionNormalizationRebuildingVisitor(list, descriptor, helper, datatypeFilter);
        ASTFunctionNode node = (ASTFunctionNode) function.jjtAccept(visitor, null);
        if (!visitor.normalizationFailed) {
            functions.add(node);
        }
    }
    // now reduce the set of functions by eliminating those variants with identical normalized values
    Comparator<ASTFunctionNode> comparator = new ASTFunctionNodeComparator();
    Collections.sort(functions, comparator);
    ASTFunctionNode last = null;
    for (Iterator<ASTFunctionNode> it = functions.iterator(); it.hasNext(); ) {
        ASTFunctionNode test = it.next();
        if (last != null && comparator.compare(last, test) == 0) {
            it.remove();
        } else {
            last = test;
        }
    }
    if (functions.isEmpty()) {
        return copy(function);
    } else if (1 == functions.size()) {
        return functions.iterator().next();
    } else {
        JexlArgumentDescriptor desc = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(function);
        if (desc.useOrForExpansion()) {
            return JexlNodeFactory.createOrNode(functions);
        } else {
            return JexlNodeFactory.createAndNode(functions);
        }
    }
}
Also used : ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) NoOpType(datawave.data.type.NoOpType) JexlNodes.newInstanceOfType(org.apache.commons.jexl2.parser.JexlNodes.newInstanceOfType) Type(datawave.data.type.Type) JexlArgumentDescriptor(datawave.query.jexl.functions.arguments.JexlArgumentDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList)

Example 12 with JexlArgumentDescriptor

use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.

the class GeoFeatureVisitor method visit.

@Override
public Object visit(ASTFunctionNode node, Object data) {
    JexlArgumentDescriptor desc = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(node);
    try {
        if (desc instanceof GeoFunctionsDescriptor.GeoJexlArgumentDescriptor) {
            JexlNode geowaveNode = ((GeoFunctionsDescriptor.GeoJexlArgumentDescriptor) desc).toGeoWaveFunction(desc.fields(null, null));
            geowaveNode.jjtAccept(this, JexlStringBuildingVisitor.buildQuery(node));
        } else if (desc instanceof GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) {
            String function = null;
            if (data != null)
                function = (String) data;
            else
                function = JexlStringBuildingVisitor.buildQuery(node);
            // reformat as a lucene function
            if (isLuceneQuery) {
                int paramsIdx = function.indexOf('(');
                String op = function.substring(0, function.indexOf('('));
                String params = function.substring(paramsIdx);
                if (op.startsWith("geowave:")) {
                    function = op.replace("geowave:", "#").toUpperCase() + params;
                } else if (op.startsWith("geo:")) {
                    String opParam = op.substring("geo:within_".length());
                    function = "#GEO(" + opParam + ", " + params.substring(1);
                }
            }
            String geometry = geoJson.toString(wktReader.read(((GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) desc).getWkt()));
            geoFeatures.add(new QueryGeometry(function, geometry));
        }
    } catch (Exception e) {
        log.error("Unable to extract geo feature from function", e);
    }
    return node;
}
Also used : QueryGeometry(datawave.webservice.query.map.QueryGeometry) GeoWaveFunctionsDescriptor(datawave.query.jexl.functions.GeoWaveFunctionsDescriptor) JexlArgumentDescriptor(datawave.query.jexl.functions.arguments.JexlArgumentDescriptor) JexlNode(org.apache.commons.jexl2.parser.JexlNode)

Example 13 with JexlArgumentDescriptor

use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.

the class GeoWavePruningVisitor method visit.

@Override
public Object visit(ASTAndNode node, Object data) {
    Multimap<String, Geometry> fieldToGeometryMap = (data instanceof Multimap) ? (Multimap<String, Geometry>) data : HashMultimap.create();
    // if one of the anded nodes is a geowave function, pass down the geometry and field name in the multimap
    for (JexlNode child : children(node)) {
        child = JexlASTHelper.dereference(child);
        if (child instanceof ASTFunctionNode) {
            JexlArgumentDescriptor desc = JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor((ASTFunctionNode) child);
            if (desc instanceof GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) {
                GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor geoWaveDesc = (GeoWaveFunctionsDescriptor.GeoWaveJexlArgumentDescriptor) desc;
                Geometry geom = GeometryNormalizer.parseGeometry(geoWaveDesc.getWkt());
                Set<String> fields = geoWaveDesc.fields(metadataHelper, Collections.emptySet());
                for (String field : fields) {
                    fieldToGeometryMap.put(field, geom);
                }
            }
        }
    }
    return super.visit(node, (fieldToGeometryMap.isEmpty() ? null : fieldToGeometryMap));
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) GeoWaveFunctionsDescriptor(datawave.query.jexl.functions.GeoWaveFunctionsDescriptor) JexlNode(org.apache.commons.jexl2.parser.JexlNode) JexlArgumentDescriptor(datawave.query.jexl.functions.arguments.JexlArgumentDescriptor)

Example 14 with JexlArgumentDescriptor

use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.

the class ContentFunctionsTest method testJexlFunctionArgumentDescriptors.

private void testJexlFunctionArgumentDescriptors(String query, String expected, Set<String> contentFields) throws ParseException {
    MockMetadataHelper metadataHelper = new MockMetadataHelper();
    metadataHelper.addTermFrequencyFields(Arrays.asList("BODY", "META"));
    metadataHelper.setIndexedFields(Sets.newHashSet("BODY", "META"));
    if (contentFields != null) {
        metadataHelper.addContentFields(contentFields);
    }
    MockDateIndexHelper dateIndexHelper = new MockDateIndexHelper();
    ASTJexlScript script = JexlASTHelper.parseJexlQuery(query);
    JexlNode ref = script.jjtGetChild(0);
    Assert.assertEquals("First child of ASTJexlScript is not an ASTReference", ASTReference.class, ref.getClass());
    JexlNode child = ref.jjtGetChild(0);
    Assert.assertEquals("First child of ASTJexlScript is not an AStFunctionNode", ASTFunctionNode.class, child.getClass());
    ASTFunctionNode function = (ASTFunctionNode) child;
    JexlArgumentDescriptor desc = new ContentFunctionsDescriptor().getArgumentDescriptor(function);
    JexlNode indexQuery = desc.getIndexQuery(null, metadataHelper, dateIndexHelper, null);
    ASTJexlScript expectedScript = JexlASTHelper.parseJexlQuery(expected);
    JexlNode scriptChild = expectedScript.jjtGetChild(0);
    Assert.assertTrue("Expected " + JexlStringBuildingVisitor.buildQuery(scriptChild) + " but was " + JexlStringBuildingVisitor.buildQuery(indexQuery), JexlASTHelper.equals(scriptChild, indexQuery));
}
Also used : MockMetadataHelper(datawave.query.util.MockMetadataHelper) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) JexlNode(org.apache.commons.jexl2.parser.JexlNode) JexlArgumentDescriptor(datawave.query.jexl.functions.arguments.JexlArgumentDescriptor) MockDateIndexHelper(datawave.query.util.MockDateIndexHelper)

Example 15 with JexlArgumentDescriptor

use of datawave.query.jexl.functions.arguments.JexlArgumentDescriptor in project datawave by NationalSecurityAgency.

the class GeoFunctionsDescriptorTest method antiMeridianTest2.

@Test
public void antiMeridianTest2() throws Exception {
    String query = "geo:within_bounding_box(LON_FIELD, LAT_FIELD, '170', '40', '-170', '50')";
    JexlNode node = JexlASTHelper.parseJexlQuery(query);
    JexlArgumentDescriptor argDesc = new GeoFunctionsDescriptor().getArgumentDescriptor((ASTFunctionNode) node.jjtGetChild(0).jjtGetChild(0));
    JexlNode queryNode = argDesc.getIndexQuery(null, null, null, null);
    Assert.assertEquals("((((_Bounded_ = true) && (LON_FIELD >= '170.0' && LON_FIELD <= '180')) && ((_Bounded_ = true) && (LAT_FIELD >= '40.0' && LAT_FIELD <= '50.0'))) && (((_Bounded_ = true) && (LON_FIELD >= '-180' && LON_FIELD <= '-170.0')) && ((_Bounded_ = true) && (LAT_FIELD >= '40.0' && LAT_FIELD <= '50.0'))))", JexlStringBuildingVisitor.buildQuery(queryNode));
}
Also used : JexlNode(org.apache.commons.jexl2.parser.JexlNode) JexlArgumentDescriptor(datawave.query.jexl.functions.arguments.JexlArgumentDescriptor) Test(org.junit.Test)

Aggregations

JexlArgumentDescriptor (datawave.query.jexl.functions.arguments.JexlArgumentDescriptor)17 JexlNode (org.apache.commons.jexl2.parser.JexlNode)12 ASTFunctionNode (org.apache.commons.jexl2.parser.ASTFunctionNode)6 HashMultimap (com.google.common.collect.HashMultimap)3 Multimap (com.google.common.collect.Multimap)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Set (java.util.Set)3 Test (org.junit.Test)3 NoOpType (datawave.data.type.NoOpType)2 Type (datawave.data.type.Type)2 FunctionJexlNodeVisitor (datawave.query.jexl.functions.FunctionJexlNodeVisitor)2 GeoWaveFunctionsDescriptor (datawave.query.jexl.functions.GeoWaveFunctionsDescriptor)2 ExceededValueThresholdMarkerJexlNode (datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode)2 MockMetadataHelper (datawave.query.util.MockMetadataHelper)2 ASTAndNode (org.apache.commons.jexl2.parser.ASTAndNode)2 ASTJexlScript (org.apache.commons.jexl2.parser.ASTJexlScript)2 JexlNodes.newInstanceOfType (org.apache.commons.jexl2.parser.JexlNodes.newInstanceOfType)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Sets (com.google.common.collect.Sets)1