Search in sources :

Example 1 with StatementListExpression

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

the class ExpandOrsOptimization method apply.

@Override
public GroovyExpression apply(GroovyExpression expr, OptimizationContext context) {
    setupRangeOptimization(expr, context);
    GroovyExpression traveralExpression = moveTransformationsToResultExpression(expr, context);
    FunctionGenerator functionGenerator = new FunctionGenerator(factory, context);
    GremlinQueryOptimizer.visitCallHierarchy(traveralExpression, functionGenerator);
    traveralExpression = functionGenerator.getNewRootExpression();
    List<GroovyExpression> bodyExpressions = expandOrs(traveralExpression, context);
    //Adds a statement to define the result variable 'v' in the
    //groovy script.  The variable is declared as a Set.  The type
    //of the objects in the Set depend on the number of aliases in the Groovy
    // expression:
    //   - 0 or 1 alias : Vertex
    //   - multiple aliases: Map<String,Vertex>
    StatementListExpression result = new StatementListExpression();
    context.prependStatement(context.getDefineResultVariableStmt());
    for (GroovyExpression bodyExpression : bodyExpressions) {
        result.addStatement(bodyExpression);
    }
    result.addStatement(context.getResultExpression());
    return result;
}
Also used : StatementListExpression(org.apache.atlas.groovy.StatementListExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

Example 2 with StatementListExpression

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

the class GremlinQueryOptimizer method optimize.

/**
     * Optimizes the provided groovy expression.  Note that the optimization
     * is a <i>destructive</i> process.  The source GroovyExpression will be
     * modified as part of the optimization process.  This is done to avoid
     * expensive copying operations where possible.
     *
     * @param source what to optimize
     * @return the optimized query
     */
public GroovyExpression optimize(GroovyExpression source) {
    LOGGER.debug("Optimizing gremlin query: " + source);
    OptimizationContext context = new OptimizationContext();
    GroovyExpression updatedExpression = source;
    for (GremlinOptimization opt : optimizations) {
        updatedExpression = optimize(updatedExpression, opt, context);
        LOGGER.debug("After " + opt.getClass().getSimpleName() + ", query = " + updatedExpression);
    }
    StatementListExpression result = new StatementListExpression();
    result.addStatements(context.getInitialStatements());
    result.addStatement(updatedExpression);
    LOGGER.debug("Final optimized query:  " + result.toString());
    return result;
}
Also used : StatementListExpression(org.apache.atlas.groovy.StatementListExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

Aggregations

GroovyExpression (org.apache.atlas.groovy.GroovyExpression)2 StatementListExpression (org.apache.atlas.groovy.StatementListExpression)2