From 746307173c6af3b414b4c288c10607f2cd9d40ff 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 15:19:12 +0100 Subject: [PATCH] work on nested graph library --- .../kieker/analysis/graph/impl/GraphImpl.java | 15 ++--- .../analysis/graph/impl/VertexImpl.java | 65 ++++++++++--------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/main/java/kieker/analysis/graph/impl/GraphImpl.java b/src/main/java/kieker/analysis/graph/impl/GraphImpl.java index 33a23726..2ea50921 100644 --- a/src/main/java/kieker/analysis/graph/impl/GraphImpl.java +++ b/src/main/java/kieker/analysis/graph/impl/GraphImpl.java @@ -66,7 +66,7 @@ public class GraphImpl implements Graph { @Override public Edge addEdge(final Object id, final Vertex outVertex, final Vertex inVertex) { - String idString = null; + String idString; if (id == null) { do { idString = getDefaultId(); @@ -78,10 +78,10 @@ public class GraphImpl implements Graph { } } - Edge edge = new EdgeImpl(idString, outVertex, inVertex, this); + final Edge edge = new EdgeImpl(idString, outVertex, inVertex, this); this.edges.put(edge.getId().toString(), edge); - ((VertexImpl) outVertex).addOutEdge(idString, edge); - ((VertexImpl) inVertex).addInEdge(idString, edge); + ((VertexImpl) outVertex).addOutEdge(edge); + ((VertexImpl) inVertex).addInEdge(edge); return edge; } @@ -106,11 +106,8 @@ public class GraphImpl implements Graph { 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()); + ((VertexImpl) edge.getVertex(Direction.IN)).removeInEdge(edge); + ((VertexImpl) edge.getVertex(Direction.OUT)).removeOutEdge(edge); this.edges.remove(edge.getId().toString()); } diff --git a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java index 5611e523..2bfc8421 100644 --- a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java +++ b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java @@ -1,9 +1,8 @@ package kieker.analysis.graph.impl; +import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import kieker.analysis.graph.Direction; import kieker.analysis.graph.Edge; @@ -12,8 +11,8 @@ import kieker.analysis.graph.Vertex; class VertexImpl extends ElementImpl implements Vertex { - protected Map<String, Set<Edge>> outEdges = new HashMap<String, Set<Edge>>(); - protected Map<String, Set<Edge>> inEdges = new HashMap<String, Set<Edge>>(); + protected Map<String, Edge> outEdges = new HashMap<String, Edge>(); + protected Map<String, Edge> inEdges = new HashMap<String, Edge>(); protected VertexImpl(final String id, final GraphImpl graph) { super(id, graph); @@ -33,21 +32,31 @@ class VertexImpl extends ElementImpl implements Vertex { @Override 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); - * } - */ + if (direction.equals(Direction.OUT)) { + return new ArrayList<>(this.outEdges.values()); + } else if (direction.equals(Direction.IN)) { + return new ArrayList<>(this.inEdges.values()); + } else { + ArrayList<Edge> edges = new ArrayList<>(this.outEdges.values()); + edges.addAll(this.inEdges.values()); + return edges; + } } @Override public Iterable<Vertex> getVertices(final Direction direction) { - // TODO Auto-generated method stub - return null; + + if (direction.equals(Direction.BOTH)) { + ArrayList<Vertex> vertices = (ArrayList<Vertex>) getVertices(Direction.IN); + vertices.addAll((ArrayList<Vertex>) getVertices(Direction.OUT)); + return vertices; + } + + ArrayList<Vertex> vertices = new ArrayList<>(); + for (Edge edge : getEdges(direction)) { + vertices.add(edge.getVertex(direction.opposite())); + } + return vertices; } @Override @@ -65,22 +74,20 @@ class VertexImpl extends ElementImpl implements Vertex { return this.graph.addEdge(id, this, inVertex); } - protected void addOutEdge(final String idString, final Edge edge) { - Set<Edge> edges = this.outEdges.get(idString); - if (edges == null) { - edges = new HashSet<Edge>(); - this.outEdges.put(idString, edges); - } - edges.add(edge); + protected void addOutEdge(final Edge edge) { + this.outEdges.put(edge.getId().toString(), edge); } - protected void addInEdge(final String idString, final Edge edge) { - Set<Edge> edges = this.inEdges.get(idString); - if (edges == null) { - edges = new HashSet<Edge>(); - this.inEdges.put(idString, edges); - } - edges.add(edge); + protected void addInEdge(final Edge edge) { + this.inEdges.put(edge.getId().toString(), edge); + } + + protected void removeInEdge(final Edge edge) { + this.inEdges.remove(edge.getId().toString()); + } + + protected void removeOutEdge(final Edge edge) { + this.outEdges.remove(edge.getId().toString()); } } -- GitLab