Permalink
Browse files

Added example in the readme

  • Loading branch information...
bzim committed Jun 15, 2016
1 parent b2903c7 commit 0f85be3c8ad26e1447e1d02fab08bfaa9160b292
@@ -1,2 +1,50 @@
# graphml-writer
Simple streaming graphML file format writer with little support for yed graphics nodes and edges
The GraphML Writer Library is intended to be used as a sequential writer for the GraphML file format (http://graphml.graphdrawing.org).
This library just writes xml nodes, graphs and edges with necessary namespace initialization ahead.
There is explicitly no graph model, complex layout logic or any other fancy stuff. This library just writes.
Typically this is where you start with:
1. Build a nice graph model with any of the freely available libraries or your own
2. Do some operations on the graph, express your relations etc.
3. Use this library to write a representational graphML file
4. View it in an external tool, like yEd or any other graphML supporting renderer
Furthermore it has some support for writing yEd compatible nodes and edges which are displayable via the great yEd (http://www.yworks.com/products/yed) tool.
# Example Code to render a simple Graph
```java
GraphWriter graphWriter = new GraphWriter(new FileOutputStream("target/simple.graphml"),
StandardCharsets.UTF_8.name());
graphWriter.startDocument();
graphWriter.writeKeys(Arrays.asList(YedKeys.values()));
graphWriter.startGraph(BaseGraph.DIRECTED);
YedNode<ShapeNode> node = new YedNode<>(ShapeNode.asRectangle(0d, 0d, 250d, 30d, "Data"));
// write the node
graphWriter.node(node, graphWriter.getNextId());
// re-set text for next node
node.nodeGraphics.label.text = "Task1";
// adjust y coordinate
node.nodeGraphics.geometry.y = 250d;
// write the next node
graphWriter.node(node, graphWriter.getNextId());
PolyLineEdge
edgeGraphics = new PolyLineEdge();
edgeGraphics.addLabel(EdgeLabel.sourceHead("source head"));
edgeGraphics.addLabel(EdgeLabel.centered("flow"));
edgeGraphics.addLabel(EdgeLabel.targetHead("target"));
YedEdge<PolyLineEdge>
edge = new YedEdge<>(edgeGraphics);
// write the edge, connecting from node "2" to "1"
graphWriter.edge(edge, graphWriter.getNextId(), "2", "1");
graphWriter.endGraph(BaseGraph.DIRECTED);
graphWriter.endDocument();
```
![](https://github.com/bzim/graphml-writer/blob/develop/doc/images/simple.jpg)
[Resulting Graph, viewed in yEd]
BIN +6.19 KB doc/images/simple.jpg
Binary file not shown.
@@ -10,6 +10,7 @@
import de.graphml.writer.model.DefaultKeys;
import de.graphml.writer.model.Edge;
import de.graphml.writer.model.ElementWriter;
import de.graphml.writer.model.Graph;
import de.graphml.writer.model.Key;
import de.graphml.writer.model.Node;
@@ -74,30 +75,18 @@ public void writeKeys(Collection<Key> keys) {
}
}
public String startGraph(boolean defaultDirected) {
String graphId = getNextId();
startGraph(defaultDirected, graphId);
return graphId;
public String startGraph(Graph graph){
String nextId = getNextId();
startGraph(graph, nextId);
return nextId;
}
public void startGraph(boolean defaultDirected, String id) {
elementWriter.startElement("graph");
try {
xtw.writeAttribute("edgedefault", defaultDirected ? "directed" : "undirected");
xtw.writeAttribute("id", id);
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
public void description(String description) {
elementWriter.startData(DefaultKeys.DESCRIPTON_FOR_GRAPH);
elementWriter.writeText(description);
elementWriter.endElement();
public void startGraph(Graph graph, String id) {
graph.writeStart(elementWriter, id);
}
public void endGraph() {
elementWriter.endElement();
public void endGraph(Graph graph) {
graph.writeEnd(elementWriter);
}
public void endDocument() {
@@ -19,7 +19,7 @@ public void writeStart(ElementWriter w, String id, String source, String target)
if (description != null){
w.startData(DefaultKeys.DESCRIPTION_FOR_EDGE);
w.writeText(url);
w.writeText(description);
w.endElement();
}
@@ -16,7 +16,7 @@ public void writeStart(ElementWriter w, String id) {
if (description != null){
w.startData(DefaultKeys.DESCRIPTION_FOR_NODE);
w.writeText(url);
w.writeText(description);
w.endElement();
}
@@ -0,0 +1,38 @@
package de.graphml.writer.model;
public class BaseGraph implements Graph {
public String description;
public boolean defaultDirected;
public static final Graph DIRECTED = new BaseGraph().withDefaultDirected(true);
public static final Graph UNDIRECTED = new BaseGraph();
public BaseGraph(){
}
public BaseGraph withDescription(String description){
this.description = description;
return this;
}
public BaseGraph withDefaultDirected(boolean defaultDirected){
this.defaultDirected = defaultDirected;
return this;
}
@Override
public void writeStart(ElementWriter w, String id) {
w.startElement("graph");
w.writeAttribute("id", id);
w.writeAttribute("edgedefault", defaultDirected ? "directed" : "undirected");
if (description != null){
w.startData(DefaultKeys.DESCRIPTION_FOR_GRAPH);
w.writeText(description);
w.endElement();
}
}
@Override
public void writeEnd(ElementWriter w) {
w.endElement();
}
}
@@ -1,7 +1,7 @@
package de.graphml.writer.model;
public enum DefaultKeys implements Key{
DESCRIPTON_FOR_GRAPH("d0", "graph", "attr.name", "Beschreibung", "attr.type", "string"),
DESCRIPTION_FOR_GRAPH("d0", "graph", "attr.name", "Beschreibung", "attr.type", "string"),
URL_FOR_NODE("d4", "node", "attr.name", "url", "attr.type", "string"),
DESCRIPTION_FOR_NODE("d5", "node", "attr.name", "description", "attr.type", "string"),
URL_FOR_EDGE("d8", "edge", "attr.name", "url", "attr.type", "string"),
@@ -0,0 +1,6 @@
package de.graphml.writer.model;
public interface Graph{
void writeStart(ElementWriter writer, String id);
void writeEnd(ElementWriter writer);
}
@@ -5,6 +5,7 @@
import org.junit.Test;
import de.graphml.writer.GraphWriter;
import de.graphml.writer.model.BaseGraph;
import de.graphml.writer.yed.YedEdge;
import de.graphml.writer.yed.YedGroupNode;
import de.graphml.writer.yed.YedKeys;
@@ -28,14 +29,16 @@ public void writeSimpleGraph() throws Exception {
graphWriter.startDocument();
graphWriter.writeKeys(Arrays.asList(YedKeys.values()));
graphWriter.startGraph(true);
graphWriter.description("This is a description");
graphWriter.startGraph(BaseGraph.DIRECTED);
YedNode<ShapeNode> node = new YedNode<>(ShapeNode.asRectangle(0d, 0d, 250d, 30d, "Data"));
// write the node
graphWriter.node(node, graphWriter.getNextId());
// re-set text for next node
node.nodeGraphics.label.text = "Task1";
// adjust y coordinate
node.nodeGraphics.geometry.y = 250d;
// write the next node
graphWriter.node(node, graphWriter.getNextId());
PolyLineEdge
@@ -46,8 +49,9 @@ public void writeSimpleGraph() throws Exception {
YedEdge<PolyLineEdge>
edge = new YedEdge<>(edgeGraphics);
// write the edge, connecting from node "2" to "1"
graphWriter.edge(edge, graphWriter.getNextId(), "2", "1");
graphWriter.endGraph();
graphWriter.endGraph(BaseGraph.DIRECTED);
graphWriter.endDocument();
TestUtils.formatXMLFile("target/simple.graphml");
@@ -60,7 +64,7 @@ public void writeSimpleGraphRotatingEdgeLabel() throws Exception {
graphWriter.startDocument();
graphWriter.writeKeys(Arrays.asList(YedKeys.values()));
graphWriter.startGraph(true);
graphWriter.startGraph(BaseGraph.DIRECTED);
ShapeNode nodeGraphics = ShapeNode.asRectangle(250d, 30d, "Data");
YedNode<ShapeNode> node = new YedNode<>(nodeGraphics);
@@ -81,6 +85,7 @@ public void writeSimpleGraphRotatingEdgeLabel() throws Exception {
edge = new YedEdge<>(edgeGraphics);
graphWriter.edge(edge, graphWriter.getNextId(), "2", "1");
graphWriter.endGraph(BaseGraph.DIRECTED);
graphWriter.endDocument();
TestUtils.formatXMLFile("target/simple_rotating_edgelabel.graphml");
@@ -95,7 +100,7 @@ public void writeSubGraph() throws Exception {
graphWriter.startDocument();
graphWriter.writeKeys(Arrays.asList(YedKeys.values()));
graphWriter.startGraph(true);
graphWriter.startGraph(BaseGraph.DIRECTED);
ShapeNode nodeGraphics = ShapeNode.asRectangle(250d, 30d, "Data");
YedNode<ShapeNode> node = new YedNode<>(nodeGraphics);
graphWriter.node(node, graphWriter.getNextId());
@@ -107,14 +112,14 @@ public void writeSubGraph() throws Exception {
groupNodeShape.groupNode.label.autoSizePolicy = AutoSizePolicy.NODE_WIDTH;
groupNodeShape.groupNode.label.text = "test";
graphWriter.startNode(graphicsGroupNode, "g1");
graphWriter.startGraph(true);
graphWriter.startGraph(BaseGraph.DIRECTED);
{
nodeGraphics.label.text = "Sub Data 1";
graphWriter.node(node, graphWriter.getNextId());
nodeGraphics.label.text = "Sub Data 2";
graphWriter.node(node, graphWriter.getNextId());
}
graphWriter.endGraph();
graphWriter.endGraph(BaseGraph.DIRECTED);
graphWriter.endNode(graphicsGroupNode);
PolyLineEdge
edgeGraphics = new PolyLineEdge();
@@ -127,7 +132,7 @@ public void writeSubGraph() throws Exception {
graphWriter.edge(edge, graphWriter.getNextId(), "g1", "1");
graphWriter.endGraph();
graphWriter.endGraph(BaseGraph.DIRECTED);
graphWriter.endDocument();
TestUtils.formatXMLFile("target/subgraph.graphml");
@@ -156,7 +161,7 @@ public void writeBigGraph() throws Exception {
graphWriter.startDocument();
graphWriter.writeKeys(Arrays.asList(YedKeys.values()));
graphWriter.startGraph(true);
graphWriter.startGraph(BaseGraph.DIRECTED);
String oldTaskNodeId=null;
@@ -177,6 +182,7 @@ public void writeBigGraph() throws Exception {
}
}
graphWriter.endGraph(BaseGraph.DIRECTED);
graphWriter.endDocument();
TestUtils.formatXMLFile("target/big.graphml");
}

0 comments on commit 0f85be3

Please sign in to comment.