Search in sources :

Example 1 with LiteralExpression

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

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

the class ExpandOrsOptimization method moveTransformationsToResultExpression.

private GroovyExpression moveTransformationsToResultExpression(GroovyExpression expr, OptimizationContext context) {
    GroovyExpression traveralExpression = expr;
    // Determine the 'split point'.  This is the expression that will become
    // the deepest function call in the result expression.  If a split
    // point is found, its caller is changed.  The new caller is
    // set to the graph traversal expression in the result expression.
    // The original caller becomes the new traversal expression that
    // will be carried through the rest of the 'or' expansion processing.
    //
    // Example: g.V().has('x').as('x').select('x')
    // Here, select('x') is the split expression
    // so :
    // 1) the result expression in OptimizationContext becomes [base result expression].select('x')
    // 2) we return g.V().has('x').as('x')
    SplitPointFinder finder = new SplitPointFinder(factory);
    GremlinQueryOptimizer.visitCallHierarchy(traveralExpression, finder);
    AbstractFunctionExpression splitPoint = finder.getSplitPoint();
    List<LiteralExpression> aliases = new ArrayList<>();
    //the aliases.
    if (splitPoint != null) {
        traveralExpression = splitPoint.getCaller();
        AliasFinder aliasFinder = new AliasFinder();
        GremlinQueryOptimizer.visitCallHierarchy(traveralExpression, aliasFinder);
        aliases.addAll(aliasFinder.getAliases());
        if (aliasFinder.isFinalAliasNeeded()) {
            //The last alias in the expression does not capture the final vertex in the traverser,
            //so we need to create an alias to record that.
            traveralExpression = factory.generateAliasExpression(traveralExpression, context.getFinalAliasName());
            aliases.add(new LiteralExpression(context.getFinalAliasName()));
        }
        GroovyExpression resultExpr = getBaseResultExpression(context, aliases);
        splitPoint.setCaller(resultExpr);
        expr = removeMapFromPathsIfNeeded(expr, aliases);
        context.setResultExpression(expr);
    }
    //Add expression(s) to the end of the traversal expression to add the vertices
    //that were found into the intermediate variable ('r')
    traveralExpression = addCallToUpdateResultVariable(traveralExpression, aliases, context);
    return traveralExpression;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) ArrayList(java.util.ArrayList) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) AbstractFunctionExpression(org.apache.atlas.groovy.AbstractFunctionExpression)

Example 3 with LiteralExpression

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

the class Gremlin2ExpressionFactory method getAliasesRequiredByExpression.

public List<String> getAliasesRequiredByExpression(GroovyExpression expr) {
    if (!(expr instanceof FunctionCallExpression)) {
        return Collections.emptyList();
    }
    FunctionCallExpression fc = (FunctionCallExpression) expr;
    if (!fc.getFunctionName().equals(LOOP_METHOD)) {
        return Collections.emptyList();
    }
    LiteralExpression aliasName = (LiteralExpression) fc.getArguments().get(0);
    return Collections.singletonList(aliasName.getValue().toString());
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 4 with LiteralExpression

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

the class Gremlin3ExpressionFactory method setRangeParameters.

@Override
public void setRangeParameters(GroovyExpression expr, int startIndex, int endIndex) {
    if (isRangeExpression(expr)) {
        FunctionCallExpression rangeExpression = (FunctionCallExpression) expr;
        rangeExpression.setArgument(0, new LiteralExpression(Integer.valueOf(startIndex)));
        rangeExpression.setArgument(1, new LiteralExpression(Integer.valueOf(endIndex)));
    } else {
        throw new IllegalArgumentException(expr + " is not a valid range expression");
    }
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) FunctionCallExpression(org.apache.atlas.groovy.FunctionCallExpression)

Example 5 with LiteralExpression

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

the class Gremlin3ExpressionFactory method escapePropertyValue.

private GroovyExpression escapePropertyValue(GroovyExpression propertyValue) {
    GroovyExpression ret = propertyValue;
    if (propertyValue instanceof LiteralExpression) {
        LiteralExpression exp = (LiteralExpression) propertyValue;
        if (exp != null && exp.getValue() instanceof String) {
            String stringValue = (String) exp.getValue();
            // replace '*' with ".*", replace '?' with '.'
            stringValue = stringValue.replaceAll("\\*", ".*").replaceAll("\\?", ".");
            ret = new LiteralExpression(stringValue);
        }
    }
    return ret;
}
Also used : LiteralExpression(org.apache.atlas.groovy.LiteralExpression) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

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