Search in sources :

Example 21 with GroovyExpression

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

Example 22 with GroovyExpression

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

the class GremlinExpressionFactory method typeTestExpressionMultiStep.

private List<GroovyExpression> typeTestExpressionMultiStep(GraphPersistenceStrategies s, String typeName, IntSequence intSeq) {
    String varName = "_var_" + intSeq.next();
    GroovyExpression varExpr = new IdentifierExpression(varName);
    List<GroovyExpression> result = new ArrayList<>();
    result.add(newSetVar(varName));
    result.add(fillVarWithTypeInstances(s, typeName, varName));
    result.add(fillVarWithSubTypeInstances(s, typeName, varName));
    result.add(initialExpression(varExpr, s));
    return result;
}
Also used : ArrayList(java.util.ArrayList) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) IdentifierExpression(org.apache.atlas.groovy.IdentifierExpression)

Example 23 with GroovyExpression

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

the class GremlinExpressionFactory method typeTestExpressionUsingInFilter.

private List<GroovyExpression> typeTestExpressionUsingInFilter(GraphPersistenceStrategies s, GroovyExpression parent, final String typeName) throws AtlasException {
    List<GroovyExpression> typeNames = new ArrayList<>();
    typeNames.add(new LiteralExpression(typeName));
    Map<TYPE_FILTER, String> filters = new HashMap<TYPE_FILTER, String>() {

        {
            put(TYPE_FILTER.SUPERTYPE, typeName);
        }
    };
    ImmutableList<String> subTypes = TypeSystem.getInstance().getTypeNames(filters);
    if (!subTypes.isEmpty()) {
        for (String subType : subTypes) {
            typeNames.add(new LiteralExpression(subType));
        }
    }
    GroovyExpression inFilterExpr = generateHasExpression(s, parent, s.typeAttributeName(), IN_OPERATOR, new ListExpression(typeNames), null);
    return Collections.singletonList(inFilterExpr);
}
Also used : HashMap(java.util.HashMap) LiteralExpression(org.apache.atlas.groovy.LiteralExpression) ListExpression(org.apache.atlas.groovy.ListExpression) ArrayList(java.util.ArrayList) GroovyExpression(org.apache.atlas.groovy.GroovyExpression) TYPE_FILTER(org.apache.atlas.typesystem.types.cache.TypeCache.TYPE_FILTER)

Example 24 with GroovyExpression

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

the class GremlinQueryOptimizer method optimize.

/**
     * Optimizes the expression using the given optimization
     * @param source
     * @param optimization
     * @param context
     * @return
     */
private GroovyExpression optimize(GroovyExpression source, GremlinOptimization optimization, OptimizationContext context) {
    GroovyExpression result = source;
    if (optimization.appliesTo(source, context)) {
        //Apply the optimization to the expression.
        result = optimization.apply(source, context);
    }
    if (optimization.isApplyRecursively()) {
        //Visit the children, update result with the optimized
        //children.
        List<GroovyExpression> updatedChildren = new ArrayList<>();
        boolean changed = false;
        for (GroovyExpression child : result.getChildren()) {
            //Recursively optimize this child.
            GroovyExpression updatedChild = optimize(child, optimization, context);
            changed |= updatedChild != child;
            updatedChildren.add(updatedChild);
        }
        if (changed) {
            //TBD - Can we update in place rather than making a copy?
            result = result.copy(updatedChildren);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) GroovyExpression(org.apache.atlas.groovy.GroovyExpression)

Example 25 with GroovyExpression

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

the class AbstractGremlinQueryOptimizerTest method testMultipleAliases.

@Test
public void testMultipleAliases() throws AtlasException {
    GroovyExpression expr1 = makeHasExpression("name", "Fred");
    GroovyExpression expr2 = makeHasExpression("name", "George");
    GroovyExpression expr3 = makeHasExpression("age", "13");
    GroovyExpression expr4 = makeHasExpression("age", "14");
    GroovyExpression toOptimize = getVerticesExpression();
    toOptimize = getFactory().generateLogicalExpression(toOptimize, "or", Arrays.asList(expr1, expr2));
    toOptimize = getFactory().generateAliasExpression(toOptimize, "x");
    toOptimize = getFactory().generateLogicalExpression(toOptimize, "or", Arrays.asList(expr3, expr4));
    toOptimize = getFactory().generateAliasExpression(toOptimize, "y");
    GroovyExpression optimized = GremlinQueryOptimizer.getInstance().optimize(toOptimize);
    assertEquals(optimized.toString(), getExpectedGreminForTestMultipleAliases());
}
Also used : GroovyExpression(org.apache.atlas.groovy.GroovyExpression) Test(org.testng.annotations.Test)

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