VIPRA Documentation
Loading...
Searching...
No Matches
parameters.hpp
1#pragma once
2
3#include <iostream>
4#include <map>
5
6#include <nlohmann/json.hpp>
7#include <set>
8#include <type_traits>
9
10#include "vipra/input/json.hpp"
11
12#include "vipra/modules.hpp"
13#include "vipra/random/random.hpp"
14
15// TODO(rolland): Check that all required parameters are provided (maybe not needed, they are checked when the module tries to get it)
16// TODO(rolland): issue #26 No way of checking what a parameter was, once get_param is called the random engine moves on
17// - this is needed for outputing the parameters used in a simulation run
18
19namespace VIPRA {
21 public:
22 void reset();
23
24 void register_param(Modules::Type module, std::string const& moduleName,
25 std::string const& paramName);
26
27 [[nodiscard]] auto has_param(Modules::Type module, std::string const& moduleName,
28 std::string const& paramName) const -> bool;
29 [[nodiscard]] auto has_required_param(Modules::Type module,
30 std::string const& moduleName,
31 std::string const& paramName) const -> bool;
32
33 [[nodiscard]] auto get_input() -> VIPRA::Input::JSON& { return _input; }
34 [[nodiscard]] auto get_used_parameters() const -> std::string
35 {
36 return _usedParameters.dump();
37 }
38
39 template <typename data_t>
40 [[nodiscard]] auto get_param(Modules::Type module, std::string const& moduleName,
41 std::string const& paramName, Random::Engine& engine) const
42 -> std::remove_cvref_t<data_t>;
43
44 private:
45 VIPRA::Input::JSON _input;
46 std::map<Modules::Type, std::map<std::string, std::set<std::string>>> _params;
47
48 // TODO(rolland, issue #26): mutable is disgusting, but currently needed to keep get_param const while still recording the parameters used.
49 mutable nlohmann::json _usedParameters;
50
51 template <typename array_t>
52 auto get_array_param(Modules::Type module, std::string const& moduleName,
53 std::string const& paramName) const -> array_t;
54
55 [[nodiscard]] auto contains(Modules::Type module, std::string const& moduleName,
56 std::string const& paramName) const -> bool
57 {
58 return _params.contains(module) && _params.at(module).contains(moduleName) &&
59 _params.at(module).at(moduleName).contains(paramName);
60 }
61
70 void record_param(Modules::Type module, std::string const& moduleName,
71 std::string const& paramName, auto const& value) const
72 {
73 _usedParameters[to_string(module)][moduleName][paramName] = value;
74 }
75};
76
86template <typename data_t>
87auto Parameters::get_param(Modules::Type module, std::string const& moduleName,
88 std::string const& paramName,
89 Random::Engine& engine) const -> std::remove_cvref_t<data_t>
90{
91 using param_t = std::remove_cvref_t<data_t>;
92
93 std::string moduleStr = to_string(module);
94
95 // Check that the parameter was registered
96 if ( ! contains(module, moduleName, paramName) ) {
97 throw std::runtime_error("Parameter: " + paramName + " For " + to_string(module) +
98 " Module: " + moduleName + " Not Registered");
99 }
100
101 // load the value of the parameter
102 auto value = std::optional<param_t>{};
104 // the parameter is supposed to be an array, load it as such
105 value = get_array_param<param_t>(module, moduleName, paramName);
106 }
107 else {
108 // the parameter is meant to be a single value, follow parameter randomization rules
109 value = _input.get_param<param_t>(moduleStr, moduleName, paramName, engine);
110 }
111
112 if ( ! value.has_value() ) {
113 // parameter value wasn't found, throw
114 throw std::runtime_error("Required Parameter: " + paramName + " For " +
115 to_string(module) + " Module: " + moduleName +
116 " Not Provided In Input");
117 }
118
119 record_param(module, moduleName, paramName, value.value());
120
121 return param_t{std::move(value.value())};
122}
123
134template <typename array_t>
135auto Parameters::get_array_param(Modules::Type module, std::string const& moduleName,
136 std::string const& paramName) const -> array_t
137{
138 std::string moduleStr = to_string(module);
139
140 if ( ! contains(module, moduleName, paramName) )
141 throw std::runtime_error("Parameter: " + paramName + " For " + to_string(module) +
142 " Module: " + moduleName + " Not Registered");
143
144 auto value = _input.get<array_t>({moduleStr, moduleName, paramName});
145 if ( ! value.has_value() ) {
146 throw std::runtime_error("Required Parameter: " + paramName + " For " +
147 to_string(module) + " Module: " + moduleName +
148 " Not Provided In Input");
149 }
150
151 return value.value();
152}
153
154} // namespace VIPRA
Parameter and Polygon qualified JSON input module.
Definition json.hpp:36
Definition parameters.hpp:20
auto has_param(Modules::Type module, std::string const &moduleName, std::string const &paramName) const -> bool
Returns true if the parameter exists, otherwise false.
Definition parameters.cpp:46
auto has_required_param(Modules::Type module, std::string const &moduleName, std::string const &paramName) const -> bool
Returns true if the parameter exists, otherwise false.
Definition parameters.cpp:61
auto get_param(Modules::Type module, std::string const &moduleName, std::string const &paramName, Random::Engine &engine) const -> std::remove_cvref_t< data_t >
Returns the value of the parameter if it exists, otherwise throws an error.
Definition parameters.hpp:87
void register_param(Modules::Type module, std::string const &moduleName, std::string const &paramName)
Registers a parameter for a module.
Definition parameters.cpp:28
void reset()
Clears the parameters, does NOT reset the input module.
Definition parameters.cpp:12
Psuedo Random number engine.
Definition random.hpp:22
Utility struct that gets whether a type is a specialization of another templated type.
Definition template_specialization.hpp:14