Search in sources :

Example 21 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project sqlg by pietermartin.

the class TestLocalVertexStepRepeatStep method testLocalRepeatStepEmitTimesBefore.

@Test
public void testLocalRepeatStepEmitTimesBefore() {
    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);
    this.sqlgGraph.tx().commit();
    List<Path> paths = this.gt.V(a1).local(__.<Vertex>emit().times(2).repeat(__.out())).path().toList();
    for (Path path : paths) {
        System.out.println(path);
    }
    assertEquals(3, paths.size());
    List<Predicate<Path>> pathsToAssert = Arrays.asList(p -> p.size() == 1 && p.get(0).equals(a1), p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1), p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(b1) && p.get(2).equals(c1));
    for (Predicate<Path> pathPredicate : pathsToAssert) {
        Optional<Path> path = paths.stream().filter(pathPredicate).findAny();
        assertTrue(path.isPresent());
        assertTrue(paths.remove(path.get()));
    }
    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) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 22 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project sqlg by pietermartin.

the class TestLocalVertexStepRepeatStep method testEmitTimes2MultiplePaths.

@Test
public void testEmitTimes2MultiplePaths() {
    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("aa", a2);
    a2.addEdge("ab", b1);
    this.sqlgGraph.tx().commit();
    List<Path> paths = this.sqlgGraph.traversal().V().local(__.<Vertex>emit().times(2).repeat(__.out()).path()).toList();
    assertEquals(6, paths.size());
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a1)));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a2)));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(b1)));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(a2)));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(b1)));
    assertTrue(paths.stream().anyMatch(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(a2) && p.get(2).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a1)).findAny().get());
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a2)).findAny().get());
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(b1)).findAny().get());
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(a2)).findAny().get());
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a2) && p.get(1).equals(b1)).findAny().get());
    paths.remove(paths.stream().filter(p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(a2) && p.get(2).equals(b1)).findAny().get());
    assertTrue(paths.isEmpty());
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) java.util(java.util) Predicate(java.util.function.Predicate) Graph(org.apache.tinkerpop.gremlin.structure.Graph) BaseTest(org.umlg.sqlg.test.BaseTest) Assert.assertTrue(org.junit.Assert.assertTrue) 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) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Assert(org.junit.Assert) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) MapHelper(org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper) Assert.assertEquals(org.junit.Assert.assertEquals) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 23 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project sqlg by pietermartin.

the class TestLocalVertexStepWithOrder method testSqlgVertexStepOrderStartsProperly.

@Test
public void testSqlgVertexStepOrderStartsProperly() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1", "order", 1);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2", "order", 2);
    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, "order", 1);
    a1.addEdge("ab", b2, "order", 2);
    a1.addEdge("ab", b3, "order", 3);
    a2.addEdge("ab", b1, "order", 1);
    a2.addEdge("ab", b2, "order", 2);
    a2.addEdge("ab", b3, "order", 3);
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1", "order", 1);
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2", "order", 2);
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "c3", "order", 3);
    Vertex c4 = this.sqlgGraph.addVertex(T.label, "C", "name", "c4", "order", 1);
    Vertex c5 = this.sqlgGraph.addVertex(T.label, "C", "name", "c5", "order", 2);
    Vertex c6 = this.sqlgGraph.addVertex(T.label, "C", "name", "c6", "order", 3);
    Vertex c7 = this.sqlgGraph.addVertex(T.label, "C", "name", "c7", "order", 1);
    Vertex c8 = this.sqlgGraph.addVertex(T.label, "C", "name", "c8", "order", 2);
    Vertex c9 = this.sqlgGraph.addVertex(T.label, "C", "name", "c9", "order", 3);
    b1.addEdge("bc", c1);
    b1.addEdge("bc", c2);
    b1.addEdge("bc", c3);
    b2.addEdge("bc", c4);
    b2.addEdge("bc", c5);
    b2.addEdge("bc", c6);
    b3.addEdge("bc", c7);
    b3.addEdge("bc", c8);
    b3.addEdge("bc", c9);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().hasLabel("A").order().by("order").local(__.outE("ab").order().by("order", Order.decr).inV().local(__.out("bc").order().by("order", Order.decr))).path();
    List<Path> paths = traversal.toList();
    for (Path path : paths) {
        System.out.println(path.toString() + " " + ((Vertex) path.objects().get(3)).<String>value("name"));
    }
    Assert.assertEquals(18, paths.size());
    Assert.assertEquals(a1, paths.get(0).objects().get(0));
    Assert.assertEquals(c9, paths.get(0).objects().get(3));
    Assert.assertEquals(a1, paths.get(1).objects().get(0));
    Assert.assertEquals(c8, paths.get(1).objects().get(3));
    Assert.assertEquals(a1, paths.get(2).objects().get(0));
    Assert.assertEquals(c7, paths.get(2).objects().get(3));
    Assert.assertEquals(a1, paths.get(3).objects().get(0));
    Assert.assertEquals(c6, paths.get(3).objects().get(3));
    Assert.assertEquals(a1, paths.get(4).objects().get(0));
    Assert.assertEquals(c5, paths.get(4).objects().get(3));
    Assert.assertEquals(a1, paths.get(5).objects().get(0));
    Assert.assertEquals(c4, paths.get(5).objects().get(3));
    Assert.assertEquals(a1, paths.get(6).objects().get(0));
    Assert.assertEquals(c3, paths.get(6).objects().get(3));
    Assert.assertEquals(a1, paths.get(7).objects().get(0));
    Assert.assertEquals(c2, paths.get(7).objects().get(3));
    Assert.assertEquals(a1, paths.get(8).objects().get(0));
    Assert.assertEquals(c1, paths.get(8).objects().get(3));
    Assert.assertEquals(a2, paths.get(9).objects().get(0));
    Assert.assertEquals(c9, paths.get(9).objects().get(3));
    Assert.assertEquals(a2, paths.get(10).objects().get(0));
    Assert.assertEquals(c8, paths.get(10).objects().get(3));
    Assert.assertEquals(a2, paths.get(11).objects().get(0));
    Assert.assertEquals(c7, paths.get(11).objects().get(3));
    Assert.assertEquals(a2, paths.get(12).objects().get(0));
    Assert.assertEquals(c6, paths.get(12).objects().get(3));
    Assert.assertEquals(a2, paths.get(13).objects().get(0));
    Assert.assertEquals(c5, paths.get(13).objects().get(3));
    Assert.assertEquals(a2, paths.get(14).objects().get(0));
    Assert.assertEquals(c4, paths.get(14).objects().get(3));
    Assert.assertEquals(a2, paths.get(15).objects().get(0));
    Assert.assertEquals(c3, paths.get(15).objects().get(3));
    Assert.assertEquals(a2, paths.get(16).objects().get(0));
    Assert.assertEquals(c2, paths.get(16).objects().get(3));
    Assert.assertEquals(a2, paths.get(17).objects().get(0));
    Assert.assertEquals(c1, paths.get(17).objects().get(3));
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 24 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project sqlg by pietermartin.

the class TestVertexStepPerformance method testLocalVertexStepNotOptimizedPerformance.

// @Test
public void testLocalVertexStepNotOptimizedPerformance() {
    this.sqlgGraph.tx().normalBatchModeOn();
    for (int i = 0; i < 10_000; i++) {
        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();
    StopWatch stopWatch = new StopWatch();
    for (int i = 0; i < 1000; i++) {
        DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.gt.V().local(__.optional(__.where(__.has(T.label, "A")).out())).path();
        stopWatch.start();
        List<Path> paths = traversal.toList();
        stopWatch.stop();
        System.out.println(stopWatch.toString());
        stopWatch.reset();
        Assert.assertEquals(30_000, paths.size());
    }
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 25 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project sqlg by pietermartin.

the class TestRepeatStepGraphOut method testOnDuplicatePathsFromVertexTimes1EmitAfter.

@Test
public void testOnDuplicatePathsFromVertexTimes1EmitAfter() {
    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);
    b1.addEdge("ba", a2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().repeat(__.out("ab", "ba")).emit().times(1).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(2, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b1)).findAny().get());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(a2)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(b1) && p.get(1).equals(a2)).findAny().get());
    Assert.assertTrue(paths.isEmpty());
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) java.util(java.util) SqlgRepeatStepBarrier(org.umlg.sqlg.step.barrier.SqlgRepeatStepBarrier) 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) StopWatch(org.apache.commons.lang3.time.StopWatch) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Tree(org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree) T(org.apache.tinkerpop.gremlin.structure.T) SqlgVertexStep(org.umlg.sqlg.step.SqlgVertexStep) Assert(org.junit.Assert) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) MapHelper(org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

Path (org.apache.tinkerpop.gremlin.process.traversal.Path)137 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)131 Test (org.junit.Test)125 BaseTest (org.umlg.sqlg.test.BaseTest)125 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)81 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)74 T (org.apache.tinkerpop.gremlin.structure.T)74 Predicate (java.util.function.Predicate)73 Assert (org.junit.Assert)73 Graph (org.apache.tinkerpop.gremlin.structure.Graph)50 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)47 IOException (java.io.IOException)46 java.util (java.util)39 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)39 List (java.util.List)38 MapHelper (org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper)38 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)32 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)26 StopWatch (org.apache.commons.lang3.time.StopWatch)22 Map (java.util.Map)19