Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.Step#getLabels()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Step#getLabels() . 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: TraversalHelper.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static boolean hasLabels(final Traversal.Admin<?, ?> traversal) {
    for (final Step<?, ?> step : traversal.getSteps()) {
        for (final String label : step.getLabels()) {
            if (!Graph.Hidden.isHidden(label))
                return true;
        }
        if (step instanceof TraversalParent) {
            for (final Traversal.Admin<?, ?> local : ((TraversalParent) step).getLocalChildren()) {
                if (TraversalHelper.hasLabels(local))
                    return true;
            }
            for (final Traversal.Admin<?, ?> global : ((TraversalParent) step).getGlobalChildren()) {
                if (TraversalHelper.hasLabels(global))
                    return true;
            }
        }
    }
    return false;
}
 
Example 2
Source File: SqlgWhereStrategy.java    From sqlg with MIT License 6 votes vote down vote up
/**
 * add all labels for the step in the given map
 * @param step
 * @param stepsByLabel the map to fill up
 */
private void captureLabels(Step<?,?> step,Map<String,Object> stepsByLabel){
	for (String s:step.getLabels()){
       	stepsByLabel.put(s, step);
       }
       // labels on replaced steps are not bubbled up to the graphstep
       if (step instanceof SqlgGraphStep<?, ?>){
       	SqlgGraphStep<?, ?> sgs=(SqlgGraphStep<?, ?>)step;
       	for (ReplacedStep<?,?> rs:sgs.getReplacedSteps()){
       		for (String label:rs.getLabels()){
       			 if (label.contains(BaseStrategy.PATH_LABEL_SUFFIX)) {
       				 stepsByLabel.put(label.substring(label.indexOf(BaseStrategy.PATH_LABEL_SUFFIX) + BaseStrategy.PATH_LABEL_SUFFIX.length()),rs);
                    } else if (label.contains(BaseStrategy.EMIT_LABEL_SUFFIX)) {
                   	 stepsByLabel.put(label.substring(label.indexOf(BaseStrategy.EMIT_LABEL_SUFFIX) + BaseStrategy.EMIT_LABEL_SUFFIX.length()),rs);
                    }
       		}
       	}
       }
}
 
Example 3
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
static void foldInIds(HasStepFolder janusgraphStep, Traversal.Admin<?, ?> traversal) {
        Step<?, ?> currentStep = janusgraphStep.getNextStep();
        while (true) {
            if (currentStep instanceof HasContainerHolder) {
                HasContainerHolder hasContainerHolder = (HasContainerHolder) currentStep;
                GraphStep graphStep = (GraphStep) janusgraphStep;
                // HasContainer collection that we get back is immutable so we keep track of which containers
                // need to be deleted after they've been folded into the JanusGraphStep and then remove them from their
                // step using HasContainer.removeHasContainer
                List<HasContainer> removableHasContainers = new ArrayList<>();
                Set<String> stepLabels = currentStep.getLabels();
                hasContainerHolder.getHasContainers().forEach(hasContainer -> {
                    if (GraphStep.processHasContainerIds(graphStep, hasContainer)) {
                        stepLabels.forEach(janusgraphStep::addLabel);
                        // this has container is no longer needed because its ids will be folded into the JanusGraphStep
                        removableHasContainers.add(hasContainer);
                    }
                });

                if (!removableHasContainers.isEmpty()) {
                    removableHasContainers.forEach(hasContainerHolder::removeHasContainer);
                }
                // if all has containers have been removed, the current step can be removed
                if (hasContainerHolder.getHasContainers().isEmpty()) {
                    traversal.removeStep(currentStep);
                }
//            } else if (currentStep instanceof IdentityStep) {
//                // do nothing, has no impact
//            } else if (currentStep instanceof NoOpBarrierStep) {
//                // do nothing, has no impact
            } else {
                break;
            }
            currentStep = currentStep.getNextStep();
        }
    }
 
Example 4
Source File: LP_O_OB_S_SE_SL_Traverser.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public <R> Traverser.Admin<R> split(final R r, final Step<T, R> step) {
    final LP_O_OB_S_SE_SL_Traverser<R> clone = (LP_O_OB_S_SE_SL_Traverser<R>) super.split(r, step);
    clone.path = clone.path.clone();
    final Set<String> labels = step.getLabels();
    if (!labels.isEmpty()) clone.path = clone.path.extend(r, labels);
    return clone;
}
 
Example 5
Source File: B_LP_O_S_SE_SL_Traverser.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public <R> Traverser.Admin<R> split(final R r, final Step<T, R> step) {
    final B_LP_O_S_SE_SL_Traverser<R> clone = (B_LP_O_S_SE_SL_Traverser<R>) super.split(r, step);
    clone.path = clone.path.clone();
    final Set<String> labels = step.getLabels();
    if (!labels.isEmpty()) clone.path = clone.path.extend(r, labels);
    return clone;
}
 
Example 6
Source File: TraversalHelper.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static void copyLabels(final Step<?, ?> fromStep, final Step<?, ?> toStep, final boolean moveLabels) {
    if (!fromStep.getLabels().isEmpty()) {
        for (final String label : moveLabels ? new LinkedHashSet<>(fromStep.getLabels()) : fromStep.getLabels()) {
            toStep.addLabel(label);
            if (moveLabels)
                fromStep.removeLabel(label);
        }
    }
}
 
Example 7
Source File: IncidentToAdjacentStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Optimizes the given edge-emitting step and the vertex-emitting step by replacing them with a single
 * vertex-emitting step.
 *
 * @param traversal the traversal that holds the given steps
 * @param step1     the edge-emitting step to replace
 * @param step2     the vertex-emitting step to replace
 */
private static void optimizeSteps(final Traversal.Admin traversal, final VertexStep step1, final Step step2) {
    final Step newStep = new VertexStep(traversal, Vertex.class, step1.getDirection(), step1.getEdgeLabels());
    for (final String label : (Iterable<String>) step2.getLabels()) {
        newStep.addLabel(label);
    }
    TraversalHelper.replaceStep(step1, newStep, traversal);
    traversal.removeStep(step2);
}
 
Example 8
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 9
Source File: LP_O_OB_S_SE_SL_Traverser.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public LP_O_OB_S_SE_SL_Traverser(final T t, final Step<T, ?> step) {
    super(t, step);
    this.path = ImmutablePath.make();
    final Set<String> labels = step.getLabels();
    if (!labels.isEmpty()) this.path = this.path.extend(t, labels);
}
 
Example 10
Source File: B_LP_O_S_SE_SL_Traverser.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public B_LP_O_S_SE_SL_Traverser(final T t, final Step<T, ?> step, final long initialBulk) {
    super(t, step, initialBulk);
    this.path = ImmutablePath.make();
    final Set<String> labels = step.getLabels();
    if (!labels.isEmpty()) this.path = this.path.extend(t, labels);
}