Search in sources :

Example 21 with DefaultGraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal in project sqlg by pietermartin.

the class TestRepeatStepGraphOut method testRepeat.

@Test
public void testRepeat() {
    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);
    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);
    b1.addEdge("bc", c2);
    b1.addEdge("bc", c3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").times(0).repeat(__.out("ab").out("bc"));
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(a1));
    DefaultGraphTraversal<Vertex, Vertex> traversal1 = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").repeat(__.out("ab", "bc")).times(1);
    Assert.assertEquals(3, traversal1.getSteps().size());
    vertices = traversal1.toList();
    Assert.assertEquals(1, traversal1.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(b1));
    Assert.assertTrue(vertices.contains(b2));
    Assert.assertTrue(vertices.contains(b3));
    DefaultGraphTraversal<Vertex, Vertex> traversal2 = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").times(1).repeat(__.out("ab", "bc"));
    Assert.assertEquals(3, traversal2.getSteps().size());
    vertices = traversal2.toList();
    Assert.assertEquals(1, traversal2.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(b1));
    Assert.assertTrue(vertices.contains(b2));
    Assert.assertTrue(vertices.contains(b3));
    DefaultGraphTraversal<Vertex, Vertex> traversal3 = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").repeat(__.out("ab", "bc")).times(2);
    Assert.assertEquals(3, traversal3.getSteps().size());
    vertices = traversal3.toList();
    Assert.assertEquals(1, traversal3.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(c1));
    Assert.assertTrue(vertices.contains(c2));
    Assert.assertTrue(vertices.contains(c3));
    DefaultGraphTraversal<Vertex, Vertex> traversal4 = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").times(2).repeat(__.out("ab", "bc"));
    Assert.assertEquals(3, traversal4.getSteps().size());
    vertices = traversal4.toList();
    Assert.assertEquals(1, traversal4.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(c1));
    Assert.assertTrue(vertices.contains(c2));
    Assert.assertTrue(vertices.contains(c3));
}
Also used : 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 22 with DefaultGraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal in project sqlg by pietermartin.

the class TestRepeatStepGraphOut method g_V_repeatXoutX_timesX2X.

@Test
public void g_V_repeatXoutX_timesX2X() throws IOException {
    final List<DefaultGraphTraversal<Vertex, Vertex>> traversals = new ArrayList<>();
    loadModern();
    Graph graph = this.sqlgGraph;
    assertModernGraph(graph, true, false);
    DefaultGraphTraversal t = (DefaultGraphTraversal) graph.traversal().V().repeat(__.out()).times(2);
    Assert.assertEquals(2, t.getSteps().size());
    traversals.add(t);
    traversals.forEach(traversal -> {
        printTraversalForm(traversal);
        Assert.assertEquals(1, traversal.getSteps().size());
        int counter = 0;
        while (traversal.hasNext()) {
            counter++;
            Vertex vertex = traversal.next();
            Assert.assertTrue(vertex.value("name").equals("lop") || vertex.value("name").equals("ripple"));
        }
        Assert.assertEquals(2, counter);
        Assert.assertFalse(traversal.hasNext());
    });
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Graph(org.apache.tinkerpop.gremlin.structure.Graph) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 23 with DefaultGraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal in project sqlg by pietermartin.

the class TestRepeatStepGraphOut method testRepeatWithTimesBefore.

@Test
public void testRepeatWithTimesBefore() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    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.sqlgGraph.traversal().V().emit().times(2).repeat(__.out()).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(3, paths.size());
    Assert.assertTrue(paths.stream().anyMatch(p -> p.size() == 1 && p.get(0).equals(a1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(a1)).findAny().get());
    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() == 1 && p.get(0).equals(b1)));
    paths.remove(paths.stream().filter(p -> p.size() == 1 && p.get(0).equals(b1)).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)

Example 24 with DefaultGraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal in project sqlg by pietermartin.

the class TestRepeatStepGraphOut method testTimesBeforeEmitBeforeToSelf.

@Test
public void testTimesBeforeEmitBeforeToSelf() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
    a1.addEdge("aa", a2);
    a1.addEdge("ab", b1);
    a2.addEdge("aa", a3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V(a1).emit().times(2).repeat(__.out()).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    for (Path path : paths) {
        System.out.println(path);
    }
    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() == 2 && p.get(0).equals(a1) && p.get(1).equals(a2), p -> p.size() == 3 && p.get(0).equals(a1) && p.get(1).equals(a2) && p.get(2).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) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Predicate(java.util.function.Predicate) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 25 with DefaultGraphTraversal

use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal in project sqlg by pietermartin.

the class TestRepeatStepGraphOut method testOnLeftJoinOnLeaveNode.

@Test
public void testOnLeftJoinOnLeaveNode() {
    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);
    a2.addEdge("ab", b1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").emit().repeat(__.out("ab")).times(1);
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(4, vertices.size());
}
Also used : 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

DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)270 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)260 Test (org.junit.Test)251 BaseTest (org.umlg.sqlg.test.BaseTest)251 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)80 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)78 T (org.apache.tinkerpop.gremlin.structure.T)56 Assert (org.junit.Assert)56 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)55 Graph (org.apache.tinkerpop.gremlin.structure.Graph)49 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)43 List (java.util.List)35 Predicate (java.util.function.Predicate)35 Map (java.util.Map)31 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)31 Edge (org.apache.tinkerpop.gremlin.structure.Edge)30 ReplacedStep (org.umlg.sqlg.sql.parse.ReplacedStep)29 SqlgVertex (org.umlg.sqlg.structure.SqlgVertex)24 java.util (java.util)23 IOException (java.io.IOException)22