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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper#getStepsOfAssignableClassRecursively() . 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: SideEffectCapStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected E supply() {
    if (null == this.sideEffectCapableSteps) {
        this.sideEffectCapableSteps = new HashMap<>();
        Traversal.Admin<?, ?> parentTraversal = this.getTraversal();
        while (!(parentTraversal instanceof EmptyTraversal)) {
            for (final SideEffectCapable<Object, E> capableStep : TraversalHelper.getStepsOfAssignableClassRecursively(SideEffectCapable.class, parentTraversal)) {
                if (this.sideEffectKeys.contains(capableStep.getSideEffectKey()) && !this.sideEffectCapableSteps.containsKey(capableStep.getSideEffectKey()))
                    this.sideEffectCapableSteps.put(capableStep.getSideEffectKey(), capableStep);
            }
            if (this.sideEffectKeys.size() == this.sideEffectCapableSteps.size())
                break;
            parentTraversal = parentTraversal.getParent().asStep().getTraversal();
        }
    }
    ////////////
    if (this.sideEffectKeys.size() == 1) {
        final String sideEffectKey = this.sideEffectKeys.get(0);
        final E result = this.getTraversal().getSideEffects().<E>get(sideEffectKey);
        final SideEffectCapable<Object, E> sideEffectCapable = this.sideEffectCapableSteps.get(sideEffectKey);
        final E finalResult = null == sideEffectCapable ? result : sideEffectCapable.generateFinalResult(result);
        this.getTraversal().getSideEffects().set(sideEffectKey, finalResult);
        return finalResult;
    } else
        return (E) this.getMapOfSideEffects();
}
 
Example 2
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 3
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 4
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 5
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 6
Source File: TestAndStepBarrier.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testAndStepBarrierMultiple() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    a1.addEdge("abb", b1);
    a1.addEdge("abbb", 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"),
            __.out("abbb")
    );
    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 7
Source File: TraversalHelperTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSetPreviousStepToEmptyStep() {
    final Traversal.Admin<?, ?> traversal = __.V().out().asAdmin();
    //transform the traversal to __.V().not(out())
    //the VertexStep's previousStep should be the EmptyStep
    Optional<VertexStep> vertexStepOpt = TraversalHelper.getFirstStepOfAssignableClass(VertexStep.class, traversal);
    assertTrue(vertexStepOpt.isPresent());
    Traversal.Admin<?,?> inner = __.start().asAdmin();
    inner.addStep(0, vertexStepOpt.get());
    TraversalHelper.replaceStep(vertexStepOpt.get(), new NotStep<>(__.identity().asAdmin(), inner), traversal);
    List<VertexStep> vertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(VertexStep.class, traversal);
    assertEquals(1, vertexSteps.size());
    VertexStep vertexStep = vertexSteps.get(0);
    assertTrue("Expected the previousStep to be an EmptyStep, found instead " + vertexStep.getPreviousStep().toString(),vertexStep.getPreviousStep() == EmptyStep.instance());
}
 
Example 8
Source File: SparkSingleIterationStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    final Graph graph = traversal.getGraph().orElse(EmptyGraph.instance()); // best guess at what the graph will be as its dynamically determined
    for (final TraversalVertexProgramStep step : TraversalHelper.getStepsOfClass(TraversalVertexProgramStep.class, traversal)) {
        final Traversal.Admin<?, ?> computerTraversal = step.generateProgram(graph, EmptyMemory.instance()).getTraversal().get().clone();
        if (!computerTraversal.isLocked())
            computerTraversal.applyStrategies();
        ///
        boolean doesMessagePass = TraversalHelper.hasStepOfAssignableClassRecursively(Scope.global, MULTI_ITERATION_CLASSES, computerTraversal);
        if (!doesMessagePass) {
            for (final VertexStep vertexStep : TraversalHelper.getStepsOfAssignableClassRecursively(Scope.global, VertexStep.class, computerTraversal)) {
                if (vertexStep.returnsVertex() || !vertexStep.getDirection().equals(Direction.OUT)) { // in-edges require message pass in OLAP
                    doesMessagePass = true;
                    break;
                }
            }
        }
        if (!doesMessagePass &&
                !MessagePassingReductionStrategy.endsWithElement(computerTraversal.getEndStep()) &&
                !(computerTraversal.getTraverserRequirements().contains(TraverserRequirement.LABELED_PATH) || // TODO: remove this when dynamic detachment is available in 3.3.0
                        computerTraversal.getTraverserRequirements().contains(TraverserRequirement.PATH))) {  // TODO: remove this when dynamic detachment is available in 3.3.0
            step.setComputer(step.getComputer()
                    // if no message passing, don't partition the loaded graph
                    .configure(Constants.GREMLIN_SPARK_SKIP_PARTITIONER, true)
                    // if no message passing, don't cache the loaded graph
                    .configure(Constants.GREMLIN_SPARK_SKIP_GRAPH_CACHE, true));
        }
    }
}
 
Example 9
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 10
Source File: TestLocalVertexStepOptionalWithOrder.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testSimple() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    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")
            .local(
                    __.out().order().by("order", Order.decr)
            );
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(1);

    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    SqlgVertexStep sqlgVertexStep = sqlgVertexSteps.get(0);
    assertStep(sqlgVertexStep, false, false, false, false);
    Assert.assertEquals(3, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
}
 
Example 11
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 12
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 13
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 14
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 15
Source File: TestOrStepBarrier.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrStepBarrier3Ors() {
    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);
    Vertex a4 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b4 = this.sqlgGraph.addVertex(T.label, "B");
    a4.addEdge("abbbb", b4);


    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .or(
                    __.out("ab"),
                    __.out("abb"),
                    __.out("abbb")
            );
    printTraversalForm(traversal);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.containsAll(Arrays.asList(a1, a2, a3)));
    List<SqlgOrStepBarrier> sqlgOrStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgOrStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgOrStepBarriers.size());
}
 
Example 16
Source File: TestAndStepBarrier.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testAndStepBarrierWithUserSuppliedID() {
    Schema publicSchema = this.sqlgGraph.getTopology().getPublicSchema();
    VertexLabel aVertexLabel = publicSchema.ensureVertexLabelExist(
            "A",
            new HashMap<String, PropertyType>() {{
                put("uid", PropertyType.varChar(100));
                put("country", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("uid", "country")));
    VertexLabel bVertexLabel = publicSchema.ensureVertexLabelExist(
            "B",
            new HashMap<String, PropertyType>() {{
                put("uid", PropertyType.varChar(100));
                put("country", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("uid", "country")));
    aVertexLabel.ensureEdgeLabelExist(
            "ab",
            bVertexLabel,
            new HashMap<String, PropertyType>() {{
                put("uid", PropertyType.varChar(100));
                put("country", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("uid", "country")));
    aVertexLabel.ensureEdgeLabelExist(
            "abb",
            bVertexLabel,
            new HashMap<String, PropertyType>() {{
                put("uid", PropertyType.varChar(100));
                put("country", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("uid", "country")));
    aVertexLabel.ensureEdgeLabelExist(
            "abbb",
            bVertexLabel,
            new HashMap<String, PropertyType>() {{
                put("uid", PropertyType.varChar(100));
                put("country", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Arrays.asList("uid", "country")));
    this.sqlgGraph.tx().commit();

    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "uid", UUID.randomUUID().toString(), "country", "SA");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "uid", UUID.randomUUID().toString(), "country", "SA");
    a1.addEdge("ab", b1 , "uid", UUID.randomUUID().toString(), "country", "SA");
    a1.addEdge("abb", b1, "uid", UUID.randomUUID().toString(), "country", "SA");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "uid", UUID.randomUUID().toString(), "country", "SA");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "uid", UUID.randomUUID().toString(), "country", "SA");
    a2.addEdge("abb", b2, "uid", UUID.randomUUID().toString(), "country", "SA");
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "uid", UUID.randomUUID().toString(), "country", "SA");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "uid", UUID.randomUUID().toString(), "country", "SA");
    a3.addEdge("abbb", b3, "uid", UUID.randomUUID().toString(), "country", "SA");

    this.sqlgGraph.tx().commit();

    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 17
Source File: TestLocalVertexStepOptionalWithOrder.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testOptionalWithOrderAndRange2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
    this.sqlgGraph.addVertex(T.label, "A", "name", "aa");
    this.sqlgGraph.addVertex(T.label, "A", "name", "aaa");

    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "d");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "c");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
    Vertex bb1 = this.sqlgGraph.addVertex(T.label, "BB", "name", "g");
    Vertex bb2 = this.sqlgGraph.addVertex(T.label, "BB", "name", "f");
    Vertex bb3 = this.sqlgGraph.addVertex(T.label, "BB", "name", "e");

    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "h");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "i");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "j");
    Vertex cc1 = this.sqlgGraph.addVertex(T.label, "CC", "name", "k");
    Vertex cc2 = this.sqlgGraph.addVertex(T.label, "CC", "name", "l");
    Vertex cc3 = this.sqlgGraph.addVertex(T.label, "CC", "name", "m");

    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    a1.addEdge("abb", bb1);
    a1.addEdge("abb", bb2);
    a1.addEdge("abb", bb3);

    b1.addEdge("bc", c1);
    b1.addEdge("bc", c2);
    b1.addEdge("bc", c3);
    b2.addEdge("bcc", cc1);
    b2.addEdge("bcc", cc2);
    b2.addEdge("bcc", cc3);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .local(
                    __.optional(
                            __.out().order().by("name").range(1, 2).optional(
                                    __.out().order().by("name", Order.decr).range(2, 3)
                            )
                    )
            )
            .path();
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    //This query is no fully optimized.
    //The range messes it up, so it has a SqlgVertexStep
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(1) instanceof LocalStep);
    LocalStep<?, ?> localStep = (LocalStep<?, ?>) traversal.getSteps().get(1);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    //first optional step can not be optimized because it has a range step that is not the last step
    //the second is optimized.
    Assert.assertEquals(2, sqlgVertexSteps.size());

    Assert.assertEquals(3, paths.size());
    List<Path> pathsOfLength1 = paths.stream().filter(p -> p.size() == 1).collect(Collectors.toList());
    Assert.assertEquals(2, pathsOfLength1.size());

    List<Path> pathsOfLength3 = paths.stream().filter(p -> p.size() == 3).collect(Collectors.toList());
    Assert.assertEquals(1, pathsOfLength3.size());

    Vertex v = (Vertex) pathsOfLength3.get(0).objects().get(2);
    Assert.assertEquals("k", v.value("name"));
}
 
Example 18
Source File: TestLocalVertexStepOptionalWithOrder.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testSimpleButLessSo() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex a12 = this.sqlgGraph.addVertex(T.label, "A", "order", 10);
    Vertex b12 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b22 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b32 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    a12.addEdge("ab", b12);
    a12.addEdge("ab", b22);
    a12.addEdge("ab", b32);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").as("a")
            .local(
                    __.out().as("b")
            )
            .order()
            .by(__.select("a").by("order"), Order.decr)
            .by(__.select("b").by("order"), Order.decr);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(2);
    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    SqlgVertexStep sqlgVertexStep = sqlgVertexSteps.get(0);
    assertStep(sqlgVertexStep, false, false, false, true);
    Assert.assertEquals(6, vertices.size());
    Assert.assertEquals(b32, vertices.get(0));
    Assert.assertEquals(b22, vertices.get(1));
    Assert.assertEquals(b12, vertices.get(2));
    Assert.assertEquals(b3, vertices.get(3));
    Assert.assertEquals(b2, vertices.get(4));
    Assert.assertEquals(b1, vertices.get(5));
}
 
Example 19
Source File: TraversalVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public boolean terminate(final Memory memory) {
    // memory is local
    MemoryTraversalSideEffects.setMemorySideEffects(this.traversal.get(), memory, ProgramPhase.TERMINATE);
    final boolean voteToHalt = memory.<Boolean>get(VOTE_TO_HALT);
    memory.set(VOTE_TO_HALT, true);
    memory.set(ACTIVE_TRAVERSERS, new IndexedTraverserSet.VertexIndexedTraverserSet());
    if (voteToHalt) {
        // local traverser sets to process
        final TraverserSet<Object> toProcessTraversers = new TraverserSet<>();
        // traversers that need to be sent back to the workers (no longer can be processed locally by the master traversal)
        final IndexedTraverserSet<Object,Vertex> remoteActiveTraversers = new IndexedTraverserSet.VertexIndexedTraverserSet();
        // halted traversers that have completed their journey
        final TraverserSet<Object> haltedTraversers = memory.get(HALTED_TRAVERSERS);
        // get all barrier traversers
        final Set<String> completedBarriers = new HashSet<>();
        MasterExecutor.processMemory(this.traversalMatrix, memory, toProcessTraversers, completedBarriers);
        // process all results from barriers locally and when elements are touched, put them in remoteActiveTraversers
        MasterExecutor.processTraversers(this.traversal, this.traversalMatrix, toProcessTraversers, remoteActiveTraversers, haltedTraversers, this.haltedTraverserStrategy);
        // tell parallel barriers that might not have been active in the last round that they are no longer active
        memory.set(COMPLETED_BARRIERS, completedBarriers);
        if (!remoteActiveTraversers.isEmpty() ||
                completedBarriers.stream().map(this.traversalMatrix::getStepById).filter(step -> step instanceof LocalBarrier).findAny().isPresent()) {
            // send active traversers back to workers
            memory.set(ACTIVE_TRAVERSERS, remoteActiveTraversers);
            return false;
        } else {
            // finalize locally with any last traversers dangling in the local traversal
            final Step<?, Object> endStep = (Step<?, Object>) this.traversal.get().getEndStep();
            while (endStep.hasNext()) {
                haltedTraversers.add(this.haltedTraverserStrategy.halt(endStep.next()));
            }
            // the result of a TraversalVertexProgram are the halted traversers
            memory.set(HALTED_TRAVERSERS, haltedTraversers);
            // finalize profile side-effect steps. (todo: try and hide this)
            for (final ProfileSideEffectStep profileStep : TraversalHelper.getStepsOfAssignableClassRecursively(ProfileSideEffectStep.class, this.traversal.get())) {
                this.traversal.get().getSideEffects().set(profileStep.getSideEffectKey(), profileStep.generateFinalResult(this.traversal.get().getSideEffects().get(profileStep.getSideEffectKey())));
            }
            return true;
        }
    } else {
        return false;
    }
}
 
Example 20
Source File: TraversalVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void loadState(final Graph graph, final Configuration configuration) {
    if (!configuration.containsKey(TRAVERSAL))
        throw new IllegalArgumentException("The configuration does not have a traversal: " + TRAVERSAL);
    this.traversal = PureTraversal.loadState(configuration, TRAVERSAL, graph);
    if (!this.traversal.get().isLocked())
        this.traversal.get().applyStrategies();
    /// traversal is compiled and ready to be introspected
    this.traversalMatrix = new TraversalMatrix<>(this.traversal.get());
    // get any master-traversal halted traversers
    this.haltedTraversers = TraversalVertexProgram.loadHaltedTraversers(configuration);
    // if results will be serialized out, don't save halted traversers across the cluster
    this.returnHaltedTraversers =
            (this.traversal.get().getParent().asStep().getNextStep() instanceof ComputerResultStep || // if its just going to stream it out, don't distribute
                    this.traversal.get().getParent().asStep().getNextStep() instanceof EmptyStep ||  // same as above, but if using TraversalVertexProgramStep directly
                    (this.traversal.get().getParent().asStep().getNextStep() instanceof ProfileStep && // same as above, but needed for profiling
                            this.traversal.get().getParent().asStep().getNextStep().getNextStep() instanceof ComputerResultStep));

    // determine how to store halted traversers
    final Iterator<?> itty = IteratorUtils.filter(this.traversal.get().getStrategies(), strategy -> strategy instanceof HaltedTraverserStrategy).iterator();
    this.haltedTraverserStrategy = itty.hasNext() ? (HaltedTraverserStrategy) itty.next() : HaltedTraverserStrategy.reference();

    // register traversal side-effects in memory
    this.memoryComputeKeys.addAll(MemoryTraversalSideEffects.getMemoryComputeKeys(this.traversal.get()));
    // register MapReducer memory compute keys
    for (final MapReducer<?, ?, ?, ?, ?> mapReducer : TraversalHelper.getStepsOfAssignableClassRecursively(MapReducer.class, this.traversal.get())) {
        this.mapReducers.add(mapReducer.getMapReduce());
        this.memoryComputeKeys.add(MemoryComputeKey.of(mapReducer.getMapReduce().getMemoryKey(), Operator.assign, false, false));
    }
    // register memory computing steps that use memory compute keys
    for (final MemoryComputing<?> memoryComputing : TraversalHelper.getStepsOfAssignableClassRecursively(MemoryComputing.class, this.traversal.get())) {
        this.memoryComputeKeys.add(memoryComputing.getMemoryComputeKey());
    }
    // register profile steps (TODO: try to hide this)
    for (final ProfileStep profileStep : TraversalHelper.getStepsOfAssignableClassRecursively(ProfileStep.class, this.traversal.get())) {
        this.traversal.get().getSideEffects().register(profileStep.getId(), new MutableMetricsSupplier(profileStep.getPreviousStep()), ProfileStep.ProfileBiOperator.instance());
    }
    // register TraversalVertexProgram specific memory compute keys
    this.memoryComputeKeys.add(MemoryComputeKey.of(VOTE_TO_HALT, Operator.and, false, true));
    this.memoryComputeKeys.add(MemoryComputeKey.of(HALTED_TRAVERSERS, Operator.addAll, false, false));
    this.memoryComputeKeys.add(MemoryComputeKey.of(ACTIVE_TRAVERSERS, Operator.addAll, true, true));
    this.memoryComputeKeys.add(MemoryComputeKey.of(MUTATED_MEMORY_KEYS, Operator.addAll, false, true));
    this.memoryComputeKeys.add(MemoryComputeKey.of(COMPLETED_BARRIERS, Operator.addAll, true, true));

    // does the traversal need profile information
    this.profile = !TraversalHelper.getStepsOfAssignableClassRecursively(ProfileStep.class, this.traversal.get()).isEmpty();
}