org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper. 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: JanusGraphTraversalUtil.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
public static JanusGraphTransaction getTx(Traversal.Admin<?, ?> traversal) {
    JanusGraphTransaction tx;
    Optional<Graph> optGraph = TraversalHelper.getRootTraversal(traversal.asAdmin()).getGraph();

    if (!optGraph.isPresent()) {
        throw new IllegalArgumentException("Traversal is not bound to a graph: " + traversal);
    }
    Graph graph = optGraph.get();
    if (graph instanceof JanusGraphTransaction) {
        tx = (JanusGraphTransaction) graph;
    } else {
        throw new IllegalArgumentException("Traversal is not bound to a JanusGraph Graph, but: " + graph);
    }

    return tx;
}
 
Example #2
Source File: TinkerGraphCountStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.getParent() instanceof EmptyStep) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #3
Source File: TinkerGraphCountStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!(traversal.isRoot()) || TraversalHelper.onGraphComputer(traversal))
        return;
    final List<Step> steps = traversal.getSteps();
    if (steps.size() < 2 ||
            !(steps.get(0) instanceof GraphStep) ||
            0 != ((GraphStep) steps.get(0)).getIds().length ||
            !(steps.get(steps.size() - 1) instanceof CountGlobalStep))
        return;
    for (int i = 1; i < steps.size() - 1; i++) {
        final Step current = steps.get(i);
        if (!(//current instanceof MapStep ||  // MapSteps will not necessarily emit an element as demonstrated in https://issues.apache.org/jira/browse/TINKERPOP-1958
                current instanceof IdentityStep ||
                current instanceof NoOpBarrierStep ||
                current instanceof CollectingBarrierStep) ||
                (current instanceof TraversalParent &&
                        TraversalHelper.anyStepRecursively(s -> (s instanceof SideEffectStep || s instanceof AggregateGlobalStep), (TraversalParent) current)))
            return;
    }
    final Class<? extends Element> elementClass = ((GraphStep<?, ?>) steps.get(0)).getReturnClass();
    TraversalHelper.removeAllSteps(traversal);
    traversal.addStep(new TinkerCountGlobalStep<>(traversal, elementClass));
}
 
Example #4
Source File: TinkerGraphStepStrategy.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.onGraphComputer(traversal))
        return;

    for (final GraphStep originalGraphStep : TraversalHelper.getStepsOfClass(GraphStep.class, traversal)) {
        final TinkerGraphStep<?, ?> tinkerGraphStep = new TinkerGraphStep<>(originalGraphStep);
        TraversalHelper.replaceStep(originalGraphStep, tinkerGraphStep, traversal);
        Step<?, ?> currentStep = tinkerGraphStep.getNextStep();
        while (currentStep instanceof HasStep || currentStep instanceof NoOpBarrierStep) {
            if (currentStep instanceof HasStep) {
                for (final HasContainer hasContainer : ((HasContainerHolder) currentStep).getHasContainers()) {
                    if (!GraphStep.processHasContainerIds(tinkerGraphStep, hasContainer))
                        tinkerGraphStep.addHasContainer(hasContainer);
                }
                TraversalHelper.copyLabels(currentStep, currentStep.getPreviousStep(), false);
                traversal.removeStep(currentStep);
            }
            currentStep = currentStep.getNextStep();
        }
    }
}
 
Example #5
Source File: HugeVertexStepStrategy.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void apply(final Traversal.Admin<?, ?> traversal) {
    TraversalUtil.convAllHasSteps(traversal);

    List<VertexStep> steps = TraversalHelper.getStepsOfClass(
                             VertexStep.class, traversal);
    for (VertexStep originStep : steps) {
        HugeVertexStep<?> newStep = new HugeVertexStep<>(originStep);
        TraversalHelper.replaceStep(originStep, newStep, traversal);

        TraversalUtil.extractHasContainer(newStep, traversal);

        // TODO: support order-by optimize
        // TraversalUtil.extractOrder(newStep, traversal);

        TraversalUtil.extractRange(newStep, traversal, true);

        TraversalUtil.extractCount(newStep, traversal);
    }
}
 
Example #6
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractOrder(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof OrderGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            @SuppressWarnings("resource")
            OrderGlobalStep<?, ?> orderStep = (OrderGlobalStep<?, ?>) step;
            orderStep.getComparators().forEach(comp -> {
                ElementValueComparator<?> comparator =
                        (ElementValueComparator<?>) comp.getValue1();
                holder.orderBy(comparator.getPropertyKey(),
                               (Order) comparator.getValueComparator());
            });
            TraversalHelper.copyLabels(step, newStep, false);
            traversal.removeStep(step);
        }
        step = step.getNextStep();
    } while (step instanceof OrderGlobalStep ||
             step instanceof IdentityStep);
}
 
Example #7
Source File: TestOrStepBarrier.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrStepBarrier() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a2.addEdge("abb", b2);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    a3.addEdge("abbb", b3);


    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .or(
                    __.out("ab"),
                    __.out("abb")
            );
    printTraversalForm(traversal);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.containsAll(Arrays.asList(a1, a2)));
    List<SqlgOrStepBarrier> sqlgOrStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgOrStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgOrStepBarriers.size());
}
 
Example #8
Source File: TraversalVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void workerIterationEnd(final Memory memory) {
    // store profile metrics in proper ProfileStep metrics
    if (this.profile) {
        final List<ProfileStep> profileSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ProfileStep.class, this.traversal.get());
        // guess the profile step to store data
        int profileStepIndex = memory.getIteration();
        // if we guess wrongly write timing into last step
        profileStepIndex = profileStepIndex >= profileSteps.size() ? profileSteps.size() - 1 : profileStepIndex;
        this.iterationMetrics.finish(0);
        // reset counts
        this.iterationMetrics.setCount(TraversalMetrics.TRAVERSER_COUNT_ID,0);
        if (null != MemoryTraversalSideEffects.getMemorySideEffectsPhase(this.traversal.get())) {
            this.traversal.get().getSideEffects().add(profileSteps.get(profileStepIndex).getId(), this.iterationMetrics);
        }
        this.iterationMetrics = null;
    }
}
 
Example #9
Source File: GraphFilterStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (TraversalHelper.getStepsOfAssignableClass(VertexProgramStep.class, traversal).size() > 1)  // do not do if there is an OLAP chain
        return;
    final Graph graph = traversal.getGraph().orElse(EmptyGraph.instance()); // given that this strategy only works for single OLAP jobs, the graph is the traversal graph
    for (final TraversalVertexProgramStep step : TraversalHelper.getStepsOfClass(TraversalVertexProgramStep.class, traversal)) {   // will be zero or one step
        final Traversal.Admin<?, ?> computerTraversal = step.generateProgram(graph, EmptyMemory.instance()).getTraversal().get().clone();
        if (!computerTraversal.isLocked())
            computerTraversal.applyStrategies();
        final Computer computer = step.getComputer();
        if (null == computer.getEdges() && !GraphComputer.Persist.EDGES.equals(computer.getPersist())) {  // if edges() already set, use it
            final Traversal.Admin<Vertex, Edge> edgeFilter = getEdgeFilter(computerTraversal);
            if (null != edgeFilter)  // if no edges can be filtered, then don't set edges()
                step.setComputer(computer.edges(edgeFilter));
        }
    }
}
 
Example #10
Source File: SubgraphStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotRetainMarkers() {
    final SubgraphStrategy strategy = SubgraphStrategy.build().vertices(__.<Vertex>out().hasLabel("person")).create();
    final Traversal.Admin<?, ?> t = out().inE().asAdmin();
    t.setStrategies(t.getStrategies().clone().addStrategies(strategy, StandardVerificationStrategy.instance()));
    t.applyStrategies();
    assertEquals(t.getSteps().get(0).getClass(), VertexStep.class);
    assertEquals(t.getSteps().get(1).getClass(), TraversalFilterStep.class);
    assertEquals(AndStep.class, ((TraversalFilterStep<?>) t.getSteps().get(1)).getLocalChildren().get(0).getStartStep().getClass());
    assertEquals(0, ((TraversalFilterStep<?>) t.getSteps().get(1)).getLocalChildren().get(0).getStartStep().getLabels().size());
    assertEquals(t.getSteps().get(2).getClass(), EdgeVertexStep.class);
    assertEquals(t.getSteps().get(3).getClass(), TraversalFilterStep.class);
    assertEquals(VertexStep.class, ((TraversalFilterStep<?>) t.getSteps().get(3)).getLocalChildren().get(0).getStartStep().getClass());
    assertEquals(0, ((TraversalFilterStep<?>) t.getSteps().get(3)).getLocalChildren().get(0).getStartStep().getLabels().size());
    TraversalHelper.getStepsOfAssignableClassRecursively(Step.class, t).forEach(step -> assertTrue(step.getLabels().isEmpty()));
}
 
Example #11
Source File: JanusGraphTraversalUtil.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method searches the traversal for traversal parents which are multiQuery compatible.
 * Being multiQuery compatible is not solely determined by the class of the parent step, it
 * must also have a vertex step as the first step in one of its local or global children.
 *
 * @param traversal The traversal in which to search for multiQuery compatible steps
 * @return A list of traversal parents which were multiQuery compatible
 */
public static List<Step> getMultiQueryCompatibleSteps(Traversal.Admin<?, ?> traversal) {
    final Set<Step> multiQueryCompatibleSteps = new HashSet<>();
    for (Step step : traversal.getSteps()) {
        if (isMultiQueryCompatibleStep(step)) {
            Step parentStep = step;
            ((TraversalParent) parentStep).getGlobalChildren().forEach(childTraversal -> getMultiQueryCompatibleStepsFromChildTraversal(childTraversal, parentStep, multiQueryCompatibleSteps));
            ((TraversalParent) parentStep).getLocalChildren().forEach(childTraversal -> getMultiQueryCompatibleStepsFromChildTraversal(childTraversal, parentStep, multiQueryCompatibleSteps));

            if (parentStep instanceof RepeatStep && multiQueryCompatibleSteps.contains(parentStep)) {
                RepeatStep repeatStep = (RepeatStep) parentStep;
                List<RepeatEndStep> repeatEndSteps = TraversalHelper.getStepsOfClass(RepeatEndStep.class, repeatStep.getRepeatTraversal());
                if (repeatEndSteps.size() == 1) {
                    // Want the RepeatEndStep so the start of one iteration can feed into the next
                    multiQueryCompatibleSteps.remove(parentStep);
                    multiQueryCompatibleSteps.add(repeatEndSteps.get(0));
                }
            }
        }
    }
    return Lists.newArrayList(multiQueryCompatibleSteps);
}
 
Example #12
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 #13
Source File: FilterRankingStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    boolean modified = true;
    while (modified) {
        modified = false;
        final List<Step> steps = traversal.getSteps();
        for (int i = 0; i < steps.size() - 1; i++) {
            final Step<?, ?> step = steps.get(i);
            final Step<?, ?> nextStep = step.getNextStep();
            if (!usesLabels(nextStep, step.getLabels())) {
                final int nextRank = getStepRank(nextStep);
                if (nextRank != 0) {
                    if (!step.getLabels().isEmpty()) {
                        TraversalHelper.copyLabels(step, nextStep, true);
                        modified = true;
                    }
                    if (getStepRank(step) > nextRank) {
                        traversal.removeStep(nextStep);
                        traversal.addStep(i, nextStep);
                        modified = true;
                    }
                }
            }
        }
    }
}
 
Example #14
Source File: HadoopIoStrategy.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // since hadoopgraph can't be modified we can't try to use the existing IoStep for standard processing
    // without graphcomputer
    if (traversal.getStartStep() instanceof IoStep)
        throw new VerificationException("HadoopGraph requires a GraphComputer for io() step", traversal);

    // VertexProgramStrategy should wrap up the IoStep in a TraversalVertexProgramStep. use that to grab the
    // GraphComputer that was injected in there and push that in to the HadoopIoStep. this step pattern match
    // is fairly specific and since you really can't chain together steps after io() this approach should work
    if (traversal.getStartStep() instanceof TraversalVertexProgramStep) {
        final TraversalVertexProgramStep tvp = (TraversalVertexProgramStep) traversal.getStartStep();
        if (tvp.computerTraversal.get().getStartStep() instanceof ReadWriting) {
            final ReadWriting readWriting = (ReadWriting) tvp.computerTraversal.get().getStartStep();
            final HadoopIoStep hadoopIoStep = new HadoopIoStep(traversal, readWriting.getFile());
            hadoopIoStep.setMode(readWriting.getMode());
            hadoopIoStep.setComputer(tvp.getComputer());
            readWriting.getParameters().getRaw().forEach((key, value) -> value.forEach(v -> hadoopIoStep.configure(key, v)));

            TraversalHelper.replaceStep(tvp, hadoopIoStep, traversal);
        }
    }
}
 
Example #15
Source File: BranchStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void addGlobalChildOption(final M pickToken, final Traversal.Admin<S, E> traversalOption) {
    if (pickToken instanceof Pick) {
        if (this.traversalPickOptions.containsKey(pickToken))
            this.traversalPickOptions.get(pickToken).add(traversalOption);
        else
            this.traversalPickOptions.put((Pick) pickToken, new ArrayList<>(Collections.singletonList(traversalOption)));
    } else {
        final Traversal.Admin pickOptionTraversal;
        if (pickToken instanceof Traversal) {
            pickOptionTraversal = ((Traversal) pickToken).asAdmin();
        } else {
            pickOptionTraversal = new PredicateTraversal(pickToken);
        }
        this.traversalOptions.add(Pair.with(pickOptionTraversal, traversalOption));
    }
    // adding an IdentityStep acts as a placeholder when reducing barriers get in the way - see the
    // standardAlgorithm() method for more information.
    if (TraversalHelper.hasStepOfAssignableClass(ReducingBarrierStep.class, traversalOption))
        traversalOption.addStep(0, new IdentityStep(traversalOption));
    traversalOption.addStep(new EndStep(traversalOption));

    if (!this.hasBarrier && !TraversalHelper.getStepsOfAssignableClassRecursively(Barrier.class, traversalOption).isEmpty())
        this.hasBarrier = true;
    this.integrateChild(traversalOption);
}
 
Example #16
Source File: TestOrStepBarrier.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_emitXhasXname_markoX_or_loops_isX2XX_repeatXoutX_valuesXnameX() {
    loadModern();
    final DefaultGraphTraversal<Vertex, String> traversal = (DefaultGraphTraversal<Vertex, String>) this.sqlgGraph.traversal()
            .V()
            .emit(
                    __.has("name", "marko")
                            .or()
                            .loops().is(2)
            )
            .repeat(__.out())
            .<String>values("name");
    printTraversalForm(traversal);
    checkResults(Arrays.asList("marko", "ripple", "lop"), traversal);
    List<SqlgRepeatStepBarrier> sqlgRepeatStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgRepeatStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgRepeatStepBarriers.size());
    List<SqlgOrStepBarrier> sqlgOrStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgOrStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgOrStepBarriers.size());
}
 
Example #17
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 #18
Source File: TestAndStepBarrier.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testAndStepBarrier() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    a1.addEdge("abb", b1);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a2.addEdge("abb", b2);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    a3.addEdge("abbb", b3);


    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").and(
            __.out("ab"),
            __.out("abb")
    );
    printTraversalForm(traversal);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(a1));

    List<SqlgAndStepBarrier> sqlgAndStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgAndStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgAndStepBarriers.size());
}
 
Example #19
Source File: GraphTraversal.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Filters vertices, edges and vertex properties based on their value.
 *
 * @param value       the value of the {@link Element}
 * @param otherValues additional values of the {@link Element}
 * @return the traversal with an appended {@link HasStep}
 * @see <a href="http://tinkerpop.apache.org/docs/${project.version}/reference/#has-step" target="_blank">Reference Documentation - Has Step</a>
 */
public default GraphTraversal<S, E> hasValue(final Object value, final Object... otherValues) {
    if (value instanceof P)
        return this.hasValue((P) value);
    else {
        final List<Object> values = new ArrayList<>();
        if (value instanceof Object[]) {
            Collections.addAll(values, (Object[]) value);
        } else
            values.add(value);
        for (final Object v : otherValues) {
            if (v instanceof Object[]) {
                Collections.addAll(values, (Object[]) v);
            } else
                values.add(v);
        }
        this.asAdmin().getBytecode().addStep(Symbols.hasValue, values.toArray());
        return TraversalHelper.addHasContainer(this.asAdmin(), new HasContainer(T.value.getAccessor(), values.size() == 1 ? P.eq(values.get(0)) : P.within(values)));
    }
}
 
Example #20
Source File: HugeGraphStepStrategy.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void apply(Traversal.Admin<?, ?> traversal) {
    TraversalUtil.convAllHasSteps(traversal);

    // Extract conditions in GraphStep
    List<GraphStep> steps = TraversalHelper.getStepsOfClass(
                            GraphStep.class, traversal);
    for (GraphStep originStep : steps) {
        HugeGraphStep<?, ?> newStep = new HugeGraphStep<>(originStep);
        TraversalHelper.replaceStep(originStep, newStep, traversal);

        TraversalUtil.extractHasContainer(newStep, traversal);

        // TODO: support order-by optimize
        // TraversalUtil.extractOrder(newStep, traversal);

        TraversalUtil.extractRange(newStep, traversal, false);

        TraversalUtil.extractCount(newStep, traversal);
    }
}
 
Example #21
Source File: Traversal.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Add all the results of the traversal to the provided collection.
 *
 * @param collection the collection to fill
 * @return the collection now filled
 */
public default <C extends Collection<E>> C fill(final C collection) {
    try {
        if (!this.asAdmin().isLocked()) this.asAdmin().applyStrategies();
        // use the end step so the results are bulked
        final Step<?, E> endStep = this.asAdmin().getEndStep();
        while (true) {
            final Traverser<E> traverser = endStep.next();
            TraversalHelper.addToCollection(collection, traverser.get(), traverser.bulk());
        }
    } catch (final NoSuchElementException ignored) {
    } finally {
        CloseableIterator.closeIterator(this);
    }
    return collection;
}
 
Example #22
Source File: TestAndStepBarrier.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void g_V_asXaX_outXknowsX_and_outXcreatedX_inXcreatedX_asXaX_name() {
    loadModern();
    final DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>)this.sqlgGraph.traversal()
            .V().as("a")
            .out("knows")
            .and()
            .out("created").in("created").as("a")
            .<Vertex>values("name");
    printTraversalForm(traversal);
    checkResults(Arrays.asList(convertToVertex(this.sqlgGraph, "marko")), traversal);
    List<SqlgAndStepBarrier> sqlgAndStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgAndStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgAndStepBarriers.size());
}
 
Example #23
Source File: ComputerVerificationStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (!TraversalHelper.onGraphComputer(traversal))
        return;

    if (traversal.getParent() instanceof TraversalVertexProgramStep) {
        if (TraversalHelper.getStepsOfAssignableClassRecursively(GraphStep.class, traversal).size() > 1)
            throw new VerificationException("Mid-traversal V()/E() is currently not supported on GraphComputer", traversal);
        if (TraversalHelper.hasStepOfAssignableClassRecursively(ProfileStep.class, traversal) && TraversalHelper.getStepsOfAssignableClass(VertexProgramStep.class, TraversalHelper.getRootTraversal(traversal)).size() > 1)
            throw new VerificationException("Profiling a multi-VertexProgramStep traversal is currently not supported on GraphComputer", traversal);
    }

    // this is a problem because sideEffect.merge() is transient on the OLAP reduction
    if (TraversalHelper.getRootTraversal(traversal).getTraverserRequirements().contains(TraverserRequirement.ONE_BULK))
        throw new VerificationException("One bulk is currently not supported on GraphComputer: " + traversal, traversal);

    // you can not traverse past the local star graph with localChildren (e.g. by()-modulators).
    if (!TraversalHelper.isGlobalChild(traversal) && !TraversalHelper.isLocalStarGraph(traversal))
        throw new VerificationException("Local traversals may not traverse past the local star-graph on GraphComputer: " + traversal, traversal);

    for (final Step<?, ?> step : traversal.getSteps()) {
        if (step instanceof PathProcessor && ((PathProcessor) step).getMaxRequirement() != PathProcessor.ElementRequirement.ID)
            throw new VerificationException("It is not possible to access more than a path element's id on GraphComputer: " + step + " requires " + ((PathProcessor) step).getMaxRequirement(), traversal);

        if (UNSUPPORTED_STEPS.stream().filter(c -> c.isAssignableFrom(step.getClass())).findFirst().isPresent())
            throw new VerificationException("The following step is currently not supported on GraphComputer: " + step, traversal);
    }

    Step<?, ?> nextParentStep = traversal.getParent().asStep();
    while (!(nextParentStep instanceof EmptyStep)) {
        if (nextParentStep instanceof PathProcessor && ((PathProcessor) nextParentStep).getMaxRequirement() != PathProcessor.ElementRequirement.ID)
            throw new VerificationException("The following path processor step requires more than the element id on GraphComputer: " + nextParentStep + " requires " + ((PathProcessor) nextParentStep).getMaxRequirement(), traversal);
        nextParentStep = nextParentStep.getNextStep();
    }
}
 
Example #24
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFindStepOfClassInTraversal() {
    final Traversal.Admin traversal = new DefaultTraversal<>(EmptyGraph.instance());
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));
    traversal.asAdmin().addStep(0, new HasStep(traversal));

    assertThat(TraversalHelper.hasStepOfClass(FilterStep.class, traversal), is(false));
}
 
Example #25
Source File: ComputerFinalizationStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(Traversal.Admin<?, ?> traversal) {
    if (!TraversalHelper.onGraphComputer(traversal) || !TraversalHelper.isGlobalChild(traversal))
        return;

    for (final Step<?, ?> step : traversal.getSteps()) {
        if (step instanceof GraphComputing)
            ((GraphComputing) step).onGraphComputer();
    }
}
 
Example #26
Source File: ProgramVertexProgramStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public VertexProgram generateProgram(final Graph graph, final Memory memory) {
    final MapConfiguration base = new MapConfiguration(this.configuration);
    PureTraversal.storeState(base, ROOT_TRAVERSAL, TraversalHelper.getRootTraversal(this.getTraversal()).clone());
    base.setProperty(STEP_ID, this.getId());
    if (memory.exists(TraversalVertexProgram.HALTED_TRAVERSERS))
        TraversalVertexProgram.storeHaltedTraversers(base, memory.get(TraversalVertexProgram.HALTED_TRAVERSERS));
    return VertexProgram.createVertexProgram(graph, base);
}
 
Example #27
Source File: ReferenceElementStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    if (traversal.getParent() == EmptyStep.instance()) {
        final Optional<ProfileSideEffectStep> profileStep = TraversalHelper.getFirstStepOfAssignableClass(ProfileSideEffectStep.class, traversal);
        final int index = profileStep.map(step -> traversal.getSteps().indexOf(step))
                .orElseGet(() -> traversal.getSteps().size());
        traversal.addStep(index, new ReferenceElementStep<>(traversal));
    }
}
 
Example #28
Source File: SqlgAddVertexStartStepStrategy.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<AddVertexStartStep> addVertexStartSteps = TraversalHelper.getStepsOfAssignableClassRecursively(AddVertexStartStep.class, traversal);
    for (AddVertexStartStep addVertexStartStep : addVertexStartSteps) {
        SqlgAddVertexStartStep sqlgAddVertexStartStep = new SqlgAddVertexStartStep(
                addVertexStartStep.getTraversal(),
                addVertexStartStep.getParameters()
        );
        for (EventCallback<Event.VertexAddedEvent> callback : addVertexStartStep.getMutatingCallbackRegistry().getCallbacks()) {
            sqlgAddVertexStartStep.getMutatingCallbackRegistry().addCallback(callback);
        }
        for (String label : addVertexStartStep.getLabels()) {
            sqlgAddVertexStartStep.addLabel(label);
        }
        //noinspection unchecked
        TraversalHelper.replaceStep(
                addVertexStartStep,
                sqlgAddVertexStartStep,
                addVertexStartStep.getTraversal()
        );
    }
}
 
Example #29
Source File: TestLocalVertexStepOptionalWithOrder.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOptionalWithOrder2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").as("a")
            .local(
                    __.optional(
                            __.outE().as("e").otherV().as("v")
                    )
            ).order().by("order");
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(4, traversal.getSteps().size());

    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
    Assert.assertTrue(traversal.getSteps().get(3) instanceof OrderGlobalStep);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(2);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());

    Assert.assertEquals(5, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
    Assert.assertEquals(a3, vertices.get(3));
    Assert.assertEquals(a2, vertices.get(4));
}
 
Example #30
Source File: IncidentToAdjacentStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // using a hidden label marker to denote whether the traversal should not be processed by this strategy
    if ((traversal.isRoot() || traversal.getParent() instanceof VertexProgramStep) &&
            TraversalHelper.hasStepOfAssignableClassRecursively(INVALIDATING_STEP_CLASSES, traversal))
        TraversalHelper.applyTraversalRecursively(t -> t.getStartStep().addLabel(MARKER), traversal);
    if (traversal.getStartStep().getLabels().contains(MARKER)) {
        traversal.getStartStep().removeLabel(MARKER);
        return;
    }
    ////////////////////////////////////////////////////////////////////////////
    final Collection<Pair<VertexStep, Step>> stepsToReplace = new ArrayList<>();
    Step prev = null;
    for (final Step curr : traversal.getSteps()) {
        if (curr instanceof TraversalParent) {
            ((TraversalParent) curr).getLocalChildren().forEach(this::apply);
            ((TraversalParent) curr).getGlobalChildren().forEach(this::apply);
        }
        if (isOptimizable(prev, curr)) {
            stepsToReplace.add(Pair.with((VertexStep) prev, curr));
        }
        prev = curr;
    }
    if (!stepsToReplace.isEmpty()) {
        for (final Pair<VertexStep, Step> pair : stepsToReplace) {
            optimizeSteps(traversal, pair.getValue0(), pair.getValue1());
        }
    }
}