Search in sources :

Example 1 with TypeCoersionExpression

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

the class Gremlin3ExpressionFactory method getFieldInSelect.

@Override
public GroovyExpression getFieldInSelect() {
    // this logic is needed to remove extra results from
    // what is emitted by repeat loops. Technically
    // for queries that don't have a loop in them we could just use "it"
    // the reason for this is that in repeat loops with an alias,
    // although the alias gets set to the right value, for some
    // reason the select actually includes all vertices that were traversed
    // through in the loop. In these cases, we only want the last vertex
    // traversed in the loop to be selected. The logic here handles that
    // case by converting the result to a list and just selecting the
    // last item from it.
    GroovyExpression itExpr = getItVariable();
    GroovyExpression expr1 = new TypeCoersionExpression(itExpr, VERTEX_ARRAY_CLASS);
    GroovyExpression expr2 = new TypeCoersionExpression(expr1, VERTEX_LIST_CLASS);
    return new FunctionCallExpression(expr2, LAST_METHOD);
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 2 with TypeCoersionExpression

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

the class Gremlin3ExpressionFactory method generateSelectExpression.

@Override
public GroovyExpression generateSelectExpression(GroovyExpression parent, List<LiteralExpression> sourceNames, List<GroovyExpression> srcExprs) {
    FunctionCallExpression result = new FunctionCallExpression(TraversalStepType.MAP_TO_VALUE, parent, SELECT_METHOD, sourceNames);
    for (GroovyExpression expr : srcExprs) {
        GroovyExpression closure = new ClosureExpression(expr);
        GroovyExpression castClosure = new TypeCoersionExpression(closure, FUNCTION_CLASS);
        result = new FunctionCallExpression(TraversalStepType.SIDE_EFFECT, result, BY_METHOD, castClosure);
    }
    return result;
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 3 with TypeCoersionExpression

use of org.apache.atlas.groovy.TypeCoersionExpression 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 4 with TypeCoersionExpression

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

the class Gremlin3ExpressionFactory method initialExpression.

@Override
protected GroovyExpression initialExpression(GroovyExpression varExpr, GraphPersistenceStrategies s) {
    // this bit of groovy magic converts the set of vertices in varName into
    // a String containing the ids of all the vertices. This becomes the
    // argument
    // to g.V(). This is needed because Gremlin 3 does not support
    // _()
    // s"g.V(${varName}.collect{it.id()} as String[])"
    GroovyExpression gExpr = getGraphExpression();
    GroovyExpression varRefExpr = new TypeCoersionExpression(varExpr, OBJECT_ARRAY_CLASS);
    GroovyExpression matchingVerticesExpr = new FunctionCallExpression(TraversalStepType.START, gExpr, V_METHOD, varRefExpr);
    GroovyExpression isEmpty = new FunctionCallExpression(varExpr, "isEmpty");
    GroovyExpression emptyGraph = getEmptyTraversalExpression();
    GroovyExpression expr = new TernaryOperatorExpression(isEmpty, emptyGraph, matchingVerticesExpr);
    return s.addInitialQueryCondition(expr);
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) TernaryOperatorExpression(org.apache.atlas.groovy.TernaryOperatorExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 5 with TypeCoersionExpression

use of org.apache.atlas.groovy.TypeCoersionExpression 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)

Aggregations

GroovyExpression (org.apache.atlas.groovy.GroovyExpression)6 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)6 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)5 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)3 CastExpression (org.apache.atlas.groovy.CastExpression)1 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)1 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)1 ListExpression (org.apache.atlas.groovy.ListExpression)1 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)1 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)1 VariableAssignmentExpression (org.apache.atlas.groovy.VariableAssignmentExpression)1