Package com.jgalgo.io

Class GexfGraphReader<V,​E>

  • All Implemented Interfaces:
    GraphReader<V,​E>

    public class GexfGraphReader<V,​E>
    extends Object
    Read a graph in 'GEXF' format.

    GEXF is an XML-based format for graphs. Both directed and undirected graphs are supported, along with mixed graphs (some edges are directed while others are undirected) although the Graph does not supported mixed graphs (therefore the reader also doesn't support it). The format support graphs with vertices and edges of any type, as long as they can be written as an XML attribute string. The format also support multiple weights for vertices and edges, of any primitive Java type (int, long, double, boolean, ect.), String, Date, URI, BigInteger, BigDecimal, along with 'list' (arrays) of any of the above except Date and URI.

    Identifiers of vertices are mandatory, and must be unique. Identifiers of edges are optional, if not specified the reader will try generate them using a builder provided by the user, or a default builder for certain types (see setEdgeBuilder(IdBuilder) and setEdgeBuilderDefault(Class)). Vertices identifiers (and edges identifiers if specified) are parsed using a parser provided by the user, or a default parser for certain types (see setVertexParserDefault(Class) and setEdgeParserDefault(Class)).

    When the reader reads a graph with weights, it will create a Weights object for each type of weights. For any of the primitive types such as int, long, double, boolean, ect., the reader will create a Weights object of the corresponding type, such as WeightsInt, WeightsLong, WeightsDouble, WeightsBool, ect. For String, Date, URI, BigInteger, BigDecimal, the reader will create a Weights object of type WeightsObj. For 'list' types, which are supported for any primitive, String, BigInteger and BigDecimal, the reader will create a Weights object of type WeightsObj and will populate it with arrays of the corresponding type, such as int[], long[], String[], BigInteger[], ect. Default values are supported for all types of weights, and will be available after reading via Weights.defaultWeightAsObj() or any of the specific types weights such as WeightsInt.defaultWeight(). Note that the default value of 'list' types is an array, which is shared between all vertices/edges that do not explicitly specify a value for the weight, and should not be modified.

    The GEXF format support both self edges and parallel edges. The format documentation can be found here.

    Author:
    Barak Ugav
    See Also:
    GexfGraphWriter
    • Constructor Detail

      • GexfGraphReader

        public GexfGraphReader()
        Create a new reader.

        The user should set the vertex/edge parsers and edge builder manually using setVertexParser(Function), setEdgeParser(Function) and setEdgeBuilder(IdBuilder). Setting the vertex parser is mandatory, while setting the edge parser is only required if edges identifiers are specified. Similarly, setting the edge builder is only required if edges identifiers are not specified.

      • GexfGraphReader

        public GexfGraphReader​(Class<V> vertexType,
                               Class<E> edgeType)
        Create a new reader with default parsers and builders for the given vertex and edge types.

        During the reading process, the reader will use the parser to convert the vertex identifiers from string to the given type, and similarly for edges if edges identifiers are specified. If edges identifiers are not specified, the reader will use the builder to generate them. Default parsers exist for types byte, short, int, long, float, double and String. Default edge builder is instantiated using IdBuilder.defaultBuilder(Class), see it documentation for supported types. If the given types are not supported by the default parsers and builder, the reader will throw an exception. In such case, the constructor GexfGraphReader() should be used, and the user should set the vertex/edge parsers and edge builder manually using setVertexParser(Function), setEdgeParser(Function) and setEdgeBuilder(IdBuilder).

        Parameters:
        vertexType - the type of the vertices
        edgeType - the type of the edges
        Throws:
        IllegalArgumentException - if the given types are not supported by the default vertex/edge parsers and edge builder. The supported types are byte, short, int, long, float, double and String.
        See Also:
        setVertexParserDefault(Class), setEdgeParserDefault(Class), setEdgeBuilderDefault(Class)
    • Method Detail

      • setVertexParser

        public void setVertexParser​(Function<String,​V> vertexParser)
        Set the parser for the vertices identifiers.

        The parser is used to convert the vertex identifiers from string to the given vertex type. The parser is mandatory, and must be set before reading a graph. For default parsers for certain types, see setVertexParserDefault(Class).

        Parameters:
        vertexParser - a parser for the vertices identifiers
      • setVertexParserDefault

        public void setVertexParserDefault​(Class<V> vertexType)
        Set the parser for the vertices identifiers, using a default parser for the given vertex type.

        The parser is used to convert the vertex identifiers from string to the given vertex type. The parser is mandatory, and must be set before reading a graph. The default parser exists for types byte, short, int, long, float, double and String. If the given type is not supported by the default parser, the reader will throw an exception. In such case, the method setVertexParser(Function) should be used for custom parsing.

        Parameters:
        vertexType - the type of the vertices
        Throws:
        IllegalArgumentException - if the given type is not supported by the default parser. The supported types are byte, short, int, long, float, double and String.
      • setEdgeParser

        public void setEdgeParser​(Function<String,​E> edgeParser)
        Set the parser for the edges identifiers.

        The parser is used to convert the edges identifiers from string to the given edge type. The parser is mandatory if edges identifiers are specified. In case edge identifiers are not specified, an edge builder must be set (see setEdgeBuilder(IdBuilder)). For default parsers for certain types, see setEdgeParserDefault(Class).

        Parameters:
        edgeParser - a parser for the edges identifiers
      • setEdgeParserDefault

        public void setEdgeParserDefault​(Class<E> edgeType)
        Set the parser for the edges identifiers, using a default parser for the given edge type.

        The parser is used to convert the edges identifiers from string to the given edge type. The parser is mandatory if edges identifiers are specified. In case edge identifiers are not specified, an edge builder must be set (see setEdgeBuilder(IdBuilder)). The default parser exists for types byte, short, int, long, float, double and String. If the given type is not supported by the default parser, the reader will throw an exception. In such case, the method setEdgeParser(Function) should be used for custom parsing.

        Parameters:
        edgeType - the type of the edges
        Throws:
        IllegalArgumentException - if the given type is not supported by the default parser. The supported types are byte, short, int, long, float, double and String.
      • setEdgeBuilder

        public void setEdgeBuilder​(IdBuilder<E> edgeBuilder)
        Set the builder for the edges identifiers.

        The builder is used to generate edges identifiers if edges identifiers are not specified. The builder is mandatory if edges identifiers are not specified. In case edge identifiers are specified, an edge parser must be set (see setEdgeParser(Function)). For default builders for certain types, see setEdgeBuilderDefault(Class).

        The edge builder accepts a set of existing edges, and should return a new edge identifier that is not in the set.

        Parameters:
        edgeBuilder - a builder for the edges identifiers
      • setEdgeBuilderDefault

        public void setEdgeBuilderDefault​(Class<E> edgeType)
        Set the builder for the edges identifiers, using a default builder for the given edge type.

        The builder is used to generate edges identifiers if edges identifiers are not specified. The builder is mandatory if edges identifiers are not specified. In case edge identifiers are specified, an edge parser must be set (see setEdgeParser(Function)). The default builder is instantiated using IdBuilder.defaultBuilder(Class), see it documentation for supported types. If the given type is not supported by the default builder, the reader will throw an exception. In such case, the method setEdgeBuilder(IdBuilder) should be used for custom builder.

        Parameters:
        edgeType - the type of the edges
        Throws:
        IllegalArgumentException - if the given type is not supported by the default builder. See IdBuilder.defaultBuilder(Class) for supported types
      • 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