Java Code Examples for org.apache.tinkerpop.gremlin.process.traversal.Traversal#hasNext()

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Traversal#hasNext() . 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: ComplexTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.GRATEFUL)
public void playlistPaths() {
    final Traversal<Vertex, Map<String, List<String>>> traversal = getPlaylistPaths();
    printTraversalForm(traversal);
    assertTrue(traversal.hasNext());
    Map<String, List<String>> map = traversal.next();
    assertTrue(map.get("artists").contains("Bob_Dylan"));
    boolean hasJohnnyCash = false;
    while (traversal.hasNext()) {
        map = traversal.next();
        if (map.get("artists").contains("Johnny_Cash"))
            hasJohnnyCash = true;
    }
    assertTrue(hasJohnnyCash);
    assertTrue(map.get("artists").contains("Grateful_Dead"));
}
 
Example 2
Source File: WhereTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_hasXageX_asXaX_out_in_hasXageX_asXbX_selectXa_bX_whereXa_neqXbXX() {
    final Traversal<Vertex, Map<String, Object>> traversal = get_g_V_hasXageX_asXaX_out_in_hasXageX_asXbX_selectXa_bX_whereXa_neqXbXX();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        final Map<String, Object> map = traversal.next();
        assertEquals(2, map.size());
        assertTrue(map.containsKey("a"));
        assertTrue(map.containsKey("b"));
        assertNotEquals(map.get("a"), map.get("b"));
        assertTrue(((Vertex) map.get("a")).id().equals(convertToVertexId("marko")) ||
                ((Vertex) map.get("a")).id().equals(convertToVertexId("peter")) ||
                ((Vertex) map.get("a")).id().equals(convertToVertexId("josh")));
        assertTrue(((Vertex) map.get("b")).id().equals(convertToVertexId("marko")) ||
                ((Vertex) map.get("b")).id().equals(convertToVertexId("peter")) ||
                ((Vertex) map.get("b")).id().equals(convertToVertexId("josh")));
    }
    assertEquals(6, counter);
    assertFalse(traversal.hasNext());
}
 
Example 3
Source File: TestPropertyValues.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testValueMapOneObject() {
    loadModern();
    final Traversal<Vertex, Map<Object, Object>> traversal = sqlgGraph.traversal().V().hasLabel("person").valueMap("name");
    checkColumnsNotPresent(traversal, "age");
    checkRestrictedProperties(SqlgGraphStep.class, traversal, 0, "name");
    Set<String> names = new HashSet<>();
    while (traversal.hasNext()) {
        Map<Object, Object> m = traversal.next();
        Assert.assertNotNull(m);
        Assert.assertEquals(1, m.size());
        Assert.assertTrue(m.containsKey("name"));
        Object v = m.get("name");
        // "It is important to note that the map of a vertex maintains a list of values for each key."
        Assert.assertTrue(v instanceof List<?>);
        List<?> l = (List<?>) v;
        Assert.assertEquals(1, l.size());
        Object v1 = l.get(0);
        Assert.assertTrue(v1 instanceof String);
        names.add((String) v1);
    }
    Assert.assertEquals(new HashSet<>(Arrays.asList("marko", "vadas", "josh", "peter")), names);
}
 
Example 4
Source File: LocalTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_byXidX() {
    final Traversal<Vertex, Map<String, Object>> traversal = get_g_V_hasXlabel_personX_asXaX_localXoutXcreatedX_asXbXX_selectXa_bX_byXnameX_byXidX();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        final Map<String, Object> map = traversal.next();
        counter++;
        assertEquals(2, map.size());
        if (map.get("a").equals("marko")) {
            assertEquals(convertToVertexId("lop"), map.get("b"));
        } else if (map.get("a").equals("josh")) {
            assertTrue(convertToVertexId("lop").equals(map.get("b")) || convertToVertexId("ripple").equals(map.get("b")));
        } else if (map.get("a").equals("peter")) {
            assertEquals(convertToVertexId("lop"), map.get("b"));
        } else {
            fail("The following map should not have been returned: " + map);
        }
    }
    assertEquals(4, counter);
}
 
Example 5
Source File: TestSample.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_localXoutE_sampleX1X_byXweightXX() {
    loadModern();
    final Traversal<Vertex, Edge> traversal = this.sqlgGraph.traversal()
            .V()
            .local(
                    __.outE().sample(1).by("weight")
            );
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        traversal.next();
    }
    Assert.assertEquals(3, counter);
    Assert.assertFalse(traversal.hasNext());
}
 
Example 6
Source File: ValueMapTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * TINKERPOP-1483
 */
@Test
@LoadGraphWith(MODERN)
public void g_V_hasLabelXpersonX_filterXoutEXcreatedXX_valueMap_withXtokensX() {
	final Traversal<Vertex,Map<Object,Object>> gt = get_g_V_hasLabelXpersonX_filterXoutEXcreatedXX_valueMap_withXtokensX();
	int cnt = 0;
	while(gt.hasNext()){
		final Map<Object,Object> m = gt.next();
		assertTrue(m.size() > 0);
		for (Object o : m.keySet()){
			assertNotNull(m.get(o));
		}
		assertTrue(m.containsKey(id));
		assertTrue(m.containsKey(label));
		assertEquals("person",m.get(label));
		cnt++;
	}
	// check we had results
	assertTrue(cnt > 0);
}
 
Example 7
Source File: FilterTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_filterXlang_eq_javaX() {
    final Traversal<Vertex, Vertex> traversal = get_g_V_filterXlang_eq_javaX();
    printTraversalForm(traversal);
    int counter = 0;
    final Set<Vertex> vertices = new HashSet<>();
    while (traversal.hasNext()) {
        counter++;
        final Vertex vertex = traversal.next();
        vertices.add(vertex);
        assertTrue(vertex.value("name").equals("ripple") ||
                vertex.value("name").equals("lop"));
    }
    assertEquals(2, counter);
    assertEquals(2, vertices.size());
}
 
Example 8
Source File: TestTraversalFilterStepBarrier.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_hasXageX_asXaX_out_in_hasXageX_asXbX_selectXa_bX_whereXa_eqXbXX() {
    loadModern();
    final Traversal<Vertex, Map<String, Object>> traversal = this.sqlgGraph.traversal()
            .V().has("age").as("a")
            .out().in().has("age").as("b")
            .select("a", "b")
            .where("a", P.eq("b"));
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        final Map<String, Object> map = traversal.next();
        Assert.assertEquals(2, map.size());
        Assert.assertTrue(map.containsKey("a"));
        Assert.assertTrue(map.containsKey("b"));
        Assert.assertEquals(map.get("a"), map.get("b"));
    }
    Assert.assertEquals(6, counter);
    Assert.assertFalse(traversal.hasNext());
}
 
Example 9
Source File: GraphTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
public void g_V_hasLabelXpersonX_asXpX_VXsoftwareX_addInEXuses_pX() {
    final Traversal<Vertex, Edge> traversal = get_g_V_hasLabelXpersonX_asXpX_VXsoftwareX_addInEXuses_pX();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        final Edge edge = traversal.next();
        assertEquals("uses", edge.label());
        assertEquals("person", edge.outVertex().label());
        assertEquals("software", edge.inVertex().label());
        counter++;
    }
    assertEquals(8, counter);
}
 
Example 10
Source File: PageRankTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_pageRank_withXedges_outEXknowsXX_withXpropertyName_friendRankX_project_byXnameX_byXvaluesXfriendRankX_mathX() {
    final Traversal<Vertex, Map<String, Object>> traversal = get_g_V_pageRank_withXedges_outEXknowsXX_withXpropertyName_friendRankX_project_byXnameX_byXvaluesXfriendRankX_mathX();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        final Map<String, Object> map = traversal.next();
        assertEquals(2, map.size());
        final String name = (String) map.get("name");
        final Double friendRank = (Double) map.get("friendRank");
        if (name.equals("lop") || name.equals("ripple") || name.equals("peter") || name.equals("marko"))
            assertEquals(15.0, friendRank, 0.01);
        else
            assertEquals(21.0, friendRank, 0.01);

        counter++;
    }
    assertEquals(6, counter);
}
 
Example 11
Source File: VertexTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V() {
    final Traversal<Vertex, Vertex> traversal = get_g_V();
    printTraversalForm(traversal);
    int counter = 0;
    final Set<Vertex> vertices = new HashSet<>();
    while (traversal.hasNext()) {
        counter++;
        vertices.add(traversal.next());
    }
    assertEquals(6, vertices.size());
    assertEquals(6, counter);
}
 
Example 12
Source File: VertexTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void assert_g_v1_out(final Traversal<Vertex, Vertex> traversal) {
    printTraversalForm(traversal);
    int counter = 0;
    final Set<Vertex> vertices = new HashSet<>();
    while (traversal.hasNext()) {
        counter++;
        final Vertex vertex = traversal.next();
        vertices.add(vertex);
        assertTrue(vertex.value("name").equals("vadas") ||
                vertex.value("name").equals("josh") ||
                vertex.value("name").equals("lop"));
    }
    assertEquals(3, counter);
    assertEquals(3, vertices.size());
}
 
Example 13
Source File: FilterTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_E_filterXtrueX() {
    final Traversal<Edge, Edge> traversal = get_g_E_filterXtrueX();
    printTraversalForm(traversal);
    int counter = 0;
    final Set<Edge> edges = new HashSet<>();
    while (traversal.hasNext()) {
        counter++;
        edges.add(traversal.next());
    }
    assertEquals(6, counter);
    assertEquals(6, edges.size());
    assertFalse(traversal.hasNext());
}
 
Example 14
Source File: RangeTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Scenario: range step, Scope.local, 1 item requested, List input, String output
 */
@Test
@LoadGraphWith(MODERN)
public void g_V_asXaX_out_asXaX_out_asXaX_selectXmixed_aX_byXunfold_valuesXnameX_foldX_rangeXlocal_1_2X() {
    final Traversal<Vertex, String> traversal = get_g_V_asXaX_out_asXaX_out_asXaX_selectXmixed_aX_byXunfold_valuesXnameX_foldX_rangeXlocal_1_2X();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        final String s = traversal.next();
        assertEquals("josh", s);
        counter++;
    }
    assertEquals(2, counter);
}
 
Example 15
Source File: HasTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_E_hasXlabelXknowsX() {
    final Traversal<Edge, Edge> traversal = get_g_E_hasLabelXknowsX();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        assertEquals("knows", traversal.next().label());
    }
    assertEquals(2, counter);
}
 
Example 16
Source File: FlatMapTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/** TINKERPOP-782 */
@Test
@LoadGraphWith(MODERN)
public void g_V_asXaX_flatMapXselectXaXX() {
    final Traversal<Vertex, Vertex> traversal = get_g_V_asXaX_flatMapXselectXaXX();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        traversal.next();
    }
    assertEquals(6, counter);
}
 
Example 17
Source File: SelectTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_VX4X_out_asXhereX_hasXlang_javaX_selectXhereX() {
    final Traversal<Vertex, Vertex> traversal = get_g_VX4X_out_asXhereX_hasXlang_javaX_selectXhereX(convertToVertexId("josh"));
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        final Vertex vertex = traversal.next();
        assertEquals("java", vertex.<String>value("lang"));
        assertTrue(vertex.value("name").equals("ripple") || vertex.value("name").equals("lop"));
    }
    assertEquals(2, counter);
}
 
Example 18
Source File: TestLocalVertexStepLimit.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void g_V_localXoutE_limitX1X_inVX_limitX3X() {
    loadModern();
    final Traversal<Vertex, Vertex> traversal = this.sqlgGraph.traversal()
            .V().local(
                    __.outE().limit(1)
            ).inV().limit(3);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        traversal.next();
    }
    Assert.assertEquals(3, counter);
}
 
Example 19
Source File: CoinTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_coinX0X() {
    final Traversal<Vertex, Vertex> traversal = get_g_V_coinX0X();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        traversal.next();
    }
    assertEquals(0, counter);
    assertFalse(traversal.hasNext());
}
 
Example 20
Source File: RangeTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_VX1X_outXcreatedX_inXcreatedX_rangeX1_3X() {
    final Traversal<Vertex, Vertex> traversal = get_g_VX1X_outXcreatedX_inXcreatedX_rangeX1_3X(convertToVertexId("marko"));
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        final String name = traversal.next().value("name");
        assertTrue(name.equals("marko") || name.equals("josh") || name.equals("peter"));
    }
    assertEquals(2, counter);
}