Search in sources :

Example 56 with GroovyExpression

use of org.apache.atlas.groovy.GroovyExpression in project incubator-atlas by apache.

the class AbstractGremlinQueryOptimizerTest method testClosureNotCreatedWhenNoOrs.

@Test
public void testClosureNotCreatedWhenNoOrs() throws AtlasException {
    GroovyExpression expr1 = makeHasExpression("prop1", "Fred");
    GroovyExpression expr2 = makeHasExpression("prop2", "George");
    GroovyExpression toOptimize = getFactory().generateLogicalExpression(getVerticesExpression(), "and", Arrays.asList(expr1, expr2));
    toOptimize = makeOutExpression(toOptimize, "knows");
    toOptimize = makeOutExpression(toOptimize, "livesIn");
    GroovyExpression optimized = GremlinQueryOptimizer.getInstance().optimize(toOptimize);
    assertEquals(optimized.toString(), getExpectedGremlinForTestClosureNotCreatedWhenNoOrs());
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) Test(org.testng.annotations.Test)

Example 57 with GroovyExpression

use of org.apache.atlas.groovy.GroovyExpression in project incubator-atlas by apache.

the class AbstractGremlinQueryOptimizerTest method testAliasInMiddle.

@Test
public void testAliasInMiddle() throws AtlasException {
    GroovyExpression expr1 = makeHasExpression("name", "Fred");
    GroovyExpression expr2 = makeHasExpression("name", "George");
    GroovyExpression expr3 = makeHasExpression("age", "13");
    GroovyExpression expr4 = makeHasExpression("age", "14");
    GroovyExpression toOptimize = getVerticesExpression();
    toOptimize = getFactory().generateLogicalExpression(toOptimize, "or", Arrays.asList(expr1, expr2));
    toOptimize = getFactory().generateAliasExpression(toOptimize, "x");
    toOptimize = getFactory().generateLogicalExpression(toOptimize, "or", Arrays.asList(expr3, expr4));
    GroovyExpression optimized = GremlinQueryOptimizer.getInstance().optimize(toOptimize);
    assertEquals(optimized.toString(), getExpectedGremlinForTestAliasInMiddle());
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) Test(org.testng.annotations.Test)

Example 58 with GroovyExpression

use of org.apache.atlas.groovy.GroovyExpression in project incubator-atlas by apache.

the class GremlinExpressionFactory method typeTestExpressionUsingFilter.

private List<GroovyExpression> typeTestExpressionUsingFilter(GraphPersistenceStrategies s, GroovyExpression parent, String typeName) {
    GroovyExpression itExpr = getItVariable();
    GroovyExpression typeTestExpr = typeTestExpression(s, typeName, itExpr);
    GroovyExpression closureExpr = new ClosureExpression(typeTestExpr);
    GroovyExpression filterExpr = new FunctionCallExpression(parent, FILTER_METHOD, closureExpr);
    return Collections.singletonList(filterExpr);
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 59 with GroovyExpression

use of org.apache.atlas.groovy.GroovyExpression in project incubator-atlas by apache.

the class GremlinExpressionFactory method getAggregrationExpression.

private GroovyExpression getAggregrationExpression(GroovyExpression itExpr, GroovyExpression mapFunction, String functionName) {
    GroovyExpression collectionExpr = new CastExpression(itExpr, "Collection");
    ClosureExpression collectFunction = new ClosureExpression(mapFunction);
    GroovyExpression transformedList = new FunctionCallExpression(collectionExpr, "collect", collectFunction);
    return new FunctionCallExpression(transformedList, functionName);
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) CastExpression(org.apache.atlas.groovy.CastExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 60 with GroovyExpression

use of org.apache.atlas.groovy.GroovyExpression in project incubator-atlas by apache.

the class ExpandOrsOptimization method getBaseResultExpression.

/**
     *
     * This method creates a base result expression that recreates the state of the
     * graph traverser at start of the result expression to what it would have been
     * if we had been executing one Gremlin query (instead of many and doing a union).
     *
     * To do this, we start with an anonymous graph traversal that will iterate
     * through the values in the intermediate Set that was created.  We then need
     * to set things up so that the aliases that were in the original gremlin query
     * refer to steps with the correct traverser value.
     *
     * The way we do this depends on the number of aliases.  If there are 0 or 1 alias,
     * the intermediate variable already contains Vertices, so we just create the alias.
     *
     * If there are multiple aliases, the intermediate variable contains a String->Vertex
     * map.  We first create a temporary alias that refers to that map.  For each alias,
     * we use a MapStep to map the map to the Vertex for that alias.  We then add back
     * the alias, making it refer to the MapStep.  Between the alias restorations, we restore the
     * traverser object back to the map.
     *
     * @param context
     * @param aliases
     * @return
     */
private GroovyExpression getBaseResultExpression(OptimizationContext context, List<LiteralExpression> aliases) {
    //Start with an anonymous traversal that gets its objects from the intermediate result variable.
    GroovyExpression parent = factory.generateSeededTraversalExpresssion(aliases.size() > 1, context.getResultVariable());
    if (aliases.isEmpty()) {
        return parent;
    }
    //The expression we will return.
    GroovyExpression result = parent;
    //alias, the save/restore is not needed, so there is no need to create this alias.
    if (aliases.size() > 1) {
        result = factory.generateAliasExpression(result, context.getTempAliasName());
    }
    Iterator<LiteralExpression> it = aliases.iterator();
    while (it.hasNext()) {
        LiteralExpression curAlias = it.next();
        //alias, the intermediate variable will directly contain the vertices.`
        if (factory.isSelectGeneratesMap(aliases.size())) {
            //Since there is more than one alias, the current traverser object is an alias->vertex
            //map.  We use a MapStep to map that map to the Vertex for the current alias.  This sets
            //the current traverser object to that Vertex.  We do this by defining the closure we
            //pass to the MapStep call [map].get(aliasName) where [map] is the expression
            //that refers to the map.
            GroovyExpression rowMapExpr = factory.getCurrentTraverserObject(factory.getClosureArgumentValue());
            GroovyExpression getExpr = factory.generateGetSelectedValueExpression(curAlias, rowMapExpr);
            result = factory.generateMapExpression(result, new ClosureExpression(getExpr));
        }
        //Create alias that points to the previous step.  The traverser value at that step
        //is the Vertex associated with this alias.
        result = factory.generateAliasExpression(result, curAlias.getValue().toString());
        if (it.hasNext()) {
            //Restore the current value of the traverser back to the current alias->vertex map
            result = factory.generateBackReferenceExpression(result, false, context.getTempAliasName());
        }
    }
    return result;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression)

Aggregations

GroovyExpression (org.apache.atlas.groovy.GroovyExpression)80 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)34 Test (org.testng.annotations.Test)33 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)17 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)16 AbstractFunctionExpression (org.apache.atlas.groovy.AbstractFunctionExpression)11 ArrayList (java.util.ArrayList)9 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)6 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)6 CastExpression (org.apache.atlas.groovy.CastExpression)5 FieldExpression (org.apache.atlas.groovy.FieldExpression)5 ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)4 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)4 ListExpression (org.apache.atlas.groovy.ListExpression)3 RangeFinder (org.apache.atlas.gremlin.optimizer.RangeFinder)2 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)2 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)2 StatementListExpression (org.apache.atlas.groovy.StatementListExpression)2 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)2 IDataType (org.apache.atlas.typesystem.types.IDataType)2