1 #ifndef GRAPHML_WRITER_HPP
2 #define GRAPHML_WRITER_HPP
4 #include "Graph/map_graph.hpp"
7 #include "util/common.hpp"
20 void create_prop(std::string * str)
23 if (has_attributes<T>::value )
26 for (
size_t i = 0 ; i < T::max_prop ; i++)
28 str[i] = std::string(T::attributes::name[i]);
34 for (
size_t i = 0 ; i < T::max_prop ; i++)
36 str[i] =
"attr" + std::to_string(i);
60 std::string * attributes_names;
75 vertex_prop(std::string & v_prop,
typename G::V_type::attributes & a_name)
76 :v_prop(v_prop),attributes_names(a_name.name)
79 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
91 :v_prop(v_prop),attributes_names(NULL)
94 n_attr = G::V_type::max_prop;
97 attributes_names =
new std::string[G::V_type::max_prop];
100 create_prop<typename G::V_type>(attributes_names);
111 if (attributes_names[cnt] ==
"x" || attributes_names[cnt] ==
"y"
112 || attributes_names[cnt] ==
"z" || attributes_names[cnt] ==
"shape" )
116 if (
typeid(T) ==
typeid(
float))
117 v_prop +=
"<key id=\"vk" + std::to_string(cnt) +
"\" for=\"node\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"float\"/>\n";
118 else if (
typeid(T) ==
typeid(
double))
119 v_prop +=
"<key id=\"vk" + std::to_string(cnt) +
"\" for=\"node\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"double\"/>\n";
120 else if (
typeid(T) ==
typeid(
int))
121 v_prop +=
"<key id=\"vk" + std::to_string(cnt) +
"\" for=\"node\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"int\"/>\n";
122 else if (
typeid(T) ==
typeid(
long int))
123 v_prop +=
"<key id=\"vk" + std::to_string(cnt) +
"\" for=\"node\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"long\"/>\n";
124 else if (
typeid(T) ==
typeid(
bool))
125 v_prop +=
"<key id=\"vk" + std::to_string(cnt) +
"\" for=\"node\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"boolean\"/>\n";
126 else if (
typeid(T) ==
typeid(std::string))
127 v_prop +=
"<key id=\"vk" + std::to_string(cnt) +
"\" for=\"node\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"string\"/>\n";
147 const typename G::V_container & vo;
153 std::string & v_node;
156 std::string * attributes_names;
171 inline vertex_node(std::string & v_node,
const typename G::V_container & n_obj,
typename G::V_type::attributes & a_name)
172 :vo(n_obj),v_node(v_node),attributes_names(a_name.name)
175 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
185 inline vertex_node(std::string & v_node,
const typename G::V_container && n_obj,
typename G::V_type::attributes & a_name)
186 :vo(n_obj),v_node(v_node),attributes_names(a_name.name)
187 {std::cerr <<
"Error: " <<__FILE__ <<
":" << __LINE__ <<
" Passing a temporal object\n";};
198 inline vertex_node(std::string & v_node,
const typename G::V_container & n_obj)
199 :vo(n_obj),v_node(v_node),attributes_names(NULL)
202 n_attr = G::V_type::max_prop;
205 attributes_names =
new std::string[G::V_type::max_prop];
208 create_prop<typename G::V_type>(attributes_names);
213 delete [] attributes_names;
223 inline vertex_node(std::string & v_node,
const typename G::V_container && n_obj)
224 :vo(n_obj),v_node(v_node),attributes_names(NULL)
225 {std::cerr <<
"Error: " <<__FILE__ <<
":" << __LINE__ <<
" Passing a temporal object\n";};
236 v_node +=
"<node id=\"n"+ std::to_string(v_c) +
"\">\n";
250 v_node +=
"</node>\n";
258 if (T::value < n_attr)
261 if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
float))
262 v_node +=
" <data key=\"vk" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
263 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
double))
264 v_node +=
" <data key=\"vk" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
265 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
int))
266 v_node +=
" <data key=\"vk" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
267 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
long int))
268 v_node +=
" <data key=\"vk" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
269 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
bool))
270 v_node +=
" <data key=\"vk" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
271 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(std::string))
272 v_node +=
" <data key=\"vk" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
295 std::string & e_prop;
298 std::string * attributes_names;
312 edge_prop(std::string & e_prop,
typename G::E_type::attributes & a_name)
313 :e_prop(e_prop),attributes_names(a_name.name)
316 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
328 :e_prop(e_prop),attributes_names(NULL)
331 n_attr = G::E_type::max_prop;
334 attributes_names =
new std::string[G::E_type::max_prop];
337 create_prop<typename G::E_type>(attributes_names);
348 if (
typeid(T) ==
typeid(
float))
349 e_prop +=
"<key id=\"ek" + std::to_string(cnt) +
"\" for=\"edge\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"float\"/>\n";
350 else if (
typeid(T) ==
typeid(
double))
351 e_prop +=
"<key id=\"ek" + std::to_string(cnt) +
"\" for=\"edge\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"double\"/>\n";
352 else if (
typeid(T) ==
typeid(
int))
353 e_prop +=
"<key id=\"ek" + std::to_string(cnt) +
"\" for=\"edge\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"int\"/>\n";
354 else if (
typeid(T) ==
typeid(
long int))
355 e_prop +=
"<key id=\"ek" + std::to_string(cnt) +
"\" for=\"edge\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"long\"/>\n";
356 else if (
typeid(T) ==
typeid(
bool))
357 e_prop +=
"<key id=\"ek" + std::to_string(cnt) +
"\" for=\"edge\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"boolean\"/>\n";
358 else if (
typeid(T) ==
typeid(std::string))
359 e_prop +=
"<key id=\"ek" + std::to_string(cnt) +
"\" for=\"edge\" attr.name=\"" + attributes_names[cnt] +
"\" attr.type=\"string\"/>\n";
379 typename G::E_container & vo;
385 std::string & e_node;
388 std::string * attributes_names;
403 edge_node(std::string & e_node,
typename G::E_container & n_obj,
typename G::E_type::attributes & a_name)
404 :vo(n_obj),e_node(e_node),attributes_names(a_name.name)
407 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
419 edge_node(std::string & e_node,
typename G::E_container & n_obj)
420 :vo(n_obj),e_node(e_node),attributes_names(NULL)
423 n_attr = G::E_type::max_prop;
426 attributes_names =
new std::string[G::E_type::max_prop];
429 create_prop<typename G::E_type>(attributes_names);
441 e_node +=
"<edge id=\"e"+ std::to_string(v_c) +
"\" source=\"n" + std::to_string(s) +
"\" target=\"n" + std::to_string(d) +
"\">\n";
455 e_node +=
"</edge>\n";
463 if (T::value < n_attr)
466 if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
float))
467 e_node +=
" <data key=\"ek" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
468 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
double))
469 e_node +=
" <data key=\"ek" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
470 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
int))
471 e_node +=
" <data key=\"ek" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
472 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
long int))
473 e_node +=
" <data key=\"ek" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
474 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(
bool))
475 e_node +=
" <data key=\"ek" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
476 else if (
typeid(decltype(vo.template get<T::value>())) ==
typeid(std::string))
477 e_node +=
" <data key=\"ek" + std::to_string(cnt) +
"\">" + std::to_string(vo.template get<T::value>()) +
"</data>\n";
490 template <
typename Graph>
508 std::string v_out(
"");
514 boost::mpl::for_each< typename Graph::V_type::type >(vp);
537 boost::mpl::for_each< typename Graph::E_type::type >(ep);
552 auto it = g.getVertexIterator();
557 auto v = g.vertex(it.get());
566 boost::mpl::for_each_ref< boost::mpl::range_c<int,0,Graph::V_type::max_prop> >(vn);
589 auto it = g.getEdgeIterator();
595 auto obj = g.edge(it.get());
601 en.
new_node(nc,it.source(),it.target());
604 boost::mpl::for_each< boost::mpl::range_c<int,0,Graph::E_type::max_prop> >(en);
638 bool write(std::string file, std::string graph_name=
"Graph")
641 std::string gml_header;
643 std::string vertex_list;
645 std::string gml_header_end;
647 std::string graph_header;
649 std::string graph_header_end;
651 std::string edge_list;
653 std::string vertex_prop_header;
655 std::string edge_prop_header;
658 gml_header =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
659 <graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n\
660 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n\
661 xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n\
662 http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n";
665 graph_header =
"<graph id=\"" + graph_name +
"\" edgedefault=\"undirected\">\n";
667 graph_header_end =
"</graph>\n";
682 gml_header_end =
"</graphml>";
686 std::ofstream ofs(file);
689 if (ofs.is_open() ==
false)
690 {std::cerr <<
"Error cannot creare the graphML file: " + file;}
692 ofs << gml_header << graph_header << vertex_prop_header << edge_prop_header <<
693 vertex_list << edge_list << graph_header_end << gml_header_end;
this class is a functor for "for_each" algorithm
vertex_prop(std::string &v_prop)
Constructor.
vertex_node(std::string &v_node, const typename G::V_container &n_obj)
Constructor.
void end_node()
Close a node.
this class is a functor for "for_each" algorithm
void new_node(size_t v_c, size_t s, size_t d)
Create a new node.
std::string get_vertex_list()
std::string get_edge_list()
edge_node(std::string &e_node, typename G::E_container &n_obj, typename G::E_type::attributes &a_name)
Constructor.
void new_node(size_t v_c)
Create a new node.
bool write(std::string file, std::string graph_name="Graph")
It write a GraphML file from a graph.
edge_node(std::string &e_node, typename G::E_container &n_obj)
Constructor.
vertex_node(std::string &v_node, const typename G::V_container &n_obj, typename G::V_type::attributes &a_name)
Constructor.
void operator()(T &t)
It call the functor for each member.
this class is a functor for "for_each" algorithm
void operator()(T &t)
It call the functor for each member.
std::string get_vertex_properties_list()
It get the vertex properties list.
vertex_prop(std::string &v_prop, typename G::V_type::attributes &a_name)
Constructor.
void operator()(T &t)
It call the functor for each member.
void operator()(T &t)
It call the functor for each member.
edge_prop(std::string &e_prop, typename G::E_type::attributes &a_name)
Constructor.
this class is a functor for "for_each" algorithm
void end_node()
Close a node.
edge_prop(std::string &e_prop)
Constructor.
std::string get_edge_properties_list()
It get the edge properties list.