Package com.jgalgo.io

Class 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 using GmlGraphReader the identifiers will be parsed correctly.

    The writer support writing weights of vertices and edges of type WeightsByte, WeightsShort and WeightsInt as integer weights, WeightsFloat, WeightsDouble and WeightsLong as floating numbers weights, WeightsObj as string weights using the Object.toString() method, WeightsChar as string weights, and lastly WeightsBool as integer weights where 0 is false and 1 is true. Note that this way of writing the weights will not yield the exact same graph when reading it using GmlGraphReader, since the weights will be parsed into WeightsInt, WeightsDouble and WeightsObj only. If only WeightsInt, WeightsDouble and WeightsObj of 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, use setVerticesWeightsKeys(Collection) and setEdgesWeightsKeys(Collection).

    The format was presented in a paper 'GML: A portable Graph File Format' by Michael Himsolt.

    Author:
    Barak Ugav
    • Constructor Detail

      • GmlGraphWriter

        public GmlGraphWriter()
        Create a new writer.
    • 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, WeightsShort and WeightsInt as integer weights, WeightsFloat, WeightsDouble and WeightsLong as floating numbers weights, WeightsObj as string weights using the Object.toString() method, WeightsChar as string weights, and lastly WeightsBool as integer weights where 0 is false and 1 is true. Note that this way of writing the weights will not yield the exact same graph when reading it using GmlGraphReader, since the weights will be parsed into WeightsInt, WeightsDouble and WeightsObj only. If only WeightsInt, WeightsDouble and WeightsObj of 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, or null to 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, WeightsShort and WeightsInt as integer weights, WeightsFloat, WeightsDouble and WeightsLong as floating numbers weights, WeightsObj as string weights using the Object.toString() method, WeightsChar as string weights, and lastly WeightsBool as integer weights where 0 is false and 1 is true. Note that this way of writing the weights will not yield the exact same graph when reading it using GmlGraphReader, since the weights will be parsed into WeightsInt, WeightsDouble and WeightsObj only. If only WeightsInt, WeightsDouble and WeightsObj of 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, or null to write all weights (which is the default)
      • writeGraph

        public final void writeGraph​(Graph<V,​E> graph,
                                     Writer writer)
        Description copied from interface: GraphWriter
        Write a graph to an I/O writer.
        Specified by:
        writeGraph in interface GraphWriter<V,​E>
        Parameters:
        graph - a graph
        writer - an I/O writer to which the graph description will be written to