Search in sources :

Example 41 with DefaultGraphTraversal

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

the class TestOptionalWithOrder 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").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(2, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, 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) 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) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 42 with DefaultGraphTraversal

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

the class TestOptionalWithOrder method testOptionalWithOrder2.

@Test
public void testOptionalWithOrder2() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 13);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "order", 12);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "order", 11);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    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").as("a").optional(__.outE().as("e").otherV().as("v")).order().by("order");
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, false, false, true);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof IdentityStep);
    Assert.assertTrue(traversal.getSteps().get(2) instanceof OrderGlobalStep);
    Assert.assertEquals(5, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
    Assert.assertEquals(a3, vertices.get(3));
    Assert.assertEquals(a2, vertices.get(4));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) IdentityStep(org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IdentityStep) OrderGlobalStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.OrderGlobalStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 43 with DefaultGraphTraversal

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

the class TestPathStep method g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_by.

@Test
public void g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_by() throws IOException {
    Graph graph = this.sqlgGraph;
    loadModern(this.sqlgGraph);
    assertModernGraph(graph, true, false);
    GraphTraversalSource g = graph.traversal();
    DefaultGraphTraversal<Vertex, Map<String, Object>> traversal = (DefaultGraphTraversal<Vertex, Map<String, Object>>) g.V().has(T.label, "person").as("a").local(__.out("created").as("b")).select("a", "b").by("name").by(T.id);
    Assert.assertEquals(4, traversal.getSteps().size());
    printTraversalForm(traversal);
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertEquals(4, traversal.getSteps().size());
    int counter = 0;
    while (traversal.hasNext()) {
        final Map<String, Object> map = traversal.next();
        counter++;
        Assert.assertEquals(2, map.size());
        if (map.get("a").equals("marko")) {
            Assert.assertEquals(convertToVertexId("lop"), map.get("b"));
        } else if (map.get("a").equals("josh")) {
            Assert.assertTrue(convertToVertexId("lop").equals(map.get("b")) || convertToVertexId("ripple").equals(map.get("b")));
        } else if (map.get("a").equals("peter")) {
            Assert.assertEquals(convertToVertexId("lop"), map.get("b"));
        } else {
            Assert.fail("The following map should not have been returned: " + map);
        }
    }
    Assert.assertEquals(4, counter);
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) Graph(org.apache.tinkerpop.gremlin.structure.Graph) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) Map(java.util.Map) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 44 with DefaultGraphTraversal

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

the class TestPathStep method testSimplePath.

@Test
public void testSimplePath() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1", "age", 1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V().as("a").has("name", "a1").as("b").has("age", 1).as("c").path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertEquals(1, paths.size());
    Assert.assertEquals(1, paths.get(0).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) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 45 with DefaultGraphTraversal

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

the class TestPathStep method testVertexPathBackToSelf.

@Test
public void testVertexPathBackToSelf() {
    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> gt = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal().V(a1).out().in().path();
    Assert.assertEquals(4, gt.getSteps().size());
    Path p = gt.next();
    Assert.assertEquals(2, gt.getSteps().size());
    Assert.assertEquals(3, p.size());
    Assert.assertEquals(a1, p.get(0));
    Assert.assertEquals(b1, p.get(1));
    Assert.assertEquals(a1, p.get(2));
}
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)

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