diff --git a/src/main/java/kieker/analysis/graph/GraphTester.java b/src/main/java/kieker/analysis/graph/GraphTester.java index 577ec64016841692bc20e30a8e2762d23edfe678..3c619c9f7b51a3d7403a63fd32d6b272fa2551c7 100644 --- a/src/main/java/kieker/analysis/graph/GraphTester.java +++ b/src/main/java/kieker/analysis/graph/GraphTester.java @@ -1,38 +1,23 @@ package kieker.analysis.graph; -import com.tinkerpop.blueprints.Graph; -import com.tinkerpop.blueprints.impls.tg.TinkerGraph; +import kieker.analysis.graph.impl.GraphImpl; public class GraphTester { public static void main(final String[] args) { - Graph graph1 = new TinkerGraph(); - Graph graph2 = new TinkerGraph(); + Graph graph = new GraphImpl(); - com.tinkerpop.blueprints.Vertex g1n1 = graph1.addVertex("g1::n1"); + Vertex node1 = graph.addVertex("n1"); - com.tinkerpop.blueprints.Vertex g2n1 = graph2.addVertex("g2::n1"); + Vertex node2 = graph.addVertex("n2"); - com.tinkerpop.blueprints.Edge edge = g1n1.addEdge("...", g2n1); + Graph subgraph = node1.addSubgraph(); - System.out.println(graph1); - System.out.println(graph2); + Vertex node11 = subgraph.addVertex("n1::n1"); + + // node2.addEdgeTo(node11); - /* - * Graph graph = new GraphImpl(); - * - * Vertex node1 = graph.addVertex("n1"); - * - * Vertex node2 = graph.addVertex("n2"); - * - * Graph subgraph = node1.addSubgraph(); - * - * Vertex node11 = subgraph.addVertex("n1::n1"); - * - * node2.addEdgeTo(node11);* - * - */ } } diff --git a/src/main/java/kieker/analysis/graph/Vertex.java b/src/main/java/kieker/analysis/graph/Vertex.java index 7953729e577442ab20831637696d1d99a89e63d0..9434544396e077dac3479404f7c44619224bab1e 100644 --- a/src/main/java/kieker/analysis/graph/Vertex.java +++ b/src/main/java/kieker/analysis/graph/Vertex.java @@ -8,9 +8,9 @@ public interface Vertex extends Element { public Graph addSubgraph(); - // public Iterable<Edge> getEdges(Direction direction, String... labels); + // public Iterable<Edge> getEdges(Direction direction); - // public Iterable<Vertex> getVertices(Direction direction, String... labels); + // public Iterable<Vertex> getVertices(Direction direction); public Edge addEdge(Vertex inVertex); diff --git a/src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java b/src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..1fe1e2464383e685b63e333cc70f9272d7566cd7 --- /dev/null +++ b/src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java @@ -0,0 +1,54 @@ +package kieker.analysis.graph.impl; + +/** + * The ExceptionFactory provides standard exceptions for graphs. + * + * @author Sören Henning + */ +class ExceptionFactory { + + // Graph related exceptions + + public static IllegalArgumentException vertexIdCanNotBeNull() { + return new IllegalArgumentException("Vertex id can not be null"); + } + + public static IllegalArgumentException edgeIdCanNotBeNull() { + return new IllegalArgumentException("Edge id can not be null"); + } + + public static IllegalArgumentException vertexWithIdAlreadyExists(final Object id) { + return new IllegalArgumentException("Vertex with id already exists: " + id); + } + + public static IllegalArgumentException edgeWithIdAlreadyExists(final Object id) { + return new IllegalArgumentException("Edge with id already exists: " + id); + } + + public static IllegalStateException vertexWithIdDoesNotExist(final Object id) { + return new IllegalStateException("Vertex with id does not exist: " + id); + } + + public static IllegalStateException edgeWithIdDoesNotExist(final Object id) { + return new IllegalStateException("Edge with id does not exist: " + id); + } + + // Element related exceptions + + public static IllegalArgumentException propertyKeyIsReserved(final String key) { + return new IllegalArgumentException("Property key is reserved for all elements: " + key); + } + + public static IllegalArgumentException propertyKeyCanNotBeEmpty() { + return new IllegalArgumentException("Property key can not be the empty string"); + } + + public static IllegalArgumentException propertyKeyCanNotBeNull() { + return new IllegalArgumentException("Property key can not be null"); + } + + public static IllegalArgumentException propertyValueCanNotBeNull() { + return new IllegalArgumentException("Property value can not be null"); + } + +} diff --git a/src/main/java/kieker/analysis/graph/impl/GraphImpl.java b/src/main/java/kieker/analysis/graph/impl/GraphImpl.java index 14c70dbb9610930020465f6c49addd083005f819..4cdc8d6623ae1f9bddf3e5d50c8c66456adf7c8d 100644 --- a/src/main/java/kieker/analysis/graph/impl/GraphImpl.java +++ b/src/main/java/kieker/analysis/graph/impl/GraphImpl.java @@ -27,7 +27,7 @@ public class GraphImpl implements Graph { } else { idString = id.toString(); if (vertices.containsKey(idString)) { - // TODO Throw Exception ExceptionFactory.vertexWithIdAlreadyExists(id) + throw ExceptionFactory.vertexWithIdAlreadyExists(id); } } @@ -38,7 +38,9 @@ public class GraphImpl implements Graph { @Override public Vertex getVertex(final Object id) { - // TODO exception if not exists + if (id == null) { + throw ExceptionFactory.vertexIdCanNotBeNull(); + } String idString = id.toString(); return this.vertices.get(idString); } @@ -68,21 +70,23 @@ public class GraphImpl implements Graph { } else { idString = id.toString(); if (edges.containsKey(idString)) { - // TODO Throw Exception ExceptionFactory.edgeWithIdAlreadyExists(id) + throw ExceptionFactory.edgeWithIdAlreadyExists(id); } } Edge edge = new EdgeImpl(idString, outVertex, inVertex, this); - edges.put(edge.getId().toString(), edge); - - // TODO Add Edge to vertices + this.edges.put(edge.getId().toString(), edge); + ((VertexImpl) outVertex).addOutEdge(idString, edge); + ((VertexImpl) inVertex).addInEdge(idString, edge); return edge; } @Override public Edge getEdge(final Object id) { - // TODO exception if not exists + if (id == null) { + throw ExceptionFactory.edgeIdCanNotBeNull(); + } String idString = id.toString(); return this.edges.get(idString); } diff --git a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java index 0135d1b539ee276da09e28b2d4b43ef7e992f331..9ac75f7e8b21acbdfba2e33c60633fc431627111 100644 --- a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java +++ b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java @@ -1,29 +1,37 @@ package kieker.analysis.graph.impl; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + import kieker.analysis.graph.Edge; import kieker.analysis.graph.Graph; 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 VertexImpl(final String id, final GraphImpl graph) { super(id, graph); } @Override - public Boolean hasSubgraph() { + public Graph addSubgraph() { // TODO Auto-generated method stub - return false; + return null; } @Override - public Graph getSubgraph() { + public Boolean hasSubgraph() { // TODO Auto-generated method stub - return null; + return false; } @Override - public Graph addSubgraph() { + public Graph getSubgraph() { // TODO Auto-generated method stub return null; } @@ -33,13 +41,31 @@ class VertexImpl extends ElementImpl implements Vertex { return addEdge(null, inVertex); } + @Override + public void remove() { + graph.removeVertex(this); + } + @Override public Edge addEdge(final Object id, final Vertex inVertex) { return this.graph.addEdge(id, this, inVertex); } - @Override - public void remove() { - graph.removeVertex(this); + 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 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); } }