Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Functions
Utopia::DataIO::GraphLoad Namespace Reference

Functions

template<typename Graph >
Graph load_graph (const Config &cfg, boost::dynamic_properties pmaps)
 Load a graph.
 

Function Documentation

◆ load_graph()

template<typename Graph >
Graph Utopia::DataIO::GraphLoad::load_graph ( const Config cfg,
boost::dynamic_properties  pmaps 
)

Load a graph.

This function loads a graph

/tparam Graph The graph type

/param file_name The name of the file to load /param pmaps Any additional property maps; if this contains a property map named weight, the weights will be loaded additionally, if the data file contains that information.

/return Graph The loaded graph

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}
std::string get_abs_filepath(const Config &cfg)
Extracts an absolute file path from a configuration.
Definition filesystem.hh:73