private SubGraph() {
}
+ public static Graph extract(Vertex start, Object... edgePath) {
+ return new SubGraph().process(start, edgePath);
+ }
+
private Graph process(Vertex start, Object... edgePath) {
+ copy(start);
browse(start, 0, edgePath);
for (Edge edge : edgesToCopy) {
- Vertex from = edge.getVertex(Direction.IN);
- Vertex to = edge.getVertex(Direction.OUT);
+ Vertex from = edge.getVertex(Direction.OUT);
+ Vertex to = edge.getVertex(Direction.IN);
Edge copyEdge = sub.addEdge(edge.getId(), sub.getVertex(from.getId()), sub.getVertex(to.getId()), edge.getLabel());
ElementHelper.copyProperties(edge, copyEdge);
}
return sub;
}
- public static Graph extract(Vertex start, Object... edgePath) {
- return new SubGraph().process(start, edgePath);
- }
-
- private void browse(Vertex vertex, int cursor, Object... edgePath) {
- if (vertex != null) {
- copy(vertex);
+ private void browse(Vertex from, int cursor, Object... edgePath) {
+ if (from != null) {
if (cursor < edgePath.length) {
String edgeLabel = (String) edgePath[cursor];
Direction edgeDirection = (Direction) edgePath[cursor + 1];
- Iterable<Edge> edges = vertex.getEdges(edgeDirection, edgeLabel);
+ Iterable<Edge> edges = from.getEdges(edgeDirection, edgeLabel);
for (Edge edge : edges) {
edgesToCopy.add(edge);
- browse(edge.getVertex(edgeDirection.opposite()), cursor + 2, edgePath);
+ Vertex tail = edge.getVertex(edgeDirection.opposite());
+ copy(tail);
+ browse(tail, cursor + 2, edgePath);
}
}
}
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.util.GraphHelper;
+import org.junit.Before;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;
public class SubGraphTest {
- @Test
- public void extract_acyclic_graph() {
- TinkerGraph graph = new TinkerGraph();
- Vertex a = GraphHelper.addVertex(graph, null, "key", "a");
- Vertex b = GraphHelper.addVertex(graph, null, "key", "b");
- Vertex c = GraphHelper.addVertex(graph, null, "key", "c");
- Vertex d = GraphHelper.addVertex(graph, null, "key", "d");
- Vertex e = GraphHelper.addVertex(graph, null, "key", "e");
- Edge ab = GraphHelper.addEdge(graph, null, a, b, "uses");
- Edge bc = GraphHelper.addEdge(graph, null, b, c, "inherits");
- Edge ad = GraphHelper.addEdge(graph, null, a, d, "uses");
- Edge de = GraphHelper.addEdge(graph, null, d, e, "implements");
+ TinkerGraph graph;
+ Vertex a, b, c, d, e;
+ Edge ab, bc, ad, de;
+
+ @Before
+ public void before() {
+ graph = new TinkerGraph();
+ a = GraphHelper.addVertex(graph, null, "key", "a");
+ b = GraphHelper.addVertex(graph, null, "key", "b");
+ c = GraphHelper.addVertex(graph, null, "key", "c");
+ d = GraphHelper.addVertex(graph, null, "key", "d");
+ e = GraphHelper.addVertex(graph, null, "key", "e");
+
+ ab = GraphHelper.addEdge(graph, null, a, b, "uses");
+ bc = GraphHelper.addEdge(graph, null, b, c, "inherits");
+ ad = GraphHelper.addEdge(graph, null, a, d, "uses");
+ de = GraphHelper.addEdge(graph, null, d, e, "implements");
// a -uses-> b -inherits -> c
// a -uses-> d -implements-> e
+ }
+ @Test
+ public void should_extract_graph() {
Graph sub = SubGraph.extract(a, "uses", Direction.OUT, "implements", Direction.OUT);
// a -uses-> b
assertThat(sub.getEdges()).hasSize(3);
assertThat(sub.getEdge(ab.getId()).getLabel()).isEqualTo("uses");
- assertThat(sub.getEdge(ab.getId()).getId()).isEqualTo(ab.getId());
+ assertThat(sub.getEdge(ab.getId()).toString()).isEqualTo(ab.toString());
assertThat(sub.getEdge(bc.getId())).isNull();
- assertThat(sub.getEdge(ad.getId()).getId()).isEqualTo(ad.getId());
- assertThat(sub.getEdge(de.getId()).getId()).isEqualTo(de.getId());
+ assertThat(sub.getEdge(ad.getId()).toString()).isEqualTo(ad.toString());
+ assertThat(sub.getEdge(de.getId()).toString()).isEqualTo(de.toString());
+ }
+
+ @Test
+ public void should_check_edge_direction() {
+ Graph sub = SubGraph.extract(a, "uses", Direction.IN /* instead of out */, "implements", Direction.OUT);
+
+ assertThat(sub.getVertices()).hasSize(1);
+ assertThat(sub.getVertex(a.getId())).isNotNull();
+ assertThat(sub.getEdges()).isEmpty();
}
}