org.apache.tinkerpop.gremlin.process.traversal.step.filter.OrStep Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.filter.OrStep. 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: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static void foldInHasContainer(HasStepFolder janusgraphStep, Traversal.Admin<?, ?> traversal, Traversal<?, ?> rootTraversal) {
    Step<?, ?> currentStep = janusgraphStep.getNextStep();
    while (true) {
        if (currentStep instanceof OrStep && janusgraphStep instanceof JanusGraphStep) {
            for (Traversal.Admin<?, ?> child : ((OrStep<?>) currentStep).getLocalChildren()) {
                if (!validFoldInHasContainer(child.getStartStep(), false)) {
                    return;
                }
            }
            ((OrStep<?>) currentStep).getLocalChildren().forEach(t -> localFoldInHasContainer(janusgraphStep, t.getStartStep(), t, rootTraversal));
            traversal.removeStep(currentStep);
        } else if (currentStep instanceof HasContainerHolder) {
            Iterable<HasContainer> containers = ((HasContainerHolder) currentStep).getHasContainers().stream().map(c -> JanusGraphPredicate.Converter.convert(c)).collect(Collectors.toList());
            if (validFoldInHasContainer(currentStep, true)) {
                janusgraphStep.addAll(containers);
                currentStep.getLabels().forEach(janusgraphStep::addLabel);
                traversal.removeStep(currentStep);
            }
        } else if (!(currentStep instanceof IdentityStep) && !(currentStep instanceof NoOpBarrierStep) && !(currentStep instanceof HasContainerHolder)) {
            break;
        }
        currentStep = currentStep.getNextStep();
    }
}
 
Example #2
Source File: InlineFilterStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    boolean changed = true; // recursively walk child traversals trying to inline them into the current traversal line.
    while (changed) {
        changed = false;
        final Iterator<FilterStep> filterStepIterator = TraversalHelper.getStepsOfAssignableClass(FilterStep.class, traversal).iterator();
        while (!changed && filterStepIterator.hasNext()) {
            final FilterStep<?> step = filterStepIterator.next();
            changed = step instanceof HasStep && InlineFilterStrategy.processHasStep((HasStep) step, traversal) ||
                    step instanceof TraversalFilterStep && InlineFilterStrategy.processTraversalFilterStep((TraversalFilterStep) step, traversal) ||
                    step instanceof OrStep && InlineFilterStrategy.processOrStep((OrStep) step, traversal) ||
                    step instanceof AndStep && InlineFilterStrategy.processAndStep((AndStep) step, traversal);
        }
        if (!changed && traversal.isRoot()) {
            final Iterator<MatchStep> matchStepIterator = TraversalHelper.getStepsOfClass(MatchStep.class, traversal).iterator();
            while (!changed && matchStepIterator.hasNext()) {
                if (InlineFilterStrategy.processMatchStep(matchStepIterator.next(), traversal))
                    changed = true;
            }
        }
    }
}
 
Example #3
Source File: FilterRankingStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Ranks the given step. Steps with lower ranks can be moved in front of steps with higher ranks. 0 means that
 * the step has no rank and thus is not exchangeable with its neighbors.
 *
 * @param step the step to get a ranking for
 * @return The rank of the given step.
 */
private static int getStepRank(final Step step) {
    final int rank;
    if (!(step instanceof FilterStep || step instanceof OrderGlobalStep))
        return 0;
    else if (step instanceof IsStep || step instanceof ClassFilterStep)
        rank = 1;
    else if (step instanceof HasStep)
        rank = 2;
    else if (step instanceof WherePredicateStep && ((WherePredicateStep) step).getLocalChildren().isEmpty())
        rank = 3;
    else if (step instanceof TraversalFilterStep || step instanceof NotStep)
        rank = 4;
    else if (step instanceof WhereTraversalStep)
        rank = 5;
    else if (step instanceof OrStep)
        rank = 6;
    else if (step instanceof AndStep)
        rank = 7;
    else if (step instanceof WherePredicateStep) // has by()-modulation
        rank = 8;
    else if (step instanceof DedupGlobalStep)
        rank = 9;
    else if (step instanceof OrderGlobalStep)
        rank = 10;
    else
        return 0;
    ////////////
    if (step instanceof TraversalParent)
        return getMaxStepRank((TraversalParent) step, rank);
    else
        return rank;
}
 
Example #4
Source File: ConnectiveStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static void processConjunctionMarker(final Class<? extends ConnectiveStep> markerClass, final Traversal.Admin<?, ?> traversal) {

        final List<Step> steps = traversal.getSteps();
        for (int i = 0; i < steps.size(); i++) {
            final Step step = steps.get(i);
            if (step.getClass().equals(markerClass)) {
                final ConnectiveStep<?> currentStep = (ConnectiveStep) step;
                if (currentStep.getLocalChildren().isEmpty()) {
                    Traversal.Admin<?, ?> connectiveTraversal;
                    currentStep.addLocalChild(connectiveTraversal = __.start().asAdmin());
                    for (int j = i - 1; j >= 0; i--, j--) {
                        final Step previousStep = steps.get(j);
                        if (legalCurrentStep(previousStep)) {
                            connectiveTraversal.addStep(0, previousStep);
                            traversal.removeStep(previousStep);
                        } else break;
                    }
                    i++;
                    currentStep.addLocalChild(connectiveTraversal = connectiveTraversal(connectiveTraversal, currentStep));
                    currentStep.getLabels().forEach(currentStep::removeLabel);
                    while (i < steps.size()) {
                        final Step nextStep = steps.get(i);
                        if (legalCurrentStep(nextStep)) {
                            if (nextStep.getClass().equals(markerClass) &&
                                    ((ConnectiveStep) nextStep).getLocalChildren().isEmpty()) {
                                final ConnectiveStep<?> nextConnectiveStep = (ConnectiveStep<?>) nextStep;
                                currentStep.addLocalChild(connectiveTraversal = connectiveTraversal(connectiveTraversal, nextConnectiveStep));
                            } else {
                                connectiveTraversal.addStep(nextStep);
                            }
                            traversal.removeStep(nextStep);
                        } else break;
                    }
                    if (currentStep instanceof OrStep) {
                        currentStep.getLocalChildren().forEach(t -> processConjunctionMarker(AndStep.class, t));
                    }
                }
            }
        }
    }
 
Example #5
Source File: AndOrHasContainer.java    From sqlg with MIT License 5 votes vote down vote up
public static TYPE from(ConnectiveStep<?> connectiveStep) {
    if (connectiveStep instanceof AndStep) {
        return AND;
    } else if (connectiveStep instanceof OrStep) {
        return OR;
    } else {
        return NONE;
    }
}
 
Example #6
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 #7
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 #8
Source File: ConnectiveStrategy.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private static void processConnectiveMarker(final Traversal.Admin<?, ?> traversal) {
    processConjunctionMarker(OrStep.class, traversal);
    processConjunctionMarker(AndStep.class, traversal);
}
 
Example #9
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Ensures that at least one of the provided traversals yield a result.
 *
 * @param orTraversals filter traversals where at least one must be satisfied
 * @return the traversal with an appended {@link OrStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#or-step" target="_blank">Reference Documentation - Or Step</a>
 * @since 3.0.0-incubating
 */
public default GraphTraversal<S, E> or(final Traversal<?, ?>... orTraversals) {
    this.asAdmin().getBytecode().addStep(Symbols.or, orTraversals);
    return this.asAdmin().addStep(new OrStep(this.asAdmin(), orTraversals));
}