src/lemon/graph_reader.h
author alpar
Thu, 19 May 2005 11:46:42 +0000
changeset 1428 9ba88ddc629c
parent 1408 892c29484414
child 1429 4283998fb2be
permissions -rw-r--r--
A very simple xml parser
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 * src/lemon/graph_reader.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 reader.
deba@1137
    20
deba@1214
    21
#ifndef LEMON_GRAPH_READER_H
deba@1214
    22
#define LEMON_GRAPH_READER_H
deba@1214
    23
deba@1137
    24
#include <iostream>
deba@1137
    25
deba@1137
    26
#include <lemon/error.h>
deba@1408
    27
#include <lemon/lemon_reader.h>
deba@1137
    28
deba@1137
    29
namespace lemon {
deba@1137
    30
deba@1333
    31
  /// \addtogroup io_group
deba@1333
    32
  /// @{
deba@1137
    33
deba@1137
    34
  /// \brief The graph reader class.
deba@1137
    35
  ///
deba@1333
    36
  /// The given file format may contain several maps and labeled nodes or 
deba@1333
    37
  /// edges.
deba@1333
    38
  ///
deba@1333
    39
  /// If you read a graph you need not read all the maps and items just those
deba@1333
    40
  /// that you need. The interface of the \c GraphReader is very similar to
deba@1333
    41
  /// the GraphWriter but the reading method does not depend on the order the
deba@1333
    42
  /// given commands.
deba@1333
    43
  ///
deba@1333
    44
  /// The reader object suppose that each not readed value does not contain 
deba@1333
    45
  /// whitespaces, therefore it has some extra possibilities to control how
deba@1333
    46
  /// it should skip the values when the string representation contains spaces.
deba@1333
    47
  ///
deba@1333
    48
  /// \code
deba@1333
    49
  /// GraphReader<ListGraph> reader(std::cin, graph);
deba@1333
    50
  /// \endcode
deba@1333
    51
  ///
deba@1394
    52
  /// The \c readNodeMap() function reads a map from the \c \@nodeset section.
deba@1333
    53
  /// If there is a map that you do not want to read from the file and there is
deba@1333
    54
  /// whitespace in the string represenation of the values then you should
deba@1333
    55
  /// call the \c skipNodeMap() template member function with proper 
deba@1333
    56
  /// parameters.
deba@1333
    57
  ///
deba@1333
    58
  /// \code
deba@1421
    59
  /// reader.readNodeMap("coords", coords);
deba@1333
    60
  ///
deba@1394
    61
  /// reader.readNodeMap<QuotedStringReader>("label", labelMap);
deba@1333
    62
  /// reader.skipNodeMap<QuotedStringReader>("description");
deba@1333
    63
  ///
deba@1394
    64
  /// reader.readNodeMap("color", colorMap);
deba@1333
    65
  /// \endcode
deba@1333
    66
  ///
deba@1394
    67
  /// With the \c readEdgeMap() member function you can give an edge map
deba@1333
    68
  /// reading command similar to the NodeMaps. 
deba@1333
    69
  ///
deba@1333
    70
  /// \code
deba@1394
    71
  /// reader.readEdgeMap("weight", weightMap);
deba@1394
    72
  /// reader.readEdgeMap("label", labelMap);
deba@1333
    73
  /// \endcode
deba@1333
    74
  ///
deba@1408
    75
  /// With \c readNode() and \c readEdge() functions you can read 
deba@1408
    76
  /// labeled Nodes and Edges.
deba@1333
    77
  ///
deba@1333
    78
  /// \code
deba@1394
    79
  /// reader.readNode("source", sourceNode);
deba@1394
    80
  /// reader.readNode("target", targetNode);
deba@1333
    81
  ///
deba@1394
    82
  /// reader.readEdge("observed", edge);
deba@1333
    83
  /// \endcode
deba@1333
    84
  ///
deba@1408
    85
  /// With the \c readAttribute() functions you can read an attribute
deba@1408
    86
  /// in a variable. You can specify the reader for the attribute as
deba@1408
    87
  /// the nodemaps.
deba@1408
    88
  ///
deba@1333
    89
  /// After you give all read commands you must call the \c run() member
deba@1333
    90
  /// function, which execute all the commands.
deba@1333
    91
  ///
deba@1333
    92
  /// \code
deba@1333
    93
  /// reader.run();
deba@1333
    94
  /// \endcode
deba@1333
    95
  ///
alpar@1287
    96
  /// \see DefaultReaderTraits
alpar@1287
    97
  /// \see QuotedStringReader
alpar@1138
    98
  /// \see \ref GraphWriter
alpar@1138
    99
  /// \see \ref graph-io-page
deba@1333
   100
  /// \author Balazs Dezso
deba@1137
   101
  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
deba@1137
   102
  class GraphReader {
deba@1137
   103
  public:
deba@1137
   104
    
deba@1137
   105
    typedef _Graph Graph;
deba@1137
   106
    typedef typename Graph::Node Node;
deba@1137
   107
    typedef typename Graph::Edge Edge;
deba@1137
   108
deba@1137
   109
    typedef _ReaderTraits ReaderTraits;
deba@1408
   110
    typedef typename ReaderTraits::Skipper DefaultSkipper;
deba@1137
   111
deba@1137
   112
    /// \brief Construct a new GraphReader.
deba@1137
   113
    ///
deba@1208
   114
    /// Construct a new GraphReader. It reads into the given graph
deba@1208
   115
    /// and it use the given reader as the default skipper.
deba@1421
   116
    GraphReader(std::istream& _is, 
deba@1421
   117
		typename SmartParameter<Graph>::Type _graph, 
deba@1408
   118
		const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   119
      : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
deba@1421
   120
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   121
	edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   122
		       std::string(), skipper),
deba@1408
   123
	node_reader(*reader, nodeset_reader, std::string()),
deba@1408
   124
	edge_reader(*reader, edgeset_reader, std::string()),
deba@1408
   125
	attribute_reader(*reader, std::string()) {}
deba@1408
   126
deba@1408
   127
    /// \brief Construct a new GraphReader.
deba@1408
   128
    ///
deba@1408
   129
    /// Construct a new GraphReader. It reads into the given graph
deba@1408
   130
    /// and it use the given reader as the default skipper.
deba@1421
   131
    GraphReader(const std::string& _filename, 
deba@1421
   132
		typename SmartParameter<Graph>::Type _graph, 
deba@1408
   133
		const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1408
   134
      : reader(new LemonReader(_filename)), own_reader(true), 
deba@1421
   135
	skipper(_skipper),
deba@1421
   136
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   137
	edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   138
		       std::string(), skipper),
deba@1408
   139
	node_reader(*reader, nodeset_reader, std::string()),
deba@1408
   140
	edge_reader(*reader, edgeset_reader, std::string()),
deba@1408
   141
	attribute_reader(*reader, std::string()) {}
deba@1408
   142
deba@1408
   143
    /// \brief Construct a new GraphReader.
deba@1408
   144
    ///
deba@1408
   145
    /// Construct a new GraphReader. It reads into the given graph
deba@1408
   146
    /// and it use the given reader as the default skipper.
deba@1421
   147
    GraphReader(LemonReader& _reader, 
deba@1421
   148
		typename SmartParameter<Graph>::Type _graph, 
deba@1408
   149
		const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   150
      : reader(_reader), own_reader(false), skipper(_skipper),
deba@1421
   151
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   152
	edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   153
		       std::string(), skipper),
deba@1408
   154
	node_reader(*reader, nodeset_reader, std::string()),
deba@1408
   155
	edge_reader(*reader, edgeset_reader, std::string()),
deba@1408
   156
	attribute_reader(*reader, std::string()) {}
deba@1137
   157
deba@1137
   158
    /// \brief Destruct the graph reader.
deba@1137
   159
    ///
deba@1137
   160
    /// Destruct the graph reader.
deba@1137
   161
    ~GraphReader() {
deba@1408
   162
      if (own_reader) 
deba@1408
   163
	delete reader;
deba@1137
   164
    }
deba@1137
   165
deba@1137
   166
    /// \brief Add a new node map reader command for the reader.
deba@1137
   167
    ///
deba@1137
   168
    /// Add a new node map reader command for the reader.
deba@1137
   169
    template <typename Map>
deba@1394
   170
    GraphReader& readNodeMap(std::string name, Map& map) {
deba@1421
   171
      nodeset_reader.readNodeMap(name, map);
deba@1421
   172
      return *this;
deba@1421
   173
    }
deba@1421
   174
deba@1421
   175
    template <typename Map>
deba@1421
   176
    GraphReader& readNodeMap(std::string name, const Map& map) {
deba@1421
   177
      nodeset_reader.readNodeMap(name, map);
deba@1408
   178
      return *this;
deba@1137
   179
    }
deba@1137
   180
deba@1137
   181
    /// \brief Add a new node map reader command for the reader.
deba@1137
   182
    ///
deba@1137
   183
    /// Add a new node map reader command for the reader.
deba@1137
   184
    template <typename Reader, typename Map>
deba@1394
   185
    GraphReader& readNodeMap(std::string name, Map& map, 
deba@1137
   186
			     const Reader& reader = Reader()) {
deba@1421
   187
      nodeset_reader.readNodeMap(name, map, reader);
deba@1421
   188
      return *this;
deba@1421
   189
    }
deba@1421
   190
deba@1421
   191
    template <typename Reader, typename Map>
deba@1421
   192
    GraphReader& readNodeMap(std::string name, const Map& map, 
deba@1421
   193
			     const Reader& reader = Reader()) {
deba@1421
   194
      nodeset_reader.readNodeMap(name, map, reader);
deba@1137
   195
      return *this;
deba@1137
   196
    }
deba@1137
   197
deba@1137
   198
    /// \brief Add a new node map skipper command for the reader.
deba@1137
   199
    ///
deba@1137
   200
    /// Add a new node map skipper command for the reader.
deba@1137
   201
    template <typename Reader>
deba@1137
   202
    GraphReader& skipNodeMap(std::string name, 
deba@1137
   203
			     const Reader& reader = Reader()) {
deba@1421
   204
      nodeset_reader.skipNodeMap(name, reader);
deba@1137
   205
      return *this;
deba@1137
   206
    }
deba@1137
   207
deba@1137
   208
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   209
    ///
deba@1137
   210
    /// Add a new edge map reader command for the reader.
deba@1137
   211
    template <typename Map>
deba@1394
   212
    GraphReader& readEdgeMap(std::string name, Map& map) { 
deba@1421
   213
      edgeset_reader.readEdgeMap(name, map);
deba@1421
   214
      return *this;
deba@1421
   215
    }
deba@1421
   216
deba@1421
   217
    template <typename Map>
deba@1421
   218
    GraphReader& readEdgeMap(std::string name, const Map& map) { 
deba@1421
   219
      edgeset_reader.readEdgeMap(name, map);
deba@1408
   220
      return *this;
deba@1137
   221
    }
deba@1137
   222
deba@1137
   223
deba@1137
   224
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   225
    ///
deba@1137
   226
    /// Add a new edge map reader command for the reader.
deba@1137
   227
    template <typename Reader, typename Map>
deba@1394
   228
    GraphReader& readEdgeMap(std::string name, Map& map,
deba@1137
   229
			     const Reader& reader = Reader()) {
deba@1421
   230
      edgeset_reader.readEdgeMap(name, map, reader);
deba@1421
   231
      return *this;
deba@1421
   232
    }
deba@1421
   233
deba@1421
   234
    template <typename Reader, typename Map>
deba@1421
   235
    GraphReader& readEdgeMap(std::string name, const Map& map,
deba@1421
   236
			     const Reader& reader = Reader()) {
deba@1421
   237
      edgeset_reader.readEdgeMap(name, map, reader);
deba@1137
   238
      return *this;
deba@1137
   239
    }
deba@1137
   240
deba@1137
   241
    /// \brief Add a new edge map skipper command for the reader.
deba@1137
   242
    ///
deba@1137
   243
    /// Add a new edge map skipper command for the reader.
deba@1137
   244
    template <typename Reader>
deba@1421
   245
    GraphReader& skipEdgeMap(std::string name, 
deba@1137
   246
			     const Reader& reader = Reader()) {
deba@1421
   247
      edgeset_reader.skipEdgeMap(name, reader);
deba@1137
   248
      return *this;
deba@1137
   249
    }
deba@1137
   250
deba@1137
   251
    /// \brief Add a new labeled node reader for the reader.
deba@1137
   252
    ///
deba@1137
   253
    /// Add a new labeled node reader for the reader.
deba@1394
   254
    GraphReader& readNode(std::string name, Node& node) {
deba@1408
   255
      node_reader.readNode(name, node);
deba@1137
   256
      return *this;
deba@1137
   257
    }
deba@1137
   258
deba@1137
   259
    /// \brief Add a new labeled edge reader for the reader.
deba@1137
   260
    ///
deba@1137
   261
    /// Add a new labeled edge reader for the reader.
deba@1394
   262
    GraphReader& readEdge(std::string name, Edge& edge) {
deba@1408
   263
      edge_reader.readEdge(name, edge);
deba@1408
   264
    }
deba@1408
   265
deba@1408
   266
    /// \brief Add a new attribute reader command.
deba@1408
   267
    ///
deba@1408
   268
    ///  Add a new attribute reader command.
deba@1408
   269
    template <typename Value>
deba@1408
   270
    GraphReader& readAttribute(std::string name, Value& value) {
deba@1408
   271
      attribute_reader.readAttribute(name, value);
deba@1137
   272
      return *this;
deba@1137
   273
    }
deba@1408
   274
    
deba@1408
   275
    /// \brief Add a new attribute reader command.
deba@1408
   276
    ///
deba@1408
   277
    ///  Add a new attribute reader command.
deba@1408
   278
    template <typename Reader, typename Value>
deba@1408
   279
    GraphReader& readAttribute(std::string name, Value& value, 
deba@1408
   280
			       const Reader& reader) {
deba@1408
   281
      attribute_reader.readAttribute<Reader>(name, value, reader);
deba@1408
   282
      return *this;
deba@1408
   283
    }
deba@1408
   284
deba@1408
   285
    /// \brief Conversion operator to LemonReader.
deba@1408
   286
    ///
deba@1408
   287
    /// Conversion operator to LemonReader. It make possible
deba@1408
   288
    /// to access the encapsulated \e LemonReader, this way
deba@1408
   289
    /// you can attach to this reader new instances of 
deba@1408
   290
    /// \e LemonReader::SectionReader.
deba@1408
   291
    operator LemonReader&() {
deba@1408
   292
      return *reader;
deba@1408
   293
    }
deba@1137
   294
deba@1137
   295
    /// \brief Executes the reader commands.
deba@1137
   296
    ///
deba@1137
   297
    /// Executes the reader commands.
deba@1137
   298
    void run() {
deba@1408
   299
      reader->run();
deba@1396
   300
    }
deba@1396
   301
deba@1137
   302
  private:
deba@1137
   303
deba@1408
   304
    LemonReader* reader;
deba@1408
   305
    bool own_reader;
deba@1137
   306
deba@1408
   307
    DefaultSkipper skipper;
deba@1137
   308
deba@1408
   309
    NodeSetReader<Graph, ReaderTraits> nodeset_reader;
deba@1408
   310
    EdgeSetReader<Graph, ReaderTraits> edgeset_reader;
deba@1408
   311
deba@1408
   312
    NodeReader<Graph> node_reader;
deba@1408
   313
    EdgeReader<Graph> edge_reader;
deba@1408
   314
    
deba@1408
   315
    AttributeReader<ReaderTraits> attribute_reader;
deba@1137
   316
  };
deba@1137
   317
deba@1333
   318
  /// \brief Read a graph from the input.
deba@1333
   319
  ///
deba@1333
   320
  /// Read a graph from the input.
deba@1333
   321
  /// \param is The input stream.
deba@1333
   322
  /// \param g The graph.
deba@1333
   323
  /// \param capacity The capacity map.
deba@1333
   324
  /// \param s The source node.
deba@1333
   325
  /// \param t The target node.
deba@1333
   326
  /// \param cost The cost map.
deba@1208
   327
  template<typename Graph, typename CapacityMap, typename CostMap>
deba@1208
   328
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   329
		  typename Graph::Node &s, typename Graph::Node &t, 
deba@1208
   330
		  CostMap& cost) {
deba@1208
   331
    GraphReader<Graph> reader(is, g);
deba@1394
   332
    reader.readEdgeMap("capacity", capacity);
deba@1394
   333
    reader.readEdgeMap("cost", cost);
deba@1394
   334
    reader.readNode("source", s);
deba@1394
   335
    reader.readNode("target", t);
deba@1208
   336
    reader.run();
deba@1208
   337
  }
deba@1208
   338
deba@1333
   339
  /// \brief Read a graph from the input.
deba@1333
   340
  ///
deba@1333
   341
  /// Read a graph from the input.
deba@1333
   342
  /// \param is The input stream.
deba@1333
   343
  /// \param g The graph.
deba@1333
   344
  /// \param capacity The capacity map.
deba@1333
   345
  /// \param s The source node.
deba@1333
   346
  /// \param t The target node.
deba@1208
   347
  template<typename Graph, typename CapacityMap>
deba@1208
   348
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   349
		  typename Graph::Node &s, typename Graph::Node &t) {
deba@1208
   350
    GraphReader<Graph> reader(is, g);
deba@1394
   351
    reader.readEdgeMap("capacity", capacity);
deba@1394
   352
    reader.readNode("source", s);
deba@1394
   353
    reader.readNode("target", t);
deba@1208
   354
    reader.run();
deba@1208
   355
  }
deba@1208
   356
deba@1333
   357
  /// \brief Read a graph from the input.
deba@1333
   358
  ///
deba@1333
   359
  /// Read a graph from the input.
deba@1333
   360
  /// \param is The input stream.
deba@1333
   361
  /// \param g The graph.
deba@1333
   362
  /// \param capacity The capacity map.
deba@1333
   363
  /// \param s The source node.
deba@1208
   364
  template<typename Graph, typename CapacityMap>
deba@1208
   365
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, 
deba@1208
   366
		  typename Graph::Node &s) {
deba@1208
   367
    GraphReader<Graph> reader(is, g);
deba@1394
   368
    reader.readEdgeMap("capacity", capacity);
deba@1394
   369
    reader.readNode("source", s);
deba@1208
   370
    reader.run();
deba@1208
   371
  }
deba@1208
   372
deba@1333
   373
  /// \brief Read a graph from the input.
deba@1333
   374
  ///
deba@1333
   375
  /// Read a graph from the input.
deba@1333
   376
  /// \param is The input stream.
deba@1333
   377
  /// \param g The graph.
deba@1333
   378
  /// \param capacity The capacity map.
deba@1208
   379
  template<typename Graph, typename CapacityMap>
deba@1208
   380
  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
deba@1208
   381
    GraphReader<Graph> reader(is, g);
deba@1394
   382
    reader.readEdgeMap("capacity", capacity);
deba@1208
   383
    reader.run();
deba@1208
   384
  }
deba@1208
   385
deba@1333
   386
  /// \brief Read a graph from the input.
deba@1333
   387
  ///
deba@1333
   388
  /// Read a graph from the input.
deba@1333
   389
  /// \param is The input stream.
deba@1333
   390
  /// \param g The graph.
deba@1208
   391
  template<typename Graph>
deba@1208
   392
  void readGraph(std::istream& is, Graph &g) {
deba@1208
   393
    GraphReader<Graph> reader(is, g);
deba@1208
   394
    reader.run();
deba@1208
   395
  }
deba@1208
   396
deba@1421
   397
  /// \brief The undir graph reader class.
deba@1421
   398
  ///
deba@1421
   399
  /// The given file format may contain several maps and labeled nodes or 
deba@1421
   400
  /// edges.
deba@1421
   401
  ///
deba@1421
   402
  /// If you read a graph you need not read all the maps and items just those
deba@1421
   403
  /// that you need. The interface of the \c GraphReader is very similar to
deba@1421
   404
  /// the GraphWriter but the reading method does not depend on the order the
deba@1421
   405
  /// given commands.
deba@1421
   406
  ///
deba@1421
   407
  /// The reader object suppose that each not readed value does not contain 
deba@1421
   408
  /// whitespaces, therefore it has some extra possibilities to control how
deba@1421
   409
  /// it should skip the values when the string representation contains spaces.
deba@1421
   410
  ///
deba@1421
   411
  /// \code
deba@1421
   412
  /// UndirGraphReader<UndirListGraph> reader(std::cin, graph);
deba@1421
   413
  /// \endcode
deba@1421
   414
  ///
deba@1421
   415
  /// The \c readNodeMap() function reads a map from the \c \@nodeset section.
deba@1421
   416
  /// If there is a map that you do not want to read from the file and there is
deba@1421
   417
  /// whitespace in the string represenation of the values then you should
deba@1421
   418
  /// call the \c skipNodeMap() template member function with proper 
deba@1421
   419
  /// parameters.
deba@1421
   420
  ///
deba@1421
   421
  /// \code
deba@1421
   422
  /// reader.readNodeMap("coords", coords);
deba@1421
   423
  ///
deba@1421
   424
  /// reader.readNodeMap<QuotedStringReader>("label", labelMap);
deba@1421
   425
  /// reader.skipNodeMap<QuotedStringReader>("description");
deba@1421
   426
  ///
deba@1421
   427
  /// reader.readNodeMap("color", colorMap);
deba@1421
   428
  /// \endcode
deba@1421
   429
  ///
deba@1421
   430
  /// With the \c readUndirEdgeMap() member function you can give an 
deba@1421
   431
  /// undir edge map reading command similar to the NodeMaps. 
deba@1421
   432
  ///
deba@1421
   433
  /// \code
deba@1421
   434
  /// reader.readUndirEdgeMap("capacity", capacityMap);
deba@1421
   435
  /// \endcode
deba@1421
   436
  ///
deba@1421
   437
  /// The reading of the directed edge maps is just a syntactical sugar.
deba@1421
   438
  /// It reads two undirected edgemaps into a directed edge map. The 
deba@1421
   439
  /// undirected edge maps' name should be start with the \c '+' and the
deba@1421
   440
  /// \c '-' character and the same.
deba@1421
   441
  ///
deba@1421
   442
  /// \code
deba@1421
   443
  /// reader.readEdgeMap("flow", flowMap);
deba@1421
   444
  /// \endcode 
deba@1421
   445
  ///
deba@1421
   446
  /// With \c readNode() and \c readUndirEdge() functions you can read 
deba@1421
   447
  /// labeled Nodes and UndirEdges.
deba@1421
   448
  ///
deba@1421
   449
  /// \code
deba@1421
   450
  /// reader.readNode("source", sourceNode);
deba@1421
   451
  /// reader.readNode("target", targetNode);
deba@1421
   452
  ///
deba@1421
   453
  /// reader.readUndirEdge("observed", undirEdge);
deba@1421
   454
  /// \endcode
deba@1421
   455
  ///
deba@1421
   456
  /// With the \c readAttribute() functions you can read an attribute
deba@1421
   457
  /// in a variable. You can specify the reader for the attribute as
deba@1421
   458
  /// the nodemaps.
deba@1421
   459
  ///
deba@1421
   460
  /// After you give all read commands you must call the \c run() member
deba@1421
   461
  /// function, which execute all the commands.
deba@1421
   462
  ///
deba@1421
   463
  /// \code
deba@1421
   464
  /// reader.run();
deba@1421
   465
  /// \endcode
deba@1421
   466
  ///
deba@1421
   467
  /// \see GraphReader
deba@1421
   468
  /// \see DefaultReaderTraits
deba@1421
   469
  /// \see \ref UndirGraphWriter
deba@1421
   470
  /// \see \ref graph-io-page
deba@1421
   471
  ///
deba@1421
   472
  /// \author Balazs Dezso
deba@1421
   473
  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
deba@1421
   474
  class UndirGraphReader {
deba@1421
   475
  public:
deba@1421
   476
    
deba@1421
   477
    typedef _Graph Graph;
deba@1421
   478
    typedef typename Graph::Node Node;
deba@1421
   479
    typedef typename Graph::Edge Edge;
deba@1421
   480
    typedef typename Graph::UndirEdge UndirEdge;
deba@1421
   481
deba@1421
   482
    typedef _ReaderTraits ReaderTraits;
deba@1421
   483
    typedef typename ReaderTraits::Skipper DefaultSkipper;
deba@1421
   484
deba@1421
   485
    /// \brief Construct a new UndirGraphReader.
deba@1421
   486
    ///
deba@1421
   487
    /// Construct a new UndirGraphReader. It reads into the given graph
deba@1421
   488
    /// and it use the given reader as the default skipper.
deba@1421
   489
    UndirGraphReader(std::istream& _is, Graph& _graph, 
deba@1421
   490
		     const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   491
      : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
deba@1421
   492
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   493
	undir_edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   494
			     std::string(), skipper),
deba@1421
   495
	node_reader(*reader, nodeset_reader, std::string()),
deba@1421
   496
	undir_edge_reader(*reader, undir_edgeset_reader, std::string()),
deba@1421
   497
	attribute_reader(*reader, std::string()) {}
deba@1421
   498
deba@1421
   499
    /// \brief Construct a new UndirGraphReader.
deba@1421
   500
    ///
deba@1421
   501
    /// Construct a new UndirGraphReader. It reads into the given graph
deba@1421
   502
    /// and it use the given reader as the default skipper.
deba@1421
   503
    UndirGraphReader(const std::string& _filename, Graph& _graph, 
deba@1421
   504
		     const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   505
      : reader(new LemonReader(_filename)), own_reader(true), 
deba@1421
   506
	skipper(_skipper),
deba@1421
   507
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   508
	undir_edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   509
			     std::string(), skipper),
deba@1421
   510
	node_reader(*reader, nodeset_reader, std::string()),
deba@1421
   511
	undir_edge_reader(*reader, undir_edgeset_reader, std::string()),
deba@1421
   512
	attribute_reader(*reader, std::string()) {}
deba@1421
   513
deba@1421
   514
    /// \brief Construct a new UndirGraphReader.
deba@1421
   515
    ///
deba@1421
   516
    /// Construct a new UndirGraphReader. It reads into the given graph
deba@1421
   517
    /// and it use the given reader as the default skipper.
deba@1421
   518
    UndirGraphReader(LemonReader& _reader, Graph& _graph, 
deba@1421
   519
		     const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
   520
      : reader(_reader), own_reader(false), skipper(_skipper),
deba@1421
   521
	nodeset_reader(*reader, _graph, std::string(), skipper),
deba@1421
   522
	undir_edgeset_reader(*reader, _graph, nodeset_reader, 
deba@1421
   523
			     std::string(), skipper),
deba@1421
   524
	node_reader(*reader, nodeset_reader, std::string()),
deba@1421
   525
	undir_edge_reader(*reader, undir_edgeset_reader, std::string()),
deba@1421
   526
	attribute_reader(*reader, std::string()) {}
deba@1421
   527
deba@1421
   528
    /// \brief Destruct the graph reader.
deba@1421
   529
    ///
deba@1421
   530
    /// Destruct the graph reader.
deba@1421
   531
    ~UndirGraphReader() {
deba@1421
   532
      if (own_reader) 
deba@1421
   533
	delete reader;
deba@1421
   534
    }
deba@1421
   535
deba@1421
   536
    /// \brief Add a new node map reader command for the reader.
deba@1421
   537
    ///
deba@1421
   538
    /// Add a new node map reader command for the reader.
deba@1421
   539
    template <typename Map>
deba@1421
   540
    UndirGraphReader& readNodeMap(std::string name, Map& map) {
deba@1421
   541
      nodeset_reader.readNodeMap(name, map);
deba@1421
   542
      return *this;
deba@1421
   543
    }
deba@1421
   544
deba@1421
   545
    template <typename Map>
deba@1421
   546
    UndirGraphReader& readNodeMap(std::string name, const Map& map) {
deba@1421
   547
      nodeset_reader.readNodeMap(name, map);
deba@1421
   548
      return *this;
deba@1421
   549
    }
deba@1421
   550
deba@1421
   551
    /// \brief Add a new node map reader command for the reader.
deba@1421
   552
    ///
deba@1421
   553
    /// Add a new node map reader command for the reader.
deba@1421
   554
    template <typename Reader, typename Map>
deba@1421
   555
    UndirGraphReader& readNodeMap(std::string name, Map& map, 
deba@1421
   556
				  const Reader& reader = Reader()) {
deba@1421
   557
      nodeset_reader.readNodeMap(name, map, reader);
deba@1421
   558
      return *this;
deba@1421
   559
    }
deba@1421
   560
deba@1421
   561
    template <typename Reader, typename Map>
deba@1421
   562
    UndirGraphReader& readNodeMap(std::string name, const Map& map, 
deba@1421
   563
				  const Reader& reader = Reader()) {
deba@1421
   564
      nodeset_reader.readNodeMap(name, map, reader);
deba@1421
   565
      return *this;
deba@1421
   566
    }
deba@1421
   567
deba@1421
   568
    /// \brief Add a new node map skipper command for the reader.
deba@1421
   569
    ///
deba@1421
   570
    /// Add a new node map skipper command for the reader.
deba@1421
   571
    template <typename Reader>
deba@1421
   572
    UndirGraphReader& skipNodeMap(std::string name, 
deba@1421
   573
			     const Reader& reader = Reader()) {
deba@1421
   574
      nodeset_reader.skipNodeMap(name, reader);
deba@1421
   575
      return *this;
deba@1421
   576
    }
deba@1421
   577
deba@1421
   578
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
   579
    ///
deba@1421
   580
    /// Add a new undirected edge map reader command for the reader.
deba@1421
   581
    template <typename Map>
deba@1421
   582
    UndirGraphReader& readUndirEdgeMap(std::string name, Map& map) { 
deba@1421
   583
      undir_edgeset_reader.readUndirEdgeMap(name, map);
deba@1421
   584
      return *this;
deba@1421
   585
    }
deba@1421
   586
deba@1421
   587
    template <typename Map>
deba@1421
   588
    UndirGraphReader& readUndirEdgeMap(std::string name, const Map& map) { 
deba@1421
   589
      undir_edgeset_reader.readUndirEdgeMap(name, map);
deba@1421
   590
      return *this;
deba@1421
   591
    }
deba@1421
   592
deba@1421
   593
deba@1421
   594
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
   595
    ///
deba@1421
   596
    /// Add a new undirected edge map reader command for the reader.
deba@1421
   597
    template <typename Reader, typename Map>
deba@1421
   598
    UndirGraphReader& readUndirEdgeMap(std::string name, Map& map,
deba@1421
   599
				       const Reader& reader = Reader()) {
deba@1421
   600
      undir_edgeset_reader.readUndirEdgeMap(name, map, reader);
deba@1421
   601
      return *this;
deba@1421
   602
    }
deba@1421
   603
deba@1421
   604
    template <typename Reader, typename Map>
deba@1421
   605
    UndirGraphReader& readUndirEdgeMap(std::string name, const Map& map,
deba@1421
   606
				       const Reader& reader = Reader()) {
deba@1421
   607
      undir_edgeset_reader.readUndirEdgeMap(name, map, reader);
deba@1421
   608
      return *this;
deba@1421
   609
    }
deba@1421
   610
deba@1421
   611
    /// \brief Add a new undirected edge map skipper command for the reader.
deba@1421
   612
    ///
deba@1421
   613
    /// Add a new undirected edge map skipper command for the reader.
deba@1421
   614
    template <typename Reader>
deba@1421
   615
    UndirGraphReader& skipUndirEdgeMap(std::string name,
deba@1421
   616
				       const Reader& reader = Reader()) {
deba@1421
   617
      undir_edgeset_reader.skipUndirMap(name, reader);
deba@1421
   618
      return *this;
deba@1421
   619
    }
deba@1421
   620
deba@1421
   621
deba@1421
   622
    /// \brief Add a new edge map reader command for the reader.
deba@1421
   623
    ///
deba@1421
   624
    /// Add a new edge map reader command for the reader.
deba@1421
   625
    template <typename Map>
deba@1421
   626
    UndirGraphReader& readEdgeMap(std::string name, Map& map) { 
deba@1421
   627
      undir_edgeset_reader.readEdgeMap(name, map);
deba@1421
   628
      return *this;
deba@1421
   629
    }
deba@1421
   630
deba@1421
   631
    template <typename Map>
deba@1421
   632
    UndirGraphReader& readEdgeMap(std::string name, const Map& map) { 
deba@1421
   633
      undir_edgeset_reader.readEdgeMap(name, map);
deba@1421
   634
      return *this;
deba@1421
   635
    }
deba@1421
   636
deba@1421
   637
deba@1421
   638
    /// \brief Add a new edge map reader command for the reader.
deba@1421
   639
    ///
deba@1421
   640
    /// Add a new edge map reader command for the reader.
deba@1421
   641
    template <typename Reader, typename Map>
deba@1421
   642
    UndirGraphReader& readEdgeMap(std::string name, Map& map,
deba@1421
   643
				       const Reader& reader = Reader()) {
deba@1421
   644
      undir_edgeset_reader.readEdgeMap(name, map, reader);
deba@1421
   645
      return *this;
deba@1421
   646
    }
deba@1421
   647
deba@1421
   648
    template <typename Reader, typename Map>
deba@1421
   649
    UndirGraphReader& readEdgeMap(std::string name, const Map& map,
deba@1421
   650
				       const Reader& reader = Reader()) {
deba@1421
   651
      undir_edgeset_reader.readEdgeMap(name, map, reader);
deba@1421
   652
      return *this;
deba@1421
   653
    }
deba@1421
   654
deba@1421
   655
    /// \brief Add a new edge map skipper command for the reader.
deba@1421
   656
    ///
deba@1421
   657
    /// Add a new edge map skipper command for the reader.
deba@1421
   658
    template <typename Reader>
deba@1421
   659
    UndirGraphReader& skipEdgeMap(std::string name,
deba@1421
   660
				       const Reader& reader = Reader()) {
deba@1421
   661
      undir_edgeset_reader.skipEdgeMap(name, reader);
deba@1421
   662
      return *this;
deba@1421
   663
    }
deba@1421
   664
deba@1421
   665
    /// \brief Add a new labeled node reader for the reader.
deba@1421
   666
    ///
deba@1421
   667
    /// Add a new labeled node reader for the reader.
deba@1421
   668
    UndirGraphReader& readNode(std::string name, Node& node) {
deba@1421
   669
      node_reader.readNode(name, node);
deba@1421
   670
      return *this;
deba@1421
   671
    }
deba@1421
   672
deba@1421
   673
    /// \brief Add a new labeled edge reader for the reader.
deba@1421
   674
    ///
deba@1421
   675
    /// Add a new labeled edge reader for the reader.
deba@1421
   676
    UndirGraphReader& readUndirEdge(std::string name, UndirEdge& edge) {
deba@1421
   677
      undir_edge_reader.readUndirEdge(name, edge);
deba@1421
   678
    }
deba@1421
   679
deba@1421
   680
    /// \brief Add a new attribute reader command.
deba@1421
   681
    ///
deba@1421
   682
    ///  Add a new attribute reader command.
deba@1421
   683
    template <typename Value>
deba@1421
   684
    UndirGraphReader& readAttribute(std::string name, Value& value) {
deba@1421
   685
      attribute_reader.readAttribute(name, value);
deba@1421
   686
      return *this;
deba@1421
   687
    }
deba@1421
   688
    
deba@1421
   689
    /// \brief Add a new attribute reader command.
deba@1421
   690
    ///
deba@1421
   691
    ///  Add a new attribute reader command.
deba@1421
   692
    template <typename Reader, typename Value>
deba@1421
   693
    UndirGraphReader& readAttribute(std::string name, Value& value, 
deba@1421
   694
			       const Reader& reader) {
deba@1421
   695
      attribute_reader.readAttribute<Reader>(name, value, reader);
deba@1421
   696
      return *this;
deba@1421
   697
    }
deba@1421
   698
deba@1421
   699
    /// \brief Conversion operator to LemonReader.
deba@1421
   700
    ///
deba@1421
   701
    /// Conversion operator to LemonReader. It make possible
deba@1421
   702
    /// to access the encapsulated \e LemonReader, this way
deba@1421
   703
    /// you can attach to this reader new instances of 
deba@1421
   704
    /// \e LemonReader::SectionReader.
deba@1421
   705
    operator LemonReader&() {
deba@1421
   706
      return *reader;
deba@1421
   707
    }
deba@1421
   708
deba@1421
   709
    /// \brief Executes the reader commands.
deba@1421
   710
    ///
deba@1421
   711
    /// Executes the reader commands.
deba@1421
   712
    void run() {
deba@1421
   713
      reader->run();
deba@1421
   714
    }
deba@1421
   715
deba@1421
   716
  private:
deba@1421
   717
deba@1421
   718
    LemonReader* reader;
deba@1421
   719
    bool own_reader;
deba@1421
   720
deba@1421
   721
    DefaultSkipper skipper;
deba@1421
   722
deba@1421
   723
    NodeSetReader<Graph, ReaderTraits> nodeset_reader;
deba@1421
   724
    UndirEdgeSetReader<Graph, ReaderTraits> undir_edgeset_reader;
deba@1421
   725
deba@1421
   726
    NodeReader<Graph> node_reader;
deba@1421
   727
    UndirEdgeReader<Graph> undir_edge_reader;
deba@1421
   728
    
deba@1421
   729
    AttributeReader<ReaderTraits> attribute_reader;
deba@1421
   730
  };
deba@1421
   731
deba@1421
   732
  /// \brief Read an undir graph from the input.
deba@1421
   733
  ///
deba@1421
   734
  /// Read an undir graph from the input.
deba@1421
   735
  /// \param is The input stream.
deba@1421
   736
  /// \param g The graph.
deba@1421
   737
  /// \param capacity The capacity map.
deba@1421
   738
  template<typename Graph, typename CapacityMap>
deba@1421
   739
  void readUndirGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
deba@1421
   740
    UndirGraphReader<Graph> reader(is, g);
deba@1421
   741
    reader.readUndirEdgeMap("capacity", capacity);
deba@1421
   742
    reader.run();
deba@1421
   743
  }
deba@1421
   744
deba@1421
   745
  /// \brief Read an undir graph from the input.
deba@1421
   746
  ///
deba@1421
   747
  /// Read an undir graph from the input.
deba@1421
   748
  /// \param is The input stream.
deba@1421
   749
  /// \param g The graph.
deba@1421
   750
  template<typename Graph>
deba@1421
   751
  void readUndirGraph(std::istream& is, Graph &g) {
deba@1421
   752
    UndirGraphReader<Graph> reader(is, g);
deba@1421
   753
    reader.run();
deba@1421
   754
  }
deba@1421
   755
deba@1333
   756
  /// @}
deba@1137
   757
}
deba@1214
   758
deba@1214
   759
#endif