Search in sources :

Example 1 with ClosureExpression

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

the class Gremlin3ExpressionFactory method generateHasExpression.

@Override
public GroovyExpression generateHasExpression(GraphPersistenceStrategies s, GroovyExpression parent, String propertyName, String symbol, GroovyExpression requiredValue, FieldInfo fInfo) throws AtlasException {
    AttributeInfo attrInfo = fInfo.attrInfo();
    IDataType attrType = attrInfo.dataType();
    GroovyExpression propertNameExpr = new LiteralExpression(propertyName);
    if (s.isPropertyValueConversionNeeded(attrType)) {
        // for some types, the logical value cannot be stored directly in
        // the underlying graph,
        // and conversion logic is needed to convert the persistent form of
        // the value
        // to the actual value. In cases like this, we generate a conversion
        // expression to
        // do this conversion and use the filter step to perform the
        // comparsion in the gremlin query
        GroovyExpression itExpr = getItVariable();
        GroovyExpression vertexExpr = new CastExpression(new FunctionCallExpression(itExpr, GET_METHOD), VERTEX_CLASS);
        GroovyExpression propertyValueExpr = new FunctionCallExpression(vertexExpr, VALUE_METHOD, propertNameExpr);
        GroovyExpression conversionExpr = s.generatePersisentToLogicalConversionExpression(propertyValueExpr, attrType);
        GroovyExpression propertyIsPresentExpression = new FunctionCallExpression(new FunctionCallExpression(vertexExpr, PROPERTY_METHOD, propertNameExpr), IS_PRESENT_METHOD);
        GroovyExpression valueMatchesExpr = new ComparisonExpression(conversionExpr, getGroovyOperator(symbol), requiredValue);
        GroovyExpression filterCondition = new LogicalExpression(propertyIsPresentExpression, LogicalOperator.AND, valueMatchesExpr);
        GroovyExpression filterFunction = new ClosureExpression(filterCondition);
        return new FunctionCallExpression(TraversalStepType.FILTER, parent, FILTER_METHOD, filterFunction);
    } else {
        GroovyExpression valueMatches = new FunctionCallExpression(getComparisonFunction(symbol), requiredValue);
        return new FunctionCallExpression(TraversalStepType.FILTER, parent, HAS_METHOD, propertNameExpr, valueMatches);
    }
}
Also used : AttributeInfo(org.apache.atlas.typesystem.types.AttributeInfo) ComparisonExpression(org.apache.atlas.groovy.ComparisonExpression) LogicalExpression(org.apache.atlas.groovy.LogicalExpression) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) CastExpression(org.apache.atlas.groovy.CastExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) IDataType(org.apache.atlas.typesystem.types.IDataType) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 2 with ClosureExpression

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

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

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

the class Gremlin2ExpressionFactory method generateOrderByExpression.

@Override
public GroovyExpression generateOrderByExpression(GroovyExpression parent, List<GroovyExpression> translatedOrderBy, boolean isAscending) {
    GroovyExpression aPropertyExpr = translatedOrderBy.get(0);
    GroovyExpression bPropertyExpr = translatedOrderBy.get(1);
    GroovyExpression aPropertyNotNull = new ComparisonExpression(aPropertyExpr, ComparisonOperator.NOT_EQUALS, LiteralExpression.NULL);
    GroovyExpression bPropertyNotNull = new ComparisonExpression(bPropertyExpr, ComparisonOperator.NOT_EQUALS, LiteralExpression.NULL);
    GroovyExpression aCondition = new TernaryOperatorExpression(aPropertyNotNull, new FunctionCallExpression(aPropertyExpr, TO_LOWER_CASE_METHOD), aPropertyExpr);
    GroovyExpression bCondition = new TernaryOperatorExpression(bPropertyNotNull, new FunctionCallExpression(bPropertyExpr, TO_LOWER_CASE_METHOD), bPropertyExpr);
    GroovyExpression comparisonFunction = null;
    if (isAscending) {
        comparisonFunction = new ComparisonOperatorExpression(aCondition, bCondition);
    } else {
        comparisonFunction = new ComparisonOperatorExpression(bCondition, aCondition);
    }
    return new FunctionCallExpression(TraversalStepType.BARRIER, parent, ORDER_METHOD, new ClosureExpression(comparisonFunction));
}
Also used : ComparisonExpression(org.apache.atlas.groovy.ComparisonExpression) ComparisonOperatorExpression(org.apache.atlas.groovy.ComparisonOperatorExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) TernaryOperatorExpression(org.apache.atlas.groovy.TernaryOperatorExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 5 with ClosureExpression

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

the class GremlinExpressionFactory method removeExtraMapFromPathInResult.

public GroovyExpression removeExtraMapFromPathInResult(GroovyExpression parent) {
    GroovyExpression listItem = getItVariable();
    GroovyExpression tailExpr = new FunctionCallExpression(listItem, "tail");
    return new FunctionCallExpression(parent, "collect", new ClosureExpression(tailExpr));
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) ClosureExpression(org.apache.atlas.groovy.ClosureExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Aggregations

ClosureExpression (org.apache.atlas.groovy.ClosureExpression)17 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)16 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)14 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)5 CastExpression (org.apache.atlas.groovy.CastExpression)3 ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)3 FieldExpression (org.apache.atlas.groovy.FieldExpression)3 TypeCoersionExpression (org.apache.atlas.groovy.TypeCoersionExpression)3 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)2 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)2 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)2 IDataType (org.apache.atlas.typesystem.types.IDataType)2 ArrayList (java.util.ArrayList)1 VariableDeclaration (org.apache.atlas.groovy.ClosureExpression.VariableDeclaration)1 IdentifierExpression (org.apache.atlas.groovy.IdentifierExpression)1 ListExpression (org.apache.atlas.groovy.ListExpression)1 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)1 VariableAssignmentExpression (org.apache.atlas.groovy.VariableAssignmentExpression)1 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)1