Search in sources :

Example 6 with SqlgLocalStepBarrier

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

the class TestLocalVertexStepOptional method testLocalVertexStepNotOptimized.

@Test
public void testLocalVertexStepNotOptimized() {
    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");
    a1.addEdge("ab", b1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.gt.V().local(__.optional(__.where(__.has(T.label, "A")).out())).path();
    Assert.assertEquals(3, 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> sqlgVertexStepCompileds = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexStepCompileds.size());
    SqlgVertexStep sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(0);
    assertStep(sqlgVertexStepCompiled, false, false, false, true);
    Assert.assertEquals(3, paths.size());
    List<Predicate<Path>> pathsToAssert = Arrays.asList(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1), p -> p.size() == 1 && p.get(0).equals(a2), p -> p.size() == 1 && p.get(0).equals(b1));
    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());
// while (traversal.hasNext()) {
// System.out.println(traversal.next());
// }
}
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)

Example 7 with SqlgLocalStepBarrier

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

the class TestOrStepAfterVertexStepBarrier method testOrStepAfterVertexStepBarrier.

@Test
public void testOrStepAfterVertexStepBarrier() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    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");
    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().or(__.has("name", "b2"), __.has("name", "b3")));
    List<Vertex> vertices = traversal.toList();
    List<Step> steps = traversal.getSteps();
    Assert.assertEquals(2, steps.size());
    Assert.assertTrue(steps.get(1) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier sqlgLocalStepBarrier = (SqlgLocalStepBarrier) steps.get(1);
    Assert.assertEquals(1, sqlgLocalStepBarrier.getLocalChildren().size());
    Traversal.Admin t = (Traversal.Admin) sqlgLocalStepBarrier.getLocalChildren().get(0);
    // or step is collapsed into the vertex step.
    Assert.assertEquals(1, t.getSteps().size());
    Assert.assertEquals(2, vertices.size());
}
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) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 8 with SqlgLocalStepBarrier

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

the class TestLocalVertexStepOptional method testLocalVertexStepOptimized.

@Test
public void testLocalVertexStepOptimized() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a11 = this.sqlgGraph.addVertex(T.label, "A", "name", "a11");
    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);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.gt.V(a11).local(__.optional(__.out()).values("name")).path();
    Assert.assertEquals(3, 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> sqlgVertexStepCompileds = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexStepCompileds.size());
    SqlgVertexStep sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(0);
    assertStep(sqlgVertexStepCompiled, false, false, true, true);
    Assert.assertEquals(1, paths.size());
    List<Predicate<Path>> pathsToAssert = Arrays.asList(p -> p.size() == 2 && p.get(0).equals(a11) && p.get(1).equals("a11"));
    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());
    paths = this.gt.V(a1).local(__.out().out().values("name")).path().toList();
    Assert.assertEquals(1, paths.size());
    pathsToAssert = Arrays.asList(p -> p.size() == 4 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1) && p.get(3).equals("c1"));
    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) TraversalHelper(org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper) Arrays(java.util.Arrays) Predicate(java.util.function.Predicate) Graph(org.apache.tinkerpop.gremlin.structure.Graph) BaseTest(org.umlg.sqlg.test.BaseTest) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Test(org.junit.Test) IOException(java.io.IOException) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) SqlgLocalStepBarrier(org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier) List(java.util.List) Optional(java.util.Optional) Assert(org.junit.Assert) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) 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)

Example 9 with SqlgLocalStepBarrier

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

the class TestLocalVertexStepOptional method testUnoptimizableChooseStep.

@Test
public void testUnoptimizableChooseStep() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V(a1).local(__.<Vertex, Vertex>choose(v -> v.label().equals("A"), __.out(), __.in()));
    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> sqlgVertexStepCompileds = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(2, sqlgVertexStepCompileds.size());
    SqlgVertexStep sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(0);
    assertStep(sqlgVertexStepCompiled, false, false, false, true);
    sqlgVertexStepCompiled = sqlgVertexStepCompileds.get(1);
    assertStep(sqlgVertexStepCompiled, false, false, false, true);
    Assert.assertEquals(2, vertices.size());
}
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 10 with SqlgLocalStepBarrier

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

the class TestLocalVertexStepOptional method testLocalOptional2.

@Test
public void testLocalOptional2() {
    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> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(2, sqlgVertexSteps.size());
    SqlgVertexStep sqlgVertexStep = sqlgVertexSteps.get(0);
    // isForMultipleQueries is arbitary here.
    // Some will be true and some false, depending on the out from which vertex.
    assertStep(sqlgVertexStep, false, false, true);
    sqlgVertexStep = sqlgVertexSteps.get(1);
    // isForMultipleQueries is arbitary here.
    // Some will be true and some false, depending on the out from which vertex.
    assertStep(sqlgVertexStep, 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() == 1 && p.get(0).equals(c1), p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(b2), 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