GraphMLWriter.hpp 20 KB
Newer Older
incardon's avatar
incardon committed
1
2
3
#ifndef GRAPHML_WRITER_HPP
#define GRAPHML_WRITER_HPP

incardon's avatar
incardon committed
4
#include "Graph/map_graph.hpp"
incardon's avatar
incardon committed
5
6
#include <iostream>
#include <fstream>
incardon's avatar
incardon committed
7
#include "util/common.hpp"
incardon's avatar
incardon committed
8

incardon's avatar
incardon committed
9

incardon's avatar
incardon committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*! \brief Create properties name starting from a type T
 *
 * if T has defined some properties name that name are used otherwise
 * default name are created
 *
 * \tparam T vertex type
 *
 */

template <typename T>
void create_prop(std::string * str)
{
	// if T has attributes defined
	if (has_attributes<T>::value )
incardon's avatar
incardon committed
24
	{
incardon's avatar
incardon committed
25
		// Create properties names based on the attributes name defined
incardon's avatar
incardon committed
26
		for (size_t i = 0 ; i < T::max_prop ; i++)
incardon's avatar
incardon committed
27
28
29
30
31
32
33
		{
			str[i] = std::string(T::attributes::name[i]);
		}
	}
	else
	{
		// Create default properties name
incardon's avatar
incardon committed
34
		for (size_t i = 0 ; i < T::max_prop ; i++)
incardon's avatar
incardon committed
35
36
37
		{
			str[i] = "attr" + std::to_string(i);
		}
incardon's avatar
incardon committed
38
39
40
	}
}

incardon's avatar
incardon committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to create a string containing all the vertex
 * properties
 *
 */

template<typename G>
struct vertex_prop
{
	// Properties counter
	int cnt = 0;

	// vertex properties
	std::string & v_prop;

	// Attribute names
	std::string * attributes_names;

	// Number of attributes name defined into the vertex
	int n_attr = 0;

	/*! \brief Constructor
	 *
	 * Create a vertex properties list
	 *
	 * \param v_prop std::string that is filled with the graph properties in the GraphML format
	 * \param stub SFINAE, it basically check if G has properties names defined, if yes this
	 *        constructor is selected over the other one
	 *
	 */
incardon's avatar
incardon committed
74

incardon's avatar
incardon committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	vertex_prop(std::string & v_prop, typename G::V_type::attributes & a_name)
	:v_prop(v_prop),attributes_names(a_name.name)
	{
		// Calculate the number of attributes name
		n_attr = sizeof(a_name.name)/sizeof(std::string);
	};

	/*! \brief Constructor
	 *
	 * Create a vertex properties list
	 *
	 * \param v_prop std::string that is filled with the graph properties in the GraphML format
	 * \param n_prop number of properties
	 *
	 */
	vertex_prop(std::string & v_prop)
	:v_prop(v_prop),attributes_names(NULL)
	{
incardon's avatar
incardon committed
93
94
95
96
97
98
99
100
		// Calculate the number of attributes
		n_attr = G::V_type::max_prop;

		// Create default property names
		attributes_names = new std::string[G::V_type::max_prop];

		// Create default property names
		create_prop<typename G::V_type>(attributes_names);
incardon's avatar
incardon committed
101
102
103
104
105
106
107
108
109
110
	};

	//! It call the functor for each member
    template<typename T>
    void operator()(T& t)
    {
    	//! Create an entry for the attribute
    	if (cnt < n_attr)
    	{
    		// if it is a yFile extension property name, does not process it
incardon's avatar
incardon committed
111
112
    		if (attributes_names[cnt] == "x" || attributes_names[cnt] == "y"
    			|| attributes_names[cnt] == "z" || attributes_names[cnt] == "shape" )
incardon's avatar
incardon committed
113
114
115
116
    		{cnt++; return ;}

    		// Create a property string based on the type of the property
    		if (typeid(T) == typeid(float))
incardon's avatar
incardon committed
117
    			v_prop += "<key id=\"vk" + std::to_string(cnt) + "\" for=\"node\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"float\"/>\n";
incardon's avatar
incardon committed
118
    		else if (typeid(T) == typeid(double))
incardon's avatar
incardon committed
119
    			v_prop += "<key id=\"vk" + std::to_string(cnt) + "\" for=\"node\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"double\"/>\n";
incardon's avatar
incardon committed
120
    		else if (typeid(T) == typeid(int))
incardon's avatar
incardon committed
121
    			v_prop += "<key id=\"vk" + std::to_string(cnt) + "\" for=\"node\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"int\"/>\n";
incardon's avatar
incardon committed
122
    		else if (typeid(T) == typeid(long int))
incardon's avatar
incardon committed
123
    			v_prop += "<key id=\"vk" + std::to_string(cnt) + "\" for=\"node\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"long\"/>\n";
incardon's avatar
incardon committed
124
    		else if (typeid(T) == typeid(bool))
incardon's avatar
incardon committed
125
    			v_prop += "<key id=\"vk" + std::to_string(cnt) + "\" for=\"node\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"boolean\"/>\n";
incardon's avatar
incardon committed
126
    		else if (typeid(T) == typeid(std::string))
incardon's avatar
incardon committed
127
    			v_prop += "<key id=\"vk" + std::to_string(cnt) + "\" for=\"node\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"string\"/>\n";
incardon's avatar
incardon committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    	}

    	cnt++;
    }
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to create a string containing all the vertex
 * properties
 *
 */

template<typename G>
struct vertex_node
{
	// Vertex object container
	const typename G::V_container & vo;

	// Properties counter
	int cnt = 0;

	// vertex node string
	std::string & v_node;

	// Attribute names
	std::string * attributes_names;

	// Number of attributes name defined into the vertex
	int n_attr = 0;

	/*! \brief Constructor
	 *
	 * Create a vertex node
	 *
	 * \param v_node std::string that is filled with the graph node definition in the GraphML format
	 * \param n_obj object container to access its properties for example encapc<...>
	 * \param stub SFINAE, it basically check if G has properties names defined, if yes this
	 *        constructor is selected over the other one
	 *
	 */
171
	inline vertex_node(std::string & v_node, const typename G::V_container & n_obj, typename G::V_type::attributes & a_name)
incardon's avatar
incardon committed
172
173
174
175
176
177
	:vo(n_obj),v_node(v_node),attributes_names(a_name.name)
	{
		// Calculate the number of attributes name
		n_attr = sizeof(a_name.name)/sizeof(std::string);
	};

178
179
180
181
182
183
184
185
186
187
188
189
#ifdef DEBUG
	/*! \brief Constructor
	 *
	 * Calling this constructor produce an error. This class store the reference of the object,
	 * this mean that the object passed must not be a temporal object
	 *
	 */
	inline vertex_node(std::string & v_node, const typename G::V_container && n_obj, typename G::V_type::attributes & a_name)
	:vo(n_obj),v_node(v_node),attributes_names(a_name.name)
	{std::cerr << "Error: " <<__FILE__ << ":" << __LINE__ << " Passing a temporal object\n";};
#endif

incardon's avatar
incardon committed
190
191
192
193
	/*! \brief Constructor
	 *
	 * Create a vertex properties list
	 *
incardon's avatar
incardon committed
194
	 * \param v_node std::string that is filled with the graph properties in the GraphML format
incardon's avatar
incardon committed
195
196
197
	 * \param n_obj object container to access its properties for example encapc<...>
	 *
	 */
198
	inline vertex_node(std::string & v_node, const typename G::V_container & n_obj)
incardon's avatar
incardon committed
199
200
	:vo(n_obj),v_node(v_node),attributes_names(NULL)
	{
incardon's avatar
incardon committed
201
202
203
204
205
206
207
208
		// Calculate the number of attributes
		n_attr = G::V_type::max_prop;

		// Create default property names
		attributes_names = new std::string[G::V_type::max_prop];

		// Create default property names
		create_prop<typename G::V_type>(attributes_names);
incardon's avatar
incardon committed
209
210
	};

211
212
213
214
215
	inline ~vertex_node()
	{
		delete [] attributes_names;
	}

216
217
218
219
220
221
222
223
224
225
226
227
#ifdef DEBUG
	/*! \brief Constructor
	 *
	 * Calling this constructor produce an error. This class store the reference of the object,
	 * this mean that the object passed must not be a temporal object
	 *
	 */
	inline vertex_node(std::string & v_node, const typename G::V_container && n_obj)
	:vo(n_obj),v_node(v_node),attributes_names(NULL)
	{std::cerr << "Error: " <<__FILE__ << ":" << __LINE__ << " Passing a temporal object\n";};
#endif

incardon's avatar
incardon committed
228
229
230
231
232
	/*! \brief Create a new node
	 *
	 * Create a new node
	 *
	 */
incardon's avatar
incardon committed
233
	void new_node(size_t v_c)
incardon's avatar
incardon committed
234
235
	{
		// start a new node
incardon's avatar
incardon committed
236
		v_node += "<node id=\"n"+ std::to_string(v_c) + "\">\n";
incardon's avatar
incardon committed
237
238
239
240
241
242
243
244
245
246
247
248
249

		// reset the counter properties
		cnt = 0;
	}

	/*! \brief Close a node
	 *
	 * Close a node
	 *
	 */
	void end_node()
	{
		// close a node
incardon's avatar
incardon committed
250
		v_node += "</node>\n";
incardon's avatar
incardon committed
251
252
253
254
255
256
257
258
259
	}

	//! It call the functor for each member
    template<typename T>
    void operator()(T& t)
    {
    	//! Create an entry for the attribute
    	if (T::value < n_attr)
    	{
incardon's avatar
incardon committed
260
261
262
263
264
265
266
267
268
269
270
271
272
    		// Create a property string based on the type of the property
    		if (typeid(decltype(vo.template get<T::value>())) == typeid(float))
    			v_node += "  <data key=\"vk" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(double))
    			v_node += "  <data key=\"vk" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(int))
    			v_node += "  <data key=\"vk" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(long int))
    			v_node += "  <data key=\"vk" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(bool))
    			v_node += "  <data key=\"vk" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(std::string))
    			v_node += "  <data key=\"vk" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
incardon's avatar
incardon committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
    	}

    	cnt++;
    }
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to create a string containing all the edge
 * properties
 *
 */

template<typename G>
struct edge_prop
{
	// Properties counter
	int cnt = 0;

	// edge properties
	std::string & e_prop;

	// Attribute names
	std::string * attributes_names;

	// Number of attributes name defined into the vertex
	int n_attr = 0;

	/*! \brief Constructor
	 *
	 * Create an edge properties list
	 *
	 * \param e_prop std::string that is filled with the graph properties in the GraphML format
	 * \param stub SFINAE, it basically check if G::E_type has properties names defined, if yes this
	 *        constructor is selected over the other one
	 *
	 */
	edge_prop(std::string & e_prop, typename G::E_type::attributes & a_name)
	:e_prop(e_prop),attributes_names(a_name.name)
	{
		// Calculate the number of attributes name
		n_attr = sizeof(a_name.name)/sizeof(std::string);
	};

	/*! \brief Constructor
	 *
	 * Create an edge properties list
	 *
	 * \param e_prop std::string that is filled with the graph properties in the GraphML format
	 * \param n_prop number of properties
	 *
	 */
incardon's avatar
incardon committed
327
	edge_prop(std::string & e_prop)
incardon's avatar
incardon committed
328
329
	:e_prop(e_prop),attributes_names(NULL)
	{
incardon's avatar
incardon committed
330
331
332
333
334
335
336
337
		// Calculate the number of attributes
		n_attr = G::E_type::max_prop;

		// Create default property names
		attributes_names = new std::string[G::E_type::max_prop];

		// Create default property names
		create_prop<typename G::E_type>(attributes_names);
incardon's avatar
incardon committed
338
339
340
341
342
343
344
345
346
347
348
	};

	//! It call the functor for each member
    template<typename T>
    void operator()(T& t)
    {
    	//! Create an entry for the attribute
    	if (cnt < n_attr)
    	{
    		// Create a property string based on the type of the property
    		if (typeid(T) == typeid(float))
incardon's avatar
incardon committed
349
    			e_prop += "<key id=\"ek" + std::to_string(cnt) + "\" for=\"edge\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"float\"/>\n";
incardon's avatar
incardon committed
350
    		else if (typeid(T) == typeid(double))
incardon's avatar
incardon committed
351
    			e_prop += "<key id=\"ek" + std::to_string(cnt) + "\" for=\"edge\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"double\"/>\n";
incardon's avatar
incardon committed
352
    		else if (typeid(T) == typeid(int))
incardon's avatar
incardon committed
353
    			e_prop += "<key id=\"ek" + std::to_string(cnt) + "\" for=\"edge\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"int\"/>\n";
incardon's avatar
incardon committed
354
    		else if (typeid(T) == typeid(long int))
incardon's avatar
incardon committed
355
    			e_prop += "<key id=\"ek" + std::to_string(cnt) + "\" for=\"edge\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"long\"/>\n";
incardon's avatar
incardon committed
356
    		else if (typeid(T) == typeid(bool))
incardon's avatar
incardon committed
357
    			e_prop += "<key id=\"ek" + std::to_string(cnt) + "\" for=\"edge\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"boolean\"/>\n";
incardon's avatar
incardon committed
358
    		else if (typeid(T) == typeid(std::string))
incardon's avatar
incardon committed
359
    			e_prop += "<key id=\"ek" + std::to_string(cnt) + "\" for=\"edge\" attr.name=\"" + attributes_names[cnt] + "\" attr.type=\"string\"/>\n";
incardon's avatar
incardon committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
    	}

    	cnt++;
    }
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to create a string containing all the edge
 * properties
 *
 */

template<typename G>
struct edge_node
{
	// Vertex object container
incardon's avatar
incardon committed
379
	typename G::E_container & vo;
incardon's avatar
incardon committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402

	// Properties counter
	int cnt = 0;

	// edge node string
	std::string & e_node;

	// Attribute names
	std::string * attributes_names;

	// Number of attributes name defined into the vertex
	int n_attr = 0;

	/*! \brief Constructor
	 *
	 * Create an edge node
	 *
	 * \param e_node std::string that is filled with the graph node definition in the GraphML format
	 * \param n_obj object container to access the object properties for example encapc<...>
	 * \param stub SFINAE, it basically check if G has properties names defined, if yes this
	 *        constructor is selected over the other one
	 *
	 */
incardon's avatar
incardon committed
403
	edge_node(std::string & e_node, typename G::E_container & n_obj, typename G::E_type::attributes & a_name)
incardon's avatar
incardon committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
	:vo(n_obj),e_node(e_node),attributes_names(a_name.name)
	{
		// Calculate the number of attributes name
		n_attr = sizeof(a_name.name)/sizeof(std::string);
	};

	/*! \brief Constructor
	 *
	 * Create an edge node
	 *
	 * \param e_node std::string that is filled with the graph properties in the GraphML format
	 * \param n_obj object container to access the object properties for example encapc<...>
	 * \param n_prop number of properties
	 *
	 */
incardon's avatar
incardon committed
419
	edge_node(std::string & e_node, typename G::E_container & n_obj)
incardon's avatar
incardon committed
420
421
	:vo(n_obj),e_node(e_node),attributes_names(NULL)
	{
incardon's avatar
incardon committed
422
423
424
425
426
427
428
429
430
		// Calculate the number of attributes
		n_attr = G::E_type::max_prop;

		// Create a number of default properties name
		attributes_names  = new std::string[G::E_type::max_prop];

		// Create default property names
		create_prop<typename G::E_type>(attributes_names);

incardon's avatar
incardon committed
431
432
433
434
	};

	/*! \brief Create a new node
	 *
incardon's avatar
incardon committed
435
	 * \param vc node number
incardon's avatar
incardon committed
436
437
	 *
	 */
incardon's avatar
incardon committed
438
	void new_node(size_t v_c, size_t s, size_t d)
incardon's avatar
incardon committed
439
440
	{
		// start a new node
incardon's avatar
incardon committed
441
		e_node += "<edge id=\"e"+ std::to_string(v_c) + "\" source=\"n" + std::to_string(s) + "\" target=\"n" + std::to_string(d) + "\">\n";
incardon's avatar
incardon committed
442
443
444
445
446
447
448
449
450
451
452
453
454

		// reset the counter properties
		cnt = 0;
	}

	/*! \brief Close a node
	 *
	 * Close a node
	 *
	 */
	void end_node()
	{
		// close a node
incardon's avatar
incardon committed
455
		e_node += "</edge>\n";
incardon's avatar
incardon committed
456
457
458
459
460
461
462
463
464
	}

	//! It call the functor for each member
    template<typename T>
    void operator()(T& t)
    {
    	//! Create an entry for the attribute
    	if (T::value < n_attr)
    	{
incardon's avatar
incardon committed
465
466
467
468
469
470
471
472
473
474
475
476
477
    		// Create a property string based on the type of the property
    		if (typeid(decltype(vo.template get<T::value>())) == typeid(float))
    			e_node += "  <data key=\"ek" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(double))
    			e_node += "  <data key=\"ek" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(int))
    			e_node += "  <data key=\"ek" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(long int))
    			e_node += "  <data key=\"ek" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(bool))
    			e_node += "  <data key=\"ek" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
    		else if (typeid(decltype(vo.template get<T::value>())) == typeid(std::string))
    			e_node += "  <data key=\"ek" + std::to_string(cnt) + "\">" + std::to_string(vo.template get<T::value>()) + "</data>\n";
incardon's avatar
incardon committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    	}

    	cnt++;
    }
};

/*!
 *
 * From a Graphbasic structure it write a GraphML format file
 *
 */

template <typename Graph>
class GraphMLWriter
{
	Graph & g;

	/*! \brief It get the vertex properties list
	 *
	 * It get the vertex properties list of the vertex defined as a GraphML header
incardon's avatar
incardon committed
498
	 * and
incardon's avatar
incardon committed
499
500
501
502
503
504
505
506
507
	 * define position and shape of the node
	 *
	 * \return a string that define the vertex properties in graphML format
	 *
	 */

	std::string get_vertex_properties_list()
	{
		//! vertex property output string
incardon's avatar
incardon committed
508
		std::string v_out("");
incardon's avatar
incardon committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530

		// create a vertex property functor
		vertex_prop<Graph> vp(v_out);

		// Iterate through all the vertex and create the vertex list
		boost::mpl::for_each< typename Graph::V_type::type >(vp);

		// return the vertex properties string
		return v_out;
	}

	/*! \brief It get the edge properties list
	 *
	 * It get the edge properties list of the edge defined as a GraphML header
	 *
	 * \return a string that define the edge properties in graphML format
	 *
	 */

	std::string get_edge_properties_list()
	{
		//! edge property output string
incardon's avatar
incardon committed
531
		std::string e_out;
incardon's avatar
incardon committed
532
533

		// create a vertex property functor
incardon's avatar
incardon committed
534
		edge_prop<Graph> ep(e_out);
incardon's avatar
incardon committed
535
536

		// Iterate through all the vertex and create the vertex list
incardon's avatar
incardon committed
537
		boost::mpl::for_each< typename Graph::E_type::type >(ep);
incardon's avatar
incardon committed
538
539

		// return the edge properties string
incardon's avatar
incardon committed
540
		return e_out;
incardon's avatar
incardon committed
541
542
543
544
	}

	std::string get_vertex_list()
	{
incardon's avatar
incardon committed
545
546
547
		// node counter
		size_t nc = 0;

incardon's avatar
incardon committed
548
549
550
551
552
553
554
555
556
		//! vertex node output string
		std::string v_out;

		//! Get a vertex iterator
		auto it = g.getVertexIterator();

		// if there is the next element
		while (it.isNext())
		{
557
558
			auto v = g.vertex(it.get());

incardon's avatar
incardon committed
559
			// create a vertex list functor
560
			vertex_node<Graph> vn(v_out,v);
incardon's avatar
incardon committed
561

incardon's avatar
incardon committed
562
563
564
			// create new node
			vn.new_node(nc);

incardon's avatar
incardon committed
565
			// Iterate through all the vertex and create the vertex list
566
			boost::mpl::for_each_ref< boost::mpl::range_c<int,0,Graph::V_type::max_prop> >(vn);
incardon's avatar
incardon committed
567

incardon's avatar
incardon committed
568
569
570
571
			// end node
			vn.end_node();

			// increment the iterator and counter
incardon's avatar
incardon committed
572
			++it;
incardon's avatar
incardon committed
573
			nc++;
incardon's avatar
incardon committed
574
575
576
577
578
579
580
581
		}

		// return the vertex list
		return v_out;
	}

	std::string get_edge_list()
	{
incardon's avatar
incardon committed
582
583
584
		// node counter
		size_t nc = 0;

incardon's avatar
incardon committed
585
		//! edge node output string
incardon's avatar
incardon committed
586
		std::string e_out;
incardon's avatar
incardon committed
587
588
589
590
591
592
593

		//! Get an edge iterator
		auto it = g.getEdgeIterator();

		// if there is the next element
		while (it.isNext())
		{
incardon's avatar
incardon committed
594
595
596
			// Get the edge object
			auto obj = g.edge(it.get());

incardon's avatar
incardon committed
597
			// create an edge list functor
incardon's avatar
incardon committed
598
599
600
601
			edge_node<Graph> en(e_out,obj);

			// create a new node
			en.new_node(nc,it.source(),it.target());
incardon's avatar
incardon committed
602

incardon's avatar
incardon committed
603
604
			// Iterate through all the edges and create the edge list
			boost::mpl::for_each< boost::mpl::range_c<int,0,Graph::E_type::max_prop> >(en);
incardon's avatar
incardon committed
605

incardon's avatar
incardon committed
606
607
608
			// end new node
			en.end_node();

incardon's avatar
incardon committed
609
610
			// increment the operator
			++it;
incardon's avatar
incardon committed
611
			nc++;
incardon's avatar
incardon committed
612
613
614
		}

		// return the edge list
incardon's avatar
incardon committed
615
		return e_out;
incardon's avatar
incardon committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
	}

public:

	/*!
	 *
	 * GraphMLWriter constructor, it take a graph and write a GraphML format
	 *
	 * \param g Graph to write
	 *
	 */
	GraphMLWriter(Graph & g)
	:g(g)
	{}

	/*! \brief It write a GraphML file from a graph
	 *
	 * \param file path where to write
	 * \param name of the graph
	 *
	 */

	bool write(std::string file, std::string graph_name="Graph")
	{
		// Header for the GraphML
		std::string gml_header;
		// Vertex list of the GraphML
		std::string vertex_list;
		// End for the GraphML
		std::string gml_header_end;
		// Graph header
		std::string graph_header;
		// Graph header end
		std::string graph_header_end;
		// Edge list of the GraphML
		std::string edge_list;
		// vertex properties header
		std::string vertex_prop_header;
		// edge properties header
		std::string edge_prop_header;

		// GraphML header
incardon's avatar
incardon committed
658
659
660
661
662
		gml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
		<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n\
		    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n\
		    xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n\
		     http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n";
incardon's avatar
incardon committed
663
664

		// Graph header to define an header
incardon's avatar
incardon committed
665
		graph_header = "<graph id=\"" + graph_name + "\" edgedefault=\"undirected\">\n";
incardon's avatar
incardon committed
666
		// Graph header end
incardon's avatar
incardon committed
667
		graph_header_end =  "</graph>\n";
incardon's avatar
incardon committed
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688

		// Vertex properties header
		vertex_prop_header = get_vertex_properties_list();

		// Edge properties header
		edge_prop_header = get_edge_properties_list();

		// Get the node graph list
		vertex_list = get_vertex_list();

		// Get the edge graph list
		edge_list = get_edge_list();

		// Header end
		gml_header_end = "</graphml>";

		// write the file

		std::ofstream ofs(file);

		// Check if the file is open
incardon's avatar
incardon committed
689
		if (ofs.is_open() == false)
incardon's avatar
incardon committed
690
691
692
693
694
		{std::cerr << "Error cannot creare the graphML file: " + file;}

		ofs << gml_header << graph_header << vertex_prop_header << edge_prop_header <<
			   vertex_list << edge_list << graph_header_end << gml_header_end;

incardon's avatar
incardon committed
695
696
697
698
		// Close the file

		ofs.close();

incardon's avatar
incardon committed
699
700
701
702
703
704
		// Completed succefully
		return true;
	}
};

#endif