diff --git a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp index add1b800c01ad8186008d55574559e0705527a4b..72f2eb78f04b610b0be55fc7a4d9fdabb90cab6d 100644 --- a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp +++ b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/Generator.cpp @@ -253,8 +253,7 @@ namespace armarx::aron::codegenerator::cpp b->addLine("using _Aron_TNonConst [[maybe_unused]] = typename ReaderT::InputTypeNonConst;"); b->addLine("this->resetSoft();"); - b->addLine("if (" + ARON_READER_ACCESSOR + ".readNull(input))"); - b->addLineAsBlock("throw ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"The input to the read method must not be null.\");"); + b->addLine("ARMARX_CHECK_AND_THROW(!" + ARON_READER_ACCESSOR + ".readNull(input), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"The input to the read method must not be null.\"));"); b->addLine("try"); b->addBlock(this->getReadBlock("", "input")); diff --git a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp index 2fbfad0e89159185d4481abde6fada3e9d79d14c..569d5be67dfd601cf09157655ff0542f022680ae 100644 --- a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp +++ b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/ndarray/Matrix.cpp @@ -101,7 +101,27 @@ namespace armarx::aron::codegenerator::cpp::generator block_if_data->addLine("std::string " + type + ";"); block_if_data->addLine("std::vector<int> " + dims + ";"); block_if_data->addLine("std::vector<unsigned char> " + data + ";"); + block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readNDArray("+variantAccessor+", "+dims+", "+type+", "+data+"); // of " + cppAccessor); + + if((this->type.getRows() == -1 or this->type.getRows() == Eigen::Dynamic) and this->type.getCols() != -1) + { + block_if_data->addLine(cppAccessor + nextEl() + "resize(" + dims + ".at(0));"); + } + + if(this->type.getRows() != -1 and (this->type.getCols() == -1 or this->type.getCols() == Eigen::Dynamic)) + { + block_if_data->addLine(cppAccessor + nextEl() + "resize(" + dims + ".at(1));"); + } + + if((this->type.getRows() == -1 or this->type.getRows() == Eigen::Dynamic) and (this->type.getCols() == -1 or this->type.getCols() == Eigen::Dynamic)) + { + block_if_data->addLine(cppAccessor + nextEl() + "resize(" + dims + ".at(0), " + dims + ".at(1));"); + } + + block_if_data->addLine("ARMARX_CHECK_AND_THROW(" + cppAccessor + nextEl() + "rows() == " + dims + ".at(0) and " + cppAccessor + nextEl() + "cols() == " + dims + ".at(1), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Received wrong dimensions for member '"+cppAccessor+"'.\"));"); + block_if_data->addLine("ARMARX_CHECK_AND_THROW(" + type + " == \"" + std::get<0>(ElementType2Cpp.at(this->type.getElementType())) + "\", ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Received wrong type for member '"+cppAccessor+"'.\"));"); + block_if_data->addLine("std::memcpy(reinterpret_cast<unsigned char*>(" + cppAccessor + nextEl() + "data()), "+data+".data(), "+data+".size());"); return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor); } diff --git a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp index c1c77424ba3d97dc217bd846bb1335023761fa2a..4e5b54e5c6a41bd74147a5445d4d5abdb298b5a4 100644 --- a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp +++ b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/IntEnumClass.cpp @@ -134,7 +134,10 @@ namespace armarx::aron::codegenerator::cpp::generator CppBlockPtr block_if_data = std::make_shared<CppBlock>(); block_if_data->addLine("int " + INT_ENUM_TMP_VALUE + ";"); block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readPrimitive("+variantAccessor+", "+INT_ENUM_TMP_VALUE+"); // of top level enum " + getInstantiatedCppTypename()); - block_if_data->addLine("value = ValueToEnumMap.at("+INT_ENUM_TMP_VALUE+");"); + + block_if_data->addLine("auto valueToEnumMap_iterator = ValueToEnumMap.find("+INT_ENUM_TMP_VALUE+");"); + block_if_data->addLine("ARMARX_CHECK_AND_THROW(valueToEnumMap_iterator != ValueToEnumMap.end(), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Missing enum for value '\" + std::to_string(" + INT_ENUM_TMP_VALUE + ") + \"' in aron enum '" + getFullClassCppTypename() + "'.\"))"); + block_if_data->addLine("value = valueToEnumMap_iterator->second;"); return block_if_data; } diff --git a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp index 0d54dc8bf173b1262ecbc938a6c1678fb1dff8b2..5d5409792c245220df2a0f4918c02ef9412fa6af 100644 --- a/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp +++ b/source/RobotAPI/libraries/aron/codegeneration/codegenerator/codewriter/cpp/generator/toplevel/ObjectClass.cpp @@ -213,8 +213,10 @@ namespace armarx::aron::codegenerator::cpp::generator for (const auto& [key, child] : type.getMemberTypes()) { const auto child_s = FromAronType(*child); - const std::string child_variant_accessor = OBJECT_MEMBERS_ACCESSOR + ".at(\"" + key + "\")"; - block_if_data->appendBlock(child_s->getReadBlock(key, child_variant_accessor)); + std::string child_accessor = OBJECT_MEMBERS_ACCESSOR + "_" + key + "_iterator"; + block_if_data->addLine("auto " + child_accessor + " = " + OBJECT_MEMBERS_ACCESSOR + ".find(\"" + key + "\");"); + block_if_data->addLine("ARMARX_CHECK_AND_THROW(" + child_accessor + " != " + OBJECT_MEMBERS_ACCESSOR + ".end(), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Missing member '" + key + "' in aron object '" + getFullClassCppTypename() + "'.\"));"); + block_if_data->appendBlock(child_s->getReadBlock(key, child_accessor + "->second")); } return block_if_data; } @@ -237,4 +239,3 @@ namespace armarx::aron::codegenerator::cpp::generator return block_if_data; } } - diff --git a/source/RobotAPI/libraries/aron/codegeneration/test/CMakeLists.txt b/source/RobotAPI/libraries/aron/codegeneration/test/CMakeLists.txt index e57da47b6d56c9d33a146a609c6dcba9c6c06f30..d7add84ada2fcafe2c8bf8f9f9a8d2c40e663c7d 100644 --- a/source/RobotAPI/libraries/aron/codegeneration/test/CMakeLists.txt +++ b/source/RobotAPI/libraries/aron/codegeneration/test/CMakeLists.txt @@ -106,6 +106,25 @@ armarx_add_test( ${Simox_INCLUDE_DIR} ) +###################### +# ARON JSON EXPORT TEST +###################### +armarx_add_test( + TEST_NAME + aronJsonExportTest + TEST_FILE + aronJsonExportTest.cpp + LIBS + SimoxUtility # Simox::SimoxUtility + ArmarXCore + RobotAPI::aron + ARON_FILES + aron/OptionalTest.xml + aron/MatrixTest.xml + INCLUDE_DIRECTORIES + ${Simox_INCLUDE_DIR} +) + ######################## # ARON RANDOMIZED TEST # ######################## diff --git a/source/RobotAPI/libraries/aron/codegeneration/test/aronJsonExportTest.cpp b/source/RobotAPI/libraries/aron/codegeneration/test/aronJsonExportTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dea56801557a7890824c8f03d5b2cf5b2711d9a0 --- /dev/null +++ b/source/RobotAPI/libraries/aron/codegeneration/test/aronJsonExportTest.cpp @@ -0,0 +1,86 @@ +/* + * This file is part of ArmarX. + * + * ArmarX is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * ArmarX is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * @package RobotAPI::ArmarXObjects::aron + * @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu ) + * @date 2019 + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#define BOOST_TEST_MODULE RobotAPI::ArmarXLibraries::aron + +#define ARMARX_BOOST_TEST + +// STD/STL +#include <iostream> +#include <cstdlib> +#include <ctime> +#include <numeric> +#include <fstream> + +// Test +#include <RobotAPI/Test.h> + +// ArmarX +#include <ArmarXCore/libraries/cppgen/CppMethod.h> +#include <ArmarXCore/libraries/cppgen/CppClass.h> +#include <RobotAPI/libraries/aron/core/Exception.h> + +// Aron +#include <RobotAPI/libraries/aron/core/data/variant/All.h> +#include <RobotAPI/libraries/aron/core/type/variant/All.h> +#include <RobotAPI/libraries/aron/core/data/rw/writer/nlohmannJSON/NlohmannJSONWriter.h> +#include <RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReader.h> +#include <RobotAPI/libraries/aron/core/type/rw/writer/nlohmannJSON/NlohmannJSONWriter.h> +#include <RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.h> + +// Generated File +#include <RobotAPI/libraries/aron/codegeneration/test/aron/MatrixTest.aron.generated.h> +#include <RobotAPI/libraries/aron/codegeneration/test/aron/OptionalTest.aron.generated.h> + +using namespace armarx; +using namespace aron; + +BOOST_AUTO_TEST_CASE(AronJsonExportTest) +{ + std::cout << "Aron json export test" << std::endl; + OptionalTest clazz; + + { + auto t = clazz.ToAronType(); + + auto type_writer = type::writer::NlohmannJSONWriter(); + auto type_json = clazz.writeType(type_writer); + + std::ofstream type_ofs("/tmp/aronJsonExportTestType.json"); + type_ofs << type_json.dump(2); + + auto data_writer = data::writer::NlohmannJSONWriter(); + auto data_json = clazz.write(data_writer); + + std::ofstream data_ofs("/tmp/aronJsonExportTestData.json"); + data_ofs << data_json.dump(2); + } + + + // Try to read data again + auto data_reader = data::reader::NlohmannJSONReader(); + std::ifstream data_ifs("/tmp/aronJsonExportTestData.json"); + auto data_json = nlohmann::json::parse(data_ifs); + + clazz.read(data_reader, data_json); +} + diff --git a/source/RobotAPI/libraries/aron/core/Exception.h b/source/RobotAPI/libraries/aron/core/Exception.h index 52de8098900e7a8dc5538aa4d53453cffe52a7e5..5395c9eb65d2a61632ab56ad5ee1704a8679e6e1 100644 --- a/source/RobotAPI/libraries/aron/core/Exception.h +++ b/source/RobotAPI/libraries/aron/core/Exception.h @@ -52,6 +52,14 @@ namespace armarx::aron::error LocalException(prettymethod + ": " + reason + ". The path was: " + path.toString()) { } + + /// call operator to append a message to the exception. Used by ARMARX_CHECK_AND_THROW + AronException& operator()(const std::string& additionalMessage = "") + { + auto currentReason = getReason(); + setReason(currentReason + ". Additional Message: " + additionalMessage); + return *this; + } }; /** diff --git a/source/RobotAPI/libraries/aron/core/codegeneration/cpp/AronGeneratedClass.h b/source/RobotAPI/libraries/aron/core/codegeneration/cpp/AronGeneratedClass.h index 552c653a9bb983f1066a7ef87737fa69377c8a8a..8372d89c318cf821a9056c6e5f00f58f372492db 100644 --- a/source/RobotAPI/libraries/aron/core/codegeneration/cpp/AronGeneratedClass.h +++ b/source/RobotAPI/libraries/aron/core/codegeneration/cpp/AronGeneratedClass.h @@ -30,6 +30,8 @@ #include <map> // ArmarX +#include <ArmarXCore/core/exceptions/local/ExpressionException.h> + #include <RobotAPI/libraries/aron/core/Exception.h> #include <RobotAPI/libraries/aron/core/data/variant/All.h> diff --git a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp index e773c517d9ceee0c8c166f327b639a2c8f799f6b..985f914d16d8c501992aeab2b44a0c7a00d9b8e6 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp +++ b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.cpp @@ -19,13 +19,21 @@ */ // STD/STL +#include <cmath> +#include <cstdint> +#include <cstring> #include <memory> #include <numeric> +#include <SimoxUtility/algorithm/get_map_keys_values.h> + +#include "ArmarXCore/core/logging/Logging.h" + // Header #include "NlohmannJSONReaderWithoutTypeCheck.h" // ArmarX +#include <RobotAPI/interface/aron/Aron.h> #include <RobotAPI/libraries/aron/core/Exception.h> #include <RobotAPI/libraries/aron/core/data/visitor/nlohmannJSON/NlohmannJSONVisitor.h> @@ -56,6 +64,26 @@ namespace armarx::aron::data::reader elements = input.get<std::map<std::string, nlohmann::json>>(); } + + const std::map<std::string, type::matrix::ElementType> ElementTypeAsString = { + {"short", ::armarx::aron::type::matrix::INT16}, + {"int", ::armarx::aron::type::matrix::INT32}, + {"long", ::armarx::aron::type::matrix::INT64}, + {"float", ::armarx::aron::type::matrix::FLOAT32}, + {"double", ::armarx::aron::type::matrix::FLOAT64}}; + + template <typename T> + void + readTo(const nlohmann::json& input, std::vector<unsigned char>& data) + { + const std::vector<T> d = input.at("data").get<std::vector<T>>(); + + const std::size_t bufferLen = d.size() * sizeof(T); + + data.resize(bufferLen); + memcpy(data.data(), d.data(), bufferLen); + } + void NlohmannJSONReaderWithoutTypeCheck::readNDArray(const nlohmann::json& input, std::vector<int>& shape, @@ -64,7 +92,37 @@ namespace armarx::aron::data::reader Path& p) { shape = input.at("dims").get<std::vector<int>>(); - data = input.at("data").get<std::vector<unsigned char>>(); + + typeAsString = input.at("type").get<std::string>(); + + ARMARX_CHECK(ElementTypeAsString.count(typeAsString) > 0) + << "Invalid element `" << typeAsString << "`. Valid elements are " + << simox::alg::get_keys(ElementTypeAsString) << "."; + + const type::matrix::ElementType elementType = ElementTypeAsString.at(typeAsString); + + // as we need to return a vector<uchar>, we first need to read the json array as the specific type + // and then convert it to the vector<uchar> + switch (elementType) + { + case type::matrix::INT16: + readTo<std::int16_t>(input, data); + break; + case type::matrix::INT32: + readTo<std::int32_t>(input, data); + break; + case type::matrix::INT64: + readTo<std::int64_t>(input, data); + break; + case type::matrix::FLOAT32: + readTo<std::float_t>(input, data); + break; + case type::matrix::FLOAT64: + readTo<std::double_t>(input, data); + break; + } + + ARMARX_INFO << VAROUT(typeAsString); } void @@ -104,4 +162,5 @@ namespace armarx::aron::data::reader { i = input; } + } // namespace armarx::aron::data::reader diff --git a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.h b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.h index e0bed7b71869e2abc074484fe3fb9774e0ef6730..2d17af7f5ead72cc51f04623a816e1fb8236c8ad 100644 --- a/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.h +++ b/source/RobotAPI/libraries/aron/core/data/rw/reader/nlohmannJSON/NlohmannJSONReaderWithoutTypeCheck.h @@ -69,5 +69,6 @@ namespace armarx::aron::data::reader void readDouble(InputType& input, double& i, Path& p) override; void readString(InputType& input, std::string& i, Path& p) override; void readBool(InputType& input, bool& i, Path& p) override; + }; } // namespace armarx::aron::data::reader