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

add GraphML export stages

parent edcbb4b1
No related branches found
No related tags found
1 merge request!17Get impletemented stages and Java 8
Pipeline #
Showing
with 160 additions and 141 deletions
......@@ -4,7 +4,6 @@ import java.io.OutputStreamWriter;
import java.util.ArrayList;
import kieker.analysis.graph.export.dot.DotExporter;
import kieker.analysis.graph.export.graphml.GraphMLExporter;
import kieker.analysis.graph.impl.GraphImpl;
public class GraphTester {
......@@ -96,14 +95,11 @@ public class GraphTester {
vertex.setProperty("xyzprop", new ArrayList<Boolean>());
}
GraphMLExporter graphMLExporter = new GraphMLExporter();
graphMLExporter.export(graph, System.out);
DotExporter dotWriter = new DotExporter(graph, new OutputStreamWriter(System.out));
dotWriter.transform();
// DotExporter dotExporter = new DotExporter();
// dotExporter.export(graph, System.out);
DotExporter dotExporter = new DotExporter(graph, new OutputStreamWriter(System.out));
dotExporter.transform();
// Could be useful for testing
//
......
......@@ -6,10 +6,6 @@ import teetime.stage.basic.AbstractTransformation;
public class BlueprintsTransformerStage extends AbstractTransformation<Graph, com.tinkerpop.blueprints.Graph> {
public BlueprintsTransformerStage() {
super();
}
@Override
protected void execute(final Graph graph) {
BlueprintsTransformer transformer = new BlueprintsTransformer(graph);
......
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;
//TODO remove
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 {
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<GraphmlType> rootElement = objectFactory.createGraphml(graphmlType);
JAXBContext context = JAXBContext.newInstance(rootElement.getDeclaredType());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, FORMATTED_OUTPUT);
marshaller.marshal(rootElement, outputStream);
} catch (JAXBException e) {
// TODO Auto-generated catch block
// TODO wrap exception
e.printStackTrace();
}
}
}
package kieker.analysis.graph.export.graphml;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import kieker.analysis.graph.Graph;
import kieker.analysis.graph.mapping.GraphMapper;
public class GraphMLFileWriterComposite extends GraphMLWriterComposite {
public GraphMLFileWriterComposite(final GraphMapper<String> fileNameMapper) {
super(new GraphMapper<OutputStream>() {
@Override
public OutputStream map(final Graph graph) {
try {
return new FileOutputStream(fileNameMapper.map(graph));
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
});
}
}
package kieker.analysis.graph.export.graphml;
import java.io.OutputStream;
import javax.xml.bind.JAXBElement;
import org.graphdrawing.graphml.GraphmlType;
import org.graphdrawing.graphml.ObjectFactory;
import kieker.analysis.graph.Graph;
import kieker.analysis.graph.mapping.GraphMapper;
import kieker.analysis.util.JAXBMarshalElement;
import teetime.stage.basic.AbstractTransformation;
public class GraphMLTransformationStage extends AbstractTransformation<Graph, JAXBMarshalElement<GraphmlType>> {
private final ObjectFactory objectFactory = new ObjectFactory();
private final GraphMapper<OutputStream> outputStreamMapper;
public GraphMLTransformationStage(final GraphMapper<OutputStream> outputStreamMapper) {
this.outputStreamMapper = outputStreamMapper;
}
@Override
protected void execute(final Graph graph) {
final GraphTypeTransformer graphTypeTransformer = new GraphTypeTransformer(graph);
final GraphmlType graphmlType = new GraphmlType();
graphmlType.getGraphOrData().add(graphTypeTransformer.transform());
final JAXBElement<GraphmlType> jaxbElement = objectFactory.createGraphml(graphmlType);
final OutputStream outputStream = outputStreamMapper.map(graph);
final JAXBMarshalElement<GraphmlType> marshalElement = new JAXBMarshalElement<>(jaxbElement, outputStream);
this.getOutputPort().send(marshalElement);
}
}
package kieker.analysis.graph.export.graphml;
import java.io.OutputStream;
import org.graphdrawing.graphml.GraphmlType;
import kieker.analysis.graph.Graph;
import kieker.analysis.graph.mapping.GraphMapper;
import kieker.analysis.util.JAXBMarshalStage;
import teetime.framework.CompositeStage;
import teetime.framework.InputPort;
public class GraphMLWriterComposite extends CompositeStage {
private final InputPort<Graph> inputPort;
public GraphMLWriterComposite(final GraphMapper<OutputStream> outputStreamMapper) {
final GraphMLTransformationStage graphTypeTransformer;
final JAXBMarshalStage<GraphmlType> jaxbMarshaller;
graphTypeTransformer = new GraphMLTransformationStage(outputStreamMapper);
jaxbMarshaller = new JAXBMarshalStage<GraphmlType>(GraphmlType.class);
this.inputPort = graphTypeTransformer.getInputPort();
super.connectPorts(graphTypeTransformer.getOutputPort(), jaxbMarshaller.getInputPort());
}
public InputPort<Graph> getInputPort() {
return this.inputPort;
}
}
package kieker.analysis.graph.mapping;
import kieker.analysis.graph.Graph;
import kieker.analysis.graph.util.IMapper;
public interface GraphMapper<O> extends IMapper<Graph, O> {
}
package kieker.analysis.graph.util;
public interface IMapper<I, O> {
public O map(I object);
}
package kieker.analysis.util;
import java.io.OutputStream;
import javax.xml.bind.JAXBElement;
public class JAXBMarshalElement<T> {
private JAXBElement<T> element;
private OutputStream outputStream;
public JAXBMarshalElement(final JAXBElement<T> element, final OutputStream outputStream) {
this.element = element;
this.outputStream = outputStream;
}
public JAXBElement<T> getElement() {
return element;
}
public void setElement(final JAXBElement<T> element) {
this.element = element;
}
public OutputStream getOutputStream() {
return outputStream;
}
public void setOutputStream(final OutputStream outputStream) {
this.outputStream = outputStream;
}
}
package kieker.analysis.util;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
......@@ -12,33 +10,26 @@ import teetime.framework.AbstractConsumerStage;
* This stage marshals the content tree rooted at an incoming element into an
* output stream.
*
* A class object has to be passed at creation. Only elements of this type or
* elements of this type wrapped in a {@code JAXBElement} could be marshaled.
*
* Incoming elements have to be an instance of {@code JAXBElement} or are
* annotated with {@code @XmlRootElement}.
* A class object has to be passed at creation. Only elements of this type
* wrapped in a {@code JAXBElement} could be marshaled.
*
* Marshaling with this stage is faster than using
* {@link UniversalJAXBMarshalStage}. So It should be used if the type of the
* incoming elements is known.
* Incoming elements must be {@code JAXBMarshalElement} which stores the
* {@code JAXBElement} and an output stream.
*
* @author Sören Henning
*
*/
public class JAXBMarshalStage extends AbstractConsumerStage<Object> {
public class JAXBMarshalStage<T> extends AbstractConsumerStage<JAXBMarshalElement<T>> {
private static final Boolean FORMATTED_OUTPUT_DEFAULT = Boolean.TRUE;
private final Marshaller marshaller;
private final OutputStream outputStream;
public JAXBMarshalStage(final Class<?> elementsClass, final OutputStream outputStream) {
this(elementsClass, outputStream, FORMATTED_OUTPUT_DEFAULT);
public JAXBMarshalStage(final Class<T> elementsClass) {
this(elementsClass, FORMATTED_OUTPUT_DEFAULT);
}
public JAXBMarshalStage(final Class<?> elementsClass, final OutputStream outputStream, final Boolean formattedOutput) {
this.outputStream = outputStream;
public JAXBMarshalStage(final Class<T> elementsClass, final Boolean formattedOutput) {
try {
this.marshaller = JAXBContext.newInstance(elementsClass).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
......@@ -49,10 +40,9 @@ public class JAXBMarshalStage extends AbstractConsumerStage<Object> {
}
@Override
protected void execute(final Object element) {
protected void execute(final JAXBMarshalElement<T> element) {
try {
marshaller.marshal(element, outputStream);
marshaller.marshal(element.getElement(), element.getOutputStream());
} catch (JAXBException e) {
// TODO Exception
throw new IllegalStateException("The received element could not be marshalled.", e);
......
package kieker.analysis.util;
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 teetime.framework.AbstractConsumerStage;
/**
* This stage marshals the content tree rooted at an incoming element into an
* output stream.
*
* Incoming elements have to be an instance of {@code JAXBElement} or are
* annotated with {@code @XmlRootElement}.
*
* {@link JAXBMarshalStage} should be preferred if all incoming elements have
* the same type and the type is known, because {@link JAXBMarshalStage} is
* faster.
*
* @author Sören Henning
*
*/
public class UniversalJAXBMarshalStage extends AbstractConsumerStage<Object> {
private static final Boolean FORMATTED_OUTPUT_DEFAULT = Boolean.TRUE;
private final OutputStream outputStream;
private final Boolean formattedOutput;
public UniversalJAXBMarshalStage(final OutputStream outputStream) {
this(outputStream, FORMATTED_OUTPUT_DEFAULT);
}
public UniversalJAXBMarshalStage(final OutputStream outputStream, final Boolean formattedOutput) {
this.outputStream = outputStream;
this.formattedOutput = formattedOutput;
}
@Override
protected void execute(final Object element) {
// Determine whether the incoming object is wrapped in a JAXBElement
Class<?> elementClass;
if (element instanceof JAXBElement) {
elementClass = ((JAXBElement<?>) element).getDeclaredType();
} else {
elementClass = element.getClass();
}
try {
final Marshaller marshaller = JAXBContext.newInstance(elementClass).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
marshaller.marshal(element, outputStream);
} catch (JAXBException e) {
// TODO Exception
throw new IllegalStateException("The received element could not be marshalled.", e);
}
}
}
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