From: Peter Munch Date: Thu, 11 Jan 2024 08:29:31 +0000 (+0100) Subject: ParameterHandler::parse_input_from_json(): mangle entries internally X-Git-Tag: v9.6.0-rc1~477^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8ef99c869c7ed92c8028991310ef9067dddc2e0;p=dealii.git ParameterHandler::parse_input_from_json(): mangle entries internally Co-authored-by: Magdalena Schreter-Fleischhacker --- diff --git a/doc/news/changes/incompatibilities/20240126SchreterMunch b/doc/news/changes/incompatibilities/20240126SchreterMunch new file mode 100644 index 0000000000..1464386dbd --- /dev/null +++ b/doc/news/changes/incompatibilities/20240126SchreterMunch @@ -0,0 +1,4 @@ +Changed:`ParameterHandler::parse_input_from_json()` reads now a json-file +without mangled parameter names. +
+(Magdalena Schreter, Peter Munch, 2024/01/26) diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index d713ee9310..fefa8fc96a 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -331,15 +331,17 @@ namespace } /** - * Demangle all parameters recursively and attach them to @p tree_out. + * Mangle (@p do_mangle = true) or demangle (@p do_mangle = false) all + * parameters recursively and attach them to @p tree_out. * @p is_parameter_or_alias_node indicates, whether a given node * @p tree_in is a parameter node or an alias node (as opposed to being * a subsection). */ void - recursively_demangle(const boost::property_tree::ptree &tree_in, - boost::property_tree::ptree &tree_out, - const bool is_parameter_or_alias_node = false) + recursively_mangle_or_demangle(const boost::property_tree::ptree &tree_in, + boost::property_tree::ptree &tree_out, + const bool do_mangle, + const bool is_parameter_or_alias_node = false) { for (const auto &p : tree_in) { @@ -354,11 +356,13 @@ namespace if (const auto val = p.second.get_value_optional()) temp.put_value(*val); - recursively_demangle(p.second, - temp, - is_parameter_node(p.second) || - is_alias_node(p.second)); - tree_out.put_child(demangle(p.first), temp); + recursively_mangle_or_demangle(p.second, + temp, + do_mangle, + is_parameter_node(p.second) || + is_alias_node(p.second)); + tree_out.put_child(do_mangle ? mangle(p.first) : demangle(p.first), + temp); } } } @@ -815,9 +819,14 @@ 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. + // This function can only read mangled files. Therefore, we create a mangeled + // tree first. + boost::property_tree::ptree node_tree_mangled; + recursively_mangle_or_demangle(node_tree, + node_tree_mangled, + true /*do_mangle*/); read_xml_recursively( - node_tree, "", path_separator, patterns, skip_undefined, *this); + node_tree_mangled, "", path_separator, patterns, skip_undefined, *this); } @@ -1359,9 +1368,11 @@ ParameterHandler::print_parameters(std::ostream &out, if ((style & JSON) != 0) { - boost::property_tree::ptree current_entries_damangled; - recursively_demangle(current_entries, current_entries_damangled); - write_json(out, current_entries_damangled); + boost::property_tree::ptree current_entries_demangled; + recursively_mangle_or_demangle(current_entries, + current_entries_demangled, + false /*do_mangle*/); + write_json(out, current_entries_demangled); return out; } diff --git a/tests/parameter_handler/parameter_handler_read_json.cc b/tests/parameter_handler/parameter_handler_read_json_01.cc similarity index 77% rename from tests/parameter_handler/parameter_handler_read_json.cc rename to tests/parameter_handler/parameter_handler_read_json_01.cc index f3816b0912..a1d8be6702 100644 --- a/tests/parameter_handler/parameter_handler_read_json.cc +++ b/tests/parameter_handler/parameter_handler_read_json_01.cc @@ -14,7 +14,7 @@ -// check ParameterHandler::parse_input_from_xml +// check ParameterHandler::parse_input_from_json #include @@ -27,13 +27,14 @@ 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; + int int1 = 1; + int int2 = 2; + double double1 = 1.234; + double double2 = 4.321; + std::string str = "< & > ; /"; + int intint = 2; + double doubledouble = 6.1415926; + double double_double = 0; ParameterHandler prm; prm.add_parameter("int1", int1, "doc 1"); @@ -56,11 +57,12 @@ main() prm.add_parameter("string&list", str, "docs 1"); prm.add_parameter("int*int", intint); prm.add_parameter("double+double", doubledouble, "docs 3"); + prm.add_parameter("double_double", double_double, "docs 4"); } prm.leave_subsection(); // read from json - std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json.prm"); + std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_01.json"); prm.parse_input_from_json(in); Assert(int1 == 2, ExcNotImplemented()); @@ -70,6 +72,7 @@ main() Assert(str == "< & > ; /", ExcNotImplemented()); Assert(intint == 2, ExcNotImplemented()); Assert(doubledouble == 7.1415926, ExcNotImplemented()); + Assert(double_double == 1.234, ExcNotImplemented()); // write it out again prm.print_parameters(deallog.get_file_stream(), ParameterHandler::JSON); diff --git a/tests/parameter_handler/parameter_handler_read_json.output b/tests/parameter_handler/parameter_handler_read_json_01.output similarity index 87% rename from tests/parameter_handler/parameter_handler_read_json.output rename to tests/parameter_handler/parameter_handler_read_json_01.output index 70095fb249..ac315ef54c 100644 --- a/tests/parameter_handler/parameter_handler_read_json.output +++ b/tests/parameter_handler/parameter_handler_read_json_01.output @@ -29,6 +29,15 @@ "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", "actions": "6" }, + "double_double": + { + "value": "1.234", + "default_value": "0", + "documentation": "docs 4", + "pattern": "7", + "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", + "actions": "7" + }, "int*int": { "value": "2", diff --git a/tests/parameter_handler/parameter_handler_read_json_02.cc b/tests/parameter_handler/parameter_handler_read_json_02.cc index 92c87ee457..d29bbb1640 100644 --- a/tests/parameter_handler/parameter_handler_read_json_02.cc +++ b/tests/parameter_handler/parameter_handler_read_json_02.cc @@ -42,7 +42,7 @@ main() prm.leave_subsection(); // read from json - std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.prm"); + std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.json"); prm.parse_input_from_json(in, true); AssertDimension(int1, 1); diff --git a/tests/parameter_handler/parameter_handler_read_json_03.cc b/tests/parameter_handler/parameter_handler_read_json_03.cc index d49c41123d..a3f0ceb79d 100644 --- a/tests/parameter_handler/parameter_handler_read_json_03.cc +++ b/tests/parameter_handler/parameter_handler_read_json_03.cc @@ -31,6 +31,7 @@ main() int int2 = 0; int int3 = 0; int int4 = 0; + int int5 = 0; ParameterHandler prm; prm.add_parameter("int1", int1); @@ -41,6 +42,10 @@ main() prm.add_parameter("int4", int4); prm.leave_subsection(); + prm.enter_subsection("test_underscore"); + prm.add_parameter("int_5", int5); + prm.leave_subsection(); + std::string source = SOURCE_DIR; std::string filename = source + "/prm/parameter_handler_read_json_03.json"; prm.parse_input(filename, "", true, true); @@ -49,6 +54,7 @@ main() AssertDimension(int2, 2); AssertDimension(int3, 3); AssertDimension(int4, 4); + AssertDimension(int5, 10); return 0; } diff --git a/tests/parameter_handler/prm/parameter_handler_read_json.prm b/tests/parameter_handler/prm/parameter_handler_read_json_01.json similarity index 81% rename from tests/parameter_handler/prm/parameter_handler_read_json.prm rename to tests/parameter_handler/prm/parameter_handler_read_json_01.json index 989cdc8a07..af93caeda5 100644 --- a/tests/parameter_handler/prm/parameter_handler_read_json.prm +++ b/tests/parameter_handler/prm/parameter_handler_read_json_01.json @@ -18,7 +18,7 @@ }, "ss1": { - "double_201": + "double 1": { "value": "2.234", "default_value": "1.234", @@ -28,7 +28,7 @@ }, "ss2": { - "double_202": + "double 2": { "value": "5.321", "default_value": "4.321", @@ -38,9 +38,9 @@ } } }, - "Testing_25testing": + "Testing%testing": { - "string_26list": + "string&list": { "value": "< & > ; \/", "default_value": "< & > ; \/", @@ -48,7 +48,7 @@ "pattern": "4", "pattern_description": "[Anything]" }, - "int_2aint": + "int*int": { "value": "2", "default_value": "2", @@ -56,13 +56,21 @@ "pattern": "5", "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]" }, - "double_2bdouble": + "double+double": { "value": "7.1415926", "default_value": "6.1415926", "documentation": "docs 3", "pattern": "6", "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]" + }, + "double_double": + { + "value": "1.234", + "default_value": "0", + "documentation": "docs 4", + "pattern": "7", + "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]" } } } diff --git a/tests/parameter_handler/prm/parameter_handler_read_json_02.prm b/tests/parameter_handler/prm/parameter_handler_read_json_02.json similarity index 100% rename from tests/parameter_handler/prm/parameter_handler_read_json_02.prm rename to tests/parameter_handler/prm/parameter_handler_read_json_02.json diff --git a/tests/parameter_handler/prm/parameter_handler_read_json_03.json b/tests/parameter_handler/prm/parameter_handler_read_json_03.json index 0ed12e1a28..f7c74e3031 100644 --- a/tests/parameter_handler/prm/parameter_handler_read_json_03.json +++ b/tests/parameter_handler/prm/parameter_handler_read_json_03.json @@ -4,5 +4,6 @@ "dummy" : {"int3" : 3, "int4" : 4}, "int5": {"value": 5}, "int6": 6, - "dummy2" : {"int7" : 7, "int8" : 8} -} \ No newline at end of file + "dummy2" : {"int7" : 7, "int8" : 8}, + "test_underscore": {"int_5": 10} +}