Search in sources :

Example 6 with SqlgVertexStep

use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.

the class TestLocalVertexStepOptionalWithOrder method testOptionalWithOrderAndRange2.

@Test
public void testOptionalWithOrderAndRange2() {
    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").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"));
}
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) LocalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 7 with SqlgVertexStep

use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.

the class TestOptionalWithOrder method testOptionalWithOrderAndRange2.

@Test
public void testOptionalWithOrderAndRange2() {
    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").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
    assertStep(traversal.getSteps().get(0), true, false, false, true);
    Step<?, ?> step = traversal.getSteps().get(1);
    Assert.assertTrue(step instanceof SqlgOptionalStepBarrier);
    // SqlgOptionalStepBarrier<?,?,?> sqlgOptionalStepBarrier = (SqlgOptionalStepBarrier<?, ?, ?>) step;
    SqlgOptionalStepBarrier<?> sqlgOptionalStepBarrier = (SqlgOptionalStepBarrier<?>) step;
    Traversal.Admin<?, ?> traversal1 = sqlgOptionalStepBarrier.getLocalChildren().get(0);
    Assert.assertTrue(traversal1.getSteps().get(0) instanceof SqlgVertexStep);
    assertStep(traversal1.getSteps().get(0), false, true, true, true);
    // There is another SqlgVertexStep but it is not being asserted
    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"));
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) 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) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) List(java.util.List) 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) 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) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgOptionalStepBarrier(org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 8 with SqlgVertexStep

use of org.umlg.sqlg.step.SqlgVertexStep in project sqlg by pietermartin.

the class TestRepeatStepGraphBoth method testEmitRepeatWithVertexStepAfter.

@Test
public void testEmitRepeatWithVertexStepAfter() {
    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 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");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    b1.addEdge("bc", c1);
    b2.addEdge("bc", c2);
    b2.addEdge("bc", c3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").repeat(__.out()).emit().times(3).out();
    printTraversalForm(traversal);
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgVertexStep);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.containsAll(Arrays.asList(c1, c2, c3)));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 9 with SqlgVertexStep

use of org.umlg.sqlg.step.SqlgVertexStep 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)

Example 10 with SqlgVertexStep

use of org.umlg.sqlg.step.SqlgVertexStep 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)

Aggregations

SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)21 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)19 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)19 Test (org.junit.Test)19 BaseTest (org.umlg.sqlg.test.BaseTest)19 SqlgLocalStepBarrier (org.umlg.sqlg.step.barrier.SqlgLocalStepBarrier)16 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)12 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)9 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)8 SqlgOptionalStepBarrier (org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier)7 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)6 OrderGlobalStep (org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep)6 IdentityStep (org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep)6 T (org.apache.tinkerpop.gremlin.structure.T)6 Assert (org.junit.Assert)6 Predicate (java.util.function.Predicate)5 Order (org.apache.tinkerpop.gremlin.process.traversal.Order)5 LocalStep (org.apache.tinkerpop.gremlin.process.traversal.step.branch.LocalStep)5