NGen
Loading...
Searching...
No Matches
Formulation.hpp
1#ifndef FORMULATION_H
2#define FORMULATION_H
3
4#include <memory>
5#include <string>
6#include <map>
7#include <exception>
8#include <vector>
9
10#include "JSONProperty.hpp"
11
12#include <boost/property_tree/ptree.hpp>
13#include <boost/algorithm/string.hpp>
14
15namespace realization {
16
18 public:
19 typedef long time_step_t;
20
21 Formulation(std::string id) : id(id) {}
22
23 virtual ~Formulation(){};
24
25 virtual std::string get_formulation_type() const = 0;
26
27 // TODO: to truly make this properly generalized (beyond catchments, and to some degree even in that
28 // context) a more complex type for the entirety of the response/output is needed, perhaps with independent
29 // access functions
30
31 // TODO: a reference to the actual time of the initial time step is needed
32
33 // TODO: a reference to the last calculated time step is needed
34
35 // TODO: a mapping of previously calculated time steps to the size/delta of each is needed (unless we
36 // introduce a way to enforce immutable time step delta values for an object)
37
38 // TODO: a convenience method for getting the actual time of calculated time steps (at least the last)
39
40 std::string get_id() const {
41 return this->id;
42 }
43
44 virtual void create_formulation(boost::property_tree::ptree &config, geojson::PropertyMap *global = nullptr) = 0;
45 virtual void create_formulation(geojson::PropertyMap properties) = 0;
46
47 protected:
48
49 virtual const std::vector<std::string>& get_required_parameters() const = 0;
50
51 geojson::PropertyMap interpret_parameters(boost::property_tree::ptree &config, geojson::PropertyMap *global = nullptr) {
53
54 for (auto &formulation_parameter : config) {
55 options.emplace(formulation_parameter.first, geojson::JSONProperty(formulation_parameter.first, formulation_parameter.second));
56 }
57
58 if (global != nullptr) {
59 for(auto &global_option : *global) {
60 if (options.count(global_option.first) == 0) {
61 options.emplace(global_option.first, global_option.second);
62 }
63 }
64 }
65
66 validate_parameters(options);
67
68 return options;
69 }
70
72 std::vector<std::string> missing_parameters;
73 std::vector<std::string> required_parameters = get_required_parameters();
74
75 for (auto parameter : required_parameters) {
76 if (options.count(parameter) == 0) {
77 missing_parameters.push_back(parameter);
78 }
79 }
80
81 if (missing_parameters.size() > 0) {
82 std::string message = "A " + get_formulation_type() + " formulation cannot be created; the following parameters are missing: ";
83
84 for (int missing_parameter_index = 0; missing_parameter_index < missing_parameters.size(); missing_parameter_index++) {
85 message += missing_parameters[missing_parameter_index];
86
87 if (missing_parameter_index < missing_parameters.size() - 1) {
88 message += ", ";
89 }
90 }
91
92 throw std::runtime_error(message);
93 }
94 }
95
96 std::string id;
97 };
98
99}
100#endif // FORMULATION_H
@TODO: Convert JSONProperty into a variant of the supported types
Definition JSONProperty.hpp:165
Definition Formulation.hpp:17
std::string get_id() const
Definition Formulation.hpp:40
virtual std::string get_formulation_type() const =0
virtual ~Formulation()
Definition Formulation.hpp:23
virtual void create_formulation(geojson::PropertyMap properties)=0
Formulation(std::string id)
Definition Formulation.hpp:21
void validate_parameters(geojson::PropertyMap options)
Definition Formulation.hpp:71
virtual void create_formulation(boost::property_tree::ptree &config, geojson::PropertyMap *global=nullptr)=0
geojson::PropertyMap interpret_parameters(boost::property_tree::ptree &config, geojson::PropertyMap *global=nullptr)
Definition Formulation.hpp:51
std::string id
Definition Formulation.hpp:96
virtual const std::vector< std::string > & get_required_parameters() const =0
long time_step_t
Definition Formulation.hpp:19
std::map< std::string, JSONProperty > PropertyMap
Shorthand for a mapping between strings and properties.
Definition JSONProperty.hpp:21
Definition Bmi_C_Formulation.hpp:11