Skip to content
Snippets Groups Projects
Commit b380df29 authored by Sören Henning's avatar Sören Henning
Browse files

add graph exporter

parent f89c0b0f
No related branches found
No related tags found
1 merge request!17Get impletemented stages and Java 8
package kieker.analysis.graph;
import java.util.HashMap;
import java.util.Map;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
public class BlueprintsExporter {
private final Graph graph;
private final com.tinkerpop.blueprints.Graph exportGraph = new TinkerGraph();
private final Map<Vertex, com.tinkerpop.blueprints.Vertex> mappedVertices = new HashMap<>();
private static final String LABEL_PROPERTY = "label";
public BlueprintsExporter(final Graph graph) {
this.graph = graph;
}
public com.tinkerpop.blueprints.Graph exportGraph() {
mapVertices();
mapEdges();
return exportGraph;
}
private void mapVertices() {
for (final Vertex vertex : graph.getVertices()) {
com.tinkerpop.blueprints.Vertex mappedVertex = exportGraph.addVertex(vertex.getId());
mappedVertices.put(vertex, mappedVertex);
for (final String propertyKey : vertex.getPropertyKeys()) {
mappedVertex.setProperty(propertyKey, vertex.getProperty(propertyKey));
}
}
}
private void mapEdges() {
for (final Edge edge : graph.getEdges()) {
final com.tinkerpop.blueprints.Vertex mappedInVertex = mappedVertices.get(edge.getVertex(Direction.IN));
final com.tinkerpop.blueprints.Vertex mappedOutVertex = mappedVertices.get(edge.getVertex(Direction.OUT));
String label = edge.getProperty(LABEL_PROPERTY);
if (label == null) {
label = "";
}
com.tinkerpop.blueprints.Edge mappedEdge = exportGraph.addEdge(edge.getId(), mappedOutVertex, mappedInVertex, label);
for (final String propertyKey : edge.getPropertyKeys()) {
mappedEdge.setProperty(propertyKey, edge.getProperty(propertyKey));
}
}
}
}
......@@ -2,6 +2,10 @@ package kieker.analysis.graph;
public interface Graph {
public String getName();
public void setName(String name);
public Vertex addVertex(Object id);
public Vertex getVertex(Object id);
......
package kieker.analysis.graph;
import kieker.analysis.graph.export.BlueprintsExporter;
import kieker.analysis.graph.export.DotExporter;
import kieker.analysis.graph.impl.GraphImpl;
public class GraphTester {
......@@ -77,7 +79,10 @@ public class GraphTester {
// Export to Blueprints
BlueprintsExporter blueprintsExporter = new BlueprintsExporter(graph);
System.out.println(blueprintsExporter.exportGraph());
System.out.println(blueprintsExporter.export());
DotExporter dotExporter = new DotExporter(graph);
System.out.println(dotExporter.export());
}
......
package kieker.analysis.graph.export;
import kieker.analysis.graph.Edge;
import kieker.analysis.graph.Graph;
import kieker.analysis.graph.Vertex;
public abstract class AbstractExporter<O> {
protected Graph graph;
public AbstractExporter(final Graph graph) {
this.graph = graph;
}
public final O export() {
for (final Vertex vertex : graph.getVertices()) {
mapVertex(vertex);
}
for (final Edge edge : graph.getEdges()) {
mapEdge(edge);
}
return getExport();
}
protected abstract void mapVertex(Vertex vertex);
protected abstract void mapEdge(Edge edge);
protected abstract O getExport();
}
package kieker.analysis.graph.export;
import java.util.HashMap;
import java.util.Map;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import kieker.analysis.graph.Direction;
import kieker.analysis.graph.Edge;
import kieker.analysis.graph.Graph;
import kieker.analysis.graph.Vertex;
public class BlueprintsExporter extends AbstractExporter<com.tinkerpop.blueprints.Graph> {
private final com.tinkerpop.blueprints.Graph exportGraph = new TinkerGraph();
private final Map<Vertex, com.tinkerpop.blueprints.Vertex> mappedVertices = new HashMap<>();
private static final String LABEL_PROPERTY = "label";
public BlueprintsExporter(final Graph graph) {
super(graph);
}
@Override
protected void mapVertex(final Vertex vertex) {
com.tinkerpop.blueprints.Vertex mappedVertex = exportGraph.addVertex(vertex.getId());
mappedVertices.put(vertex, mappedVertex);
for (final String propertyKey : vertex.getPropertyKeys()) {
mappedVertex.setProperty(propertyKey, vertex.getProperty(propertyKey));
}
}
@Override
protected void mapEdge(final Edge edge) {
final com.tinkerpop.blueprints.Vertex mappedInVertex = mappedVertices.get(edge.getVertex(Direction.IN));
final com.tinkerpop.blueprints.Vertex mappedOutVertex = mappedVertices.get(edge.getVertex(Direction.OUT));
String label = edge.getProperty(LABEL_PROPERTY);
if (label == null) {
label = "";
}
com.tinkerpop.blueprints.Edge mappedEdge = exportGraph.addEdge(edge.getId(), mappedOutVertex, mappedInVertex, label);
for (final String propertyKey : edge.getPropertyKeys()) {
mappedEdge.setProperty(propertyKey, edge.getProperty(propertyKey));
}
}
@Override
protected com.tinkerpop.blueprints.Graph getExport() {
return exportGraph;
}
}
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();
}
}
package kieker.analysis.graph.export;
import kieker.analysis.graph.Graph;
//TODO maybe no interface required
public interface Exporter<O> {
public O export(Graph graph);
}
......@@ -11,6 +11,8 @@ import kieker.analysis.graph.Vertex;
public class GraphImpl implements Graph {
protected String name = "G";
protected Map<String, Vertex> vertices = new HashMap<String, Vertex>();
protected Map<String, Edge> edges = new HashMap<String, Edge>();
......@@ -126,4 +128,14 @@ public class GraphImpl implements Graph {
return idString;
}
@Override
public String getName() {
return this.name;
}
@Override
public void setName(final String name) {
this.name = name;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment