]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Further changes to ParameterHandler::parse_input_from_json 9434/head
authorPeter Munch <peterrmuench@gmail.com>
Mon, 27 Jan 2020 07:22:48 +0000 (08:22 +0100)
committerPeter Munch <peterrmuench@gmail.com>
Wed, 29 Jan 2020 10:03:06 +0000 (11:03 +0100)
Conflicts:
source/base/parameter_handler.cc

doc/news/changes/minor/20200127PeterMunch-1 [new file with mode: 0644]
doc/news/changes/minor/20200127PeterMunch-2 [new file with mode: 0644]
include/deal.II/base/parameter_handler.h
source/base/parameter_handler.cc
tests/parameter_handler/parameter_handler_read_json_02.cc [new file with mode: 0644]
tests/parameter_handler/parameter_handler_read_json_02.output [new file with mode: 0644]
tests/parameter_handler/prm/parameter_handler_read_json_02.prm [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20200127PeterMunch-1 b/doc/news/changes/minor/20200127PeterMunch-1
new file mode 100644 (file)
index 0000000..8eb9be2
--- /dev/null
@@ -0,0 +1,4 @@
+New: ParameterHandler::parse_input_from_json() and  ParameterHandler::parse_input_from_xml() can skip
+undifined sections and entries.
+<br>
+(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 (file)
index 0000000..9f3b682
--- /dev/null
@@ -0,0 +1,4 @@
+Improved: ParameterHandler::parse_input_from_json() can handle values of type `"a" : 1', in addition to
+`"a" : {"value" : 1}'.
+<br>
+(Peter Munch, 2020/01/27)
index 2c105a28cb45c127ae829404eac70d95e0965efa..46b10bd7244123f1019cb7ad69ea0399668da708 100644 (file)
@@ -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.
index da76d25baaf0fefdbbcb9e2f0db4903f47f9d5c8..97ce2170a2b8615c7f20e0db5fca3dbc3982937b 100644 (file)
@@ -544,15 +544,46 @@ namespace
     const std::string &                current_path,
     const char                         path_separator,
     const std::vector<std::unique_ptr<const Patterns::PatternBase>> &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<std::string>("value"))
+        if (p.second.empty())
           {
             // set the found parameter in the destination argument
-            prm.set(demangle(p.first), p.second.get<std::string>("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<std::string>("value"))
+          {
+            // set the found parameter in the destination argument
+            if (skip_undefined)
+              {
+                try
+                  {
+                    prm.set(demangle(p.first),
+                            p.second.get<std::string>("value"));
+                  }
+                catch (const ParameterHandler::ExcEntryUndeclared &)
+                  {
+                    // ignore undeclared entry assert
+                  }
+              }
+            else
+              prm.set(demangle(p.first), p.second.get<std::string>("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 (file)
index 0000000..b1f7e0f
--- /dev/null
@@ -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 <deal.II/base/parameter_handler.h>
+
+#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 (file)
index 0000000..8b13789
--- /dev/null
@@ -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 (file)
index 0000000..0ed12e1
--- /dev/null
@@ -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

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.