Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
tools.hh
Go to the documentation of this file.
1#ifndef UTOPIA_MODELS_ENVIRONMENT_TOOLS_HH
2#define UTOPIA_MODELS_ENVIRONMENT_TOOLS_HH
3
5
10enum class ValMode {
12 Set,
13
15 Add
16};
17
20 const std::string& context)
21{
22 const auto mode_key = get_as<std::string>("mode", cfg);
23
24 if (mode_key == "add") {
25 return ValMode::Add;
26 }
27 else if (mode_key == "set") {
28 return ValMode::Set;
29 }
30
31 throw std::invalid_argument("The `mode` argument for configuration of "
32 "environment function " + context + " can be 'add' or 'set', but "
33 "was '" + mode_key + "'!");
34}
35
38template <typename Time>
39std::pair<bool, std::set<Time>> extract_times(const DataIO::Config& cfg) {
40 bool invoke_always = true;
41 std::set<Time> times;
42
43 if (not cfg.IsMap()) {
44 // Already return here
45 return {invoke_always, times};
46 }
47
48 // Extract information from configuration
49 if (cfg["times"]) {
50 invoke_always = false;
51 auto times_list = get_as<std::vector<Time>>("times", cfg);
52 // TODO Consider wrapping negative values around
53
54 // Make sure negative times or 0 is not included
55 // NOTE 0 may not be included because the environment state functions
56 // are invoked separately for this time.
57 times_list.erase(
58 std::remove_if(times_list.begin(), times_list.end(),
59 [](auto& t){ return (t <= 0); }),
60 times_list.end()
61 );
62
63 // Populate the set; this will impose ordering
64 times.insert(times_list.begin(), times_list.end());
65 }
66
67 return {invoke_always, times};
68}
69
72template <typename Time>
73std::tuple<bool, bool, std::set<Time>>
75{
76 bool invoke_at_initialization = get_as<bool>("invoke_at_initialization",
77 cfg);
78
79 auto [invoke_always, times] = extract_times<Time>(cfg);
80
81 return {invoke_at_initialization, invoke_always, times};
82}
83
85template <typename Time>
87 // get starting time
88 auto times_list = get_as<std::vector<Time>>("times", cfg, {0});
89 auto time_start = times_list.front();
90 for (auto t : times_list) {
92 time_start = t;
93 }
94
95 return time_start;
96}
97
98
99// End group Environment
104} // namespace Utopia::Models::Environment
105
106#endif
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:71
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
Time extract_time_start(const DataIO::Config &cfg)
Given a configuration, extracts the time of first function invocation.
Definition tools.hh:86
std::tuple< bool, bool, std::set< Time > > extract_times_and_initialization(const DataIO::Config &cfg)
Definition tools.hh:74
ValMode extract_val_mode(const DataIO::Config &cfg, const std::string &context)
Given a configuration node, extract the value mode.
Definition tools.hh:19
std::pair< bool, std::set< Time > > extract_times(const DataIO::Config &cfg)
Definition tools.hh:39
ValMode
Value calculation mode.
Definition tools.hh:10
@ Set
Set a value, discarding the current state.
@ Add
Add the new value to the existing value.
Definition env_param_func_collection.hh:8