Search in sources :

Example 61 with GroovyExpression

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

the class GremlinQueryOptimizer method visitCallHierarchy.

/**
     * Visits all expressions in the call hierarchy of an expression.  For example,
     * in the expression g.V().has('x','y'), the order would be
     * <ol>
     *    <li>pre-visit has('x','y')</li>
     *    <li>pre-visit V()</li>
     *    <li>visit g (non-function caller)</li>
     *    <li>post-visit V()</li>
     *    <li>post-visit has('x','y')</li>
     * </ol>
     * @param expr
     * @param visitor
     */
public static void visitCallHierarchy(GroovyExpression expr, CallHierarchyVisitor visitor) {
    if (expr == null) {
        visitor.visitNullCaller();
        return;
    }
    if (expr instanceof AbstractFunctionExpression) {
        AbstractFunctionExpression functionCall = (AbstractFunctionExpression) expr;
        if (!visitor.preVisitFunctionCaller(functionCall)) {
            return;
        }
        GroovyExpression caller = functionCall.getCaller();
        visitCallHierarchy(caller, visitor);
        if (!visitor.postVisitFunctionCaller(functionCall)) {
            return;
        }
    } else {
        visitor.visitNonFunctionCaller(expr);
    }
}
Also used : AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

Example 62 with GroovyExpression

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

the class GremlinQueryOptimizer method copyWithNewLeafNode.

/**
     * Recursively copies and follows the caller hierarchy of the expression until we come
     * to a function call with a null caller.  The caller of that expression is set
     * to newLeaf.
     *
     * @param expr
     * @param newLeaf
     * @return the updated (/copied) expression
     */
public static GroovyExpression copyWithNewLeafNode(AbstractFunctionExpression expr, GroovyExpression newLeaf) {
    AbstractFunctionExpression result = (AbstractFunctionExpression) expr.copy();
    //remove leading anonymous traversal expression, if there is one
    if (FACTORY.isLeafAnonymousTraversalExpression(expr)) {
        result = (AbstractFunctionExpression) newLeaf;
    } else {
        GroovyExpression newCaller = null;
        if (expr.getCaller() == null) {
            newCaller = newLeaf;
        } else {
            newCaller = copyWithNewLeafNode((AbstractFunctionExpression) result.getCaller(), newLeaf);
        }
        result.setCaller(newCaller);
    }
    return result;
}
Also used : AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

Example 63 with GroovyExpression

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

the class IsOrParent method apply.

@Override
public Boolean apply(GroovyExpression expr) {
    if (!(expr instanceof AbstractFunctionExpression)) {
        return false;
    }
    AbstractFunctionExpression functionCall = (AbstractFunctionExpression) expr;
    GroovyExpression target = functionCall.getCaller();
    if (!(target instanceof FunctionCallExpression)) {
        return false;
    }
    if (target.getType() != TraversalStepType.FILTER) {
        return false;
    }
    FunctionCallExpression targetFunction = (FunctionCallExpression) target;
    return targetFunction.getFunctionName().equals("or");
}
Also used : AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 64 with GroovyExpression

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

the class OptimizationContext method getDefineResultVariableStmt.

public GroovyExpression getDefineResultVariableStmt() {
    GroovyExpression castExpression = new TypeCoersionExpression(new ListExpression(), "Set");
    GroovyExpression resultVarDef = new VariableAssignmentExpression(RESULT_VARIABLE, castExpression);
    return resultVarDef;
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) ListExpression(org.apache.atlas.groovy.ListExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) VariableAssignmentExpression(org.apache.atlas.groovy.VariableAssignmentExpression)

Example 65 with GroovyExpression

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

the class ExpandOrsOptimization method addCallToUpdateResultVariable.

/**
     * This method adds steps to the end of the initial traversal to add the vertices
     * that were found into an intermediate variable (defined as a Set).  If there is one alias,
     * this set will contain the vertices associated with that Alias.  If there are multiple
     * aliases, the values in the set will be alias->vertex maps that have the vertex
     * associated with the alias for each result.

     * @param expr
     * @param aliasNames
     * @param context
     * @return
     */
private GroovyExpression addCallToUpdateResultVariable(GroovyExpression expr, List<LiteralExpression> aliasNames, OptimizationContext context) {
    GroovyExpression result = expr;
    // If there is one range expression in the unoptimized gremlin,
    // add a range expression here so that the intermediate variable will only contain
    // the specified range of vertices.
    AbstractFunctionExpression rangeExpression = context.getRangeExpression();
    if (rangeExpression != null) {
        int[] rangeParameters = factory.getRangeParameters(rangeExpression);
        result = factory.generateRangeExpression(result, rangeParameters[0], rangeParameters[1]);
    }
    if (!aliasNames.isEmpty()) {
        result = factory.generateSelectExpression(result, aliasNames, Collections.<GroovyExpression>emptyList());
    }
    return factory.generateFillExpression(result, context.getResultVariable());
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression)

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