graphml.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright (C) 2006 Tiago de Paula Peixoto <[email protected]>
  2. // Copyright (C) 2004 The Trustees of Indiana University.
  3. //
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Authors: Douglas Gregor
  9. // Andrew Lumsdaine
  10. // Tiago de Paula Peixoto
  11. #ifndef BOOST_GRAPH_GRAPHML_HPP
  12. #define BOOST_GRAPH_GRAPHML_HPP
  13. #include <boost/config.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/any.hpp>
  16. #include <boost/type_traits/is_convertible.hpp>
  17. #include <boost/graph/dll_import_export.hpp>
  18. #include <boost/graph/graphviz.hpp> // for exceptions
  19. #include <boost/mpl/bool.hpp>
  20. #include <boost/mpl/vector.hpp>
  21. #include <boost/mpl/find.hpp>
  22. #include <boost/mpl/for_each.hpp>
  23. #include <boost/property_tree/detail/xml_parser_utils.hpp>
  24. #include <boost/throw_exception.hpp>
  25. #include <exception>
  26. #include <sstream>
  27. #include <typeinfo>
  28. namespace boost
  29. {
  30. /////////////////////////////////////////////////////////////////////////////
  31. // Graph reader exceptions
  32. /////////////////////////////////////////////////////////////////////////////
  33. struct BOOST_SYMBOL_VISIBLE parse_error : public graph_exception
  34. {
  35. parse_error(const std::string& err)
  36. {
  37. error = err;
  38. statement = "parse error: " + error;
  39. }
  40. ~parse_error() throw() BOOST_OVERRIDE {}
  41. const char* what() const throw() BOOST_OVERRIDE { return statement.c_str(); }
  42. std::string statement;
  43. std::string error;
  44. };
  45. class mutate_graph
  46. {
  47. public:
  48. virtual ~mutate_graph() {}
  49. virtual bool is_directed() const = 0;
  50. virtual boost::any do_add_vertex() = 0;
  51. virtual std::pair< boost::any, bool > do_add_edge(
  52. boost::any source, boost::any target)
  53. = 0;
  54. virtual void set_graph_property(const std::string& name,
  55. const std::string& value, const std::string& value_type)
  56. = 0;
  57. virtual void set_vertex_property(const std::string& name, boost::any vertex,
  58. const std::string& value, const std::string& value_type)
  59. = 0;
  60. virtual void set_edge_property(const std::string& name, boost::any edge,
  61. const std::string& value, const std::string& value_type)
  62. = 0;
  63. };
  64. template < typename MutableGraph > class mutate_graph_impl : public mutate_graph
  65. {
  66. typedef typename graph_traits< MutableGraph >::vertex_descriptor
  67. vertex_descriptor;
  68. typedef
  69. typename graph_traits< MutableGraph >::edge_descriptor edge_descriptor;
  70. public:
  71. mutate_graph_impl(MutableGraph& g, dynamic_properties& dp)
  72. : m_g(g), m_dp(dp)
  73. {
  74. }
  75. bool is_directed() const BOOST_OVERRIDE
  76. {
  77. return is_convertible<
  78. typename graph_traits< MutableGraph >::directed_category,
  79. directed_tag >::value;
  80. }
  81. any do_add_vertex() BOOST_OVERRIDE { return any(add_vertex(m_g)); }
  82. std::pair< any, bool > do_add_edge(any source, any target) BOOST_OVERRIDE
  83. {
  84. std::pair< edge_descriptor, bool > retval
  85. = add_edge(any_cast< vertex_descriptor >(source),
  86. any_cast< vertex_descriptor >(target), m_g);
  87. return std::make_pair(any(retval.first), retval.second);
  88. }
  89. void set_graph_property(const std::string& name,
  90. const std::string& value, const std::string& value_type) BOOST_OVERRIDE
  91. {
  92. bool type_found = false;
  93. try
  94. {
  95. mpl::for_each< value_types >(
  96. put_property< MutableGraph*, value_types >(name, m_dp, &m_g,
  97. value, value_type, m_type_names, type_found));
  98. }
  99. catch (const bad_lexical_cast&)
  100. {
  101. BOOST_THROW_EXCEPTION(parse_error("invalid value \"" + value
  102. + "\" for key " + name + " of type " + value_type));
  103. }
  104. if (!type_found)
  105. {
  106. BOOST_THROW_EXCEPTION(parse_error(
  107. "unrecognized type \"" + value_type + "\" for key " + name));
  108. }
  109. }
  110. void set_vertex_property(const std::string& name, any vertex,
  111. const std::string& value, const std::string& value_type) BOOST_OVERRIDE
  112. {
  113. bool type_found = false;
  114. try
  115. {
  116. mpl::for_each< value_types >(
  117. put_property< vertex_descriptor, value_types >(name, m_dp,
  118. any_cast< vertex_descriptor >(vertex), value, value_type,
  119. m_type_names, type_found));
  120. }
  121. catch (const bad_lexical_cast&)
  122. {
  123. BOOST_THROW_EXCEPTION(parse_error("invalid value \"" + value
  124. + "\" for key " + name + " of type " + value_type));
  125. }
  126. if (!type_found)
  127. {
  128. BOOST_THROW_EXCEPTION(parse_error(
  129. "unrecognized type \"" + value_type + "\" for key " + name));
  130. }
  131. }
  132. void set_edge_property(const std::string& name, any edge,
  133. const std::string& value, const std::string& value_type) BOOST_OVERRIDE
  134. {
  135. bool type_found = false;
  136. try
  137. {
  138. mpl::for_each< value_types >(
  139. put_property< edge_descriptor, value_types >(name, m_dp,
  140. any_cast< edge_descriptor >(edge), value, value_type,
  141. m_type_names, type_found));
  142. }
  143. catch (const bad_lexical_cast&)
  144. {
  145. BOOST_THROW_EXCEPTION(parse_error("invalid value \"" + value
  146. + "\" for key " + name + " of type " + value_type));
  147. }
  148. if (!type_found)
  149. {
  150. BOOST_THROW_EXCEPTION(parse_error(
  151. "unrecognized type \"" + value_type + "\" for key " + name));
  152. }
  153. }
  154. template < typename Key, typename ValueVector > class put_property
  155. {
  156. public:
  157. put_property(const std::string& name, dynamic_properties& dp,
  158. const Key& key, const std::string& value,
  159. const std::string& value_type, const char** type_names,
  160. bool& type_found)
  161. : m_name(name)
  162. , m_dp(dp)
  163. , m_key(key)
  164. , m_value(value)
  165. , m_value_type(value_type)
  166. , m_type_names(type_names)
  167. , m_type_found(type_found)
  168. {
  169. }
  170. template < class Value > void operator()(Value)
  171. {
  172. if (m_value_type
  173. == m_type_names[mpl::find< ValueVector,
  174. Value >::type::pos::value])
  175. {
  176. put(m_name, m_dp, m_key, lexical_cast< Value >(m_value));
  177. m_type_found = true;
  178. }
  179. }
  180. private:
  181. const std::string& m_name;
  182. dynamic_properties& m_dp;
  183. const Key& m_key;
  184. const std::string& m_value;
  185. const std::string& m_value_type;
  186. const char** m_type_names;
  187. bool& m_type_found;
  188. };
  189. protected:
  190. MutableGraph& m_g;
  191. dynamic_properties& m_dp;
  192. typedef mpl::vector< bool, int, long, float, double, std::string >
  193. value_types;
  194. static const char* m_type_names[];
  195. };
  196. template < typename MutableGraph >
  197. const char* mutate_graph_impl< MutableGraph >::m_type_names[]
  198. = { "boolean", "int", "long", "float", "double", "string" };
  199. void BOOST_GRAPH_DECL read_graphml(
  200. std::istream& in, mutate_graph& g, size_t desired_idx);
  201. template < typename MutableGraph >
  202. void read_graphml(std::istream& in, MutableGraph& g, dynamic_properties& dp,
  203. size_t desired_idx = 0)
  204. {
  205. mutate_graph_impl< MutableGraph > mg(g, dp);
  206. read_graphml(in, mg, desired_idx);
  207. }
  208. template < typename Types > class get_type_name
  209. {
  210. public:
  211. get_type_name(const std::type_info& type, const char** type_names,
  212. std::string& type_name)
  213. : m_type(type), m_type_names(type_names), m_type_name(type_name)
  214. {
  215. }
  216. template < typename Type > void operator()(Type)
  217. {
  218. if (typeid(Type) == m_type)
  219. m_type_name
  220. = m_type_names[mpl::find< Types, Type >::type::pos::value];
  221. }
  222. private:
  223. const std::type_info& m_type;
  224. const char** m_type_names;
  225. std::string& m_type_name;
  226. };
  227. template < typename Graph, typename VertexIndexMap >
  228. void write_graphml(std::ostream& out, const Graph& g,
  229. VertexIndexMap vertex_index, const dynamic_properties& dp,
  230. bool ordered_vertices = false)
  231. {
  232. typedef typename graph_traits< Graph >::directed_category directed_category;
  233. typedef typename graph_traits< Graph >::edge_descriptor edge_descriptor;
  234. typedef typename graph_traits< Graph >::vertex_descriptor vertex_descriptor;
  235. using boost::property_tree::xml_parser::encode_char_entities;
  236. BOOST_STATIC_CONSTANT(bool,
  237. graph_is_directed
  238. = (is_convertible< directed_category*, directed_tag* >::value));
  239. out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  240. << "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" "
  241. "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
  242. "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns "
  243. "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n";
  244. typedef mpl::vector< bool, short, unsigned short, int, unsigned int, long,
  245. unsigned long, long long, unsigned long long, float, double,
  246. long double, std::string >
  247. value_types;
  248. const char* type_names[] = { "boolean", "int", "int", "int", "int", "long",
  249. "long", "long", "long", "float", "double", "double", "string" };
  250. std::map< std::string, std::string > graph_key_ids;
  251. std::map< std::string, std::string > vertex_key_ids;
  252. std::map< std::string, std::string > edge_key_ids;
  253. int key_count = 0;
  254. // Output keys
  255. for (dynamic_properties::const_iterator i = dp.begin(); i != dp.end(); ++i)
  256. {
  257. std::string key_id = "key" + lexical_cast< std::string >(key_count++);
  258. if (i->second->key() == typeid(Graph*))
  259. graph_key_ids[i->first] = key_id;
  260. else if (i->second->key() == typeid(vertex_descriptor))
  261. vertex_key_ids[i->first] = key_id;
  262. else if (i->second->key() == typeid(edge_descriptor))
  263. edge_key_ids[i->first] = key_id;
  264. else
  265. continue;
  266. std::string type_name = "string";
  267. mpl::for_each< value_types >(get_type_name< value_types >(
  268. i->second->value(), type_names, type_name));
  269. out << " <key id=\"" << encode_char_entities(key_id) << "\" for=\""
  270. << (i->second->key() == typeid(Graph*)
  271. ? "graph"
  272. : (i->second->key() == typeid(vertex_descriptor)
  273. ? "node"
  274. : "edge"))
  275. << "\""
  276. << " attr.name=\"" << i->first << "\""
  277. << " attr.type=\"" << type_name << "\""
  278. << " />\n";
  279. }
  280. out << " <graph id=\"G\" edgedefault=\""
  281. << (graph_is_directed ? "directed" : "undirected") << "\""
  282. << " parse.nodeids=\"" << (ordered_vertices ? "canonical" : "free")
  283. << "\""
  284. << " parse.edgeids=\"canonical\" parse.order=\"nodesfirst\">\n";
  285. // Output graph data
  286. for (dynamic_properties::const_iterator i = dp.begin(); i != dp.end(); ++i)
  287. {
  288. if (i->second->key() == typeid(Graph*))
  289. {
  290. // The const_cast here is just to get typeid correct for property
  291. // map key; the graph should not be mutated using it.
  292. out << " <data key=\"" << graph_key_ids[i->first] << "\">"
  293. << encode_char_entities(
  294. i->second->get_string(const_cast< Graph* >(&g)))
  295. << "</data>\n";
  296. }
  297. }
  298. typedef typename graph_traits< Graph >::vertex_iterator vertex_iterator;
  299. vertex_iterator v, v_end;
  300. for (boost::tie(v, v_end) = vertices(g); v != v_end; ++v)
  301. {
  302. out << " <node id=\"n" << get(vertex_index, *v) << "\">\n";
  303. // Output data
  304. for (dynamic_properties::const_iterator i = dp.begin(); i != dp.end();
  305. ++i)
  306. {
  307. if (i->second->key() == typeid(vertex_descriptor))
  308. {
  309. out << " <data key=\"" << vertex_key_ids[i->first] << "\">"
  310. << encode_char_entities(i->second->get_string(*v))
  311. << "</data>\n";
  312. }
  313. }
  314. out << " </node>\n";
  315. }
  316. typedef typename graph_traits< Graph >::edge_iterator edge_iterator;
  317. edge_iterator e, e_end;
  318. typename graph_traits< Graph >::edges_size_type edge_count = 0;
  319. for (boost::tie(e, e_end) = edges(g); e != e_end; ++e)
  320. {
  321. out << " <edge id=\"e" << edge_count++ << "\" source=\"n"
  322. << get(vertex_index, source(*e, g)) << "\" target=\"n"
  323. << get(vertex_index, target(*e, g)) << "\">\n";
  324. // Output data
  325. for (dynamic_properties::const_iterator i = dp.begin(); i != dp.end();
  326. ++i)
  327. {
  328. if (i->second->key() == typeid(edge_descriptor))
  329. {
  330. out << " <data key=\"" << edge_key_ids[i->first] << "\">"
  331. << encode_char_entities(i->second->get_string(*e))
  332. << "</data>\n";
  333. }
  334. }
  335. out << " </edge>\n";
  336. }
  337. out << " </graph>\n"
  338. << "</graphml>\n";
  339. }
  340. template < typename Graph >
  341. void write_graphml(std::ostream& out, const Graph& g,
  342. const dynamic_properties& dp, bool ordered_vertices = false)
  343. {
  344. write_graphml(out, g, get(vertex_index, g), dp, ordered_vertices);
  345. }
  346. } // boost namespace
  347. #endif // BOOST_GRAPH_GRAPHML_HPP