NGen
Loading...
Searching...
No Matches
formulation.hpp
1#ifndef NGEN_REALIZATION_CONFIG_FORMULATION_H
2#define NGEN_REALIZATION_CONFIG_FORMULATION_H
3
4#include <NGenConfig.h>
5#include <boost/property_tree/ptree.hpp>
6#include <string>
7
8#include "JSONProperty.hpp"
9#include "features/FeatureBase.hpp"
10
11namespace realization{
12 namespace config{
13
15 std::string type;
16 //Formulation parameters, object as a PropertyMap
18 //List of nested formulations (used for multi bmi representations)
19 std::vector<Formulation> nested;
20
26 Formulation() = default;
27
34 Formulation(std::string type, geojson::PropertyMap params):type(std::move(type)), parameters(params){}
35
44 Formulation(const boost::property_tree::ptree& tree){
45 type = tree.get<std::string>("name");
46 for (std::pair<std::string, boost::property_tree::ptree> setting : tree.get_child("params")) {
47 //Construct the geoJSON PropertyMap from each key, value pair in "params"
48 parameters.emplace(
49 setting.first,
50 geojson::JSONProperty(setting.first, setting.second)
51 );
52 }
53 if(type=="bmi_multi"){
54 for(auto& module : tree.get_child("params.modules")){
55 //Create the nested formulations in order of definition
56 nested.push_back(Formulation(module.second));
57 }
58 }
59 }
60
68
69 if(type == "bmi_multi"){
70 std::vector<geojson::JSONProperty> tmp;
71 for(auto& n : nested){
72 //Iterate and link any nested modules with this feature
73 if(n.parameters.count("model_params")){
74 n.link_external(feature);
75 }
76 //Need a temporary map to hold the updated formulation properties in
77 geojson::PropertyMap map = {};
78 map.emplace("name", geojson::JSONProperty("name", n.type));
79 map.emplace("params", geojson::JSONProperty("", n.parameters));
80 tmp.push_back(geojson::JSONProperty("", map));
81 }
82 //Reset the bmi_multi modules with the now linked module definitions
83 parameters.at("modules") = geojson::JSONProperty("modules", tmp);
84 return;
85 }
86 //Short circut
87 if(parameters.count("model_params") < 1 ) return;
88 //Have some model params, check to see if any should be linked to the hyrdofabric feature
89 geojson::PropertyMap attr = parameters.at("model_params").get_values();
90 for (decltype(auto) param : attr) {
91 // Check for type to short-circuit. If param.second is not an object, `.has_key()` will throw
92 if (param.second.get_type() != geojson::PropertyType::Object || !param.second.has_key("source")) {
93 attr.emplace(param.first, param.second);
94 continue;
95 }
96
97 decltype(auto) param_source = param.second.at("source");
98 decltype(auto) param_source_name = param_source.as_string();
99 if (param_source_name != "hydrofabric") {
100 // TODO: temporary until the logic for alternative sources is designed
101 throw std::logic_error("ERROR: 'model_params' source `" + param_source_name + "` not currently supported. Only `hydrofabric` is supported.");
102 }
103
104 // Property name in the feature properties is either
105 // the value of key "from", or has the same name as
106 // the expected model parameter key
107 decltype(auto) param_name = param.second.has_key("from")
108 ? param.second.at("from").as_string()
109 : param.first;
110
111 if (feature->has_property(param_name)) {
112 auto catchment_attribute = feature->get_property(param_name);
113
114 // Use param.first in the `.emplace` calls instead of param_name since
115 // the expected name is given by the key of the model_params values.
116 switch (catchment_attribute.get_type()) {
119 // TODO: Should list/object values be passed to model parameters?
120 // Typically, feature properties *should* be scalars.
121 std::cerr << "WARNING: property type " << static_cast<int>(catchment_attribute.get_type()) << " not allowed as model parameter. "
122 << "Must be one of: Natural (int), Real (double), Boolean, or String" << '\n';
123 break;
124 default:
125 attr.at(param.first) = geojson::JSONProperty(param.first, catchment_attribute);
126 }
127 }
128 }
129 parameters.at("model_params") = geojson::JSONProperty("model_params", attr);
130 }
131 };
132
133 }//end namespace config
134}//end namespace realization
135#endif //NGEN_REALIZATION_CONFIG_FORMULATION_H
@TODO: Convert JSONProperty into a variant of the supported types
Definition JSONProperty.hpp:161
std::map< std::string, JSONProperty > PropertyMap
Shorthand for a mapping between strings and properties.
Definition JSONProperty.hpp:21
std::shared_ptr< FeatureBase > Feature
An easy name for a smart pointer for FeatureBase and its children.
Definition FeatureBase.hpp:34
Definition HY_CatchmentRealization.hpp:13
STL namespace.
Definition formulation.hpp:14
geojson::PropertyMap parameters
Definition formulation.hpp:17
std::string type
Definition formulation.hpp:15
Formulation()=default
Construct a new default Formulation object.
Formulation(std::string type, geojson::PropertyMap params)
Construct a new Formulation object.
Definition formulation.hpp:34
Formulation(const boost::property_tree::ptree &tree)
Construct a new Formulation object from a boost property tree.
Definition formulation.hpp:44
std::vector< Formulation > nested
Definition formulation.hpp:19
void link_external(geojson::Feature feature)
Link formulation parameters to hydrofabric data held in feature.
Definition formulation.hpp:67