]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix ParameterHandler::read_xml_recursively for actions 9430/head
authorPeter Munch <peterrmuench@gmail.com>
Fri, 24 Jan 2020 15:36:04 +0000 (16:36 +0100)
committerPeter Munch <peterrmuench@gmail.com>
Mon, 27 Jan 2020 19:12:05 +0000 (20:12 +0100)
include/deal.II/base/parameter_handler.h
source/base/parameter_handler.cc
tests/parameter_handler/parameter_handler_read_json.cc
tests/parameter_handler/parameter_handler_read_json.output
tests/parameter_handler/parameter_handler_read_xml_error_01.output
tests/parameter_handler/parameter_handler_read_xml_error_02.cc
tests/parameter_handler/parameter_handler_read_xml_error_02.output
tests/parameter_handler/parameter_handler_read_xml_error_04.cc
tests/parameter_handler/parameter_handler_read_xml_error_04.output

index fdaaf0d61f358381553de192d919ce483c03c3ee..8f48ddb11601cfbbfd642d22498d628393e94520 100644 (file)
@@ -1574,17 +1574,11 @@ public:
    * Exception for when an entry in an XML parameter file does not match the
    * provided pattern. The arguments are, in order, the entry value, entry
    * name, and a description of the pattern.
+   *
+   * @deprecated Use ExcValueDoesNotMatchPattern instead of ExcInvalidEntryForPatternXML.
    */
-  DeclException3(ExcInvalidEntryForPatternXML,
-                 std::string,
-                 std::string,
-                 std::string,
-                 << "    The entry value \n"
-                 << "        " << arg1 << '\n'
-                 << "    for the entry named\n"
-                 << "        " << arg2 << '\n'
-                 << "    does not match the given pattern:\n"
-                 << "        " << arg3);
+  using ExcInvalidEntryForPatternXML DEAL_II_DEPRECATED =
+    ExcValueDoesNotMatchPattern;
 
   /**
    * Exception for when the file given in an include statement cannot be
index 13a3d4ecb5e6d0a068093d3cba0e9b26ff99ffda..e764da8981f5e5f16ebaeae91af4202b94bf5722 100644 (file)
@@ -482,7 +482,7 @@ namespace
     const std::string &                current_path,
     const char                         path_separator,
     const std::vector<std::unique_ptr<const Patterns::PatternBase>> &patterns,
-    boost::property_tree::ptree &destination)
+    ParameterHandler &                                               prm)
   {
     for (boost::property_tree::ptree::const_iterator p = source.begin();
          p != source.end();
@@ -491,32 +491,8 @@ namespace
         // a sub-tree must either be a parameter node or a subsection
         if (p->second.get_optional<std::string>("value"))
           {
-            // make sure we have a corresponding entry in the destination
-            // object as well
-            const std::string full_path =
-              (current_path.empty() ? p->first :
-                                      current_path + path_separator + p->first);
-
-            const std::string new_value = p->second.get<std::string>("value");
-            AssertThrow(destination.get_optional<std::string>(full_path) &&
-                          destination.get_optional<std::string>(
-                            full_path + path_separator + "value"),
-                        ParameterHandler::ExcEntryUndeclared(p->first));
-
-            // first make sure that the new entry actually satisfies its
-            // constraints
-            const unsigned int pattern_index = destination.get<unsigned int>(
-              full_path + path_separator + "pattern");
-            AssertThrow(patterns[pattern_index]->match(new_value),
-                        ParameterHandler::ExcInvalidEntryForPatternXML
-                        // XML entries sometimes have extra surrounding
-                        // newlines
-                        (Utilities::trim(new_value),
-                         p->first,
-                         patterns[pattern_index]->description()));
-
             // set the found parameter in the destination argument
-            destination.put(full_path + path_separator + "value", new_value);
+            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
@@ -532,13 +508,15 @@ namespace
         else
           {
             // it must be a subsection
+            prm.enter_subsection(demangle(p->first));
             read_xml_recursively(p->second,
                                  (current_path.empty() ?
                                     p->first :
                                     current_path + path_separator + p->first),
                                  path_separator,
                                  patterns,
-                                 destination);
+                                 prm);
+            prm.leave_subsection();
           }
       }
   }
@@ -596,7 +574,7 @@ 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, *entries);
+  read_xml_recursively(my_entries, "", path_separator, patterns, *this);
 }
 
 
@@ -612,7 +590,7 @@ 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, *entries);
+  read_xml_recursively(node_tree, "", path_separator, patterns, *this);
 }
 
 
index b6d68e58256149970506d451aec51b4998dc6a25..fd380e251310919a5a2cb12f3dbb9271295a1a44 100644 (file)
@@ -27,16 +27,25 @@ 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;
+
   ParameterHandler prm;
-  prm.declare_entry("int1", "1", Patterns::Integer(), "doc 1");
-  prm.declare_entry("int2", "2", Patterns::Integer(), "doc 2");
+  prm.add_parameter("int1", int1, "doc 1");
+  prm.add_parameter("int2", int2, "doc 2");
   prm.enter_subsection("ss1");
   {
-    prm.declare_entry("double 1", "1.234", Patterns::Double(), "doc 3");
+    prm.add_parameter("double 1", double1, "doc 3");
 
     prm.enter_subsection("ss2");
     {
-      prm.declare_entry("double 2", "4.321", Patterns::Double(), "doc 4");
+      prm.add_parameter("double 2", double2, "doc 4");
     }
     prm.leave_subsection();
   }
@@ -45,15 +54,9 @@ main()
   // things with strange characters
   prm.enter_subsection("Testing%testing");
   {
-    prm.declare_entry("string&list",
-                      "< & > ; /",
-                      Patterns::Anything(),
-                      "docs 1");
-    prm.declare_entry("int*int", "2", Patterns::Integer());
-    prm.declare_entry("double+double",
-                      "6.1415926",
-                      Patterns::Double(),
-                      "docs 3");
+    prm.add_parameter("string&list", str, "docs 1");
+    prm.add_parameter("int*int", intint);
+    prm.add_parameter("double+double", doubledouble, "docs 3");
   }
   prm.leave_subsection();
 
@@ -61,6 +64,14 @@ main()
   std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json.prm");
   prm.parse_input_from_json(in);
 
+  Assert(int1 == 2, ExcNotImplemented());
+  Assert(int2 == 3, ExcNotImplemented());
+  Assert(double1 == 2.234, ExcNotImplemented());
+  Assert(double2 == 5.321, ExcNotImplemented());
+  Assert(str == "< & > ; /", ExcNotImplemented());
+  Assert(intint == 2, ExcNotImplemented());
+  Assert(doubledouble == 7.1415926, ExcNotImplemented());
+
   // write it out again
   prm.print_parameters(deallog.get_file_stream(), ParameterHandler::JSON);
   deallog.get_file_stream() << std::endl;
index fd2d434348c8e3d0782c24a1159e4ae6685f11be..f2790c891d929260b527aaef4abc832f9464da7c 100644 (file)
@@ -6,7 +6,8 @@
         "default_value": "1",
         "documentation": "doc 1",
         "pattern": "0",
-        "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]"
+        "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]",
+        "actions": "0"
     },
     "int2":
     {
@@ -14,7 +15,8 @@
         "default_value": "2",
         "documentation": "doc 2",
         "pattern": "1",
-        "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]"
+        "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]",
+        "actions": "1"
     },
     "ss1":
     {
@@ -24,7 +26,8 @@
             "default_value": "1.234",
             "documentation": "doc 3",
             "pattern": "2",
-            "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]"
+            "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]",
+            "actions": "2"
         },
         "ss2":
         {
@@ -34,7 +37,8 @@
                 "default_value": "4.321",
                 "documentation": "doc 4",
                 "pattern": "3",
-                "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]"
+                "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]",
+                "actions": "3"
             }
         }
     },
@@ -46,7 +50,8 @@
             "default_value": "< & > ; \/",
             "documentation": "docs 1",
             "pattern": "4",
-            "pattern_description": "[Anything]"
+            "pattern_description": "[Anything]",
+            "actions": "4"
         },
         "int_2aint":
         {
             "default_value": "2",
             "documentation": "",
             "pattern": "5",
-            "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]"
+            "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]",
+            "actions": "5"
         },
         "double_2bdouble":
         {
             "value": "7.1415926",
-            "default_value": "6.1415926",
+            "default_value": "6.14159",
             "documentation": "docs 3",
             "pattern": "6",
-            "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]"
+            "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]",
+            "actions": "6"
         }
     }
 }
index 5e12ed85b0e39785423b07230efc98a03991826f..b1dd6505d7d6e8cdf4d521b6f11079f407bd82a1 100644 (file)
@@ -1,3 +1,3 @@
 
-DEAL::ParameterHandler::ExcEntryUndeclared (p->first)
-You can't ask for entry <int1234> you have not yet declared.
+DEAL::ExcEntryUndeclared(entry_string)
+    You can't ask for entry <int1234> you have not yet declared.
index 36eeeb97ea4be7b893d6603c57be55afb6684da6..9fc7e917fd21e807bdc394da214f8d6e92915e3e 100644 (file)
@@ -64,7 +64,7 @@ main()
     {
       prm.parse_input_from_xml(in);
     }
-  catch (const ParameterHandler::ExcInvalidEntryForPatternXML &exc)
+  catch (const ParameterHandler::ExcValueDoesNotMatchPattern &exc)
     {
       deallog << exc.get_exc_name() << std::endl;
       exc.print_info(deallog.get_file_stream());
index 1ec9a3ecba2bacd63f923f7d3bb3a4f8c4a191c8..2cd836d3dd0b5c4fd08b4927368833d46630887a 100644 (file)
@@ -1,8 +1,3 @@
 
-DEAL::ParameterHandler::ExcInvalidEntryForPatternXML (Utilities::trim(new_value), p->first, patterns[pattern_index]->description())
-    The entry value 
-        __2
-    for the entry named
-        int_2aint
-    does not match the given pattern:
-        [Integer range -2147483648...2147483647 (inclusive)]
+DEAL::ExcValueDoesNotMatchPattern(new_value, entries->get<std::string>( path + path_separator + "pattern_description"))
+    The string <__2> does not match the given pattern <[Integer range -2147483648...2147483647 (inclusive)]>.
index 6d75bd986d1f9ce795c8dbadd7b0892967660ee1..ae3c448a96327dd81287222acdc250899a12542f 100644 (file)
@@ -64,7 +64,7 @@ main()
     {
       prm.parse_input_from_xml(in);
     }
-  catch (const ParameterHandler::ExcInvalidEntryForPatternXML &exc)
+  catch (const ParameterHandler::ExcValueDoesNotMatchPattern &exc)
     {
       deallog << exc.get_exc_name() << std::endl;
       exc.print_info(deallog.get_file_stream());
index e8962e3456b1dc802514cde5d1a058852f0b4392..1fa8d342761c2ca5e531b2c876b3e4fc6d0d3fa0 100644 (file)
@@ -1,8 +1,3 @@
 
-DEAL::ParameterHandler::ExcInvalidEntryForPatternXML (Utilities::trim(new_value), p->first, patterns[pattern_index]->description())
-    The entry value 
-        2x
-    for the entry named
-        int1
-    does not match the given pattern:
-        [Integer range -2147483648...2147483647 (inclusive)]
+DEAL::ExcValueDoesNotMatchPattern(new_value, entries->get<std::string>( path + path_separator + "pattern_description"))
+    The string <2x> does not match the given pattern <[Integer range -2147483648...2147483647 (inclusive)]>.

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.