1

I have a adjacency list of type

boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, GraphData>

Where GraphData is a structure contains name

struct GraphItem
{
    std::string Name;
}

I am able to write graph to xml

void WriteGraph() {
     boost::dynamic_properties dp;

     dp.property("Name", make_transform_value_property_map(&Name, 
     boost::get(vertex_bundle, graph)));

     boost::write_graphml(filename, graph, dp, true); 
}

std::string Name(boost::vertex_bundle_type<Graph>::type v) {
    std::ostringstream oss;
    oss << v.Name;
    return oss.str();
}

I get XML as

<graphml>
  <key id="key0" for="node" attr.name="Name" attr.type="string" />
  <graph id="G" edgedefault="directed" parse.nodeids="canonical" 
   parse.edgeids="canonical" parse.order="nodesfirst">
    <node id="n0">
      <data key="key0">A</data>
    </node>
    <node id="n1">
      <data key="key0">D</data>
    </node>
    <node id="n2">
      <data key="key0">B</data>
    </node>
    <node id="n3">
      <data key="key0">C</data>
    </node>
    <edge id="e0" source="n0" target="n1">
    </edge>
    <edge id="e1" source="n2" target="n3">
    </edge>
  </graph>
</graphml>

When I read graph

void ReadGraph() {
    boost::dynamic_properties dp;
    std::ifstream file(fileName);
    boost::read_graphml(file, graph, dp);
}

This is crashing says property Name not found. If I use ignore_other_properties for property,

boost::dynamic_properties dp(ignore_other_properties);

It works but I am not getting any graph item in graph vertices.

sehe
  • 374,641
  • 47
  • 450
  • 633

1 Answers1

2

The graph is not empty, you get:

0 --> 1 
1 --> 
2 --> 3 
3 --> 

Or in XML: https://paste.ubuntu.com/p/c4tGmxGssJ/

Of course, you wanted to read the name property. For that you obviously need to register the property with the dynamic-properties map.

Note You can access members of property bundles much simpler:

    dp.property("Name", boost::get(&GraphData::Name, graph));

Full Demo

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

struct GraphData {
    std::string Name;
};

using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, GraphData>;

Graph ReadGraph(std::string const& fileName) {
    Graph graph;
    boost::dynamic_properties dp;
    dp.property("Name", boost::get(&GraphData::Name, graph));

    std::ifstream file(fileName);
    boost::read_graphml(file, graph, dp);

    return graph;
}

void WriteGraph(std::ostream& os, Graph& graph) {
    boost::dynamic_properties dp;
    dp.property("Name", get(&GraphData::Name, graph));

    boost::write_graphml(os, graph, dp, true); 
}

#include <boost/graph/graph_utility.hpp>

int main() {
    Graph g = ReadGraph("input.txt");
    print_graph(g, get(&GraphData::Name, g));

    // or as XML
    WriteGraph(std::cout << "==== XML version: ====\n\n", g);
}

Prints

A --> D 
D --> 
B --> C 
C --> 
==== XML version: ====

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="key0" for="node" attr.name="Name" attr.type="string" />
  <graph id="G" edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
    <node id="n0">
      <data key="key0">A</data>
    </node>
    <node id="n1">
      <data key="key0">D</data>
    </node>
    <node id="n2">
      <data key="key0">B</data>
    </node>
    <node id="n3">
      <data key="key0">C</data>
    </node>
    <edge id="e0" source="n0" target="n1">
    </edge>
    <edge id="e1" source="n2" target="n3">
    </edge>
  </graph>
</graphml>
sehe
  • 374,641
  • 47
  • 450
  • 633
  • If I have user define property inside structure, what should I do while writing graph so that it should consider that object type rather than primitive type same is for reading graph. – Milan Dabhi Mar 02 '18 at 07:52
  • Can you rephrase that? Perhaps you should post a new question so you can show an example – sehe Mar 02 '18 at 08:18
  • struct GraphData { std::string Name; Student student; } class student { int roll_no; std::string division; }; How should I write and read these properties ? – Milan Dabhi Mar 03 '18 at 09:58
  • Yeah I'd make that a new question. – sehe Mar 03 '18 at 10:03
  • In case you post that question, I've written up a draft answer containing three approaches. [timelapse](http://stackoverflow-sehe.s3.amazonaws.com/bbd9b921-6d80-44d3-9ac6-e0d4276b139d/timelapse.mp4) coding. – sehe Mar 03 '18 at 13:33
  • I assume you solved your issue some other way. I'll forget about it then. – sehe Mar 06 '18 at 13:28
  • Thanks, Yes It has been solved I have overloaded istream and ostream operator and it works for me. I have another question I shall post it asap. – Milan Dabhi Mar 07 '18 at 16:24