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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Step#getPreviousStep() . 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: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static final char processesPropertyType(Step step) {
    while (!(step instanceof EmptyStep)) {
        if (step instanceof FilterStep || step instanceof SideEffectStep)
            step = step.getPreviousStep();
        else if (step instanceof GraphStep && ((GraphStep) step).returnsVertex())
            return 'v';
        else if (step instanceof EdgeVertexStep)
            return 'v';
        else if (step instanceof VertexStep)
            return ((VertexStep) step).returnsVertex() ? 'v' : 'p';
        else if (step instanceof PropertyMapStep || step instanceof PropertiesStep)
            return 'p';
        else
            return 'x';
    }
    return 'x';
}
 
Example 2
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 3
Source File: CountStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private boolean doStrategy(final Step step) {
    if (!(step instanceof CountGlobalStep) ||
            !(step.getNextStep() instanceof IsStep) ||
            step.getPreviousStep() instanceof RangeGlobalStep) // if a RangeStep was provided, assume that the user knows what he's doing
        return false;

    final Step parent = step.getTraversal().getParent().asStep();
    return (parent instanceof FilterStep || parent.getLabels().isEmpty()) && // if the parent is labeled, then the count matters
            !(parent.getNextStep() instanceof MatchStep.MatchEndStep && // if this is in a pattern match, then don't do it.
                    ((MatchStep.MatchEndStep) parent.getNextStep()).getMatchKey().isPresent());
}
 
Example 4
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 5
Source File: VertexProgramStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
protected boolean previousTraversalVertexProgram() {
    Step<?, ?> currentStep = this;
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof TraversalVertexProgramStep)
            return true;
        currentStep = currentStep.getPreviousStep();
    }
    return false;
}
 
Example 6
Source File: MessagePassingReductionStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static final boolean endsWithElement(Step<?, ?> currentStep) {
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof VertexStep) // only inE, in, and out send messages
            return (((VertexStep) currentStep).returnsVertex() || !((VertexStep) currentStep).getDirection().equals(Direction.OUT));
        else if (currentStep instanceof EdgeVertexStep) // TODO: add GraphStep but only if its mid-traversal V()/E()
            return true;
        else if (currentStep instanceof TraversalFlatMapStep || currentStep instanceof TraversalMapStep || currentStep instanceof LocalStep)
            return endsWithElement(((TraversalParent) currentStep).getLocalChildren().get(0).getEndStep());
        else if (!(currentStep instanceof FilterStep || currentStep instanceof SideEffectStep || currentStep instanceof IdentityStep || currentStep instanceof Barrier))
            return false;
        currentStep = currentStep.getPreviousStep();
    }
    return false;
}
 
Example 7
Source File: VertexProgramStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static Step<?, ?> getLastLegalOLAPStep(Step<?, ?> currentStep) {
    while (currentStep instanceof VertexComputing)
        currentStep = currentStep.getNextStep();
    while (!(currentStep instanceof EmptyStep)) {
        if (currentStep instanceof VertexComputing)
            return currentStep.getPreviousStep();
        currentStep = currentStep.getNextStep();
    }
    return EmptyStep.instance();
}
 
Example 8
Source File: HugeCountStepStrategy.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void apply(Traversal.Admin<?, ?> traversal) {
    TraversalUtil.convAllHasSteps(traversal);

    // Extract CountGlobalStep
    List<CountGlobalStep> steps = TraversalHelper.getStepsOfClass(
                                  CountGlobalStep.class, traversal);
    if (steps.isEmpty()) {
        return;
    }

    // Find HugeGraphStep before count()
    CountGlobalStep<?> originStep = steps.get(0);
    List<Step<?, ?>> originSteps = new ArrayList<>();
    HugeGraphStep<?, ? extends Element> graphStep = null;
    Step<?, ?> step = originStep;
    do {
        if (!(step instanceof CountGlobalStep ||
              step instanceof GraphStep ||
              step instanceof IdentityStep ||
              step instanceof NoOpBarrierStep ||
              step instanceof CollectingBarrierStep) ||
             (step instanceof TraversalParent &&
              TraversalHelper.anyStepRecursively(s -> {
                  return s instanceof SideEffectStep ||
                         s instanceof AggregateStep;
              }, (TraversalParent) step))) {
            return;
        }
        originSteps.add(step);
        if (step instanceof HugeGraphStep) {
            graphStep = (HugeGraphStep<?, ? extends Element>) step;
            break;
        }
        step = step.getPreviousStep();
    } while (step != null);

    if (graphStep == null) {
        return;
    }

    // Replace with HugeCountStep
    graphStep.queryInfo().aggregate(AggregateFunc.COUNT, null);
    HugeCountStep<?> countStep = new HugeCountStep<>(traversal, graphStep);
    for (Step<?, ?> origin : originSteps) {
        traversal.removeStep(origin);
    }
    traversal.addStep(0, countStep);
}
 
Example 9
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
/**
 * Sets a {@link Property} value and related meta properties if supplied, if supported by the {@link Graph}
 * and if the {@link Element} is a {@link VertexProperty}.  This method is the long-hand version of
 * {@link #property(Object, Object, Object...)} with the difference that the {@link VertexProperty.Cardinality}
 * can be supplied.
 * <p/>
 * Generally speaking, this method will append an {@link AddPropertyStep} to the {@link Traversal} but when
 * possible, this method will attempt to fold key/value pairs into an {@link AddVertexStep}, {@link AddEdgeStep} or
 * {@link AddVertexStartStep}.  This potential optimization can only happen if cardinality is not supplied
 * and when meta-properties are not included.
 *
 * @param cardinality the specified cardinality of the property where {@code null} will allow the {@link Graph}
 *                    to use its default settings
 * @param key         the key for the property
 * @param value       the value for the property
 * @param keyValues   any meta properties to be assigned to this property
 * @return the traversal with the last step modified to add a property
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#addproperty-step" target="_blank">AddProperty Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> property(final VertexProperty.Cardinality cardinality, final Object key, final Object value, final Object... keyValues) {
    if (null == cardinality)
        this.asAdmin().getBytecode().addStep(Symbols.property, key, value, keyValues);
    else
        this.asAdmin().getBytecode().addStep(Symbols.property, cardinality, key, value, keyValues);

    // if it can be detected that this call to property() is related to an addV/E() then we can attempt to fold
    // the properties into that step to gain an optimization for those graphs that support such capabilities.
    Step endStep = this.asAdmin().getEndStep();

    // always try to fold the property() into the initial "AddElementStep" as the performance will be better
    // and as it so happens with T the value must be set by way of that approach otherwise you get an error.
    // it should be safe to execute this loop this way as we'll either hit an "AddElementStep" or an "EmptyStep".
    // if empty, it will just use the regular AddPropertyStep being tacked on to the end of the traversal as usual
    while (endStep instanceof AddPropertyStep) {
        endStep = endStep.getPreviousStep();
    }

    // edge properties can always be folded as there is no cardinality/metaproperties. for a vertex mutation,
    // it's possible to fold the property() into the Mutating step if there are no metaproperties (i.e. keyValues)
    // and if (1) the key is an instance of T OR OR (3) the key is a string and the cardinality is not specifiied.
    // Note that checking for single cardinality of the argument doesn't work well because once folded we lose
    // the cardinality argument associated to the key/value pair and then it relies on the graph. that
    // means that if you do:
    //
    // g.addV().property(single, 'k',1).property(single,'k',2)
    //
    // you could end up with whatever the cardinality is for the key which might seem "wrong" if you were explicit
    // about the specification of "single". it also isn't possible to check the Graph Features for cardinality
    // as folding seems to have different behavior based on different graphs - we clearly don't have that aspect
    // of things tested/enforced well.
    //
    // of additional note is the folding that occurs if the key is a Traversal. the key here is technically
    // unknown until traversal execution as the anonymous traversal result isn't evaluated during traversal
    // construction but during iteration. not folding to AddVertexStep creates different (breaking) traversal
    // semantics than we've had in previous versions so right/wrong could be argued, but since it's a breaking
    // change we'll just arbitrarily account for it to maintain the former behavior.
    if ((endStep instanceof AddEdgeStep || endStep instanceof AddEdgeStartStep) ||
            ((endStep instanceof AddVertexStep || endStep instanceof AddVertexStartStep) &&
              keyValues.length == 0 &&
              (key instanceof T || (key instanceof String && null == cardinality) || key instanceof Traversal))) {
        ((Mutating) endStep).configure(key, value);
    } else {
        final AddPropertyStep<Element> addPropertyStep = new AddPropertyStep<>(this.asAdmin(), cardinality, key, value);
        this.asAdmin().addStep(addPropertyStep);
        addPropertyStep.configure(keyValues);
    }
    return this;
}