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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper#getStepsOfAssignableClass() . 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: JanusPreviousPropertyStepStrategy.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void apply(Traversal.Admin<?, ?> traversal) {

    // Retrieve all graph (`V()`) steps - this is the step the strategy should replace
    List<GraphStep> graphSteps = TraversalHelper.getStepsOfAssignableClass(GraphStep.class, traversal);

    for (GraphStep graphStep : graphSteps) {
        // For each graph step, confirm it follows this pattern:
        // `V().filter(__.properties(a).where(P.eq(b)))`

        if (!(graphStep.getNextStep() instanceof TraversalFilterStep)) continue;
        TraversalFilterStep<Vertex> filterStep = (TraversalFilterStep<Vertex>) graphStep.getNextStep();

        // Retrieve the filter steps e.g. `__.properties(a).where(P.eq(b))`
        List<Step> steps = stepsFromFilterStep(filterStep);

        if (steps.size() < 2) continue;
        Step propertiesStep = steps.get(0); // This is `properties(a)`
        Step whereStep = steps.get(1);      // This is `filter(__.where(P.eq(b)))`

        // Get the property key `a`
        if (!(propertiesStep instanceof PropertiesStep)) continue;
        Optional<String> propertyKey = propertyFromPropertiesStep((PropertiesStep<Vertex>) propertiesStep);
        if (!propertyKey.isPresent()) continue;

        // Get the step label `b`
        if (!(whereStep instanceof WherePredicateStep)) continue;
        Optional<String> label = labelFromWhereEqPredicate((WherePredicateStep<Vertex>) whereStep);
        if (!label.isPresent()) continue;

        executeStrategy(traversal, graphStep, filterStep, propertyKey.get(), label.get());
    }
}
 
Example 2
Source File: SeedStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    final List<Seedable> seedableSteps = TraversalHelper.getStepsOfAssignableClass(Seedable.class, traversal);
    for (final Seedable seedableStepsToReset : seedableSteps) {
        seedableStepsToReset.resetSeed(seed);
    }
}
 
Example 3
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 4
Source File: SqlgTraversalFilterStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<TraversalFilterStep> traversalFilterSteps = TraversalHelper.getStepsOfAssignableClass(TraversalFilterStep.class, traversal);
    for (@SuppressWarnings("unchecked") TraversalFilterStep<S> traversalFilterStep : traversalFilterSteps) {

        List<Traversal.Admin<S, ?>> filterTraversals = traversalFilterStep.getLocalChildren();
        Preconditions.checkState(filterTraversals.size() == 1);
        Traversal.Admin<S, ?> filterTraversal = filterTraversals.get(0);

        //reducing barrier steps like count does not work with Sqlg's barrier optimizations
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, filterTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }

        SqlgTraversalFilterStepBarrier sqlgTraversalFilterStepBarrier = new SqlgTraversalFilterStepBarrier<>(
                traversal,
                filterTraversal
        );
        for (String label : traversalFilterStep.getLabels()) {
            sqlgTraversalFilterStepBarrier.addLabel(label);
        }
        //noinspection unchecked
        TraversalHelper.replaceStep(
                traversalFilterStep,
                sqlgTraversalFilterStepBarrier,
                traversalFilterStep.getTraversal()
        );
    }
}
 
Example 5
Source File: SqlgAndStepStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    //noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<AndStep> andSteps = TraversalHelper.getStepsOfAssignableClass(AndStep.class, traversal);
    for (AndStep<S> andStep : andSteps) {

        Collection<Traversal.Admin<S, ?>> andTraversals = andStep.getLocalChildren();

        //reducing barrier steps like count does not work with Sqlg's barrier optimizations
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, traversal);
        if (!reducingBarrierSteps.isEmpty()) {
            return;
        }

        SqlgAndStepBarrier<S> sqlgAndStepBarrier = new SqlgAndStepBarrier(
                traversal,
                andTraversals
        );
        for (String label : andStep.getLabels()) {
            sqlgAndStepBarrier.addLabel(label);
        }
        TraversalHelper.replaceStep(
                andStep,
                sqlgAndStepBarrier,
                andStep.getTraversal()
        );
    }
}
 
Example 6
Source File: SqlgOptionalStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<OptionalStep> optionalSteps = TraversalHelper.getStepsOfAssignableClass(OptionalStep.class, traversal);
    //noinspection unchecked
    for (OptionalStep<S> optionalStep : optionalSteps) {

        //The predicate branch step is a local traversal.
        //As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work.
        Traversal.Admin<S, S> optionalTraversal = optionalStep.getLocalChildren().get(0);

        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, optionalTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }

        SqlgOptionalStepBarrier sqlgOptionalStepBarrier = new SqlgOptionalStepBarrier<>(
                traversal,
                optionalTraversal
        );
        for (String label : optionalStep.getLabels()) {
            sqlgOptionalStepBarrier.addLabel(label);
        }
        //noinspection unchecked
        TraversalHelper.replaceStep(
                optionalStep,
                sqlgOptionalStepBarrier,
                optionalStep.getTraversal()
        );
    }
}
 
Example 7
Source File: SqlgNotStepStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<NotStep> notSteps = TraversalHelper.getStepsOfAssignableClass(NotStep.class, traversal);
    //noinspection unchecked
    for (NotStep<S> notStep : notSteps) {

        //reducing barrier steps like count does not work with Sqlg's barrier optimizations
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, traversal);
        if (!reducingBarrierSteps.isEmpty()) {
            return;
        }

        SqlgNotStepBarrier<S> sqlgNotStepBarrier = new SqlgNotStepBarrier<>(traversal, notStep.getLocalChildren().get(0));
        for (String label : notStep.getLabels()) {
            sqlgNotStepBarrier.addLabel(label);
        }
        TraversalHelper.replaceStep(
                notStep,
                sqlgNotStepBarrier,
                notStep.getTraversal()
        );
    }
}
 
Example 8
Source File: SqlgWhereTraversalStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<WhereTraversalStep> whereTraversalSteps = TraversalHelper.getStepsOfAssignableClass(WhereTraversalStep.class, traversal);
    for (@SuppressWarnings("unchecked") WhereTraversalStep<S> whereTraversalStep : whereTraversalSteps) {

        List<Traversal.Admin<?, ?>> whereTraversals = whereTraversalStep.getLocalChildren();
        Preconditions.checkState(whereTraversals.size() == 1);
        Traversal.Admin<?, ?> whereTraversal = whereTraversals.get(0);

        //reducing barrier steps like count does not work with Sqlg's barrier optimizations
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, whereTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }

        SqlgWhereTraversalStepBarrier sqlgTraversalFilterStepBarrier = new SqlgWhereTraversalStepBarrier<>(
                traversal,
                whereTraversalStep
        );
        for (String label : whereTraversalStep.getLabels()) {
            sqlgTraversalFilterStepBarrier.addLabel(label);
        }
        //noinspection unchecked
        TraversalHelper.replaceStep(
                whereTraversalStep,
                sqlgTraversalFilterStepBarrier,
                whereTraversalStep.getTraversal()
        );
    }
}
 
Example 9
Source File: SqlgOrStepStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<OrStep> orSteps = TraversalHelper.getStepsOfAssignableClass(OrStep.class, traversal);
    for (@SuppressWarnings("unchecked") OrStep<S> orStep : orSteps) {

        Collection<Traversal.Admin<S, ?>> orTraversals = orStep.getLocalChildren();

        //reducing barrier steps like count does not work with Sqlg's barrier optimizations
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, traversal);
        if (!reducingBarrierSteps.isEmpty()) {
            return;
        }

        @SuppressWarnings("unchecked") SqlgOrStepBarrier<S> sqlgOrStepBarrier = new SqlgOrStepBarrier(
                traversal,
                orTraversals
        );
        for (String label : orStep.getLabels()) {
            sqlgOrStepBarrier.addLabel(label);
        }
        TraversalHelper.replaceStep(
                orStep,
                sqlgOrStepBarrier,
                orStep.getTraversal()
        );
    }
}
 
Example 10
Source File: SqlgHasStepStrategy.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<HasStep> hasSteps = TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal);
    for (HasStep<?> hasStep : hasSteps) {
        List<HasContainer> hasContainers = new ArrayList<>(hasStep.getHasContainers());
        SqlgHasStep sqlgHasStep = new SqlgHasStep(
                hasStep.getTraversal(),
                hasContainers.toArray(new HasContainer[]{})
        );
        for (String label : hasStep.getLabels()) {
            sqlgHasStep.addLabel(label);
        }
        //noinspection unchecked
        TraversalHelper.replaceStep(
                hasStep,
                sqlgHasStep,
                hasStep.getTraversal()
        );
    }
}
 
Example 11
Source File: SqlgChooseStepStrategy.java    From sqlg with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    //Only optimize SqlgGraph. StarGraph also passes through here.
    //noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().orElseThrow(IllegalStateException::new) instanceof SqlgGraph)) {
        return;
    }
    if (!SqlgTraversalUtil.mayOptimize(traversal)) {
        return;
    }
    List<ChooseStep> chooseSteps = TraversalHelper.getStepsOfAssignableClass(ChooseStep.class, traversal);
    for (ChooseStep<S, E, M> chooseStep : chooseSteps) {
        Traversal.Admin<S, M> predicateTraversal = chooseStep.getLocalChildren().get(0);

        //The predicate branch step is a local traversal.
        //As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work.
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, predicateTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }

        if (predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1) instanceof HasNextStep) {
            predicateTraversal.removeStep(predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1));
        }

        SqlgBranchStepBarrier sqlgBranchStepBarrier = new SqlgChooseStepBarrier<>(
                traversal,
                predicateTraversal
        );
        for (String label : chooseStep.getLabels()) {
            sqlgBranchStepBarrier.addLabel(label);
        }
        try {
            Field traversalOptionsField = chooseStep.getClass().getSuperclass().getDeclaredField("traversalOptions");
            traversalOptionsField.setAccessible(true);
            Map<M, List<Traversal.Admin<S, E>>> traversalOptions = (Map<M, List<Traversal.Admin<S, E>>>) traversalOptionsField.get(chooseStep);
            for (Map.Entry<M, List<Traversal.Admin<S, E>>> entry : traversalOptions.entrySet()) {
                for (Traversal.Admin<S, E> admin : entry.getValue()) {
                    sqlgBranchStepBarrier.addGlobalChildOption(entry.getKey(), admin);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        TraversalHelper.replaceStep(
                chooseStep,
                sqlgBranchStepBarrier,
                chooseStep.getTraversal()
        );
    }
}