use of org.apache.atlas.groovy.ClosureExpression 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.ClosureExpression 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.ClosureExpression 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;
}
use of org.apache.atlas.groovy.ClosureExpression in project incubator-atlas by apache.
the class OptimizationContext method addFunctionDefinition.
public String addFunctionDefinition(VariableDeclaration decl, GroovyExpression body) {
String functionName = getUniqueFunctionName();
List<VariableDeclaration> decls = (decl == null) ? Collections.<VariableDeclaration>emptyList() : Collections.singletonList(decl);
ClosureExpression bodyClosure = new ClosureExpression(body, decls);
VariableAssignmentExpression expr = new VariableAssignmentExpression(functionName, bodyClosure);
initialStatements.add(expr);
functionBodies.put(functionName, bodyClosure);
return functionName;
}
use of org.apache.atlas.groovy.ClosureExpression in project incubator-atlas by apache.
the class FunctionGenerator method updateCurrentFunctionDefintion.
private void updateCurrentFunctionDefintion(AbstractFunctionExpression exprToAdd) {
ClosureExpression functionBodyClosure = context.getUserDefinedFunctionBody(currentFunctionName);
if (functionBodyClosure == null) {
throw new IllegalStateException("User-defined function " + currentFunctionName + " not found!");
}
List<GroovyExpression> exprs = functionBodyClosure.getStatements();
GroovyExpression currentFunctionBody = exprs.get(exprs.size() - 1);
//Update the expression so it is called by the current return
//value of the function.
exprToAdd.setCaller(currentFunctionBody);
functionBodyClosure.replaceStatement(exprs.size() - 1, exprToAdd);
}
Aggregations