Utopia  2
Framework for studying models of complex & adaptive systems.
graph_load.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_DATAIO_GRAPH_LOAD_HH
2 #define UTOPIA_DATAIO_GRAPH_LOAD_HH
3 
4 #include <string>
5 #include <fstream>
6 #include <boost/graph/graphml.hpp>
7 #include <boost/graph/graphviz.hpp>
8 #include <boost/property_map/dynamic_property_map.hpp>
9 
10 #include "utopia/core/types.hh"
13 
14 
15 namespace Utopia {
16 namespace DataIO {
22 namespace GraphLoad {
24 
38 template <typename Graph>
39 Graph load_graph(const Config& cfg,
40  boost::dynamic_properties pmaps)
41 {
42  const auto abs_file_path = get_abs_filepath(cfg);
43  const auto format = get_as<std::string>("format", cfg, "dot");
44 
45  // Create an empty graph
46  Graph g;
47 
48  // Load file into file stream
49  std::ifstream ifs(abs_file_path.c_str());
50  if (not ifs.is_open()) {
51  throw std::invalid_argument(
52  "Failed opening file for loading graph! Make sure there "
53  "exists a file at " + abs_file_path + "!"
54  );
55  }
56 
57  // Load the data from the file stream
58  if (format == "graphviz" or format == "gv" or format == "dot") {
59  boost::read_graphviz(ifs, g, pmaps);
60 
61  } else if (format == "graphml") {
62  boost::read_graphml(ifs, g, pmaps);
63 
64  } else {
65  throw std::invalid_argument(
66  "The given file format is not supported. The file format needs "
67  "to be one of 'graphviz' / 'gv' / 'dot' or 'graphml' "
68  "and needs to be specified in the config's format node, e.g. "
69  "load_from_file: { format: graphml }.");
70  }
71 
72  // Return the graph
73  return g;
74 }
75 
76 } // namespace GraphLoad
77  // end of group DataIO
79 
80 } // namespace DataIO
81 } // namespace Utopia
82 
83 #endif // UTOPIA_DATAIO_GRAPH_LOAD_HH
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
std::string get_abs_filepath(const Config &cfg)
Extracts an absolute file path from a configuration.
Definition: filesystem.hh:73
Graph load_graph(const Config &cfg, boost::dynamic_properties pmaps)
Load a graph.
Definition: graph_load.hh:39
Definition: agent.hh:11