graphml.hpp 13 KB

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