Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.Traversal#Admin

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Traversal#Admin . 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: MatchStepTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCalculateStartLabelCorrectly() {
    Traversal.Admin<?, ?> traversal = match(
            where(and(
                    as("a").out("created").as("b"),
                    as("b").in("created").count().is(eq(3)))),
            as("a").both().as("b"),
            where(as("b").in())).asAdmin();
    assertEquals("a", MatchStep.Helper.computeStartLabel(((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren()));
    /////
    traversal = match(
            where("a", P.neq("c")),
            as("a").out("created").as("b"),
            or(
                    as("a").out("knows").has("name", "vadas"),
                    as("a").in("knows").and().as("a").has(T.label, "person")
            ),
            as("b").in("created").as("c"),
            as("b").in("created").count().is(P.gt(1))).asAdmin();
    assertEquals("a", MatchStep.Helper.computeStartLabel(((MatchStep<?, ?>) traversal.getStartStep()).getGlobalChildren()));
}
 
Example 2
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 3
Source File: TitanLocalQueryOptimizerStrategy.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private static void unfoldLocalTraversal(final Traversal.Admin<?, ?> traversal,
                                         LocalStep<?,?> localStep, Traversal.Admin localTraversal,
                                         MultiQueriable vstep, boolean useMultiQuery) {
    assert localTraversal.asAdmin().getSteps().size() > 0;
    if (localTraversal.asAdmin().getSteps().size() == 1) {
        //Can replace the entire localStep by the vertex step in the outer traversal
        assert localTraversal.getStartStep() == vstep;
        vstep.setTraversal(traversal);
        TraversalHelper.replaceStep(localStep, vstep, traversal);

        if (useMultiQuery) {
            vstep.setUseMultiQuery(true);
        }
    }
}
 
Example 4
Source File: SubgraphStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private SubgraphStrategy(final Builder builder) {

        this.vertexCriterion = null == builder.vertexCriterion ? null : builder.vertexCriterion.asAdmin().clone();
        this.checkAdjacentVertices = builder.checkAdjacentVertices;

        // if there is no vertex predicate there is no need to test either side of the edge - also this option can
        // be simply configured in the builder to not be used
        if (null == this.vertexCriterion || !checkAdjacentVertices) {
            this.edgeCriterion = null == builder.edgeCriterion ? null : builder.edgeCriterion.asAdmin().clone();
        } else {
            final Traversal.Admin<Edge, ?> vertexPredicate;
            vertexPredicate = __.<Edge>and(
                    __.inV().filter(this.vertexCriterion),
                    __.outV().filter(this.vertexCriterion)).asAdmin();

            // if there is a vertex predicate then there is an implied edge filter on vertices even if there is no
            // edge predicate provided by the user.
            this.edgeCriterion = null == builder.edgeCriterion ?
                    vertexPredicate :
                    builder.edgeCriterion.asAdmin().clone().addStep(new TraversalFilterStep<>(builder.edgeCriterion.asAdmin(), vertexPredicate));
        }

        this.vertexPropertyCriterion = null == builder.vertexPropertyCriterion ? null : builder.vertexPropertyCriterion.asAdmin().clone();

        if (null != this.vertexCriterion)
            TraversalHelper.applyTraversalRecursively(t -> t.getStartStep().addLabel(MARKER), this.vertexCriterion);
        if (null != this.edgeCriterion)
            TraversalHelper.applyTraversalRecursively(t -> t.getStartStep().addLabel(MARKER), this.edgeCriterion);
        if (null != this.vertexPropertyCriterion)
            TraversalHelper.applyTraversalRecursively(t -> t.getStartStep().addLabel(MARKER), this.vertexPropertyCriterion);
    }
 
Example 5
Source File: TraversalHelper.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static boolean onGraphComputer(Traversal.Admin<?, ?> traversal) {
    while (!(traversal.isRoot())) {
        if (traversal.getParent() instanceof TraversalVertexProgramStep)
            return true;
        traversal = traversal.getParent().asStep().getTraversal();
    }
    return false;
}
 
Example 6
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldInsertBeforeStep() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    final HasStep hasStep = new HasStep(traversal);
    final IdentityStep identityStep = new IdentityStep(traversal);
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, hasStep);
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    TraversalHelper.insertBeforeStep(identityStep, hasStep, traversal);

    assertEquals(traversal.asAdmin().getSteps().get(1), identityStep);
    assertEquals(4, traversal.asAdmin().getSteps().size());
}
 
Example 7
Source File: TraversalRing.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public void replaceTraversal(final Traversal.Admin<A, B> oldTraversal, final Traversal.Admin<A, B> newTraversal) {
    for (int i = 0, j = this.traversals.size(); i < j; i++) {
        if (Objects.equals(oldTraversal, this.traversals.get(i))) {
            this.traversals.set(i, newTraversal);
            break;
        }
    }
}
 
Example 8
Source File: GroupStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void setTraversal(final Traversal.Admin<?, ?> parentTraversal) {
    super.setTraversal(parentTraversal);
    integrateChild(this.keyTraversal);
    integrateChild(this.valueTraversal);
}
 
Example 9
Source File: TraversalMatrix.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public TraversalMatrix(final Traversal.Admin<S, E> traversal) {
    this.harvestSteps(this.traversal = traversal);
}
 
Example 10
Source File: StartStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public StartStep(final Traversal.Admin traversal) {
    this(traversal, null);
}
 
Example 11
Source File: LazyBarrierStrategy.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // drop() is a problem for bulked edge/meta properties because of Property equality changes in TINKERPOP-2318
    // which made it so that a Property is equal if the key/value is equal. as a result, they bulk together which
    // is fine for almost all cases except when you wish to drop the property.
    if (TraversalHelper.onGraphComputer(traversal) ||
            traversal.getTraverserRequirements().contains(TraverserRequirement.PATH) ||
            TraversalHelper.hasStepOfAssignableClass(DropStep.class, traversal))
        return;

    boolean foundFlatMap = false;
    boolean labeledPath = false;
    for (int i = 0; i < traversal.getSteps().size(); i++) {
        final Step<?, ?> step = traversal.getSteps().get(i);

        if (step instanceof PathProcessor) {
            final Set<String> keepLabels = ((PathProcessor) step).getKeepLabels();
            if (null != keepLabels && keepLabels.isEmpty()) // if no more path data, then start barrier'ing again
                labeledPath = false;
        }
        if (step instanceof FlatMapStep &&
                !(step instanceof VertexStep && ((VertexStep) step).returnsEdge()) ||
                (step instanceof GraphStep &&
                        (i > 0 || ((GraphStep) step).getIds().length >= BIG_START_SIZE ||
                                (((GraphStep) step).getIds().length == 0 && !(step.getNextStep() instanceof HasStep))))) {

            // NoneStep, EmptyStep signify the end of the traversal where no barriers are really going to be
            // helpful after that. ProfileSideEffectStep means the traversal had profile() called on it and if
            // we don't account for that a barrier will inject at the end of the traversal where it wouldn't
            // be otherwise. LazyBarrierStrategy executes before the finalization strategy of ProfileStrategy
            // so additionally injected ProfileSideEffectStep instances should not have effect here.
            if (foundFlatMap && !labeledPath &&
                    !(step.getNextStep() instanceof Barrier) &&
                    !(step.getNextStep() instanceof NoneStep) &&
                    !(step.getNextStep() instanceof EmptyStep) &&
                    !(step.getNextStep() instanceof ProfileSideEffectStep)) {
                final Step noOpBarrierStep = new NoOpBarrierStep<>(traversal, MAX_BARRIER_SIZE);
                TraversalHelper.copyLabels(step, noOpBarrierStep, true);
                TraversalHelper.insertAfterStep(noOpBarrierStep, step, traversal);
            } else
                foundFlatMap = true;
        }
        if (!step.getLabels().isEmpty())
            labeledPath = true;

    }
}
 
Example 12
Source File: FoldStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public FoldStep(final Traversal.Admin traversal) {
    this(traversal, (Supplier) ArrayListSupplier.instance(), (BiFunction) Operator.addAll);
}
 
Example 13
Source File: MinLocalStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public MinLocalStep(final Traversal.Admin traversal) {
    super(traversal);
}
 
Example 14
Source File: GraphStrategy.java    From sqlg with MIT License 4 votes vote down vote up
private GraphStrategy(Traversal.Admin<?, ?> traversal) {
    super(traversal);
}
 
Example 15
Source File: TraversalFilterStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public TraversalFilterStep(final Traversal.Admin traversal, final Traversal<S, ?> filterTraversal) {
    super(traversal);
    this.filterTraversal = this.integrateChild(filterTraversal.asAdmin());
}
 
Example 16
Source File: OrderGlobalStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public List<Traversal.Admin<S, C>> getLocalChildren() {
    return (List) this.comparators.stream().map(Pair::getValue0).collect(Collectors.toList());
}
 
Example 17
Source File: ProjectStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void modulateBy(final Traversal.Admin<?, ?> selectTraversal) {
    this.traversalRing.addTraversal(this.integrateChild(selectTraversal));
}
 
Example 18
Source File: PureTraversal.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public Traversal.Admin<S, E> getPure() {
    return this.pureTraversal.clone();
}
 
Example 19
Source File: AdjacentVertexFilterOptimizerStrategy.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {

    TraversalHelper.getStepsOfClass(TraversalFilterStep.class, traversal).forEach(originalStep -> {
        // Check if this filter traversal matches the pattern: _.inV/outV/otherV.is(x)
        Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) originalStep.getLocalChildren().get(0);
        List<Step> steps = filterTraversal.getSteps();
        if (steps.size() == 2 &&
                (steps.get(0) instanceof EdgeVertexStep || steps.get(0) instanceof EdgeOtherVertexStep) &&
                (steps.get(1) instanceof IsStep)) {
            //Get the direction in which we filter on the adjacent vertex (or null if not a valid adjacency filter)
            Direction direction = null;
            if (steps.get(0) instanceof EdgeVertexStep) {
                EdgeVertexStep evs = (EdgeVertexStep) steps.get(0);
                if (evs.getDirection() != Direction.BOTH) direction = evs.getDirection();
            } else {
                assert steps.get(0) instanceof EdgeOtherVertexStep;
                direction = Direction.BOTH;
            }
            P predicate = ((IsStep) steps.get(1)).getPredicate();
            //Check that we have a valid direction and a valid vertex filter predicate
            if (direction != null && predicate.getBiPredicate() == Compare.eq && predicate.getValue() instanceof Vertex) {
                TitanVertex vertex = TitanTraversalUtil.getTitanVertex((Vertex) predicate.getValue());

                //Now, check that this step is preceeded by VertexStep that returns edges
                Step<?, ?> currentStep = originalStep.getPreviousStep();
                while (true) {
                    if (currentStep instanceof HasStep || currentStep instanceof IdentityStep) {
                        //We can jump over those steps as we move backward
                    } else break;
                }
                if (currentStep instanceof VertexStep) {
                    VertexStep vstep = (VertexStep) currentStep;
                    if (vstep.returnsEdge()
                            && (direction == Direction.BOTH || direction.equals(vstep.getDirection().opposite()))) {
                        //Now replace the step with a has condition
                        TraversalHelper.replaceStep(originalStep,
                                new HasStep(traversal,
                                        HasContainer.makeHasContainers(ImplicitKey.ADJACENT_ID.name(), P.eq(vertex))),
                                traversal);
                    }
                }

            }
        }

    });

}
 
Example 20
Source File: PathFilterStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public PathFilterStep(final Traversal.Admin traversal, final boolean isSimple) {
    super(traversal);
    this.traversalRing = new TraversalRing<>();
    this.isSimple = isSimple;
}