src/work/deba/graph_writer.h
changeset 1119 d3504fc075dc
parent 1037 3eaff8d04171
child 1133 9fd485470fee
equal deleted inserted replaced
0:0b89fa58c5ed 1:43821f31297c
    52     typedef std::string Value;
    52     typedef std::string Value;
    53 
    53 
    54     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
    54     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
    55 
    55 
    56     void write(std::ostream& os, const std::string& value) {
    56     void write(std::ostream& os, const std::string& value) {
    57       char c;
       
    58       os << "\"";
    57       os << "\"";
    59       if (escaped) {
    58       if (escaped) {
    60 	ostringstream ls;
    59 	ostringstream ls;
    61 	for (int i = 0; i < value.size(); ++i) {
    60 	for (int i = 0; i < (int)value.size(); ++i) {
    62 	  writeEscape(ls, value[i]);
    61 	  writeEscape(ls, value[i]);
    63 	}
    62 	}
    64 	os << ls.str();
    63 	os << ls.str();
    65       } else {
    64       } else {
    66 	os << value;
    65 	os << value;
   134 
   133 
   135     GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
   134     GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
   136 
   135 
   137 
   136 
   138     ~GraphWriter() {
   137     ~GraphWriter() {
   139 
   138       for (typename NodeMapWriters::iterator it = node_map_writers.begin(); 
   140       for (typename NodeMapWriters::iterator it = node_map_writers.begin(); it != node_map_writers.end(); ++it) {
   139 	   it != node_map_writers.end(); ++it) {
   141 	delete it->second;
   140 	delete it->second;
   142       }
   141       }
   143 
   142 
   144       for (typename EdgeMapWriters::iterator it = edge_map_writers.begin(); it != edge_map_writers.end(); ++it) {
   143       for (typename EdgeMapWriters::iterator it = edge_map_writers.begin();
       
   144 	   it != edge_map_writers.end(); ++it) {
   145 	delete it->second;
   145 	delete it->second;
   146       }
   146       }
   147 
   147 
   148     }
   148     }
   149 
   149 
   150     // Node map rules
   150     // Node map rules
   151 
   151 
   152     template <typename Map>
   152     template <typename Map>
   153     GraphWriter& writeNodeMap(std::string name, const Map& map) {
   153     GraphWriter& addNodeMap(std::string name, const Map& map) {
   154       return writeNodeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map);
   154       return addNodeMap<typename WriterTraits::template Writer<
       
   155 	typename Map::Value>, Map>(name, map);
   155     }
   156     }
   156 
   157 
   157     template <typename Writer, typename Map>
   158     template <typename Writer, typename Map>
   158     GraphWriter& writeNodeMap(std::string name, const Map& map, const Writer& writer = Writer()) {
   159     GraphWriter& addNodeMap(std::string name, const Map& map, 
       
   160 			      const Writer& writer = Writer()) {
   159       //      if (node_map_writers.find(name) != node_map_writers.end()) {
   161       //      if (node_map_writers.find(name) != node_map_writers.end()) {
   160       //	throw Exception() << "Multiple write rule for node map: " << name;
   162       //	throw Exception() << "Multiple write rule for node map: " 
       
   163       //        << name;
   161       //      }
   164       //      }
   162       node_map_writers.push_back(make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
   165       node_map_writers.push_back(
       
   166         make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
   163       return *this;
   167       return *this;
   164     }
   168     }
   165 
   169 
   166     // Edge map rules
   170     // Edge map rules
   167 
   171 
   168     template <typename Map>
   172     template <typename Map>
   169     GraphWriter& writeEdgeMap(std::string name, const Map& map) { 
   173     GraphWriter& addEdgeMap(std::string name, const Map& map) { 
   170       return writeEdgeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map);
   174       return addEdgeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map);
   171     }
   175     }
   172 
   176 
   173 
   177 
   174     template <typename Writer, typename Map>
   178     template <typename Writer, typename Map>
   175     GraphWriter& writeEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) {
   179     GraphWriter& addEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) {
   176       //      if (edge_map_writers.find(name) != edge_map_writers.end()) {
   180       //      if (edge_map_writers.find(name) != edge_map_writers.end()) {
   177       //	throw Exception() << "Multiple write rule for edge map: " << name;
   181       //	throw Exception() << "Multiple write rule for edge map: " << name;
   178       //      }
   182       //      }
   179       edge_map_writers.push_back(make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer)));
   183       edge_map_writers.push_back(make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer)));
   180       return *this;
   184       return *this;
   181     }
   185     }
   182 
   186 
   183     // Node rules
   187     // Node rules
   184     GraphWriter& writeNode(std::string name, const Node& node) {
   188     GraphWriter& addNode(std::string name, const Node& node) {
   185       //      if (node_writers.find(name) != node_writers.end()) {
   189       //      if (node_writers.find(name) != node_writers.end()) {
   186       //	throw Exception() << "Multiple write rule for node";
   190       //	throw Exception() << "Multiple write rule for node";
   187       //      }
   191       //      }
   188       node_writers.push_back(make_pair(name, node));
   192       node_writers.push_back(make_pair(name, node));
       
   193       return *this;
   189     }
   194     }
   190 
   195 
   191     // Edge rules
   196     // Edge rules
   192 
   197 
   193     GraphWriter& writeEdge(std::string name, const Edge& edge) {
   198     GraphWriter& addEdge(std::string name, const Edge& edge) {
   194       //      if (edge_writers.find(name) != edge_writers.end()) {
   199       //      if (edge_writers.find(name) != edge_writers.end()) {
   195       //	throw Exception() << "Multiple write rule for edge";
   200       //	throw Exception() << "Multiple write rule for edge";
   196       //      }
   201       //      }
   197       edge_writers.push_back(make_pair(name, edge));
   202       edge_writers.push_back(make_pair(name, edge));
       
   203       return *this;
   198     }
   204     }
   199 
   205 
   200     void write() {   
   206     void write() {   
   201       writeNodeSet();
   207       writeNodeSet();
   202       writeEdgeSet();
   208       writeEdgeSet();
   208   private:
   214   private:
   209 
   215 
   210     void writeNodeSet() {
   216     void writeNodeSet() {
   211       if (node_map_writers.size() == 0) return;
   217       if (node_map_writers.size() == 0) return;
   212       os << "@nodeset" << std::endl;
   218       os << "@nodeset" << std::endl;
   213       for (int i = 0; i < node_map_writers.size(); ++i) {
   219       for (int i = 0; i < (int)node_map_writers.size(); ++i) {
   214 	os << node_map_writers[i].first << '\t';
   220 	os << node_map_writers[i].first << '\t';
   215       } 
   221       } 
   216       os << std::endl;
   222       os << std::endl;
   217       for (NodeIt it(graph); it != INVALID; ++it) {
   223       for (NodeIt it(graph); it != INVALID; ++it) {
   218 	for (int i = 0; i < node_map_writers.size(); ++i) {
   224 	for (int i = 0; i < (int)node_map_writers.size(); ++i) {
   219 	  node_map_writers[i].second->write(os, it);
   225 	  node_map_writers[i].second->write(os, it);
   220 	}
   226 	}
   221 	os << std::endl;
   227 	os << std::endl;
   222       }
   228       }
   223 
   229 
   228       if (node_map_writers.size() == 0) {
   234       if (node_map_writers.size() == 0) {
   229 	throw Exception() << "Missing node id map";
   235 	throw Exception() << "Missing node id map";
   230       }
   236       }
   231       os << "@edgeset" << std::endl;
   237       os << "@edgeset" << std::endl;
   232       os << "\t\t";
   238       os << "\t\t";
   233       for (int i = 0; i < edge_map_writers.size(); ++i) {
   239       for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
   234 	os << edge_map_writers[i].first << '\t';
   240 	os << edge_map_writers[i].first << '\t';
   235       } 
   241       } 
   236       os << std::endl;
   242       os << std::endl;
   237       for (EdgeIt it(graph); it != INVALID; ++it) {
   243       for (EdgeIt it(graph); it != INVALID; ++it) {
   238 	node_map_writers[0].second->write(os, graph.source(it));
   244 	node_map_writers[0].second->write(os, graph.source(it));
   239 	node_map_writers[0].second->write(os, graph.target(it));
   245 	node_map_writers[0].second->write(os, graph.target(it));
   240 	for (int i = 0; i < edge_map_writers.size(); ++i) {
   246 	for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
   241 	  edge_map_writers[i].second->write(os, it);
   247 	  edge_map_writers[i].second->write(os, it);
   242 	}
   248 	}
   243 	os << std::endl;
   249 	os << std::endl;
   244       }
   250       }
   245     }
   251     }
   248       if (node_writers.size() == 0) return;
   254       if (node_writers.size() == 0) return;
   249       if (node_map_writers.size() == 0) {
   255       if (node_map_writers.size() == 0) {
   250 	throw Exception() << "Missing node id map";
   256 	throw Exception() << "Missing node id map";
   251       }
   257       }
   252       os << "@nodes" << std::endl;
   258       os << "@nodes" << std::endl;
   253       for (int i = 0; i < node_writers.size(); ++i) {
   259       for (int i = 0; i < (int)node_writers.size(); ++i) {
   254 	os << node_writers[i].first << '\t';
   260 	os << node_writers[i].first << '\t';
   255 	node_map_writers[0].second->write(os, node_writers[i].second);
   261 	node_map_writers[0].second->write(os, node_writers[i].second);
   256 	os << std::endl;
   262 	os << std::endl;
   257       } 
   263       } 
   258     }
   264     }
   261       if (edge_writers.size() == 0) return;
   267       if (edge_writers.size() == 0) return;
   262       if (edge_map_writers.size() == 0) {
   268       if (edge_map_writers.size() == 0) {
   263 	throw Exception() << "Missing edge id map";
   269 	throw Exception() << "Missing edge id map";
   264       }
   270       }
   265       os << "@edges" << std::endl;
   271       os << "@edges" << std::endl;
   266       for (int i = 0; i < edge_writers.size(); ++i) {
   272       for (int i = 0; i < (int)edge_writers.size(); ++i) {
   267 	os << edge_writers[i].first << '\t';
   273 	os << edge_writers[i].first << '\t';
   268 	edge_map_writers[0].second->write(os, edge_writers[i].second);
   274 	edge_map_writers[0].second->write(os, edge_writers[i].second);
   269 	os << std::endl;
   275 	os << std::endl;
   270       } 
   276       } 
   271     }
   277     }
   293       MapWriter(const Map& _map, const Writer& _writer) 
   299       MapWriter(const Map& _map, const Writer& _writer) 
   294 	: map(_map), writer(_writer) {}
   300 	: map(_map), writer(_writer) {}
   295 
   301 
   296 
   302 
   297       virtual void write(std::ostream& os, const Item& item) {
   303       virtual void write(std::ostream& os, const Item& item) {
   298 	Value value;
       
   299 	writer.write(os, map[item]);
   304 	writer.write(os, map[item]);
   300       }
   305       }
   301 
   306 
   302     };
   307     };
   303 
   308 
   304 
   309 
   305 
   310 
   306     typedef std::vector< std::pair<std::string, WriterBase<Node>*> > NodeMapWriters;
   311     typedef std::vector< std::pair<std::string, WriterBase<Node>*> > 
       
   312       NodeMapWriters;
   307     NodeMapWriters node_map_writers;
   313     NodeMapWriters node_map_writers;
   308 
   314 
   309     typedef std::vector< std::pair<std::string, WriterBase<Edge>*> > EdgeMapWriters;
   315     typedef std::vector< std::pair<std::string, WriterBase<Edge>*> > 
       
   316       EdgeMapWriters;
   310     EdgeMapWriters edge_map_writers;
   317     EdgeMapWriters edge_map_writers;
   311 
   318 
   312     typedef std::vector<std::pair<std::string, Node> > NodeWriters;
   319     typedef std::vector<std::pair<std::string, Node> > NodeWriters;
   313     NodeWriters node_writers;
   320     NodeWriters node_writers;
   314 
   321