From 55f5d41f4ed4ee894c99265ae48f0c3df1aa08bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Henning?= <stu114708@informatik.uni-kiel.de>
Date: Tue, 1 Mar 2016 14:07:03 +0100
Subject: [PATCH] worked on graph exporting

---
 .../kieker/analysis/graph/GraphTester.java    | 75 +++++++++----------
 .../graph/export/AbstractExporter.java        |  1 +
 .../graph/export/AbstractTransformer.java     | 34 +++++++++
 .../analysis/graph/export/DotExporter.java    | 38 ----------
 .../graph/export/dot/DotExporter.java         | 17 +++++
 .../graph/export/dot/GraphTransformer.java    | 49 ++++++++++++
 .../graph/export/graphml/GraphMLExporter.java | 46 ++++++++++++
 .../export/graphml/GraphTypeTransformer.java  | 49 ++++++++++++
 .../analysis/graph/impl/VertexImpl.java       |  2 +-
 .../graph/traversal/FlatGraphTraverser.java   | 21 ++++++
 .../graph/traversal/NestedGraphTraverser.java | 28 +++++++
 11 files changed, 282 insertions(+), 78 deletions(-)
 create mode 100644 src/main/java/kieker/analysis/graph/export/AbstractTransformer.java
 delete mode 100644 src/main/java/kieker/analysis/graph/export/DotExporter.java
 create mode 100644 src/main/java/kieker/analysis/graph/export/dot/DotExporter.java
 create mode 100644 src/main/java/kieker/analysis/graph/export/dot/GraphTransformer.java
 create mode 100644 src/main/java/kieker/analysis/graph/export/graphml/GraphMLExporter.java
 create mode 100644 src/main/java/kieker/analysis/graph/export/graphml/GraphTypeTransformer.java
 create mode 100644 src/main/java/kieker/analysis/graph/traversal/FlatGraphTraverser.java
 create mode 100644 src/main/java/kieker/analysis/graph/traversal/NestedGraphTraverser.java

diff --git a/src/main/java/kieker/analysis/graph/GraphTester.java b/src/main/java/kieker/analysis/graph/GraphTester.java
index ef9c05bb..cc5f78ae 100644
--- a/src/main/java/kieker/analysis/graph/GraphTester.java
+++ b/src/main/java/kieker/analysis/graph/GraphTester.java
@@ -1,17 +1,6 @@
 package kieker.analysis.graph;
 
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-
-import org.graphdrawing.graphml.EdgeType;
-import org.graphdrawing.graphml.GraphType;
-import org.graphdrawing.graphml.GraphmlType;
-import org.graphdrawing.graphml.KeyType;
-import org.graphdrawing.graphml.NodeType;
-import org.graphdrawing.graphml.ObjectFactory;
-
+import kieker.analysis.graph.export.graphml.GraphMLExporter;
 import kieker.analysis.graph.impl.GraphImpl;
 
 public class GraphTester {
@@ -52,7 +41,7 @@ public class GraphTester {
 		// BlueprintsExporter blueprintsExporter = new BlueprintsExporter(graph);
 		// System.out.println(blueprintsExporter.exportGraph());
 
-		node1snd.remove();
+		// node1snd.remove();
 
 		for (Vertex vertex : graph.getVertices()) {
 			System.out.println(vertex.getId());
@@ -65,6 +54,12 @@ public class GraphTester {
 		Vertex node1sub3 = node1.getChildGraph().addVertex("n1::s3");
 		node1sub3.addEdge("wormhole", node4);
 
+		if (node1.hasChildGraph()) {
+			System.out.println("Node 1 has Child graph");
+		} else {
+			System.out.println("Node 1 does not have a child graph");
+		}
+
 		System.out.println("Node 1's child graph");
 
 		for (Vertex vertex : node1.getChildGraph().getVertices()) {
@@ -85,33 +80,35 @@ public class GraphTester {
 			System.out.println(edge.getId());
 		}
 
-		GraphmlType graphmlType = new GraphmlType();
-		graphmlType.getKey().add(new KeyType());
-		GraphType graphType = new GraphType();
-		graphType.getDataOrNodeOrEdge().add(new NodeType());
-		graphType.getDataOrNodeOrEdge().add(new NodeType());
-		graphType.getDataOrNodeOrEdge().add(new EdgeType());
-		graphmlType.getGraphOrData().add(graphType);
-
-		try {
-			JAXBContext context = JAXBContext.newInstance(GraphmlType.class);
-			Marshaller m = context.createMarshaller();
-			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
-
-			ObjectFactory objectFactory = new ObjectFactory();
-			JAXBElement<GraphmlType> element = objectFactory.createGraphml(graphmlType);
-
-			m.marshal(element, System.out);
-		} catch (JAXBException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-
-		// Use object factory
+		GraphMLExporter graphMLExporter = new GraphMLExporter();
+
+		graphMLExporter.export(graph, System.out);
+
+		// JAXB Test
+
+		//
+		// GraphmlType graphmlType = new GraphmlType();
+		// graphmlType.getKey().add(new KeyType());
+		// GraphType graphType = new GraphType();
+		// graphType.getDataOrNodeOrEdge().add(new NodeType());
+		// graphType.getDataOrNodeOrEdge().add(new NodeType());
+		// graphType.getDataOrNodeOrEdge().add(new EdgeType());
+		// graphmlType.getGraphOrData().add(graphType);
+		//
+		// try {
+		// JAXBContext context = JAXBContext.newInstance(GraphmlType.class);
+		// Marshaller m = context.createMarshaller();
+		// m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+		//
+		// ObjectFactory objectFactory = new ObjectFactory();
+		// JAXBElement<GraphmlType> element = objectFactory.createGraphml(graphmlType);
+		//
+		// m.marshal(element, System.out);
+		// } catch (JAXBException e) {
+		// // TODO Auto-generated catch block
+		// e.printStackTrace();
+		// }
 
-		// http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html
-		// http://stackoverflow.com/questions/24519449/unable-to-marshal-type-as-an-element-because-it-is-missing-an-xmlrootelement-an
-		// http://stackoverflow.com/questions/819720/no-xmlrootelement-generated-by-jaxb
 	}
 
 }
diff --git a/src/main/java/kieker/analysis/graph/export/AbstractExporter.java b/src/main/java/kieker/analysis/graph/export/AbstractExporter.java
index 05cc3b13..16ece18e 100644
--- a/src/main/java/kieker/analysis/graph/export/AbstractExporter.java
+++ b/src/main/java/kieker/analysis/graph/export/AbstractExporter.java
@@ -4,6 +4,7 @@ import kieker.analysis.graph.Edge;
 import kieker.analysis.graph.Graph;
 import kieker.analysis.graph.Vertex;
 
+//TODO unused
 public abstract class AbstractExporter<O> {
 
 	protected Graph graph;
diff --git a/src/main/java/kieker/analysis/graph/export/AbstractTransformer.java b/src/main/java/kieker/analysis/graph/export/AbstractTransformer.java
new file mode 100644
index 00000000..0c640f4e
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/export/AbstractTransformer.java
@@ -0,0 +1,34 @@
+package kieker.analysis.graph.export;
+
+import kieker.analysis.graph.Edge;
+import kieker.analysis.graph.Graph;
+import kieker.analysis.graph.Vertex;
+
+//TODO better name mapper
+public abstract class AbstractTransformer<O> {
+
+	protected Graph graph;
+
+	public AbstractTransformer(final Graph graph) {
+		this.graph = graph;
+	}
+
+	public final O transform() {
+
+		for (final Vertex vertex : graph.getVertices()) {
+			mapVertex(vertex);
+		}
+
+		for (final Edge edge : graph.getEdges()) {
+			mapEdge(edge);
+		}
+
+		return getTransformation();
+	}
+
+	protected abstract void mapVertex(Vertex vertex);
+
+	protected abstract void mapEdge(Edge edge);
+
+	protected abstract O getTransformation();
+}
diff --git a/src/main/java/kieker/analysis/graph/export/DotExporter.java b/src/main/java/kieker/analysis/graph/export/DotExporter.java
deleted file mode 100644
index 72eaee2a..00000000
--- a/src/main/java/kieker/analysis/graph/export/DotExporter.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package kieker.analysis.graph.export;
-
-import kieker.analysis.graph.Direction;
-import kieker.analysis.graph.Edge;
-import kieker.analysis.graph.Graph;
-import kieker.analysis.graph.Vertex;
-import kieker.analysis.util.DotBuilder;
-
-public class DotExporter extends AbstractExporter<String> {
-
-	private final DotBuilder dotBuilder;
-
-	public DotExporter(final Graph graph) {
-		super(graph);
-		dotBuilder = new DotBuilder(graph.getName());
-	}
-
-	@Override
-	protected void mapVertex(final Vertex vertex) {
-		dotBuilder.addNode(vertex.getId().toString());
-		// TODO Add style, label, etc
-		// TODO Handle child graph
-	}
-
-	@Override
-	protected void mapEdge(final Edge edge) {
-		String sourceId = edge.getVertex(Direction.OUT).getId().toString();
-		String targetId = edge.getVertex(Direction.IN).getId().toString();
-		dotBuilder.addEdge(sourceId, targetId);
-		// TODO Add style, label, etc
-	}
-
-	@Override
-	protected String getExport() {
-		return dotBuilder.get();
-	}
-
-}
diff --git a/src/main/java/kieker/analysis/graph/export/dot/DotExporter.java b/src/main/java/kieker/analysis/graph/export/dot/DotExporter.java
new file mode 100644
index 00000000..31519988
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/export/dot/DotExporter.java
@@ -0,0 +1,17 @@
+package kieker.analysis.graph.export.dot;
+
+import java.io.OutputStream;
+
+import kieker.analysis.graph.Graph;
+
+public class DotExporter {
+
+	public void export(final Graph graph, final OutputStream outputStream) {
+
+		GraphTransformer graphTransformer = new GraphTransformer(graph);
+		// TODO Receiving a dot object would be (maybe) better
+		String dotString = graphTransformer.transform();
+
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/graph/export/dot/GraphTransformer.java b/src/main/java/kieker/analysis/graph/export/dot/GraphTransformer.java
new file mode 100644
index 00000000..18ff2f6a
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/export/dot/GraphTransformer.java
@@ -0,0 +1,49 @@
+package kieker.analysis.graph.export.dot;
+
+import kieker.analysis.graph.Direction;
+import kieker.analysis.graph.Edge;
+import kieker.analysis.graph.Graph;
+import kieker.analysis.graph.Vertex;
+import kieker.analysis.graph.export.AbstractTransformer;
+import kieker.analysis.util.DotBuilder;
+
+public class GraphTransformer extends AbstractTransformer<String> {
+
+	private final DotBuilder dotBuilder;
+
+	public GraphTransformer(final Graph graph) {
+		super(graph);
+		dotBuilder = new DotBuilder(graph.getName());
+	}
+
+	@Override
+	protected void mapVertex(final Vertex vertex) {
+		if (vertex.hasChildGraph()) {
+			Graph childGraph = vertex.getChildGraph();
+			GraphTransformer graphTransformer = new GraphTransformer(childGraph);
+			String childGraphString = graphTransformer.transform();
+			// TODO We need a subgraph cluster_XYZ { ... }
+			// TODO dotBuilder.addCluster();
+		} else {
+			dotBuilder.addNode(vertex.getId().toString());
+			// TODO Add style, label, etc
+			// TODO Handle child graph
+		}
+
+	}
+
+	@Override
+	protected void mapEdge(final Edge edge) {
+		String sourceId = edge.getVertex(Direction.OUT).getId().toString();
+		String targetId = edge.getVertex(Direction.IN).getId().toString();
+		dotBuilder.addEdge(sourceId, targetId);
+		// TODO Add style, label, etc
+
+	}
+
+	@Override
+	protected String getTransformation() {
+		return dotBuilder.get();
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/graph/export/graphml/GraphMLExporter.java b/src/main/java/kieker/analysis/graph/export/graphml/GraphMLExporter.java
new file mode 100644
index 00000000..853efca0
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/export/graphml/GraphMLExporter.java
@@ -0,0 +1,46 @@
+package kieker.analysis.graph.export.graphml;
+
+import java.io.OutputStream;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+
+import org.graphdrawing.graphml.GraphType;
+import org.graphdrawing.graphml.GraphmlType;
+import org.graphdrawing.graphml.ObjectFactory;
+
+import kieker.analysis.graph.Graph;
+
+public class GraphMLExporter {
+
+	private static final Boolean FORMATTED_OUTPUT = Boolean.TRUE;
+
+	public void export(final Graph graph, final OutputStream outputStream) {
+
+		GraphmlType graphmlType = new GraphmlType();
+
+		GraphTypeTransformer graphTypeTransformer = new GraphTypeTransformer(graph);
+		GraphType graphType = graphTypeTransformer.transform();
+
+		graphmlType.getGraphOrData().add(graphType);
+
+		try {
+			JAXBContext context = JAXBContext.newInstance(GraphmlType.class);
+			Marshaller marshaller = context.createMarshaller();
+			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, FORMATTED_OUTPUT);
+
+			ObjectFactory objectFactory = new ObjectFactory();
+			JAXBElement<GraphmlType> rootElement = objectFactory.createGraphml(graphmlType);
+
+			marshaller.marshal(rootElement, outputStream);
+		} catch (JAXBException e) {
+			// TODO Auto-generated catch block
+			// TODO wrap exception
+			e.printStackTrace();
+		}
+
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/graph/export/graphml/GraphTypeTransformer.java b/src/main/java/kieker/analysis/graph/export/graphml/GraphTypeTransformer.java
new file mode 100644
index 00000000..b9367e7c
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/export/graphml/GraphTypeTransformer.java
@@ -0,0 +1,49 @@
+package kieker.analysis.graph.export.graphml;
+
+import org.graphdrawing.graphml.EdgeType;
+import org.graphdrawing.graphml.GraphType;
+import org.graphdrawing.graphml.NodeType;
+
+import kieker.analysis.graph.Edge;
+import kieker.analysis.graph.Graph;
+import kieker.analysis.graph.Vertex;
+import kieker.analysis.graph.export.AbstractTransformer;
+
+public class GraphTypeTransformer extends AbstractTransformer<GraphType> {
+
+	private final GraphType graphType;
+
+	public GraphTypeTransformer(final Graph graph) {
+		super(graph);
+		graphType = new GraphType();
+	}
+
+	@Override
+	protected void mapVertex(final Vertex vertex) {
+
+		NodeType nodeType = new NodeType();
+		// TODO Map Properties + id
+
+		if (vertex.hasChildGraph()) {
+			Graph childGraph = vertex.getChildGraph();
+			GraphTypeTransformer graphTypeTransformer = new GraphTypeTransformer(childGraph);
+			GraphType childGraphType = graphTypeTransformer.transform();
+			nodeType.setGraph(childGraphType);
+		}
+
+		graphType.getDataOrNodeOrEdge().add(nodeType);
+	}
+
+	@Override
+	protected void mapEdge(final Edge edge) {
+		EdgeType edgeType = new EdgeType();
+		// TODO Map Properties + id + source/target
+		graphType.getDataOrNodeOrEdge().add(edgeType);
+	}
+
+	@Override
+	protected GraphType getTransformation() {
+		return graphType;
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java
index feed76b4..aa3fef03 100644
--- a/src/main/java/kieker/analysis/graph/impl/VertexImpl.java
+++ b/src/main/java/kieker/analysis/graph/impl/VertexImpl.java
@@ -27,7 +27,7 @@ class VertexImpl extends ElementImpl implements Vertex {
 
 	@Override
 	public boolean hasChildGraph() {
-		return (this.childGraph == null);
+		return this.childGraph != null;
 	}
 
 	@Override
diff --git a/src/main/java/kieker/analysis/graph/traversal/FlatGraphTraverser.java b/src/main/java/kieker/analysis/graph/traversal/FlatGraphTraverser.java
new file mode 100644
index 00000000..182bad43
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/traversal/FlatGraphTraverser.java
@@ -0,0 +1,21 @@
+package kieker.analysis.graph.traversal;
+
+import kieker.analysis.graph.Edge;
+import kieker.analysis.graph.Graph;
+import kieker.analysis.graph.Vertex;
+
+public class FlatGraphTraverser {
+
+	public void traverse(final Graph graph) {
+
+		for (Vertex vertex : graph.getVertices()) {
+			// TODO call (vertex) visitor
+		}
+
+		for (Edge edge : graph.getEdges()) {
+			// TODO Call (edge)visitor
+		}
+
+	}
+
+}
diff --git a/src/main/java/kieker/analysis/graph/traversal/NestedGraphTraverser.java b/src/main/java/kieker/analysis/graph/traversal/NestedGraphTraverser.java
new file mode 100644
index 00000000..046ac6ce
--- /dev/null
+++ b/src/main/java/kieker/analysis/graph/traversal/NestedGraphTraverser.java
@@ -0,0 +1,28 @@
+package kieker.analysis.graph.traversal;
+
+import kieker.analysis.graph.Edge;
+import kieker.analysis.graph.Graph;
+import kieker.analysis.graph.Vertex;
+
+//TODO unused
+public class NestedGraphTraverser {
+
+	public void traverse(final Graph graph) {
+
+		for (Vertex vertex : graph.getVertices()) {
+			// TODO call (vertex) visitor
+
+			NestedGraphTraverser childGraphTraverser = new NestedGraphTraverser();
+			// Visitor vistor;
+			// childGraphTraverser.addVisitor(visitor);
+			childGraphTraverser.traverse(graph);
+			// visitor.
+		}
+
+		for (Edge edge : graph.getEdges()) {
+			// TODO Call (edge)visitor
+		}
+
+	}
+
+}
-- 
GitLab