1#ifndef GRAPHML_WRITER_HPP
2#define GRAPHML_WRITER_HPP
4#include "Graph/map_graph.hpp"
7#include "util/common.hpp"
20void create_prop(std::string * str)
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);
80 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
94 n_attr = G::V_type::max_prop;
128 if (std::is_same<T,float>::value)
130 else if (std::is_same<T,double>::value)
131 v_prop +=
"<key id=\"vk" + std::to_string(
cnt) +
"\" for=\"node\" attr.name=\"" +
attributes_names[
cnt] +
"\" attr.type=\"double\"/>\n";
132 else if (std::is_same<T,int>::value)
134 else if (std::is_same<T,long int>::value)
136 else if (std::is_same<T,bool>::value)
137 v_prop +=
"<key id=\"vk" + std::to_string(
cnt) +
"\" for=\"node\" attr.name=\"" +
attributes_names[
cnt] +
"\" attr.type=\"boolean\"/>\n";
138 else if (std::is_same<T,std::string>::value)
139 v_prop +=
"<key id=\"vk" + std::to_string(
cnt) +
"\" for=\"node\" attr.name=\"" +
attributes_names[
cnt] +
"\" attr.type=\"string\"/>\n";
159 const typename G::V_container &
vo;
186 inline vertex_node(std::string &
v_node,
const typename G::V_container & n_obj,
typename G::V_type::attributes & a_name)
190 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
200 inline vertex_node(std::string &
v_node,
const typename G::V_container && n_obj,
typename G::V_type::attributes & a_name)
202 {std::cerr <<
"Error: " <<__FILE__ <<
":" << __LINE__ <<
" Passing a temporal object\n";};
217 n_attr = G::V_type::max_prop;
242 {std::cerr <<
"Error: " <<__FILE__ <<
":" << __LINE__ <<
" Passing a temporal object\n";};
253 v_node +=
"<node id=\"n"+ std::to_string(v_c) +
"\">\n";
281 typedef typename std::remove_reference<
decltype(
vo.template get<T::value>())>::type type_get;
284 if (std::is_same<type_get,float>::value)
285 v_node +=
" <data key=\"vk" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
286 else if (std::is_same<type_get,double>::value)
287 v_node +=
" <data key=\"vk" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
288 else if (std::is_same<type_get,int>::value )
289 v_node +=
" <data key=\"vk" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
290 else if (std::is_same<type_get,long int>::value)
291 v_node +=
" <data key=\"vk" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
292 else if (std::is_same<type_get,bool>::value)
293 v_node +=
" <data key=\"vk" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
294 else if (std::is_same<type_get,std::string>::value)
295 v_node +=
" <data key=\"vk" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
342 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
356 n_attr = G::E_type::max_prop;
384 if (std::is_same<T,float>::value)
386 else if (std::is_same<T,double>::value)
387 e_prop +=
"<key id=\"ek" + std::to_string(
cnt) +
"\" for=\"edge\" attr.name=\"" +
attributes_names[
cnt] +
"\" attr.type=\"double\"/>\n";
388 else if (std::is_same<T,int>::value)
390 else if (std::is_same<T,long int>::value)
392 else if (std::is_same<T,bool>::value)
393 e_prop +=
"<key id=\"ek" + std::to_string(
cnt) +
"\" for=\"edge\" attr.name=\"" +
attributes_names[
cnt] +
"\" attr.type=\"boolean\"/>\n";
394 else if (std::is_same<T,std::string>::value)
395 e_prop +=
"<key id=\"ek" + std::to_string(
cnt) +
"\" for=\"edge\" attr.name=\"" +
attributes_names[
cnt] +
"\" attr.type=\"string\"/>\n";
415 typename G::E_container &
vo;
441 edge_node(std::string &
e_node,
typename G::E_container & n_obj,
typename G::E_type::attributes & a_name)
445 n_attr =
sizeof(a_name.name)/
sizeof(std::string);
460 n_attr = G::E_type::max_prop;
487 e_node +=
"<edge id=\"e"+ std::to_string(v_c) +
"\" source=\"n" + std::to_string(s) +
"\" target=\"n" + std::to_string(d) +
"\">\n";
515 typedef typename std::remove_reference<
decltype(
vo.template get<T::value>())>::type type_get;
518 if (std::is_same<type_get,float>::value)
519 e_node +=
" <data key=\"ek" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
520 else if (std::is_same<type_get,double>::value)
521 e_node +=
" <data key=\"ek" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
522 else if (std::is_same<type_get,int>::value)
523 e_node +=
" <data key=\"ek" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
524 else if (std::is_same<type_get,long int>::value)
525 e_node +=
" <data key=\"ek" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
526 else if (std::is_same<type_get,bool>::value)
527 e_node +=
" <data key=\"ek" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
528 else if (std::is_same<type_get,std::string>::value)
529 e_node +=
" <data key=\"ek" + std::to_string(
cnt) +
"\">" + std::to_string(
vo.template get<T::value>()) +
"</data>\n";
542template <
typename Graph>
560 std::string v_out(
"");
566 boost::mpl::for_each_ref< typename Graph::V_type::type >(vp);
588 boost::mpl::for_each_ref< typename Graph::E_type::type >(ep);
608 auto it =
g.getVertexIterator();
613 auto v =
g.vertex(it.get());
622 boost::mpl::for_each_ref< boost::mpl::range_c<int,0,Graph::V_type::max_prop> >(vn);
650 auto it =
g.getEdgeIterator();
656 auto obj =
g.edge(it.get());
662 en.
new_node(nc,it.source(),it.target());
665 boost::mpl::for_each_ref< boost::mpl::range_c<int,0,Graph::E_type::max_prop> >(en);
698 bool write(std::string file, std::string graph_name=
"Graph")
701 std::string gml_header;
703 std::string vertex_list;
705 std::string gml_header_end;
707 std::string graph_header;
709 std::string graph_header_end;
711 std::string edge_list;
713 std::string vertex_prop_header;
715 std::string edge_prop_header;
718 gml_header =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
719 <graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n\
720 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n\
721 xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n\
722 http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n";
725 graph_header =
"<graph id=\"" + graph_name +
"\" edgedefault=\"undirected\">\n";
727 graph_header_end =
"</graph>\n";
742 gml_header_end =
"</graphml>";
746 std::ofstream ofs(file);
749 if (ofs.is_open() ==
false)
750 {std::cerr <<
"Error cannot creare the graphML file: " + file;}
752 ofs << gml_header << graph_header << vertex_prop_header << edge_prop_header <<
753 vertex_list << edge_list << graph_header_end << gml_header_end;
std::string get_vertex_properties_list()
It get the vertex properties list.
std::string get_edge_properties_list()
It get the edge properties list.
bool write(std::string file, std::string graph_name="Graph")
It write a GraphML file from a graph.
std::string get_edge_list()
return the edge list as a string
std::string get_vertex_list()
Get the string containing the set of vertices.
this class is a functor for "for_each" algorithm
bool to_destroy
indicate if attributes_names is to destroy
int cnt
Properties counter.
std::string * attributes_names
Attribute names.
void end_node()
Close a node.
int n_attr
Number of attributes name defined into the vertex.
edge_node(std::string &e_node, typename G::E_container &n_obj)
Constructor.
void new_node(size_t v_c, size_t s, size_t d)
Create a new node.
G::E_container & vo
Vertex object container.
std::string & e_node
edge node string
edge_node(std::string &e_node, typename G::E_container &n_obj, typename G::E_type::attributes &a_name)
Constructor.
this class is a functor for "for_each" algorithm
edge_prop(std::string &e_prop, typename G::E_type::attributes &a_name)
Constructor.
int cnt
Properties counter.
std::string & e_prop
edge properties
int n_attr
Number of attributes name defined into the vertex.
edge_prop(std::string &e_prop)
Constructor.
void operator()(T &t)
It call the functor for each member.
std::string * attributes_names
Attribute names.
bool to_destroy
indicate if attributes_names is to destroy
this class is a functor for "for_each" algorithm
void end_node()
Close a node.
void operator()(T &t)
It call the functor for each member.
std::string & v_node
vertex node string
bool to_destroy
indicate if attributes_names is to destroy
int cnt
Properties counter.
vertex_node(std::string &v_node, const typename G::V_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 new_node(size_t v_c)
Create a new node.
int n_attr
Number of attributes name defined into the vertex.
std::string * attributes_names
Attribute names.
const G::V_container & vo
Vertex object container.
this class is a functor for "for_each" algorithm
bool to_destroy
indicate if attributes_names is to destroy
vertex_prop(std::string &v_prop)
Constructor.
std::string * attributes_names
Attribute names.
std::string & v_prop
vertex properties
int cnt
Properties counter.
vertex_prop(std::string &v_prop, typename G::V_type::attributes &a_name)
Constructor.
int n_attr
Number of attributes name defined into the vertex.