Search in sources :

Example 16 with LiteralExpression

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

the class Gremlin2ExpressionFactory method generateLoopExpression.

@Override
public GroovyExpression generateLoopExpression(GroovyExpression parent, GraphPersistenceStrategies s, IDataType dataType, GroovyExpression loopExpr, String alias, Integer times) {
    GroovyExpression emitExpr = generateLoopEmitExpression(s, dataType);
    //note that in Gremlin 2 (unlike Gremlin 3), the parent is not explicitly used.  It is incorporated
    //in the loopExpr.
    GroovyExpression whileFunction = null;
    if (times != null) {
        GroovyExpression loopsExpr = new FieldExpression(getItVariable(), LOOP_COUNT_FIELD);
        GroovyExpression timesExpr = new LiteralExpression(times);
        whileFunction = new ClosureExpression(new ComparisonExpression(loopsExpr, ComparisonOperator.LESS_THAN, timesExpr));
    } else {
        GroovyExpression pathExpr = new FieldExpression(getItVariable(), PATH_FIELD);
        GroovyExpression itObjectExpr = getCurrentObjectExpression();
        GroovyExpression pathContainsExpr = new FunctionCallExpression(pathExpr, CONTAINS, itObjectExpr);
        whileFunction = new ClosureExpression(new TernaryOperatorExpression(pathContainsExpr, LiteralExpression.FALSE, LiteralExpression.TRUE));
    }
    GroovyExpression emitFunction = new ClosureExpression(emitExpr);
    GroovyExpression loopCall = new FunctionCallExpression(TraversalStepType.BRANCH, loopExpr, LOOP_METHOD, new LiteralExpression(alias), whileFunction, emitFunction);
    return new FunctionCallExpression(TraversalStepType.SIDE_EFFECT, loopCall, ENABLE_PATH_METHOD);
}
Also used : ComparisonExpression(org.apache.atlas.groovy.ComparisonExpression) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) TernaryOperatorExpression(org.apache.atlas.groovy.TernaryOperatorExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) FieldExpression(org.apache.atlas.groovy.FieldExpression)

Example 17 with LiteralExpression

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

the class Gremlin3ExpressionFactory method generateFieldExpression.

@Override
public GroovyExpression generateFieldExpression(GroovyExpression parent, FieldInfo fInfo, String propertyName, boolean inSelect) {
    AttributeInfo attrInfo = fInfo.attrInfo();
    IDataType attrType = attrInfo.dataType();
    GroovyExpression propertyNameExpr = new LiteralExpression(propertyName);
    //Whether it is the user or shared graph does not matter here, since we're
    //just getting the conversion expression.  Ideally that would be moved someplace else.
    AtlasGraph graph = AtlasGraphProvider.getGraphInstance();
    if (inSelect) {
        GroovyExpression expr = new FunctionCallExpression(parent, PROPERTY_METHOD, propertyNameExpr);
        expr = new FunctionCallExpression(expr, OR_ELSE_METHOD, LiteralExpression.NULL);
        return graph.generatePersisentToLogicalConversionExpression(expr, attrType);
    } else {
        GroovyExpression unmapped = new FunctionCallExpression(TraversalStepType.FLAT_MAP_TO_VALUES, parent, VALUES_METHOD, propertyNameExpr);
        if (graph.isPropertyValueConversionNeeded(attrType)) {
            GroovyExpression toConvert = new FunctionCallExpression(getItVariable(), GET_METHOD);
            GroovyExpression conversionFunction = graph.generatePersisentToLogicalConversionExpression(toConvert, attrType);
            return new FunctionCallExpression(TraversalStepType.MAP_TO_VALUE, unmapped, MAP_METHOD, new ClosureExpression(conversionFunction));
        } else {
            return unmapped;
        }
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) IDataType(org.apache.atlas.typesystem.types.IDataType) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 18 with LiteralExpression

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

the class GremlinExpressionFactory method getAliasNameIfRelevant.

/**
     * Checks if the given expression is an alias expression, and if so
     * returns the alias from the expression.  Otherwise, null is
     * returned.
     */
public String getAliasNameIfRelevant(GroovyExpression expr) {
    if (!(expr instanceof FunctionCallExpression)) {
        return null;
    }
    FunctionCallExpression fc = (FunctionCallExpression) expr;
    if (!fc.getFunctionName().equals(AS_METHOD)) {
        return null;
    }
    LiteralExpression aliasName = (LiteralExpression) fc.getArguments().get(0);
    return aliasName.getValue().toString();
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 19 with LiteralExpression

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

the class AliasFinder method postVisitFunctionCaller.

@Override
public boolean postVisitFunctionCaller(AbstractFunctionExpression functionCall) {
    if (functionCall instanceof FunctionCallExpression) {
        FunctionCallExpression expr = (FunctionCallExpression) functionCall;
        if (expr.getType() == TraversalStepType.SIDE_EFFECT && expr.getFunctionName().equals("as")) {
            //We found an alias.  This is currently the last expression we've seen
            //in our traversal back up the expression tree, so at this point a final
            //alias is not needed.
            LiteralExpression aliasNameExpr = (LiteralExpression) expr.getArguments().get(0);
            foundAliases.add(aliasNameExpr);
            finalAliasNeeded = false;
        }
    }
    if (TRANSFORMATION_STEP_TYPES.contains(functionCall.getType())) {
        //needs to be added.
        if (!foundAliases.isEmpty()) {
            finalAliasNeeded = true;
        }
    }
    return true;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 20 with LiteralExpression

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

Aggregations

LiteralExpression (org.apache.atlas.groovy.LiteralExpression)22 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)17 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)15 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)5 ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)3 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)3 ArrayList (java.util.ArrayList)2 AbstractFunctionExpression (org.apache.atlas.groovy.AbstractFunctionExpression)2 CastExpression (org.apache.atlas.groovy.CastExpression)2 FieldExpression (org.apache.atlas.groovy.FieldExpression)2 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)2 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)2 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)2 IDataType (org.apache.atlas.typesystem.types.IDataType)2 Test (org.testng.annotations.Test)2 HashMap (java.util.HashMap)1 RangeFinder (org.apache.atlas.gremlin.optimizer.RangeFinder)1 ListExpression (org.apache.atlas.groovy.ListExpression)1 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)1 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)1