Search in sources :

Example 11 with ClosureExpression

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);
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 12 with ClosureExpression

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);
}
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 13 with ClosureExpression

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;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression)

Example 14 with ClosureExpression

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;
}
Also used : VariableDeclaration(org.apache.atlas.groovy.ClosureExpression.VariableDeclaration) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) VariableAssignmentExpression(org.apache.atlas.groovy.VariableAssignmentExpression)

Example 15 with ClosureExpression

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);
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression)

Aggregations

ClosureExpression (org.apache.atlas.groovy.ClosureExpression)17 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)16 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)14 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)5 CastExpression (org.apache.atlas.groovy.CastExpression)3 ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)3 FieldExpression (org.apache.atlas.groovy.FieldExpression)3 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)3 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)2 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)2 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)2 IDataType (org.apache.atlas.typesystem.types.IDataType)2 ArrayList (java.util.ArrayList)1 VariableDeclaration (org.apache.atlas.groovy.ClosureExpression.VariableDeclaration)1 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)1 ListExpression (org.apache.atlas.groovy.ListExpression)1 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)1 VariableAssignmentExpression (org.apache.atlas.groovy.VariableAssignmentExpression)1 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)1