0

How would I output the following graph into graphml?

typedef struct Vertex{ std::string name; std::string cmdb_id;

             Vertex& operator= (const Vertex& rhs)
             {
                     if (this == &rhs)
                             return *this;

                     name = rhs.name;
                     cmdb_id = rhs.cmdb_id;
             }

             bool operator< (const Vertex& rhs) const
             {
                     return cmdb_id < rhs.cmdb_id;
             };

             bool operator== (const Vertex& rhs) const
             {
                     return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name));
             };

     }vertex_container;

typedef struct Edge {std::string name;} edge_container;

boost::directed_graph<vertex_container, edge_container> Graph g;
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
bayerb
  • 649
  • 2
  • 9
  • 28

2 Answers2

2

Here is a version of the other answer that doesn't use c++11 features:

#include <iostream>

#include <boost/graph/directed_graph.hpp>
#include <boost/graph/graphml.hpp>



typedef struct Vertex
{ 
   std::string name; 
   std::string cmdb_id;

   Vertex& operator= (const Vertex& rhs)
   {
      if (this == &rhs)
        return *this;

      name = rhs.name;
      cmdb_id = rhs.cmdb_id;
   }

   bool operator< (const Vertex& rhs) const
   {
      return cmdb_id < rhs.cmdb_id;
   };

   bool operator== (const Vertex& rhs) const
   {
      return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name));
   };

}vertex_container;

typedef struct Edge {std::string name;} edge_container;

typedef boost::directed_graph<vertex_container, edge_container> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;

int main()
{
   Graph g;
   vertex_container A, B;
   edge_container AB;

   A.name="A";
   A.cmdb_id="1";
   B.name="B";
   B.cmdb_id="2";
   AB.name="A-B";

   vertex_descriptor v0 = g.add_vertex(A);
   vertex_descriptor v1 = g.add_vertex(B);
   g.add_edge(v0,v1,AB);

   boost::dynamic_properties dp;
   dp.property("vertex_name",get(&vertex_container::name,g));
   dp.property("vertex_cmdb_id",get(&vertex_container::cmdb_id,g));
   dp.property("edge_name",get(&edge_container::name,g));

   write_graphml(std::cout, g, dp);

}

Working on g++ 4.6.3 on LWS.

  • I don't get it. It doesn't compile on my environment, except I delete the write_graphml... line. Boost version is 1.46.1. – bayerb Apr 13 '13 at 09:46
  • clang shows me following error for the write_graphml line: `/usr/include/boost/graph/directed_graph.hpp:95:11: error: no matching constructor for initialization of 'graph_type' (aka 'adjacency_list') : m_graph(x), m_num_vertices(x.m_num_vertices), m_num_edges(x.m_num_edges) ^ ~ ` – bayerb Apr 15 '13 at 08:41
  • On boost 1.51.0 everything is fine. – bayerb Apr 15 '13 at 09:27
0
#include <iostream>

#include <boost/graph/directed_graph.hpp>
#include <boost/graph/graphml.hpp>



typedef struct Vertex
{ 
   std::string name; 
   std::string cmdb_id;

   Vertex& operator= (const Vertex& rhs)
   {
      if (this == &rhs)
        return *this;

      name = rhs.name;
      cmdb_id = rhs.cmdb_id;
   }

   bool operator< (const Vertex& rhs) const
   {
      return cmdb_id < rhs.cmdb_id;
   };

   bool operator== (const Vertex& rhs) const
   {
      return ((cmdb_id == rhs.cmdb_id) && (name == rhs.name));
   };

}vertex_container;

typedef struct Edge {std::string name;} edge_container;

typedef boost::directed_graph<vertex_container, edge_container> Graph;

int main()
{
   Graph g;
   auto v0 = g.add_vertex({"A","1"});
   auto v1 = g.add_vertex({"B","2"});
   g.add_edge(v0,v1,{"A-B"});

   boost::dynamic_properties dp;
   dp.property("vertex_name",get(&vertex_container::name,g));
   dp.property("vertex_cmdb_id",get(&vertex_container::cmdb_id,g));
   dp.property("edge_name",get(&edge_container::name,g));

   write_graphml(std::cout, g, dp);

}
  • 1
    If I compile your sample, I get a long compiler error. I would not paste it into here, its too long. I use gcc 4.6.3 btw. – bayerb Apr 12 '13 at 10:26