COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/deba/graph_reader.h @ 1075:789bad021e2d

Last change on this file since 1075:789bad021e2d was 1037:3eaff8d04171, checked in by Balazs Dezso, 20 years ago

graph_io under construction
This is a working version, but needs more improvments.

todo:

documention + fix the file format
improve the exception system

add some possible asserts

tutorials

File size: 15.4 KB
RevLine 
[1032]1/* -*- C++ -*-
2 * src/lemon/graph_reader.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
17///\ingroup gio
18///\file
[1036]19///\brief Graph reader.
[1032]20
21#include <iostream>
22#include <sstream>
23
24#include <map>
25#include <vector>
26
[1037]27#include <memory>
28
[1032]29#include <lemon/error.h>
30
31/// \todo fix exceptions
32
33
34namespace lemon {
[1036]35
36  // Exceptions
37
38  class IOException {
39  public:
40    virtual string what() const = 0;
41  };
42
43  class DataFormatException : public IOException {
44    std::string message;
45  public:
46    DataFormatException(const std::string& _message)
47      : message(_message) {}
48    std::string what() const {
49      return "DataFormatException: " + message;
50    }
51  };
52
[1037]53  template <typename _Exception>
54  class StreamException : public _Exception {
[1036]55  public:
[1037]56    typedef _Exception Exception;
57    StreamException(int _line, Exception _exception)
58      : line_num(_line), Exception(_exception) {}
59    virtual int line() const {
60      return line_num;
61    }
62    virtual std::string what() const {
63      ostringstream os;
64      os << Exception::what() << " in line " << line();
65      return os.str();
66    }
[1036]67  private:
68    int line_num;
69  }; 
70
71
72  // Readers and ReaderTraits
[1032]73 
74  struct DefaultReaderTraits {
75
76    template <typename _Value>
77    struct Reader {
78      typedef _Value Value;
79      void read(std::istream& is, Value& value) {
[1036]80        if (!(is >> value))
81          throw DataFormatException("Default Reader format exception");
[1032]82      }
83    };
84
[1036]85    typedef Reader<std::string> DefaultReader;
[1032]86
87  };
88
[1036]89  class QuotedStringReader {
90  public:
91    typedef std::string Value;
92
93    QuotedStringReader(bool _escaped = true) : escaped(_escaped) {}
94
95    void read(std::istream& is, std::string& value) {
96      char c;
97      value.clear();
98      is >> ws;
[1037]99      if (!is.get(c) || c != '\"') throw DataFormatException("Quoted string format");
[1036]100      while (is.get(c) && c != '\"') {
101        if (escaped && c == '\\') {
102          value += readEscape(is);
103        } else {
104          value += c;
105        }
106      }
[1037]107      if (!is) throw DataFormatException("Quoted string format");
[1036]108    }
109
110  private:
111   
112    static char readEscape(std::istream& is) {
113      char c;
114      switch (is.get(c), c) {
115      case '\\':
116        return '\\';
117      case '\"':
118        return '\"';
119      case '\'':
120        return '\'';
121      case '\?':
122        return '\?';
123      case 'a':
124        return '\a';
125      case 'b':
126        return '\b';
127      case 'f':
128        return '\f';
129      case 'n':
130        return '\n';
131      case 'r':
132        return '\r';
133      case 't':
134        return '\t';
135      case 'v':
136        return '\v';
137      case 'x':
138        {
139          int code;
140          if (!is.get(c) || !isHex(c)) throw DataFormatException("Escape format exception");
141          else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
142          else code = code * 16 + valueHex(c);
143          return code;
144        }
145      default:
146        {
147          int code;
148          if (!isOct(c)) throw DataFormatException("Escape format exception");
149          else if (code = valueOct(c), !is.get(c) || !isOct(c)) is.putback(c);
150          else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) is.putback(c);
151          else code = code * 8 + valueOct(c);
152          return code;
153        }             
154      }
155    }
156
157    static bool isOct(char c) {
158      return '0' <= c && c <='7';
159    }
160   
161    static int valueOct(char c) {
162      return c - '0';
163    }
164
165   static bool isHex(char c) {
166      return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
167    }
168   
169    static int valueHex(char c) {
170      if ('0' <= c && c <= '9') return c - '0';
171      if ('a' <= c && c <= 'z') return c - 'a' + 10;
172      return c - 'A' + 10;
173    }
174
175    bool escaped;
[1032]176  };
177
178
[1036]179
180
181
182  // Graph reader
[1032]183 
184  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits>
185  class GraphReader {
186  public:
187   
188    typedef _Graph Graph;
189    typedef typename Graph::Node Node;
190    typedef typename Graph::Edge Edge;
191
192    typedef _ReaderTraits ReaderTraits;
[1036]193    typedef typename ReaderTraits::DefaultReader DefaultReader;
[1032]194
[1037]195    GraphReader(std::istream& _is, Graph& _graph, const DefaultReader& _reader = DefaultReader())
[1036]196      : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
197
198
199    ~GraphReader() {
200
[1037]201      for (typename NodeMapReaders::iterator it = node_map_readers.begin(); it != node_map_readers.end(); ++it) {
[1036]202        delete it->second;
203      }
204
[1037]205      for (typename EdgeMapReaders::iterator it = edge_map_readers.begin(); it != edge_map_readers.end(); ++it) {
[1036]206        delete it->second;
207      }
208
209    }
[1032]210
[1037]211    // Node map rules
212
[1032]213    template <typename Map>
[1036]214    GraphReader& readNodeMap(std::string name, Map& map) {
215      return readNodeMap<typename ReaderTraits::template Reader<typename Map::Value>, Map>(name, map);
[1032]216    }
217
[1036]218    template <typename Reader, typename Map>
[1032]219    GraphReader& readNodeMap(std::string name, Map& map, const Reader& reader = Reader()) {
[1037]220      if (node_map_readers.find(name) != node_map_readers.end()) {
221        throw Exception() << "Multiple read rule for node map: " << name;
[1036]222      }
[1037]223      node_map_readers.insert(make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
[1032]224      return *this;
225    }
226
[1036]227    template <typename Reader>
228    GraphReader& skipNodeMap(std::string name, const Reader& reader = Reader()) {
[1037]229      if (node_map_readers.find(name) != node_map_readers.end()) {
230        throw Exception() << "Multiple read rule for node map: " << name;
[1036]231      }
[1037]232      node_map_readers.insert(make_pair(name, new SkipReader<Node, Reader>(reader)));
[1036]233      return *this;
[1032]234    }
235
[1037]236    // Edge map rules
237
[1036]238    template <typename Map>
239    GraphReader& readEdgeMap(std::string name, Map& map) {
240      return readEdgeMap<typename ReaderTraits::template Reader<typename Map::Value>, Map>(name, map);
241    }
242
243
244    template <typename Reader, typename Map>
[1032]245    GraphReader& readEdgeMap(std::string name, Map& map, const Reader& reader = Reader()) {
[1037]246      if (edge_map_readers.find(name) != edge_map_readers.end()) {
247        throw Exception() << "Multiple read rule for edge map: " << name;
[1036]248      }
[1037]249      edge_map_readers.insert(make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
[1032]250      return *this;
251    }
252
[1036]253    template <typename Reader>
254    GraphReader& skipEdgeMap(std::string name, const Reader& reader = Reader()) {
[1037]255      if (edge_map_readers.find(name) != edge_map_readers.end()) {
256        throw Exception() << "Multiple read rule for edge map: " << name;
257      }
258      edge_map_readers.insert(make_pair(name, new SkipReader<Edge, Reader>(reader)));
259      return *this;
260    }
261
262    // Node rules
263    GraphReader& readNode(std::string name, Node& node) {
264      if (node_readers.find(name) != node_readers.end()) {
265        throw Exception() << "Multiple read rule for node";
266      }
267      node_readers.insert(make_pair(name, &node));
268    }
269
270    // Edge rules
271
272    GraphReader& readEdge(std::string name, Edge& edge) {
[1036]273      if (edge_readers.find(name) != edge_readers.end()) {
[1037]274        throw Exception() << "Multiple read rule for edge";
[1036]275      }
[1037]276      edge_readers.insert(make_pair(name, &edge));
[1036]277    }
278
[1032]279    void read() {
280      int line_num = 0;
[1037]281      std::auto_ptr<InverterBase<Node> > nodeInverter;
282      std::auto_ptr<InverterBase<Edge> > edgeInverter;
283      try {
284        std::string line = readNotEmptyLine(is, line_num);
285        if (line.find("@nodeset") == 0) {
286          line = readNodeSet(line_num, nodeInverter);
287        }
288        if (line.find("@edgeset") == 0) {
289          line = readEdgeSet(line_num, edgeInverter, nodeInverter);
[1036]290        }
[1037]291        if (line.find("@nodes") == 0) {
292          line = readNodes(line_num, nodeInverter);
293        }
294        if (line.find("@edges") == 0) {
295          line = readEdges(line_num, edgeInverter);
296        }
297        if (line.find("@end") != 0) {
298          throw DataFormatException("Invalid control sequence: " + line);
299        }
300      } catch (DataFormatException e) {
301        throw StreamException<DataFormatException>(line_num, e);
302      }
[1032]303    }
304
305  private:
306
[1036]307    template <typename Item> class InverterBase;
308
[1037]309    std::string readNodeSet(int& line_num, auto_ptr<InverterBase<Node> > & nodeInverter) {
310      std::vector<ReaderBase<Node>* > index;
[1032]311      {
312        std::string line = readNotEmptyLine(is, line_num);   
313        std::string id;
314        std::istringstream ls(line);   
315        while (ls >> id) {
[1036]316          if (id[0] == '#') break;
[1037]317          typename NodeMapReaders::iterator it = node_map_readers.find(id);
318          if (it != node_map_readers.end()) {
[1032]319            index.push_back(it->second);
[1037]320            node_map_readers.erase(it);
[1032]321          } else {
[1036]322            index.push_back(&nodeSkipper);
[1032]323          }
324        }
325      }
[1036]326
[1037]327      if (index.size() == 0) {
328        throw DataFormatException("No node map found");
329      }
330
331      nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
[1032]332      std::string line;
333      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
334        Node node = graph.addNode();
335        std::istringstream ls(line);
[1036]336        nodeInverter->read(ls, node);
[1037]337        for (int i = 1; i < index.size(); ++i) {
[1036]338          index[i]->read(ls, node);
[1032]339        }
340      }
[1037]341      return line;
[1032]342    }
343
[1037]344    std::string readEdgeSet(int& line_num,
345                     auto_ptr<InverterBase<Edge> > & edgeInverter, auto_ptr<InverterBase<Node> > & nodeInverter) {
[1036]346      std::vector<ReaderBase<Edge>*> index;
347      {
348        std::string line = readNotEmptyLine(is, line_num);   
349        std::string id;
350        std::istringstream ls(line);   
351        while (ls >> id) {
352          if (id[0] == '#') break;
[1037]353          typename EdgeMapReaders::iterator it = edge_map_readers.find(id);
354          if (it != edge_map_readers.end()) {
[1036]355            index.push_back(it->second);
[1037]356            edge_map_readers.erase(it);
[1036]357          } else {
358            index.push_back(&edgeSkipper);
359          }
360        }
361      }
[1037]362
363      if (index.size() == 0) {
364        throw DataFormatException("No edge map found");
365      }
366
367      edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
[1036]368      std::string line;
369      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {   
370        std::istringstream ls(line);
371        Node source = nodeInverter->read(ls);
372        Node target = nodeInverter->read(ls);
373        Edge edge = graph.addEdge(source, target);
374        edgeInverter->read(ls, edge);
[1037]375        for (int i = 1; i < index.size(); ++i) {
[1036]376          index[i]->read(ls, edge);
377        }
378      }     
[1037]379      return line;
380    }
381
382    std::string readNodes(int& line_num, auto_ptr<InverterBase<Node> >& nodeInverter) {
383      std::string line;
384      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
385        std::istringstream ls(line);
386        std::string name;
387        ls >> name;
388        typename NodeReaders::iterator it = node_readers.find(name);
389        if (it != node_readers.end()) {
390          *(it -> second) = nodeInverter->read(ls);
391        }
392      }       
393      return line;
394    }
395
396    std::string readEdges(int& line_num, auto_ptr<InverterBase<Edge> >& edgeInverter) {
397      std::string line;
398      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
399        std::istringstream ls(line);
400        std::string name;
401        ls >> name;
402        typename EdgeReaders::iterator it = edge_readers.find(name);
403        if (it != edge_readers.end()) {
404          *(it -> second) = edgeInverter->read(ls);
405        }
406      }       
407      return line;   
[1032]408    }
409
410    std::string readNotEmptyLine(std::istream& is, int& line_num) {
411      std::string line;
412      while (++line_num, getline(is, line)) {   
413        int vi = line.find_first_not_of(" \t");
414        if (vi != string::npos && line[vi] != '#') {
415          return line.substr(vi);
416        }
417      }
[1037]418      throw DataFormatException("End of stream");
[1032]419    }
420   
[1037]421    // Inverters store and give back the Item from the id,
422    // and may put the ids into a map.
423   
[1036]424    template <typename _Item>
425    class InverterBase {
426    public:
427      typedef _Item Item;
[1037]428      virtual void read(std::istream&, const Item&) = 0;
429      virtual Item read(std::istream&) = 0;
[1036]430    };
[1032]431
[1036]432    template <typename _Item, typename _Map, typename _Reader>
433    class MapReaderInverter : public InverterBase<_Item> {
434    public:
435      typedef _Item Item;
436      typedef _Reader Reader;
437      typedef typename Reader::Value Value;
438      typedef _Map Map;
439      typedef std::map<Value, Item> Inverse;
440
441      Map& map;
442      Reader reader;
443      Inverse inverse;
444
445      MapReaderInverter(Map& _map, const Reader& _reader)
446        : map(_map), reader(_reader) {}
447
[1037]448      virtual void read(std::istream& is, const Item& item) {
[1036]449        Value value;
450        reader.read(is, value);
451        map.set(item, value);
452        typename Inverse::iterator it = inverse.find(value);
453        if (it == inverse.end()) {
454          inverse.insert(make_pair(value, item));
455        } else {
456          throw DataFormatException("Multiple ID occurence");
457        }
458      }
459
[1037]460      virtual Item read(std::istream& is) {
[1036]461        Value value;
462        reader.read(is, value);
463        typename Inverse::const_iterator it = inverse.find(value);
464        if (it != inverse.end()) {
465          return it->second;
466        } else {
467          throw DataFormatException("Invalid ID");
468        }
469      }     
470    };
471
472    template <typename _Item, typename _Reader>
473    class SkipReaderInverter : public InverterBase<_Item> {
474    public:
475      typedef _Item Item;
476      typedef _Reader Reader;
477      typedef typename Reader::Value Value;
478      typedef std::map<Value, Item> Inverse;
479
480      Reader reader;
481
482      SkipReaderInverter(const Reader& _reader)
483        : reader(_reader) {}
484
[1037]485      virtual void read(std::istream& is, const Item& item) {
[1036]486        Value value;
487        reader.read(is, value);
488        typename Inverse::iterator it = inverse.find(value);
489        if (it == inverse.end()) {
490          inverse.insert(make_pair(value, item));
491        } else {
492          throw DataFormatException("Multiple ID occurence");
493        }
494      }
495
[1037]496      virtual Item read(std::istream& is) {
[1036]497        Value value;
498        reader.read(is, value);
499        typename Inverse::const_iterator it = inverse.find(value);
500        if (it != inverse.end()) {
501          return it->second;
502        } else {
503          throw DataFormatException("Invalid ID");
504        }
505      }     
506    private:
507      Inverse inverse;
508    };
509
[1037]510    // Readers
[1032]511
512    template <typename _Item>   
[1036]513    class ReaderBase {
[1032]514    public:
515      typedef _Item Item;
[1037]516
517      virtual void read(std::istream& is, const Item& item) = 0;
[1036]518      virtual InverterBase<_Item>* getInverter() = 0;
[1032]519    };
[1036]520
[1032]521    template <typename _Item, typename _Map, typename _Reader>
[1036]522    class MapReader : public ReaderBase<_Item> {
[1032]523    public:
524      typedef _Map Map;
525      typedef _Reader Reader;
[1036]526      typedef typename Reader::Value Value;
[1032]527      typedef _Item Item;
528     
529      Map& map;
530      Reader reader;
531
532      MapReader(Map& _map, const Reader& _reader)
533        : map(_map), reader(_reader) {}
534
535
[1037]536      virtual void read(std::istream& is, const Item& item) {
[1036]537        Value value;
[1032]538        reader.read(is, value);
539        map.set(item, value);
540      }
[1036]541
542      virtual InverterBase<_Item>* getInverter() {
543        return new MapReaderInverter<Item, Map, Reader>(map, reader);
544      }
[1032]545    };
546
[1036]547
548    template <typename _Item, typename _Reader>
549    class SkipReader : public ReaderBase<_Item> {
550    public:
551      typedef _Reader Reader;
552      typedef typename Reader::Value Value;
553      typedef _Item Item;
554
555      Reader reader;
556      SkipReader(const Reader& _reader) : reader(_reader) {}
557
[1037]558      virtual void read(std::istream& is, const Item& item) {
[1036]559        Value value;
560        reader.read(is, value);
561      }     
562
563      virtual InverterBase<Item>* getInverter() {
564        return new SkipReaderInverter<Item, Reader>(reader);
565      }
566    };
567
568
[1037]569    typedef std::map<std::string, ReaderBase<Node>*> NodeMapReaders;
570    NodeMapReaders node_map_readers;
571
572    typedef std::map<std::string, ReaderBase<Edge>*> EdgeMapReaders;
573    EdgeMapReaders edge_map_readers;
574
575    typedef std::map<std::string, Node*> NodeReaders;
[1032]576    NodeReaders node_readers;
577
[1037]578    typedef std::map<std::string, Edge*> EdgeReaders;
[1032]579    EdgeReaders edge_readers;
580
[1036]581    std::istream& is;
582    Graph& graph;
583
584    SkipReader<Node, DefaultReader> nodeSkipper;
585    SkipReader<Edge, DefaultReader> edgeSkipper;
586
[1032]587  };
588
589}
Note: See TracBrowser for help on using the repository browser.