org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.branch.RepeatStep. 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: JanusGraphTraversalUtil.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method searches the traversal for traversal parents which are multiQuery compatible.
 * Being multiQuery compatible is not solely determined by the class of the parent step, it
 * must also have a vertex step as the first step in one of its local or global children.
 *
 * @param traversal The traversal in which to search for multiQuery compatible steps
 * @return A list of traversal parents which were multiQuery compatible
 */
public static List<Step> getMultiQueryCompatibleSteps(Traversal.Admin<?, ?> traversal) {
    final Set<Step> multiQueryCompatibleSteps = new HashSet<>();
    for (Step step : traversal.getSteps()) {
        if (isMultiQueryCompatibleStep(step)) {
            Step parentStep = step;
            ((TraversalParent) parentStep).getGlobalChildren().forEach(childTraversal -> getMultiQueryCompatibleStepsFromChildTraversal(childTraversal, parentStep, multiQueryCompatibleSteps));
            ((TraversalParent) parentStep).getLocalChildren().forEach(childTraversal -> getMultiQueryCompatibleStepsFromChildTraversal(childTraversal, parentStep, multiQueryCompatibleSteps));

            if (parentStep instanceof RepeatStep && multiQueryCompatibleSteps.contains(parentStep)) {
                RepeatStep repeatStep = (RepeatStep) parentStep;
                List<RepeatEndStep> repeatEndSteps = TraversalHelper.getStepsOfClass(RepeatEndStep.class, repeatStep.getRepeatTraversal());
                if (repeatEndSteps.size() == 1) {
                    // Want the RepeatEndStep so the start of one iteration can feed into the next
                    multiQueryCompatibleSteps.remove(parentStep);
                    multiQueryCompatibleSteps.add(repeatEndSteps.get(0));
                }
            }
        }
    }
    return Lists.newArrayList(multiQueryCompatibleSteps);
}
 
Example #2
Source File: TraversalHelper.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static boolean isLocalProperties(final Traversal.Admin<?, ?> traversal) {
    for (final Step step : traversal.getSteps()) {
        if (step instanceof RepeatStep) {
            for (final Traversal.Admin<?, ?> global : ((RepeatStep<?>) step).getGlobalChildren()) {
                if (TraversalHelper.hasStepOfAssignableClass(VertexStep.class, global))
                    return false;
            }
        } else if (step instanceof VertexStep) {
            return false;
        } else if (step instanceof EdgeVertexStep) {
            return false;
        } else if (step instanceof TraversalParent) {
            for (final Traversal.Admin<?, ?> local : ((TraversalParent) step).getLocalChildren()) {
                if (!TraversalHelper.isLocalProperties(local))
                    return false;
            }
        }
    }
    return true;
}
 
Example #3
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIdentifyGlobalChildren() {
    final Traversal.Admin<?, ?> globalChild = __.select("a", "b").by("name").asAdmin();
    TraversalParent parent = new RepeatStep<>(new DefaultTraversal());
    ((RepeatStep) parent).setRepeatTraversal(globalChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
    ///
    new UnionStep<>(new DefaultTraversal(), globalChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
    ///
    new TraversalVertexProgramStep(new DefaultTraversal<>(), globalChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
    ///
    final Traversal.Admin<?, ?> remoteRemoteChild = __.repeat(globalChild).asAdmin();
    new UnionStep<>(new DefaultTraversal(), remoteRemoteChild);
    assertTrue(TraversalHelper.isGlobalChild(globalChild));
}
 
Example #4
Source File: JanusGraphVertexStep.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Many parent traversals drip feed their start vertices in one at a time. To best exploit
 * the multiQuery we need to load all possible starts in one go so this method will attempt
 * to find a JanusGraphMultiQueryStep with the starts of the parent, and if found cache it.
 */
private void setParentMultiQueryStep() {
    Step firstStep = traversal.getStartStep();
    while (firstStep instanceof StartStep || firstStep instanceof SideEffectStep) {
        // Want the next step if this is a side effect
        firstStep = firstStep.getNextStep();
    }
    if (this.equals(firstStep)) {
        Step<?, ?> parentStep = traversal.getParent().asStep();
        if (JanusGraphTraversalUtil.isMultiQueryCompatibleStep(parentStep)) {
            Step<?, ?> parentPreviousStep = parentStep.getPreviousStep();
            if (parentStep instanceof RepeatStep) {
                RepeatStep repeatStep = (RepeatStep) parentStep;
                List<RepeatEndStep> repeatEndSteps = TraversalHelper.getStepsOfClass(RepeatEndStep.class, repeatStep.getRepeatTraversal());
                if (repeatEndSteps.size() == 1) {
                    parentPreviousStep = repeatEndSteps.get(0).getPreviousStep();
                }
            }
            if (parentPreviousStep instanceof ProfileStep) {
                parentPreviousStep = parentPreviousStep.getPreviousStep();
            }
            if (parentPreviousStep instanceof JanusGraphMultiQueryStep) {
                parentMultiQueryStep = (JanusGraphMultiQueryStep) parentPreviousStep;
            }
        }
    }
}
 
Example #5
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Modifies a {@link #repeat(Traversal)} to specify how many loops should occur before exiting.
 *
 * @param maxLoops the number of loops to execute prior to exiting
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> times(final int maxLoops) {
    this.asAdmin().getBytecode().addStep(Symbols.times, maxLoops);
    if (this.asAdmin().getEndStep() instanceof TimesModulating) {
        ((TimesModulating) this.asAdmin().getEndStep()).modulateTimes(maxLoops);
        return this;
    } else
        return RepeatStep.addUntilToTraversal(this, new LoopTraversal<>(maxLoops));
}
 
Example #6
Source File: RepeatUnrollStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (int i = 0; i < traversal.getSteps().size(); i++) {
        if (traversal.getSteps().get(i) instanceof RepeatStep) {
            final RepeatStep<?> repeatStep = (RepeatStep) traversal.getSteps().get(i);
            if (null == repeatStep.getEmitTraversal() && null != repeatStep.getRepeatTraversal() &&
                    repeatStep.getUntilTraversal() instanceof LoopTraversal && ((LoopTraversal) repeatStep.getUntilTraversal()).getMaxLoops() > 0 &&
                    !TraversalHelper.hasStepOfAssignableClassRecursively(Scope.global, DedupGlobalStep.class, repeatStep.getRepeatTraversal()) &&
                    !TraversalHelper.hasStepOfAssignableClassRecursively(INVALIDATING_STEPS, repeatStep.getRepeatTraversal())) {
                final Traversal.Admin<?, ?> repeatTraversal = repeatStep.getGlobalChildren().get(0);
                repeatTraversal.removeStep(repeatTraversal.getSteps().size() - 1); // removes the RepeatEndStep
                TraversalHelper.applySingleLevelStrategies(traversal, repeatTraversal, RepeatUnrollStrategy.class);
                final int repeatLength = repeatTraversal.getSteps().size();
                int insertIndex = i;
                final int loops = (int) ((LoopTraversal) repeatStep.getUntilTraversal()).getMaxLoops();
                for (int j = 0; j < loops; j++) {
                    TraversalHelper.insertTraversal(insertIndex, repeatTraversal.clone(), traversal);
                    insertIndex = insertIndex + repeatLength;
                    if ((j != (loops - 1) || !(traversal.getSteps().get(insertIndex).getNextStep() instanceof Barrier)) // only add a final NoOpBarrier is subsequent step is not a barrier
                        && !(traversal.getSteps().get(insertIndex) instanceof NoOpBarrierStep) // Don't add a barrier if this step is a barrier (prevents nested repeat adding the barrier multiple times)
                       ) {
                        traversal.addStep(++insertIndex, new NoOpBarrierStep<>(traversal, MAX_BARRIER_SIZE));
                    }
                }
                // label last step if repeat() was labeled
                if (!repeatStep.getLabels().isEmpty())
                    TraversalHelper.copyLabels(repeatStep, traversal.getSteps().get(insertIndex), false);
                traversal.removeStep(i); // remove the RepeatStep
            }
        }
    }
}
 
Example #7
Source File: StandardVerificationStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!traversal.getStrategies().getStrategy(ComputerFinalizationStrategy.class).isPresent() &&
            !traversal.getStrategies().getStrategy(ComputerVerificationStrategy.class).isPresent()) {
        if (!TraversalHelper.getStepsOfAssignableClass(VertexComputing.class, traversal).isEmpty())
            throw new VerificationException("VertexComputing steps must be executed with a GraphComputer: " + TraversalHelper.getStepsOfAssignableClass(VertexComputing.class, traversal), traversal);
    }

    for (final Step<?, ?> step : traversal.getSteps()) {
        for (String label : new HashSet<>(step.getLabels())) {
            if (Graph.Hidden.isHidden(label))
                step.removeLabel(label);
        }
        if (step instanceof ReducingBarrierStep && step.getTraversal().getParent() instanceof RepeatStep && step.getTraversal().getParent().getGlobalChildren().get(0).getSteps().contains(step))
            throw new VerificationException("The parent of a reducing barrier can not be repeat()-step: " + step, traversal);
    }

    // The ProfileSideEffectStep must be one of the following
    // (1) the last step
    // (2) 2nd last step when accompanied by the cap step or none step (i.e. iterate() to nothing)
    // (3) 3rd to last when the traversal ends with a RequirementsStep.
    final Step<?, ?> endStep = traversal.asAdmin().getEndStep();
    if (TraversalHelper.hasStepOfClass(ProfileSideEffectStep.class, traversal) &&
            !(endStep instanceof ProfileSideEffectStep ||
                    (endStep instanceof SideEffectCapStep && endStep.getPreviousStep() instanceof ProfileSideEffectStep) ||
                    (endStep instanceof NoneStep && endStep.getPreviousStep() instanceof SideEffectCapStep && endStep.getPreviousStep().getPreviousStep() instanceof ProfileSideEffectStep) ||
                    (endStep instanceof RequirementsStep && endStep.getPreviousStep() instanceof SideEffectCapStep && endStep.getPreviousStep().getPreviousStep() instanceof ProfileSideEffectStep) ||
                    (endStep instanceof RequirementsStep && endStep.getPreviousStep() instanceof NoneStep && endStep.getPreviousStep().getPreviousStep() instanceof SideEffectCapStep && endStep.getPreviousStep().getPreviousStep().getPreviousStep() instanceof ProfileSideEffectStep))) {
        throw new VerificationException("When specified, the profile()-Step must be the last step or followed only by the cap()-step.", traversal);
    }

    if (TraversalHelper.getStepsOfClass(ProfileSideEffectStep.class, traversal).size() > 1) {
        throw new VerificationException("The profile()-Step cannot be specified multiple times.", traversal);
    }
}
 
Example #8
Source File: MutableMetricsSupplier.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public MutableMetricsSupplier(final Step<?, ?> previousStep) {
    this.stepId = previousStep.getId();
    this.stepString =
            previousStep instanceof RepeatStep.RepeatEndStep || previousStep instanceof ComputerAwareStep.EndStep ?
                    previousStep.toString() + " (profiling ignored)" :
                    previousStep.toString();
}
 
Example #9
Source File: TestUnoptimizedRepeatStep.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testRepeatStepWithLimit() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    a2.addEdge("ab", b2);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .until(__.has(T.label, "B"))
            .repeat(__.out())
            .limit(1);

    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(2) instanceof RepeatStep);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof RangeGlobalStep);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgRepeatStepBarrier);
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));

    vertices = this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .repeat(__.out())
            .until(__.has(T.label, "B"))
            .limit(1)
            .toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(b1) || vertices.contains(b2));

}
 
Example #10
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * This step is used for looping over a traversal given some break predicate.
 *
 * @param repeatTraversal the traversal to repeat over
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> repeat(final Traversal<?, E> repeatTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.repeat, repeatTraversal);
    return RepeatStep.addRepeatToTraversal(this, (Traversal.Admin<E, E>) repeatTraversal);
}
 
Example #11
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * This step is used for looping over a traversal given some break predicate and with a specified loop name.
 *
 * @param repeatTraversal the traversal to repeat over
 * @param loopName The name given to the loop
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.4.0
 */
public default GraphTraversal<S, E> repeat(final String loopName, final Traversal<?, E> repeatTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.repeat, loopName, repeatTraversal);
    return RepeatStep.addRepeatToTraversal(this, loopName, (Traversal.Admin<E, E>) repeatTraversal);
}
 
Example #12
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Emit is used in conjunction with {@link #repeat(Traversal)} to determine what objects get emit from the loop.
 *
 * @param emitTraversal the emit predicate defined as a traversal
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> emit(final Traversal<?, ?> emitTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.emit, emitTraversal);
    return RepeatStep.addEmitToTraversal(this, (Traversal.Admin<E, ?>) emitTraversal);
}
 
Example #13
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Emit is used in conjunction with {@link #repeat(Traversal)} to determine what objects get emit from the loop.
 *
 * @param emitPredicate the emit predicate
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> emit(final Predicate<Traverser<E>> emitPredicate) {
    this.asAdmin().getBytecode().addStep(Symbols.emit, emitPredicate);
    return RepeatStep.addEmitToTraversal(this, (Traversal.Admin<E, ?>) __.filter(emitPredicate));
}
 
Example #14
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Emit is used in conjunction with {@link #repeat(Traversal)} to emit all objects from the loop.
 *
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> emit() {
    this.asAdmin().getBytecode().addStep(Symbols.emit);
    return RepeatStep.addEmitToTraversal(this, TrueTraversal.instance());
}
 
Example #15
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Modifies a {@link #repeat(Traversal)} to determine when the loop should exit.
 *
 * @param untilTraversal the traversal that determines when the loop exits
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> until(final Traversal<?, ?> untilTraversal) {
    this.asAdmin().getBytecode().addStep(Symbols.until, untilTraversal);
    return RepeatStep.addUntilToTraversal(this, (Traversal.Admin<E, ?>) untilTraversal);
}
 
Example #16
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Modifies a {@link #repeat(Traversal)} to determine when the loop should exit.
 *
 * @param untilPredicate the predicate that determines when the loop exits
 * @return the traversal with the appended {@link RepeatStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#repeat-step" target="_blank">Reference Documentation - Repeat Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> until(final Predicate<Traverser<E>> untilPredicate) {
    this.asAdmin().getBytecode().addStep(Symbols.until, untilPredicate);
    return RepeatStep.addUntilToTraversal(this, (Traversal.Admin<E, ?>) __.filter(untilPredicate));
}