]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Print a reduced parameter tree for XML and JSON 9707/head
authorPeter Munch <peterrmuench@gmail.com>
Sun, 22 Mar 2020 07:19:45 +0000 (08:19 +0100)
committerPeter Munch <peterrmuench@gmail.com>
Mon, 23 Mar 2020 17:25:42 +0000 (18:25 +0100)
doc/news/changes/minor/20200322PeterMunch [new file with mode: 0644]
include/deal.II/base/parameter_handler.h
source/base/parameter_handler.cc
tests/parameter_handler/parameter_handler_write_json.cc
tests/parameter_handler/parameter_handler_write_json.output
tests/parameter_handler/parameter_handler_write_xml.cc
tests/parameter_handler/parameter_handler_write_xml.output

diff --git a/doc/news/changes/minor/20200322PeterMunch b/doc/news/changes/minor/20200322PeterMunch
new file mode 100644 (file)
index 0000000..ed1fcf4
--- /dev/null
@@ -0,0 +1,4 @@
+New: The function ParameterHandler::print_parameters() can now also
+print a reduced parameter tree for XML and JSON file formats.
+<br>
+(Peter Munch, 2020/03/22)
index 483e79a5f5e051d1264110700dc438ef850071fd..6c2258872e00d3cbfa168b3fcdc848767919853c 100644 (file)
@@ -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 <tt>out</tt>.
+   * 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 <tt>false</tt>: in this case entries are printed in the same order
    * as they have been declared.
    *
+   * In the case of <tt>XML</tt> or <tt>JSON</tt>, a reduced tree, only
+   * containing the values and skipping the documentation, can be
+   * printed by setting @p print_documentation to <tt>false</tt>.
+   *
    * In <tt>Text</tt> 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
index bbe09a7b13e61fbaf74948f740e7fff3f0c1c75b..d30dd9b2a849511c10c0acdf0007a08daa651dc2 100644 (file)
@@ -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<std::string>("value"))
+          {
+            // save the value in a temporal variable
+            const auto temp = p.second.get<std::string>("value");
+            // clear node (children and value)
+            p.second.clear();
+            // set the correct value
+            p.second.put_value<std::string>(temp);
+          }
+        else if (p.second.get_optional<std::string>("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<std::string>(),
-                                  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<std::string>(), // start at the top level
     style,
     0,
index 3a43bdce81150c9e91f7506593a510005ddd3f00..50505f010a34968d4f45c593170378e878920bea 100644 (file)
@@ -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;
 }
index 3a33dbda3d083ec01437d8a623f3c4e07cddac21..824c2c36e57a72bc2b4132619251487aea5d995a 100644 (file)
     }
 }
 
+{
+    "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"
+        }
+    }
+}
+
index ad9f35bdad630fcc4be6da67e5322fb19cf895ef..af37cfe7e71b492f3ad60709a9e7a2c978ba5dfc 100644 (file)
@@ -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;
index f2911de69c31a17205bdd052148e97936e06ab8f..d1a374dedcc849f3230e031cc208a5b6431e3a86 100644 (file)
@@ -1,3 +1,5 @@
 
 <?xml version="1.0" encoding="utf-8"?>
 <ParameterHandler><int1><value>1</value><default_value>1</default_value><documentation>doc 1</documentation><pattern>0</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int1><int2><value>2</value><default_value>2</default_value><documentation>doc 2</documentation><pattern>1</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int2><Testing_25testing><double_2bdouble><value>6.1415926</value><default_value>6.1415926</default_value><documentation>docs 3</documentation><pattern>6</pattern><pattern_description>[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]</pattern_description></double_2bdouble><int_2aint><value>2</value><default_value>2</default_value><documentation/><pattern>5</pattern><pattern_description>[Integer range -2147483648...2147483647 (inclusive)]</pattern_description></int_2aint><string_26list><value>&lt; &amp; &gt; ; /</value><default_value>&lt; &amp; &gt; ; /</default_value><documentation>docs 1</documentation><pattern>4</pattern><pattern_description>[Anything]</pattern_description></string_26list></Testing_25testing><ss1><double_201><value>1.234</value><default_value>1.234</default_value><documentation>doc 3</documentation><pattern>2</pattern><pattern_description>[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]</pattern_description></double_201><ss2><double_202><value>4.321</value><default_value>4.321</default_value><documentation>doc 4</documentation><pattern>3</pattern><pattern_description>[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]</pattern_description></double_202></ss2></ss1></ParameterHandler>
+<?xml version="1.0" encoding="utf-8"?>
+<ParameterHandler><int1>1</int1><int2>2</int2><Testing_25testing><double_2bdouble>6.1415926</double_2bdouble><int_2aint>2</int_2aint><string_26list>&lt; &amp; &gt; ; /</string_26list></Testing_25testing><ss1><double_201>1.234</double_201><ss2><double_202>4.321</double_202></ss2></ss1></ParameterHandler>

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.