From 8bff34ebb0c544eb8be0eb6271d72a0afa54db58 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Henning?= <stu114708@informatik.uni-kiel.de>
Date: Tue, 19 Apr 2016 17:23:27 +0200
Subject: [PATCH] redesigned dot export process (not done)

---
 .../graph/export/dot/DotElementExporter.java  | 60 +++++++------------
 .../export/dot/DotExportConfiguration.java    | 48 +++++++++++++++
 .../util/graph/export/dot/DotExporter.java    | 31 ++++++++--
 .../dot/attributes/DotClusterAttribute.java   | 18 ++++++
 .../util/dot/attributes/DotEdgeAttribute.java | 18 ++++++
 .../dot/attributes/DotGraphAttribute.java     | 18 ++++++
 .../util/dot/attributes/DotNodeAttribute.java | 18 ++++++
 .../dot/attributes/DotSubgraphAttribute.java  | 18 ++++++
 8 files changed, 186 insertions(+), 43 deletions(-)
 create mode 100644 src/main/java/kieker/analysis/util/graph/export/dot/DotExportConfiguration.java
 create mode 100644 src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotClusterAttribute.java
 create mode 100644 src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotEdgeAttribute.java
 create mode 100644 src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotGraphAttribute.java
 create mode 100644 src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotNodeAttribute.java
 create mode 100644 src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotSubgraphAttribute.java

diff --git a/src/main/java/kieker/analysis/util/graph/export/dot/DotElementExporter.java b/src/main/java/kieker/analysis/util/graph/export/dot/DotElementExporter.java
index 61285c74..aba973b7 100644
--- a/src/main/java/kieker/analysis/util/graph/export/dot/DotElementExporter.java
+++ b/src/main/java/kieker/analysis/util/graph/export/dot/DotElementExporter.java
@@ -1,26 +1,28 @@
 package kieker.analysis.util.graph.export.dot;
 
 import java.io.IOException;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 import kieker.analysis.util.graph.Direction;
 import kieker.analysis.util.graph.Edge;
-import kieker.analysis.util.graph.Element;
 import kieker.analysis.util.graph.Graph;
 import kieker.analysis.util.graph.Vertex;
 import kieker.analysis.util.graph.export.AbstractTransformer;
 import kieker.analysis.util.graph.util.dot.DotGraphWriter;
+import kieker.analysis.util.graph.util.dot.attributes.DotClusterAttribute;
 
 class DotElementExporter extends AbstractTransformer<Void> {
 
 	protected final DotGraphWriter dotGraphWriter;
+	protected final DotExportConfiguration configuration;
 
-	protected DotElementExporter(final Graph graph, final DotGraphWriter dotGraphWriter) {
+	protected DotElementExporter(final Graph graph, final DotGraphWriter dotGraphWriter, final DotExportConfiguration configuration) {
 		super(graph);
 		this.dotGraphWriter = dotGraphWriter;
+		this.configuration = configuration;
 	}
 
 	@Override
@@ -31,19 +33,16 @@ class DotElementExporter extends AbstractTransformer<Void> {
 
 				dotGraphWriter.addClusterStart(vertex.getId().toString());
 
-				Map<String, String> attributes = transformProperties(vertex);
-				attributes.putAll(transformProperties(childGraph));
-				for (Entry<String, String> attribute : attributes.entrySet()) {
-					dotGraphWriter.addGraphAttribute(attribute.getKey(), attribute.getValue());
+				for (Entry<DotClusterAttribute, Function<Vertex, String>> attribute : configuration.getClusterAttributes().entrySet()) {
+					dotGraphWriter.addGraphAttribute(attribute.getKey().toString(), attribute.getValue().apply(vertex));
 				}
-				transformDefaultElementProperties(childGraph);
 
-				DotElementExporter childGraphWriter = new DotElementExporter(childGraph, dotGraphWriter);
+				DotElementExporter childGraphWriter = new DotElementExporter(childGraph, dotGraphWriter, configuration);
 				childGraphWriter.transform();
 
 				dotGraphWriter.addClusterStop();
 			} else {
-				dotGraphWriter.addNode(vertex.getId().toString(), transformProperties(vertex));
+				dotGraphWriter.addNode(vertex.getId().toString(), getAttributes(vertex));
 			}
 		} catch (IOException e) {
 			handleIOException(e);
@@ -55,45 +54,28 @@ class DotElementExporter extends AbstractTransformer<Void> {
 		try {
 			final String sourceId = edge.getVertex(Direction.OUT).getId().toString();
 			final String targetId = edge.getVertex(Direction.IN).getId().toString();
-			dotGraphWriter.addEdge(sourceId, targetId, transformProperties(edge));
+
+			dotGraphWriter.addEdge(sourceId, targetId, getAttributes(edge));
 		} catch (IOException e) {
 			handleIOException(e);
 		}
 
 	}
 
-	protected void transformDefaultElementProperties(final Graph graph) throws IOException {
-		final Object defaultNodeProperties = graph.getProperty(DotExportPropertyTokens.DEFAULT_NODE_PROPERTIES);
-		if (defaultNodeProperties instanceof Map) {
-			@SuppressWarnings("unchecked")
-			Map<String, String> defaultNodeAttributes = (Map<String, String>) defaultNodeProperties;
-			dotGraphWriter.addDefaultNodeAttributes(defaultNodeAttributes);
-		}
-		final Object defaultEdgeProperties = graph.getProperty(DotExportPropertyTokens.DEFAULT_EDGE_PROPERTIES);
-		if (defaultEdgeProperties instanceof Map) {
-			@SuppressWarnings("unchecked")
-			Map<String, String> defaultEdgeAttributes = (Map<String, String>) defaultEdgeProperties;
-			dotGraphWriter.addDefaultEdgeAttributes(defaultEdgeAttributes);
-		}
-	}
-
 	protected void handleIOException(final IOException ioException) {
 		throw new IllegalStateException(ioException);
 	}
 
-	protected Map<String, String> transformProperties(final Element element) {
-
-		final Set<String> propertyKeys = element.getPropertyKeys();
-		final Map<String, String> properties = new HashMap<>(propertyKeys.size(), 1);
-
-		for (final String propertyKey : propertyKeys) {
-			if (propertyKey.equals(DotExportPropertyTokens.DEFAULT_NODE_PROPERTIES) || propertyKey.equals(DotExportPropertyTokens.DEFAULT_EDGE_PROPERTIES)) {
-				break;
-			}
-			properties.put(propertyKey, element.getProperty(propertyKey).toString());
-		}
+	protected Map<String, String> getAttributes(final Edge edge) {
+		return configuration.getEdgeAttributes().entrySet().stream().collect(Collectors.toMap(
+				e -> e.getKey().toString(),
+				e -> e.getValue().apply(edge)));
+	}
 
-		return properties;
+	protected Map<String, String> getAttributes(final Vertex vertex) {
+		return configuration.getNodeAttributes().entrySet().stream().collect(Collectors.toMap(
+				e -> e.getKey().toString(),
+				e -> e.getValue().apply(vertex)));
 	}
 
 	@Override
diff --git a/src/main/java/kieker/analysis/util/graph/export/dot/DotExportConfiguration.java b/src/main/java/kieker/analysis/util/graph/export/dot/DotExportConfiguration.java
new file mode 100644
index 00000000..a1ef1e84
--- /dev/null
+++ b/src/main/java/kieker/analysis/util/graph/export/dot/DotExportConfiguration.java
@@ -0,0 +1,48 @@
+package kieker.analysis.util.graph.export.dot;
+
+import java.util.EnumMap;
+import java.util.Map;
+import java.util.function.Function;
+
+import kieker.analysis.util.graph.Edge;
+import kieker.analysis.util.graph.Graph;
+import kieker.analysis.util.graph.Vertex;
+import kieker.analysis.util.graph.util.dot.attributes.DotClusterAttribute;
+import kieker.analysis.util.graph.util.dot.attributes.DotEdgeAttribute;
+import kieker.analysis.util.graph.util.dot.attributes.DotGraphAttribute;
+import kieker.analysis.util.graph.util.dot.attributes.DotNodeAttribute;
+
+public class DotExportConfiguration {
+
+	final private Map<DotGraphAttribute, Function<Graph, String>> graphAttributes = new EnumMap<>(DotGraphAttribute.class);
+	final private Map<DotNodeAttribute, Function<Graph, String>> defaultNodeAttributes = new EnumMap<>(DotNodeAttribute.class);
+	final private Map<DotEdgeAttribute, Function<Graph, String>> defaultEdgeAttributes = new EnumMap<>(DotEdgeAttribute.class);
+	final private Map<DotNodeAttribute, Function<Vertex, String>> nodeAttributes = new EnumMap<>(DotNodeAttribute.class);
+	final private Map<DotEdgeAttribute, Function<Edge, String>> edgeAttributes = new EnumMap<>(DotEdgeAttribute.class);
+	final private Map<DotClusterAttribute, Function<Vertex, String>> clusterAttributes = new EnumMap<>(DotClusterAttribute.class);
+
+	public Map<DotGraphAttribute, Function<Graph, String>> getGraphAttributes() {
+		return graphAttributes;
+	}
+
+	public Map<DotNodeAttribute, Function<Graph, String>> getDefaultNodeAttributes() {
+		return defaultNodeAttributes;
+	}
+
+	public Map<DotEdgeAttribute, Function<Graph, String>> getDefaultEdgeAttributes() {
+		return defaultEdgeAttributes;
+	}
+
+	public Map<DotNodeAttribute, Function<Vertex, String>> getNodeAttributes() {
+		return nodeAttributes;
+	}
+
+	public Map<DotEdgeAttribute, Function<Edge, String>> getEdgeAttributes() {
+		return edgeAttributes;
+	}
+
+	public Map<DotClusterAttribute, Function<Vertex, String>> getClusterAttributes() {
+		return clusterAttributes;
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/util/graph/export/dot/DotExporter.java b/src/main/java/kieker/analysis/util/graph/export/dot/DotExporter.java
index b30aa05b..842c526a 100644
--- a/src/main/java/kieker/analysis/util/graph/export/dot/DotExporter.java
+++ b/src/main/java/kieker/analysis/util/graph/export/dot/DotExporter.java
@@ -2,25 +2,48 @@ package kieker.analysis.util.graph.export.dot;
 
 import java.io.IOException;
 import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Map.Entry;
+import java.util.function.Function;
 
 import kieker.analysis.util.graph.Graph;
 import kieker.analysis.util.graph.util.dot.DotGraphWriter;
+import kieker.analysis.util.graph.util.dot.attributes.DotEdgeAttribute;
+import kieker.analysis.util.graph.util.dot.attributes.DotGraphAttribute;
+import kieker.analysis.util.graph.util.dot.attributes.DotNodeAttribute;
 
 public class DotExporter extends DotElementExporter {
 
 	public DotExporter(final Graph graph, final Writer writer) {
-		super(graph, new DotGraphWriter(writer));
+		this(graph, writer, new DotExportConfiguration());
+	}
+
+	public DotExporter(final Graph graph, final Writer writer, final DotExportConfiguration configuration) {
+		super(graph, new DotGraphWriter(writer), configuration);
 	}
 
 	@Override
 	protected void beforeTransformation() {
 		try {
 			dotGraphWriter.start(graph.getName());
-			for (Entry<String, String> attribute : transformProperties(graph).entrySet()) {
-				dotGraphWriter.addGraphAttribute(attribute.getKey(), attribute.getValue());
+
+			for (Entry<DotGraphAttribute, Function<Graph, String>> attribute : configuration.getGraphAttributes().entrySet()) {
+				dotGraphWriter.addGraphAttribute(attribute.getKey().toString(), attribute.getValue().apply(graph));
+			}
+
+			Map<String, String> defaultNodeAttributes = new HashMap<>();
+			for (Entry<DotNodeAttribute, Function<Graph, String>> attribute : configuration.getDefaultNodeAttributes().entrySet()) {
+				defaultNodeAttributes.put(attribute.getKey().toString(), attribute.getValue().apply(graph));
+			}
+			dotGraphWriter.addDefaultNodeAttributes(defaultNodeAttributes);
+
+			Map<String, String> defaultEdgeAttributes = new HashMap<>();
+			for (Entry<DotEdgeAttribute, Function<Graph, String>> attribute : configuration.getDefaultEdgeAttributes().entrySet()) {
+				defaultEdgeAttributes.put(attribute.getKey().toString(), attribute.getValue().apply(graph));
 			}
-			transformDefaultElementProperties(graph);
+			dotGraphWriter.addDefaultEdgeAttributes(defaultEdgeAttributes);
+
 		} catch (IOException e) {
 			handleIOException(e);
 		}
diff --git a/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotClusterAttribute.java b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotClusterAttribute.java
new file mode 100644
index 00000000..203b4ed5
--- /dev/null
+++ b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotClusterAttribute.java
@@ -0,0 +1,18 @@
+package kieker.analysis.util.graph.util.dot.attributes;
+
+public enum DotClusterAttribute {
+
+	LABEL("label");
+
+	private final String attribute;
+
+	private DotClusterAttribute(final String attribute) {
+		this.attribute = attribute;
+	}
+
+	@Override
+	public String toString() {
+		return this.attribute;
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotEdgeAttribute.java b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotEdgeAttribute.java
new file mode 100644
index 00000000..f3a9b8ba
--- /dev/null
+++ b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotEdgeAttribute.java
@@ -0,0 +1,18 @@
+package kieker.analysis.util.graph.util.dot.attributes;
+
+public enum DotEdgeAttribute {
+
+	LABEL("label"), ARROWHEAD("arrowhead"), ARROWSIZE("arrowsize"), ARROWTAIL("arrowtail");
+
+	private final String attribute;
+
+	private DotEdgeAttribute(final String attribute) {
+		this.attribute = attribute;
+	}
+
+	@Override
+	public String toString() {
+		return this.attribute;
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotGraphAttribute.java b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotGraphAttribute.java
new file mode 100644
index 00000000..218bc062
--- /dev/null
+++ b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotGraphAttribute.java
@@ -0,0 +1,18 @@
+package kieker.analysis.util.graph.util.dot.attributes;
+
+public enum DotGraphAttribute {
+
+	LABEL("label"), RANKDIR("rankdir");
+
+	private final String attribute;
+
+	private DotGraphAttribute(final String attribute) {
+		this.attribute = attribute;
+	}
+
+	@Override
+	public String toString() {
+		return this.attribute;
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotNodeAttribute.java b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotNodeAttribute.java
new file mode 100644
index 00000000..28dbb4bb
--- /dev/null
+++ b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotNodeAttribute.java
@@ -0,0 +1,18 @@
+package kieker.analysis.util.graph.util.dot.attributes;
+
+public enum DotNodeAttribute {
+
+	LABEL("label"), SHAPE("shape");
+
+	private final String attribute;
+
+	private DotNodeAttribute(final String attribute) {
+		this.attribute = attribute;
+	}
+
+	@Override
+	public String toString() {
+		return this.attribute;
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotSubgraphAttribute.java b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotSubgraphAttribute.java
new file mode 100644
index 00000000..27084a43
--- /dev/null
+++ b/src/main/java/kieker/analysis/util/graph/util/dot/attributes/DotSubgraphAttribute.java
@@ -0,0 +1,18 @@
+package kieker.analysis.util.graph.util.dot.attributes;
+
+public enum DotSubgraphAttribute {
+
+	;
+
+	private final String attribute;
+
+	private DotSubgraphAttribute(final String attribute) {
+		this.attribute = attribute;
+	}
+
+	@Override
+	public String toString() {
+		return this.attribute;
+	}
+
+}
-- 
GitLab