[1137] | 1 | /* -*- C++ -*- |
---|
| 2 | * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library |
---|
| 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[1137] | 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 | |
---|
[1287] | 17 | ///\ingroup io_group |
---|
[1137] | 18 | ///\file |
---|
[1287] | 19 | ///\brief Lemon Graph Format writer. |
---|
[1137] | 20 | |
---|
[1214] | 21 | #ifndef LEMON_GRAPH_WRITER_H |
---|
| 22 | #define LEMON_GRAPH_WRITER_H |
---|
[1137] | 23 | |
---|
| 24 | #include <iostream> |
---|
| 25 | |
---|
| 26 | #include <lemon/error.h> |
---|
[1409] | 27 | #include <lemon/lemon_writer.h> |
---|
[1137] | 28 | |
---|
| 29 | namespace lemon { |
---|
| 30 | |
---|
[1333] | 31 | /// \addtogroup io_group |
---|
| 32 | /// @{ |
---|
| 33 | |
---|
[1137] | 34 | /// \brief The graph writer class. |
---|
| 35 | /// |
---|
[1333] | 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 | /// |
---|
[1394] | 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. |
---|
[1333] | 50 | /// |
---|
| 51 | /// \code |
---|
| 52 | /// IdMap<ListGraph, Node> nodeIdMap; |
---|
[1394] | 53 | /// writer.writeNodeMap("id", nodeIdMap); |
---|
[1333] | 54 | /// |
---|
[1394] | 55 | /// writer.writeNodeMap("x-coord", xCoordMap); |
---|
| 56 | /// writer.writeNodeMap("y-coord", yCoordMap); |
---|
| 57 | /// writer.writeNodeMap("color", colorMap); |
---|
[1333] | 58 | /// \endcode |
---|
| 59 | /// |
---|
[1394] | 60 | /// With the \c writeEdgeMap() member function you can give an edge map |
---|
[1333] | 61 | /// writing command similar to the NodeMaps. |
---|
| 62 | /// |
---|
| 63 | /// \code |
---|
| 64 | /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > |
---|
| 65 | /// edgeDescMap(graph); |
---|
[1394] | 66 | /// writer.writeEdgeMap("descriptor", edgeDescMap); |
---|
[1333] | 67 | /// |
---|
[1394] | 68 | /// writer.writeEdgeMap("weight", weightMap); |
---|
| 69 | /// writer.writeEdgeMap("label", labelMap); |
---|
[1333] | 70 | /// \endcode |
---|
| 71 | /// |
---|
[1394] | 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. |
---|
[1333] | 75 | /// |
---|
| 76 | /// \code |
---|
[1394] | 77 | /// writer.writeNode("source", sourceNode); |
---|
| 78 | /// writer.writeNode("target", targetNode); |
---|
[1333] | 79 | /// |
---|
[1394] | 80 | /// writer.writeEdge("observed", edge); |
---|
[1333] | 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 | /// |
---|
[1287] | 90 | /// \see DefaultWriterTraits |
---|
| 91 | /// \see QuotedStringWriter |
---|
[1333] | 92 | /// \see IdMap |
---|
| 93 | /// \see DescriptorMap |
---|
[1394] | 94 | /// \see \ref GraphWriter |
---|
[1138] | 95 | /// \see \ref graph-io-page |
---|
[1333] | 96 | /// \author Balazs Dezso |
---|
[1137] | 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; |
---|
[1409] | 106 | |
---|
[1137] | 107 | /// \brief Construct a new GraphWriter. |
---|
| 108 | /// |
---|
[1409] | 109 | /// Construct a new GraphWriter. It writes the given graph |
---|
| 110 | /// to the given stream. |
---|
[1208] | 111 | GraphWriter(std::ostream& _os, const Graph& _graph) |
---|
[1409] | 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()) {} |
---|
[1137] | 119 | |
---|
[1409] | 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(), skipper), |
---|
| 128 | edgeset_writer(*writer, graph, nodeset_writer, std::string(), skipper), |
---|
| 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()) {} |
---|
[1137] | 145 | |
---|
| 146 | /// \brief Destruct the graph writer. |
---|
| 147 | /// |
---|
| 148 | /// Destruct the graph writer. |
---|
| 149 | ~GraphWriter() { |
---|
[1409] | 150 | if (own_writer) |
---|
| 151 | delete writer; |
---|
[1137] | 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> |
---|
[1394] | 158 | GraphWriter& writeNodeMap(std::string name, const Map& map) { |
---|
[1409] | 159 | nodeset_writer.writeMap(name, map); |
---|
| 160 | return *this; |
---|
[1137] | 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> |
---|
[1394] | 167 | GraphWriter& writeNodeMap(std::string name, const Map& map, |
---|
[1409] | 168 | const Writer& writer = Writer()) { |
---|
| 169 | nodeset_writer.writeMap(name, map, writer); |
---|
[1137] | 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> |
---|
[1394] | 178 | GraphWriter& writeEdgeMap(std::string name, const Map& map) { |
---|
[1409] | 179 | edgeset_writer.writeMap(name, map); |
---|
| 180 | return *this; |
---|
[1137] | 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> |
---|
[1409] | 188 | GraphWriter& writeEdgeMap(std::string name, const Map& map, |
---|
| 189 | const Writer& writer = Writer()) { |
---|
| 190 | edgeset_writer.writeMap(name, map, writer); |
---|
[1137] | 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. |
---|
[1394] | 197 | GraphWriter& writeNode(std::string name, const Node& node) { |
---|
[1409] | 198 | node_writer.writeNode(name, node); |
---|
[1137] | 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. |
---|
[1394] | 205 | GraphWriter& writeEdge(std::string name, const Edge& edge) { |
---|
[1409] | 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); |
---|
[1137] | 225 | return *this; |
---|
| 226 | } |
---|
| 227 | |
---|
[1409] | 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; |
---|
[1396] | 236 | } |
---|
| 237 | |
---|
[1137] | 238 | /// \brief Executes the writer commands. |
---|
| 239 | /// |
---|
| 240 | /// Executes the writer commands. |
---|
[1409] | 241 | void run() { |
---|
| 242 | writer->run(); |
---|
[1137] | 243 | } |
---|
| 244 | |
---|
| 245 | private: |
---|
| 246 | |
---|
[1409] | 247 | LemonWriter* writer; |
---|
| 248 | bool own_writer; |
---|
[1137] | 249 | |
---|
[1208] | 250 | const Graph& graph; |
---|
[1137] | 251 | |
---|
[1409] | 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; |
---|
[1137] | 259 | }; |
---|
| 260 | |
---|
[1409] | 261 | |
---|
[1333] | 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. |
---|
[1208] | 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) { |
---|
[1394] | 275 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 276 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 277 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 278 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 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(); |
---|
[1208] | 285 | } |
---|
| 286 | |
---|
[1333] | 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. |
---|
[1297] | 295 | template<typename Graph, typename CapacityMap> |
---|
[1208] | 296 | void writeGraph(std::ostream& os, const Graph &g, |
---|
| 297 | const CapacityMap& capacity, const typename Graph::Node &s, |
---|
| 298 | const typename Graph::Node &t) { |
---|
[1394] | 299 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 300 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 301 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 302 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 303 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 304 | writer.writeEdgeMap("capacity", capacity); |
---|
| 305 | writer.writeNode("source", s); |
---|
| 306 | writer.writeNode("target", t); |
---|
| 307 | writer.run(); |
---|
[1208] | 308 | } |
---|
| 309 | |
---|
[1333] | 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. |
---|
[1208] | 317 | template<typename Graph, typename CapacityMap> |
---|
| 318 | void writeGraph(std::ostream& os, const Graph &g, |
---|
| 319 | const CapacityMap& capacity, const typename Graph::Node &s) { |
---|
[1394] | 320 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 321 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 322 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 323 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 324 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 325 | writer.writeEdgeMap("capacity", capacity); |
---|
| 326 | writer.writeNode("source", s); |
---|
| 327 | writer.run(); |
---|
[1208] | 328 | } |
---|
[1333] | 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. |
---|
[1208] | 336 | template<typename Graph, typename CapacityMap> |
---|
| 337 | void writeGraph(std::ostream& os, const Graph &g, |
---|
| 338 | const CapacityMap& capacity) { |
---|
[1394] | 339 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 340 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 341 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 342 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 343 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 344 | writer.writeEdgeMap("capacity", capacity); |
---|
| 345 | writer.run(); |
---|
[1208] | 346 | } |
---|
[1333] | 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. |
---|
[1208] | 353 | template<typename Graph> |
---|
| 354 | void writeGraph(std::ostream& os, const Graph &g) { |
---|
[1394] | 355 | GraphWriter<Graph> writer(os, g); |
---|
[1208] | 356 | IdMap<Graph, typename Graph::Node> nodeIdMap(g); |
---|
[1394] | 357 | writer.writeNodeMap("id", nodeIdMap); |
---|
[1208] | 358 | IdMap<Graph, typename Graph::Edge> edgeIdMap(g); |
---|
[1394] | 359 | writer.writeEdgeMap("id", edgeIdMap); |
---|
| 360 | writer.run(); |
---|
[1208] | 361 | } |
---|
| 362 | |
---|
[1333] | 363 | /// @} |
---|
[1137] | 364 | |
---|
| 365 | } |
---|
[1214] | 366 | |
---|
| 367 | #endif |
---|