org.apache.tinkerpop.gremlin.process.traversal.step.filter.FilterStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.filter.FilterStep. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    boolean changed = true; // recursively walk child traversals trying to inline them into the current traversal line.
    while (changed) {
        changed = false;
        final Iterator<FilterStep> filterStepIterator = TraversalHelper.getStepsOfAssignableClass(FilterStep.class, traversal).iterator();
        while (!changed && filterStepIterator.hasNext()) {
            final FilterStep<?> step = filterStepIterator.next();
            changed = step instanceof HasStep && InlineFilterStrategy.processHasStep((HasStep) step, traversal) ||
                    step instanceof TraversalFilterStep && InlineFilterStrategy.processTraversalFilterStep((TraversalFilterStep) step, traversal) ||
                    step instanceof OrStep && InlineFilterStrategy.processOrStep((OrStep) step, traversal) ||
                    step instanceof AndStep && InlineFilterStrategy.processAndStep((AndStep) step, traversal);
        }
        if (!changed && traversal.isRoot()) {
            final Iterator<MatchStep> matchStepIterator = TraversalHelper.getStepsOfClass(MatchStep.class, traversal).iterator();
            while (!changed && matchStepIterator.hasNext()) {
                if (InlineFilterStrategy.processMatchStep(matchStepIterator.next(), traversal))
                    changed = true;
            }
        }
    }
}
 
Example #2
Source File: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final boolean processTraversalFilterStep(final TraversalFilterStep<?> step, final Traversal.Admin<?, ?> traversal) {
    final Traversal.Admin<?, ?> childTraversal = step.getLocalChildren().get(0);
    if (TraversalHelper.hasAllStepsOfClass(childTraversal, FilterStep.class) &&
            !TraversalHelper.hasStepOfClass(childTraversal,
                    DropStep.class,
                    RangeGlobalStep.class,
                    DedupGlobalStep.class,
                    LambdaHolder.class)) {
        TraversalHelper.applySingleLevelStrategies(traversal, childTraversal, InlineFilterStrategy.class);
        final Step<?, ?> finalStep = childTraversal.getEndStep();
        TraversalHelper.insertTraversal((Step) step, childTraversal, traversal);
        TraversalHelper.copyLabels(step, finalStep, false);
        traversal.removeStep(step);
        return true;
    }
    return false;
}
 
Example #3
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final char processesPropertyType(Step step) {
    while (!(step instanceof EmptyStep)) {
        if (step instanceof FilterStep || step instanceof SideEffectStep)
            step = step.getPreviousStep();
        else if (step instanceof GraphStep && ((GraphStep) step).returnsVertex())
            return 'v';
        else if (step instanceof EdgeVertexStep)
            return 'v';
        else if (step instanceof VertexStep)
            return ((VertexStep) step).returnsVertex() ? 'v' : 'p';
        else if (step instanceof PropertyMapStep || step instanceof PropertiesStep)
            return 'p';
        else
            return 'x';
    }
    return 'x';
}
 
Example #4
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void extractCount(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof CountGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            holder.setCount();
        }
    } while (step instanceof CountGlobalStep ||
             step instanceof FilterStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}
 
Example #5
Source File: FulgoraUtil.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static void verifyIncidentTraversal(FulgoraElementTraversal<Vertex,Edge> traversal) {
    //First step must be TitanVertexStep
    List<Step> steps = traversal.getSteps();
    Step<Vertex,?> startStep = steps.get(0);
    Preconditions.checkArgument(startStep instanceof TitanVertexStep &&
            TitanTraversalUtil.isEdgeReturnStep((TitanVertexStep) startStep),"Expected first step to be an edge step but found: %s",startStep);
    Optional<Step> violatingStep = steps.stream().filter(s -> !(s instanceof TitanVertexStep ||
            s instanceof OrderGlobalStep || s instanceof OrderLocalStep ||
                    s instanceof IdentityStep || s instanceof FilterStep)).findAny();
    if (violatingStep.isPresent()) throw new IllegalArgumentException("Encountered unsupported step in incident traversal: " + violatingStep.get());
}
 
Example #6
Source File: CountStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private boolean doStrategy(final Step step) {
    if (!(step instanceof CountGlobalStep) ||
            !(step.getNextStep() instanceof IsStep) ||
            step.getPreviousStep() instanceof RangeGlobalStep) // if a RangeStep was provided, assume that the user knows what he's doing
        return false;

    final Step parent = step.getTraversal().getParent().asStep();
    return (parent instanceof FilterStep || parent.getLabels().isEmpty()) && // if the parent is labeled, then the count matters
            !(parent.getNextStep() instanceof MatchStep.MatchEndStep && // if this is in a pattern match, then don't do it.
                    ((MatchStep.MatchEndStep) parent.getNextStep()).getMatchKey().isPresent());
}
 
Example #7
Source File: FilterRankingStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Ranks the given step. Steps with lower ranks can be moved in front of steps with higher ranks. 0 means that
 * the step has no rank and thus is not exchangeable with its neighbors.
 *
 * @param step the step to get a ranking for
 * @return The rank of the given step.
 */
private static int getStepRank(final Step step) {
    final int rank;
    if (!(step instanceof FilterStep || step instanceof OrderGlobalStep))
        return 0;
    else if (step instanceof IsStep || step instanceof ClassFilterStep)
        rank = 1;
    else if (step instanceof HasStep)
        rank = 2;
    else if (step instanceof WherePredicateStep && ((WherePredicateStep) step).getLocalChildren().isEmpty())
        rank = 3;
    else if (step instanceof TraversalFilterStep || step instanceof NotStep)
        rank = 4;
    else if (step instanceof WhereTraversalStep)
        rank = 5;
    else if (step instanceof OrStep)
        rank = 6;
    else if (step instanceof AndStep)
        rank = 7;
    else if (step instanceof WherePredicateStep) // has by()-modulation
        rank = 8;
    else if (step instanceof DedupGlobalStep)
        rank = 9;
    else if (step instanceof OrderGlobalStep)
        rank = 10;
    else
        return 0;
    ////////////
    if (step instanceof TraversalParent)
        return getMaxStepRank((TraversalParent) step, rank);
    else
        return rank;
}
 
Example #8
Source File: MessagePassingReductionStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static final boolean endsWithElement(Step<?, ?> currentStep) {
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof VertexStep) // only inE, in, and out send messages
            return (((VertexStep) currentStep).returnsVertex() || !((VertexStep) currentStep).getDirection().equals(Direction.OUT));
        else if (currentStep instanceof EdgeVertexStep) // TODO: add GraphStep but only if its mid-traversal V()/E()
            return true;
        else if (currentStep instanceof TraversalFlatMapStep || currentStep instanceof TraversalMapStep || currentStep instanceof LocalStep)
            return endsWithElement(((TraversalParent) currentStep).getLocalChildren().get(0).getEndStep());
        else if (!(currentStep instanceof FilterStep || currentStep instanceof SideEffectStep || currentStep instanceof IdentityStep || currentStep instanceof Barrier))
            return false;
        currentStep = currentStep.getPreviousStep();
    }
    return false;
}
 
Example #9
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFindStepOfClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfClass(FilterStep.class, traversal), is(false));
}
 
Example #10
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFindStepOfAssignableClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfAssignableClass(FilterStep.class, traversal), is(true));
}
 
Example #11
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public static void extractAggregateFunc(Step<?, ?> newStep,
                                        Traversal.Admin<?, ?> traversal) {
    PropertiesStep<?> propertiesStep = null;
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof PropertiesStep) {
            @SuppressWarnings("resource")
            PropertiesStep<?> propStep = (PropertiesStep<?>) step;
            if (propStep.getReturnType() == PropertyType.VALUE &&
                propStep.getPropertyKeys().length == 1) {
                propertiesStep = propStep;
            }
        } else if (propertiesStep != null &&
                   step instanceof ReducingBarrierStep) {
            AggregateFunc aggregateFunc;
            if (step instanceof CountGlobalStep) {
                aggregateFunc = AggregateFunc.COUNT;
            } else if (step instanceof MaxGlobalStep) {
                aggregateFunc = AggregateFunc.MAX;
            } else if (step instanceof MinGlobalStep) {
                aggregateFunc = AggregateFunc.MIN;
            } else if (step instanceof MeanGlobalStep) {
                aggregateFunc = AggregateFunc.AVG;
            } else if (step instanceof SumGlobalStep) {
                aggregateFunc = AggregateFunc.SUM;
            } else {
                aggregateFunc = null;
            }

            if (aggregateFunc != null) {
                QueryHolder holder = (QueryHolder) newStep;
                holder.setAggregate(aggregateFunc,
                                    propertiesStep.getPropertyKeys()[0]);
                traversal.removeStep(step);
                traversal.removeStep(propertiesStep);
            }
        }
    } while (step instanceof FilterStep ||
             step instanceof PropertiesStep ||
             step instanceof IdentityStep ||
             step instanceof NoOpBarrierStep);
}