]> https://gitweb.dealii.org/ - dealii.git/commitdiff
ParameterHandler::parse_input_from_json(): mangle entries internally 16544/head
authorPeter Munch <peterrmuench@gmail.com>
Thu, 11 Jan 2024 08:29:31 +0000 (09:29 +0100)
committerMagdalena Schreter <magdalena.schreter@tum.de>
Wed, 13 Mar 2024 18:54:34 +0000 (19:54 +0100)
Co-authored-by: Magdalena Schreter-Fleischhacker <schreter.magdalena@gmail.com>
doc/news/changes/incompatibilities/20240126SchreterMunch [new file with mode: 0644]
source/base/parameter_handler.cc
tests/parameter_handler/parameter_handler_read_json_01.cc [moved from tests/parameter_handler/parameter_handler_read_json.cc with 77% similarity]
tests/parameter_handler/parameter_handler_read_json_01.output [moved from tests/parameter_handler/parameter_handler_read_json.output with 87% similarity]
tests/parameter_handler/parameter_handler_read_json_02.cc
tests/parameter_handler/parameter_handler_read_json_03.cc
tests/parameter_handler/prm/parameter_handler_read_json_01.json [moved from tests/parameter_handler/prm/parameter_handler_read_json.prm with 81% similarity]
tests/parameter_handler/prm/parameter_handler_read_json_02.json [moved from tests/parameter_handler/prm/parameter_handler_read_json_02.prm with 100% similarity]
tests/parameter_handler/prm/parameter_handler_read_json_03.json

diff --git a/doc/news/changes/incompatibilities/20240126SchreterMunch b/doc/news/changes/incompatibilities/20240126SchreterMunch
new file mode 100644 (file)
index 0000000..1464386
--- /dev/null
@@ -0,0 +1,4 @@
+Changed:`ParameterHandler::parse_input_from_json()` reads now a json-file 
+without mangled parameter names.
+<br>
+(Magdalena Schreter, Peter Munch, 2024/01/26)
index d713ee9310358a379dbede0f9e6892a816d3da93..fefa8fc96abd455956bf45cb93bef4c4ca9aa5f3 100644 (file)
@@ -331,15 +331,17 @@ namespace
   }
 
   /**
-   * Demangle all parameters recursively and attach them to @p tree_out.
+   * Mangle (@p do_mangle = true) or demangle (@p do_mangle = false) all
+   * parameters recursively and attach them to @p tree_out.
    * @p is_parameter_or_alias_node indicates, whether a given node
    * @p tree_in is a parameter node or an alias node (as opposed to being
    * a subsection).
    */
   void
-  recursively_demangle(const boost::property_tree::ptree &tree_in,
-                       boost::property_tree::ptree       &tree_out,
-                       const bool is_parameter_or_alias_node = false)
+  recursively_mangle_or_demangle(const boost::property_tree::ptree &tree_in,
+                                 boost::property_tree::ptree       &tree_out,
+                                 const bool                         do_mangle,
+                                 const bool is_parameter_or_alias_node = false)
   {
     for (const auto &p : tree_in)
       {
@@ -354,11 +356,13 @@ namespace
             if (const auto val = p.second.get_value_optional<std::string>())
               temp.put_value<std::string>(*val);
 
-            recursively_demangle(p.second,
-                                 temp,
-                                 is_parameter_node(p.second) ||
-                                   is_alias_node(p.second));
-            tree_out.put_child(demangle(p.first), temp);
+            recursively_mangle_or_demangle(p.second,
+                                           temp,
+                                           do_mangle,
+                                           is_parameter_node(p.second) ||
+                                             is_alias_node(p.second));
+            tree_out.put_child(do_mangle ? mangle(p.first) : demangle(p.first),
+                               temp);
           }
       }
   }
@@ -815,9 +819,14 @@ 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.
+  // This function can only read mangled files. Therefore, we create a mangeled
+  // tree first.
+  boost::property_tree::ptree node_tree_mangled;
+  recursively_mangle_or_demangle(node_tree,
+                                 node_tree_mangled,
+                                 true /*do_mangle*/);
   read_xml_recursively(
-    node_tree, "", path_separator, patterns, skip_undefined, *this);
+    node_tree_mangled, "", path_separator, patterns, skip_undefined, *this);
 }
 
 
@@ -1359,9 +1368,11 @@ ParameterHandler::print_parameters(std::ostream     &out,
 
   if ((style & JSON) != 0)
     {
-      boost::property_tree::ptree current_entries_damangled;
-      recursively_demangle(current_entries, current_entries_damangled);
-      write_json(out, current_entries_damangled);
+      boost::property_tree::ptree current_entries_demangled;
+      recursively_mangle_or_demangle(current_entries,
+                                     current_entries_demangled,
+                                     false /*do_mangle*/);
+      write_json(out, current_entries_demangled);
       return out;
     }
 
similarity index 77%
rename from tests/parameter_handler/parameter_handler_read_json.cc
rename to tests/parameter_handler/parameter_handler_read_json_01.cc
index f3816b09123c6ee41ee1ae91e3dfdd17c1a3f21d..a1d8be6702fa6e177b447b8cd0e24db1be9cfabe 100644 (file)
@@ -14,7 +14,7 @@
 
 
 
-// check ParameterHandler::parse_input_from_xml
+// check ParameterHandler::parse_input_from_json
 
 #include <deal.II/base/parameter_handler.h>
 
@@ -27,13 +27,14 @@ 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;
+  int         int1          = 1;
+  int         int2          = 2;
+  double      double1       = 1.234;
+  double      double2       = 4.321;
+  std::string str           = "< & > ; /";
+  int         intint        = 2;
+  double      doubledouble  = 6.1415926;
+  double      double_double = 0;
 
   ParameterHandler prm;
   prm.add_parameter("int1", int1, "doc 1");
@@ -56,11 +57,12 @@ main()
     prm.add_parameter("string&list", str, "docs 1");
     prm.add_parameter("int*int", intint);
     prm.add_parameter("double+double", doubledouble, "docs 3");
+    prm.add_parameter("double_double", double_double, "docs 4");
   }
   prm.leave_subsection();
 
   // read from json
-  std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json.prm");
+  std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_01.json");
   prm.parse_input_from_json(in);
 
   Assert(int1 == 2, ExcNotImplemented());
@@ -70,6 +72,7 @@ main()
   Assert(str == "< & > ; /", ExcNotImplemented());
   Assert(intint == 2, ExcNotImplemented());
   Assert(doubledouble == 7.1415926, ExcNotImplemented());
+  Assert(double_double == 1.234, ExcNotImplemented());
 
   // write it out again
   prm.print_parameters(deallog.get_file_stream(), ParameterHandler::JSON);
similarity index 87%
rename from tests/parameter_handler/parameter_handler_read_json.output
rename to tests/parameter_handler/parameter_handler_read_json_01.output
index 70095fb249890f0788e3703498947fece51ec3f6..ac315ef54ced85e8ebe7986edff3a4113df28072 100644 (file)
             "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]",
             "actions": "6"
         },
+        "double_double":
+        {
+            "value": "1.234",
+            "default_value": "0",
+            "documentation": "docs 4",
+            "pattern": "7",
+            "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]",
+            "actions": "7"
+        },
         "int*int":
         {
             "value": "2",
index 92c87ee4572568449c2cbb371f6ff98a5dd42f50..d29bbb16402c29dc46d1ea07b03c8cada20e5283 100644 (file)
@@ -42,7 +42,7 @@ main()
   prm.leave_subsection();
 
   // read from json
-  std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.prm");
+  std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.json");
   prm.parse_input_from_json(in, true);
 
   AssertDimension(int1, 1);
index d49c41123d7ace62cff02d0d7ba196e774dcafb4..a3f0ceb79d97673c220969a354c43e781c78f43c 100644 (file)
@@ -31,6 +31,7 @@ main()
   int int2 = 0;
   int int3 = 0;
   int int4 = 0;
+  int int5 = 0;
 
   ParameterHandler prm;
   prm.add_parameter("int1", int1);
@@ -41,6 +42,10 @@ main()
   prm.add_parameter("int4", int4);
   prm.leave_subsection();
 
+  prm.enter_subsection("test_underscore");
+  prm.add_parameter("int_5", int5);
+  prm.leave_subsection();
+
   std::string source   = SOURCE_DIR;
   std::string filename = source + "/prm/parameter_handler_read_json_03.json";
   prm.parse_input(filename, "", true, true);
@@ -49,6 +54,7 @@ main()
   AssertDimension(int2, 2);
   AssertDimension(int3, 3);
   AssertDimension(int4, 4);
+  AssertDimension(int5, 10);
 
   return 0;
 }
similarity index 81%
rename from tests/parameter_handler/prm/parameter_handler_read_json.prm
rename to tests/parameter_handler/prm/parameter_handler_read_json_01.json
index 989cdc8a0731ae53013f9cda5cc965b6a18278a1..af93caeda59d0a5ccc1434749c55b7aafb4f7d9f 100644 (file)
@@ -18,7 +18,7 @@
     },
     "ss1":
     {
-        "double_201":
+        "double 1":
         {
             "value": "2.234",
             "default_value": "1.234",
@@ -28,7 +28,7 @@
         },
         "ss2":
         {
-            "double_202":
+            "double 2":
             {
                 "value": "5.321",
                 "default_value": "4.321",
@@ -38,9 +38,9 @@
             }
         }
     },
-    "Testing_25testing":
+    "Testing%testing":
     {
-        "string_26list":
+        "string&list":
         {
             "value": "< & > ; \/",
             "default_value": "< & > ; \/",
@@ -48,7 +48,7 @@
             "pattern": "4",
             "pattern_description": "[Anything]"
         },
-        "int_2aint":
+        "int*int":
         {
             "value": "2",
             "default_value": "2",
             "pattern": "5",
             "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]"
         },
-        "double_2bdouble":
+        "double+double":
         {
             "value": "7.1415926",
             "default_value": "6.1415926",
             "documentation": "docs 3",
             "pattern": "6",
             "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]"
+        },
+        "double_double":
+        {
+            "value": "1.234",
+            "default_value": "0",
+            "documentation": "docs 4",
+            "pattern": "7",
+            "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]"
         }
     }
 }
index 0ed12e1a28d460f01976a96fe25e396ad957fd34..f7c74e30311a2976b51186fc4e1567908e445bd5 100644 (file)
@@ -4,5 +4,6 @@
     "dummy" : {"int3" : 3, "int4" : 4},
     "int5": {"value": 5},
     "int6": 6,
-    "dummy2" : {"int7" : 7, "int8" : 8}
-}
\ No newline at end of file
+    "dummy2" : {"int7" : 7, "int8" : 8},
+    "test_underscore": {"int_5": 10}
+}

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.