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());
}
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());
}
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);
}
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);
}
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;
}
Aggregations