Search in sources :

Example 66 with GroovyExpression

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

the class FunctionGenerator method updateCurrentFunctionDefintion.

private void updateCurrentFunctionDefintion(AbstractFunctionExpression exprToAdd) {
    ClosureExpression functionBodyClosure = context.getUserDefinedFunctionBody(currentFunctionName);
    if (functionBodyClosure == null) {
        throw new IllegalStateException("User-defined function " + currentFunctionName + " not found!");
    }
    List<GroovyExpression> exprs = functionBodyClosure.getStatements();
    GroovyExpression currentFunctionBody = exprs.get(exprs.size() - 1);
    //Update the expression so it is called by the current return
    //value of the function.
    exprToAdd.setCaller(currentFunctionBody);
    functionBodyClosure.replaceStatement(exprs.size() - 1, exprToAdd);
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression)

Example 67 with GroovyExpression

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

the class FunctionGenerator method updateCurrentFunction.

/**
     * Adds the caller of parentExpr to the current body of the last
     * function that was created.
     *
     * @param parentExpr
     */
private void updateCurrentFunction(AbstractFunctionExpression parentExpr) {
    GroovyExpression expr = parentExpr.getCaller();
    if (expr instanceof AbstractFunctionExpression) {
        AbstractFunctionExpression exprAsFunction = (AbstractFunctionExpression) expr;
        GroovyExpression exprCaller = exprAsFunction.getCaller();
        parentExpr.setCaller(exprCaller);
        updateCurrentFunctionDefintion(exprAsFunction);
    }
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression)

Example 68 with GroovyExpression

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

the class Gremlin3ExpressionFactory method generateLikeExpressionUsingFilter.

@Override
public GroovyExpression generateLikeExpressionUsingFilter(GroovyExpression parent, String propertyName, GroovyExpression propertyValue) throws AtlasException {
    GroovyExpression itExpr = getItVariable();
    GroovyExpression nameExpr = new FieldExpression(itExpr, propertyName);
    GroovyExpression matchesExpr = new FunctionCallExpression(nameExpr, MATCHES, escapePropertyValue(propertyValue));
    GroovyExpression closureExpr = new ClosureExpression(matchesExpr);
    GroovyExpression filterExpr = new FunctionCallExpression(parent, FILTER_METHOD, closureExpr);
    return filterExpr;
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) FieldExpression(org.apache.atlas.groovy.FieldExpression)

Example 69 with GroovyExpression

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

the class Gremlin3ExpressionFactory method generateGroupByExpression.

@Override
public GroovyExpression generateGroupByExpression(GroovyExpression parent, GroovyExpression groupByExpression, GroovyExpression aggregationFunction) {
    GroovyExpression result = new FunctionCallExpression(TraversalStepType.BARRIER, parent, "group");
    GroovyExpression groupByClosureExpr = new TypeCoersionExpression(new ClosureExpression(groupByExpression), "Function");
    result = new FunctionCallExpression(TraversalStepType.SIDE_EFFECT, result, "by", groupByClosureExpr);
    result = new FunctionCallExpression(TraversalStepType.END, result, "toList");
    GroovyExpression mapValuesClosure = new ClosureExpression(new FunctionCallExpression(new CastExpression(getItVariable(), "Map"), "values"));
    result = new FunctionCallExpression(result, "collect", mapValuesClosure);
    //when we call Map.values(), we end up with an extra list around the result.  We remove this by calling toList().get(0).  This
    //leaves us with a list of lists containing the vertices that match each group.  We then apply the aggregation functions
    //specified in the select list to each of these inner lists.
    result = new FunctionCallExpression(result, "toList");
    result = new FunctionCallExpression(result, "get", new LiteralExpression(0));
    GroovyExpression aggregrationFunctionClosure = new ClosureExpression(aggregationFunction);
    result = new FunctionCallExpression(result, "collect", aggregrationFunctionClosure);
    return result;
}
Also used : TypeCoersionExpression(org.apache.atlas.groovy.TypeCoersionExpression) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) CastExpression(org.apache.atlas.groovy.CastExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 70 with GroovyExpression

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

the class GremlinExpressionFactory method generateTypeTestExpression.

/**
    /**
     * Generates a sequence of groovy expressions that filter the vertices to only
     * those that match the specified type.  If GraphPersistenceStrategies.collectTypeInstancesIntoVar()
     * is set and the gremlin optimizer is disabled, the vertices are put into a variable whose name is generated
     * from the specified IntSequence.  The last item in the result will be a graph traversal restricted to only
     * the matching vertices.
     */
public List<GroovyExpression> generateTypeTestExpression(GraphPersistenceStrategies s, GroovyExpression parent, String typeName, IntSequence intSeq) throws AtlasException {
    if (AtlasRepositoryConfiguration.isGremlinOptimizerEnabled()) {
        GroovyExpression superTypeAttributeNameExpr = new LiteralExpression(s.superTypeAttributeName());
        GroovyExpression typeNameExpr = new LiteralExpression(typeName);
        GroovyExpression superTypeMatchesExpr = new FunctionCallExpression(TraversalStepType.FILTER, HAS_METHOD, superTypeAttributeNameExpr, typeNameExpr);
        GroovyExpression typeAttributeNameExpr = new LiteralExpression(s.typeAttributeName());
        GroovyExpression typeMatchesExpr = new FunctionCallExpression(TraversalStepType.FILTER, HAS_METHOD, typeAttributeNameExpr, typeNameExpr);
        GroovyExpression result = new FunctionCallExpression(TraversalStepType.FILTER, parent, "or", typeMatchesExpr, superTypeMatchesExpr);
        return Collections.singletonList(result);
    } else {
        if (s.filterBySubTypes()) {
            return typeTestExpressionUsingInFilter(s, parent, typeName);
        } else if (s.collectTypeInstancesIntoVar()) {
            return typeTestExpressionMultiStep(s, typeName, intSeq);
        } else {
            return typeTestExpressionUsingFilter(s, parent, typeName);
        }
    }
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

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