You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2016/05/04 10:41:28 UTC
[28/50] [abbrv] incubator-tinkerpop git commit: fixed a self-loop bug
in StarGraph. Added StarGraphTest.shouldHandleSelfLoops() to ensure correct
behavior.
fixed a self-loop bug in StarGraph. Added StarGraphTest.shouldHandleSelfLoops() to ensure correct behavior.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/0563ec36
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/0563ec36
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/0563ec36
Branch: refs/heads/TINKERPOP-946
Commit: 0563ec36bb823d617acde6a15217c2b61471408d
Parents: 2451bb9
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Fri Apr 29 09:33:29 2016 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Fri Apr 29 09:33:29 2016 -0600
----------------------------------------------------------------------
.../gremlin/structure/util/star/StarGraph.java | 17 +++--
.../structure/util/star/StarGraphTest.java | 70 ++++++++++++++++++++
2 files changed, 82 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/0563ec36/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
index c613dbb..a8a72ff 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraph.java
@@ -206,10 +206,8 @@ public final class StarGraph implements Graph, Serializable {
});
vertex.edges(Direction.OUT).forEachRemaining(edge -> {
- if (!ElementHelper.areEqual(starVertex, edge.inVertex())) { // only do a self loop once
- final Edge starEdge = starVertex.addOutEdge(edge.label(), starGraph.addVertex(T.id, edge.inVertex().id()), T.id, edge.id());
- edge.properties().forEachRemaining(p -> starEdge.property(p.key(), p.value()));
- }
+ final Edge starEdge = starVertex.addOutEdge(edge.label(), starGraph.addVertex(T.id, edge.inVertex().id()), T.id, edge.id());
+ edge.properties().forEachRemaining(p -> starEdge.property(p.key(), p.value()));
});
return starGraph;
}
@@ -290,7 +288,16 @@ public final class StarGraph implements Graph, Serializable {
@Override
public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
- return this.addOutEdge(label, inVertex, keyValues);
+ final Edge edge = this.addOutEdge(label, inVertex, keyValues);
+ if (inVertex.equals(this)) {
+ List<Edge> inE = inEdges.get(label);
+ if (null == inE) {
+ inE = new ArrayList<>();
+ this.inEdges.put(label, inE);
+ }
+ inE.add(edge);
+ }
+ return edge;
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/0563ec36/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
index 2c5d6d2..fda6e37 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/util/star/StarGraphTest.java
@@ -40,6 +40,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
@@ -47,6 +48,7 @@ import java.util.UUID;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
@@ -161,6 +163,74 @@ public class StarGraphTest extends AbstractGremlinTest {
TestHelper.validateEquality(starVertex, createdVertex);
}
+ @Test
+ @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)
+ @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
+ @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
+ public void shouldHandleSelfLoops() {
+ assertEquals(0l, IteratorUtils.count(graph.vertices()));
+ assertEquals(0l, IteratorUtils.count(graph.edges()));
+ final Vertex vertex = graph.addVertex("person");
+ final VertexProperty<String> vertexProperty = vertex.property("name", "furnace");
+ final Edge edge = vertex.addEdge("self", vertex);
+ final Property<String> edgeProperty = edge.property("acl", "private");
+ assertEquals(1l, IteratorUtils.count(graph.vertices()));
+ assertEquals(1l, IteratorUtils.count(graph.edges()));
+ assertEquals(1l, IteratorUtils.count(vertex.properties()));
+ assertEquals(1l, IteratorUtils.count(edge.properties()));
+ assertEquals(vertexProperty, vertex.properties().next());
+ assertEquals(edgeProperty, edge.properties().next());
+ ///
+ final StarGraph starGraph = StarGraph.of(vertex);
+ final StarGraph.StarVertex starVertex = starGraph.getStarVertex();
+ final Edge starEdge = starVertex.edges(Direction.OUT).next();
+ assertEquals(vertex, starVertex);
+ assertEquals(edge, starEdge);
+ assertEquals(1l, IteratorUtils.count(starVertex.properties()));
+ assertEquals("furnace", starVertex.value("name"));
+ assertEquals(2l, IteratorUtils.count(starVertex.vertices(Direction.BOTH, "self")));
+ assertEquals(1l, IteratorUtils.count(starVertex.vertices(Direction.OUT, "self")));
+ assertEquals(1l, IteratorUtils.count(starVertex.vertices(Direction.IN, "self")));
+ Iterator<Vertex> vertexIterator = starVertex.vertices(Direction.BOTH, "self");
+ assertEquals(starVertex, vertexIterator.next());
+ assertEquals(starVertex, vertexIterator.next());
+ assertFalse(vertexIterator.hasNext());
+ assertEquals(starVertex, starVertex.vertices(Direction.OUT, "self").next());
+ assertEquals(starVertex, starVertex.vertices(Direction.IN, "self").next());
+ ///
+ assertEquals(2l, IteratorUtils.count(starVertex.vertices(Direction.BOTH)));
+ assertEquals(1l, IteratorUtils.count(starVertex.vertices(Direction.OUT)));
+ assertEquals(1l, IteratorUtils.count(starVertex.vertices(Direction.IN)));
+ vertexIterator = starVertex.vertices(Direction.BOTH);
+ assertEquals(starVertex, vertexIterator.next());
+ assertEquals(starVertex, vertexIterator.next());
+ assertFalse(vertexIterator.hasNext());
+ assertEquals(starVertex, starVertex.vertices(Direction.OUT).next());
+ assertEquals(starVertex, starVertex.vertices(Direction.IN).next());
+ ///
+ assertEquals(2l, IteratorUtils.count(starVertex.edges(Direction.BOTH, "self", "nothing")));
+ assertEquals(1l, IteratorUtils.count(starVertex.edges(Direction.OUT, "self", "nothing")));
+ assertEquals(1l, IteratorUtils.count(starVertex.edges(Direction.IN, "self", "nothing")));
+ Iterator<Edge> edgeIterator = starVertex.edges(Direction.BOTH, "self", "nothing");
+ Edge tempEdge = edgeIterator.next();
+ assertEquals(1l, IteratorUtils.count(tempEdge.properties()));
+ assertEquals("private", tempEdge.value("acl"));
+ assertEquals(starEdge, tempEdge);
+ tempEdge = edgeIterator.next();
+ assertEquals(1l, IteratorUtils.count(tempEdge.properties()));
+ assertEquals("private", tempEdge.value("acl"));
+ assertEquals(starEdge, tempEdge);
+ assertFalse(edgeIterator.hasNext());
+ assertEquals(starEdge, starVertex.edges(Direction.OUT, "self", "nothing").next());
+ assertEquals(starEdge, starVertex.edges(Direction.IN, "self", "nothing").next());
+ //
+ final StarGraph starGraphCopy = serializeDeserialize(starGraph).getValue0();
+ TestHelper.validateVertexEquality(vertex, starGraph.getStarVertex(), true);
+ TestHelper.validateVertexEquality(vertex, starGraphCopy.getStarVertex(), true);
+ TestHelper.validateVertexEquality(starGraph.getStarVertex(), starGraphCopy.getStarVertex(), true);
+ }
+
private Pair<StarGraph, Integer> serializeDeserialize(final StarGraph starGraph) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {