Class GmlGraphWriter<V,E>
- java.lang.Object
-
- com.jgalgo.io.GmlGraphWriter<V,E>
-
- All Implemented Interfaces:
GraphWriter<V,E>
public class GmlGraphWriter<V,E> extends Object
Write a graph in 'GML' format.The GML format is a simple text format for describing graphs. It can represent directed and undirected graphs, and supports integers, floats and strings as vertices/edges identifiers and weights. The format is described in Wikipedia. The format uses a tree-like structure, similar to JSON or XML. The root of the tree is a node with the key "graph", and its children are the vertices, edges, and additional properties such as whether the graph is directed or not. Each vertex and edge will have a property 'id', which is the identifier in the written graph. The source and target of each edge will be stored as properties of an edge node. Except for the 'id' property for vertices, and 'id','source' and 'target' for edges, the weights of each vertex/edge will be stored as properties of the vertex/edge node. An example of a GML file:
graph [ # "This is a sample graph" directed 1 node [ id 1 label "node 1" thisIsASampleAttribute 42 ] node [ id 2 label "node 2" thisIsASampleAttribute 43 ] node [ id 3 label "node 3" thisIsASampleAttribute 44 ] edge [ id 1 source 1 target 2 label "Edge from node 1 to node 2" weight 22.7 ] edge [ id 2 source 2 target 3 label "Edge from node 2 to node 3" weight 1.5 ] edge [ id 3 source 3 target 1 label "Edge from node 3 to node 1" weight -13.54 ] ]The identifiers of a graph will be written using the
Object.toString()method. The vertices and edges should be either integers, floats, or strings so that when reading the graph usingGmlGraphReaderthe identifiers will be parsed correctly.The writer support writing weights of vertices and edges of type
WeightsByte,WeightsShortandWeightsIntas integer weights,WeightsFloat,WeightsDoubleandWeightsLongas floating numbers weights,WeightsObjas string weights using theObject.toString()method,WeightsCharas string weights, and lastlyWeightsBoolas integer weights where0isfalseand1istrue. Note that this way of writing the weights will not yield the exact same graph when reading it usingGmlGraphReader, since the weights will be parsed intoWeightsInt,WeightsDoubleandWeightsObjonly. If onlyWeightsInt,WeightsDoubleandWeightsObjof strings are written, the read graph will be identical. The weights will be written as properties of the vertex/edge node, and the key of the property will be the key of the weights. The weights keys should be valid GML keys. By default, all weights of the vertices/edges will be written. To write only specific weights, usesetVerticesWeightsKeys(Collection)andsetEdgesWeightsKeys(Collection).The format was presented in a paper 'GML: A portable Graph File Format' by Michael Himsolt.
- Author:
- Barak Ugav
-
-
Constructor Summary
Constructors Constructor Description GmlGraphWriter()Create a new writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidsetEdgesWeightsKeys(Collection<String> edgesWeightsKeys)Set the weights keys of the edges to write.voidsetVerticesWeightsKeys(Collection<String> verticesWeightsKeys)Set the weights keys of the vertices to write.voidwriteGraph(Graph<V,E> graph, Writer writer)Write a graph to an I/O writer.voidwriteGraphImpl(Graph<V,E> graph, Writer writer)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.jgalgo.io.GraphWriter
writeGraph, writeGraph
-
-
-
-
Method Detail
-
setVerticesWeightsKeys
public void setVerticesWeightsKeys(Collection<String> verticesWeightsKeys)
Set the weights keys of the vertices to write.By default, all weights of the vertices will be written. The writer support writing weights of type
WeightsByte,WeightsShortandWeightsIntas integer weights,WeightsFloat,WeightsDoubleandWeightsLongas floating numbers weights,WeightsObjas string weights using theObject.toString()method,WeightsCharas string weights, and lastlyWeightsBoolas integer weights where0isfalseand1istrue. Note that this way of writing the weights will not yield the exact same graph when reading it usingGmlGraphReader, since the weights will be parsed intoWeightsInt,WeightsDoubleandWeightsObjonly. If onlyWeightsInt,WeightsDoubleandWeightsObjof strings are written, the read graph will be identical. The weights will be written as properties of the vertex node, and the key of the property will be the key of the weights. The weights keys should be valid GML keys.- Parameters:
verticesWeightsKeys- The weights keys of the vertices to write, ornullto write all weights (which is the default)
-
setEdgesWeightsKeys
public void setEdgesWeightsKeys(Collection<String> edgesWeightsKeys)
Set the weights keys of the edges to write.By default, all weights of the edges will be written. The writer support writing weights of type
WeightsByte,WeightsShortandWeightsIntas integer weights,WeightsFloat,WeightsDoubleandWeightsLongas floating numbers weights,WeightsObjas string weights using theObject.toString()method,WeightsCharas string weights, and lastlyWeightsBoolas integer weights where0isfalseand1istrue. Note that this way of writing the weights will not yield the exact same graph when reading it usingGmlGraphReader, since the weights will be parsed intoWeightsInt,WeightsDoubleandWeightsObjonly. If onlyWeightsInt,WeightsDoubleandWeightsObjof strings are written, the read graph will be identical. The weights will be written as properties of the edge node, and the key of the property will be the key of the weights. The weights keys should be valid GML keys.- Parameters:
edgesWeightsKeys- The weights keys of the edges to write, ornullto write all weights (which is the default)
-
writeGraphImpl
public void writeGraphImpl(Graph<V,E> graph, Writer writer) throws IOException
- Throws:
IOException
-
writeGraph
public final void writeGraph(Graph<V,E> graph, Writer writer)
Description copied from interface:GraphWriterWrite a graph to an I/O writer.- Specified by:
writeGraphin interfaceGraphWriter<V,E>- Parameters:
graph- a graphwriter- an I/O writer to which the graph description will be written to
-
-