From b7b44524414b377e9bf338af66a9277ad2364701 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Henning?= <stu114708@informatik.uni-kiel.de>
Date: Tue, 2 Feb 2016 14:03:16 +0100
Subject: [PATCH] work on nested graph library

---
 .../java/kieker/analysis/graph/Direction.java | 22 +++++++++++++++
 src/main/java/kieker/analysis/graph/Edge.java |  2 +-
 .../kieker/analysis/graph/GraphTester.java    |  2 +-
 .../java/kieker/analysis/graph/Vertex.java    | 10 +++----
 .../kieker/analysis/graph/impl/EdgeImpl.java  | 12 ++++++++-
 .../analysis/graph/impl/ExceptionFactory.java |  4 +++
 .../kieker/analysis/graph/impl/GraphImpl.java | 23 ++++++++++++----
 .../analysis/graph/impl/VertexImpl.java       | 27 ++++++++++++++-----
 8 files changed, 82 insertions(+), 20 deletions(-)
 create mode 100644 src/main/java/kieker/analysis/graph/Direction.java

diff --git a/src/main/java/kieker/analysis/graph/Direction.java b/src/main/java/kieker/analysis/graph/Direction.java
new file mode 100644
index 00000000..fea516c1
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/Direction.java
@@ -0,0 +1,22 @@
+package kieker.analysis.graph;
+
+/**
+ * Direction is used to denote the direction of an edge or location of a vertex on an edge.
+ *
+ * @author Sören Henning
+ *
+ */
+public enum Direction {
+
+	OUT, IN, BOTH;
+
+	public Direction opposite() {
+		if (this.equals(OUT)) {
+			return IN;
+		} else if (this.equals(IN)) {
+			return OUT;
+		} else {
+			return BOTH;
+		}
+	}
+}
diff --git a/src/main/java/kieker/analysis/graph/Edge.java b/src/main/java/kieker/analysis/graph/Edge.java
index bc29f760..82f8065b 100644
--- a/src/main/java/kieker/analysis/graph/Edge.java
+++ b/src/main/java/kieker/analysis/graph/Edge.java
@@ -2,6 +2,6 @@ package kieker.analysis.graph;
 
 public interface Edge extends Element {
 
-	// public Vertex getVertex(Direction direction) throws IllegalArgumentException;
+	public Vertex getVertex(Direction direction) throws IllegalArgumentException;
 
 }
diff --git a/src/main/java/kieker/analysis/graph/GraphTester.java b/src/main/java/kieker/analysis/graph/GraphTester.java
index 3c619c9f..03566b68 100644
--- a/src/main/java/kieker/analysis/graph/GraphTester.java
+++ b/src/main/java/kieker/analysis/graph/GraphTester.java
@@ -12,7 +12,7 @@ public class GraphTester {
 
 		Vertex node2 = graph.addVertex("n2");
 
-		Graph subgraph = node1.addSubgraph();
+		Graph subgraph = node1.addChildGraph();
 
 		Vertex node11 = subgraph.addVertex("n1::n1");
 
diff --git a/src/main/java/kieker/analysis/graph/Vertex.java b/src/main/java/kieker/analysis/graph/Vertex.java
index 94345443..6d6b02a8 100644
--- a/src/main/java/kieker/analysis/graph/Vertex.java
+++ b/src/main/java/kieker/analysis/graph/Vertex.java
@@ -2,15 +2,13 @@ package kieker.analysis.graph;
 
 public interface Vertex extends Element {
 
-	public Boolean hasSubgraph();
+	public Graph getChildGraph();
 
-	public Graph getSubgraph();
+	public Graph addChildGraph();
 
-	public Graph addSubgraph();
+	public Iterable<Edge> getEdges(Direction direction);
 
-	// public Iterable<Edge> getEdges(Direction direction);
-
-	// public Iterable<Vertex> getVertices(Direction direction);
+	public Iterable<Vertex> getVertices(Direction direction);
 
 	public Edge addEdge(Vertex inVertex);
 
diff --git a/src/main/java/kieker/analysis/graph/impl/EdgeImpl.java b/src/main/java/kieker/analysis/graph/impl/EdgeImpl.java
index 05cc2949..6b0dbae3 100644
--- a/src/main/java/kieker/analysis/graph/impl/EdgeImpl.java
+++ b/src/main/java/kieker/analysis/graph/impl/EdgeImpl.java
@@ -1,5 +1,6 @@
 package kieker.analysis.graph.impl;
 
+import kieker.analysis.graph.Direction;
 import kieker.analysis.graph.Edge;
 import kieker.analysis.graph.Vertex;
 
@@ -19,6 +20,15 @@ class EdgeImpl extends ElementImpl implements Edge {
 		this.graph.removeEdge(this);
 	}
 
-	// public Vertex getVertex(Direction direction) throws IllegalArgumentException;
+	@Override
+	public Vertex getVertex(final Direction direction) throws IllegalArgumentException {
+		if (direction.equals(Direction.IN)) {
+			return this.inVertex;
+		} else if (direction.equals(Direction.OUT)) {
+			return this.outVertex;
+		} else {
+			throw ExceptionFactory.bothIsNotSupported();
+		}
+	}
 
 }
diff --git a/src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java b/src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java
index 1fe1e246..63605411 100644
--- a/src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java
+++ b/src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java
@@ -33,6 +33,10 @@ class ExceptionFactory {
 		return new IllegalStateException("Edge with id does not exist: " + id);
 	}
 
+	public static IllegalArgumentException bothIsNotSupported() {
+		return new IllegalArgumentException("A direction of BOTH is not supported");
+	}
+
 	// Element related exceptions
 
 	public static IllegalArgumentException propertyKeyIsReserved(final String key) {
diff --git a/src/main/java/kieker/analysis/graph/impl/GraphImpl.java b/src/main/java/kieker/analysis/graph/impl/GraphImpl.java
index 4cdc8d66..33a23726 100644
--- a/src/main/java/kieker/analysis/graph/impl/GraphImpl.java
+++ b/src/main/java/kieker/analysis/graph/impl/GraphImpl.java
@@ -4,6 +4,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 
+import kieker.analysis.graph.Direction;
 import kieker.analysis.graph.Edge;
 import kieker.analysis.graph.Graph;
 import kieker.analysis.graph.Vertex;
@@ -52,12 +53,15 @@ public class GraphImpl implements Graph {
 
 	@Override
 	public void removeVertex(final Vertex vertex) {
-		// TODO Check whether the vertex exists
-
-		// TODO Remove all edges
+		if (!this.vertices.containsKey(vertex.getId().toString())) {
+			throw ExceptionFactory.vertexWithIdDoesNotExist(vertex.getId());
+		}
 
-		// TODO remove this vertex from map
+		for (Edge edge : vertex.getEdges(Direction.BOTH)) {
+			this.removeEdge(edge);
+		}
 
+		this.vertices.remove(vertex.getId().toString());
 	}
 
 	@Override
@@ -98,8 +102,17 @@ public class GraphImpl implements Graph {
 
 	@Override
 	public void removeEdge(final Edge edge) {
-		// TODO Auto-generated method stub
+		if (!this.edges.containsKey(edge.getId().toString())) {
+			throw ExceptionFactory.edgeWithIdDoesNotExist(edge.getId());
+		}
+
+		final VertexImpl outVertex = (VertexImpl) edge.getVertex(Direction.OUT);
+		outVertex.outEdges.remove(edge.getId().toString());
+
+		final VertexImpl inVertex = (VertexImpl) edge.getVertex(Direction.IN);
+		inVertex.inEdges.remove(edge.getId().toString());
 
+		this.edges.remove(edge.getId().toString());
 	}
 
 	private String getDefaultId() {
diff --git a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java
index 9ac75f7e..5611e523 100644
--- a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java
+++ b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java
@@ -5,6 +5,7 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
+import kieker.analysis.graph.Direction;
 import kieker.analysis.graph.Edge;
 import kieker.analysis.graph.Graph;
 import kieker.analysis.graph.Vertex;
@@ -19,26 +20,34 @@ class VertexImpl extends ElementImpl implements Vertex {
 	}
 
 	@Override
-	public Graph addSubgraph() {
+	public Graph addChildGraph() {
 		// TODO Auto-generated method stub
 		return null;
 	}
 
 	@Override
-	public Boolean hasSubgraph() {
+	public Graph getChildGraph() {
 		// TODO Auto-generated method stub
-		return false;
+		return null;
 	}
 
 	@Override
-	public Graph getSubgraph() {
+	public Iterable<Edge> getEdges(final Direction direction) {
 		// TODO Auto-generated method stub
 		return null;
+		/*
+		 * if (direction.equals(Direction.OUT)) {
+		 * return this.getOutEdges(labels);
+		 * } else if (direction.equals(Direction.IN))
+		 * return this.getInEdges(labels);
+		 * }
+		 */
 	}
 
 	@Override
-	public Edge addEdge(final Vertex inVertex) {
-		return addEdge(null, inVertex);
+	public Iterable<Vertex> getVertices(final Direction direction) {
+		// TODO Auto-generated method stub
+		return null;
 	}
 
 	@Override
@@ -46,6 +55,11 @@ class VertexImpl extends ElementImpl implements Vertex {
 		graph.removeVertex(this);
 	}
 
+	@Override
+	public Edge addEdge(final Vertex inVertex) {
+		return addEdge(null, inVertex);
+	}
+
 	@Override
 	public Edge addEdge(final Object id, final Vertex inVertex) {
 		return this.graph.addEdge(id, this, inVertex);
@@ -68,4 +82,5 @@ class VertexImpl extends ElementImpl implements Vertex {
 		}
 		edges.add(edge);
 	}
+
 }
-- 
GitLab