36#ifndef OPM_PARAMETERGROUP_IMPL_HEADER
37#define OPM_PARAMETERGROUP_IMPL_HEADER
60 conversion_error =
"The XML tag was '" + tag +
65 conversion_error =
"";
74 ParameterGroup::to_string(
const T& val)
76 std::ostringstream os;
83 ParameterGroup::to_string(
const bool&
b) {
98 inline std::pair<std::string, std::string>
99 ParameterGroup::filename_split(
const std::string& filename)
101 int fpos = filename.rfind(
'.');
104 return std::make_pair(
name, type);
107 template <
typename StringArray>
109 const bool enable_output)
110 : path_(
ID_path_root), parent_(0), output_is_enabled_(enable_output)
112 if (verify_syntax && (argc < 2)) {
114 <<
"[paramfilename1.param] "
115 <<
"[paramfilename2.param] "
116 <<
"[overridden_arg1=value1] "
117 <<
"[overridden_arg2=value2] "
118 <<
"[...]" << std::endl;
121 this->parseCommandLineArguments(argc, argv, verify_syntax);
124 template <
typename StringArray>
125 void ParameterGroup::parseCommandLineArguments(
int argc, StringArray argv,
bool verify_syntax)
127 std::vector<std::string> files;
128 std::vector<std::pair<std::string, std::string> > assignments;
129 for (
int i = 1; i < argc; ++i) {
132 if (fpos ==
int(std::string::npos)) {
134 files.push_back(filename);
139 if (spos ==
int(std::string::npos)) {
142 assignments.push_back(std::make_pair(
name,
value));
147 +
"') detected in argument " + to_string(i));
149 for (
int i = 0; i < int(files.size()); ++i) {
150 std::pair<std::string, std::string> file_type = filename_split(files[i]);
151 if (file_type.second ==
"param") {
155 std::cerr <<
"ERROR: Input '" << files[i] <<
"' is not a valid name for a parameter file.\n";
156 std::cerr <<
" Valid filename extensions are 'param'.\n";
157 OPM_THROW(std::runtime_error,
"ParameterGroup cannot handle argument: " << files[i]);
159 unhandled_arguments_.push_back(files[i]);
163 for (
int i = 0; i < int(assignments.size()); ++i) {
175 template<
typename T,
class Requirement>
177 const Requirement& r)
const
181 map_type::const_iterator it = map_.find(name_path.first);
182 if (it == map_.end()) {
185 if (output_is_enabled_) {
188 return parent_->
get<T>(
name, r);
193 <<
"' does not contain an element named '"
199 if (name_path.second ==
"") {
200 T val = this->translate<T>(*it, r);
201 it->second->setUsed();
202 if (output_is_enabled_) {
209 return pg.
get<T>(name_path.second, r);
215 const T& default_value)
const
220 template<
typename T,
class Requirement>
222 const T& default_value,
223 const Requirement& r)
const
227 map_type::const_iterator it = map_.find(name_path.first);
228 if (it == map_.end()) {
231 if (output_is_enabled_) {
238 if (requirement_result !=
"") {
239 std::cerr <<
"ERROR: The default value for the "
240 <<
" element named '"
242 <<
"' in the group '"
244 <<
"' failed to meet a requirenemt.\n";
245 std::cerr <<
"The requirement enforcer returned the following message:\n"
246 << requirement_result
251 if (output_is_enabled_) {
252 OpmLog::debug(
name +
" not found. Using default value '" + to_string(default_value) +
"'.");
254 return default_value;
256 if (name_path.second ==
"") {
257 T val = this->translate<T>(*it, r);
258 it->second->setUsed();
259 if (output_is_enabled_) {
261 +
", value is '" + to_string(val) +
"'.");
267 return pg.
getDefault<T>(name_path.second, default_value, r);
271 template<
typename T,
class Requirement>
272 inline T ParameterGroup::translate(
const pair_type& named_data,
273 const Requirement& chk)
const
276 const data_type
data = named_data.second;
280 if (conversion_error !=
"") {
281 std::cerr <<
"ERROR: Failed to convert the element named '"
283 <<
"' in the group '"
288 std::cerr <<
"The conversion routine returned the following message:\n"
291 throw WrongTypeException();
294 if (requirement_result !=
"") {
295 std::cerr <<
"ERROR: The element named '"
297 <<
"' in the group '"
301 <<
"' failed to meet a requirenemt.\n";
302 std::cerr <<
"The requirement enforcer returned the following message:\n"
303 << requirement_result
305 throw RequirementFailedException<Requirement>();
#define OPM_THROW(Exception, message)
Definition: ErrorMacros.hpp:52
const cJSON *const b
Definition: cJSON.h:251
const char *const name
Definition: cJSON.h:258
cJSON * item
Definition: cJSON.h:218
const char *const string
Definition: cJSON.h:170
static void debug(const std::string &message)
static void warning(const std::string &message)
Definition: ParameterGroup.hpp:81
std::string path() const
Returns the path of the parameter group.
void insertParameter(const std::string &name, const std::string &value)
Insert a new parameter item into the group.
T getDefault(const std::string &name, const T &default_value) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:214
void readParam(const std::string ¶m_filename)
Reads the contents of the param file specified by param_filename into this ParameterGroup.
T get(const std::string &name) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:170
constexpr const double second
Definition: custom-opm-common/opm-common/opm/parser/eclipse/Units/Units.hpp:104
const std::string ID_true
Definition: ParameterStrings.hpp:42
const std::string ID_path_root
Definition: ParameterStrings.hpp:57
const std::string ID_delimiter_path
Definition: ParameterStrings.hpp:58
const std::string ID_xmltag__param_grp
Definition: ParameterStrings.hpp:45
const std::string ID_false
Definition: ParameterStrings.hpp:43
std::pair< std::string, std::string > splitParam(const std::string &name)
const std::string ID_delimiter_assignment
Definition: ParameterStrings.hpp:60
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955
Definition: ParameterGroup.hpp:83
Definition: ParameterGroup.hpp:88
Definition: ParameterMapItem.hpp:47
void setUsed() const
Definition: ParameterMapItem.hpp:57
static ParameterGroup convert(const ParameterMapItem &item, std::string &conversion_error, bool enable_output)
Definition: ParameterGroup_impl.hpp:54
static std::string type()
Definition: ParameterGroup_impl.hpp:69
Definition: ParameterMapItem.hpp:64
static std::string type()
static T convert(const ParameterMapItem &, std::string &conversion_error)
Definition: ParameterRequirement.hpp:51