2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019,2020, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "keyvaluetree.h"
42 #include "gromacs/utility/compare.h"
43 #include "gromacs/utility/gmxassert.h"
44 #include "gromacs/utility/strconvert.h"
45 #include "gromacs/utility/stringutil.h"
46 #include "gromacs/utility/textwriter.h"
54 //! Helper function to split a KeyValueTreePath to its components
55 std::vector<std::string> splitPathElements(const std::string& path)
57 GMX_ASSERT(!path.empty() && path[0] == '/', "Paths to KeyValueTree should start with '/'");
58 return splitDelimitedString(path.substr(1), '/');
63 /********************************************************************
67 KeyValueTreePath::KeyValueTreePath(const char* path) : path_(splitPathElements(path)) {}
69 KeyValueTreePath::KeyValueTreePath(const std::string& path) : path_(splitPathElements(path)) {}
71 std::string KeyValueTreePath::toString() const
73 return "/" + joinStrings(path_, "/");
76 /********************************************************************
80 bool KeyValueTreeObject::hasDistinctProperties(const KeyValueTreeObject& obj) const
82 for (const auto& prop : obj.values_)
84 if (keyExists(prop.key()))
86 GMX_RELEASE_ASSERT(!prop.value().isArray(), "Comparison of arrays not implemented");
87 if (prop.value().isObject() && valueMap_.at(prop.key()).isObject())
89 return valueMap_.at(prop.key()).asObject().hasDistinctProperties(prop.value().asObject());
97 /********************************************************************
102 void dumpKeyValueTree(TextWriter* writer, const KeyValueTreeObject& tree)
104 for (const auto& prop : tree.properties())
106 const auto& value = prop.value();
107 if (value.isObject())
109 writer->writeString(prop.key());
110 writer->writeLine(":");
111 int oldIndent = writer->wrapperSettings().indent();
112 writer->wrapperSettings().setIndent(oldIndent + 2);
113 dumpKeyValueTree(writer, value.asObject());
114 writer->wrapperSettings().setIndent(oldIndent);
116 else if (value.isArray()
117 && std::all_of(value.asArray().values().begin(), value.asArray().values().end(),
118 [](const auto& elem) { return elem.isObject(); }))
120 // Array containing only objects
121 writer->writeString(prop.key());
122 writer->writeLine(":");
123 int oldIndent = writer->wrapperSettings().indent();
124 writer->wrapperSettings().setIndent(oldIndent + 2);
125 for (const auto& elem : value.asArray().values())
127 dumpKeyValueTree(writer, elem.asObject());
129 writer->wrapperSettings().setIndent(oldIndent);
133 int indent = writer->wrapperSettings().indent();
134 writer->writeString(formatString("%*s", -(33 - indent), prop.key().c_str()));
135 writer->writeString(" = ");
138 writer->writeString("[");
139 for (const auto& elem : value.asArray().values())
142 !elem.isObject() && !elem.isArray(),
143 "Only arrays of simple types and array of objects are implemented. "
144 "Arrays of arrays and mixed arrays are not supported.");
145 writer->writeString(" ");
146 writer->writeString(simpleValueToString(elem));
148 writer->writeString(" ]");
152 writer->writeString(simpleValueToString(value));
160 /********************************************************************
161 * Key value tree comparison
170 CompareHelper(TextWriter* writer, real ftol, real abstol) :
177 void compareObjects(const KeyValueTreeObject& obj1, const KeyValueTreeObject& obj2)
179 for (const auto& prop1 : obj1.properties())
181 currentPath_.append(prop1.key());
182 if (obj2.keyExists(prop1.key()))
184 compareValues(prop1.value(), obj2[prop1.key()]);
188 handleMissingKeyInSecondObject(prop1.value());
190 currentPath_.pop_back();
192 for (const auto& prop2 : obj2.properties())
194 currentPath_.append(prop2.key());
195 if (!obj1.keyExists(prop2.key()))
197 handleMissingKeyInFirstObject(prop2.value());
199 currentPath_.pop_back();
204 void compareValues(const KeyValueTreeValue& value1, const KeyValueTreeValue& value2)
206 if (value1.type() == value2.type())
208 if (value1.isObject())
210 compareObjects(value1.asObject(), value2.asObject());
212 else if (value1.isArray())
214 GMX_RELEASE_ASSERT(false, "Array comparison not implemented");
216 else if (!areSimpleValuesOfSameTypeEqual(value1, value2))
218 writer_->writeString(currentPath_.toString());
219 writer_->writeLine(formatString(" (%s - %s)", simpleValueToString(value1).c_str(),
220 simpleValueToString(value2).c_str()));
223 else if ((value1.isType<double>() && value2.isType<float>())
224 || (value1.isType<float>() && value2.isType<double>()))
226 const bool firstIsDouble = value1.isType<double>();
227 const float v1 = firstIsDouble ? value1.cast<double>() : value1.cast<float>();
228 const float v2 = firstIsDouble ? value2.cast<float>() : value2.cast<double>();
229 if (!equal_float(v1, v2, ftol_, abstol_))
231 writer_->writeString(currentPath_.toString());
232 writer_->writeLine(formatString(" (%e - %e)", v1, v2));
237 handleMismatchingTypes(value1, value2);
241 bool areSimpleValuesOfSameTypeEqual(const KeyValueTreeValue& value1, const KeyValueTreeValue& value2)
243 GMX_ASSERT(value1.type() == value2.type(), "Caller should ensure that types are equal");
244 if (value1.isType<bool>())
246 return value1.cast<bool>() == value2.cast<bool>();
248 else if (value1.isType<int>())
250 return value1.cast<int>() == value2.cast<int>();
252 else if (value1.isType<int64_t>())
254 return value1.cast<int64_t>() == value2.cast<int64_t>();
256 else if (value1.isType<double>())
258 return equal_double(value1.cast<double>(), value2.cast<double>(), ftol_, abstol_);
260 else if (value1.isType<float>())
262 return equal_float(value1.cast<float>(), value2.cast<float>(), ftol_, abstol_);
264 else if (value1.isType<std::string>())
266 return value1.cast<std::string>() == value2.cast<std::string>();
270 GMX_RELEASE_ASSERT(false, "Unknown value type");
275 void handleMismatchingTypes(const KeyValueTreeValue& /* value1 */,
276 const KeyValueTreeValue& /* value2 */)
278 writer_->writeString(currentPath_.toString());
279 writer_->writeString(" type mismatch");
282 void handleMissingKeyInFirstObject(const KeyValueTreeValue& value)
284 const std::string message = formatString("%s (missing - %s)", currentPath_.toString().c_str(),
285 formatValueForMissingMessage(value).c_str());
286 writer_->writeLine(message);
288 void handleMissingKeyInSecondObject(const KeyValueTreeValue& value)
290 const std::string message = formatString("%s (%s - missing)", currentPath_.toString().c_str(),
291 formatValueForMissingMessage(value).c_str());
292 writer_->writeLine(message);
295 static std::string formatValueForMissingMessage(const KeyValueTreeValue& value)
297 if (value.isObject() || value.isArray())
301 return simpleValueToString(value);
304 KeyValueTreePath currentPath_;
313 void compareKeyValueTrees(TextWriter* writer,
314 const KeyValueTreeObject& tree1,
315 const KeyValueTreeObject& tree2,
319 CompareHelper helper(writer, ftol, abstol);
320 helper.compareObjects(tree1, tree2);