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

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Traversal#next() . 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: TestUnoptimizedRepeatStep.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_VX6X_repeatXa_bothXcreatedX_simplePathX_emitXrepeatXb_bothXknowsXX_untilXloopsXbX_asXb_whereXloopsXaX_asXbX_hasXname_vadasXX_dedup_name() {
    loadModern();
    Object peterId = this.convertToVertexId("peter");
    Traversal<Vertex, String> traversal = this.sqlgGraph.traversal().V(peterId)
            .repeat("a", __.both("created").simplePath())
            .emit(
                    __.repeat("b", __.both("knows"))
                            .until(
                                    __.loops("b").as("c").where(__.loops("a").as("c"))
                            )
                            .has("name", "vadas")
            ).dedup().values("name");


    this.printTraversalForm(traversal);
    Assert.assertTrue(traversal.hasNext());
    String name = (String) traversal.next();
    Assert.assertEquals("josh", name);
    Assert.assertFalse(traversal.hasNext());
}
 
Example 2
Source File: MatchTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_matchXa__a_both_b__b_both_cX_dedupXa_bX() {
    final Traversal<Vertex, Map<String, Vertex>> traversal = get_g_V_matchXa__a_both_b__b_both_cX_dedupXa_bX();
    printTraversalForm(traversal);
    int counter = 0;
    final Set<List<Vertex>> results = new HashSet<>();
    while (traversal.hasNext()) {
        final Map<String, Vertex> map = traversal.next();
        assertEquals(3, map.size());
        assertTrue(results.add(Arrays.asList(map.get("a"), map.get("b"))));
        counter++;
    }
    assertEquals(12, counter);
    assertEquals(results.size(), counter);
}
 
Example 3
Source File: OrderTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_VX1X_elementMap_orderXlocalX_byXkeys_descXunfold() {
    final Traversal<Vertex, ?> traversal = get_g_VX1X_elementMap_orderXlocalX_byXkeys_descXunfold(convertToVertexId(graph, "marko"));
    printTraversalForm(traversal);

    final Object name = traversal.next();
    assertEquals("name", getKey(name));
    final Object label = traversal.next();
    assertEquals(T.label, getKey(label));
    final Object id = traversal.next();
    assertEquals(T.id, getKey(id));
    final Object age = traversal.next();
    assertEquals("age", getKey(age));

    assertThat(traversal.hasNext(), is(false));
}
 
Example 4
Source File: TestRepeatStepGraphBoth.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void g_V_asXaX_repeatXbothX_timesX3X_emit_asXbX_group_byXselectXaXX_byXselectXbX_dedup_order_byXidX_foldX_selectXvaluesX_unfold_dedup() {
    loadModern();
    final Traversal<Vertex, Collection<String>> traversal = this.sqlgGraph.traversal()
            .V().as("a")
            .repeat(__.both()).times(3).emit().values("name").as("b")
            .group()
            .by(__.select("a"))
            .by(__.select("b").dedup().order().fold())
            .select(Column.values).<Collection<String>>unfold().dedup();
    final List<String> vertices = new ArrayList<>(traversal.next());
    Assert.assertFalse(traversal.hasNext());
    Assert.assertEquals(6, vertices.size());
    Assert.assertEquals("josh", vertices.get(0));
    Assert.assertEquals("lop", vertices.get(1));
    Assert.assertEquals("marko", vertices.get(2));
    Assert.assertEquals("peter", vertices.get(3));
    Assert.assertEquals("ripple", vertices.get(4));
    Assert.assertEquals("vadas", vertices.get(5));
}
 
Example 5
Source File: GroupTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_repeatXout_groupXaX_byXnameX_byXcountX_timesX2X_capXaX() {
    final Traversal<Vertex, Map<String, Long>> traversal = get_g_V_repeatXout_groupXaX_byXnameX_byXcountX_timesX2X_capXaX();
    printTraversalForm(traversal);
    final Map<String, Long> map = traversal.next();
    assertFalse(traversal.hasNext());
    assertEquals(4, map.size());
    assertTrue(map.containsKey("vadas"));
    assertEquals(Long.valueOf(1), map.get("vadas"));
    assertTrue(map.containsKey("josh"));
    assertEquals(Long.valueOf(1), map.get("josh"));
    assertTrue(map.containsKey("lop"));
    assertEquals(Long.valueOf(4), map.get("lop"));
    assertTrue(map.containsKey("ripple"));
    assertEquals(Long.valueOf(2), map.get("ripple"));
    checkSideEffects(traversal.asAdmin().getSideEffects(), "a", HashMap.class);
}
 
Example 6
Source File: RepeatTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static void assertPath(final Traversal<Vertex, Path> traversal) {
    int path1 = 0;
    int path2 = 0;
    int path3 = 0;
    while (traversal.hasNext()) {
        final Path path = traversal.next();
        if (path.size() == 1) {
            path1++;
        } else if (path.size() == 2) {
            path2++;
        } else if (path.size() == 3) {
            path3++;
        } else {
            fail("Only path lengths of 1, 2, or 3 should be seen");
        }
    }
    assertEquals(6, path1);
    assertEquals(6, path2);
    assertEquals(2, path3);
}
 
Example 7
Source File: OrderTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_hasLabelXpersonX_group_byXnameX_byXoutE_weight_sumX_unfold_order_byXvalues_descX() {
    final Traversal<Vertex, Map.Entry<String, Number>> traversal = get_g_V_hasLabelXpersonX_group_byXnameX_byXoutE_weight_sumX_unfold_order_byXvalues_descX();
    printTraversalForm(traversal);
    assertTrue(traversal.hasNext());

    // the object should be a Map.Entry, but GLVs don't have that object so the assertion is made a bit more friendly.
    Object o = traversal.next();
    Map.Entry<String, Number> entry = o instanceof Map.Entry ? (Map.Entry) o : ((Map<String,Number>) o).entrySet().iterator().next();
    assertEquals("marko", entry.getKey());
    assertEquals(1.9, entry.getValue().doubleValue(), 0.0001);
    o = traversal.next();
    entry = o instanceof Map.Entry ? (Map.Entry) o : ((Map<String,Number>) o).entrySet().iterator().next();
    assertEquals("josh", entry.getKey());
    assertEquals(1.4, entry.getValue().doubleValue(), 0.0001);
    o = traversal.next();
    entry = o instanceof Map.Entry ? (Map.Entry) o : ((Map<String,Number>) o).entrySet().iterator().next();
    assertEquals("peter", entry.getKey());
    assertEquals(0.2, entry.getValue().doubleValue(), 0.0001);
    assertFalse(traversal.hasNext());
}
 
Example 8
Source File: ProfileTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_repeat_both_profile() {
    final Traversal<Vertex, TraversalMetrics> traversal = get_g_V_repeatXbothX_timesX3X_profile();
    printTraversalForm(traversal);

    final TraversalMetrics traversalMetrics = traversal.next();
    validate_g_V_repeat_both_modern_profile(traversalMetrics,
            traversal.asAdmin().getStrategies().getStrategy(RepeatUnrollStrategy.class).isPresent() &&
                    !traversal.asAdmin().getStrategies().getStrategy(ComputerVerificationStrategy.class).isPresent());
}
 
Example 9
Source File: MatchTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_valueMap_matchXa_selectXnameX_bX() {
    final Traversal<Vertex, Map<String, Object>> traversal = get_g_V_valueMap_matchXa_selectXnameX_bX();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        final Map<String, Object> map = traversal.next();
        assertTrue(Map.class.isAssignableFrom(map.get("a").getClass()));
        final String name = ((Map<String, List<String>>) map.get("a")).get("name").get(0);
        assertEquals(name, ((List<String>) map.get("b")).get(0));
    }
    assertEquals(6, counter);
}
 
Example 10
Source File: CoinTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_coinX1X() {
    final Traversal<Vertex, Vertex> traversal = get_g_V_coinX1X();
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        traversal.next();
    }
    assertEquals(6, counter);
    assertFalse(traversal.hasNext());
}
 
Example 11
Source File: SelectTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static void assertCommonB(final Traversal<Vertex, Edge> traversal) {
    assertTrue(traversal.hasNext());
    assertTrue(traversal.hasNext());
    final Edge edge = traversal.next();
    assertEquals("knows", edge.label());
    assertEquals(1.0d, edge.<Double>value("weight"), 0.00001d);
    assertFalse(traversal.hasNext());
    assertFalse(traversal.hasNext());
}
 
Example 12
Source File: AddVertexTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
public void g_addVXpersonX_propertyXname_stephenX() {
    final Traversal<Vertex, Vertex> traversal = get_g_addVXpersonX_propertyXname_stephenX();
    printTraversalForm(traversal);
    final Vertex stephen = traversal.next();
    assertFalse(traversal.hasNext());
    assertEquals("person", stephen.label());
    assertEquals("stephen", stephen.value("name"));
    assertEquals(1, IteratorUtils.count(stephen.properties()));
    assertEquals(7, IteratorUtils.count(g.V()));
}
 
Example 13
Source File: ParameterizedGroovyTranslatorTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldIncludeCustomTypeTranslationForSomethingSilly() throws Exception {
    final TinkerGraph graph = TinkerGraph.open();
    final ParameterizedSillyClass notSillyEnough = ParameterizedSillyClass.from("not silly enough", 100);
    final GraphTraversalSource g = graph.traversal();

    // without type translation we get uglinesss
    final Script parameterizedScriptBad = GroovyTranslator.of("g", true).
            translate(g.inject(notSillyEnough).asAdmin().getBytecode());
    Bindings bindings = new SimpleBindings();
    parameterizedScriptBad.getParameters().ifPresent(bindings::putAll);
    assertEquals(String.format("g.inject(%s)", "_args_0"), parameterizedScriptBad.getScript());
    assertEquals(1, bindings.size());
    assertEquals(notSillyEnough, bindings.get("_args_0"));
    bindings.clear();

    // with type translation we get valid gremlin
    final Script parameterizedScriptGood = GroovyTranslator.of("g", new ParameterizedSillyClassTranslatorCustomizer().createTypeTranslator()).
            translate(g.inject(notSillyEnough).asAdmin().getBytecode());
    parameterizedScriptGood.getParameters().ifPresent(bindings::putAll);
    assertEquals(2, bindings.size());
    assertEquals(notSillyEnough.getX(), bindings.get("_args_0"));
    assertEquals(notSillyEnough.getY(), bindings.get("_args_1"));
    assertEquals("g.inject(org.apache.tinkerpop.gremlin.groovy.jsr223.ParameterizedGroovyTranslatorTest.ParameterizedSillyClass.from(_args_0,_args_1))", parameterizedScriptGood.getScript());
    bindings.put("g", g);
    assertThatScriptOk(parameterizedScriptGood.getScript(), bindings);

    final GremlinGroovyScriptEngine customEngine = new GremlinGroovyScriptEngine(new ParameterizedSillyClassTranslatorCustomizer());
    final Traversal t = customEngine.eval(g.inject(notSillyEnough).asAdmin().getBytecode(), bindings, "g");
    final ParameterizedSillyClass sc = (ParameterizedSillyClass) t.next();
    assertEquals(notSillyEnough.getX(), sc.getX());
    assertEquals(notSillyEnough.getY(), sc.getY());
    assertThat(t.hasNext(), is(false));
}
 
Example 14
Source File: GroupCountTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_repeatXout_groupCountXaX_byXnameXX_timesX2X_capXaX() {
    final Traversal<Vertex, Map<String, Long>> traversal = get_g_V_repeatXout_groupCountXaX_byXnameXX_timesX2X_capXaX();
    printTraversalForm(traversal);
    final Map<String, Long> map = traversal.next();
    assertFalse(traversal.hasNext());
    assertEquals(4, map.size());
    assertEquals(4l, map.get("lop").longValue());
    assertEquals(2l, map.get("ripple").longValue());
    assertEquals(1l, map.get("josh").longValue());
    assertEquals(1l, map.get("vadas").longValue());
    checkSideEffects(traversal.asAdmin().getSideEffects(), "a", HashMap.class);
}
 
Example 15
Source File: SampleTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_E_sampleX1X() {
    final Traversal<Edge, Edge> traversal = get_g_E_sampleX1X();
    assertTrue(traversal.hasNext());
    traversal.next();
    assertFalse(traversal.hasNext());
}
 
Example 16
Source File: GroupCountTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private static void assertCommonA(final Traversal<Vertex, Map<String, Long>> traversal) {
    final Map<String, Long> map = traversal.next();
    assertEquals(map.size(), 2);
    assertEquals(Long.valueOf(3l), map.get("lop"));
    assertEquals(Long.valueOf(1l), map.get("ripple"));
    assertFalse(traversal.hasNext());
}
 
Example 17
Source File: RepeatTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_repeatXbothX_untilXname_eq_marko_or_loops_gt_1X_groupCount_byXnameX() {
    final Traversal<Vertex, Map<String, Long>> traversal = get_g_V_repeatXbothX_untilXname_eq_marko_or_loops_gt_1X_groupCount_byXnameX();
    printTraversalForm(traversal);
    final Map<String, Long> map = traversal.next();
    assertFalse(traversal.hasNext());
    assertEquals(5, map.size());
    assertEquals(3L, map.get("ripple").longValue());
    assertEquals(3L, map.get("vadas").longValue());
    assertEquals(4L, map.get("josh").longValue());
    assertEquals(10L, map.get("lop").longValue());
    assertEquals(4L, map.get("marko").longValue());
}
 
Example 18
Source File: SelectTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_outXcreatedX_unionXasXprojectX_inXcreatedX_hasXname_markoX_selectXprojectX__asXprojectX_inXcreatedX_inXknowsX_hasXname_markoX_selectXprojectXX_groupCount_byXnameX() {
    Traversal<Vertex, Map<String, Long>> traversal = get_g_V_outXcreatedX_unionXasXprojectX_inXcreatedX_hasXname_markoX_selectXprojectX__asXprojectX_inXcreatedX_inXknowsX_hasXname_markoX_selectXprojectXX_groupCount_byXnameX();
    printTraversalForm(traversal);
    assertTrue(traversal.hasNext());
    final Map<String, Long> map = traversal.next();
    assertFalse(traversal.hasNext());
    assertEquals(2, map.size());
    assertEquals(1l, map.get("ripple").longValue());
    assertEquals(6l, map.get("lop").longValue());
}
 
Example 19
Source File: AggregateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_aggregateXxX_byXnameX_capXxX() {
    final Traversal<Vertex, List<String>> traversal = get_g_V_aggregateXxX_byXnameX_capXxX();
    printTraversalForm(traversal);
    final Collection<String> names = traversal.next();
    assertFalse(traversal.hasNext());
    checkListOfNames(names);
}
 
Example 20
Source File: TestIoAgain.java    From sqlg with MIT License 4 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeTree() throws Exception {
    loadModern();
    final org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper mapper = this.sqlgGraph.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().version(GraphSONVersion.V1_0).create().createMapper();
    Traversal<Vertex, Tree> traversal = this.sqlgGraph.traversal().V(convertToVertexId("marko")).out().properties("name").tree();
    printTraversalForm(traversal);
    Tree t = traversal.next();
    final String json = mapper.writeValueAsString(t);

    final HashMap<String, Object> m = (HashMap<String, Object>) mapper.readValue(json, mapTypeReference);

    // Check Structure
    assertEquals(1, m.size());
    assertTrue(m.containsKey(convertToVertexId("marko").toString()));

    // Check Structure n+1
    final HashMap<String, Object> branch = (HashMap<String, Object>) m.get(convertToVertexId("marko").toString());
    assertEquals(2, branch.size());
    assertTrue(branch.containsKey(GraphSONTokens.KEY));
    assertTrue(branch.containsKey(GraphSONTokens.VALUE));

    //Check n+1 key (traversed element)
    final HashMap<String, Object> branchKey = (HashMap<String, Object>) branch.get(GraphSONTokens.KEY);
    assertTrue(branchKey.containsKey(GraphSONTokens.ID));
    assertTrue(branchKey.containsKey(GraphSONTokens.LABEL));
    assertTrue(branchKey.containsKey(GraphSONTokens.TYPE));
    assertTrue(branchKey.containsKey(GraphSONTokens.PROPERTIES));
    assertEquals(convertToVertexId("marko").toString(), branchKey.get(GraphSONTokens.ID).toString());
    assertEquals("person", branchKey.get(GraphSONTokens.LABEL));
    assertEquals("vertex", branchKey.get(GraphSONTokens.TYPE));
    final HashMap<String, List<HashMap<String, Object>>> branchKeyProps = (HashMap<String, List<HashMap<String, Object>>>) branchKey.get(GraphSONTokens.PROPERTIES);
    assertEquals("marko", branchKeyProps.get("name").get(0).get("value"));
    assertEquals(29, branchKeyProps.get("age").get(0).get("value"));

    //Check n+1 value (traversed element)
    final HashMap<String, Object> branchValue = (HashMap<String, Object>) branch.get(GraphSONTokens.VALUE);
    assertEquals(3, branchValue.size());
    assertTrue(branchValue.containsKey(convertToVertexId("vadas").toString()));
    assertTrue(branchValue.containsKey(convertToVertexId("lop").toString()));
    assertTrue(branchValue.containsKey(convertToVertexId("josh").toString()));

    // Check that vp[] functioned properly
    final HashMap<String, HashMap<String, Object>> branch2 = (HashMap<String, HashMap<String, Object>>) branchValue.get(convertToVertexId("vadas").toString());
    assertTrue(branch2.containsKey(GraphSONTokens.KEY));
    assertTrue(branch2.containsKey(GraphSONTokens.VALUE));

    final Map.Entry entry = branch2.get(GraphSONTokens.VALUE).entrySet().iterator().next();
    final HashMap<String, HashMap<String, Object>> branch2Prop = (HashMap<String, HashMap<String, Object>>) entry.getValue();
    assertTrue(branch2Prop.get(GraphSONTokens.KEY).containsKey(GraphSONTokens.ID));
    assertTrue(branch2Prop.get(GraphSONTokens.KEY).containsKey(GraphSONTokens.VALUE));
    assertTrue(branch2Prop.get(GraphSONTokens.KEY).containsKey(GraphSONTokens.LABEL));
    assertEquals("name", branch2Prop.get(GraphSONTokens.KEY).get(GraphSONTokens.LABEL));
    assertEquals("vadas", branch2Prop.get(GraphSONTokens.KEY).get(GraphSONTokens.VALUE));
    assertEquals(entry.getKey().toString(), branch2Prop.get(GraphSONTokens.KEY).get(GraphSONTokens.ID).toString());
}