Java Code Examples for org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#list()

The following examples show how to use org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#list() . 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: GraphSerializer.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeValue(final Graph value, final Buffer buffer, final GraphBinaryWriter context) throws IOException {
    // this kinda looks scary memory-wise, but GraphBinary is about network derser so we are dealing with a
    // graph instance that should live in memory already - not expecting "big" stuff here.
    final List<Vertex> vertexList = IteratorUtils.list(value.vertices());
    final List<Edge> edgeList = IteratorUtils.list(value.edges());

    context.writeValue(vertexList.size(), buffer, false);

    for (Vertex v : vertexList) {
        writeVertex(buffer, context, v);
    }

    context.writeValue(edgeList.size(), buffer, false);

    for (Edge e : edgeList) {
        writeEdge(buffer, context, e);
    }
}
 
Example 2
Source File: AddEdgeTest.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_aggregateXxX_asXaX_selectXxX_unfold_addEXexistsWithX_toXaX_propertyXtime_nowX() {
    final Traversal<Vertex, Edge> traversal = get_g_V_aggregateXxX_asXaX_selectXxX_unfold_addEXexistsWithX_toXaX_propertyXtime_nowX();
    printTraversalForm(traversal);
    int count = 0;
    while (traversal.hasNext()) {
        final Edge edge = traversal.next();
        assertEquals("existsWith", edge.label());
        assertEquals("now", g.E(edge).values("time").next());
        assertEquals(1, g.E(edge).properties().count().next().intValue());
        count++;
    }
    assertEquals(36, count);
    assertEquals(42, IteratorUtils.count(g.E()));
    for (final Vertex vertex : IteratorUtils.list(g.V())) {
        assertEquals(6, g.V(vertex).out("existsWith").count().next().intValue());
        assertEquals(6, g.V(vertex).in("existsWith").count().next().intValue());
    }
    assertEquals(6, IteratorUtils.count(g.V()));
}
 
Example 3
Source File: GraphSerializer.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void writeVertex(Buffer buffer, GraphBinaryWriter context, Vertex vertex) throws IOException {
    final List<VertexProperty<Object>> vertexProperties = IteratorUtils.list(vertex.properties());

    context.write(vertex.id(), buffer);
    context.writeValue(vertex.label(), buffer, false);
    context.writeValue(vertexProperties.size(), buffer, false);

    for (VertexProperty<Object> vp : vertexProperties) {
        context.write(vp.id(), buffer);
        context.writeValue(vp.label(), buffer, false);
        context.write(vp.value(), buffer);

        // maintain the VertexProperty format we have with this empty parent.........
        context.write(null, buffer);

        // write those properties out using the standard Property serializer
        context.writeValue(IteratorUtils.list(vp.properties()), buffer, false);
    }
}
 
Example 4
Source File: AbstractCompatibilityTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
protected void assertVertex(final Vertex expected, final Vertex actual) {
    assertEquals(expected.id(), actual.id());
    assertEquals(expected.label(), actual.label());

    if (!(getCompatibility() instanceof GraphBinaryCompatibility)) {
        assertEquals(IteratorUtils.count(expected.properties()), IteratorUtils.count(actual.properties()));
        for (String k : expected.keys()) {
            final Iterator<VertexProperty<Object>> expectedVps = expected.properties(k);
            final List<VertexProperty<Object>> actualVps = IteratorUtils.list(actual.properties(k));
            while (expectedVps.hasNext()) {
                final VertexProperty expectedVp = expectedVps.next();
                final VertexProperty<Object> found = actualVps.stream()
                        .filter(vp -> vp.id().equals(expectedVp.id()))
                        .findFirst()
                        .orElseThrow(() -> new RuntimeException("Could not find VertexProperty for " + expectedVp.id()));
                assertVertexProperty(expectedVp, found);
            }
        }
    }
}
 
Example 5
Source File: GraphSONMessageSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeEdgeProperty() throws Exception {
    final Graph g = TinkerGraph.open();
    final Vertex v1 = g.addVertex();
    final Vertex v2 = g.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Property<Object>> iterable = IteratorUtils.list(e.properties("abc"));
    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());

    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);
    assertEquals(1, converted.size());

    final JsonNode propertyAsJson = converted.get(0);
    assertNotNull(propertyAsJson);

    assertEquals(123, propertyAsJson.get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt());
}
 
Example 6
Source File: CommunityGenerator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private Builder(final Graph g) {
    super(Builder.class);
    this.g = g;
    final List<Vertex> allVertices = IteratorUtils.list(g.vertices());
    this.vertices = allVertices;
    this.expectedNumEdges = allVertices.size() * 2;
}
 
Example 7
Source File: CustomTest.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldTraverseInOutFromVertexWithMultipleEdgeLabelFilter() {
    final Vertex a = graph.addVertex();
    final Vertex b = graph.addVertex();
    final Vertex c = graph.addVertex();

    final String labelFriend = graphProvider.convertLabel("friend");
    final String labelHate = graphProvider.convertLabel("hate");

    final Edge aFriendB = a.addEdge(labelFriend, b);
    final Edge aFriendC = a.addEdge(labelFriend, c);
    final Edge aHateC = a.addEdge(labelHate, c);
    final Edge cHateA = c.addEdge(labelHate, a);
    final Edge cHateB = c.addEdge(labelHate, b);

    List<Edge> results = IteratorUtils.list(a.edges(Direction.OUT, labelFriend, labelHate));
    assertEquals(3, results.size());
    assertTrue(results.contains(aFriendB));
    assertTrue(results.contains(aFriendC));
    assertTrue(results.contains(aHateC));

    results = IteratorUtils.list(a.edges(Direction.IN, labelFriend, labelHate));
    assertEquals(1, results.size());
    assertTrue(results.contains(cHateA));

    results = IteratorUtils.list(b.edges(Direction.IN, labelFriend, labelHate));
    assertEquals(2, results.size());
    assertTrue(results.contains(aFriendB));
    assertTrue(results.contains(cHateB));

    results = IteratorUtils.list(b.edges(Direction.IN, graphProvider.convertLabel("blah1"), graphProvider.convertLabel("blah2")));
    assertEquals(0, results.size());
}
 
Example 8
Source File: GraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
public void shouldTraverseInOutFromVertexWithMultipleEdgeLabelFilter() {
    final Vertex a = graph.addVertex();
    final Vertex b = graph.addVertex();
    final Vertex c = graph.addVertex();

    final String labelFriend = graphProvider.convertLabel("friend");
    final String labelHate = graphProvider.convertLabel("hate");

    final Edge aFriendB = a.addEdge(labelFriend, b);
    final Edge aFriendC = a.addEdge(labelFriend, c);
    final Edge aHateC = a.addEdge(labelHate, c);
    final Edge cHateA = c.addEdge(labelHate, a);
    final Edge cHateB = c.addEdge(labelHate, b);

    List<Edge> results = IteratorUtils.list(a.edges(Direction.OUT, labelFriend, labelHate));
    assertEquals(3, results.size());
    assertTrue(results.contains(aFriendB));
    assertTrue(results.contains(aFriendC));
    assertTrue(results.contains(aHateC));

    results = IteratorUtils.list(a.edges(Direction.IN, labelFriend, labelHate));
    assertEquals(1, results.size());
    assertTrue(results.contains(cHateA));

    results = IteratorUtils.list(b.edges(Direction.IN, labelFriend, labelHate));
    assertEquals(2, results.size());
    assertTrue(results.contains(aFriendB));
    assertTrue(results.contains(cHateB));

    results = IteratorUtils.list(b.edges(Direction.IN, graphProvider.convertLabel("blah1"), graphProvider.convertLabel("blah2")));
    assertEquals(0, results.size());
}
 
Example 9
Source File: VertexTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_PROPERTY)
public void shouldGetPropertyKeysOnVertex() {
    final Vertex v = graph.addVertex("name", "marko", "location", "desert", "status", "dope");
    Set<String> keys = v.keys();
    assertEquals(3, keys.size());

    assertTrue(keys.contains("name"));
    assertTrue(keys.contains("location"));
    assertTrue(keys.contains("status"));

    final List<VertexProperty<Object>> m = IteratorUtils.list(v.properties());
    assertEquals(3, m.size());
    assertTrue(m.stream().anyMatch(p -> p.key().equals("name")));
    assertTrue(m.stream().anyMatch(p -> p.key().equals("location")));
    assertTrue(m.stream().anyMatch(p -> p.key().equals("status")));
    assertEquals("marko", m.stream().filter(p -> p.key().equals("name")).map(Property::value).findAny().orElse(null));
    assertEquals("desert", m.stream().filter(p -> p.key().equals("location")).map(Property::value).findAny().orElse(null));
    assertEquals("dope", m.stream().filter(p -> p.key().equals("status")).map(Property::value).findAny().orElse(null));

    v.property("status").remove();

    keys = v.keys();
    assertEquals(2, keys.size());
    assertTrue(keys.contains("name"));
    assertTrue(keys.contains("location"));

    v.properties().forEachRemaining(Property::remove);

    keys = v.keys();
    assertEquals(0, keys.size());
}
 
Example 10
Source File: DistributionGenerator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private Builder(final Graph g) {
    super(Builder.class);
    this.g = g;
    final List<Vertex> allVertices = IteratorUtils.list(g.vertices());
    this.out = allVertices;
    this.in = allVertices;
    this.expectedNumEdges = allVertices.size() * 2;
}
 
Example 11
Source File: GraphSONMessageSerializerV1d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeEdge() throws Exception {
    final Graph g = TinkerGraph.open();
    final Vertex v1 = g.addVertex();
    final Vertex v2 = g.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Edge> iterable = IteratorUtils.list(g.edges());
    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());

    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);
    assertEquals(1, converted.size());

    final JsonNode edgeAsJson = converted.get(0);
    assertNotNull(edgeAsJson);

    assertEquals(((Long) e.id()).intValue(), edgeAsJson.get(GraphSONTokens.ID).asLong());  // lossy
    assertEquals(((Long) v1.id()).intValue(), edgeAsJson.get(GraphSONTokens.OUT).asLong());// lossy
    assertEquals(((Long) v2.id()).intValue(), edgeAsJson.get(GraphSONTokens.IN).asLong()); // lossy
    assertEquals(e.label(), edgeAsJson.get(GraphSONTokens.LABEL).asText());
    assertEquals(GraphSONTokens.EDGE, edgeAsJson.get(GraphSONTokens.TYPE).asText());

    final JsonNode properties = edgeAsJson.get(GraphSONTokens.PROPERTIES);
    assertNotNull(properties);
    assertEquals(123, properties.get("abc").asInt());
}
 
Example 12
Source File: GraphSONMessageSerializerGremlinV1d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeEdge() throws Exception {
    final Graph graph = TinkerGraph.open();
    final Vertex v1 = graph.addVertex();
    final Vertex v2 = graph.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Edge> iterable = IteratorUtils.list(graph.edges());

    final ResponseMessage response = convert(iterable);
    assertCommon(response);

    final List<Map<String, Object>> edgeList = (List<Map<String, Object>>) response.getResult().getData();
    assertEquals(1, edgeList.size());

    final Map<String, Object> deserializedEdge = edgeList.get(0);
    assertEquals(e.id(), deserializedEdge.get(GraphSONTokens.ID));
    assertEquals(v1.id(), deserializedEdge.get(GraphSONTokens.OUT));
    assertEquals(v2.id(), deserializedEdge.get(GraphSONTokens.IN));
    assertEquals(v1.label(), deserializedEdge.get(GraphSONTokens.OUT_LABEL));
    assertEquals(v2.label(), deserializedEdge.get(GraphSONTokens.IN_LABEL));
    assertEquals(e.label(), deserializedEdge.get(GraphSONTokens.LABEL));
    assertEquals(GraphSONTokens.EDGE, deserializedEdge.get(GraphSONTokens.TYPE));

    final Map<String, Object> properties = (Map<String, Object>) deserializedEdge.get(GraphSONTokens.PROPERTIES);
    assertNotNull(properties);
    assertEquals(123, properties.get("abc"));

}
 
Example 13
Source File: GraphSONMessageSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeEdge() throws Exception {
    final Graph g = TinkerGraph.open();
    final Vertex v1 = g.addVertex();
    final Vertex v2 = g.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Edge> iterable = IteratorUtils.list(g.edges());
    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());

    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);
    assertEquals(1, converted.size());

    final JsonNode edgeAsJson = converted.get(0).get(GraphSONTokens.VALUEPROP);
    assertNotNull(edgeAsJson);

    assertEquals(((Long) e.id()).longValue(), edgeAsJson.get(GraphSONTokens.ID).get(GraphSONTokens.VALUEPROP).asLong());
    assertEquals(((Long) v1.id()).longValue(), edgeAsJson.get(GraphSONTokens.OUT).get(GraphSONTokens.VALUEPROP).asLong());
    assertEquals(((Long) v2.id()).longValue(), edgeAsJson.get(GraphSONTokens.IN).get(GraphSONTokens.VALUEPROP).asLong());
    assertEquals(e.label(), edgeAsJson.get(GraphSONTokens.LABEL).asText());

    final JsonNode properties = edgeAsJson.get(GraphSONTokens.PROPERTIES);
    assertNotNull(properties);
    assertEquals(123, properties.get("abc").get(GraphSONTokens.VALUEPROP).get("value").get(GraphSONTokens.VALUEPROP).asInt());
}
 
Example 14
Source File: GraphSONMessageSerializerGremlinV2d0Test.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeEdge() throws Exception {
    final Graph graph = TinkerGraph.open();
    final Vertex v1 = graph.addVertex();
    final Vertex v2 = graph.addVertex();
    final Edge e = v1.addEdge("test", v2);
    e.property("abc", 123);

    final Iterable<Edge> iterable = IteratorUtils.list(graph.edges());

    final ResponseMessage response = convert(iterable);
    assertCommon(response);

    final List<Edge> edgeList = (List<Edge>) response.getResult().getData();
    assertEquals(1, edgeList.size());

    final Edge deserializedEdge = edgeList.get(0);
    assertEquals(e.id(), deserializedEdge.id());
    assertEquals(v1.id(), deserializedEdge.outVertex().id());
    assertEquals(v2.id(), deserializedEdge.inVertex().id());
    assertEquals(v1.label(), deserializedEdge.outVertex().label());
    assertEquals(v2.label(), deserializedEdge.inVertex().label());
    assertEquals(e.label(), deserializedEdge.label());

    final List<Property> properties = new ArrayList<>();
    deserializedEdge.properties().forEachRemaining(properties::add);
    assertEquals(1, properties.size());

    assertNotNull(properties);
    assertEquals("abc", properties.get(0).key());
    assertEquals(123, properties.get(0).value());

}
 
Example 15
Source File: GraphSONMessageSerializerGremlinV2d0Test.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSerializeVertexWithEmbeddedMap() throws Exception {
    final Graph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    final Map<String, Object> map = new HashMap<>();
    map.put("x", 500);
    map.put("y", "some");

    final ArrayList<Object> friends = new ArrayList<>();
    friends.add("x");
    friends.add(5);
    friends.add(map);

    v.property(VertexProperty.Cardinality.single, "friends", friends);

    final List list = IteratorUtils.list(graph.vertices());

    final ResponseMessage response = convert(list);
    assertCommon(response);

    final List<Vertex> vertexList = (List<Vertex>) response.getResult().getData();
    assertEquals(1, vertexList.size());

    final Vertex deserializedVertex = vertexList.get(0);
    assertEquals(v.id(), deserializedVertex.id());
    assertEquals(Vertex.DEFAULT_LABEL, deserializedVertex.label());

    final List<VertexProperty> properties = new ArrayList<>();
    deserializedVertex.properties().forEachRemaining(properties::add);
    assertEquals(1, properties.size());

    final VertexProperty friendsProperty = properties.get(0);
    final List<Object> deserializedInnerList = (List<Object>) friendsProperty.value();

    assertEquals(3, deserializedInnerList.size());
    assertEquals("x", deserializedInnerList.get(0));
    assertEquals(5, deserializedInnerList.get(1));

    final Map<String, Object> deserializedInnerInnerMap = (Map<String, Object>) deserializedInnerList.get(2);
    assertEquals(2, deserializedInnerInnerMap.size());
    assertEquals(500, deserializedInnerInnerMap.get("x"));
    assertEquals("some", deserializedInnerInnerMap.get("y"));
}
 
Example 16
Source File: GraphComputerTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldSupportJobChaining() throws Exception {
    final ComputerResult result1 = graphProvider.getGraphComputer(graph)
            .program(PageRankVertexProgram.build().iterations(5).create(graph)).persist(GraphComputer.Persist.EDGES).result(GraphComputer.ResultGraph.NEW).submit().get();
    final Graph graph1 = result1.graph();
    final Memory memory1 = result1.memory();
    assertEquals(5, memory1.getIteration());
    assertEquals(6, graph1.traversal().V().count().next().intValue());
    assertEquals(6, graph1.traversal().E().count().next().intValue());
    assertEquals(6, graph1.traversal().V().values(PageRankVertexProgram.PAGE_RANK).count().next().intValue());
    assertEquals(18, graph1.traversal().V().values().count().next().intValue());
    //
    final ComputerResult result2 = graph1.compute(graphProvider.getGraphComputer(graph1).getClass())
            .program(PeerPressureVertexProgram.build().maxIterations(4).create(graph1)).persist(GraphComputer.Persist.EDGES).result(GraphComputer.ResultGraph.NEW).submit().get();
    final Graph graph2 = result2.graph();
    final Memory memory2 = result2.memory();
    assertTrue(memory2.getIteration() <= 4);
    assertEquals(6, graph2.traversal().V().count().next().intValue());
    assertEquals(6, graph2.traversal().E().count().next().intValue());
    assertEquals(6, graph2.traversal().V().values(PeerPressureVertexProgram.CLUSTER).count().next().intValue());
    assertEquals(6, graph2.traversal().V().values(PageRankVertexProgram.PAGE_RANK).count().next().intValue());
    assertEquals(24, graph2.traversal().V().values().count().next().intValue());
    //
    final ComputerResult result3 = graph2.compute(graphProvider.getGraphComputer(graph2).getClass())
            .program(TraversalVertexProgram.build().traversal(g.V().groupCount("m").by(__.values(PageRankVertexProgram.PAGE_RANK).count()).label().asAdmin()).create(graph2)).persist(GraphComputer.Persist.EDGES).result(GraphComputer.ResultGraph.NEW).submit().get();
    final Graph graph3 = result3.graph();
    final Memory memory3 = result3.memory();
    assertTrue(memory3.keys().contains("m"));
    assertTrue(memory3.keys().contains(TraversalVertexProgram.HALTED_TRAVERSERS));
    assertEquals(1, memory3.<Map<Long, Long>>get("m").size());
    assertEquals(6, memory3.<Map<Long, Long>>get("m").get(1l).intValue());
    List<Traverser<String>> traversers = IteratorUtils.list(memory3.<TraverserSet>get(TraversalVertexProgram.HALTED_TRAVERSERS).iterator());
    assertEquals(6l, traversers.stream().map(Traverser::bulk).reduce((a, b) -> a + b).get().longValue());
    assertEquals(4l, traversers.stream().filter(s -> s.get().equals("person")).map(Traverser::bulk).reduce((a, b) -> a + b).get().longValue());
    assertEquals(2l, traversers.stream().filter(s -> s.get().equals("software")).map(Traverser::bulk).reduce((a, b) -> a + b).get().longValue());
    assertEquals(6, graph3.traversal().V().count().next().intValue());
    assertEquals(6, graph3.traversal().E().count().next().intValue());
    assertEquals(0, graph3.traversal().V().values(TraversalVertexProgram.HALTED_TRAVERSERS).count().next().intValue());
    assertEquals(6, graph3.traversal().V().values(PeerPressureVertexProgram.CLUSTER).count().next().intValue());
    assertEquals(6, graph3.traversal().V().values(PageRankVertexProgram.PAGE_RANK).count().next().intValue());
    assertEquals(24, graph3.traversal().V().values().count().next().intValue()); // no halted traversers

    // TODO: add a test the shows DAG behavior -- splitting another TraversalVertexProgram off of the PeerPressureVertexProgram job.
}
 
Example 17
Source File: TestRepeatStepOnEdges.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testBug116() {
    Vertex v0 = this.sqlgGraph.addVertex("code", "0");
    Vertex v1 = this.sqlgGraph.addVertex("code", "1");
    Vertex v2 = this.sqlgGraph.addVertex("code", "2");
    Vertex v3 = this.sqlgGraph.addVertex("code", "3");
    v0.addEdge("tsw", v1, "speed", "1", "arrTime", 10L, "depTime", 5L);
    v1.addEdge("tsw", v2, "speed", "1", "arrTime", 15L, "depTime", 9L); //must be ignored in longest path
    v1.addEdge("tsw", v2, "speed", "1", "arrTime", 20L, "depTime", 17L); //must be used in longest path
    v2.addEdge("tsw", v3, "speed", "1", "arrTime", 30L, "depTime", 25L);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal gp = (DefaultGraphTraversal) this.sqlgGraph.traversal().V().outE("tsw").as("e").inV().emit().repeat(
            __.outE("tsw").as("e").inV().simplePath()
    ).times(20);
    Assert.assertEquals(4, gp.getSteps().size());

    //noinspection unchecked
    Assert.assertEquals(10, IteratorUtils.list(gp).size());
    Assert.assertEquals(2, gp.getSteps().size());

    gp = query1(this.sqlgGraph.traversal());
    checkResult(gp);

    //check paths
    gp = query1(this.sqlgGraph.traversal());
    //check paths
    @SuppressWarnings("unchecked") List<List<Object>> paths = IteratorUtils.list(gp);
    List<Predicate<List<Object>>> pathsToAssert = Arrays.asList(
            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v0.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(5L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(10L)
                    && ((Path) p.get(1)).get(2).equals(v1.id()),

            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v0.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(5L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(10L)
                    && ((Path) p.get(1)).get(2).equals(v1.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(3)).get("depTime").equals(17L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(3)).get("arrTime").equals(20L)
                    && ((Path) p.get(1)).get(4).equals(v2.id()),

            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v0.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(5L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(10L)
                    && ((Path) p.get(1)).get(2).equals(v1.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(3)).get("depTime").equals(17L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(3)).get("arrTime").equals(20L)
                    && ((Path) p.get(1)).get(4).equals(v2.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(5)).get("depTime").equals(25L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(5)).get("arrTime").equals(30L)
                    && ((Path) p.get(1)).get(6).equals(v3.id()),

            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v1.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(9L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(15L)
                    && ((Path) p.get(1)).get(2).equals(v2.id()),

            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v1.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(9L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(15L)
                    && ((Path) p.get(1)).get(2).equals(v2.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(3)).get("depTime").equals(25L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(3)).get("arrTime").equals(30L)
                    && ((Path) p.get(1)).get(4).equals(v3.id()),

            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v1.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(17L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(20L)
                    && ((Path) p.get(1)).get(2).equals(v2.id()),

            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v1.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(17L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(20L)
                    && ((Path) p.get(1)).get(2).equals(v2.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(3)).get("depTime").equals(25L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(3)).get("arrTime").equals(30L)
                    && ((Path) p.get(1)).get(4).equals(v3.id()),

            p -> p.size() == 2 && p.get(0).equals("1") && ((Path) p.get(1)).get(0).equals(v2.id())
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("depTime") && ((Map) ((Path) p.get(1)).get(1)).get("depTime").equals(25L)
                    && ((Map) ((Path) p.get(1)).get(1)).containsKey("arrTime") && ((Map) ((Path) p.get(1)).get(1)).get("arrTime").equals(30L)
                    && ((Path) p.get(1)).get(2).equals(v3.id())
    );
    for (Predicate<List<Object>> pathPredicate : pathsToAssert) {
        Optional<List<Object>> objectList = paths.stream().filter(pathPredicate).findAny();
        Assert.assertTrue(objectList.isPresent());
        Assert.assertTrue(paths.remove(objectList.get()));
    }
    Assert.assertTrue(paths.isEmpty());

    gp = query2(this.sqlgGraph.traversal());
    checkResult(gp);
}
 
Example 18
Source File: GraphSONMessageSerializerV2d0Test.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSerializeToJsonIteratorWithEmbeddedMap() throws Exception {
    final Graph g = TinkerGraph.open();
    final Vertex v = g.addVertex();
    final Map<String, Object> map = new HashMap<>();
    map.put("x", 500);
    map.put("y", "some");

    final ArrayList<Object> friends = new ArrayList<>();
    friends.add("x");
    friends.add(5);
    friends.add(map);

    v.property(VertexProperty.Cardinality.single, "friends", friends);

    final Iterable iterable = IteratorUtils.list(g.vertices());
    final String results = SERIALIZER.serializeResponseAsString(ResponseMessage.build(msg).result(iterable).create());
    final JsonNode json = mapper.readTree(results);

    assertNotNull(json);
    assertEquals(msg.getRequestId().toString(), json.get(SerTokens.TOKEN_REQUEST).asText());
    final JsonNode converted = json.get(SerTokens.TOKEN_RESULT).get(SerTokens.TOKEN_DATA);

    assertNotNull(converted);
    assertEquals(1, converted.size());

    final JsonNode vertexAsJson = converted.get(0).get(GraphSONTokens.VALUEPROP);
    assertNotNull(vertexAsJson);

    final JsonNode properties = vertexAsJson.get(GraphSONTokens.PROPERTIES);
    assertNotNull(properties);
    assertEquals(1, properties.size());

    final JsonNode friendProperties = properties.get("friends");
    assertEquals(1, friendProperties.size());
    final JsonNode friendsProperty = friendProperties.get(0).get(GraphSONTokens.VALUEPROP).get(GraphSONTokens.VALUE);
    assertNotNull(friendsProperty);
    assertEquals(3, friendsProperty.size());

    final String object1 = friendsProperty.get(0).asText();
    assertEquals("x", object1);

    final int object2 = friendsProperty.get(1).get(GraphSONTokens.VALUEPROP).asInt();
    assertEquals(5, object2);

    final JsonNode object3 = friendsProperty.get(2);
    assertEquals(500, object3.get("x").get(GraphSONTokens.VALUEPROP).asInt());
    assertEquals("some", object3.get("y").asText());
}
 
Example 19
Source File: DetachedVertexTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldConstructDetachedVertexFromPartsWithPropertiesOnProperties() {
    final Map<String, Object> properties = new HashMap<>();
    final Map<String, Object> propX1 = new HashMap<>();
    propX1.put("value", "a");
    propX1.put("id", 123);
    propX1.put("label", "x");
    propX1.put("properties", ElementHelper.asMap("propX1a", "a", "propX11", 1, "same", 123.01d, "extra", "something"));
    final Map<String, Object> propX2 = new HashMap<>();
    propX2.put("value", "c");
    propX2.put("id", 124);
    propX2.put("label", "x");
    properties.put("x", Arrays.asList(propX1, propX2));

    final Map<String, Object> propY1 = new HashMap<>();
    propY1.put("value", "b");
    propY1.put("id", 125);
    propY1.put("label", "");
    final Map<String, Object> propY2 = new HashMap<>();
    propY2.put("value", "d");
    propY2.put("id", 126);
    propY2.put("label", "y");
    properties.put("y", Arrays.asList(propY1, propY2));

    final DetachedVertex dv = new DetachedVertex(1, "test", properties);

    assertEquals(1, dv.id());
    assertEquals("test", dv.label());

    final List<VertexProperty<Object>> propertyX = IteratorUtils.list(dv.properties("x"));
    assertEquals(2, propertyX.size());
    assertTrue(propertyX.stream().allMatch(p ->
            p.label().equals(p.key())
                    && (p.id().equals(123) || p.id().equals(124))
                    && (p.value().equals("a") || p.value().equals("c"))));

    // there should be only one with properties on properties
    final VertexProperty<?> propertyOnProperty = propertyX.stream().filter(p -> p.properties().hasNext()).findAny().get();
    assertEquals("a", propertyOnProperty.properties("propX1a").next().value());
    assertEquals(1, propertyOnProperty.properties("propX11").next().value());
    assertEquals(123.01d, propertyOnProperty.properties("same").next().value());
    assertEquals("something", propertyOnProperty.properties("extra").next().value());
    assertEquals(4, IteratorUtils.count(propertyOnProperty.properties()));
}
 
Example 20
Source File: GraphSONMessageSerializerV3d0Test.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSerializeVertexWithEmbeddedMap() throws Exception {
    final Graph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    final Map<String, Object> map = new HashMap<>();
    map.put("x", 500);
    map.put("y", "some");

    final ArrayList<Object> friends = new ArrayList<>();
    friends.add("x");
    friends.add(5);
    friends.add(map);

    v.property(VertexProperty.Cardinality.single, "friends", friends);

    final List list = IteratorUtils.list(graph.vertices());

    final ResponseMessage response = convert(list);
    assertCommon(response);

    final List<Vertex> vertexList = (List<Vertex>) response.getResult().getData();
    assertEquals(1, vertexList.size());

    final Vertex deserializedVertex = vertexList.get(0);
    assertEquals(v.id(), deserializedVertex.id());
    assertEquals(Vertex.DEFAULT_LABEL, deserializedVertex.label());

    final List<VertexProperty> properties = new ArrayList<>();
    deserializedVertex.properties().forEachRemaining(properties::add);
    assertEquals(1, properties.size());

    final VertexProperty friendsProperty = properties.get(0);
    final List<Object> deserializedInnerList = (List<Object>) friendsProperty.value();

    assertEquals(3, deserializedInnerList.size());
    assertEquals("x", deserializedInnerList.get(0));
    assertEquals(5, deserializedInnerList.get(1));

    final Map<String, Object> deserializedInnerInnerMap = (Map<String, Object>) deserializedInnerList.get(2);
    assertEquals(2, deserializedInnerInnerMap.size());
    assertEquals(500, deserializedInnerInnerMap.get("x"));
    assertEquals("some", deserializedInnerInnerMap.get("y"));
}