COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/graph_writer.h @ 1410:dcfad73b3965

Last change on this file since 1410:dcfad73b3965 was 1410:dcfad73b3965, checked in by Balazs Dezso, 19 years ago

Bug fixes.

File size: 11.6 KB
Line 
1/* -*- C++ -*-
2 * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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 io_group
18///\file
19///\brief Lemon Graph Format writer.
20
21#ifndef LEMON_GRAPH_WRITER_H
22#define LEMON_GRAPH_WRITER_H
23
24#include <iostream>
25
26#include <lemon/error.h>
27#include <lemon/lemon_writer.h>
28
29namespace lemon {
30
31  /// \addtogroup io_group
32  /// @{
33
34  /// \brief The graph writer class.
35  ///
36  /// The \c GraphWriter class provides the graph output. To write a graph
37  /// you should first give writing commands for the writer. You can declare
38  /// write command as \c NodeMap or \c EdgeMap writing and labeled Node and
39  /// Edge writing.
40  ///
41  /// \code
42  /// GraphWriter<ListGraph> writer(std::cout, graph);
43  /// \endcode
44  ///
45  /// The \c writeNodeMap() function declares a \c NodeMap writing
46  /// command in the \c GraphWriter. You should give as parameter
47  /// the name of the map and the map object. The NodeMap writing
48  /// command with name "id" should write a unique map because it
49  /// is regarded as ID map.
50  ///
51  /// \code
52  /// IdMap<ListGraph, Node> nodeIdMap;
53  /// writer.writeNodeMap("id", nodeIdMap);
54  ///
55  /// writer.writeNodeMap("x-coord", xCoordMap);
56  /// writer.writeNodeMap("y-coord", yCoordMap);
57  /// writer.writeNodeMap("color", colorMap);
58  /// \endcode
59  ///
60  /// With the \c writeEdgeMap() member function you can give an edge map
61  /// writing command similar to the NodeMaps.
62  ///
63  /// \code
64  /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> >
65  ///   edgeDescMap(graph);
66  /// writer.writeEdgeMap("descriptor", edgeDescMap);
67  ///
68  /// writer.writeEdgeMap("weight", weightMap);
69  /// writer.writeEdgeMap("label", labelMap);
70  /// \endcode
71  ///
72  /// With \c writeNode() and \c writeEdge() functions you can
73  /// point out Nodes and Edges in the graph. By example, you can
74  /// write out the source and target of the graph.
75  ///
76  /// \code
77  /// writer.writeNode("source", sourceNode);
78  /// writer.writeNode("target", targetNode);
79  ///
80  /// writer.writeEdge("observed", edge);
81  /// \endcode
82  ///
83  /// After you give all write commands you must call the \c run() member
84  /// function, which execute all the writer commands.
85  ///
86  /// \code
87  /// writer.run();
88  /// \endcode
89  ///
90  /// \see DefaultWriterTraits
91  /// \see QuotedStringWriter
92  /// \see IdMap
93  /// \see DescriptorMap
94  /// \see \ref GraphWriter
95  /// \see \ref graph-io-page
96  /// \author Balazs Dezso
97  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits>
98  class GraphWriter {
99  public:
100   
101    typedef _Graph Graph;
102    typedef typename Graph::Node Node;
103    typedef typename Graph::Edge Edge;
104
105    typedef _WriterTraits WriterTraits;
106
107    /// \brief Construct a new GraphWriter.
108    ///
109    /// Construct a new GraphWriter. It writes the given graph
110    /// to the given stream.
111    GraphWriter(std::ostream& _os, const Graph& _graph)
112      : writer(new LemonWriter(_os)), own_writer(true),
113        graph(_graph),
114        nodeset_writer(*writer, graph, std::string()),
115        edgeset_writer(*writer, graph, nodeset_writer, std::string()),
116        node_writer(*writer, nodeset_writer, std::string()),
117        edge_writer(*writer, edgeset_writer, std::string()),
118        attribute_writer(*writer, std::string()) {}
119
120    /// \brief Construct a new GraphWriter.
121    ///
122    /// Construct a new GraphWriter. It writes into the given graph
123    /// to the given file.
124    GraphWriter(const std::string& _filename, const Graph& _graph)
125      : writer(new LemonWriter(_filename)), own_writer(true),
126        graph(_graph),
127        nodeset_writer(*writer, graph, std::string()),
128        edgeset_writer(*writer, graph, nodeset_writer, std::string()),
129        node_writer(*writer, nodeset_writer, std::string()),
130        edge_writer(*writer, edgeset_writer, std::string()),
131        attribute_writer(*writer, std::string()) {}
132
133    /// \brief Construct a new GraphWriter.
134    ///
135    /// Construct a new GraphWriter. It writes into the given graph
136    /// to given LemonReader.
137    GraphWriter(LemonWriter& _writer, const Graph& _graph)
138      : writer(_writer), own_writer(false),
139        graph(_graph),
140        nodeset_writer(*writer, graph, std::string()),
141        edgeset_writer(*writer, graph, nodeset_writer, std::string()),
142        node_writer(*writer, nodeset_writer, std::string()),
143        edge_writer(*writer, edgeset_writer, std::string()),
144        attribute_writer(*writer, std::string()) {}
145
146    /// \brief Destruct the graph writer.
147    ///
148    /// Destruct the graph writer.
149    ~GraphWriter() {
150      if (own_writer)
151        delete writer;
152    }
153
154    /// \brief Add a new node map writer command for the writer.
155    ///
156    /// Add a new node map writer command for the writer.
157    template <typename Map>
158    GraphWriter& writeNodeMap(std::string name, const Map& map) {
159      nodeset_writer.writeMap(name, map);
160      return *this;
161    }
162
163    /// \brief Add a new node map writer command for the writer.
164    ///
165    /// Add a new node map writer command for the writer.
166    template <typename Writer, typename Map>
167    GraphWriter& writeNodeMap(std::string name, const Map& map,
168                             const Writer& writer = Writer()) {
169      nodeset_writer.writeMap(name, map, writer);
170      return *this;
171    }
172
173
174    /// \brief Add a new edge map writer command for the writer.
175    ///
176    /// Add a new edge map writer command for the writer.
177    template <typename Map>
178    GraphWriter& writeEdgeMap(std::string name, const Map& map) {
179      edgeset_writer.writeMap(name, map);
180      return *this;
181    }
182
183
184    /// \brief Add a new edge map writer command for the writer.
185    ///
186    /// Add a new edge map writer command for the writer.
187    template <typename Writer, typename Map>
188    GraphWriter& writeEdgeMap(std::string name, const Map& map,
189                             const Writer& writer = Writer()) {
190      edgeset_writer.writeMap(name, map, writer);
191      return *this;
192    }
193
194    /// \brief Add a new labeled node writer for the writer.
195    ///
196    /// Add a new labeled node writer for the writer.
197    GraphWriter& writeNode(std::string name, const Node& node) {
198      node_writer.writeNode(name, node);
199      return *this;
200    }
201
202    /// \brief Add a new labeled edge writer for the writer.
203    ///
204    /// Add a new labeled edge writer for the writer.
205    GraphWriter& writeEdge(std::string name, const Edge& edge) {
206      edge_writer.writeEdge(name, edge);
207    }
208
209    /// \brief Add a new attribute writer command.
210    ///
211    ///  Add a new attribute writer command.
212    template <typename Value>
213    GraphWriter& writeAttribute(std::string name, const Value& value) {
214      attribute_writer.writeAttribute(name, value);
215      return *this;
216    }
217   
218    /// \brief Add a new attribute writer command.
219    ///
220    ///  Add a new attribute writer command.
221    template <typename Writer, typename Value>
222    GraphWriter& writeAttribute(std::string name, const Value& value,
223                               const Writer& writer) {
224      attribute_writer.writeAttribute<Writer>(name, value, writer);
225      return *this;
226    }
227
228    /// \brief Conversion operator to LemonWriter.
229    ///
230    /// Conversion operator to LemonWriter. It make possible
231    /// to access the encapsulated \e LemonWriter, this way
232    /// you can attach to this writer new instances of
233    /// \e LemonWriter::SectionWriter.
234    operator LemonWriter&() {
235      return *writer;
236    }
237
238    /// \brief Executes the writer commands.
239    ///
240    /// Executes the writer commands.
241    void run() {
242      writer->run();
243    }
244
245  private:
246
247    LemonWriter* writer;
248    bool own_writer;
249
250    const Graph& graph;
251
252    NodeSetWriter<Graph, WriterTraits> nodeset_writer;
253    EdgeSetWriter<Graph, WriterTraits> edgeset_writer;
254
255    NodeWriter<Graph> node_writer;
256    EdgeWriter<Graph> edge_writer;
257   
258    AttributeWriter<WriterTraits> attribute_writer;
259  };
260
261
262  /// \brief Write a graph to the output.
263  ///
264  /// Write a graph to the output.
265  /// \param os The output stream.
266  /// \param g The graph.
267  /// \param capacity The capacity map.
268  /// \param s The source node.
269  /// \param t The target node.
270  /// \param cost The cost map.
271  template<typename Graph, typename CapacityMap, typename CostMap>
272  void writeGraph(std::ostream& os, const Graph &g,
273                  const CapacityMap& capacity, const typename Graph::Node &s,
274                  const typename Graph::Node &t, const CostMap& cost) {
275    GraphWriter<Graph> writer(os, g);
276    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
277    writer.writeNodeMap("id", nodeIdMap);
278    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
279    writer.writeEdgeMap("id", edgeIdMap);
280    writer.writeEdgeMap("capacity", capacity);
281    writer.writeEdgeMap("cost", cost);
282    writer.writeNode("source", s);
283    writer.writeNode("target", t);
284    writer.run();
285  }
286
287  /// \brief Write a graph to the output.
288  ///
289  /// Write a graph to the output.
290  /// \param os The output stream.
291  /// \param g The graph.
292  /// \param capacity The capacity map.
293  /// \param s The source node.
294  /// \param t The target node.
295  template<typename Graph, typename CapacityMap>
296  void writeGraph(std::ostream& os, const Graph &g,
297                  const CapacityMap& capacity, const typename Graph::Node &s,
298                  const typename Graph::Node &t) {
299    GraphWriter<Graph> writer(os, g);
300    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
301    writer.writeNodeMap("id", nodeIdMap);
302    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
303    writer.writeEdgeMap("id", edgeIdMap);
304    writer.writeEdgeMap("capacity", capacity);
305    writer.writeNode("source", s);
306    writer.writeNode("target", t);
307    writer.run();
308  }
309
310  /// \brief Write a graph to the output.
311  ///
312  /// Write a graph to the output.
313  /// \param os The output stream.
314  /// \param g The graph.
315  /// \param capacity The capacity map.
316  /// \param s The source node.
317  template<typename Graph, typename CapacityMap>
318  void writeGraph(std::ostream& os, const Graph &g,
319                  const CapacityMap& capacity, const typename Graph::Node &s) {
320    GraphWriter<Graph> writer(os, g);
321    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
322    writer.writeNodeMap("id", nodeIdMap);
323    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
324    writer.writeEdgeMap("id", edgeIdMap);
325    writer.writeEdgeMap("capacity", capacity);
326    writer.writeNode("source", s);
327    writer.run();
328  }
329
330  /// \brief Write a graph to the output.
331  ///
332  /// Write a graph to the output.
333  /// \param os The output stream.
334  /// \param g The graph.
335  /// \param capacity The capacity map.
336  template<typename Graph, typename CapacityMap>
337  void writeGraph(std::ostream& os, const Graph &g,
338                  const CapacityMap& capacity) {
339    GraphWriter<Graph> writer(os, g);
340    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
341    writer.writeNodeMap("id", nodeIdMap);
342    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
343    writer.writeEdgeMap("id", edgeIdMap);
344    writer.writeEdgeMap("capacity", capacity);
345    writer.run();
346  }
347
348  /// \brief Write a graph to the output.
349  ///
350  /// Write a graph to the output.
351  /// \param os The output stream.
352  /// \param g The graph.
353  template<typename Graph>
354  void writeGraph(std::ostream& os, const Graph &g) {
355    GraphWriter<Graph> writer(os, g);
356    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
357    writer.writeNodeMap("id", nodeIdMap);
358    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
359    writer.writeEdgeMap("id", edgeIdMap);
360    writer.run();
361  }
362
363  /// @}
364
365}
366
367#endif
Note: See TracBrowser for help on using the repository browser.