From: Peter Munch Date: Mon, 27 Jan 2020 07:22:48 +0000 (+0100) Subject: Further changes to ParameterHandler::parse_input_from_json X-Git-Tag: v9.2.0-rc1~592^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F9434%2Fhead;p=dealii.git Further changes to ParameterHandler::parse_input_from_json Conflicts: source/base/parameter_handler.cc --- diff --git a/doc/news/changes/minor/20200127PeterMunch-1 b/doc/news/changes/minor/20200127PeterMunch-1 new file mode 100644 index 0000000000..8eb9be2180 --- /dev/null +++ b/doc/news/changes/minor/20200127PeterMunch-1 @@ -0,0 +1,4 @@ +New: ParameterHandler::parse_input_from_json() and ParameterHandler::parse_input_from_xml() can skip +undifined sections and entries. +
+(Peter Munch, 2020/01/27) diff --git a/doc/news/changes/minor/20200127PeterMunch-2 b/doc/news/changes/minor/20200127PeterMunch-2 new file mode 100644 index 0000000000..9f3b68248d --- /dev/null +++ b/doc/news/changes/minor/20200127PeterMunch-2 @@ -0,0 +1,4 @@ +Improved: ParameterHandler::parse_input_from_json() can handle values of type `"a" : 1', in addition to +`"a" : {"value" : 1}'. +
+(Peter Munch, 2020/01/27) diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index 2c105a28cb..46b10bd724 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1023,7 +1023,7 @@ public: * parameter GUI (see the general documentation of this class). */ virtual void - parse_input_from_xml(std::istream &input); + parse_input_from_xml(std::istream &input, const bool skip_undefined = false); /** * Parse input from a JSON stream to populate known parameter fields. This @@ -1033,7 +1033,7 @@ public: * input. */ virtual void - parse_input_from_json(std::istream &input); + parse_input_from_json(std::istream &input, const bool skip_undefined = false); /** * Clear all contents. diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index da76d25baa..97ce2170a2 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -544,15 +544,46 @@ namespace const std::string & current_path, const char path_separator, const std::vector> &patterns, - ParameterHandler & prm) + const bool skip_undefined, + ParameterHandler &prm) { for (const auto &p : source) { // a sub-tree must either be a parameter node or a subsection - if (p.second.get_optional("value")) + if (p.second.empty()) { // set the found parameter in the destination argument - prm.set(demangle(p.first), p.second.get("value")); + if (skip_undefined) + { + try + { + prm.set(demangle(p.first), p.second.data()); + } + catch (const ParameterHandler::ExcEntryUndeclared &) + { + // ignore undeclared entry assert + } + } + else + prm.set(demangle(p.first), p.second.data()); + } + else if (p.second.get_optional("value")) + { + // set the found parameter in the destination argument + if (skip_undefined) + { + try + { + prm.set(demangle(p.first), + p.second.get("value")); + } + catch (const ParameterHandler::ExcEntryUndeclared &) + { + // ignore undeclared entry assert + } + } + else + 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 @@ -575,6 +606,7 @@ namespace current_path + path_separator + p.first), path_separator, patterns, + skip_undefined, prm); prm.leave_subsection(); } @@ -585,7 +617,8 @@ namespace void -ParameterHandler::parse_input_from_xml(std::istream &in) +ParameterHandler::parse_input_from_xml(std::istream &in, + const bool skip_undefined) { AssertThrow(in, ExcIO()); // read the XML tree assuming that (as we @@ -634,12 +667,14 @@ 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, *this); + read_xml_recursively( + my_entries, "", path_separator, patterns, skip_undefined, *this); } void -ParameterHandler::parse_input_from_json(std::istream &in) +ParameterHandler::parse_input_from_json(std::istream &in, + const bool skip_undefined) { AssertThrow(in, ExcIO()); @@ -650,7 +685,8 @@ 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, *this); + read_xml_recursively( + node_tree, "", path_separator, patterns, skip_undefined, *this); } diff --git a/tests/parameter_handler/parameter_handler_read_json_02.cc b/tests/parameter_handler/parameter_handler_read_json_02.cc new file mode 100644 index 0000000000..b1f7e0f6e6 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_read_json_02.cc @@ -0,0 +1,55 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// check ParameterHandler::parse_input_from_xml + +#include + +#include "../tests.h" + + +int +main() +{ + initlog(); + + // default values + int int1 = 0; + int int2 = 0; + int int3 = 0; + int int4 = 0; + + ParameterHandler prm; + prm.add_parameter("int1", int1); + prm.add_parameter("int2", int2); + + prm.enter_subsection("dummy"); + prm.add_parameter("int3", int3); + prm.add_parameter("int4", int4); + prm.leave_subsection(); + + // read from json + std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.prm"); + prm.parse_input_from_json(in, true); + + AssertDimension(int1, 1); + AssertDimension(int2, 2); + AssertDimension(int3, 3); + AssertDimension(int4, 4); + + return 0; +} diff --git a/tests/parameter_handler/parameter_handler_read_json_02.output b/tests/parameter_handler/parameter_handler_read_json_02.output new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_read_json_02.output @@ -0,0 +1 @@ + diff --git a/tests/parameter_handler/prm/parameter_handler_read_json_02.prm b/tests/parameter_handler/prm/parameter_handler_read_json_02.prm new file mode 100644 index 0000000000..0ed12e1a28 --- /dev/null +++ b/tests/parameter_handler/prm/parameter_handler_read_json_02.prm @@ -0,0 +1,8 @@ +{ + "int1": {"value": 1}, + "int2": 2, + "dummy" : {"int3" : 3, "int4" : 4}, + "int5": {"value": 5}, + "int6": 6, + "dummy2" : {"int7" : 7, "int8" : 8} +} \ No newline at end of file