graphml.hpp 13 KB

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