Package com.jgalgo.io

Class GmlGraphReader<V,​E>

  • Type Parameters:
    V - the vertices type
    E - the edges type
    All Implemented Interfaces:
    GraphReader<V,​E>

    public final class GmlGraphReader<V,​E>
    extends Object
    Read 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 node has a property 'id', which is used as identifier in the built graph. Edges are not required to have an 'id' property by the format, and we try to choose valid ids if they are missing, but its recommended to always provide an 'id' property for edges, which will be used as the edge identifier in the built graph. In addition to 'id', each edge node should have a property 'source' and 'target', specifying the endpoints vertices of the edge. Except for the 'id' property for vertices, and 'id','source' and 'target' for edges, all other properties are added as weights to the graph. An example of a GML file:

     graph [
            # This is a comment about this sample graph
            comment "This is another comment about this 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 GML format supports comments in the form of a node with the key 'comment', or lines that starts with '#'. Both are completely ignored by the reader.

    The GML reader must know the type of vertices and edges during runtime in order to safely read the vertices and edges into a graph. The types can be set using setVertexType(Class) and setEdgeType(Class), or passed in the constructor. The supported types are integers, doubles and strings. If both the vertices and edges types are int.class, an IntGraph will be built.

    The weights of the vertices will added to the built graph, and will be accessed later by the Graph.verticesWeights(String) method, and similarly for the edges weights. GML supports integers, doubles and strings as weights, and the weights will be added to the graph using WeightsInt, WeightsDouble or WeightsObj. If the vertices (edges) contains weights with the same key but different type, the weights will be added to the graph using a 'supertype' of the weights type. For example, if the vertices contains weights with key 'weight' of type int.class and double.class, the weights will be added to the graph using WeightsDouble. If the vertices contains weights with key 'weight' of type int.class and String.class, the weights will be added to the graph using WeightsObj. If a vertex do not contain a weight with a certain key but others do, the vertex will be assigned the default value of the weights type. All these rules apply also for the edges weights.

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

    Author:
    Barak Ugav
    • Constructor Detail

      • GmlGraphReader

        public GmlGraphReader()
        Create a new reader.

        Before any graph can be parsed, the vertices and edges types must be set using setVertexType(Class) and setEdgeType(Class). Alternatively, the types can be passed in the constructor.

      • GmlGraphReader

        public GmlGraphReader​(Class<V> vType,
                              Class<E> eType)
        Create a new reader and provide the types of vertices and edges.
        Parameters:
        vType - the type of vertices
        eType - the type of edges
        See Also:
        setVertexType(Class), setEdgeType(Class)
    • Method Detail

      • setVertexType

        public void setVertexType​(Class<V> vType)
        Set the type of vertices.

        The GML reader must know the type of vertices during runtime in order to safely read the vertices into a graph. This method must be called before any graph can be built, unless the vertices type was passed in the constructor.

        The supported types are integers, doubles and strings. If both the vertices and edges types are int.class, an IntGraph will be built.

        Parameters:
        vType - the type of vertices
      • setEdgeType

        public void setEdgeType​(Class<E> eType)
        Set the type of edges.

        The GML reader must know the type of edges during runtime in order to safely read the edges into a graph. This method must be called before any graph can be built, unless the edges type was passed in the constructor.

        The supported types are integers, doubles and strings. If both the vertices and edges types are int.class, an IntGraph will be built.

        Parameters:
        eType - the type of edges
      • readIntoBuilder

        public GraphBuilder<V,​E> readIntoBuilder​(Reader reader)
        Description copied from interface: GraphReader
        Read a graph from an I/O reader into a GraphBuilder.
        Specified by:
        readIntoBuilder in interface GraphReader<V,​E>
        Parameters:
        reader - an I/O reader that contain a graph description
        Returns:
        a graph builder containing the vertices and edge read from the reader