From c7edfff3094d4752b0e9d0b934683d0d8ff7eb9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Henning?= <stu114708@informatik.uni-kiel.de> Date: Mon, 1 Feb 2016 18:41:36 +0100 Subject: [PATCH] work on nested graph library --- .../kieker/analysis/graph/GraphTester.java | 31 +++-------- .../java/kieker/analysis/graph/Vertex.java | 4 +- .../analysis/graph/impl/ExceptionFactory.java | 54 +++++++++++++++++++ .../kieker/analysis/graph/impl/GraphImpl.java | 18 ++++--- .../analysis/graph/impl/VertexImpl.java | 42 ++++++++++++--- 5 files changed, 109 insertions(+), 40 deletions(-) create mode 100644 src/main/java/kieker/analysis/graph/impl/ExceptionFactory.java diff --git a/src/main/java/kieker/analysis/graph/GraphTester.java b/src/main/java/kieker/analysis/graph/GraphTester.java index 577ec640..3c619c9f 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 7953729e..94345443 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 00000000..1fe1e246 --- /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 14c70dbb..4cdc8d66 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 0135d1b5..9ac75f7e 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); } } -- GitLab