Class GmlGraphReader<V,E>
- java.lang.Object
-
- com.jgalgo.io.GmlGraphReader<V,E>
-
- Type Parameters:
V
- the vertices typeE
- the edges type
- All Implemented Interfaces:
GraphReader<V,E>
public 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)
andsetEdgeType(Class)
, or passed in the constructor. The supported types are integers, doubles and strings. If both the vertices and edges types areint.class
, anIntGraph
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 usingWeightsInt
,WeightsDouble
orWeightsObj
. 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 typeint.class
anddouble.class
, the weights will be added to the graph usingWeightsDouble
. If the vertices contains weights with key 'weight' of typeint.class
andString.class
, the weights will be added to the graph usingWeightsObj
. 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 Summary
Constructors Constructor Description GmlGraphReader()
Create a new reader.GmlGraphReader(Class<V> vType, Class<E> eType)
Create a new reader and provide the types of vertices and edges.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GraphBuilder<V,E>
readIntoBuilder(Reader reader)
Read a graph from an I/O reader into aGraphBuilder
.void
setEdgeType(Class<E> eType)
Set the type of edges.void
setVertexType(Class<V> vType)
Set the type of vertices.-
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.GraphReader
readGraph, readGraph, readGraph
-
-
-
-
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)
andsetEdgeType(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 verticeseType
- 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
, anIntGraph
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
, anIntGraph
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 aGraphBuilder
.- Specified by:
readIntoBuilder
in interfaceGraphReader<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
-
-