Search in sources :

Example 1 with SqlgLocalStepBarrier

use of org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrder.

@Test
public void testOptionalWithOrder() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "aa");
    Vertex a3 = 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").optional(__.out().order().by("name", Order.decr)))).path();
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(3, 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, true, true, true);
    Assert.assertEquals(12, paths.size());
    // assert the order
    // all the paths of length 2 and 3 must be sorted
    List<Path> pathsOfLength3 = paths.stream().filter(p -> p.size() == 3).collect(Collectors.toList());
    Vertex v = (Vertex) pathsOfLength3.get(5).objects().get(2);
    Assert.assertEquals("h", v.value("name"));
    v = (Vertex) pathsOfLength3.get(4).objects().get(2);
    Assert.assertEquals("i", v.value("name"));
    v = (Vertex) pathsOfLength3.get(3).objects().get(2);
    Assert.assertEquals("j", v.value("name"));
    v = (Vertex) pathsOfLength3.get(2).objects().get(2);
    Assert.assertEquals("k", v.value("name"));
    v = (Vertex) pathsOfLength3.get(1).objects().get(2);
    Assert.assertEquals("l", v.value("name"));
    v = (Vertex) pathsOfLength3.get(0).objects().get(2);
    Assert.assertEquals("m", v.value("name"));
    List<Path> pathsOfLength2 = paths.stream().filter(p -> p.size() == 2).collect(Collectors.toList());
    v = (Vertex) pathsOfLength2.get(0).objects().get(1);
    Assert.assertEquals("b", v.value("name"));
    v = (Vertex) pathsOfLength2.get(1).objects().get(1);
    Assert.assertEquals("e", v.value("name"));
    v = (Vertex) pathsOfLength2.get(2).objects().get(1);
    Assert.assertEquals("f", v.value("name"));
    v = (Vertex) pathsOfLength2.get(3).objects().get(1);
    Assert.assertEquals("g", v.value("name"));
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c2)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c3)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c3)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b2) && p.get(2).equals(cc1)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b2) && p.get(2).equals(cc1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b2) && p.get(2).equals(cc2)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b2) && p.get(2).equals(cc2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b2) && p.get(2).equals(cc3)));
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b2) && p.get(2).equals(cc3)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(bb1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(bb1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(bb2)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(bb2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(bb3)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(bb3)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b3)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b3)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a2)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a3)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a3)).findAny().get());
    Assert.assertTrue(paths.isEmpty());
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) BaseTest(org.umlg.sqlg.test.BaseTest) SqlgOptionalStepBarrier(org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) Collectors(java.util.stream.Collectors) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) List(java.util.List) PathStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.PathStep) Element(org.apache.tinkerpop.gremlin.structure.Element) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Assert(org.junit.Assert) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 2 with SqlgLocalStepBarrier

use of org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testUmlgRequirementWithRange.

@Test
public void testUmlgRequirementWithRange() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3");
    Vertex bb1 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb1");
    Vertex bb2 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb2");
    Vertex bb3 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb3");
    a1.addEdge("ab", b1, "order", 3);
    a1.addEdge("ab", b2, "order", 2);
    a1.addEdge("ab", b3, "order", 1);
    a1.addEdge("abb", bb1, "order", 3);
    a1.addEdge("abb", bb2, "order", 2);
    a1.addEdge("abb", bb3, "order", 1);
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "c3");
    b1.addEdge("bc", c1, "order", 1);
    b1.addEdge("bc", c2, "order", 2);
    b1.addEdge("bc", c3, "order", 3);
    Vertex cc1 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc1");
    Vertex cc2 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc2");
    Vertex cc3 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc3");
    b1.addEdge("bcc", cc1, "order", 3);
    b1.addEdge("bcc", cc2, "order", 2);
    b1.addEdge("bcc", cc3, "order", 1);
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) sqlgGraph.traversal().V().hasLabel("A").as("a").order().by("name", Order.decr).local(__.optional(__.outE().as("e1").inV().as("b").order().by(__.select("a").by(T.id)).by(T.label).by(__.select("e1").by("order")).limit(1).local(__.optional(__.outE().as("e2").inV().order().by(__.select("b").by(T.id)).by(T.label).by(__.select("e2").by("order")))))).path();
    Assert.assertEquals(5, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(2) instanceof LocalStep);
    LocalStep localStep = (LocalStep) traversal.getSteps().get(2);
    DefaultGraphTraversal<Vertex, Vertex> t = (DefaultGraphTraversal<Vertex, Vertex>) localStep.getLocalChildren().get(0);
    // First optional is a non optimized, optimized SqlgChooseBarrierStep
    Assert.assertEquals(1, t.getSteps().size());
    Assert.assertTrue(t.getSteps().get(0) instanceof SqlgOptionalStepBarrier);
    SqlgOptionalStepBarrier sqlgOptionalStepBarrier = (SqlgOptionalStepBarrier) t.getSteps().get(0);
    Traversal.Admin<?, ?> optionalTraversal = (Traversal.Admin<?, ?>) sqlgOptionalStepBarrier.getLocalChildren().get(0);
    Assert.assertEquals(2, optionalTraversal.getSteps().size());
    Assert.assertTrue(optionalTraversal.getSteps().get(0) instanceof SqlgVertexStep);
    Assert.assertTrue(optionalTraversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier sqlgLocalStepBarrier1 = (SqlgLocalStepBarrier) optionalTraversal.getSteps().get(1);
    t = (DefaultGraphTraversal<Vertex, Vertex>) sqlgLocalStepBarrier1.getLocalChildren().get(0);
    // Second optional is a optimized
    Assert.assertEquals(1, t.getSteps().size());
    Assert.assertEquals(2, paths.size());
    Path pathX = paths.get(0);
    Assert.assertEquals(1, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a2));
    pathX = paths.get(1);
    Assert.assertEquals(3, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b3));
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgOptionalStepBarrier(org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 3 with SqlgLocalStepBarrier

use of org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testSimple.

@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));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 4 with SqlgLocalStepBarrier

use of org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testUmlgRequirement.

/**
 * optionals wrapped in local steps.
 * The idea is that each traversed out/in edge needs to be sorted
 */
@Test
public void testUmlgRequirement() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3");
    Vertex bb1 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb1");
    Vertex bb2 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb2");
    Vertex bb3 = this.sqlgGraph.addVertex(T.label, "BB", "name", "bb3");
    a1.addEdge("ab", b1, "order", 3);
    a1.addEdge("ab", b2, "order", 2);
    a1.addEdge("ab", b3, "order", 1);
    a1.addEdge("abb", bb1, "order", 3);
    a1.addEdge("abb", bb2, "order", 2);
    a1.addEdge("abb", bb3, "order", 1);
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "c3");
    b1.addEdge("bc", c1, "order", 1);
    b1.addEdge("bc", c2, "order", 2);
    b1.addEdge("bc", c3, "order", 3);
    Vertex cc1 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc1");
    Vertex cc2 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc2");
    Vertex cc3 = this.sqlgGraph.addVertex(T.label, "CC", "name", "cc3");
    b1.addEdge("bcc", cc1, "order", 3);
    b1.addEdge("bcc", cc2, "order", 2);
    b1.addEdge("bcc", cc3, "order", 1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) sqlgGraph.traversal().V().hasLabel("A").as("a").order().by("name", Order.decr).local(__.optional(__.outE().as("e1").inV().as("b").order().by(T.label).by(__.select("e1").by("order")).local(__.optional(__.outE().as("e2").inV().order().by(T.label).by(__.select("e2").by("order")))))).path();
    Assert.assertEquals(5, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(4, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(2) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier sqlgLocalStepBarrier = (SqlgLocalStepBarrier) traversal.getSteps().get(2);
    DefaultGraphTraversal<Vertex, Vertex> t = (DefaultGraphTraversal<Vertex, Vertex>) sqlgLocalStepBarrier.getLocalChildren().get(0);
    // First optional is a non optimized, optimized SqlgOptionalBarrierStep
    Assert.assertEquals(1, t.getSteps().size());
    Assert.assertTrue(t.getSteps().get(0) instanceof SqlgOptionalStepBarrier);
    SqlgOptionalStepBarrier sqlgOptionalStepBarrier = (SqlgOptionalStepBarrier) t.getSteps().get(0);
    Traversal.Admin<?, ?> optionalTraversal = (Traversal.Admin<?, ?>) sqlgOptionalStepBarrier.getLocalChildren().get(0);
    Assert.assertEquals(2, optionalTraversal.getSteps().size());
    Assert.assertTrue(optionalTraversal.getSteps().get(0) instanceof SqlgVertexStep);
    Assert.assertTrue(optionalTraversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier sqlgLocalStepBarrier1 = (SqlgLocalStepBarrier) optionalTraversal.getSteps().get(1);
    t = (DefaultGraphTraversal<Vertex, Vertex>) sqlgLocalStepBarrier.getLocalChildren().get(0);
    // Second optional is a optimized
    Assert.assertEquals(1, t.getSteps().size());
    Assert.assertEquals(12, paths.size());
    Path pathX = paths.get(0);
    Assert.assertEquals(1, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a2));
    pathX = paths.get(1);
    Assert.assertEquals(3, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b3));
    pathX = paths.get(2);
    Assert.assertEquals(3, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b2));
    pathX = paths.get(3);
    Assert.assertEquals(5, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b1));
    Assert.assertTrue(pathX.get(4).equals(c1));
    pathX = paths.get(4);
    Assert.assertEquals(5, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b1));
    Assert.assertTrue(pathX.get(4).equals(c2));
    pathX = paths.get(5);
    Assert.assertEquals(5, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b1));
    Assert.assertTrue(pathX.get(4).equals(c3));
    pathX = paths.get(6);
    Assert.assertEquals(5, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b1));
    Assert.assertTrue(pathX.get(4).equals(cc3));
    pathX = paths.get(7);
    Assert.assertEquals(5, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b1));
    Assert.assertTrue(pathX.get(4).equals(cc2));
    pathX = paths.get(8);
    Assert.assertEquals(5, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    Assert.assertTrue(pathX.get(2).equals(b1));
    Assert.assertTrue(pathX.get(4).equals(cc1));
    pathX = paths.get(9);
    Assert.assertEquals(3, pathX.size());
    Assert.assertTrue(pathX.get(0).equals(a1));
    for (Path path : paths) {
        for (Object v : path.objects()) {
            Element vertex = (Element) v;
            if (vertex instanceof Vertex) {
                System.out.print(vertex.<String>value("name"));
                System.out.print(" - ");
            }
        }
    }
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Element(org.apache.tinkerpop.gremlin.structure.Element) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgOptionalStepBarrier(org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 5 with SqlgLocalStepBarrier

use of org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier in project sqlg by pietermartin.

the class TestLocalVertexStepOptional method testLocalOptionalNested.

@Test
public void testLocalOptionalNested() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    a1.addEdge("ab", b1);
    b1.addEdge("bc", c1);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    a2.addEdge("ab", b2);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "a3");
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().local(__.optional(__.out().optional(__.out()))).path();
    List<Path> paths = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier) traversal.getSteps().get(1);
    List<SqlgVertexStep> sqlgVertexStepCompileds = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexStepCompileds.size());
    SqlgVertexStep sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(0);
    assertStep(sqlgVertexStepCompiled, false, false, true);
    Assert.assertEquals(6, paths.size());
    List<Predicate<Path>> pathsToAssert = Arrays.asList(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1), p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(c1), p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(b2), p -> p.size() == 1 && p.get(0).equals(c1), p -> p.size() == 1 && p.get(0).equals(b2), p -> p.size() == 1 && p.get(0).equals(a3));
    for (Predicate<Path> pathPredicate : pathsToAssert) {
        Optional<Path> path = paths.stream().filter(pathPredicate).findAny();
        Assert.assertTrue(path.isPresent());
        Assert.assertTrue(paths.remove(path.get()));
    }
    Assert.assertTrue(paths.isEmpty());
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Predicate(java.util.function.Predicate) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)16 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)16 Test (org.junit.Test)16 SqlgLocalStepBarrier (org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier)16 BaseTest (org.umlg.sqlg.test.BaseTest)16 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)14 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)9 Predicate (java.util.function.Predicate)5 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)5 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)4 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)4 SqlgOptionalStepBarrier (org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier)4 List (java.util.List)3 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)3 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)3 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)3 TraversalHelper (org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper)3 Element (org.apache.tinkerpop.gremlin.structure.Element)3 T (org.apache.tinkerpop.gremlin.structure.T)3 Assert (org.junit.Assert)3