From: Peter Munch Date: Fri, 24 Jan 2020 15:36:04 +0000 (+0100) Subject: Fix ParameterHandler::read_xml_recursively for actions X-Git-Tag: v9.2.0-rc1~603^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d002a22c75a5764e157e405c8d66c6c7bf347b2;p=dealii.git Fix ParameterHandler::read_xml_recursively for actions --- diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index fdaaf0d61f..8f48ddb116 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1574,17 +1574,11 @@ public: * Exception for when an entry in an XML parameter file does not match the * provided pattern. The arguments are, in order, the entry value, entry * name, and a description of the pattern. + * + * @deprecated Use ExcValueDoesNotMatchPattern instead of ExcInvalidEntryForPatternXML. */ - DeclException3(ExcInvalidEntryForPatternXML, - std::string, - std::string, - std::string, - << " The entry value \n" - << " " << arg1 << '\n' - << " for the entry named\n" - << " " << arg2 << '\n' - << " does not match the given pattern:\n" - << " " << arg3); + using ExcInvalidEntryForPatternXML DEAL_II_DEPRECATED = + ExcValueDoesNotMatchPattern; /** * Exception for when the file given in an include statement cannot be diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 13a3d4ecb5..e764da8981 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -482,7 +482,7 @@ namespace const std::string & current_path, const char path_separator, const std::vector> &patterns, - boost::property_tree::ptree &destination) + ParameterHandler & prm) { for (boost::property_tree::ptree::const_iterator p = source.begin(); p != source.end(); @@ -491,32 +491,8 @@ namespace // a sub-tree must either be a parameter node or a subsection if (p->second.get_optional("value")) { - // make sure we have a corresponding entry in the destination - // object as well - const std::string full_path = - (current_path.empty() ? p->first : - current_path + path_separator + p->first); - - const std::string new_value = p->second.get("value"); - AssertThrow(destination.get_optional(full_path) && - destination.get_optional( - full_path + path_separator + "value"), - ParameterHandler::ExcEntryUndeclared(p->first)); - - // first make sure that the new entry actually satisfies its - // constraints - const unsigned int pattern_index = destination.get( - full_path + path_separator + "pattern"); - AssertThrow(patterns[pattern_index]->match(new_value), - ParameterHandler::ExcInvalidEntryForPatternXML - // XML entries sometimes have extra surrounding - // newlines - (Utilities::trim(new_value), - p->first, - patterns[pattern_index]->description())); - // set the found parameter in the destination argument - destination.put(full_path + path_separator + "value", new_value); + prm.set(demangle(p->first), p->second.get("value")); // this node might have sub-nodes in addition to "value", such as // "default_value", "documentation", etc. we might at some point @@ -532,13 +508,15 @@ namespace else { // it must be a subsection + prm.enter_subsection(demangle(p->first)); read_xml_recursively(p->second, (current_path.empty() ? p->first : current_path + path_separator + p->first), path_separator, patterns, - destination); + prm); + prm.leave_subsection(); } } } @@ -596,7 +574,7 @@ ParameterHandler::parse_input_from_xml(std::istream &in) const boost::property_tree::ptree &my_entries = single_node_tree.get_child("ParameterHandler"); - read_xml_recursively(my_entries, "", path_separator, patterns, *entries); + read_xml_recursively(my_entries, "", path_separator, patterns, *this); } @@ -612,7 +590,7 @@ ParameterHandler::parse_input_from_json(std::istream &in) // The xml function is reused to read in the xml into the parameter file. // This means that only mangled files can be read. - read_xml_recursively(node_tree, "", path_separator, patterns, *entries); + read_xml_recursively(node_tree, "", path_separator, patterns, *this); } diff --git a/tests/parameter_handler/parameter_handler_read_json.cc b/tests/parameter_handler/parameter_handler_read_json.cc index b6d68e5825..fd380e2513 100644 --- a/tests/parameter_handler/parameter_handler_read_json.cc +++ b/tests/parameter_handler/parameter_handler_read_json.cc @@ -27,16 +27,25 @@ main() { initlog(); + // default values + int int1 = 1; + int int2 = 2; + double double1 = 1.234; + double double2 = 4.321; + std::string str = "< & > ; /"; + int intint = 2; + double doubledouble = 6.1415926; + ParameterHandler prm; - prm.declare_entry("int1", "1", Patterns::Integer(), "doc 1"); - prm.declare_entry("int2", "2", Patterns::Integer(), "doc 2"); + prm.add_parameter("int1", int1, "doc 1"); + prm.add_parameter("int2", int2, "doc 2"); prm.enter_subsection("ss1"); { - prm.declare_entry("double 1", "1.234", Patterns::Double(), "doc 3"); + prm.add_parameter("double 1", double1, "doc 3"); prm.enter_subsection("ss2"); { - prm.declare_entry("double 2", "4.321", Patterns::Double(), "doc 4"); + prm.add_parameter("double 2", double2, "doc 4"); } prm.leave_subsection(); } @@ -45,15 +54,9 @@ main() // things with strange characters prm.enter_subsection("Testing%testing"); { - prm.declare_entry("string&list", - "< & > ; /", - Patterns::Anything(), - "docs 1"); - prm.declare_entry("int*int", "2", Patterns::Integer()); - prm.declare_entry("double+double", - "6.1415926", - Patterns::Double(), - "docs 3"); + prm.add_parameter("string&list", str, "docs 1"); + prm.add_parameter("int*int", intint); + prm.add_parameter("double+double", doubledouble, "docs 3"); } prm.leave_subsection(); @@ -61,6 +64,14 @@ main() std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json.prm"); prm.parse_input_from_json(in); + Assert(int1 == 2, ExcNotImplemented()); + Assert(int2 == 3, ExcNotImplemented()); + Assert(double1 == 2.234, ExcNotImplemented()); + Assert(double2 == 5.321, ExcNotImplemented()); + Assert(str == "< & > ; /", ExcNotImplemented()); + Assert(intint == 2, ExcNotImplemented()); + Assert(doubledouble == 7.1415926, ExcNotImplemented()); + // write it out again prm.print_parameters(deallog.get_file_stream(), ParameterHandler::JSON); deallog.get_file_stream() << std::endl; diff --git a/tests/parameter_handler/parameter_handler_read_json.output b/tests/parameter_handler/parameter_handler_read_json.output index fd2d434348..f2790c891d 100644 --- a/tests/parameter_handler/parameter_handler_read_json.output +++ b/tests/parameter_handler/parameter_handler_read_json.output @@ -6,7 +6,8 @@ "default_value": "1", "documentation": "doc 1", "pattern": "0", - "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]" + "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]", + "actions": "0" }, "int2": { @@ -14,7 +15,8 @@ "default_value": "2", "documentation": "doc 2", "pattern": "1", - "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]" + "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]", + "actions": "1" }, "ss1": { @@ -24,7 +26,8 @@ "default_value": "1.234", "documentation": "doc 3", "pattern": "2", - "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]" + "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", + "actions": "2" }, "ss2": { @@ -34,7 +37,8 @@ "default_value": "4.321", "documentation": "doc 4", "pattern": "3", - "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]" + "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", + "actions": "3" } } }, @@ -46,7 +50,8 @@ "default_value": "< & > ; \/", "documentation": "docs 1", "pattern": "4", - "pattern_description": "[Anything]" + "pattern_description": "[Anything]", + "actions": "4" }, "int_2aint": { @@ -54,15 +59,17 @@ "default_value": "2", "documentation": "", "pattern": "5", - "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]" + "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]", + "actions": "5" }, "double_2bdouble": { "value": "7.1415926", - "default_value": "6.1415926", + "default_value": "6.14159", "documentation": "docs 3", "pattern": "6", - "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]" + "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", + "actions": "6" } } } diff --git a/tests/parameter_handler/parameter_handler_read_xml_error_01.output b/tests/parameter_handler/parameter_handler_read_xml_error_01.output index 5e12ed85b0..b1dd6505d7 100644 --- a/tests/parameter_handler/parameter_handler_read_xml_error_01.output +++ b/tests/parameter_handler/parameter_handler_read_xml_error_01.output @@ -1,3 +1,3 @@ -DEAL::ParameterHandler::ExcEntryUndeclared (p->first) -You can't ask for entry you have not yet declared. +DEAL::ExcEntryUndeclared(entry_string) + You can't ask for entry you have not yet declared. diff --git a/tests/parameter_handler/parameter_handler_read_xml_error_02.cc b/tests/parameter_handler/parameter_handler_read_xml_error_02.cc index 36eeeb97ea..9fc7e917fd 100644 --- a/tests/parameter_handler/parameter_handler_read_xml_error_02.cc +++ b/tests/parameter_handler/parameter_handler_read_xml_error_02.cc @@ -64,7 +64,7 @@ main() { prm.parse_input_from_xml(in); } - catch (const ParameterHandler::ExcInvalidEntryForPatternXML &exc) + catch (const ParameterHandler::ExcValueDoesNotMatchPattern &exc) { deallog << exc.get_exc_name() << std::endl; exc.print_info(deallog.get_file_stream()); diff --git a/tests/parameter_handler/parameter_handler_read_xml_error_02.output b/tests/parameter_handler/parameter_handler_read_xml_error_02.output index 1ec9a3ecba..2cd836d3dd 100644 --- a/tests/parameter_handler/parameter_handler_read_xml_error_02.output +++ b/tests/parameter_handler/parameter_handler_read_xml_error_02.output @@ -1,8 +1,3 @@ -DEAL::ParameterHandler::ExcInvalidEntryForPatternXML (Utilities::trim(new_value), p->first, patterns[pattern_index]->description()) - The entry value - __2 - for the entry named - int_2aint - does not match the given pattern: - [Integer range -2147483648...2147483647 (inclusive)] +DEAL::ExcValueDoesNotMatchPattern(new_value, entries->get( path + path_separator + "pattern_description")) + The string <__2> does not match the given pattern <[Integer range -2147483648...2147483647 (inclusive)]>. diff --git a/tests/parameter_handler/parameter_handler_read_xml_error_04.cc b/tests/parameter_handler/parameter_handler_read_xml_error_04.cc index 6d75bd986d..ae3c448a96 100644 --- a/tests/parameter_handler/parameter_handler_read_xml_error_04.cc +++ b/tests/parameter_handler/parameter_handler_read_xml_error_04.cc @@ -64,7 +64,7 @@ main() { prm.parse_input_from_xml(in); } - catch (const ParameterHandler::ExcInvalidEntryForPatternXML &exc) + catch (const ParameterHandler::ExcValueDoesNotMatchPattern &exc) { deallog << exc.get_exc_name() << std::endl; exc.print_info(deallog.get_file_stream()); diff --git a/tests/parameter_handler/parameter_handler_read_xml_error_04.output b/tests/parameter_handler/parameter_handler_read_xml_error_04.output index e8962e3456..1fa8d34276 100644 --- a/tests/parameter_handler/parameter_handler_read_xml_error_04.output +++ b/tests/parameter_handler/parameter_handler_read_xml_error_04.output @@ -1,8 +1,3 @@ -DEAL::ParameterHandler::ExcInvalidEntryForPatternXML (Utilities::trim(new_value), p->first, patterns[pattern_index]->description()) - The entry value - 2x - for the entry named - int1 - does not match the given pattern: - [Integer range -2147483648...2147483647 (inclusive)] +DEAL::ExcValueDoesNotMatchPattern(new_value, entries->get( path + path_separator + "pattern_description")) + The string <2x> does not match the given pattern <[Integer range -2147483648...2147483647 (inclusive)]>.