Search in sources :

Example 1 with IdentifierExpression

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

the class FunctionGenerator method createFunctionIfNeeded.

/**
     * Creates a function whose body goes from the child of parentExpr
     * up to (and including) the functionBodyEndExpr.
     * @param parentExpr
     */
private void createFunctionIfNeeded(AbstractFunctionExpression parentExpr) {
    GroovyExpression potentialFunctionBody = parentExpr.getCaller();
    if (creatingFunctionShortensGremlin(potentialFunctionBody)) {
        GroovyExpression functionCall = null;
        if (nextFunctionBodyStart instanceof AbstractFunctionExpression) {
            //The function body start is a a function call.  In this
            //case, we generate a function that takes one argument, which
            //is a graph traversal.  We have an expression tree that
            //looks kind of like the following:
            //
            //                     parentExpr
            //                       /
            //                      / caller
            //                    |/_
            //           potentialFunctionBody
            //                   /
            //                  / caller
            //                |/_
            //               ...
            //               /
            //              / caller
            //            |/_
            //    nextFunctionBodyStart
            //           /
            //          / caller
            //        |/_
            //    oldCaller
            //
            //
            // Note that potentialFunctionBody and nextFunctionBodyStart
            // could be the same expression.  Let's say that the next
            // function name is f1
            //
            // We reshuffle these expressions to the following:
            //
            //                     parentExpr
            //                       /
            //                      / caller
            //                    |/_
            //                f1(oldCaller)
            //
            //
            //           potentialFunctionBody   <- body of new function "f1(GraphTraversal x)"
            //                   /
            //                  / caller
            //                |/_
            //               ...
            //               /
            //              / caller
            //            |/_
            //    nextFunctionBodyStart
            //           /
            //          / caller
            //        |/_
            //        x
            //
            // As an example, suppose parentExpr is g.V().or(x,y).has(a).has(b).has(c)
            // where has(a) is nextFunctionBodyStart.
            //
            // We generate a function f1 = { GraphTraversal x -> x.has(a).has(b) }
            // parentExpr would become : f1(g.V().or(x,y)).has(c)
            AbstractFunctionExpression nextFunctionBodyStartFunction = (AbstractFunctionExpression) nextFunctionBodyStart;
            String variableName = "x";
            IdentifierExpression var = new IdentifierExpression(variableName);
            GroovyExpression oldCaller = nextFunctionBodyStartFunction.getCaller();
            nextFunctionBodyStartFunction.setCaller(var);
            currentFunctionName = context.addFunctionDefinition(new VariableDeclaration(factory.getTraversalExpressionClass(), "x"), potentialFunctionBody);
            functionCall = new FunctionCallExpression(potentialFunctionBody.getType(), currentFunctionName, oldCaller);
        } else {
            //The function body start is a not a function call.  In this
            //case, we generate a function that takes no arguments.
            // As an example, suppose parentExpr is g.V().has(a).has(b).has(c)
            // where g is nextFunctionBodyStart.
            //
            // We generate a function f1 = { g.V().has(a).has(b) }
            // parentExpr would become : f1().has(c)
            currentFunctionName = context.addFunctionDefinition(null, potentialFunctionBody);
            functionCall = new FunctionCallExpression(potentialFunctionBody.getType(), currentFunctionName);
        }
        //functionBodyEnd is now part of a function definition, don't propagate it
        nextFunctionBodyStart = null;
        parentExpr.setCaller(functionCall);
    }
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression) VariableDeclaration(org.apache.atlas.groovy.ClosureExpression.VariableDeclaration) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 2 with IdentifierExpression

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

the class Gremlin3ExpressionFactory method generateOrderByExpression.

@Override
public GroovyExpression generateOrderByExpression(GroovyExpression parent, List<GroovyExpression> translatedOrderBy, boolean isAscending) {
    GroovyExpression orderByExpr = translatedOrderBy.get(0);
    GroovyExpression orderByClosure = new ClosureExpression(orderByExpr);
    GroovyExpression orderByClause = new TypeCoersionExpression(orderByClosure, FUNCTION_CLASS);
    GroovyExpression aExpr = new IdentifierExpression("a");
    GroovyExpression bExpr = new IdentifierExpression("b");
    GroovyExpression aCompExpr = new FunctionCallExpression(new FunctionCallExpression(aExpr, TO_STRING_METHOD), TO_LOWER_CASE_METHOD);
    GroovyExpression bCompExpr = new FunctionCallExpression(new FunctionCallExpression(bExpr, TO_STRING_METHOD), TO_LOWER_CASE_METHOD);
    GroovyExpression comparisonExpr = null;
    if (isAscending) {
        comparisonExpr = new ComparisonOperatorExpression(aCompExpr, bCompExpr);
    } else {
        comparisonExpr = new ComparisonOperatorExpression(bCompExpr, aCompExpr);
    }
    ClosureExpression comparisonFunction = new ClosureExpression(comparisonExpr, "a", "b");
    FunctionCallExpression orderCall = new FunctionCallExpression(TraversalStepType.BARRIER, parent, ORDER_METHOD);
    return new FunctionCallExpression(TraversalStepType.SIDE_EFFECT, orderCall, BY_METHOD, orderByClause, comparisonFunction);
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) ComparisonOperatorExpression(org.apache.atlas.groovy.ComparisonOperatorExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 3 with IdentifierExpression

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

the class GremlinExpressionFactory method fillVarWithTypeInstances.

private GroovyExpression fillVarWithTypeInstances(GraphPersistenceStrategies s, String typeName, String fillVar) {
    GroovyExpression graphExpr = getAllVerticesExpr();
    GroovyExpression typeAttributeNameExpr = new LiteralExpression(s.typeAttributeName());
    GroovyExpression typeNameExpr = new LiteralExpression(typeName);
    GroovyExpression hasExpr = new FunctionCallExpression(graphExpr, HAS_METHOD, typeAttributeNameExpr, typeNameExpr);
    GroovyExpression fillExpr = new FunctionCallExpression(hasExpr, FILL_METHOD, new IdentifierExpression(fillVar));
    return fillExpr;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 4 with IdentifierExpression

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

the class GremlinExpressionFactory method fillVarWithSubTypeInstances.

private GroovyExpression fillVarWithSubTypeInstances(GraphPersistenceStrategies s, String typeName, String fillVar) {
    GroovyExpression graphExpr = getAllVerticesExpr();
    GroovyExpression superTypeAttributeNameExpr = new LiteralExpression(s.superTypeAttributeName());
    GroovyExpression typeNameExpr = new LiteralExpression(typeName);
    GroovyExpression hasExpr = new FunctionCallExpression(graphExpr, HAS_METHOD, superTypeAttributeNameExpr, typeNameExpr);
    GroovyExpression fillExpr = new FunctionCallExpression(hasExpr, FILL_METHOD, new IdentifierExpression(fillVar));
    return fillExpr;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 5 with IdentifierExpression

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

the class GremlinExpressionFactory method typeTestExpressionMultiStep.

private List<GroovyExpression> typeTestExpressionMultiStep(GraphPersistenceStrategies s, String typeName, IntSequence intSeq) {
    String varName = "_var_" + intSeq.next();
    GroovyExpression varExpr = new IdentifierExpression(varName);
    List<GroovyExpression> result = new ArrayList<>();
    result.add(newSetVar(varName));
    result.add(fillVarWithTypeInstances(s, typeName, varName));
    result.add(fillVarWithSubTypeInstances(s, typeName, varName));
    result.add(initialExpression(varExpr, s));
    return result;
}
Also used : ArrayList(java.util.ArrayList) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Aggregations

GroovyExpression (org.apache.atlas.groovy.GroovyExpression)6 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)6 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)4 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)3 ArrayList (java.util.ArrayList)1 AbstractFunctionExpression (org.apache.atlas.groovy.AbstractFunctionExpression)1 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)1 VariableDeclaration (org.apache.atlas.groovy.ClosureExpression.VariableDeclaration)1 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)1 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)1 Test (org.testng.annotations.Test)1