Modular Megafauna Model 1.1.5
A physiological, dynamic herbivore simulator in C++.
Loading...
Searching...
No Matches
insfile_reader.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2020 W. Traylor <wolfgang.traylor@senckenberg.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
11#ifndef FAUNA_INSFILE_READER_H
12#define FAUNA_INSFILE_READER_H
13
14#include <list>
15#include "cpptoml.h"
16#include "parameters.h"
17
18namespace Fauna {
19// Forward declarations
20class Hft;
21
22// Repeat typedef from hft.h
23typedef std::vector<std::shared_ptr<const Hft>> HftList;
24
26class ambiguous_param_type : public std::runtime_error {
27 public:
29
35 ambiguous_param_type(const std::string& key, const std::string& hft,
36 const std::string& value1, const std::string& value2)
37 : std::runtime_error(
38 "Parameter '" + key + +"' " +
39 (hft == "" ? "of HFT '" + hft + "' " : "") +
40 "is ambiguous: there is '" + value1 + "' or '" + value2 + "'. " +
41 "The reason might be that the parameter can take two different "
42 "types and has then been defined twice."){};
43};
44
46class bad_array_size : public std::runtime_error {
47 public:
49
54 bad_array_size(const std::string& key, const unsigned int given_size,
55 const std::string& expected_size)
56 : std::runtime_error("Array parameter '" + key + "' has " +
57 std::to_string(given_size) + "'elements," +
58 " but required are: " + expected_size){};
59};
60
62class invalid_option : public std::runtime_error {
63 public:
65
71 invalid_option(const std::string& key, const std::string& value,
72 const std::set<std::string>& valid_options)
73 : runtime_error(construct_message(key, value, valid_options)){};
74
76
83 invalid_option(const Hft& hft, const std::string& key,
84 const std::string& value,
85 const std::set<std::string>& valid_options)
86 : runtime_error(construct_message(key, value, valid_options)){};
87
88 private:
89 std::string construct_message(const std::string& key,
90 const std::string& value,
91 const std::set<std::string>& valid_options);
92};
93
95struct missing_parameter : public std::runtime_error {
97
100 missing_parameter(const std::string& key)
101 : runtime_error("Missing mandatory parameter: \"" + key + '"'){};
102};
103
105struct missing_hft_parameter : public std::runtime_error {
107
111 missing_hft_parameter(const std::string& hft_name, const std::string& key)
112 : runtime_error("Missing mandatory parameter \"" + key + "\" in HFT \"" +
113 hft_name + "\"."){};
114};
115
117struct missing_group : public std::runtime_error {
119
123 missing_group(const std::string& hft_name, const std::string& group_name)
124 : std::runtime_error("Cannot find group with name \"" + group_name +
125 "\". " + "Required by HFT \"" + hft_name + "\"."){};
126};
127
129
136struct param_out_of_range : public std::runtime_error {
138
146 param_out_of_range(const std::string& key, const std::string& value,
147 const std::string& allowed_interval)
148 : std::runtime_error("The parameter \"" + key + "\" " +
149 "is out of range. The specified value is " + value +
150 ", which lies outside of the interval " +
151 allowed_interval + "."){};
152};
153
155struct unknown_parameters : public std::runtime_error {
157
162 unknown_parameters(const std::list<std::string> elements)
163 : runtime_error("Unknown parameters encountered:\n" +
164 concatenate_elements(elements)){};
165
167 static std::string concatenate_elements(
168 const std::list<std::string> elements) {
169 std::ostringstream str;
170 for (const auto& i : elements) str << i << "\n";
171 return str.str();
172 }
173};
174
176struct wrong_param_type : public std::runtime_error {
178
183 wrong_param_type(const std::string& key, const std::string& type_expected,
184 const std::string& type_found)
185 : std::runtime_error("The parameter \"" + key + "\" " +
186 "is of a wrong type. I found type " + type_found +
187 ", but expected " + type_expected + "."){};
188};
189
192 public:
194
200 InsfileReader(const std::string filename);
201
203 const HftList& get_hfts() const { return hfts; };
204
206 const Parameters& get_params() const { return params; };
207
208 private:
210
214 std::shared_ptr<cpptoml::table> get_group_table(
215 const std::string& group_name) const;
216
218 enum class GetValueOpt {
220 RemoveKey,
222 KeepKey
223 };
224
226
238 template <class T>
239 std::shared_ptr<T> get_value(
240 const std::shared_ptr<cpptoml::table>& table, const std::string& key,
241 const GetValueOpt opt = GetValueOpt::RemoveKey) const;
242
244
256 template <class T>
257 std::shared_ptr<std::vector<T>> get_value_array(
258 const std::shared_ptr<cpptoml::table>& table, const std::string& key,
259 const GetValueOpt opt = GetValueOpt::RemoveKey) const;
260
262
269 template <class Expected>
270 void check_wrong_type(std::shared_ptr<cpptoml::table> table,
271 const std::string& key) const;
272
274
284 template <class T>
285 std::shared_ptr<T> find_hft_parameter(
286 const std::shared_ptr<cpptoml::table>& hft_table, const std::string& key,
287 const bool mandatory);
288
290
293 template <class T>
294 std::shared_ptr<std::vector<T>> find_hft_array_parameter(
295 const std::shared_ptr<cpptoml::table>& hft_table, const std::string& key,
296 const bool mandatory);
297
299
303 std::list<std::string> get_all_keys(std::shared_ptr<cpptoml::table> table);
304
306 void read_table_forage();
307
309 void read_table_output();
310
313
316
318
324 void remove_qualified_key(std::shared_ptr<cpptoml::table> table,
325 const std::string key) const;
326
328
331 Hft read_hft(const std::shared_ptr<cpptoml::table>& table);
332
334 std::shared_ptr<cpptoml::table> ins;
335
337
345 std::set<std::string> hft_keys_parsed;
346
349};
350} // namespace Fauna
351
352#endif // FAUNA_INSFILE_READER_H
One herbivore functional type (i.e. one species).
Definition: hft.h:310
Class to read parameters and HFTs from given instruction file.
Definition: insfile_reader.h:191
std::shared_ptr< T > get_value(const std::shared_ptr< cpptoml::table > &table, const std::string &key, const GetValueOpt opt=GetValueOpt::RemoveKey) const
Retrieve a single value from the TOML file.
Definition: insfile_reader.cpp:214
std::shared_ptr< cpptoml::table > get_group_table(const std::string &group_name) const
Find table with group.
Definition: insfile_reader.cpp:188
Parameters params
Definition: insfile_reader.h:347
std::shared_ptr< cpptoml::table > ins
The root table of the instruction file from cpptoml::parse_file().
Definition: insfile_reader.h:334
GetValueOpt
How to treat a parameter after it has been parsed by get_value().
Definition: insfile_reader.h:218
@ RemoveKey
Remove the parameter with remove_qualified_key().
std::shared_ptr< T > find_hft_parameter(const std::shared_ptr< cpptoml::table > &hft_table, const std::string &key, const bool mandatory)
Retrieve HFT parameter from HFT table itself or one of its groups.
Definition: insfile_reader.cpp:257
void remove_qualified_key(std::shared_ptr< cpptoml::table > table, const std::string key) const
Remove TOML element (in order to indicate it’s been parsed).
Definition: insfile_reader.cpp:961
std::list< std::string > get_all_keys(std::shared_ptr< cpptoml::table > table)
Iterate through TOML tree and list all "leaves".
Definition: insfile_reader.cpp:328
void check_wrong_type(std::shared_ptr< cpptoml::table > table, const std::string &key) const
Throw an exception if parameter is present, but with wrong type.
Definition: insfile_reader.cpp:121
const Parameters & get_params() const
Get the global parameters that were read from the instruction file.
Definition: insfile_reader.h:206
void read_table_forage()
Read the TOML table forage.
Definition: insfile_reader.cpp:822
void read_table_output_text_tables()
Read the TOML table output.text_tables.
Definition: insfile_reader.cpp:874
Hft read_hft(const std::shared_ptr< cpptoml::table > &table)
Construct HFT object from an entry in the array of tables.
Definition: insfile_reader.cpp:349
HftList hfts
Definition: insfile_reader.h:348
void read_table_simulation()
Read the TOML table simulation.
Definition: insfile_reader.cpp:923
std::shared_ptr< std::vector< T > > get_value_array(const std::shared_ptr< cpptoml::table > &table, const std::string &key, const GetValueOpt opt=GetValueOpt::RemoveKey) const
Retrieve a value array from the TOML file.
Definition: insfile_reader.cpp:232
void read_table_output()
Read the TOML table output.
Definition: insfile_reader.cpp:838
std::shared_ptr< std::vector< T > > find_hft_array_parameter(const std::shared_ptr< cpptoml::table > &hft_table, const std::string &key, const bool mandatory)
Like find_hft_parameter(), but for an array of values.
Definition: insfile_reader.cpp:291
const HftList & get_hfts() const
Get the HFT list that was read from the instruction file.
Definition: insfile_reader.h:203
std::set< std::string > hft_keys_parsed
All TOML keys in HFTs and HFT groups that have been parsed.
Definition: insfile_reader.h:345
Exception that a parameter can be interpreted in multiple ways.
Definition: insfile_reader.h:26
ambiguous_param_type(const std::string &key, const std::string &hft, const std::string &value1, const std::string &value2)
Constructor.
Definition: insfile_reader.h:35
Exception that an array parameter does not have the correct length.
Definition: insfile_reader.h:46
bad_array_size(const std::string &key, const unsigned int given_size, const std::string &expected_size)
Constructor.
Definition: insfile_reader.h:54
Exception that a string parameter does not match possible options.
Definition: insfile_reader.h:62
std::string construct_message(const std::string &key, const std::string &value, const std::set< std::string > &valid_options)
Definition: insfile_reader.cpp:34
invalid_option(const std::string &key, const std::string &value, const std::set< std::string > &valid_options)
Constructor for global parameter.
Definition: insfile_reader.h:71
invalid_option(const Hft &hft, const std::string &key, const std::string &value, const std::set< std::string > &valid_options)
Constructor for HFT parameter.
Definition: insfile_reader.h:83
Definition: average.h:16
std::vector< std::shared_ptr< const Hft > > HftList
List of pointers to Hft objects.
Definition: world.h:28
Global parameters for the megafauna library.
Parameters for the herbivory module.
Definition: parameters.h:65
Exception if a group listed in hft.groups cannot be found.
Definition: insfile_reader.h:117
missing_group(const std::string &hft_name, const std::string &group_name)
Constructor.
Definition: insfile_reader.h:123
Exception that an HFT parameter is missing in the instruction file.
Definition: insfile_reader.h:105
missing_hft_parameter(const std::string &hft_name, const std::string &key)
Constructor for missing HFT parameter.
Definition: insfile_reader.h:111
Exception that a parameter is missing in the instruction file.
Definition: insfile_reader.h:95
Exception if an integer or double parameter is out of range.
Definition: insfile_reader.h:136
param_out_of_range(const std::string &key, const std::string &value, const std::string &allowed_interval)
Constructor for a double value.
Definition: insfile_reader.h:146
Exception that parameters couldn’t be parsed in the TOML file.
Definition: insfile_reader.h:155
static std::string concatenate_elements(const std::list< std::string > elements)
Concatenate list of strings with line breaks.
Definition: insfile_reader.h:167
unknown_parameters(const std::list< std::string > elements)
Constructor.
Definition: insfile_reader.h:162
Exception that a parameter is not of an expected data type.
Definition: insfile_reader.h:176
wrong_param_type(const std::string &key, const std::string &type_expected, const std::string &type_found)
Constructor.
Definition: insfile_reader.h:183