org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestAndStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testAndStepOptimized() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1", "surname", "aa1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2", "surname", "aa2");
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "a3", "surname", "aa3");
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .and(
                    __.has("name", "a1"),
                    __.has("surname", "aa1")
            );
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(a1));
}
 
Example #2
Source File: TestAndStepBarrier.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testAndStepBarrier() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    a1.addEdge("abb", b1);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a2.addEdge("abb", b2);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    a3.addEdge("abbb", b3);


    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("A").and(
            __.out("ab"),
            __.out("abb")
    );
    printTraversalForm(traversal);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.contains(a1));

    List<SqlgAndStepBarrier> sqlgAndStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgAndStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgAndStepBarriers.size());
}
 
Example #3
Source File: TestGremlinCompileWithHas.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_asXaX_out_asXaX_out_asXaX_selectXaX_byXunfold_valuesXnameX_foldX_rangeXlocal_1_2X() {
    loadModern();
    Graph g = this.sqlgGraph;
    assertModernGraph(g, true, false);
    DefaultGraphTraversal<Vertex, String> traversal = (DefaultGraphTraversal<Vertex, String>) g.traversal()
            .V().as("a")
            .out().as("a")
            .out().as("a")
            .<List<String>>select(Pop.all, "a")
            .by(__.unfold().values("name").fold())
            .<String>range(Scope.local, 1, 2);
    Assert.assertEquals(5, traversal.getSteps().size());
    int counter = 0;
    while (traversal.hasNext()) {
        final String s = traversal.next();
        Assert.assertEquals("josh", s);
        counter++;
    }
    Assert.assertEquals(2, counter);
    Assert.assertEquals(3, traversal.getSteps().size());
}
 
Example #4
Source File: TestRepeatStepGraphOut.java    From sqlg with MIT License 6 votes vote down vote up
@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().orElseThrow(IllegalStateException::new));
    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().orElseThrow(IllegalStateException::new));
    Assert.assertTrue(paths.isEmpty());
}
 
Example #5
Source File: TestRepeatStepGraphOut.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOnDuplicatePathsFromVertexTimes1After() {
    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().emit().repeat(__.out("ab", "ba")).times(1).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(5, 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().orElseThrow(IllegalStateException::new));
    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().orElseThrow(IllegalStateException::new));
    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().orElseThrow(IllegalStateException::new));
    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().orElseThrow(IllegalStateException::new));
    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().orElseThrow(IllegalStateException::new));
    Assert.assertTrue(paths.isEmpty());
}
 
Example #6
Source File: TestHasLabelAndId.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOutWithHasLabel() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c = this.sqlgGraph.addVertex(T.label, "C");
    Vertex d = this.sqlgGraph.addVertex(T.label, "D");
    a.addEdge("ab", b);
    a.addEdge("ac", c);
    a.addEdge("ad", d);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").out().hasLabel("B");
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
    Assert.assertEquals(2, sqlgGraphStep.getReplacedSteps().size());
    ReplacedStep replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(0);
    Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
    replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(1);
    Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
}
 
Example #7
Source File: TestHasLabelAndId.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testHasLabelWithWithinPredicate() {
    Vertex vEPerson = this.sqlgGraph.addVertex(T.label, "EnterprisePerson", "_uniqueId", "1");
    Vertex vEProvider = this.sqlgGraph.addVertex(T.label, "EnterpriseProvider", "_uniqueId", "2");
    Vertex vSPerson = this.sqlgGraph.addVertex(T.label, "SystemPerson", "_uniqueId", "3");
    Vertex vSProvider = this.sqlgGraph.addVertex(T.label, "SystemProvider", "_uniqueId", "4");
    Edge e1 = vSPerson.addEdge("euid", vEPerson);
    Edge e2 = vSProvider.addEdge("euid", vEProvider);
    Edge e3 = vSProvider.addEdge("primary", vSPerson);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V()
            .hasLabel("EnterprisePerson")
            .has("_uniqueId", "1")
            .in("euid")
            .bothE("primary")
            .otherV()
            .hasLabel("SystemPerson", "SystemProvider")
            .out("euid");
    printTraversalForm(traversal);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(vEProvider, vertices.get(0));
}
 
Example #8
Source File: TestRepeatStepVertexOut.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testSmallerRepeatWithEmitFirst() {
    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 c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    b1.addEdge("bc", c1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Path> traversal = (DefaultGraphTraversal<Vertex, Path>) this.sqlgGraph.traversal()
            .V(a1.id()).emit().repeat(__.out("ab", "bc")).times(2).path();
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Path> paths = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertEquals(4, 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() == 2 && p.get(0).equals(a1) && p.get(1).equals(b2)));
    paths.remove(paths.stream().filter(p -> p.size() == 2 && p.get(0).equals(a1) && p.get(1).equals(b2)).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(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.isEmpty());
}
 
Example #9
Source File: TestGremlinCompileWithHas.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testX() {
    Graph g = this.sqlgGraph;
    loadModern(this.sqlgGraph);
    assertModernGraph(g, true, false);
    final Object marko = convertToVertexId("marko");
    DefaultGraphTraversal<Vertex, Edge> traversal = (DefaultGraphTraversal<Vertex, Edge>) g.traversal().V(marko)
            .outE("knows").has("weight", 1.0d).as("here").inV().has("name", "josh").<Edge>select("here");
    Assert.assertEquals(6, traversal.getSteps().size());
    printTraversalForm(traversal);
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertTrue(traversal.hasNext());
    Assert.assertTrue(traversal.hasNext());
    final Edge edge = traversal.next();
    Assert.assertEquals("knows", edge.label());
    Assert.assertEquals(1.0d, edge.<Double>value("weight"), 0.00001d);
    Assert.assertFalse(traversal.hasNext());
    Assert.assertFalse(traversal.hasNext());
}
 
Example #10
Source File: TestRepeatStepGraphOut.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_repeatXoutX_timesX2X() {
    final List<DefaultGraphTraversal<Vertex, Vertex>> traversals = new ArrayList<>();
    loadModern();
    Graph graph = this.sqlgGraph;
    assertModernGraph(graph, true, false);

    DefaultGraphTraversal<Vertex, Vertex> t = (DefaultGraphTraversal<Vertex, Vertex>) 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());
    });
}
 
Example #11
Source File: TestHasLabelAndId.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testConsecutiveHasLabelWithout() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c = this.sqlgGraph.addVertex(T.label, "C");
    Vertex d = this.sqlgGraph.addVertex(T.label, "D");
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").has(T.label, P.without("B", "C"));
    Assert.assertEquals(1, traversal.toList().size());
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
    Assert.assertEquals(1, sqlgGraphStep.getReplacedSteps().size());
    ReplacedStep replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(0);
    Assert.assertEquals(2, replacedStep.getLabelHasContainers().size());
}
 
Example #12
Source File: TestOrStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrChained() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1","p1","v1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2","p1","v1");
    this.sqlgGraph.addVertex(T.label, "A", "name", "a3","p1","v1");
    this.sqlgGraph.addVertex(T.label, "A", "name", "a4","p1","v1");
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .or(
                    __.has("name", "a1").has("p1","v1"),
                    __.has("name", "a2"),
                    __.has("name", "a3").has("p1","v2")
            );
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.contains(a1) && vertices.contains(a2));
}
 
Example #13
Source File: TestGremlinCompileV.java    From sqlg with MIT License 6 votes vote down vote up
private void testOutOutWithLabels2_assert(SqlgGraph sqlgGraph, Vertex a, Vertex c) {
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) vertexTraversal(sqlgGraph, a).out("outB").out("outC");
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.contains(c));
    int count = 0;
    for (Vertex vertex : vertices) {
        if (vertex.equals(c)) {
            count++;
        }
    }
    Assert.assertEquals(2, count);
    Assert.assertEquals("c", vertices.get(vertices.indexOf(c)).value("nAmE"));
}
 
Example #14
Source File: TestHasLabelAndId.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testConsecutiveEqHasNeqIdAndLabels() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c = this.sqlgGraph.addVertex(T.label, "C");
    Vertex d = this.sqlgGraph.addVertex(T.label, "D");
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A", "B", "C", "D").has(T.id, P.neq(b.id()));
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(4, vertices.size());
    Assert.assertTrue(vertices.contains(a) && vertices.contains(b2) && vertices.contains(c) && vertices.contains(d));
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) traversal.getSteps().get(0);
    Assert.assertEquals(1, sqlgGraphStep.getReplacedSteps().size());
    ReplacedStep replacedStep = (ReplacedStep) sqlgGraphStep.getReplacedSteps().get(0);
    //without's labels are merged into the previous labelHasContainers
    //this is because without has 'or' logic rather than 'and'
    Assert.assertEquals(1, replacedStep.getLabelHasContainers().size());
}
 
Example #15
Source File: TestGremlinCompileChoose.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testUnoptimizableChooseStep() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    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()
            .hasLabel("A")
            .choose(
                    v -> v.label().equals("A"),
                    __.out(),
                    __.in()
            );
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(0) instanceof SqlgGraphStep);
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgChooseStepBarrier);
    Assert.assertEquals(2, vertices.size());
}
 
Example #16
Source File: TestGremlinCompileV.java    From sqlg with MIT License 6 votes vote down vote up
private void testOutOutWithLabels_assert(SqlgGraph sqlgGraph, Vertex a, Vertex c, Vertex d1, Vertex d2) {
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) vertexTraversal(sqlgGraph, a)
            .out("outB", "outE").out("outC", "outD");
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(4, vertices.size());
    Assert.assertTrue(vertices.contains(c));
    Assert.assertTrue(vertices.contains(d1));
    Assert.assertTrue(vertices.contains(d2));
    int count = 0;
    for (Vertex vertex : vertices) {
        if (vertex.equals(c)) {
            count++;
        }
    }
    Assert.assertEquals(2, count);
    Assert.assertEquals("c", vertices.get(vertices.indexOf(c)).value("nAmE"));
    Assert.assertEquals("d1", vertices.get(vertices.indexOf(d1)).value("NAME"));
    Assert.assertEquals("d2", vertices.get(vertices.indexOf(d2)).value("NAME"));
}
 
Example #17
Source File: TestTreeStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testTreeWithSideEffect() {
    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);

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A").out().out().tree("a");
    Assert.assertEquals(5, traversal.getSteps().size());
    Vertex v = traversal.next();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(Matchers.isOneOf(c1, c2, c3).matches(v));
}
 
Example #18
Source File: TestOrStepBarrier.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrStepBarrier() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    a2.addEdge("abb", b2);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    a3.addEdge("abbb", b3);


    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .or(
                    __.out("ab"),
                    __.out("abb")
            );
    printTraversalForm(traversal);
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.containsAll(Arrays.asList(a1, a2)));
    List<SqlgOrStepBarrier> sqlgOrStepBarriers = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgOrStepBarrier.class, traversal);
    Assert.assertEquals(1, sqlgOrStepBarriers.size());
}
 
Example #19
Source File: TestVertexStepOrderBy.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrderByToSelf() {

    Vertex root = this.sqlgGraph.addVertex(T.label, "Root");
    Vertex folder1 = this.sqlgGraph.addVertex(T.label, "Folder");
    Vertex folder2 = this.sqlgGraph.addVertex(T.label, "Folder");
    Edge e1 = root.addEdge("rootFolder", folder1);
    Edge e2 = folder1.addEdge("subFolder", folder2, "sequence", 1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal) this.sqlgGraph.traversal().V(folder1)
            .outE("subFolder").as("e")
            .inV().as("v")
            .select("e", "v")
            .order().by(__.select("e").by("sequence"), Order.incr)
            .map(m -> (Vertex) m.get().get("v"));
    Assert.assertEquals(6, traversal.getSteps().size());
    List<Vertex> result = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());

    Assert.assertEquals(1, result.size());
}
 
Example #20
Source File: TestVertexStepPerformance.java    From sqlg with MIT License 6 votes vote down vote up
public void testGroup() {
    loadGratefulDead();

    for (int i = 0; i < 10000; i++) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        DefaultGraphTraversal<Vertex, Map<String, Map<String, Number>>> traversal = (DefaultGraphTraversal<Vertex, Map<String, Map<String, Number>>>) this.sqlgGraph.traversal()
                .V()
                .out("followedBy")
                .<String, Map<String, Number>>group()
                .by("songType")
                .by(
                        __.bothE()
                                .group()
                                .by(T.label)
                                .by(
                                        __.values("weight").sum()
                                )
                );

        final Map<String, Map<String, Number>> map = traversal.next();
        stopWatch.stop();
        System.out.println(stopWatch.toString());
    }
}
 
Example #21
Source File: TestGremlinCompileWithHas.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_asXaX_out_asXaX_out_asXaX_selectXaX_byXunfold_valuesXnameX_foldX_rangeXlocal_1_2X_Simple() {
    loadModern();
    Graph g = this.sqlgGraph;
    assertModernGraph(g, true, false);
    DefaultGraphTraversal<Vertex, List<Vertex>> traversal = (DefaultGraphTraversal<Vertex, List<Vertex>>) g.traversal()
            .V().as("a")
            .out().as("a")
            .out().as("a")
            .<List<Vertex>>select(Pop.all, "a");
    Assert.assertEquals(4, traversal.getSteps().size());
    printTraversalForm(traversal);
    Assert.assertEquals(2, traversal.getSteps().size());
    int counter = 0;
    while (traversal.hasNext()) {
        final List<Vertex> s = traversal.next();
        Assert.assertEquals(3, s.size());
        System.out.println(s);
        counter++;
    }
    Assert.assertEquals(2, counter);
}
 
Example #22
Source File: TestGremlinCompileGraphStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_localXinEXknowsX_limitX2XX_outV_name() {
    loadModern();
    assertModernGraph(this.sqlgGraph, true, false);
    DefaultGraphTraversal<Vertex, String> traversal = (DefaultGraphTraversal<Vertex, String>)this.sqlgGraph.traversal()
            .V()
            .local(
                    __.inE("knows").limit(2)
            )
            .outV()
            .<String>values("name");
    Assert.assertEquals(4, traversal.getSteps().size());
    printTraversalForm(traversal);
    Assert.assertEquals(4, traversal.getSteps().size());
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        Assert.assertEquals(traversal.next(), "marko");
    }
    Assert.assertFalse(traversal.hasNext());
    Assert.assertEquals(2, counter);
}
 
Example #23
Source File: TestOrStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrStepOptimizedWith3Ors() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "a3");
    this.sqlgGraph.addVertex(T.label, "A", "name", "a4");
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .or(
                    __.has("name", "a1"),
                    __.has("name", "a2"),
                    __.has("name", "a3")
            );
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(a1) && vertices.contains(a2) && vertices.contains(a3));
}
 
Example #24
Source File: TestGremlinCompileWithHas.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testY() {
    Graph g = this.sqlgGraph;
    loadModern(this.sqlgGraph);
    assertModernGraph(g, true, false);
    Object marko = convertToVertexId(g, "marko");
    DefaultGraphTraversal<Vertex, String> traversal = (DefaultGraphTraversal<Vertex, String>) g.traversal().V(marko).outE("knows").bothV().<String>values("name");
    Assert.assertEquals(4, traversal.getSteps().size());
    printTraversalForm(traversal);
    Assert.assertEquals(2, traversal.getSteps().size());
    final List<String> names = traversal.toList();
    Assert.assertEquals(4, names.size());
    Assert.assertTrue(names.contains("marko"));
    Assert.assertTrue(names.contains("josh"));
    Assert.assertTrue(names.contains("vadas"));
    names.remove("marko");
    Assert.assertEquals(3, names.size());
    names.remove("marko");
    Assert.assertEquals(2, names.size());
    names.remove("josh");
    Assert.assertEquals(1, names.size());
    names.remove("vadas");
    Assert.assertEquals(0, names.size());
}
 
Example #25
Source File: TestOptionalWithRange.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testSimpleOrderWithRange() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    for (int i = 0; i < 10; i++) {
        Vertex b = this.sqlgGraph.addVertex(T.label, "B", "order", i);
        a1.addEdge("ab", b);
        for (int j = 0; j < 10; j++) {
            Vertex c = this.sqlgGraph.addVertex(T.label, "C", "order", i);
            b.addEdge("bc", c);
        }
    }
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .optional(
                    __.out().optional(
                            __.out().range(5, 6)
                    )
            );
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(1, vertices.size());


}
 
Example #26
Source File: TestRepeatStepVertexOut.java    From sqlg with MIT License 5 votes vote down vote up
@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(a1).repeat(__.out("ab").out("bc")).times(1);
    Assert.assertEquals(2, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.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> traversal1 = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V(a1).repeat(__.out("ab", "bc")).times(2);
    Assert.assertEquals(2, traversal1.getSteps().size());
    vertices = traversal1.toList();
    Assert.assertEquals(1, traversal1.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertTrue(vertices.contains(c1));
    Assert.assertTrue(vertices.contains(c2));
    Assert.assertTrue(vertices.contains(c3));
}
 
Example #27
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderOnEdgeWithInVValues() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "A","name","b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "A","name","b2");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "A","name","b3");
    a1.addEdge("ab", b1, "order", 0);
    a1.addEdge("ab", b2, "order", 11);
    a1.addEdge("ab", b3, "order", 2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Object> traversal = (DefaultGraphTraversal<Vertex, Object>) this.sqlgGraph.traversal()
            .V(a1.id())
            .outE("ab")
            .order().by("order", Order.decr)
            .inV().values("name");
    assertEquals(5, traversal.getSteps().size());
    List<Object> vertices = traversal.toList();
    assertEquals(2, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, false, false, false);
    assertEquals(3, vertices.size());
    assertEquals("b2", vertices.get(0));
    assertEquals("b3", vertices.get(1));
    assertEquals("b1", vertices.get(2));

    traversal = (DefaultGraphTraversal<Vertex, Object>) this.sqlgGraph.traversal()
            .V(a1.id())
            .outE("ab")
            .order().by("order", Order.incr)
            .inV().values("name");
    assertEquals(5, traversal.getSteps().size());
    vertices = traversal.toList();
    assertEquals(2, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, false, false, false);
    assertEquals(3, vertices.size());
    assertEquals("b1", vertices.get(0));
    assertEquals("b3", vertices.get(1));
    assertEquals("b2", vertices.get(2));
}
 
Example #28
Source File: TestVertexStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderby() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "a");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "c");
    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(a1)
            .out()
            .order()
            .by("name", Order.incr);
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> result = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(b1, result.get(0));
    Assert.assertEquals(b2, result.get(1));
    Assert.assertEquals(b3, result.get(2));

    DefaultGraphTraversal<Vertex, Vertex> traversal1 = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V(a1)
            .out()
            .order()
            .by("name", Order.decr);
    Assert.assertEquals(3, traversal1.getSteps().size());
    result = traversal1.toList();
    Assert.assertEquals(1, traversal1.getSteps().size());
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(b1, result.get(2));
    Assert.assertEquals(b2, result.get(1));
    Assert.assertEquals(b3, result.get(0));
}
 
Example #29
Source File: TestGremlinCompileE.java    From sqlg with MIT License 5 votes vote down vote up
private void testOutE_assert(SqlgGraph sqlgGraph, Vertex a1, Edge e) {
    DefaultGraphTraversal<Vertex, Long> traversal = (DefaultGraphTraversal<Vertex, Long>) vertexTraversal(sqlgGraph, a1).outE().count();
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertEquals(1, traversal.next().intValue());
    Assert.assertEquals(2, traversal.getSteps().size());

    DefaultGraphTraversal<Vertex, Edge> traversal1 = (DefaultGraphTraversal<Vertex, Edge>) vertexTraversal(sqlgGraph, a1).outE();
    Assert.assertEquals(2, traversal1.getSteps().size());
    Assert.assertEquals(e, traversal1.next());
    Assert.assertEquals(1, traversal1.getSteps().size());
}
 
Example #30
Source File: TestRangeLimit.java    From sqlg with MIT License 5 votes vote down vote up
@Test
    public void testRangeOnMultipleLabelsOffset() {
        for (int i = 0; i < 20; i++) {
            Vertex a = this.sqlgGraph.addVertex(T.label, "A", "name", "a" + i);
            Vertex b = this.sqlgGraph.addVertex(T.label, "B", "name", "b" + i);
            a.addEdge("E", b, "name", "e" + i);
        }
        this.sqlgGraph.tx().commit();
        DefaultGraphTraversal<Vertex, Object> g = (DefaultGraphTraversal<Vertex, Object>) this.sqlgGraph.traversal()
                .V().hasLabel("A", "B")
                .range(1, 4)
                .values("name");
        Assert.assertEquals(4, g.getSteps().size());
        ensureRangeGlobal(g);
        int cnt = 0;
        Set<String> names = new HashSet<>();
        if (g.hasNext()) {
            Assert.assertEquals(2, g.getSteps().size());
            Assert.assertTrue(g.getSteps().get(0) instanceof SqlgGraphStep);
            SqlgGraphStep sqlgGraphStep = (SqlgGraphStep) g.getSteps().get(0);
            assertStep(sqlgGraphStep, true, false, true, true, false);
        }
        while (g.hasNext()) {
            String n = (String) g.next();
            names.add(n);
            cnt++;
        }
        // cannot have offset on different labels
//        ensureRangeGlobal(g);
        Assert.assertEquals(3, cnt);
        Assert.assertEquals(names.toString(), 3, names.size());
    }