From: Peter Munch Date: Sun, 22 Mar 2020 07:19:45 +0000 (+0100) Subject: Print a reduced parameter tree for XML and JSON X-Git-Tag: v9.2.0-rc1~294^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5d3f0e20a4bbe1b351f71570cf60ead6d212e4b;p=dealii.git Print a reduced parameter tree for XML and JSON --- diff --git a/doc/news/changes/minor/20200322PeterMunch b/doc/news/changes/minor/20200322PeterMunch new file mode 100644 index 0000000000..ed1fcf47dc --- /dev/null +++ b/doc/news/changes/minor/20200322PeterMunch @@ -0,0 +1,4 @@ +New: The function ParameterHandler::print_parameters() can now also +print a reduced parameter tree for XML and JSON file formats. +
+(Peter Munch, 2020/03/22) diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index 483e79a5f5..6c2258872e 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -884,7 +884,19 @@ public: * Write input for ParameterHandler without comments or changed default * values. */ - ShortText = 193 + ShortText = 193, + + /** + * Write input for ParameterHandler without comments or changed default + * values as a XML file. + */ + ShortXML = 194, + + /** + * Write input for ParameterHandler without comments or changed default + * values as a JSON file. + */ + ShortJSON = 195 }; @@ -1340,14 +1352,18 @@ public: set(const std::string &entry_name, const bool new_value); /** - * Print all parameters with the given style to out. + * Print all parameters with the given style to @p out. * * Before printing, all current parameters and subsections are sorted * alphabetically by default. * This behavior can be disabled setting the last parameter @p sort_alphabetical - * to @p false: in this case entries are printed in the same order + * to false: in this case entries are printed in the same order * as they have been declared. * + * In the case of XML or JSON, a reduced tree, only + * containing the values and skipping the documentation, can be + * printed by setting @p print_documentation to false. + * * In Text format, the output is formatted in such a way that it is * possible to use it for later input again. This is most useful to record * the parameters for a specific run, since if you output the parameters diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index bbe09a7b13..d30dd9b2a8 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -612,6 +612,51 @@ namespace } } } + + // Recursively go through the @p source tree and collapse the nodes of the + // format: + // + //"key": + // { + // "value" : "val", + // "default_value" : "...", + // "documentation" : "...", + // "pattern" : "...", + // "pattern_description": "..." + // }, + // + // to + // + // "key" : "val"; + // + // As an example a JSON file is shown. However, this function also works for + // XML since both formats are build around the same BOOST data structures. + // + // This function is strongly based on read_xml_recursively(). + void + recursively_remove_documentation_from_tree( + boost::property_tree::ptree &source) + { + for (auto &p : source) + { + if (p.second.get_optional("value")) + { + // save the value in a temporal variable + const auto temp = p.second.get("value"); + // clear node (children and value) + p.second.clear(); + // set the correct value + p.second.put_value(temp); + } + else if (p.second.get_optional("alias")) + {} + else + { + // it must be a subsection + recursively_remove_documentation_from_tree(p.second); + } + } + } } // namespace @@ -1147,20 +1192,16 @@ ParameterHandler::print_parameters(std::ostream & out, // Create entries copy and sort it, if needed. // In this way we ensure that the class state is never // modified by this function. - boost::property_tree::ptree sorted_entries; - boost::property_tree::ptree *current_entries = entries.get(); + boost::property_tree::ptree current_entries = *entries.get(); // Sort parameters alphabetically, if needed. if (sort_alphabetical) { - sorted_entries = *entries; - current_entries = &sorted_entries; - // Dive recursively into the subsections, // starting from the top level. recursively_sort_parameters(path_separator, std::vector(), - sorted_entries); + current_entries); } // we'll have to print some text that is padded with spaces; @@ -1174,7 +1215,15 @@ ParameterHandler::print_parameters(std::ostream & out, // we treat XML and JSON is one step via BOOST, whereas all of the others are // done recursively in our own code. take care of the two special formats // first - if (style == XML) + + // explicity eliminate the documentation from the tree if requested + if (style == ShortXML || style == ShortJSON) + { + // modify the copy of the tree + recursively_remove_documentation_from_tree(current_entries); + } + + if (style == XML || style == ShortXML) { // call the writer function and exit as there is nothing // further to do down in this function @@ -1186,15 +1235,15 @@ ParameterHandler::print_parameters(std::ostream & out, // single top-level node "ParameterHandler" and // assign the existing tree under it boost::property_tree::ptree single_node_tree; - single_node_tree.add_child("ParameterHandler", *current_entries); + single_node_tree.add_child("ParameterHandler", current_entries); write_xml(out, single_node_tree); return out; } - if (style == JSON) + if (style == JSON || style == ShortJSON) { - write_json(out, *current_entries); + write_json(out, current_entries); return out; } @@ -1225,7 +1274,7 @@ ParameterHandler::print_parameters(std::ostream & out, // dive recursively into the subsections recursively_print_parameters( - *current_entries, + current_entries, std::vector(), // start at the top level style, 0, diff --git a/tests/parameter_handler/parameter_handler_write_json.cc b/tests/parameter_handler/parameter_handler_write_json.cc index 3a43bdce81..50505f010a 100644 --- a/tests/parameter_handler/parameter_handler_write_json.cc +++ b/tests/parameter_handler/parameter_handler_write_json.cc @@ -59,8 +59,14 @@ main() prm.leave_subsection(); - prm.print_parameters(deallog.get_file_stream(), ParameterHandler::JSON); + prm.print_parameters(deallog.get_file_stream(), ParameterHandler::JSON, true); deallog.get_file_stream() << std::endl; + prm.print_parameters(deallog.get_file_stream(), + ParameterHandler::ShortJSON, + true); + deallog.get_file_stream() << std::endl; + + return 0; } diff --git a/tests/parameter_handler/parameter_handler_write_json.output b/tests/parameter_handler/parameter_handler_write_json.output index 3a33dbda3d..824c2c36e5 100644 --- a/tests/parameter_handler/parameter_handler_write_json.output +++ b/tests/parameter_handler/parameter_handler_write_json.output @@ -67,3 +67,22 @@ } } +{ + "int1": "1", + "int2": "2", + "Testing_25testing": + { + "double_2bdouble": "6.1415926", + "int_2aint": "2", + "string_26list": "< & > ; \/" + }, + "ss1": + { + "double_201": "1.234", + "ss2": + { + "double_202": "4.321" + } + } +} + diff --git a/tests/parameter_handler/parameter_handler_write_xml.cc b/tests/parameter_handler/parameter_handler_write_xml.cc index ad9f35bdad..af37cfe7e7 100644 --- a/tests/parameter_handler/parameter_handler_write_xml.cc +++ b/tests/parameter_handler/parameter_handler_write_xml.cc @@ -59,7 +59,12 @@ main() prm.leave_subsection(); - prm.print_parameters(deallog.get_file_stream(), ParameterHandler::XML); + prm.print_parameters(deallog.get_file_stream(), ParameterHandler::XML, true); + deallog.get_file_stream() << std::endl; + + prm.print_parameters(deallog.get_file_stream(), + ParameterHandler::ShortXML, + true); deallog.get_file_stream() << std::endl; return 0; diff --git a/tests/parameter_handler/parameter_handler_write_xml.output b/tests/parameter_handler/parameter_handler_write_xml.output index f2911de69c..d1a374dedc 100644 --- a/tests/parameter_handler/parameter_handler_write_xml.output +++ b/tests/parameter_handler/parameter_handler_write_xml.output @@ -1,3 +1,5 @@ 11doc 10[Integer range -2147483648...2147483647 (inclusive)]22doc 21[Integer range -2147483648...2147483647 (inclusive)]6.14159266.1415926docs 36[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]225[Integer range -2147483648...2147483647 (inclusive)]< & > ; /< & > ; /docs 14[Anything]1.2341.234doc 32[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]4.3214.321doc 43[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)] + +126.14159262< & > ; /1.2344.321