Class GraphMLImporter<V,​E>

  • Type Parameters:
    V - the graph vertex type
    E - the graph edge type
    All Implemented Interfaces:

    public class GraphMLImporter<V,​E>
    extends BaseEventDrivenImporter<V,​E>
    implements GraphImporter<V,​E>
    Imports a graph from a GraphML data source.

    For a description of the format see GraphML or the GraphML Primer.

    Below is small example of a graph in GraphML format.

     <?xml version="1.0" encoding="UTF-8"?>
     <graphml xmlns=""  
         xsi:schemaLocation=" ">
       <key id="d0" for="node""color" attr.type="string">
       <key id="d1" for="edge""weight" attr.type="double"/>
       <graph id="G" edgedefault="undirected">
         <node id="n0">
           <data key="d0">green</data>
         <node id="n1"/>
         <node id="n2">
           <data key="d0">blue</data>
         <node id="n3">
           <data key="d0">red</data>
         <node id="n4"/>
         <node id="n5">
           <data key="d0">turquoise</data>
         <edge id="e0" source="n0" target="n2">
           <data key="d1">1.0</data>
         <edge id="e1" source="n0" target="n1">
           <data key="d1">1.0</data>
         <edge id="e2" source="n1" target="n3">
           <data key="d1">2.0</data>
         <edge id="e3" source="n3" target="n2"/>
         <edge id="e4" source="n2" target="n4"/>
         <edge id="e5" source="n3" target="n5"/>
         <edge id="e6" source="n5" target="n4">
           <data key="d1">1.1</data>

    The importer reads the input into a graph which is provided by the user. In case the graph is weighted and the corresponding edge key with"weight" is defined, the importer also reads edge weights. Otherwise edge weights are ignored. To test whether the graph is weighted, method Graph.getType() can be used.

    GraphML-Attributes Values are read as string key-value pairs and passed on to the vertex or edge attribute consumers respectively.

    The provided graph object, where the imported graph will be stored, must be able to support the features of the graph that is read. For example if the GraphML file contains self-loops then the graph provided must also support self-loops. The same for multiple edges. Moreover, the parser completely ignores the attribute "edgedefault" which denotes whether an edge is directed or not. Whether edges are directed or not depends on the underlying implementation of the user provided graph object.

    The importer by default validates the input using the 1.0 GraphML Schema. The user can (not recommended) disable the validation by calling setSchemaValidation(boolean).

    The graph vertices and edges are build using the corresponding graph suppliers. The id of the vertices in the original dot file are reported as a vertex attribute named "ID". Thus, in case vertices in the dot file also contain an "ID" attribute, such an attribute will be reported multiple times.

    The default behavior of the importer is to use the graph vertex supplier in order to create vertices. The user can also bypass vertex creation by providing a custom vertex factory method using setVertexFactory(Function). The factory method is responsible to create a new graph vertex given the vertex identifier read from file.

    Dimitrios Michail
    • Field Detail


        public static final java.lang.String DEFAULT_VERTEX_ID_KEY
        Default key used for vertex ID.
        See Also:
        Constant Field Values
    • Constructor Detail

      • GraphMLImporter

        public GraphMLImporter()
        Constructs a new importer.
    • Method Detail

      • getEdgeWeightAttributeName

        public java.lang.String getEdgeWeightAttributeName()
        Get the attribute name for edge weights
        the attribute name
      • setEdgeWeightAttributeName

        public void setEdgeWeightAttributeName​(java.lang.String edgeWeightAttributeName)
        Set the attribute name to use for edge weights.
        edgeWeightAttributeName - the attribute name
      • isSchemaValidation

        public boolean isSchemaValidation()
        Whether the importer validates the input
        true if the importer validates the input
      • setSchemaValidation

        public void setSchemaValidation​(boolean schemaValidation)
        Set whether the importer should validate the input
        schemaValidation - value for schema validation
      • getVertexFactory

        public java.util.function.Function<java.lang.String,​V> getVertexFactory()
        Get the user custom vertex factory. This is null by default and the graph supplier is used instead.
        the user custom vertex factory
      • setVertexFactory

        public void setVertexFactory​(java.util.function.Function<java.lang.String,​V> vertexFactory)
        Set the user custom vertex factory. The default behavior is being null in which case the graph vertex supplier is used. If supplied the vertex factory is called every time a new vertex is encountered in the file. The method is called with parameter the vertex identifier from the file and should return the actual graph vertex to add to the graph.
        vertexFactory - a vertex factory
      • importGraph

        public void importGraph​(Graph<V,​E> graph,
        Import a graph.

        The provided graph must be able to support the features of the graph that is read. For example if the GraphML file contains self-loops then the graph provided must also support self-loops. The same for multiple edges.

        If the provided graph is a weighted graph, the importer also reads edge weights.

        GraphML-Attributes Values are read as string key-value pairs and propagated to the user as events.

        Specified by:
        importGraph in interface GraphImporter<V,​E>
        graph - the output graph
        input - the input reader
        ImportException - in case an error occurs, such as I/O or parse error