36 #ifndef OPM_PARAMETERGROUP_IMPL_HEADER
37 #define OPM_PARAMETERGROUP_IMPL_HEADER
47 #include <opm/common/ErrorMacros.hpp>
56 std::string& conversion_error,
59 std::string tag = item.
getTag();
61 conversion_error =
"The XML tag was '" + tag +
66 conversion_error =
"";
70 static std::string
type() {
return "ParameterGroup";}
75 ParameterGroup::to_string(
const T& val)
77 std::ostringstream os;
84 ParameterGroup::to_string(
const bool& b) {
96 return std::string(
"<parameter group>");
99 inline std::pair<std::string, std::string>
100 ParameterGroup::filename_split(
const std::string& filename)
102 int fpos = filename.rfind(
'.');
103 std::string name = filename.substr(0, fpos);
104 std::string type = filename.substr(fpos+1);
105 return std::make_pair(name, type);
108 template <
typename StringArray>
110 const bool enable_output)
111 : path_(
ID_path_root), parent_(0), output_is_enabled_(enable_output)
113 if (verify_syntax && (argc < 2)) {
114 std::cerr <<
"Usage: " << argv[0] <<
" "
115 <<
"[paramfilename1.{xml,param}] "
116 <<
"[paramfilename2.{xml,param}] "
117 <<
"[overridden_arg1=value1] "
118 <<
"[overridden_arg2=value2] "
119 <<
"[...]" << std::endl;
122 this->parseCommandLineArguments(argc, argv, verify_syntax);
125 template <
typename StringArray>
126 void ParameterGroup::parseCommandLineArguments(
int argc, StringArray argv,
bool verify_syntax)
128 std::vector<std::string> files;
129 std::vector<std::pair<std::string, std::string> > assignments;
130 for (
int i = 1; i < argc; ++i) {
131 std::string arg(argv[i]);
133 if (fpos ==
int(std::string::npos)) {
134 std::string filename = arg.substr(0, fpos);
135 files.push_back(filename);
140 if (spos ==
int(std::string::npos)) {
141 std::string name = arg.substr(0, fpos);
142 std::string value = arg.substr(pos, spos);
143 assignments.push_back(std::make_pair(name, value));
146 std::cout <<
"WARNING: Too many assignements (' "
148 <<
"') detected in argument " << i <<
".\n";
150 for (
int i = 0; i < int(files.size()); ++i) {
151 std::pair<std::string, std::string> file_type = filename_split(files[i]);
152 if (file_type.second ==
"xml") {
154 }
else if (file_type.second ==
"param") {
158 std::cerr <<
"ERROR: Input '" << files[i] <<
"' is not a valid name for a parameter file.\n";
159 std::cerr <<
" Valid filename extensions are 'xml' and 'param'.\n";
160 OPM_THROW(std::runtime_error,
"ParameterGroup cannot handle argument: " << files[i]);
162 unhandled_arguments_.push_back(files[i]);
166 for (
int i = 0; i < int(assignments.size()); ++i) {
178 template<
typename T,
class Requirement>
180 const Requirement& r)
const
183 std::pair<std::string, std::string> name_path =
split(name);
184 map_type::const_iterator it = map_.find(name_path.first);
185 if (it == map_.end()) {
188 if (output_is_enabled_) {
192 std::cout <<
" not found at "
194 <<
", asking parent." << std::endl;
196 return parent_->
get<T>(name, r);
199 std::cerr <<
"ERROR: The group '"
201 <<
"' does not contain an element named '"
207 if (name_path.second ==
"") {
208 T val = this->translate<T>(*it, r);
209 it->second->setUsed();
210 if (output_is_enabled_) {
215 <<
", value is " << to_string(val) << std::endl;
221 return pg.
get<T>(name_path.second, r);
227 const T& default_value)
const
232 template<
typename T,
class Requirement>
234 const T& default_value,
235 const Requirement& r)
const
238 std::pair<std::string, std::string> name_path =
split(name);
239 map_type::const_iterator it = map_.find(name_path.first);
240 if (it == map_.end()) {
243 if (output_is_enabled_) {
248 <<
", asking parent." << std::endl;
250 return parent_->
getDefault<T>(name, default_value, r);
253 std::string requirement_result = r(default_value);
254 if (requirement_result !=
"") {
255 std::cerr <<
"ERROR: The default value for the "
256 <<
" element named '"
258 <<
"' in the group '"
260 <<
"' failed to meet a requirenemt.\n";
261 std::cerr <<
"The requirement enforcer returned the following message:\n"
262 << requirement_result
267 if (output_is_enabled_) {
271 std::cout <<
" not found. Using default value '"
272 << to_string(default_value) <<
"'." << std::endl;
274 return default_value;
276 if (name_path.second ==
"") {
277 T val = this->translate<T>(*it, r);
278 it->second->setUsed();
279 if (output_is_enabled_) {
284 <<
", value is '" << to_string(val) <<
"'." << std::endl;
290 return pg.
getDefault<T>(name_path.second, default_value, r);
294 template<
typename T,
class Requirement>
295 inline T ParameterGroup::translate(
const pair_type& named_data,
296 const Requirement& chk)
const
298 const std::string& name = named_data.first;
299 const data_type data = named_data.second;
300 std::string conversion_error;
303 if (conversion_error !=
"") {
304 std::cerr <<
"ERROR: Failed to convert the element named '"
306 <<
"' in the group '"
311 std::cerr <<
"The conversion routine returned the following message:\n"
314 throw WrongTypeException();
316 std::string requirement_result = chk(value);
317 if (requirement_result !=
"") {
318 std::cerr <<
"ERROR: The element named '"
320 <<
"' in the group '"
324 <<
"' failed to meet a requirenemt.\n";
325 std::cerr <<
"The requirement enforcer returned the following message:\n"
326 << requirement_result
328 throw RequirementFailedException<Requirement>();
335 #endif // OPM_PARAMETERGROUP_IMPL_HEADER
Definition: ParameterRequirement.hpp:52
std::pair< std::string, std::string > split(const std::string &name)
static ParameterGroup convert(const ParameterMapItem &item, std::string &conversion_error, bool enable_output)
Definition: ParameterGroup_impl.hpp:55
Definition: AnisotropicEikonal.hpp:43
Definition: ParameterGroup.hpp:109
void readXML(const std::string &xml_filename)
Reads the contents of the xml file specified by xml_filename into this ParameterGroup.
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:226
const std::string ID_false
Definition: ParameterStrings.hpp:44
void setUsed() const
Definition: ParameterMapItem.hpp:58
T get(const std::string &name) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:173
Definition: ParameterGroup.hpp:111
virtual std::string getTag() const =0
This function returns a string describing the ParameterMapItem.
Definition: ParameterMapItem.hpp:48
const std::string ID_delimiter_assignment
Definition: ParameterStrings.hpp:67
const double second
Definition: Units.hpp:93
static std::string type()
void readParam(const std::string ¶m_filename)
Reads the contents of the param file specified by param_filename into this ParameterGroup.
std::string path() const
Returns the path of the parameter group.
Definition: ParameterMapItem.hpp:65
Definition: ParameterGroup.hpp:116
const std::string ID_true
Definition: ParameterStrings.hpp:43
const std::string ID_path_root
Definition: ParameterStrings.hpp:64
const std::string ID_delimiter_path
Definition: ParameterStrings.hpp:65
void insertParameter(const std::string &name, const std::string &value)
Insert a new parameter item into the group.
static std::string type()
Definition: ParameterGroup_impl.hpp:70
const std::string ID_xmltag__param_grp
Definition: ParameterStrings.hpp:46
static T convert(const ParameterMapItem &, std::string &conversion_error)