org.apache.tinkerpop.gremlin.process.traversal.step.Scoping Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.Scoping. 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: MatchStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> getScopeKeys() {
    if (null == this.scopeKeys) {
        this.scopeKeys = new HashSet<>();
        this.matchTraversals.forEach(traversal -> {
            if (traversal.getStartStep() instanceof Scoping)
                this.scopeKeys.addAll(((Scoping) traversal.getStartStep()).getScopeKeys());
            if (traversal.getEndStep() instanceof Scoping)
                this.scopeKeys.addAll(((Scoping) traversal.getEndStep()).getScopeKeys());
        });
        this.scopeKeys.removeAll(this.matchEndLabels);
        this.scopeKeys.remove(this.computedStartLabel);
        this.scopeKeys = Collections.unmodifiableSet(this.scopeKeys);
    }
    return this.scopeKeys;
}
 
Example #2
Source File: MatchStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> getScopeKeys() {
    if (null == this.scopeKeys) { // computer the first time and then save resultant keys
        this.scopeKeys = new HashSet<>();
        if (null != this.selectKey)
            this.scopeKeys.add(this.selectKey);
        final Set<String> endLabels = ((MatchStep<?, ?>) this.getTraversal().getParent()).getMatchEndLabels();
        TraversalHelper.anyStepRecursively(step -> {
            if (step instanceof WherePredicateStep || step instanceof WhereTraversalStep) {
                for (final String key : ((Scoping) step).getScopeKeys()) {
                    if (endLabels.contains(key)) this.scopeKeys.add(key);
                }
            }
            return false;
        }, this.getTraversal());
        // this is the old way but it only checked for where() as the next step, not arbitrarily throughout traversal
        // just going to keep this in case something pops up in the future and this is needed as an original reference.
        /* if (this.getNextStep() instanceof WhereTraversalStep || this.getNextStep() instanceof WherePredicateStep)
           this.scopeKeys.addAll(((Scoping) this.getNextStep()).getScopeKeys());*/
        this.scopeKeys = Collections.unmodifiableSet(this.scopeKeys);
    }
    return this.scopeKeys;
}
 
Example #3
Source File: PathUtil.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static Set<String> getReferencedLabels(final Step step) {
    final Set<String> referencedLabels = new HashSet<>();

    if (step instanceof Scoping) {
        final Set<String> labels = new HashSet<>(((Scoping) step).getScopeKeys());
        if (step instanceof MatchStep) {
            // if this is the last step, keep everything, else just add founds
            if (step.getNextStep() instanceof EmptyStep) {
                labels.addAll(((MatchStep) step).getMatchEndLabels());
                labels.addAll(((MatchStep) step).getMatchStartLabels());
            }
        }
        referencedLabels.addAll(labels);

    }

    return referencedLabels;
}
 
Example #4
Source File: FilterRankingStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static boolean usesLabels(final Step<?, ?> step, final Set<String> labels) {
    if (step instanceof LambdaHolder)
        return true;
    if (step instanceof Scoping) {
        final Set<String> scopes = ((Scoping) step).getScopeKeys();
        for (final String label : labels) {
            if (scopes.contains(label))
                return true;
        }
    }
    if (step instanceof TraversalParent) {
        if (TraversalHelper.anyStepRecursively(s -> usesLabels(s, labels), (TraversalParent) step))
            return true;
    }
    return false;
}
 
Example #5
Source File: Parameters.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void addTraversal(final Traversal.Admin t) {
    this.traversals.add(t);
    for (final Object ss : t.getSteps()) {
        if (ss instanceof Scoping) {
            for (String label : ((Scoping) ss).getScopeKeys()) {
                this.referencedLabels.add(label);
            }
        }
    }
}
 
Example #6
Source File: TraversalHelper.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static Set<Scoping.Variable> getVariableLocations(final Set<Scoping.Variable> variables, final Traversal.Admin<?, ?> traversal) {
    if (variables.size() == 2) return variables;    // has both START and END so no need to compute further
    final Step<?, ?> startStep = traversal.getStartStep();
    if (StartStep.isVariableStartStep(startStep))
        variables.add(Scoping.Variable.START);
    else if (startStep instanceof WherePredicateStep) {
        if (((WherePredicateStep) startStep).getStartKey().isPresent())
            variables.add(Scoping.Variable.START);
    } else if (startStep instanceof WhereTraversalStep.WhereStartStep) {
        if (!((WhereTraversalStep.WhereStartStep) startStep).getScopeKeys().isEmpty())
            variables.add(Scoping.Variable.START);
    } else if (startStep instanceof MatchStep.MatchStartStep) {
        if (((MatchStep.MatchStartStep) startStep).getSelectKey().isPresent())
            variables.add(Scoping.Variable.START);
    } else if (startStep instanceof MatchStep) {
        for (final Traversal.Admin<?, ?> global : ((MatchStep<?, ?>) startStep).getGlobalChildren()) {
            TraversalHelper.getVariableLocations(variables, global);
        }
    } else if (startStep instanceof ConnectiveStep || startStep instanceof NotStep || startStep instanceof WhereTraversalStep) {
        for (final Traversal.Admin<?, ?> local : ((TraversalParent) startStep).getLocalChildren()) {
            TraversalHelper.getVariableLocations(variables, local);
        }
    }
    ///
    final Step<?, ?> endStep = traversal.getEndStep();
    if (endStep instanceof WherePredicateStep) {
        if (((WherePredicateStep) endStep).getStartKey().isPresent())
            variables.add(Scoping.Variable.END);
    } else if (endStep instanceof WhereTraversalStep.WhereEndStep) {
        if (!((WhereTraversalStep.WhereEndStep) endStep).getScopeKeys().isEmpty())
            variables.add(Scoping.Variable.END);
    } else if (endStep instanceof MatchStep.MatchEndStep) {
        if (((MatchStep.MatchEndStep) endStep).getMatchKey().isPresent())
            variables.add(Scoping.Variable.END);
    } else if (!endStep.getLabels().isEmpty())
        variables.add(Scoping.Variable.END);
    ///
    return variables;
}
 
Example #7
Source File: StepPositionTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void testEachTraversal(final Traversal.Admin<?, ?> traversal) {
    for (final Step step : traversal.getSteps()) {
        assertTrue(StepPosition.isStepId(step.getId()));
        if (step instanceof SideEffectCapable) {
            assertFalse(StepPosition.isStepId(((SideEffectCapable) step).getSideEffectKey()));
        }
        if (step instanceof Scoping) {
            ((Scoping) step).getScopeKeys().forEach(key -> assertFalse(StepPosition.isStepId(key)));
        }
        if (step instanceof TraversalParent) {
            ((TraversalParent) step).getLocalChildren().forEach(this::testEachTraversal);
            ((TraversalParent) step).getGlobalChildren().forEach(this::testEachTraversal);
        }
    }
}
 
Example #8
Source File: MatchStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public static Set<String> getStartLabels(final Traversal.Admin<Object, Object> traversal) {
    return ((Scoping) traversal.getStartStep()).getScopeKeys();
}
 
Example #9
Source File: WhereTraversalStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public ElementRequirement getMaxRequirement() {
    return TraversalHelper.getVariableLocations(this.whereTraversal).contains(Scoping.Variable.START) ?
            PathProcessor.super.getMaxRequirement() :
            ElementRequirement.ID;
}
 
Example #10
Source File: TraversalHelper.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public static Set<Scoping.Variable> getVariableLocations(final Traversal.Admin<?, ?> traversal) {
    return TraversalHelper.getVariableLocations(EnumSet.noneOf(Scoping.Variable.class), traversal);
}
 
Example #11
Source File: SqlgWhereTraversalStepBarrier.java    From sqlg with MIT License 4 votes vote down vote up
@Override
public ElementRequirement getMaxRequirement() {
    return TraversalHelper.getVariableLocations(this.whereTraversal).contains(Scoping.Variable.START) ?
            PathProcessor.super.getMaxRequirement() :
            ElementRequirement.ID;
}