Added support for reading reliably from files with more than one GraphML graph

[SVN r81313]
This commit is contained in:
Jeremiah Willcock 2012-11-12 22:30:19 +00:00
parent 1bb17dab63
commit 4495a409e5
4 changed files with 27 additions and 12 deletions

View File

@ -20,7 +20,7 @@ http://www.boost.org/LICENSE_1_0.txt)
Authors: Tiago de Paula Peixoto -->
<pre class="literal-block">
void read_graphml(std::istream&amp; in, MutableGraph&amp; graph,
dynamic_properties&amp; dp);
dynamic_properties&amp; dp, size_t graph_index = 0);
</pre>
<p>The <tt class="docutils literal"><span class="pre">read_graphml</span></tt> function interprets a graph described using the
<a class="reference external" href="http://graphml.graphdrawing.org/">GraphML</a> format and builds a BGL graph that captures that
@ -39,6 +39,10 @@ this object, using the GraphML attribute names as the property names,
and with the appropriate C++ value type based on the GraphML attribute type
definition. Graph properties are also set with the same
<a class="reference external" href="../../property_map/doc/dynamic_property_map.html">dynamic_properties</a> object, where the key type is the type of the graph itself.</p>
<p>If the file contains multiple graphs, the <tt class="docutils literal"><span class="pre">graph_index</span></tt> parameter controls
which graph will be loaded. It defaults to <tt class="docutils literal"><span class="pre">0</span></tt>, meaning that the first graph
in the file will be loaded. If <tt class="docutils literal"><span class="pre">graph_index</span></tt> is greater than or equal to the
number of graphs in the file, an empty graph will be returned.</p>
<dl class="docutils">
<dt>Requirements:</dt>
<dd><ul class="first last simple">
@ -156,7 +160,7 @@ graph.</li>
</div>
<div class="footer">
<hr class="footer" />
Generated on: 2009-06-12 00:41 UTC.
Generated on: 2012-11-12 22:25 UTC.
Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
</div>

View File

@ -19,7 +19,7 @@ __ ../../../index.htm
::
void read_graphml(std::istream& in, MutableGraph& graph,
dynamic_properties& dp);
dynamic_properties& dp, size_t graph_index = 0);
The ``read_graphml`` function interprets a graph described using the
@ -42,6 +42,11 @@ and with the appropriate C++ value type based on the GraphML attribute type
definition. Graph properties are also set with the same
dynamic_properties_ object, where the key type is the type of the graph itself.
If the file contains multiple graphs, the ``graph_index`` parameter controls
which graph will be loaded. It defaults to ``0``, meaning that the first graph
in the file will be loaded. If ``graph_index`` is greater than or equal to the
number of graphs in the file, an empty graph will be returned.
Requirements:
- The type of the graph must model the `Mutable Graph`_ concept.
- The type of the iterator must model the `Multi-Pass Iterator`_

View File

@ -204,14 +204,14 @@ template<typename MutableGraph>
const char* mutate_graph_impl<MutableGraph>::m_type_names[] = {"boolean", "int", "long", "float", "double", "string"};
void BOOST_GRAPH_DECL
read_graphml(std::istream& in, mutate_graph& g);
read_graphml(std::istream& in, mutate_graph& g, size_t desired_idx);
template<typename MutableGraph>
void
read_graphml(std::istream& in, MutableGraph& g, dynamic_properties& dp)
read_graphml(std::istream& in, MutableGraph& g, dynamic_properties& dp, size_t desired_idx = 0)
{
mutate_graph_impl<MutableGraph> mg(g,dp);
read_graphml(in, mg);
read_graphml(in, mg, desired_idx);
}
template <typename Types>

View File

@ -34,17 +34,23 @@ public:
}
static void get_graphs(const boost::property_tree::ptree& top,
size_t desired_idx /* or -1 for all */,
std::vector<const boost::property_tree::ptree*>& result) {
using boost::property_tree::ptree;
size_t current_idx = 0;
BOOST_FOREACH(const ptree::value_type& n, top) {
if (n.first == "graph") {
result.push_back(&n.second);
get_graphs(n.second, result);
if (current_idx == desired_idx || desired_idx == (size_t)(-1)) {
result.push_back(&n.second);
get_graphs(n.second, (size_t)(-1), result);
if (desired_idx != (size_t)(-1)) break;
}
++current_idx;
}
}
}
void run(std::istream& in)
void run(std::istream& in, size_t desired_idx)
{
using boost::property_tree::ptree;
ptree pt;
@ -74,7 +80,7 @@ public:
}
// Search for graphs
std::vector<const ptree*> graphs;
get_graphs(gml, graphs);
get_graphs(gml, desired_idx, graphs);
BOOST_FOREACH(const ptree* gr, graphs) {
// Search for nodes
BOOST_FOREACH(const ptree::value_type& node, *gr) {
@ -209,9 +215,9 @@ private:
namespace boost
{
void BOOST_GRAPH_DECL
read_graphml(std::istream& in, mutate_graph& g)
read_graphml(std::istream& in, mutate_graph& g, size_t desired_idx)
{
graphml_reader reader(g);
reader.run(in);
reader.run(in, desired_idx);
}
}