Search in sources :

Example 1 with ComparisonExpression

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

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

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

the class Gremlin2ExpressionFactory method typeTestExpression.

@Override
public GroovyExpression typeTestExpression(GraphPersistenceStrategies s, String typeName, GroovyExpression itRef) {
    GroovyExpression superTypeAttrExpr = new FieldExpression(itRef, s.superTypeAttributeName());
    GroovyExpression typeNameExpr = new LiteralExpression(typeName);
    GroovyExpression isSuperTypeExpr = new FunctionCallExpression(superTypeAttrExpr, CONTAINS, typeNameExpr);
    GroovyExpression superTypeMatchesExpr = new TernaryOperatorExpression(superTypeAttrExpr, isSuperTypeExpr, LiteralExpression.FALSE);
    GroovyExpression typeAttrExpr = new FieldExpression(itRef, s.typeAttributeName());
    GroovyExpression typeMatchesExpr = new ComparisonExpression(typeAttrExpr, ComparisonOperator.EQUALS, typeNameExpr);
    return new LogicalExpression(typeMatchesExpr, LogicalOperator.OR, superTypeMatchesExpr);
}
Also used : ComparisonExpression(org.apache.atlas.groovy.ComparisonExpression) LogicalExpression(org.apache.atlas.groovy.LogicalExpression) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) TernaryOperatorExpression(org.apache.atlas.groovy.TernaryOperatorExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression) FieldExpression(org.apache.atlas.groovy.FieldExpression)

Example 4 with ComparisonExpression

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

Aggregations

ComparisonExpression (org.apache.atlas.groovy.ComparisonExpression)4 FunctionCallExpression (org.apache.atlas.groovy.FunctionCallExpression)4 GroovyExpression (org.apache.atlas.groovy.GroovyExpression)4 ClosureExpression (org.apache.atlas.groovy.ClosureExpression)3 LiteralExpression (org.apache.atlas.groovy.LiteralExpression)3 TernaryOperatorExpression (org.apache.atlas.groovy.TernaryOperatorExpression)3 FieldExpression (org.apache.atlas.groovy.FieldExpression)2 LogicalExpression (org.apache.atlas.groovy.LogicalExpression)2 CastExpression (org.apache.atlas.groovy.CastExpression)1 ComparisonOperatorExpression (org.apache.atlas.groovy.ComparisonOperatorExpression)1 AttributeInfo (org.apache.atlas.typesystem.types.AttributeInfo)1 IDataType (org.apache.atlas.typesystem.types.IDataType)1