Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper#insertTraversal()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper#insertTraversal() . 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
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 2
Source File: SparqlStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.isRoot()))
        return;

    // assumes that the traversal starts with the single inject step that holds the sparql query
    if (traversal.getStartStep() instanceof InjectStep) {
        final InjectStep stepWithSparql = (InjectStep) traversal.getStartStep();
        final Object[] injections = stepWithSparql.getInjections();

        // further assumes that there is just one argument to that injection which is a string (i.e. sparql query)
        if (injections.length == 1 && injections[0] instanceof String) {
            final String sparql = (String) injections[0];

            // try to grab the TraversalSource from the Traversal, but if it's not there then try to the Graph
            // instance and spawn one off from there.
            final Traversal<Vertex, ?> sparqlTraversal = SparqlToGremlinCompiler.compile(
                    (GraphTraversalSource) traversal.getTraversalSource().orElseGet(() -> traversal.getGraph().map(Graph::traversal).get()), sparql);
            TraversalHelper.insertTraversal(stepWithSparql, sparqlTraversal.asAdmin(), traversal);
            traversal.removeStep(stepWithSparql);
        }
    }
}
 
Example 3
Source File: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static final boolean processMatchStep(final MatchStep<?, ?> step, final Traversal.Admin<?, ?> traversal) {
    if (step.getPreviousStep() instanceof EmptyStep)
        return false;
    boolean changed = false;
    final String startLabel = MatchStep.Helper.computeStartLabel(step.getGlobalChildren());
    for (final Traversal.Admin<?, ?> matchTraversal : new ArrayList<>(step.getGlobalChildren())) {
        if (TraversalHelper.hasAllStepsOfClass(matchTraversal,
                HasStep.class,
                MatchStep.MatchStartStep.class,
                MatchStep.MatchEndStep.class) &&
                matchTraversal.getStartStep() instanceof MatchStep.MatchStartStep &&
                startLabel.equals(((MatchStep.MatchStartStep) matchTraversal.getStartStep()).getSelectKey().orElse(null))) {
            changed = true;
            step.removeGlobalChild(matchTraversal);
            final String endLabel = ((MatchStep.MatchEndStep) matchTraversal.getEndStep()).getMatchKey().orElse(null); // why would this exist? but just in case
            matchTraversal.removeStep(0);                                       // remove MatchStartStep
            matchTraversal.removeStep(matchTraversal.getSteps().size() - 1);    // remove MatchEndStep
            TraversalHelper.applySingleLevelStrategies(traversal, matchTraversal, InlineFilterStrategy.class);

            matchTraversal.getEndStep().addLabel(startLabel);
            if (null != endLabel) matchTraversal.getEndStep().addLabel(endLabel);
            TraversalHelper.insertTraversal((Step) step.getPreviousStep(), matchTraversal, traversal);
        }
    }
    if (step.getGlobalChildren().isEmpty())
        traversal.removeStep(step);
    return changed;
}
 
Example 4
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
            }
        }
    }
}