src/lemon/graph_writer.h
author alpar
Thu, 19 May 2005 11:46:42 +0000
changeset 1428 9ba88ddc629c
parent 1410 dcfad73b3965
child 1429 4283998fb2be
permissions -rw-r--r--
A very simple xml parser
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
deba@1137
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1137
     6
 *
deba@1137
     7
 * Permission to use, modify and distribute this software is granted
deba@1137
     8
 * provided that this copyright notice appears in all copies. For
deba@1137
     9
 * precise terms see the accompanying LICENSE file.
deba@1137
    10
 *
deba@1137
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1137
    12
 * express or implied, and with no claim as to its suitability for any
deba@1137
    13
 * purpose.
deba@1137
    14
 *
deba@1137
    15
 */
deba@1137
    16
alpar@1287
    17
///\ingroup io_group
deba@1137
    18
///\file
alpar@1287
    19
///\brief Lemon Graph Format writer.
deba@1137
    20
deba@1214
    21
#ifndef LEMON_GRAPH_WRITER_H
deba@1214
    22
#define LEMON_GRAPH_WRITER_H
deba@1137
    23
deba@1137
    24
#include <iostream>
deba@1137
    25
deba@1137
    26
#include <lemon/error.h>
deba@1409
    27
#include <lemon/lemon_writer.h>
deba@1137
    28
deba@1137
    29
namespace lemon {
deba@1137
    30
deba@1333
    31
  /// \addtogroup io_group
deba@1333
    32
  /// @{
deba@1333
    33
deba@1137
    34
  /// \brief The graph writer class.
deba@1137
    35
  ///
deba@1333
    36
  /// The \c GraphWriter class provides the graph output. To write a graph
deba@1333
    37
  /// you should first give writing commands for the writer. You can declare
deba@1333
    38
  /// write command as \c NodeMap or \c EdgeMap writing and labeled Node and
deba@1333
    39
  /// Edge writing.
deba@1333
    40
  ///
deba@1333
    41
  /// \code
deba@1333
    42
  /// GraphWriter<ListGraph> writer(std::cout, graph);
deba@1333
    43
  /// \endcode
deba@1333
    44
  ///
deba@1394
    45
  /// The \c writeNodeMap() function declares a \c NodeMap writing 
deba@1394
    46
  /// command in the \c GraphWriter. You should give as parameter 
deba@1394
    47
  /// the name of the map and the map object. The NodeMap writing 
deba@1394
    48
  /// command with name "id" should write a unique map because it 
deba@1394
    49
  /// is regarded as ID map.
deba@1333
    50
  ///
deba@1333
    51
  /// \code
deba@1333
    52
  /// IdMap<ListGraph, Node> nodeIdMap;
deba@1394
    53
  /// writer.writeNodeMap("id", nodeIdMap);
deba@1333
    54
  ///
deba@1421
    55
  /// writer.writeNodeMap("coords", coords);
deba@1394
    56
  /// writer.writeNodeMap("color", colorMap);
deba@1333
    57
  /// \endcode
deba@1333
    58
  ///
deba@1394
    59
  /// With the \c writeEdgeMap() member function you can give an edge map
deba@1333
    60
  /// writing command similar to the NodeMaps.
deba@1333
    61
  ///
deba@1333
    62
  /// \code
deba@1333
    63
  /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
deba@1333
    64
  ///   edgeDescMap(graph);
deba@1394
    65
  /// writer.writeEdgeMap("descriptor", edgeDescMap);
deba@1333
    66
  ///
deba@1394
    67
  /// writer.writeEdgeMap("weight", weightMap);
deba@1394
    68
  /// writer.writeEdgeMap("label", labelMap);
deba@1333
    69
  /// \endcode
deba@1333
    70
  ///
deba@1394
    71
  /// With \c writeNode() and \c writeEdge() functions you can 
deba@1394
    72
  /// point out Nodes and Edges in the graph. By example, you can 
deba@1394
    73
  /// write out the source and target of the graph.
deba@1333
    74
  ///
deba@1333
    75
  /// \code
deba@1394
    76
  /// writer.writeNode("source", sourceNode);
deba@1394
    77
  /// writer.writeNode("target", targetNode);
deba@1333
    78
  ///
deba@1394
    79
  /// writer.writeEdge("observed", edge);
deba@1333
    80
  /// \endcode
deba@1333
    81
  ///
deba@1333
    82
  /// After you give all write commands you must call the \c run() member
deba@1333
    83
  /// function, which execute all the writer commands.
deba@1333
    84
  ///
deba@1333
    85
  /// \code
deba@1333
    86
  /// writer.run();
deba@1333
    87
  /// \endcode
deba@1333
    88
  ///
alpar@1287
    89
  /// \see DefaultWriterTraits
alpar@1287
    90
  /// \see QuotedStringWriter
deba@1333
    91
  /// \see IdMap
deba@1333
    92
  /// \see DescriptorMap
deba@1421
    93
  /// \see \ref GraphReader
alpar@1138
    94
  /// \see \ref graph-io-page
deba@1333
    95
  /// \author Balazs Dezso
deba@1137
    96
  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
deba@1137
    97
  class GraphWriter {
deba@1137
    98
  public:
deba@1137
    99
    
deba@1137
   100
    typedef _Graph Graph;
deba@1137
   101
    typedef typename Graph::Node Node;
deba@1137
   102
    typedef typename Graph::Edge Edge;
deba@1137
   103
deba@1137
   104
    typedef _WriterTraits WriterTraits;
deba@1409
   105
deba@1137
   106
    /// \brief Construct a new GraphWriter.
deba@1137
   107
    ///
deba@1409
   108
    /// Construct a new GraphWriter. It writes the given graph
deba@1409
   109
    /// to the given stream.
deba@1208
   110
    GraphWriter(std::ostream& _os, const Graph& _graph) 
deba@1409
   111
      : writer(new LemonWriter(_os)), own_writer(true), 
deba@1421
   112
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   113
	edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1409
   114
	node_writer(*writer, nodeset_writer, std::string()),
deba@1409
   115
	edge_writer(*writer, edgeset_writer, std::string()),
deba@1409
   116
	attribute_writer(*writer, std::string()) {}
deba@1137
   117
deba@1409
   118
    /// \brief Construct a new GraphWriter.
deba@1409
   119
    ///
deba@1409
   120
    /// Construct a new GraphWriter. It writes into the given graph
deba@1409
   121
    /// to the given file.
deba@1409
   122
    GraphWriter(const std::string& _filename, const Graph& _graph) 
deba@1409
   123
      : writer(new LemonWriter(_filename)), own_writer(true), 
deba@1421
   124
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   125
	edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1409
   126
	node_writer(*writer, nodeset_writer, std::string()),
deba@1409
   127
	edge_writer(*writer, edgeset_writer, std::string()),
deba@1409
   128
	attribute_writer(*writer, std::string()) {}
deba@1409
   129
deba@1409
   130
    /// \brief Construct a new GraphWriter.
deba@1409
   131
    ///
deba@1409
   132
    /// Construct a new GraphWriter. It writes into the given graph
deba@1409
   133
    /// to given LemonReader.
deba@1409
   134
    GraphWriter(LemonWriter& _writer, const Graph& _graph)
deba@1409
   135
      : writer(_writer), own_writer(false), 
deba@1421
   136
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   137
	edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1409
   138
	node_writer(*writer, nodeset_writer, std::string()),
deba@1409
   139
	edge_writer(*writer, edgeset_writer, std::string()),
deba@1409
   140
	attribute_writer(*writer, std::string()) {}
deba@1137
   141
deba@1137
   142
    /// \brief Destruct the graph writer.
deba@1137
   143
    ///
deba@1137
   144
    /// Destruct the graph writer.
deba@1137
   145
    ~GraphWriter() {
deba@1409
   146
      if (own_writer) 
deba@1409
   147
	delete writer;
deba@1137
   148
    }
deba@1137
   149
deba@1137
   150
    /// \brief Add a new node map writer command for the writer.
deba@1137
   151
    ///
deba@1137
   152
    /// Add a new node map writer command for the writer.
deba@1137
   153
    template <typename Map>
deba@1394
   154
    GraphWriter& writeNodeMap(std::string name, const Map& map) {
deba@1421
   155
      nodeset_writer.writeNodeMap(name, map);
deba@1409
   156
      return *this;
deba@1137
   157
    }
deba@1137
   158
deba@1137
   159
    /// \brief Add a new node map writer command for the writer.
deba@1137
   160
    ///
deba@1137
   161
    /// Add a new node map writer command for the writer.
deba@1137
   162
    template <typename Writer, typename Map>
deba@1394
   163
    GraphWriter& writeNodeMap(std::string name, const Map& map, 
deba@1421
   164
			      const Writer& writer = Writer()) {
deba@1421
   165
      nodeset_writer.writeNodeMap(name, map, writer);
deba@1137
   166
      return *this;
deba@1137
   167
    }
deba@1137
   168
deba@1137
   169
deba@1137
   170
    /// \brief Add a new edge map writer command for the writer.
deba@1137
   171
    ///
deba@1137
   172
    /// Add a new edge map writer command for the writer.
deba@1137
   173
    template <typename Map>
deba@1394
   174
    GraphWriter& writeEdgeMap(std::string name, const Map& map) { 
deba@1421
   175
      edgeset_writer.writeEdgeMap(name, map);
deba@1409
   176
      return *this;
deba@1137
   177
    }
deba@1137
   178
deba@1137
   179
deba@1137
   180
    /// \brief Add a new edge map writer command for the writer.
deba@1137
   181
    ///
deba@1137
   182
    /// Add a new edge map writer command for the writer.
deba@1137
   183
    template <typename Writer, typename Map>
deba@1409
   184
    GraphWriter& writeEdgeMap(std::string name, const Map& map,
deba@1421
   185
			      const Writer& writer = Writer()) {
deba@1421
   186
      edgeset_writer.writeEdgeMap(name, map, writer);
deba@1137
   187
      return *this;
deba@1137
   188
    }
deba@1137
   189
deba@1137
   190
    /// \brief Add a new labeled node writer for the writer.
deba@1137
   191
    ///
deba@1137
   192
    /// Add a new labeled node writer for the writer.
deba@1394
   193
    GraphWriter& writeNode(std::string name, const Node& node) {
deba@1409
   194
      node_writer.writeNode(name, node);
deba@1137
   195
      return *this;
deba@1137
   196
    }
deba@1137
   197
deba@1137
   198
    /// \brief Add a new labeled edge writer for the writer.
deba@1137
   199
    ///
deba@1137
   200
    /// Add a new labeled edge writer for the writer.
deba@1394
   201
    GraphWriter& writeEdge(std::string name, const Edge& edge) {
deba@1409
   202
      edge_writer.writeEdge(name, edge);
deba@1409
   203
    }
deba@1409
   204
deba@1409
   205
    /// \brief Add a new attribute writer command.
deba@1409
   206
    ///
deba@1409
   207
    ///  Add a new attribute writer command.
deba@1409
   208
    template <typename Value>
deba@1409
   209
    GraphWriter& writeAttribute(std::string name, const Value& value) {
deba@1409
   210
      attribute_writer.writeAttribute(name, value);
deba@1409
   211
      return *this;
deba@1409
   212
    }
deba@1409
   213
    
deba@1409
   214
    /// \brief Add a new attribute writer command.
deba@1409
   215
    ///
deba@1409
   216
    ///  Add a new attribute writer command.
deba@1409
   217
    template <typename Writer, typename Value>
deba@1409
   218
    GraphWriter& writeAttribute(std::string name, const Value& value, 
deba@1409
   219
			       const Writer& writer) {
deba@1409
   220
      attribute_writer.writeAttribute<Writer>(name, value, writer);
deba@1137
   221
      return *this;
deba@1137
   222
    }
deba@1137
   223
deba@1409
   224
    /// \brief Conversion operator to LemonWriter.
deba@1409
   225
    ///
deba@1409
   226
    /// Conversion operator to LemonWriter. It make possible
deba@1409
   227
    /// to access the encapsulated \e LemonWriter, this way
deba@1409
   228
    /// you can attach to this writer new instances of 
deba@1409
   229
    /// \e LemonWriter::SectionWriter.
deba@1409
   230
    operator LemonWriter&() {
deba@1409
   231
      return *writer;
deba@1396
   232
    }
deba@1396
   233
deba@1137
   234
    /// \brief Executes the writer commands.
deba@1137
   235
    ///
deba@1137
   236
    /// Executes the writer commands.
deba@1409
   237
    void run() {
deba@1409
   238
      writer->run();
deba@1137
   239
    }
deba@1137
   240
deba@1137
   241
  private:
deba@1137
   242
deba@1409
   243
    LemonWriter* writer;
deba@1409
   244
    bool own_writer;
deba@1137
   245
deba@1409
   246
    NodeSetWriter<Graph, WriterTraits> nodeset_writer;
deba@1409
   247
    EdgeSetWriter<Graph, WriterTraits> edgeset_writer;
deba@1409
   248
deba@1409
   249
    NodeWriter<Graph> node_writer;
deba@1409
   250
    EdgeWriter<Graph> edge_writer;
deba@1409
   251
    
deba@1409
   252
    AttributeWriter<WriterTraits> attribute_writer;
deba@1137
   253
  };
deba@1137
   254
deba@1409
   255
deba@1333
   256
  /// \brief Write a graph to the output.
deba@1333
   257
  ///
deba@1333
   258
  /// Write a graph to the output.
deba@1333
   259
  /// \param os The output stream.
deba@1333
   260
  /// \param g The graph.
deba@1333
   261
  /// \param capacity The capacity map.
deba@1333
   262
  /// \param s The source node.
deba@1333
   263
  /// \param t The target node.
deba@1333
   264
  /// \param cost The cost map.
deba@1208
   265
  template<typename Graph, typename CapacityMap, typename CostMap>
deba@1208
   266
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   267
		  const CapacityMap& capacity, const typename Graph::Node &s,
deba@1208
   268
		  const typename Graph::Node &t, const CostMap& cost) {
deba@1394
   269
    GraphWriter<Graph> writer(os, g);
deba@1208
   270
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1394
   271
    writer.writeNodeMap("id", nodeIdMap);
deba@1208
   272
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1394
   273
    writer.writeEdgeMap("id", edgeIdMap);
deba@1394
   274
    writer.writeEdgeMap("capacity", capacity);
deba@1394
   275
    writer.writeEdgeMap("cost", cost);
deba@1394
   276
    writer.writeNode("source", s);
deba@1394
   277
    writer.writeNode("target", t);
deba@1394
   278
    writer.run();
deba@1208
   279
  }
deba@1208
   280
deba@1333
   281
  /// \brief Write a graph to the output.
deba@1333
   282
  ///
deba@1333
   283
  /// Write a graph to the output.
deba@1333
   284
  /// \param os The output stream.
deba@1333
   285
  /// \param g The graph.
deba@1333
   286
  /// \param capacity The capacity map.
deba@1333
   287
  /// \param s The source node.
deba@1333
   288
  /// \param t The target node.
deba@1297
   289
  template<typename Graph, typename CapacityMap>
deba@1208
   290
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   291
		  const CapacityMap& capacity, const typename Graph::Node &s,
deba@1208
   292
		  const typename Graph::Node &t) {
deba@1394
   293
    GraphWriter<Graph> writer(os, g);
deba@1208
   294
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1394
   295
    writer.writeNodeMap("id", nodeIdMap);
deba@1208
   296
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1394
   297
    writer.writeEdgeMap("id", edgeIdMap);
deba@1394
   298
    writer.writeEdgeMap("capacity", capacity);
deba@1394
   299
    writer.writeNode("source", s);
deba@1394
   300
    writer.writeNode("target", t);
deba@1394
   301
    writer.run();
deba@1208
   302
  }
deba@1208
   303
deba@1333
   304
  /// \brief Write a graph to the output.
deba@1333
   305
  ///
deba@1333
   306
  /// Write a graph to the output.
deba@1333
   307
  /// \param os The output stream.
deba@1333
   308
  /// \param g The graph.
deba@1333
   309
  /// \param capacity The capacity map.
deba@1333
   310
  /// \param s The source node.
deba@1208
   311
  template<typename Graph, typename CapacityMap>
deba@1208
   312
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   313
		  const CapacityMap& capacity, const typename Graph::Node &s) {
deba@1394
   314
    GraphWriter<Graph> writer(os, g);
deba@1208
   315
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1394
   316
    writer.writeNodeMap("id", nodeIdMap);
deba@1208
   317
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1394
   318
    writer.writeEdgeMap("id", edgeIdMap);
deba@1394
   319
    writer.writeEdgeMap("capacity", capacity);
deba@1394
   320
    writer.writeNode("source", s);
deba@1394
   321
    writer.run();
deba@1208
   322
  }
deba@1333
   323
deba@1333
   324
  /// \brief Write a graph to the output.
deba@1333
   325
  ///
deba@1333
   326
  /// Write a graph to the output.
deba@1333
   327
  /// \param os The output stream.
deba@1333
   328
  /// \param g The graph.
deba@1333
   329
  /// \param capacity The capacity map.
deba@1208
   330
  template<typename Graph, typename CapacityMap>
deba@1208
   331
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   332
		  const CapacityMap& capacity) {
deba@1394
   333
    GraphWriter<Graph> writer(os, g);
deba@1208
   334
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1394
   335
    writer.writeNodeMap("id", nodeIdMap);
deba@1208
   336
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1394
   337
    writer.writeEdgeMap("id", edgeIdMap);
deba@1394
   338
    writer.writeEdgeMap("capacity", capacity);
deba@1394
   339
    writer.run();
deba@1208
   340
  }
deba@1333
   341
deba@1333
   342
  /// \brief Write a graph to the output.
deba@1333
   343
  ///
deba@1333
   344
  /// Write a graph to the output.
deba@1333
   345
  /// \param os The output stream.
deba@1333
   346
  /// \param g The graph.
deba@1208
   347
  template<typename Graph>
deba@1208
   348
  void writeGraph(std::ostream& os, const Graph &g) {
deba@1394
   349
    GraphWriter<Graph> writer(os, g);
deba@1208
   350
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1394
   351
    writer.writeNodeMap("id", nodeIdMap);
deba@1208
   352
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1394
   353
    writer.writeEdgeMap("id", edgeIdMap);
deba@1394
   354
    writer.run();
deba@1208
   355
  }
deba@1208
   356
deba@1421
   357
  /// \brief The undirected graph writer class.
deba@1421
   358
  ///
deba@1421
   359
  /// The \c UndirGraphWriter class provides the undir graph output. To write 
deba@1421
   360
  /// a graph you should first give writing commands for the writer. You can 
deba@1421
   361
  /// declare write command as \c NodeMap, \c EdgeMap or \c UndirEdgeMap 
deba@1421
   362
  /// writing and labeled Node, Edge or UndirEdge writing.
deba@1421
   363
  ///
deba@1421
   364
  /// \code
deba@1421
   365
  /// UndirGraphWriter<UndirListGraph> writer(std::cout, graph);
deba@1421
   366
  /// \endcode
deba@1421
   367
  ///
deba@1421
   368
  /// The \c writeNodeMap() function declares a \c NodeMap writing 
deba@1421
   369
  /// command in the \c UndirGraphWriter. You should give as parameter 
deba@1421
   370
  /// the name of the map and the map object. The NodeMap writing 
deba@1421
   371
  /// command with name "id" should write a unique map because it 
deba@1421
   372
  /// is regarded as ID map.
deba@1421
   373
  ///
deba@1421
   374
  /// \code
deba@1421
   375
  /// IdMap<UndirListGraph, Node> nodeIdMap;
deba@1421
   376
  /// writer.writeNodeMap("id", nodeIdMap);
deba@1421
   377
  ///
deba@1421
   378
  /// writer.writeNodeMap("coords", coords);
deba@1421
   379
  /// writer.writeNodeMap("color", colorMap);
deba@1421
   380
  /// \endcode
deba@1421
   381
  ///
deba@1421
   382
  /// With the \c writeUndirEdgeMap() member function you can give an 
deba@1421
   383
  /// undirected edge map writing command similar to the NodeMaps.
deba@1421
   384
  ///
deba@1421
   385
  /// \code
deba@1421
   386
  /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
deba@1421
   387
  ///   edgeDescMap(graph);
deba@1421
   388
  /// writer.writeUndirEdgeMap("descriptor", edgeDescMap);
deba@1421
   389
  ///
deba@1421
   390
  /// writer.writeUndirEdgeMap("weight", weightMap);
deba@1421
   391
  /// writer.writeUndirEdgeMap("label", labelMap);
deba@1421
   392
  /// \endcode
deba@1421
   393
  /// 
deba@1421
   394
  /// The EdgeMap handling is just a syntactical sugar. It writes
deba@1421
   395
  /// two undirected edge map with '+' and '-' prefix in the name.
deba@1421
   396
  ///
deba@1421
   397
  /// \code
deba@1421
   398
  /// writer.writeEdgeMap("capacity", capacityMap);
deba@1421
   399
  /// \endcode
deba@1421
   400
  ///
deba@1421
   401
  ///
deba@1421
   402
  /// With \c writeNode() and \c writeUndirEdge() functions you can 
deba@1421
   403
  /// point out nodes and undirected edges in the graph. By example, you can 
deba@1421
   404
  /// write out the source and target of the graph.
deba@1421
   405
  ///
deba@1421
   406
  /// \code
deba@1421
   407
  /// writer.writeNode("source", sourceNode);
deba@1421
   408
  /// writer.writeNode("target", targetNode);
deba@1421
   409
  ///
deba@1421
   410
  /// writer.writeUndirEdge("observed", undirEdge);
deba@1421
   411
  /// \endcode
deba@1421
   412
  ///
deba@1421
   413
  /// After you give all write commands you must call the \c run() member
deba@1421
   414
  /// function, which execute all the writer commands.
deba@1421
   415
  ///
deba@1421
   416
  /// \code
deba@1421
   417
  /// writer.run();
deba@1421
   418
  /// \endcode
deba@1421
   419
  ///
deba@1421
   420
  /// \see DefaultWriterTraits
deba@1421
   421
  /// \see QuotedStringWriter
deba@1421
   422
  /// \see IdMap
deba@1421
   423
  /// \see DescriptorMap
deba@1421
   424
  /// \see \ref GraphWriter
deba@1421
   425
  /// \see \ref graph-io-page
deba@1421
   426
  /// \author Balazs Dezso
deba@1421
   427
  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
deba@1421
   428
  class UndirGraphWriter {
deba@1421
   429
  public:
deba@1421
   430
    
deba@1421
   431
    typedef _Graph Graph;
deba@1421
   432
    typedef typename Graph::Node Node;
deba@1421
   433
    typedef typename Graph::Edge Edge;
deba@1421
   434
    typedef typename Graph::UndirEdge UndirEdge;
deba@1421
   435
deba@1421
   436
    typedef _WriterTraits WriterTraits;
deba@1421
   437
deba@1421
   438
    /// \brief Construct a new UndirGraphWriter.
deba@1421
   439
    ///
deba@1421
   440
    /// Construct a new UndirGraphWriter. It writes the given graph
deba@1421
   441
    /// to the given stream.
deba@1421
   442
    UndirGraphWriter(std::ostream& _os, const Graph& _graph) 
deba@1421
   443
      : writer(new LemonWriter(_os)), own_writer(true), 
deba@1421
   444
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   445
	undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1421
   446
	node_writer(*writer, nodeset_writer, std::string()),
deba@1421
   447
	undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
deba@1421
   448
	attribute_writer(*writer, std::string()) {}
deba@1421
   449
deba@1421
   450
    /// \brief Construct a new UndirGraphWriter.
deba@1421
   451
    ///
deba@1421
   452
    /// Construct a new UndirGraphWriter. It writes into the given graph
deba@1421
   453
    /// to the given file.
deba@1421
   454
    UndirGraphWriter(const std::string& _filename, const Graph& _graph) 
deba@1421
   455
      : writer(new LemonWriter(_filename)), own_writer(true), 
deba@1421
   456
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   457
	undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1421
   458
	node_writer(*writer, nodeset_writer, std::string()),
deba@1421
   459
	undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
deba@1421
   460
	attribute_writer(*writer, std::string()) {}
deba@1421
   461
deba@1421
   462
    /// \brief Construct a new UndirGraphWriter.
deba@1421
   463
    ///
deba@1421
   464
    /// Construct a new UndirGraphWriter. It writes into the given graph
deba@1421
   465
    /// to given LemonReader.
deba@1421
   466
    UndirGraphWriter(LemonWriter& _writer, const Graph& _graph)
deba@1421
   467
      : writer(_writer), own_writer(false), 
deba@1421
   468
	nodeset_writer(*writer, _graph, std::string()),
deba@1421
   469
	undir_edgeset_writer(*writer, _graph, nodeset_writer, std::string()),
deba@1421
   470
	node_writer(*writer, nodeset_writer, std::string()),
deba@1421
   471
	undir_edge_writer(*writer, undir_edgeset_writer, std::string()),
deba@1421
   472
	attribute_writer(*writer, std::string()) {}
deba@1421
   473
deba@1421
   474
    /// \brief Destruct the graph writer.
deba@1421
   475
    ///
deba@1421
   476
    /// Destruct the graph writer.
deba@1421
   477
    ~UndirGraphWriter() {
deba@1421
   478
      if (own_writer) 
deba@1421
   479
	delete writer;
deba@1421
   480
    }
deba@1421
   481
deba@1421
   482
    /// \brief Add a new node map writer command for the writer.
deba@1421
   483
    ///
deba@1421
   484
    /// Add a new node map writer command for the writer.
deba@1421
   485
    template <typename Map>
deba@1421
   486
    UndirGraphWriter& writeNodeMap(std::string name, const Map& map) {
deba@1421
   487
      nodeset_writer.writeNodeMap(name, map);
deba@1421
   488
      return *this;
deba@1421
   489
    }
deba@1421
   490
deba@1421
   491
    /// \brief Add a new node map writer command for the writer.
deba@1421
   492
    ///
deba@1421
   493
    /// Add a new node map writer command for the writer.
deba@1421
   494
    template <typename Writer, typename Map>
deba@1421
   495
    UndirGraphWriter& writeNodeMap(std::string name, const Map& map, 
deba@1421
   496
			      const Writer& writer = Writer()) {
deba@1421
   497
      nodeset_writer.writeNodeMap(name, map, writer);
deba@1421
   498
      return *this;
deba@1421
   499
    }
deba@1421
   500
deba@1421
   501
    /// \brief Add a new edge map writer command for the writer.
deba@1421
   502
    ///
deba@1421
   503
    /// Add a new edge map writer command for the writer.
deba@1421
   504
    template <typename Map>
deba@1421
   505
    UndirGraphWriter& writeEdgeMap(std::string name, const Map& map) { 
deba@1421
   506
      undir_edgeset_writer.writeEdgeMap(name, map);
deba@1421
   507
      return *this;
deba@1421
   508
    }
deba@1421
   509
deba@1421
   510
    /// \brief Add a new edge map writer command for the writer.
deba@1421
   511
    ///
deba@1421
   512
    /// Add a new edge map writer command for the writer.
deba@1421
   513
    template <typename Writer, typename Map>
deba@1421
   514
    UndirGraphWriter& writeEdgeMap(std::string name, const Map& map,
deba@1421
   515
				   const Writer& writer = Writer()) {
deba@1421
   516
      undir_edgeset_writer.writeEdgeMap(name, map, writer);
deba@1421
   517
      return *this;
deba@1421
   518
    }
deba@1421
   519
deba@1421
   520
    /// \brief Add a new undirected edge map writer command for the writer.
deba@1421
   521
    ///
deba@1421
   522
    /// Add a new undirected edge map writer command for the writer.
deba@1421
   523
    template <typename Map>
deba@1421
   524
    UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map) { 
deba@1421
   525
      undir_edgeset_writer.writeUndirEdgeMap(name, map);
deba@1421
   526
      return *this;
deba@1421
   527
    }
deba@1421
   528
deba@1421
   529
    /// \brief Add a new undirected edge map writer command for the writer.
deba@1421
   530
    ///
deba@1421
   531
    /// Add a new edge undirected map writer command for the writer.
deba@1421
   532
    template <typename Writer, typename Map>
deba@1421
   533
    UndirGraphWriter& writeUndirEdgeMap(std::string name, const Map& map,
deba@1421
   534
					const Writer& writer = Writer()) {
deba@1421
   535
      undir_edgeset_writer.writeUndirEdgeMap(name, map, writer);
deba@1421
   536
      return *this;
deba@1421
   537
    }
deba@1421
   538
deba@1421
   539
    /// \brief Add a new labeled node writer for the writer.
deba@1421
   540
    ///
deba@1421
   541
    /// Add a new labeled node writer for the writer.
deba@1421
   542
    UndirGraphWriter& writeNode(std::string name, const Node& node) {
deba@1421
   543
      node_writer.writeNode(name, node);
deba@1421
   544
      return *this;
deba@1421
   545
    }
deba@1421
   546
deba@1421
   547
    /// \brief Add a new labeled edge writer for the writer.
deba@1421
   548
    ///
deba@1421
   549
    /// Add a new labeled edge writer for the writer.
deba@1421
   550
    UndirGraphWriter& writeUndirEdge(std::string name, const UndirEdge& edge) {
deba@1421
   551
      undir_edge_writer.writeUndirEdge(name, edge);
deba@1421
   552
    }
deba@1421
   553
deba@1421
   554
    /// \brief Add a new attribute writer command.
deba@1421
   555
    ///
deba@1421
   556
    ///  Add a new attribute writer command.
deba@1421
   557
    template <typename Value>
deba@1421
   558
    UndirGraphWriter& writeAttribute(std::string name, const Value& value) {
deba@1421
   559
      attribute_writer.writeAttribute(name, value);
deba@1421
   560
      return *this;
deba@1421
   561
    }
deba@1421
   562
    
deba@1421
   563
    /// \brief Add a new attribute writer command.
deba@1421
   564
    ///
deba@1421
   565
    ///  Add a new attribute writer command.
deba@1421
   566
    template <typename Writer, typename Value>
deba@1421
   567
    UndirGraphWriter& writeAttribute(std::string name, const Value& value, 
deba@1421
   568
			       const Writer& writer) {
deba@1421
   569
      attribute_writer.writeAttribute<Writer>(name, value, writer);
deba@1421
   570
      return *this;
deba@1421
   571
    }
deba@1421
   572
deba@1421
   573
    /// \brief Conversion operator to LemonWriter.
deba@1421
   574
    ///
deba@1421
   575
    /// Conversion operator to LemonWriter. It make possible
deba@1421
   576
    /// to access the encapsulated \e LemonWriter, this way
deba@1421
   577
    /// you can attach to this writer new instances of 
deba@1421
   578
    /// \e LemonWriter::SectionWriter.
deba@1421
   579
    operator LemonWriter&() {
deba@1421
   580
      return *writer;
deba@1421
   581
    }
deba@1421
   582
deba@1421
   583
    /// \brief Executes the writer commands.
deba@1421
   584
    ///
deba@1421
   585
    /// Executes the writer commands.
deba@1421
   586
    void run() {
deba@1421
   587
      writer->run();
deba@1421
   588
    }
deba@1421
   589
deba@1421
   590
  private:
deba@1421
   591
deba@1421
   592
    LemonWriter* writer;
deba@1421
   593
    bool own_writer;
deba@1421
   594
deba@1421
   595
    NodeSetWriter<Graph, WriterTraits> nodeset_writer;
deba@1421
   596
    UndirEdgeSetWriter<Graph, WriterTraits> undir_edgeset_writer;
deba@1421
   597
deba@1421
   598
    NodeWriter<Graph> node_writer;
deba@1421
   599
    UndirEdgeWriter<Graph> undir_edge_writer;
deba@1421
   600
    
deba@1421
   601
    AttributeWriter<WriterTraits> attribute_writer;
deba@1421
   602
  };
deba@1421
   603
deba@1421
   604
deba@1421
   605
  /// \brief Write an undirected graph to the output.
deba@1421
   606
  ///
deba@1421
   607
  /// Write an undirected graph to the output.
deba@1421
   608
  /// \param os The output stream.
deba@1421
   609
  /// \param g The graph.
deba@1421
   610
  /// \param capacity The capacity undirected map.
deba@1421
   611
  template<typename Graph, typename CapacityMap>
deba@1421
   612
  void writeUndirGraph(std::ostream& os, const Graph &g, 
deba@1421
   613
		       const CapacityMap& capacity) {
deba@1421
   614
    UndirGraphWriter<Graph> writer(os, g);
deba@1421
   615
    writer.writeUndirEdgeMap("capacity", capacity);
deba@1421
   616
    writer.run();
deba@1421
   617
  }
deba@1421
   618
deba@1421
   619
  /// \brief Write an undirected graph to the output.
deba@1421
   620
  ///
deba@1421
   621
  /// Write an undirected graph to the output.
deba@1421
   622
  /// \param os The output stream.
deba@1421
   623
  /// \param g The graph.
deba@1421
   624
  template<typename Graph>
deba@1421
   625
  void writeUndirGraph(std::ostream& os, const Graph &g) {
deba@1421
   626
    UndirGraphWriter<Graph> writer(os, g);
deba@1421
   627
    writer.run();
deba@1421
   628
  }
deba@1421
   629
deba@1333
   630
  /// @}
deba@1137
   631
deba@1137
   632
}
deba@1214
   633
deba@1214
   634
#endif