graph_analysis
Public Types | Static Public Member Functions | List of all members
graph_analysis::io::GraphIO Class Reference

Main class for handling I/O for graphs. More...

#include <GraphIO.hpp>

Public Types

typedef std::map< representation::Type, Writer::Ptr > WriterMap
 
typedef std::map< representation::Type, Reader::Ptr > ReaderMap
 
typedef std::map< representation::Suffix, representation::Type > SuffixMap
 

Static Public Member Functions

static std::string write (const std::string &filename, const BaseGraph::Ptr &graph, representation::Type format=representation::UNKNOWN)
 
static std::string write (const std::string &filename, const BaseGraph &graph, representation::Type format=representation::UNKNOWN)
 
static void read (const std::string &filename, BaseGraph::Ptr graph, representation::Type format=representation::UNKNOWN)
 
static void read (const std::string &filename, BaseGraph &graph, representation::Type format=representation::UNKNOWN)
 
static WriterMap getWriterMap ()
 
static ReaderMap getReaderMap ()
 
static SuffixMap getSuffixMap ()
 
static representation::Type getTypeFromSuffix (representation::Suffix suffix)
 
static representation::Type getTypeFromFilename (const std::string &filename)
 
static std::string appendSuffix (const std::string &filename, representation::Type format)
 
static representation::Suffix getSuffix (representation::Type format)
 

Detailed Description

Main class for handling I/O for graphs.

The write/read functions try to infer the graph format from the filename suffix if no format is specified.

Example:

 #include"GraphIO.hpp"
 ...
 BaseGraph::Ptr graph = BaseGraph::getInstance();
 ...
 io::GraphIO::write("test-file.gexf", graph);
 io::GraphIO::write("test-file.dot", graph);

In order to allow custom vertices and edges to be serialized and deserialized you need to properly overload the getClassName and getClone methods (as you should always do)

 class MyVertex : Vertex
 {
 public:
    virtual std::string getClassName() const { return "MyVertex"; }

 protected:
    virtual Vertex* getClone() { return new MyVertex(*this); }
 };

 class MyDerivedVertex : MyVertex
 {
 public:
    virtual std::string getClassName() const { return "MyDerivedVertex"; }

 protected:
    virtual Vertex* getClone() { return new MyVertex(*this); }
 };

 class MyEdge : Edge
 {
 public:
    virtual std::string getClassName() const { return "MyEdge"; }
 protected:
    virtual Edge* getClone() { return new MyEdge(*this); }
 };

Register your custom vertex and edge types, e.g., at the beginning of your program or in the constructor of a custom graph type that used the edges and vertices

TODO: improve registration using singleton like initialization

 try {
     VertexTypeManager* vertexTypeManager = VertexTypeManager::getInstance();
     vertexTypeManager->registerType("MyVertex", make_shared<MyVertex>(), true);

     VertexTypeManager* vertexTypeManager = VertexTypeManager::getInstance();
     vertexTypeManager->registerType("MyVertex", make_shared<MyVertex>(),
        { make_shared<MyDerivedVertex>() }, true);

     EdgeTypeManager* edgeTypeManager = EdgeTypeManager::getInstance();
     edgeTypeManager->registerType("MyEdge", make_shared<MyEdge>(), true);
 } catch(...)
 {
     // already registered
 }

Eventually, the reader has to support/take care of proper edge construction and vertex construction, e.g., in GexfReader

    libgexf::t_id current = edge_it->next();
    std::string edgeClass = data.getEdgeAttribute(current, classAttr);
    std::string edgeLabel = data.getEdgeAttribute(current, labelAttr);
    Vertex::Ptr sourceVertex = vertexMap[edge_it->currentSource()]; // NOTE: assumes the .gexf(.xml) file is valid
    Vertex::Ptr targetVertex = vertexMap[edge_it->currentTarget()]; // NOTE: assumes the .gexf(.xml) file is valid
    Edge::Ptr edge = EdgeTypeManager::getInstance()->createEdge(edgeClass, sourceVertex, targetVertex, edgeLabel);
    graph->addEdge(edge);

To fully support (de)serialization of a graph, edges and vertices have to be store custom attribute data – apart from the default 'class' and 'label' attributes. For that purpose a general mechanism exists in order to implement a custom serialization:

class DerivedVertex : public graph_analysis::Vertex
{
public:
    DerivedVertex(std::string name)
        : graph_analysis::Vertex(name)
    {}

    std::string serializeMember0() { return mMember0; }
    std::string serializeMember1() { return mMember1; }

    void deserializeMember0(const std::string& s) { mMember0= s; }
    void deserializeMember1(const std::string& s) { mMember1 = s; }

    virtual std::string getClassName() const{ return "DerivedVertex"; }

    std::string mMember0;
    std::string mMember1;
protected:
    virtual graph_analysis::Vertex* getClone() const { return new DerivedVertex("CLONE"); }

};

class DerivedEdge : public graph_analysis::Edge
{
public:
    DerivedEdge(std::string name)
        : graph_analysis::Edge(name)
    {}

    std::string serializeMember0() { return mMember0; }
    std::string serializeMember1() { return mMember1; }

    void deserializeMember0(const std::string& s) { mMember0= s; }
    void deserializeMember1(const std::string& s) { mMember1 = s; }

    virtual std::string getClassName() const{ return "DerivedEdge"; }

    std::string mMember0;
    std::string mMember1;
protected:
    virtual graph_analysis::Edge* getClone() const { return new DerivedEdge("CLONE"); }

};

    Vertex::Ptr empty( new DerivedVertex("derived-vertex"));
    VertexTypeManager *vManager = VertexTypeManager::getInstance();

    vManager->registerAttribute(empty->getClassName(), "m0",
            (io::AttributeSerializationCallbacks::serialize_func_t)&DerivedVertex::serializeMember0,
            (io::AttributeSerializationCallbacks::deserialize_func_t)&DerivedVertex::deserializeMember0,
            (io::AttributeSerializationCallbacks::print_func_t)&DerivedVertex::serializeMember0);

    vManager->registerAttribute(empty->getClassName(), "m1",
            (io::AttributeSerializationCallbacks::serialize_func_t)&DerivedVertex::serializeMember1,
            (io::AttributeSerializationCallbacks::deserialize_func_t)&DerivedVertex::deserializeMember1,
            (io::AttributeSerializationCallbacks::print_func_t) &DerivedVertex::serializeMember1);

    Edge::Ptr e0(new DerivedEdge("derived-edge"));
    EdgeTypeManager* eManager = EdgeTypeManager::getInstance();
    eManager->registerType(e0);
    eManager->registerAttribute(e0->getClassName(), "a0",
            (io::AttributeSerializationCallbacks::serialize_func_t) &DerivedEdge::serializeMember0,
            (io::AttributeSerializationCallbacks::deserialize_func_t)&DerivedEdge::deserializeMember0,
            (io::AttributeSerializationCallbacks::print_func_t)&DerivedEdge::serializeMember0);

    eManager->registerAttribute(e0->getClassName(), "a1",
            (io::AttributeSerializationCallbacks::serialize_func_t) &DerivedEdge::serializeMember1,
            (io::AttributeSerializationCallbacks::deserialize_func_t)&DerivedEdge::deserializeMember1,
            (io::AttributeSerializationCallbacks::print_func_t)&DerivedEdge::serializeMember1);
...

    io::GraphIO::write(filename, graph, representation::GEXF);
    ...
    BaseGraph::Ptr read_graph = BaseGraph::getInstance();
    io::GraphIO::read(filename, read_graph, representation::GEXF);

Member Typedef Documentation

◆ ReaderMap

◆ SuffixMap

◆ WriterMap

Member Function Documentation

◆ appendSuffix()

std::string graph_analysis::io::GraphIO::appendSuffix ( const std::string &  filename,
representation::Type  format 
)
static

◆ getReaderMap()

static ReaderMap graph_analysis::io::GraphIO::getReaderMap ( )
inlinestatic

◆ getSuffix()

representation::Suffix graph_analysis::io::GraphIO::getSuffix ( representation::Type  format)
static

◆ getSuffixMap()

static SuffixMap graph_analysis::io::GraphIO::getSuffixMap ( )
inlinestatic

◆ getTypeFromFilename()

representation::Type graph_analysis::io::GraphIO::getTypeFromFilename ( const std::string &  filename)
static

◆ getTypeFromSuffix()

representation::Type graph_analysis::io::GraphIO::getTypeFromSuffix ( representation::Suffix  suffix)
static

◆ getWriterMap()

static WriterMap graph_analysis::io::GraphIO::getWriterMap ( )
inlinestatic

◆ read() [1/2]

void graph_analysis::io::GraphIO::read ( const std::string &  filename,
BaseGraph::Ptr  graph,
representation::Type  format = representation::UNKNOWN 
)
static

◆ read() [2/2]

void graph_analysis::io::GraphIO::read ( const std::string &  filename,
BaseGraph &  graph,
representation::Type  format = representation::UNKNOWN 
)
static

◆ write() [1/2]

std::string graph_analysis::io::GraphIO::write ( const std::string &  filename,
const BaseGraph::Ptr &  graph,
representation::Type  format = representation::UNKNOWN 
)
static

◆ write() [2/2]

std::string graph_analysis::io::GraphIO::write ( const std::string &  filename,
const BaseGraph &  graph,
representation::Type  format = representation::UNKNOWN 
)
static

The documentation for this class was generated from the following files: