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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper#copyLabels() . 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: Neo4jGraphStepStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final Neo4jGraphStep<?, ?> neo4jGraphStep = new Neo4jGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, neo4jGraphStep, traversal);
        Step<?, ?> currentStep = neo4jGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(neo4jGraphStep, hasContainer))
                        neo4jGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 3
Source File: TinkerGraphStepStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal);
        Step<?, ?> currentStep = tinkerGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer))
                        tinkerGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 4
Source File: FilterRankingStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    boolean modified = true;
    while (modified) {
        modified = false;
        final List<Step> steps = traversal.getSteps();
        for (int i = 0; i < steps.size() - 1; i++) {
            final Step<?, ?> step = steps.get(i);
            final Step<?, ?> nextStep = step.getNextStep();
            if (!usesLabels(nextStep, step.getLabels())) {
                final int nextRank = getStepRank(nextStep);
                if (nextRank != 0) {
                    if (!step.getLabels().isEmpty()) {
                        TraversalHelper.copyLabels(step, nextStep, true);
                        modified = true;
                    }
                    if (getStepRank(step) > nextRank) {
                        traversal.removeStep(nextStep);
                        traversal.addStep(i, nextStep);
                        modified = true;
                    }
                }
            }
        }
    }
}
 
Example 5
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractHasContainer(HugeGraphStep<?, ?> newStep,
                                       Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof HasStep) {
            HasContainerHolder holder = (HasContainerHolder) step;
            for (HasContainer has : holder.getHasContainers()) {
                if (!GraphStep.processHasContainerIds(newStep, has)) {
                    newStep.addHasContainer(has);
                }
            }
            TraversalHelper.copyLabels(step, step.getPreviousStep(), false);
            traversal.removeStep(step);
        }
    } while (step instanceof HasStep || step instanceof NoOpBarrierStep);
}
 
Example 6
Source File: HBaseGraphStepStrategy.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final HBaseGraphStep<?, ?> hbaseGraphStep = new HBaseGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, hbaseGraphStep, traversal);
        Step<?, ?> currentStep = hbaseGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(hbaseGraphStep, hasContainer))
                        hbaseGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 7
Source File: HBaseVertexStepStrategy.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    for (final VertexStep originalVertexStep : TraversalHelper.getStepsOfClass(VertexStep.class, traversal)) {
        final HBaseVertexStep<?> hbaseVertexStep = new HBaseVertexStep<>(originalVertexStep);
        TraversalHelper.replaceStep(originalVertexStep, hbaseVertexStep, traversal);
        Step<?, ?> currentStep = hbaseVertexStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    hbaseVertexStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 8
Source File: BitsyTraversalStrategy.java    From bitsy with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
	for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
		final BitsyGraphStep<?, ?> bitsyGraphStep = new BitsyGraphStep<>(originalGraphStep);
		TraversalHelper.replaceStep(originalGraphStep, bitsyGraphStep, traversal);
		Step<?, ?> currentStep = bitsyGraphStep.getNextStep();
		while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
			if (currentStep instanceof HasStep) {
				for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
					if (!GraphStep.processHasContainerIds(bitsyGraphStep, hasContainer))
						bitsyGraphStep.addHasContainer(hasContainer);
				}
				TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
				traversal.removeStep(currentStep);
			}
			currentStep = currentStep.getNextStep();
		}
	}
}
 
Example 9
Source File: TinkerGraphStepStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal);
        Step<?, ?> currentStep = tinkerGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer))
                        tinkerGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example 10
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractOrder(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof OrderGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            @SuppressWarnings("resource")
            OrderGlobalStep<?, ?> orderStep = (OrderGlobalStep<?, ?>) step;
            orderStep.getComparators().forEach(comp -> {
                ElementValueComparator<?> comparator =
                        (ElementValueComparator<?>) comp.getValue1();
                holder.orderBy(comparator.getPropertyKey(),
                               (Order) comparator.getValueComparator());
            });
            TraversalHelper.copyLabels(step, newStep, false);
            traversal.removeStep(step);
        }
        step = step.getNextStep();
    } while (step instanceof OrderGlobalStep ||
             step instanceof IdentityStep);
}
 
Example 11
Source File: IdentityRemovalStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (traversal.getSteps().size() <= 1)
        return;

    for (final IdentityStep<?> identityStep : TraversalHelper.getStepsOfClass(IdentityStep.class, traversal)) {
        if (identityStep.getLabels().isEmpty() || !(identityStep.getPreviousStep() instanceof EmptyStep)) {
            TraversalHelper.copyLabels(identityStep, identityStep.getPreviousStep(), false);
            traversal.removeStep(identityStep);
        }
    }
}
 
Example 12
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 13
Source File: EarlyLimitStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {

    final List<Step> steps = traversal.getSteps();
    Step insertAfter = null;
    boolean merge = false;
    for (int i = 0, j = steps.size(); i < j; i++) {
        final Step step = steps.get(i);
        if (step instanceof RangeGlobalStep) {
            if (insertAfter != null) {
                // RangeStep was found, move it to the earliest possible step or merge it with a
                // previous RangeStep; keep the RangeStep's labels at its preceding step
                TraversalHelper.copyLabels(step, step.getPreviousStep(), true);
                insertAfter = moveRangeStep((RangeGlobalStep) step, insertAfter, traversal, merge);
                if (insertAfter instanceof NoneStep) {
                    // any step besides a SideEffectCapStep after a NoneStep would be pointless
                    final int noneStepIndex = TraversalHelper.stepIndex(insertAfter, traversal);
                    for (i = j - 2; i > noneStepIndex; i--) {
                        if (!(steps.get(i) instanceof SideEffectCapStep) && !(steps.get(i) instanceof ProfileSideEffectStep)) {
                            traversal.removeStep(i);
                        }
                    }
                    break;
                }
                j = steps.size();
            }
        } else if (!(step instanceof MapStep || step instanceof SideEffectStep)) {
            // remember the last step that can be used to move any RangeStep to
            // any RangeStep can be moved in front of all its preceding map- and sideEffect-steps
            insertAfter = step;
            merge = true;
        } else if (step instanceof SideEffectCapable) {
            // if there's any SideEffectCapable step along the way, RangeSteps cannot be merged as this could
            // change the final traversal's internal memory
            merge = false;
        }
    }
}
 
Example 14
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static void applyCriterion(final List<Step> stepsToApplyCriterionAfter, final Traversal.Admin traversal,
                                   final Traversal.Admin<? extends Element, ?> criterion) {
    for (final Step<?, ?> step : stepsToApplyCriterionAfter) {
        // re-assign the step label to the criterion because the label should apply seamlessly after the filter
        final Step filter = new TraversalFilterStep<>(traversal, criterion.clone());
        TraversalHelper.insertAfterStep(filter, step, traversal);
        TraversalHelper.copyLabels(step, filter, true);
    }
}
 
Example 15
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static void extractHasContainer(HugeVertexStep<?> newStep,
                                       Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        if (step instanceof HasStep) {
            HasContainerHolder holder = (HasContainerHolder) step;
            for (HasContainer has : holder.getHasContainers()) {
                newStep.addHasContainer(has);
            }
            TraversalHelper.copyLabels(step, step.getPreviousStep(), false);
            traversal.removeStep(step);
        }
        step = step.getNextStep();
    } while (step instanceof HasStep || step instanceof NoOpBarrierStep);
}
 
Example 16
Source File: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private static final boolean processOrStep(final OrStep<?> step, final Traversal.Admin<?, ?> traversal) {
    boolean process = true;
    String key = null;
    P predicate = null;
    final List<String> labels = new ArrayList<>();
    for (final Traversal.Admin<?, ?> childTraversal : step.getLocalChildren()) {
        InlineFilterStrategy.instance().apply(childTraversal); // todo: this may be a bad idea, but I can't seem to find a test case to break it
        for (final Step<?, ?> childStep : childTraversal.getSteps()) {
            if (childStep instanceof HasStep) {
                P p = null;
                for (final HasContainer hasContainer : ((HasStep<?>) childStep).getHasContainers()) {
                    if (null == key)
                        key = hasContainer.getKey();
                    else if (!hasContainer.getKey().equals(key)) {
                        process = false;
                        break;
                    }
                    p = null == p ?
                            hasContainer.getPredicate() :
                            p.and(hasContainer.getPredicate());
                }
                if (process) {
                    predicate = null == predicate ? p : predicate.or(p);
                }
                labels.addAll(childStep.getLabels());
            } else {
                process = false;
                break;
            }
        }
        if (!process)
            break;
    }
    if (process) {
        final HasStep hasStep = new HasStep<>(traversal, new HasContainer(key, predicate));
        TraversalHelper.replaceStep(step, hasStep, traversal);
        TraversalHelper.copyLabels(step, hasStep, false);
        for (final String label : labels) {
            hasStep.addLabel(label);
        }
        return true;
    }
    return false;
}
 
Example 17
Source File: LazyBarrierStrategy.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // drop() is a problem for bulked edge/meta properties because of Property equality changes in TINKERPOP-2318
    // which made it so that a Property is equal if the key/value is equal. as a result, they bulk together which
    // is fine for almost all cases except when you wish to drop the property.
    if (TraversalHelper.onGraphComputer(traversal) ||
            traversal.getTraverserRequirements().contains(TraverserRequirement.PATH) ||
            TraversalHelper.hasStepOfAssignableClass(DropStep.class, traversal))
        return;

    boolean foundFlatMap = false;
    boolean labeledPath = false;
    for (int i = 0; i < traversal.getSteps().size(); i++) {
        final Step<?, ?> step = traversal.getSteps().get(i);

        if (step instanceof PathProcessor) {
            final Set<String> keepLabels = ((PathProcessor) step).getKeepLabels();
            if (null != keepLabels && keepLabels.isEmpty()) // if no more path data, then start barrier'ing again
                labeledPath = false;
        }
        if (step instanceof FlatMapStep &&
                !(step instanceof VertexStep && ((VertexStep) step).returnsEdge()) ||
                (step instanceof GraphStep &&
                        (i > 0 || ((GraphStep) step).getIds().length >= BIG_START_SIZE ||
                                (((GraphStep) step).getIds().length == 0 && !(step.getNextStep() instanceof HasStep))))) {

            // NoneStep, EmptyStep signify the end of the traversal where no barriers are really going to be
            // helpful after that. ProfileSideEffectStep means the traversal had profile() called on it and if
            // we don't account for that a barrier will inject at the end of the traversal where it wouldn't
            // be otherwise. LazyBarrierStrategy executes before the finalization strategy of ProfileStrategy
            // so additionally injected ProfileSideEffectStep instances should not have effect here.
            if (foundFlatMap && !labeledPath &&
                    !(step.getNextStep() instanceof Barrier) &&
                    !(step.getNextStep() instanceof NoneStep) &&
                    !(step.getNextStep() instanceof EmptyStep) &&
                    !(step.getNextStep() instanceof ProfileSideEffectStep)) {
                final Step noOpBarrierStep = new NoOpBarrierStep<>(traversal, MAX_BARRIER_SIZE);
                TraversalHelper.copyLabels(step, noOpBarrierStep, true);
                TraversalHelper.insertAfterStep(noOpBarrierStep, step, traversal);
            } else
                foundFlatMap = true;
        }
        if (!step.getLabels().isEmpty())
            labeledPath = true;

    }
}