diff --git a/source/RobotAPI/libraries/armem/CMakeLists.txt b/source/RobotAPI/libraries/armem/CMakeLists.txt index ef0d4e6cbfaddcb6f7a11de1c92d2366d326b273..33e61ee439fe60e32e18cd3b8c2141f89a1f1a7b 100644 --- a/source/RobotAPI/libraries/armem/CMakeLists.txt +++ b/source/RobotAPI/libraries/armem/CMakeLists.txt @@ -62,10 +62,13 @@ set(LIB_FILES query/CoreSegmentQueryProcessor.cpp query/MemoryQueryProcessor.cpp - ltm/LongTermMemoryLUT.cpp + ltm/MemoryFileSystemStorage.cpp + ltm/io/DiskWriter/DiskWriter.cpp ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.cpp + ltm/io/DiskReader/DiskReader.cpp ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.cpp ) + set(LIB_HEADERS core/Commit.h core/DataMode.h @@ -121,7 +124,9 @@ set(LIB_HEADERS query/CoreSegmentQueryProcessor.h query/MemoryQueryProcessor.h - ltm/LongTermMemoryLUT.h + ltm/FileSystemLookupMemory.h + ltm/MemoryFileSystemStorage.h + ltm/io/DiskReaderWriter.h ltm/io/DiskWriter/DiskWriter.h ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.h ltm/io/DiskReader/DiskReader.h diff --git a/source/RobotAPI/libraries/armem/core/Time.cpp b/source/RobotAPI/libraries/armem/core/Time.cpp index ce0bc6be6d4f4e102e2fe303abb16d660d8322ae..9995b685123bf8eb8f7e53378f79548b5383346a 100644 --- a/source/RobotAPI/libraries/armem/core/Time.cpp +++ b/source/RobotAPI/libraries/armem/core/Time.cpp @@ -45,7 +45,6 @@ namespace armarx return ss.str(); } - } diff --git a/source/RobotAPI/libraries/armem/core/ice_conversions.cpp b/source/RobotAPI/libraries/armem/core/ice_conversions.cpp index f942f44f0036c6b826cf91aa765245c450d0cb2b..d354077fa4503d8ed6fde686fabdeae54ea36c1b 100644 --- a/source/RobotAPI/libraries/armem/core/ice_conversions.cpp +++ b/source/RobotAPI/libraries/armem/core/ice_conversions.cpp @@ -86,7 +86,7 @@ namespace armarx update.instancesData.clear(); update.instancesData.reserve(ice.instancesData.size()); std::transform(ice.instancesData.begin(), ice.instancesData.end(), std::back_inserter(update.instancesData), - aron::datanavigator::AronDictDataNavigator::FromAronDict); + aron::datanavigator::AronDictDataNavigator::FromAronDictPtr); update.timeCreated = Time::microSeconds(ice.timeCreatedMicroSeconds); @@ -101,7 +101,7 @@ namespace armarx ice.instancesData.clear(); ice.instancesData.reserve(update.instancesData.size()); std::transform(update.instancesData.begin(), update.instancesData.end(), std::back_inserter(ice.instancesData), - aron::datanavigator::AronDictDataNavigator::ToAronDict); + aron::datanavigator::AronDictDataNavigator::ToAronDictPtr); ice.timeCreatedMicroSeconds = update.timeCreated.toMicroSeconds(); diff --git a/source/RobotAPI/libraries/armem/error/ArMemError.cpp b/source/RobotAPI/libraries/armem/error/ArMemError.cpp index 75fbee59eada8768b55590f359a3c7ecb229befe..2b61bfbac513a1854859abecf81697e5baff3aae 100644 --- a/source/RobotAPI/libraries/armem/error/ArMemError.cpp +++ b/source/RobotAPI/libraries/armem/error/ArMemError.cpp @@ -159,4 +159,64 @@ namespace armarx::armem::error } + PathNotARegularFile::PathNotARegularFile(const std::string& path, const std::string& message) : + ArMemError(makeMsg(path, message)) + { + } + + std::string PathNotARegularFile::makeMsg(const std::string& path, const std::string& message) + { + std::stringstream ss; + ss << "The path '" << path << "' is not a regular file. Expecting a regular file there."; + if (message.size() > 0) + { + ss << " " << message; + } + else + { + ss << "."; + } + return ss.str(); + } + + PathNotADirectory::PathNotADirectory(const std::string& path, const std::string& message) : + ArMemError(makeMsg(path, message)) + { + } + + std::string PathNotADirectory::makeMsg(const std::string& path, const std::string& message) + { + std::stringstream ss; + ss << "The path '" << path << "' is not a directory. Expecting a directory there."; + if (message.size() > 0) + { + ss << " " << message; + } + else + { + ss << "."; + } + return ss.str(); + } + + PathDoesNotExist::PathDoesNotExist(const std::string& path, const std::string& message) : + ArMemError(makeMsg(path, message)) + { + } + + std::string PathDoesNotExist::makeMsg(const std::string& path, const std::string& message) + { + std::stringstream ss; + ss << "The path '" << path << "' does not exist. Expecting a valid path."; + if (message.size() > 0) + { + ss << " " << message; + } + else + { + ss << "."; + } + return ss.str(); + } + } diff --git a/source/RobotAPI/libraries/armem/error/ArMemError.h b/source/RobotAPI/libraries/armem/error/ArMemError.h index cf670172466030a58947a59909316b1b30ca6c6b..c3c6a1b57be4fa49934294d6bffbba52ee3a1e82 100644 --- a/source/RobotAPI/libraries/armem/error/ArMemError.h +++ b/source/RobotAPI/libraries/armem/error/ArMemError.h @@ -158,6 +158,43 @@ namespace armarx::armem::error }; + /** + * @brief Indicates that an file is not a directory. + */ + class PathNotADirectory : public ArMemError + { + public: + + PathNotADirectory(const std::string& path, const std::string& message = ""); + + static std::string makeMsg(const std::string& path, const std::string& message = ""); + + }; + + /** + * @brief Indicates that an file is not a directory. + */ + class PathNotARegularFile : public ArMemError + { + public: + PathNotARegularFile(const std::string& path, const std::string& message = ""); + + static std::string makeMsg(const std::string& path, const std::string& message = ""); + + }; + + /** + * @brief Indicates that an file is not a directory. + */ + class PathDoesNotExist : public ArMemError + { + public: + + PathDoesNotExist(const std::string& path, const std::string& message = ""); + + static std::string makeMsg(const std::string& path, const std::string& message = ""); + + }; } diff --git a/source/RobotAPI/libraries/armem/ltm/FileSystemLookupMemory.h b/source/RobotAPI/libraries/armem/ltm/FileSystemLookupMemory.h new file mode 100644 index 0000000000000000000000000000000000000000..1267af6618751911cdb550357f0c293f681d78ad --- /dev/null +++ b/source/RobotAPI/libraries/armem/ltm/FileSystemLookupMemory.h @@ -0,0 +1,112 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> +#include <filesystem> + +// ArmarX +#include <RobotAPI/libraries/armem/memory/Memory.h> + + +namespace armarx::armem +{ + namespace ltm + { + typedef std::vector<std::filesystem::path> FileSystemLookupEntityHistory; // absolute paths to stored files + typedef std::map<Time, FileSystemLookupEntityHistory> FileSystemLookupEntity; + typedef std::map<std::string, FileSystemLookupEntity> FileSystemLookupProviderSegment; + typedef std::map<std::string, FileSystemLookupProviderSegment> FileSystemLookupCoreSegment; + typedef std::map<std::string, FileSystemLookupCoreSegment> FileSystemLookupMemory; + + class FileSystemLookupMemoryManager + { + public: + FileSystemLookupMemoryManager() = default; + + void merge(const FileSystemLookupMemoryManager& m) + { + for (const auto& [memoryName, memory] : m.memoryLookupTable) + { + if (memoryLookupTable.find(memoryName) == memoryLookupTable.end()) + { + memoryLookupTable[memoryName] = memory; + continue; + } + + FileSystemLookupMemory& lutMemory = memoryLookupTable[memoryName]; + for (const auto& [coreKey, coreSegment] : memory) + { + if (lutMemory.find(coreKey) == lutMemory.end()) + { + lutMemory[coreKey] = coreSegment; + continue; + } + + FileSystemLookupCoreSegment& lutCoreSegment = lutMemory[coreKey]; + for (const auto& [providerKey, providerSegment] : coreSegment) + { + if (lutCoreSegment.find(providerKey) == lutCoreSegment.end()) + { + lutCoreSegment[providerKey] = providerSegment; + continue; + } + + FileSystemLookupProviderSegment& lutProviderSegment = lutCoreSegment[providerKey]; + for (const auto& [entityKey, entity] : providerSegment) + { + if (lutProviderSegment.find(entityKey) == lutProviderSegment.end()) + { + lutProviderSegment[entityKey] = entity; + continue; + } + + FileSystemLookupEntity& lutEntity = lutProviderSegment[entityKey]; + for (const auto& [entityHistoryTimestamp, entitySnapshot] : entity) + { + if (lutEntity.find(entityHistoryTimestamp) == lutEntity.end()) + { + lutEntity[entityHistoryTimestamp] = entitySnapshot; + continue; + } + + FileSystemLookupEntityHistory& lutEntitySnapshot = lutEntity[entityHistoryTimestamp]; + //for (unsigned int i = 0; i < entitySnapshot.size(); ++i) + //{ + // // clear internal stored memory of that snapshot + // internalMemoryEntitySnapshot.clear(); + // internalMemoryEntitySnapshot.push_back(entitySnapshot[i]); + //} + // for now, simply overwrite all values since one history timestamp should not have two different snapshots + lutEntitySnapshot = entitySnapshot; + } + } + } + } + } + } + + std::map<std::string, FileSystemLookupMemory> memoryLookupTable; + }; + } +} diff --git a/source/RobotAPI/libraries/armem/ltm/LongTermMemoryLUT.cpp b/source/RobotAPI/libraries/armem/ltm/LongTermMemoryLUT.cpp deleted file mode 100644 index e3ade250a80eb9cf5858d6be946b11856e4ca8aa..0000000000000000000000000000000000000000 --- a/source/RobotAPI/libraries/armem/ltm/LongTermMemoryLUT.cpp +++ /dev/null @@ -1,114 +0,0 @@ -#include "LongTermMemoryLUT.h" - -namespace armarx::armem::ltm -{ - LongTermMemoryLUT::LongTermMemoryLUT(const std::string& r) : - writer(new io::NlohmannJSONDiskWriter(r)) - { - - } - LongTermMemoryLUT::LongTermMemoryLUT(const io::DiskWriterPtr& w) : - writer(w) - { - - } - - void LongTermMemoryLUT::writeOnDisk(const MemoryPtr& m) - { - io::DiskWriterReturnInformation info = writer->writeOnDisk(m); - updateLUT(info.storedElements); - } - void LongTermMemoryLUT::writeOnDisk(const std::map<std::string, CoreSegmentPtr>& m) - { - io::DiskWriterReturnInformation info = writer->writeOnDisk(m); - updateLUT(info.storedElements); - } - - void LongTermMemoryLUT::loadFromDisk() - { - - } - - std::string LongTermMemoryLUT::getLUTasString() - { - std::stringstream ss; - for (const auto& [coreKey, coreSegment] : lut) - { - ss << coreKey << ": " << std::endl; - for (const auto& [providerKey, providerSegment] : coreSegment) - { - ss << "\t" << providerKey << ": " << std::endl; - for (const auto& [entityKey, entity] : providerSegment) - { - ss << "\t\t" << entityKey << ": " << std::endl; - for (const auto& [entityHistoryTimestamp, entitySnapshot] : entity) - { - ss << "\t\t\t" << entityHistoryTimestamp.toMilliSeconds() << ":" << std::endl; - ss << "\t\t\t\t" << "["; - for (unsigned int i = 0; i < entitySnapshot.size(); ++i) - { - const std::string& newlyGenerate = entitySnapshot[i]; - if (i > 0) - { - ss << ", "; - } - ss << "(" << i << " => " << newlyGenerate << ")"; - } - ss << "]" << std::endl; - } - } - } - } - return ss.str(); - } - - void LongTermMemoryLUT::updateLUT(const EasyStringMemory& info) - { - for (const auto& [coreKey, coreSegment] : info) - { - if (lut.find(coreKey) == lut.end()) - { - lut[coreKey] = coreSegment; - continue; - } - - EasyStringCoreSegment& lutCoreSegment = lut[coreKey]; - for (const auto& [providerKey, providerSegment] : coreSegment) - { - if (lutCoreSegment.find(providerKey) == lutCoreSegment.end()) - { - lutCoreSegment[providerKey] = providerSegment; - continue; - } - - EasyStringProviderSegment& lutProviderSegment = lutCoreSegment[providerKey]; - for (const auto& [entityKey, entity] : providerSegment) - { - if (lutProviderSegment.find(entityKey) == lutProviderSegment.end()) - { - lutProviderSegment[entityKey] = entity; - continue; - } - - EasyStringEntity& lutEntity = lutProviderSegment[entityKey]; - for (const auto& [entityHistoryTimestamp, entitySnapshot] : entity) - { - if (lutEntity.find(entityHistoryTimestamp) == lutEntity.end()) - { - lutEntity[entityHistoryTimestamp] = entitySnapshot; - continue; - } - - EasyStringEntityHistory& lutEntitySnapshot = lutEntity[entityHistoryTimestamp]; - //for (unsigned int i = 0; i < entitySnapshot.size(); ++i) - //{ - //const std::string& newlyGenerate = entitySnapshot[i]; - //} - // for now, simply overwrite all values since a history timestamp should not have to different snapshots - lutEntitySnapshot = entitySnapshot; - } - } - } - } - } -} diff --git a/source/RobotAPI/libraries/armem/ltm/LongTermMemoryLUT.h b/source/RobotAPI/libraries/armem/ltm/LongTermMemoryLUT.h deleted file mode 100644 index 9f6f937e35a3e245266b31628f69198ab3bf5aa3..0000000000000000000000000000000000000000 --- a/source/RobotAPI/libraries/armem/ltm/LongTermMemoryLUT.h +++ /dev/null @@ -1,63 +0,0 @@ -/* -* 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/>. -* -* @author Fabian Peller (fabian dot peller at kit dot edu) -* @copyright http://www.gnu.org/licenses/gpl-2.0.txt -* GNU General Public License -*/ - -#pragma once - -// STD/STL -#include <memory> -#include <string> - -// ArmarX -#include <RobotAPI/interface/aron.h> -#include <RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.h> -#include <RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.h> - - -namespace armarx::armem -{ - namespace ltm - { - class LongTermMemoryLUT; - typedef std::shared_ptr<LongTermMemoryLUT> LongTermMemoryLUTPtr; - - class LongTermMemoryLUT - { - public: - LongTermMemoryLUT() = delete; - LongTermMemoryLUT(const std::string&); - LongTermMemoryLUT(const io::DiskWriterPtr&); - - void writeOnDisk(const MemoryPtr&); - void writeOnDisk(const std::map<std::string, CoreSegmentPtr>&); - - void loadFromDisk(); - - std::string getLUTasString(); - - private: - void updateLUT(const EasyStringMemory& info); - - private: - EasyStringMemory lut; - - io::DiskWriterPtr writer; - }; - } -} diff --git a/source/RobotAPI/libraries/armem/ltm/MemoryFileSystemStorage.cpp b/source/RobotAPI/libraries/armem/ltm/MemoryFileSystemStorage.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd9fb2bceea66868e9eab8b3eb3648673c85dca8 --- /dev/null +++ b/source/RobotAPI/libraries/armem/ltm/MemoryFileSystemStorage.cpp @@ -0,0 +1,142 @@ +#include "MemoryFileSystemStorage.h" + +namespace armarx::armem::ltm +{ + MemoryFileSystemStorage::MemoryFileSystemStorage(const std::string& r, bool createFolder) : + writer(new io::NlohmannJSONDiskWriter(r, createFolder)), + reader(new io::NlohmannJSONDiskReader(r, createFolder)) + { + } + + MemoryFileSystemStorage::MemoryFileSystemStorage(const io::DiskWriterPtr& w, const io::DiskReaderPtr& r) : + writer(w), + reader(r) + { + + } + + void MemoryFileSystemStorage::writeOnDisk(const Memory& m) + { + io::DiskWriterReturnInformation info = writer->writeOnDisk(m); + updateInternalMemory(info.storedElements); + } + + void MemoryFileSystemStorage::writeOnDisk(const MemoryID& id, const CoreSegment& s) + { + io::DiskWriterReturnInformation info = writer->writeOnDisk(id, s); + updateInternalMemory(info.storedElements); + } + + void MemoryFileSystemStorage::writeOnDisk(const MemoryID& id, const ProviderSegment& s) + { + io::DiskWriterReturnInformation info = writer->writeOnDisk(id, s); + updateInternalMemory(info.storedElements); + } + + void MemoryFileSystemStorage::writeOnDisk(const MemoryID& id, const Entity& s) + { + io::DiskWriterReturnInformation info = writer->writeOnDisk(id, s); + updateInternalMemory(info.storedElements); + } + + void MemoryFileSystemStorage::writeOnDisk(const MemoryID& id, const EntitySnapshot& s) + { + io::DiskWriterReturnInformation info = writer->writeOnDisk(id, s); + updateInternalMemory(info.storedElements); + } + + void MemoryFileSystemStorage::updateInternalMemoryFromFileSystem() + { + FileSystemLookupMemoryManager loaded = reader->readMemoryStructureFromDisk(); + updateInternalMemory(loaded); + } + + std::map<std::string, MemoryPtr> MemoryFileSystemStorage::getInternalMemoryAsRealMemory() const + { + std::map<std::string, MemoryPtr> ret; + + for (const auto& [memoryName, memory] : internalMemory.memoryLookupTable) + { + MemoryPtr m = MemoryPtr(new Memory(memoryName)); + MemoryID id; + id.memoryName = memoryName; + + for (const auto& [coreKey, coreSegment] : memory) + { + CoreSegment& c = m->addCoreSegment(coreKey); + id.coreSegmentName = coreKey; + for (const auto& [providerKey, providerSegment] : coreSegment) + { + ProviderSegment& p = c.addProviderSegment(providerKey); + id.providerSegmentName = providerKey; + for (const auto& [entityKey, entity] : providerSegment) + { + Entity& e = p.addEntity(entityKey); + id.entityName = entityKey; + for (const auto& [entityHistoryTimestamp, entitySnapshot] : entity) + { + EntitySnapshot& es = e.addSnapshot(entityHistoryTimestamp); + id.timestamp = entityHistoryTimestamp; + for (unsigned int i = 0; i < entitySnapshot.size(); ++i) + { + id.instanceIndex = i; + EntityInstancePtr instance = reader->readSingleInstanceFromDisk(entitySnapshot[i]); + es.instances.push_back(std::move(instance)); + } + } + } + } + } + ret.insert(std::make_pair(memoryName, std::move(m))); + } + return ret; + } + + std::string MemoryFileSystemStorage::getInternalMemoryAsString() + { + std::stringstream ss; + std::string tabs = ""; + for (const auto& [memoryName, memory] : internalMemory.memoryLookupTable) + { + ss << memoryName << ": " << std::endl; + std::string tabs = "\t"; + for (const auto& [coreKey, coreSegment] : memory) + { + ss << tabs << coreKey << ": " << std::endl; + std::string tabs = "\t\t"; + for (const auto& [providerKey, providerSegment] : coreSegment) + { + ss << tabs << providerKey << ": " << std::endl; + std::string tabs = "\t\t\t"; + for (const auto& [entityKey, entity] : providerSegment) + { + ss << tabs << entityKey << ": " << std::endl; + std::string tabs = "\t\t\t\t"; + for (const auto& [entityHistoryTimestamp, entitySnapshot] : entity) + { + ss << tabs << entityHistoryTimestamp.toMicroSeconds() << ":" << std::endl; + ss << tabs << "[" << std::endl; + for (unsigned int i = 0; i < entitySnapshot.size(); ++i) + { + const std::string& newlyGenerate = entitySnapshot[i]; + ss << tabs << "\t(" << i << " => " << newlyGenerate << ")"; + if (i < entitySnapshot.size() - 1) + { + ss << ", "; + } + ss << std::endl; + } + ss << tabs << "]" << std::endl; + } + } + } + } + } + return ss.str(); + } + + void MemoryFileSystemStorage::updateInternalMemory(const FileSystemLookupMemoryManager& info) + { + internalMemory.merge(info); + } +} diff --git a/source/RobotAPI/libraries/armem/ltm/MemoryFileSystemStorage.h b/source/RobotAPI/libraries/armem/ltm/MemoryFileSystemStorage.h new file mode 100644 index 0000000000000000000000000000000000000000..499fed293d81856db92cdac4f1d9caf10c2f80ea --- /dev/null +++ b/source/RobotAPI/libraries/armem/ltm/MemoryFileSystemStorage.h @@ -0,0 +1,72 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> + +// ArmarX +#include <RobotAPI/interface/aron.h> +#include <RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.h> +#include <RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.h> + +#include <RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.h> +#include <RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.h> + + +namespace armarx::armem +{ + namespace ltm + { + class MemoryFileSystemStorage; + typedef std::shared_ptr<MemoryFileSystemStorage> LongTermMemoryLUTPtr; + + class MemoryFileSystemStorage + { + public: + MemoryFileSystemStorage() = delete; + MemoryFileSystemStorage(const std::string&, bool createFolder = false); + MemoryFileSystemStorage(const io::DiskWriterPtr&, const io::DiskReaderPtr&); + + void writeOnDisk(const Memory&); + void writeOnDisk(const MemoryID&, const CoreSegment&); + void writeOnDisk(const MemoryID&, const ProviderSegment&); + void writeOnDisk(const MemoryID&, const Entity&); + void writeOnDisk(const MemoryID&, const EntitySnapshot&); + + void updateInternalMemoryFromFileSystem(); + EntityPtr getEntityFromFileSystem(const MemoryID&); + + std::map<std::string, MemoryPtr> getInternalMemoryAsRealMemory() const; + std::string getInternalMemoryAsString(); + + private: + void updateInternalMemory(const FileSystemLookupMemoryManager& info); + + private: + FileSystemLookupMemoryManager internalMemory; + + io::DiskWriterPtr writer; + io::DiskReaderPtr reader; + }; + } +} diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.cpp b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38050151327e2c759c4a0f4f5f870d91f6b07fbf --- /dev/null +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.cpp @@ -0,0 +1,160 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#include "DiskReader.h" + + +namespace armarx::armem +{ + namespace ltm + { + namespace io + { + DiskReader::DiskReader(bool createFolder) : + DiskReaderWriter(createFolder) + {} + + DiskReader::DiskReader(const std::string& r, bool createFolder) : + DiskReaderWriter(r, createFolder) + { + } + + FileSystemLookupMemoryManager DiskReader::readMemoryStructureFromDisk() + { + FileSystemLookupMemoryManager ret; + + for (const auto& memoryFolder : std::filesystem::directory_iterator(rootPath)) + { + if (!std::filesystem::is_directory(memoryFolder)) + { + continue; + } + + std::string folderName = memoryFolder.path().filename(); + FileSystemLookupMemoryManager tmp = readMemoryStructureFromDisk(folderName); + ret.merge(tmp); + } + return ret; + } + + FileSystemLookupMemoryManager DiskReader::readMemoryStructureFromDisk(const std::string& memoryFolderName) + { + FileSystemLookupMemoryManager ret; + + std::filesystem::path p = rootPath / memoryFolderName; + if (!std::filesystem::is_directory(p)) + { + return ret; + } + + for (const auto& coreSegmentFolder : std::filesystem::directory_iterator(p)) + { + if (!std::filesystem::is_directory(coreSegmentFolder)) + { + continue; + } + std::string coreSegmentName = coreSegmentFolder.path().filename(); + for (const auto& providerSegmentFolder : std::filesystem::directory_iterator(coreSegmentFolder.path())) + { + if (!std::filesystem::is_directory(providerSegmentFolder)) + { + continue; + } + std::string providerSegmentName = providerSegmentFolder.path().filename(); + for (const auto& entityFolder : std::filesystem::directory_iterator(providerSegmentFolder.path())) + { + if (!std::filesystem::is_directory(entityFolder)) + { + continue; + } + std::string entityName = entityFolder.path().filename(); + for (const auto& entityHistoryFolder : std::filesystem::directory_iterator(entityFolder.path())) + { + if (!std::filesystem::is_directory(entityHistoryFolder)) + { + continue; + } + Time entityTimestamp = timeFromString(entityHistoryFolder.path().filename()); + + // TODO: make this relative to amount of files in folder + for (unsigned int i = 0; i < 10000; ++i) + { + std::filesystem::path entityInstance = entityHistoryFolder / (std::to_string(i) + getEntityInstanceSuffix()); + if (!std::filesystem::is_regular_file(entityInstance)) + { + break; + } + FileSystemLookupMemory& mem = ret.memoryLookupTable[memoryFolderName]; + FileSystemLookupCoreSegment& mem_core = mem[coreSegmentName]; + FileSystemLookupProviderSegment& mem_prov = mem_core[providerSegmentName]; + FileSystemLookupEntity& mem_entity = mem_prov[entityName]; + FileSystemLookupEntityHistory& mem_entity_history = mem_entity[entityTimestamp]; + + mem_entity_history.push_back(entityInstance); + } + } + } + } + } + return ret; + } + + EntityInstancePtr DiskReader::readSingleInstanceFromDisk(const std::filesystem::path& p, const aron::typenavigator::AronTypeNavigatorPtr& expectedStructure) const + { + if (!std::filesystem::is_regular_file(p)) + { + throw error::PathNotARegularFile(p.string(), "During readSingleInstanceFromDisk tried to read an entityInstance from a non-file path."); + } + + std::ifstream ifs(p); + std::string file_content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); + + aron::datanavigator::AronDictDataNavigatorPtr dictdata = getStringAsDataNavigator(file_content, expectedStructure); + return unwrapData(dictdata); + } + + EntityInstancePtr DiskReader::unwrapData(const aron::datanavigator::AronDictDataNavigatorPtr& dataWrapped) const + { + EntityInstancePtr e = EntityInstancePtr(new EntityInstance()); + EntityInstanceMetadata& metadata = e->metadata(); + + aron::datanavigator::AronDictDataNavigatorPtr data = aron::datanavigator::AronDictDataNavigator::DynamicCastAndCheck(dataWrapped->getElement(DISK_READER_WRITER_DATA_FIELD)); + e->setData(data); + + // not used right now + //aron::datanavigator::AronLongDataNavigatorPtr timeWrapped = aron::datanavigator::AronLongDataNavigator::DynamicCastAndCheck(dataWrapped->getElement(DISK_READER_WRITER_TIME_WRAPPED_FIELD)); + + aron::datanavigator::AronLongDataNavigatorPtr timeCreated = aron::datanavigator::AronLongDataNavigator::DynamicCastAndCheck(dataWrapped->getElement(DISK_READER_WRITER_TIME_CREATED_FIELD)); + metadata.timeCreated = Time::microSeconds(timeCreated->toAronLongPtr()->value); + + aron::datanavigator::AronLongDataNavigatorPtr timeSent = aron::datanavigator::AronLongDataNavigator::DynamicCastAndCheck(dataWrapped->getElement(DISK_READER_WRITER_TIME_SENT_FIELD)); + metadata.timeSent = Time::microSeconds(timeSent->toAronLongPtr()->value); + + aron::datanavigator::AronLongDataNavigatorPtr timeArrived = aron::datanavigator::AronLongDataNavigator::DynamicCastAndCheck(dataWrapped->getElement(DISK_READER_WRITER_TIME_ARRIVED_FIELD)); + metadata.timeArrived = Time::microSeconds(timeArrived->toAronLongPtr()->value); + + aron::datanavigator::AronDoubleDataNavigatorPtr confidence = aron::datanavigator::AronDoubleDataNavigator::DynamicCastAndCheck(dataWrapped->getElement(DISK_READER_WRITER_CONFIDENCE_FIELD)); + metadata.confidence = confidence->toAronDoublePtr()->value; + + return e; + } + } + } +} diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.h b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.h index 895c888cbc3ca47ecfec4ce17a22209bcf989ee2..249b9c4aa132196ddcf8e0415fa1126ef40da103 100644 --- a/source/RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.h +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.h @@ -23,9 +23,26 @@ // STD/STL #include <memory> #include <string> +#include <sstream> +#include <fstream> +#include <iostream> +#include <filesystem> + +// BaseClass +#include <RobotAPI/libraries/armem/ltm/io/DiskReaderWriter.h> // ArmarX #include <RobotAPI/interface/aron.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronAllDataNavigators.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronAllTypeNavigators.h> + +#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NavigatorWriter/AronDataNavigatorWriter.h> + +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NavigatorWriter/AronTypeNavigatorWriter.h> + +#include <RobotAPI/libraries/armem/ltm/FileSystemLookupMemory.h> @@ -38,10 +55,22 @@ namespace armarx::armem class DiskReader; typedef std::shared_ptr<DiskReader> DiskReaderPtr; - class DiskReader + class DiskReader : + virtual public DiskReaderWriter { public: - DiskReader() = default; + DiskReader(bool createFolder); + DiskReader(const std::string&, bool createFolder); + + FileSystemLookupMemoryManager readMemoryStructureFromDisk(); + FileSystemLookupMemoryManager readMemoryStructureFromDisk(const std::string&); + EntityInstancePtr readSingleInstanceFromDisk(const std::filesystem::path&, const aron::typenavigator::AronTypeNavigatorPtr& expectedStructure = nullptr) const; + + protected: + EntityInstancePtr unwrapData(const aron::datanavigator::AronDictDataNavigatorPtr&) const; + + virtual aron::datanavigator::AronDictDataNavigatorPtr getStringAsDataNavigator(const std::string&, const aron::typenavigator::AronTypeNavigatorPtr&) const = 0; + virtual aron::typenavigator::AronObjectTypeNavigatorPtr getStringAsTypeNavigator(const std::string& s) const = 0; }; } } diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.cpp b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.cpp index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..dc9d473694eee1b5b0761dd45f61ea48034f141a 100644 --- a/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.cpp +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.cpp @@ -0,0 +1,33 @@ +#include "NlohmannJSONDiskReader.h" + +namespace armarx::armem::ltm::io +{ + + NlohmannJSONDiskReader::NlohmannJSONDiskReader(bool createFolder) : + DiskReaderWriter(createFolder), + DiskReader(createFolder) + { + } + + NlohmannJSONDiskReader::NlohmannJSONDiskReader(const std::string& rootPath, bool createFolder) : + DiskReaderWriter(createFolder), + DiskReader(rootPath, createFolder) + { + } + + aron::datanavigator::AronDictDataNavigatorPtr NlohmannJSONDiskReader::getStringAsDataNavigator(const std::string& s, const aron::typenavigator::AronTypeNavigatorPtr& expectedStructure) const + { + aron::io::AronDataNlohmannJSONReader dataReader(s); + aron::io::AronDataNavigatorWriter navWriter; + aron::io::LegacyAronDataConverter::ConvertFromReader(dataReader, navWriter, expectedStructure); + return aron::datanavigator::AronDictDataNavigator::DynamicCastAndCheck(navWriter.getResult()); + } + + aron::typenavigator::AronObjectTypeNavigatorPtr NlohmannJSONDiskReader::getStringAsTypeNavigator(const std::string& s) const + { + aron::io::AronTypeNlohmannJSONReader typeReader(s); + aron::io::AronTypeNavigatorWriter navWriter; + aron::io::LegacyAronTypeConverter::ConvertFromReader(typeReader, navWriter); + return aron::typenavigator::AronObjectTypeNavigator::DynamicCastAndCheck(navWriter.getResult()); + } +} diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.h b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.h index 1787f168640e81199d7c0a72df9ad420b8d705c6..d78539ae0e578a5e481aa8531a20b190bf47f661 100644 --- a/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.h +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskReader/NlohmannJSONDiskReader/NlohmannJSONDiskReader.h @@ -27,6 +27,9 @@ // Base Class #include <RobotAPI/libraries/armem/ltm/io/DiskReader/DiskReader.h> +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.h> namespace armarx::armem { @@ -41,7 +44,22 @@ namespace armarx::armem virtual public DiskReader { public: - NlohmannJSONDiskReader() = default; + NlohmannJSONDiskReader(bool createFolder); + NlohmannJSONDiskReader(const std::string& rootPath, bool createFolder); + + aron::datanavigator::AronDictDataNavigatorPtr getStringAsDataNavigator(const std::string&, const aron::typenavigator::AronTypeNavigatorPtr& expectedStructure = nullptr) const override; + aron::typenavigator::AronObjectTypeNavigatorPtr getStringAsTypeNavigator(const std::string& s) const override; + + protected: + std::string getEntityInstanceSuffix() const override + { + return ".data.nlohmann_json_serializer.json"; + } + + std::string getTypeSuffix() const override + { + return ".type.nlohmann_json_serializer.json"; + } }; } } diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskReaderWriter.h b/source/RobotAPI/libraries/armem/ltm/io/DiskReaderWriter.h new file mode 100644 index 0000000000000000000000000000000000000000..497d4374c258b7794322f6aaa9bf13bad31d1871 --- /dev/null +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskReaderWriter.h @@ -0,0 +1,101 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> +#include <sstream> +#include <fstream> +#include <iostream> +#include <filesystem> + +// ArmarX +#include <RobotAPI/interface/aron.h> + +#include <RobotAPI/libraries/armem/error.h> +#include <RobotAPI/libraries/armem/memory/Memory.h> +#include <RobotAPI/libraries/armem/memory/CoreSegment.h> +#include <RobotAPI/libraries/armem/memory/Entity.h> + + +namespace armarx::armem +{ + namespace ltm + { + class DiskReaderWriter + { + public: + DiskReaderWriter(bool createIfNotExistent) : + DiskReaderWriter("/tmp/MemoryExport", createIfNotExistent) + {} + DiskReaderWriter(const std::string& p, bool createIfNotExistent) : + rootPath(p) + { + if (!std::filesystem::exists(rootPath.parent_path())) + { + throw error::PathDoesNotExist(rootPath.parent_path().string(), "The parent path does not exist. At least this directory must exist!"); + } + + if (!std::filesystem::is_directory(rootPath.parent_path())) + { + throw error::PathDoesNotExist(rootPath.parent_path().string(), "The parent path is not a directory. It must be a directory."); + } + + if (!std::filesystem::exists(rootPath)) + { + if (createIfNotExistent) + { + std::filesystem::create_directory(rootPath); + } + else + { + throw error::PathDoesNotExist(rootPath.string(), "The path does not exist and a new folder should not be created!"); + } + } + + if (!std::filesystem::is_directory(rootPath)) + { + throw error::PathNotADirectory(rootPath.string(), "The Path is not valid. It points to a file instead of a folder!"); + } + } + + protected: + virtual std::string getEntityInstanceSuffix() const = 0; + virtual std::string getTypeSuffix() const = 0; + + Time timeFromString(const std::string& s) const + { + return Time::microSeconds(std::stol(s)); + } + + protected: + std::filesystem::path rootPath; + + const std::string DISK_READER_WRITER_DATA_FIELD = "LTM_WRAPPER_DATA"; + const std::string DISK_READER_WRITER_TIME_WRAPPED_FIELD = "LTM_WRAPPER_TIME_WRAPPED"; + const std::string DISK_READER_WRITER_TIME_CREATED_FIELD = "ENTITY_METADATA_TIME_CREATED"; + const std::string DISK_READER_WRITER_TIME_SENT_FIELD = "ENTITY_METADATA_TIME_SENT"; + const std::string DISK_READER_WRITER_TIME_ARRIVED_FIELD = "ENTITY_METADATA_TIME_ARRIVED"; + const std::string DISK_READER_WRITER_CONFIDENCE_FIELD = "ENTITY_METADATA_CONFIDENCE"; + }; + } +} diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.cpp b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfd780fc3ea879d80b32398315aab9c537b60a28 --- /dev/null +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.cpp @@ -0,0 +1,287 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +// STD/STL +#include "DiskWriter.h" + + +namespace armarx::armem +{ + namespace ltm + { + namespace io + { + DiskWriter::DiskWriter(bool createFolder) : + DiskReaderWriter(createFolder) + {} + + DiskWriter::DiskWriter(const std::string& r, bool createFolder) : + DiskReaderWriter(r, createFolder) + { + } + + DiskWriterReturnInformation DiskWriter::writeOnDisk(const Memory& m) + { + MemoryID id; + id.memoryName = m.name(); + + std::string memoryName = id.memoryName; + + DiskWriterReturnInformation ret(memoryName); + + std::filesystem::path p = rootPath / id.str(); + if (!ensureDirectoryPathExists(p)) + { + ret.hasError = true; + return ret; + } + + for (const auto& [coreKey, coreSegment] : m.coreSegments) + { + MemoryID storeCore(id); + storeCore.coreSegmentName = coreKey; + DiskWriterReturnInformation other = writeOnDisk(storeCore, *coreSegment); + ret.append(other); + } + return ret; + } + + DiskWriterReturnInformation DiskWriter::writeOnDisk(const MemoryID& i, const CoreSegment& s) + { + MemoryID id; + id.memoryName = i.memoryName; + id.coreSegmentName = i.coreSegmentName; + + std::string memoryName = id.memoryName; + + DiskWriterReturnInformation ret(memoryName); + + std::filesystem::path p = rootPath / id.str(); + if (!ensureDirectoryPathExists(p)) + { + ret.coreSegmentsError.push_back(id.str()); + ret.hasError = true; + return ret; + } + + if (s.hasAronType()) + { + std::string val = getTypeAsString(s.aronType()); + + std::filesystem::path coreSegmentProviderPath = rootPath / id.str() / (id.coreSegmentName + getEntityInstanceSuffix()); + std::ofstream ofs; + ofs.open(coreSegmentProviderPath); + ofs << val; + ofs.close(); + } + + for (const auto& [providerKey, providerSegment] : s.providerSegments) + { + MemoryID storeProvider(id); + storeProvider.providerSegmentName = providerKey; + DiskWriterReturnInformation other = writeOnDisk(storeProvider, *providerSegment); + ret.append(other); + } + return ret; + } + + DiskWriterReturnInformation DiskWriter::writeOnDisk(const MemoryID& i, const ProviderSegment& s) + { + MemoryID id; + id.memoryName = i.memoryName; + id.coreSegmentName = i.coreSegmentName; + id.providerSegmentName = i.providerSegmentName; + + std::string memoryName = id.memoryName; + + DiskWriterReturnInformation ret(memoryName); + + std::filesystem::path p = rootPath / id.str(); + if (!ensureDirectoryPathExists(p)) + { + ret.providerSegmentsError.push_back(id.str()); + ret.hasError = true; + return ret; + } + + if (s.hasAronType()) + { + std::string val = getTypeAsString(s.aronType()); + + std::filesystem::path providerSegmentProviderPath = rootPath / id.str() / (id.coreSegmentName + getEntityInstanceSuffix()); + std::ofstream ofs; + ofs.open(providerSegmentProviderPath); + ofs << val; + ofs.close(); + } + + for (const auto& [entityKey, entity] : s.entities) + { + MemoryID storeEntity(id); + storeEntity.entityName = entityKey; + DiskWriterReturnInformation other = writeOnDisk(storeEntity, *entity); + ret.append(other); + } + return ret; + } + + DiskWriterReturnInformation DiskWriter::writeOnDisk(const MemoryID& i, const Entity& s) + { + MemoryID id; + id.memoryName = i.memoryName; + id.coreSegmentName = i.coreSegmentName; + id.providerSegmentName = i.providerSegmentName; + id.entityName = i.entityName; + + std::string memoryName = id.memoryName; + + DiskWriterReturnInformation ret(memoryName); + + std::filesystem::path p = rootPath / id.str(); + if (!ensureDirectoryPathExists(p)) + { + ret.entitiesError.push_back(id.str()); + ret.hasError = true; + return ret; + } + + for (const auto& [ts, snapshot] : s.history) + { + MemoryID storeSnapshot(id); + storeSnapshot.timestamp = ts; + DiskWriterReturnInformation other = writeOnDisk(storeSnapshot, *snapshot); + ret.append(other); + } + return ret; + } + + DiskWriterReturnInformation DiskWriter::writeOnDisk(const MemoryID& i, const EntitySnapshot& s) + { + MemoryID id; + id.memoryName = i.memoryName; + id.coreSegmentName = i.coreSegmentName; + id.providerSegmentName = i.providerSegmentName; + id.entityName = i.entityName; + id.timestamp = i.timestamp; + + std::string memoryName = id.memoryName; + + DiskWriterReturnInformation ret(memoryName); + + std::filesystem::path p = rootPath / id.str(); + if (!ensureDirectoryPathExists(p)) + { + ret.historyTimestampsError.push_back(id.str()); + ret.hasError = true; + return ret; + } + + unsigned int x = 0; + for (const auto& instance : s.instances) + { + MemoryID storeInstance(id); + storeInstance.instanceIndex = x; + + std::filesystem::path entityElementPath = rootPath / (storeInstance.str() + getEntityInstanceSuffix()); + std::string val = getDataAsString(wrapData(*instance)); + + if (filePathExists(entityElementPath)) + { + if (std::filesystem::is_directory(entityElementPath)) + { + ret.entityInstancesError.push_back(id.str()); + ret.hasError = true; + return ret; + } + //std::ifstream ifs(entityElementPath); + //std::string file_content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); + //if (file_content == val) + //{ + // // already written. skip + // continue; + //} + } + + std::ofstream ofs; + ofs.open(entityElementPath); + ofs << val; + ofs.close(); + ret.storedElements.memoryLookupTable[id.memoryName] + [id.coreSegmentName] + [id.providerSegmentName] + [id.entityName] + [id.timestamp].push_back(entityElementPath); + + x++; + } + return ret; + } + + aron::datanavigator::AronDictDataNavigatorPtr DiskWriter::wrapData(const EntityInstance& e) const + { + aron::datanavigator::AronDictDataNavigatorPtr dataWrapped(new aron::datanavigator::AronDictDataNavigator()); + dataWrapped->addElement(DISK_READER_WRITER_DATA_FIELD, e.data()); + + aron::datanavigator::AronLongDataNavigatorPtr timeWrapped(new aron::datanavigator::AronLongDataNavigator()); + timeWrapped->setValue(Time::now().toMicroSeconds()); + dataWrapped->addElement(DISK_READER_WRITER_TIME_WRAPPED_FIELD, timeWrapped); + + + const EntityInstanceMetadata& metadata = e.metadata(); + aron::datanavigator::AronLongDataNavigatorPtr timeCreated(new aron::datanavigator::AronLongDataNavigator()); + timeCreated->setValue(metadata.timeCreated.toMicroSeconds()); + dataWrapped->addElement(DISK_READER_WRITER_TIME_CREATED_FIELD, timeCreated); + + aron::datanavigator::AronLongDataNavigatorPtr timeSent(new aron::datanavigator::AronLongDataNavigator()); + timeSent->setValue(metadata.timeSent.toMicroSeconds()); + dataWrapped->addElement(DISK_READER_WRITER_TIME_SENT_FIELD, timeSent); + + aron::datanavigator::AronLongDataNavigatorPtr timeArrived(new aron::datanavigator::AronLongDataNavigator()); + timeArrived->setValue(metadata.timeArrived.toMicroSeconds()); + dataWrapped->addElement(DISK_READER_WRITER_TIME_ARRIVED_FIELD, timeArrived); + + aron::datanavigator::AronDoubleDataNavigatorPtr confidence(new aron::datanavigator::AronDoubleDataNavigator()); + confidence->setValue(metadata.confidence); + dataWrapped->addElement(DISK_READER_WRITER_CONFIDENCE_FIELD, confidence); + + return dataWrapped; + } + + bool DiskWriter::directoryPathExists(const std::filesystem::path& p) const + { + return std::filesystem::exists(p) && std::filesystem::is_directory(p); + } + + bool DiskWriter::ensureDirectoryPathExists(const std::filesystem::path& p) const + { + if (!std::filesystem::exists(p)) + { + return std::filesystem::create_directory(p); + } + return directoryPathExists(p); + } + + bool DiskWriter::filePathExists(const std::filesystem::path& p) const + { + return std::filesystem::exists(p) && std::filesystem::is_regular_file(p); + } + } + } +} diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.h b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.h index 98367758de3c6b35ffa82e1306f4409fbc60b7fa..acb38e1f89d09b0238d112b26a2f8988aaa9dbfd 100644 --- a/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.h +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/DiskWriter.h @@ -28,15 +28,20 @@ #include <iostream> #include <filesystem> +// BaseClass +#include <RobotAPI/libraries/armem/ltm/io/DiskReaderWriter.h> + // ArmarX #include <RobotAPI/interface/aron.h> #include <RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronAllDataNavigators.h> #include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronAllTypeNavigators.h> + #include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.h> #include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataWriter.h> -//#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/ +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.h> #include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeWriter.h> +#include <RobotAPI/libraries/armem/ltm/FileSystemLookupMemory.h> #include <RobotAPI/libraries/armem/memory/Memory.h> #include <RobotAPI/libraries/armem/memory/CoreSegment.h> @@ -47,271 +52,69 @@ namespace armarx::armem { namespace ltm { - typedef std::vector<std::string> EasyStringEntityHistory; // absolute paths to stored files - typedef std::map<Time, EasyStringEntityHistory> EasyStringEntity; - typedef std::map<std::string, EasyStringEntity> EasyStringProviderSegment; - typedef std::map<std::string, EasyStringProviderSegment> EasyStringCoreSegment; - typedef std::map<std::string, EasyStringCoreSegment> EasyStringMemory; - namespace io { struct DiskWriterReturnInformation { - EasyStringMemory storedElements; + std::string memoryName = ""; + bool hasError = false; + + FileSystemLookupMemoryManager storedElements; std::vector<std::string> coreSegmentsError; std::vector<std::string> providerSegmentsError; std::vector<std::string> entitiesError; std::vector<std::string> historyTimestampsError; std::vector<std::string> entityInstancesError; - }; - - class DiskWriter; - using DiskWriterPtr = std::shared_ptr<DiskWriter>; - - class DiskWriter - { - public: - DiskWriter() : - rootPath("/tmp/MemoryExport/") - {} - DiskWriter(const std::string& r) : - rootPath(r) + DiskWriterReturnInformation(const std::string& m) : + memoryName(m) { - if (!std::filesystem::exists(rootPath)) - { - std::filesystem::create_directory(rootPath); - } - - if (!std::filesystem::is_directory(rootPath)) - { - throw LocalException("The Path is not valid. It points to a file instead of a folder!"); - } } - DiskWriterReturnInformation writeOnDisk(const MemoryPtr& m) + void append(const DiskWriterReturnInformation& o) { - return writeOnDisk(m->coreSegments); - } - - DiskWriterReturnInformation writeOnDisk(const std::map<std::string, CoreSegmentPtr>& m) - { - DiskWriterReturnInformation ret; - for (const auto& [coreKey, coreSegment] : m) + if (memoryName != o.memoryName) { - std::string currentMemoryPath = coreKey; - if (!ensureCoreSegmentPathExists(coreKey)) - { - ret.coreSegmentsError.push_back(currentMemoryPath); - continue; - } - ret.storedElements[coreKey] = {}; - for (const auto& [providerKey, providerSegment] : coreSegment->providerSegments) - { - currentMemoryPath += "/" + providerKey; - if (!ensureProviderSegmentPathExists(coreKey, providerKey)) - { - ret.providerSegmentsError.push_back(currentMemoryPath); - continue; - } - ret.storedElements[coreKey][providerKey] = {}; - for (const auto& [entityKey, entity] : providerSegment->entities) - { - currentMemoryPath += "/" + entityKey; - if (!ensureEntityPathExists(coreKey, providerKey, entityKey)) - { - ret.entitiesError.push_back(currentMemoryPath); - continue; - } - ret.storedElements[coreKey][providerKey][entityKey] = {}; - for (const auto& [entityHistoryTimestamp, entitySnapshot] : entity->history) - { - currentMemoryPath += "/" + std::to_string(entityHistoryTimestamp.toMilliSeconds()); - if (!ensureTimestampPathExists(coreKey, providerKey, entityKey, entityHistoryTimestamp)) - { - ret.historyTimestampsError.push_back(currentMemoryPath); - continue; - } - ret.storedElements[coreKey][providerKey][entityKey][entityHistoryTimestamp] = {}; - for (unsigned int i = 0; i < entitySnapshot->instances.size(); ++i) - { - currentMemoryPath += "/" + std::to_string(i); - std::filesystem::path entityElementPath = createEntityElementPath(coreKey, providerKey, entityKey, entityHistoryTimestamp, i); - std::string val = getDataAsString(wrapData(entitySnapshot->instances[i])); - if (entityElementPathExists(entityElementPath)) - { - if (std::filesystem::is_directory(entityElementPath)) - { - ret.entityInstancesError.push_back(currentMemoryPath); - continue; - } - std::ifstream ifs(entityElementPath); - std::string file_content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); - if (file_content == val) - { - // already written. skip - continue; - } - } - // write - std::ofstream ofs; - ofs.open(entityElementPath); - ofs << val; - ofs.close(); - ret.storedElements[coreKey][providerKey][entityKey][entityHistoryTimestamp].push_back(entityElementPath); - } - } - } - } + // The memory name must be equal + return; } - return ret; - } - - protected: - aron::datanavigator::AronDictDataNavigatorPtr wrapData(const EntityInstancePtr& e) const - { - aron::datanavigator::AronDictDataNavigatorPtr dataWrapped(new aron::datanavigator::AronDictDataNavigator()); - dataWrapped->addElement("data", e->data()); - - aron::datanavigator::AronLongDataNavigatorPtr timeWrapped(new aron::datanavigator::AronLongDataNavigator()); - timeWrapped->setValue(Time::now().toMilliSeconds()); - dataWrapped->addElement("timeWrapped", timeWrapped); - - const EntityInstanceMetadata& metadata = e->metadata(); - aron::datanavigator::AronLongDataNavigatorPtr timeCreated(new aron::datanavigator::AronLongDataNavigator()); - timeCreated->setValue(metadata.timeCreated.toMilliSeconds()); - dataWrapped->addElement("timeCreated", timeCreated); + storedElements.merge(o.storedElements); - aron::datanavigator::AronLongDataNavigatorPtr timeSent(new aron::datanavigator::AronLongDataNavigator()); - timeSent->setValue(metadata.timeSent.toMilliSeconds()); - dataWrapped->addElement("timeSent", timeSent); + coreSegmentsError.insert(coreSegmentsError.end(), o.coreSegmentsError.begin(), o.coreSegmentsError.end()); + providerSegmentsError.insert(providerSegmentsError.end(), o.providerSegmentsError.begin(), o.providerSegmentsError.end()); + entitiesError.insert(entitiesError.end(), o.entitiesError.begin(), o.entitiesError.end()); + historyTimestampsError.insert(historyTimestampsError.end(), o.historyTimestampsError.begin(), o.historyTimestampsError.end()); + entityInstancesError.insert(entityInstancesError.end(), o.entityInstancesError.begin(), o.entityInstancesError.end()); + } + }; - aron::datanavigator::AronLongDataNavigatorPtr timeArrived(new aron::datanavigator::AronLongDataNavigator()); - timeArrived->setValue(metadata.timeArrived.toMilliSeconds()); - dataWrapped->addElement("timeArrived", timeArrived); + class DiskWriter; + using DiskWriterPtr = std::shared_ptr<DiskWriter>; - aron::datanavigator::AronFloatDataNavigatorPtr confidence(new aron::datanavigator::AronFloatDataNavigator()); - confidence->setValue(metadata.confidence); - dataWrapped->addElement("confidence", confidence); + class DiskWriter : + virtual public DiskReaderWriter + { + public: + DiskWriter(bool createFolder); + DiskWriter(const std::string& r, bool createFolder); - return dataWrapped; - } + DiskWriterReturnInformation writeOnDisk(const Memory& m); + DiskWriterReturnInformation writeOnDisk(const MemoryID&, const CoreSegment&); + DiskWriterReturnInformation writeOnDisk(const MemoryID&, const ProviderSegment&); + DiskWriterReturnInformation writeOnDisk(const MemoryID&, const Entity&); + DiskWriterReturnInformation writeOnDisk(const MemoryID&, const EntitySnapshot&); - virtual std::string getDataAsString(const aron::datanavigator::AronDictDataNavigatorPtr&) = 0; + protected: + aron::datanavigator::AronDictDataNavigatorPtr wrapData(const EntityInstance& e) const; - std::filesystem::path createCoreSegmentPath(const std::string& name) const - { - return rootPath / name; - } - std::filesystem::path createProviderSegmentPath(const std::string& core, const std::string& prov) const - { - return createCoreSegmentPath(core) / prov; - } - std::filesystem::path createEntityPath(const std::string& core, const std::string& prov, const std::string& e) const - { - return createProviderSegmentPath(core, prov) / e; - } - std::filesystem::path createTimestampPath(const std::string& core, const std::string& prov, const std::string& e, const Time& t) const - { - return createEntityPath(core, prov, e) / t.toString("%Y-%m-%d %H:%M:%S"); - } - std::filesystem::path createEntityElementPath(const std::string& core, const std::string& prov, const std::string& e, const Time& t, unsigned int i) const - { - return createTimestampPath(core, prov, e, t) / std::to_string(i); - } + virtual std::string getDataAsString(const aron::datanavigator::AronDictDataNavigatorPtr&) const = 0; + virtual std::string getTypeAsString(const aron::typenavigator::AronObjectTypeNavigatorPtr&) const = 0; - bool coreSegmentPathExists(const std::string& name) const - { - return coreSegmentPathExists(createCoreSegmentPath(name)); - } - bool coreSegmentPathExists(const std::filesystem::path& p) const - { - return std::filesystem::exists(p) && std::filesystem::is_directory(p); - } - bool ensureCoreSegmentPathExists(const std::string& name) const - { - return ensureCoreSegmentPathExists(createCoreSegmentPath(name)); - } - bool ensureCoreSegmentPathExists(const std::filesystem::path& p) const - { - if (!std::filesystem::exists(p)) - { - return std::filesystem::create_directory(p); - } - return std::filesystem::is_directory(p); - } - bool providerSegmentPathExists(const std::string& core, const std::string& prov) const - { - return providerSegmentPathExists(createProviderSegmentPath(core, prov)); - } - bool providerSegmentPathExists(const std::filesystem::path& p) const - { - return std::filesystem::exists(p) && std::filesystem::is_directory(p); - } - bool ensureProviderSegmentPathExists(const std::string& core, const std::string& prov) const - { - return ensureProviderSegmentPathExists(createProviderSegmentPath(core, prov)); - } - bool ensureProviderSegmentPathExists(const std::filesystem::path& p) const - { - if (!std::filesystem::exists(p)) - { - return std::filesystem::create_directory(p); - } - return std::filesystem::is_directory(p); - } - bool entityPathExists(const std::string& core, const std::string& prov, const std::string& e) const - { - return entityPathExists(createEntityPath(core, prov, e)); - } - bool entityPathExists(const std::filesystem::path& p) const - { - return std::filesystem::exists(p) && std::filesystem::is_directory(p); - } - bool ensureEntityPathExists(const std::string& core, const std::string& prov, const std::string& e) const - { - return ensureEntityPathExists(createEntityPath(core, prov, e)); - } - bool ensureEntityPathExists(const std::filesystem::path& p) const - { - if (!std::filesystem::exists(p)) - { - return std::filesystem::create_directory(p); - } - return std::filesystem::is_directory(p); - } - bool timestampPathExists(const std::string& core, const std::string& prov, const std::string& e, const Time& t) const - { - return timestampPathExists(createTimestampPath(core, prov, e, t)); - } - bool timestampPathExists(const std::filesystem::path& p) const - { - return std::filesystem::exists(p) && std::filesystem::is_directory(p); - } - bool ensureTimestampPathExists(const std::string& core, const std::string& prov, const std::string& e, const Time& t) const - { - return ensureTimestampPathExists(createTimestampPath(core, prov, e, t)); - } - bool ensureTimestampPathExists(const std::filesystem::path& p) const - { - if (!std::filesystem::exists(p)) - { - return std::filesystem::create_directory(p); - } - return std::filesystem::is_directory(p); - } - bool entityElementPathExists(const std::string& core, const std::string& prov, const std::string& e, const Time& t, unsigned int i) const - { - return entityElementPathExists(createEntityElementPath(core, prov, e, t, i)); - } - bool entityElementPathExists(const std::filesystem::path& p) const - { - return std::filesystem::exists(p) && std::filesystem::is_regular_file(p); - } + bool directoryPathExists(const std::filesystem::path& p) const; + bool ensureDirectoryPathExists(const std::filesystem::path& p) const; - protected: - std::filesystem::path rootPath; + bool filePathExists(const std::filesystem::path& p) const; }; } } diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.cpp b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.cpp index 62ff8d75ff2b55444352fc1c908fe292e4e4a11d..ffdb1a0fb254d410feae0f7ebab73b692a4357de 100644 --- a/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.cpp +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.cpp @@ -2,16 +2,31 @@ namespace armarx::armem::ltm::io { + NlohmannJSONDiskWriter::NlohmannJSONDiskWriter(bool createFolder) : + DiskReaderWriter(createFolder), + DiskWriter(createFolder) + { + } - NlohmannJSONDiskWriter::NlohmannJSONDiskWriter(const std::string& rootPath) : - DiskWriter(rootPath) + NlohmannJSONDiskWriter::NlohmannJSONDiskWriter(const std::string& rootPath, bool createFolder) : + DiskReaderWriter(createFolder), + DiskWriter(rootPath, createFolder) { } - std::string NlohmannJSONDiskWriter::getDataAsString(const aron::datanavigator::AronDictDataNavigatorPtr& aronDataNav) + std::string NlohmannJSONDiskWriter::getDataAsString(const aron::datanavigator::AronDictDataNavigatorPtr& aronDataNav) const { aron::io::AronDataNlohmannJSONWriter dataWriter; aron::io::LegacyAronDataWriter::SetupWriterFromAronDataPtr(dataWriter, aronDataNav->getResult()); return dataWriter.getResult().dump(4); } + + std::string NlohmannJSONDiskWriter::getTypeAsString(const aron::typenavigator::AronObjectTypeNavigatorPtr& aronTypeNav) const + { + aron::io::AronTypeNlohmannJSONWriter typeWriter; + aron::io::LegacyAronTypeWriter::SetupWriterFromAronTypePtr(typeWriter, aronTypeNav->getResult()); + return typeWriter.getResult().dump(4); + } + + } diff --git a/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.h b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.h index c2c2ed9c4cc27536862ca499eed28e5446afe716..95b83bd8a2d01004def2db3f80ef686ff4797344 100644 --- a/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.h +++ b/source/RobotAPI/libraries/armem/ltm/io/DiskWriter/NlohmannJSONDiskWriter/NlohmannJSONDiskWriter.h @@ -45,10 +45,22 @@ namespace armarx::armem virtual public DiskWriter { public: - NlohmannJSONDiskWriter() = delete; - NlohmannJSONDiskWriter(const std::string& rootPath); + NlohmannJSONDiskWriter(bool createFolder); + NlohmannJSONDiskWriter(const std::string& rootPath, bool createFolder); - virtual std::string getDataAsString(const aron::datanavigator::AronDictDataNavigatorPtr&) override; + std::string getDataAsString(const aron::datanavigator::AronDictDataNavigatorPtr&) const override; + std::string getTypeAsString(const aron::typenavigator::AronObjectTypeNavigatorPtr&) const override; + + protected: + std::string getEntityInstanceSuffix() const override + { + return ".data.nlohmann_json_serializer.json"; + } + + std::string getTypeSuffix() const override + { + return ".type.nlohmann_json_serializer.json"; + } }; } } diff --git a/source/RobotAPI/libraries/armem/memory/CoreSegment.cpp b/source/RobotAPI/libraries/armem/memory/CoreSegment.cpp index 0aede9e46f8756ce238f2676894b4113d0a48886..cf618d66fe714f160414211e55966f1ff5a135bc 100644 --- a/source/RobotAPI/libraries/armem/memory/CoreSegment.cpp +++ b/source/RobotAPI/libraries/armem/memory/CoreSegment.cpp @@ -12,17 +12,17 @@ namespace armarx::armem { } - CoreSegment::CoreSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr aronType) : + CoreSegment::CoreSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr aronType) : CoreSegment(name, MemoryID(), aronType) { } - CoreSegment::CoreSegment(const std::string& name, const MemoryID& parentID, aron::typenavigator::AronTypeNavigatorPtr aronType) : + CoreSegment::CoreSegment(const std::string& name, const MemoryID& parentID, aron::typenavigator::AronObjectTypeNavigatorPtr aronType) : CoreSegment(parentID.getMemoryID().withCoreSegmentName(name), aronType) { } - CoreSegment::CoreSegment(const MemoryID& id, aron::typenavigator::AronTypeNavigatorPtr aronType) : + CoreSegment::CoreSegment(const MemoryID& id, aron::typenavigator::AronObjectTypeNavigatorPtr aronType) : Base(id, aronType) { } @@ -39,6 +39,22 @@ namespace armarx::armem return *this; } + bool CoreSegment::equalsDeep(const CoreSegment& other) const + { + for (const auto& [key, provider] : providerSegments) + { + if (not(other.hasProviderSegment(key))) + { + return false; + } + if (not(provider->equalsDeep(other.getProviderSegment(key)))) + { + return false; + } + } + return true; + } + CoreSegment::~CoreSegment() { } @@ -89,9 +105,9 @@ namespace armarx::armem } - ProviderSegment& CoreSegment::addProviderSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr providerSegmentType) + ProviderSegment& CoreSegment::addProviderSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr providerSegmentType) { - aron::typenavigator::AronTypeNavigatorPtr type = providerSegmentType ? providerSegmentType : this->aronType(); + aron::typenavigator::AronObjectTypeNavigatorPtr type = providerSegmentType ? providerSegmentType : this->aronType(); return addProviderSegment(std::make_unique<ProviderSegment>(name, type)); } diff --git a/source/RobotAPI/libraries/armem/memory/CoreSegment.h b/source/RobotAPI/libraries/armem/memory/CoreSegment.h index 9664a85222ccb5db8e88a5719d94230487cf4f3d..035cfeffe2216bf44453395f12446c26c5e3334d 100644 --- a/source/RobotAPI/libraries/armem/memory/CoreSegment.h +++ b/source/RobotAPI/libraries/armem/memory/CoreSegment.h @@ -24,12 +24,13 @@ namespace armarx::armem public: CoreSegment(); - CoreSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr); - CoreSegment(const std::string& name, const MemoryID& parentID, aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr); - CoreSegment(const MemoryID& id, aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr); + CoreSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr); + CoreSegment(const std::string& name, const MemoryID& parentID, aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr); + CoreSegment(const MemoryID& id, aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr); CoreSegment(const CoreSegment& other); CoreSegment& operator=(const CoreSegment& other); + bool equalsDeep(const CoreSegment& other) const; ~CoreSegment() override; @@ -56,7 +57,7 @@ namespace armarx::armem * @param providerSegmentType The provider type. If nullptr, the core segment type is used. * @return The added provider segment. */ - ProviderSegment& addProviderSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr providerSegmentType = nullptr); + ProviderSegment& addProviderSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr providerSegmentType = nullptr); /// Copy and insert a provider segment. ProviderSegment& addProviderSegment(const ProviderSegment& providerSegment); /// Move and insert a provider segment. diff --git a/source/RobotAPI/libraries/armem/memory/Entity.cpp b/source/RobotAPI/libraries/armem/memory/Entity.cpp index c41a1310a6928ab77a44eb2c3c95c8891f812353..7b5b4208bb82add7e998761e0736f675be78bb31 100644 --- a/source/RobotAPI/libraries/armem/memory/Entity.cpp +++ b/source/RobotAPI/libraries/armem/memory/Entity.cpp @@ -35,6 +35,22 @@ namespace armarx::armem return *this; } + bool Entity::equalsDeep(const Entity& other) const + { + for (const auto& [key, snapshot] : history) + { + if (not(other.hasSnapshot(key))) + { + return false; + } + if (not(snapshot->equalsDeep(other.getSnapshot(key)))) + { + return false; + } + } + return true; + } + void Entity::_copySelf(Entity& other) const { @@ -157,6 +173,10 @@ namespace armarx::armem return *it->second; } + EntitySnapshot& Entity::addSnapshot(const Time& timestamp) + { + return addSnapshot(std::make_unique<EntitySnapshot>(timestamp)); + } MemoryID Entity::update(const InternalEntityUpdate& update) { diff --git a/source/RobotAPI/libraries/armem/memory/Entity.h b/source/RobotAPI/libraries/armem/memory/Entity.h index 476d0fb9a3bc4a434f57b2bcb0abff5098e12fdd..243f43597f3508e82e78fc1084284ce6b18e6073 100644 --- a/source/RobotAPI/libraries/armem/memory/Entity.h +++ b/source/RobotAPI/libraries/armem/memory/Entity.h @@ -52,6 +52,7 @@ namespace armarx::armem Entity(const Entity& other); /// Copy the history from `other` to this. Entity& operator=(const Entity& other); + bool equalsDeep(const Entity& other) const; inline const std::string& name() const @@ -124,6 +125,7 @@ namespace armarx::armem */ EntitySnapshot& addSnapshot(const EntitySnapshot& snapshot); EntitySnapshot& addSnapshot(EntitySnapshotPtr&& snapshot); + EntitySnapshot& addSnapshot(const Time& timestamp); /** diff --git a/source/RobotAPI/libraries/armem/memory/EntityInstance.cpp b/source/RobotAPI/libraries/armem/memory/EntityInstance.cpp index a46096fbd5f7008c591f7f52c3ccf6dbaf145194..22a4e18e5de05071d507d8c0da13340ad8decb7e 100644 --- a/source/RobotAPI/libraries/armem/memory/EntityInstance.cpp +++ b/source/RobotAPI/libraries/armem/memory/EntityInstance.cpp @@ -23,6 +23,19 @@ namespace armarx::armem { } + bool EntityInstance::equalsDeep(const EntityInstance& other) const + { + if (not(_metadata == other.metadata())) + { + return false; + } + if (not(_data == other.data())) + { + return false; + } + return true; + } + void EntityInstance::update(const InternalEntityUpdate& update, int index) { ARMARX_CHECK_FITS_SIZE(index, update.instancesData.size()); diff --git a/source/RobotAPI/libraries/armem/memory/EntityInstance.h b/source/RobotAPI/libraries/armem/memory/EntityInstance.h index c9b46753bf5d8335e375d1d1a31aa6bf696bd574..ba3786bc42bec21ae6efb9c1c6d6c8a5a4d84986 100644 --- a/source/RobotAPI/libraries/armem/memory/EntityInstance.h +++ b/source/RobotAPI/libraries/armem/memory/EntityInstance.h @@ -28,6 +28,14 @@ namespace armarx::armem /// An optional confidence, may be used for things like decay. float confidence = 1.0; + + bool operator==(const EntityInstanceMetadata& other) const + { + return timeCreated == other.timeCreated && + timeSent == other.timeSent && + timeArrived == other.timeArrived && + confidence == other.confidence; + } }; @@ -47,6 +55,7 @@ namespace armarx::armem virtual ~EntityInstance() override; + bool equalsDeep(const EntityInstance& other) const; inline int& index() { diff --git a/source/RobotAPI/libraries/armem/memory/EntitySnapshot.cpp b/source/RobotAPI/libraries/armem/memory/EntitySnapshot.cpp index fcf6720caf7246ebe8d368e373dc1f84cdeedeac..cbc5d7ef0ed6b270fa6d576d8be0c0c60396e283 100644 --- a/source/RobotAPI/libraries/armem/memory/EntitySnapshot.cpp +++ b/source/RobotAPI/libraries/armem/memory/EntitySnapshot.cpp @@ -34,6 +34,24 @@ namespace armarx::armem return *this; } + bool EntitySnapshot::equalsDeep(const EntitySnapshot& other) const + { + unsigned int i = 0; + for (const auto& instance : instances) + { + if (not(other.hasInstance(i))) + { + return false; + } + if (not(instance->equalsDeep(other.getInstance(i)))) + { + return false; + } + i++; + } + return true; + } + void EntitySnapshot::_copyElements(EntitySnapshot& other, bool withData) const { @@ -63,6 +81,12 @@ namespace armarx::armem } } + bool EntitySnapshot::hasInstance(int index) const + { + size_t si = size_t(index); + return index >= 0 && si < instances.size(); + } + EntityInstance& EntitySnapshot::getInstance(int index) { @@ -71,9 +95,9 @@ namespace armarx::armem const EntityInstance& EntitySnapshot::getInstance(int index) const { - size_t si = size_t(index); - if (index >= 0 && si < instances.size()) + if (hasInstance(index)) { + size_t si = size_t(index); return *instances[si]; } else diff --git a/source/RobotAPI/libraries/armem/memory/EntitySnapshot.h b/source/RobotAPI/libraries/armem/memory/EntitySnapshot.h index 82ebf6bfabd6f26f3e593e01a41b64b75cf7b813..a3371c47767719920ef3d43ef28b0a97bc57dfd3 100644 --- a/source/RobotAPI/libraries/armem/memory/EntitySnapshot.h +++ b/source/RobotAPI/libraries/armem/memory/EntitySnapshot.h @@ -36,6 +36,7 @@ namespace armarx::armem EntitySnapshot(const EntitySnapshot& other); /// Copy the instances from `other` to this. EntitySnapshot& operator=(const EntitySnapshot& other); + bool equalsDeep(const EntitySnapshot& other) const; inline Time& time() @@ -52,6 +53,8 @@ namespace armarx::armem std::optional<MemoryID> parentID = std::nullopt); + bool hasInstance(int index) const; + /** * @brief Get the given instance. * @param index The instance's index. diff --git a/source/RobotAPI/libraries/armem/memory/Memory.cpp b/source/RobotAPI/libraries/armem/memory/Memory.cpp index c14d18a61ea2d383684b9739df25156f1f0c3b42..9157597ab295dd3bcf7feb6989eaf97f152b7de2 100644 --- a/source/RobotAPI/libraries/armem/memory/Memory.cpp +++ b/source/RobotAPI/libraries/armem/memory/Memory.cpp @@ -33,6 +33,23 @@ namespace armarx::armem return *this; } + bool Memory::equalsDeep(const Memory& other) const + { + for (const auto& [key, core] : coreSegments) + { + if (not(other.hasCoreSegment(key))) + { + return false; + } + + if (not(core->equalsDeep(other.getCoreSegment(key)))) + { + return false; + } + } + return true; + } + Memory::~Memory() { } @@ -84,7 +101,7 @@ namespace armarx::armem } - CoreSegment& Memory::addCoreSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr coreSegmentType) + CoreSegment& Memory::addCoreSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr coreSegmentType) { return addCoreSegment(std::make_unique<CoreSegment>(name, coreSegmentType)); } diff --git a/source/RobotAPI/libraries/armem/memory/Memory.h b/source/RobotAPI/libraries/armem/memory/Memory.h index 37f34d3c0122cca150421c2cf033392cad34b237..f9409e5686ada9634a2824629f0d8c1755c7f6d5 100644 --- a/source/RobotAPI/libraries/armem/memory/Memory.h +++ b/source/RobotAPI/libraries/armem/memory/Memory.h @@ -28,6 +28,7 @@ namespace armarx::armem Memory(const Memory& other); Memory& operator=(const Memory& other); + bool equalsDeep(const Memory& other) const; ~Memory() override; @@ -51,7 +52,7 @@ namespace armarx::armem * @param coreSegmentType The core segment type (optional). * @return The added core segment. */ - CoreSegment& addCoreSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr coreSegmentType = nullptr); + CoreSegment& addCoreSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr coreSegmentType = nullptr); /// Copy and insert a core segment. CoreSegment& addCoreSegment(const CoreSegment& coreSegment); /// Move and insert a core segment. diff --git a/source/RobotAPI/libraries/armem/memory/ProviderSegment.cpp b/source/RobotAPI/libraries/armem/memory/ProviderSegment.cpp index 04a5966752735d5867651c63d8e2ebb48de6475e..ca772cc4bc6fcc76bd8801b3970986b2fb0938bc 100644 --- a/source/RobotAPI/libraries/armem/memory/ProviderSegment.cpp +++ b/source/RobotAPI/libraries/armem/memory/ProviderSegment.cpp @@ -12,17 +12,17 @@ namespace armarx::armem { } - ProviderSegment::ProviderSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr aronType) : + ProviderSegment::ProviderSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr aronType) : ProviderSegment(name, MemoryID(), aronType) { } - ProviderSegment::ProviderSegment(const std::string& name, const MemoryID parentID, aron::typenavigator::AronTypeNavigatorPtr aronType) : + ProviderSegment::ProviderSegment(const std::string& name, const MemoryID parentID, aron::typenavigator::AronObjectTypeNavigatorPtr aronType) : ProviderSegment(parentID.withProviderSegmentName(name), aronType) { } - ProviderSegment::ProviderSegment(const MemoryID id, aron::typenavigator::AronTypeNavigatorPtr aronType) : + ProviderSegment::ProviderSegment(const MemoryID id, aron::typenavigator::AronObjectTypeNavigatorPtr aronType) : Base(id, aronType) { } @@ -39,6 +39,23 @@ namespace armarx::armem return *this; } + bool ProviderSegment::equalsDeep(const ProviderSegment& other) const + { + for (const auto& [key, value] : entities) + { + if (not(other.hasEntity(key))) + { + return false; + } + + if (not(value->equalsDeep(other.getEntity(key)))) + { + return false; + } + } + return true; + } + ProviderSegment::~ProviderSegment() { } diff --git a/source/RobotAPI/libraries/armem/memory/ProviderSegment.h b/source/RobotAPI/libraries/armem/memory/ProviderSegment.h index 6a9b2f820ba11972fbda84b1f29ec431b1fb0500..f6d0d49570e6948bbe3c136a76b334872f580ea5 100644 --- a/source/RobotAPI/libraries/armem/memory/ProviderSegment.h +++ b/source/RobotAPI/libraries/armem/memory/ProviderSegment.h @@ -25,12 +25,13 @@ namespace armarx::armem public: ProviderSegment(); - ProviderSegment(const std::string& name, aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr); - ProviderSegment(const std::string& name, const MemoryID parentID, aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr); - ProviderSegment(const MemoryID id, aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr); + ProviderSegment(const std::string& name, aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr); + ProviderSegment(const std::string& name, const MemoryID parentID, aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr); + ProviderSegment(const MemoryID id, aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr); ProviderSegment(const ProviderSegment& other); ProviderSegment& operator=(const ProviderSegment& other); + bool equalsDeep(const ProviderSegment& other) const; ~ProviderSegment() override; diff --git a/source/RobotAPI/libraries/armem/memory/detail/TypedEntityContainer.h b/source/RobotAPI/libraries/armem/memory/detail/TypedEntityContainer.h index 9efa3b339a617f62b56e8577b03be20f04ec0332..5a74fd0e742bc33ae3f1d75ea92ea3489d4806f3 100644 --- a/source/RobotAPI/libraries/armem/memory/detail/TypedEntityContainer.h +++ b/source/RobotAPI/libraries/armem/memory/detail/TypedEntityContainer.h @@ -2,6 +2,7 @@ #include "EntityContainer.h" +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.h> namespace armarx::armem::detail { @@ -17,9 +18,9 @@ namespace armarx::armem::detail public: - TypedEntityContainer(aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr) : _aronType(aronType) + TypedEntityContainer(aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr) : _aronType(aronType) {} - TypedEntityContainer(const MemoryID& id, aron::typenavigator::AronTypeNavigatorPtr aronType = nullptr) : + TypedEntityContainer(const MemoryID& id, aron::typenavigator::AronObjectTypeNavigatorPtr aronType = nullptr) : EntityContainer<ValueT, Derived>(id), _aronType(aronType) {} @@ -31,11 +32,11 @@ namespace armarx::armem::detail { return _aronType != nullptr; } - aron::typenavigator::AronTypeNavigatorPtr& aronType() + aron::typenavigator::AronObjectTypeNavigatorPtr& aronType() { return _aronType; } - aron::typenavigator::AronTypeNavigatorPtr aronType() const + aron::typenavigator::AronObjectTypeNavigatorPtr aronType() const { return _aronType; } @@ -54,7 +55,7 @@ namespace armarx::armem::detail private: /// The expected Aron type. May be nullptr, in which case no type information is available. - aron::typenavigator::AronTypeNavigatorPtr _aronType; + aron::typenavigator::AronObjectTypeNavigatorPtr _aronType; }; diff --git a/source/RobotAPI/libraries/armem/memory/ice_conversions.cpp b/source/RobotAPI/libraries/armem/memory/ice_conversions.cpp index de78dc0a4043c3ac9cbdf4dc3e827af93a2bcfce..9019aac9e44f1ee19b80aef1c64aab93fc1c52c5 100644 --- a/source/RobotAPI/libraries/armem/memory/ice_conversions.cpp +++ b/source/RobotAPI/libraries/armem/memory/ice_conversions.cpp @@ -54,7 +54,7 @@ namespace armarx if (data.data()) { - ice.data = data.data()->toAronDict(); + ice.data = data.data()->toAronDictPtr(); } toIce(ice.metadata, data.metadata()); } @@ -64,7 +64,7 @@ namespace armarx if (ice.data) { - data.setData(aron::datanavigator::AronDictDataNavigator::FromAronDict(ice.data)); + data.setData(aron::datanavigator::AronDictDataNavigator::FromAronDictPtr(ice.data)); } fromIce(ice.metadata, data.metadata()); } @@ -113,7 +113,9 @@ namespace armarx if (ice.aronType) { - providerSegment.aronType() = aron::typenavigator::AronTypeNavigator::FromAronType(ice.aronType); + providerSegment.aronType() = aron::typenavigator::AronObjectTypeNavigator::DynamicCastAndCheck( + aron::typenavigator::AronTypeNavigator::FromAronType(ice.aronType) + ); } fromIce(ice.entities, providerSegment.entities); } @@ -134,7 +136,9 @@ namespace armarx if (ice.aronType) { - coreSegment.aronType() = aron::typenavigator::AronTypeNavigator::FromAronType(ice.aronType); + coreSegment.aronType() = aron::typenavigator::AronObjectTypeNavigator::DynamicCastAndCheck( + aron::typenavigator::AronTypeNavigator::FromAronType(ice.aronType) + ); } fromIce(ice.providerSegments, coreSegment.providerSegments); } diff --git a/source/RobotAPI/libraries/armem/test/ArMemLTMTest.cpp b/source/RobotAPI/libraries/armem/test/ArMemLTMTest.cpp index 9afa46513fec7b71d43469e8540160df61b52f65..f0e380172e36313ff883a1b05e3fcc080cb8a2a4 100644 --- a/source/RobotAPI/libraries/armem/test/ArMemLTMTest.cpp +++ b/source/RobotAPI/libraries/armem/test/ArMemLTMTest.cpp @@ -29,7 +29,9 @@ #include "../error/ArMemError.h" #include <RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronAllDataNavigators.h> -#include <RobotAPI/libraries/armem/ltm/LongTermMemoryLUT.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronAllTypeNavigators.h> +#include <RobotAPI/libraries/armem/ltm/MemoryFileSystemStorage.h> +#include <RobotAPI/libraries/aron/aroncore/AronRandomizer.h> #include <iostream> @@ -39,117 +41,69 @@ namespace aron = armarx::aron; BOOST_AUTO_TEST_CASE(test_memory_heavy_setup_and_export) { - armem::InternalEntityUpdate update; + aron::AronRandomizer r; - armem::MemoryPtr memory = std::make_unique<armem::Memory>("Memory"); - BOOST_CHECK_EQUAL(memory->name(), "Memory"); - { - update.entityID = armem::MemoryID::fromString("OtherMemory/SomeSegment"); - BOOST_CHECK_THROW(memory->update(update), armem::error::ContainerNameMismatch); - update.entityID = armem::MemoryID::fromString("Memory/MissingSegment"); - BOOST_CHECK_THROW(memory->update(update), armem::error::MissingEntry); - } + std::string memoryName = "TestMemory"; + armem::MemoryPtr memory = std::make_unique<armem::Memory>(memoryName); + BOOST_CHECK_EQUAL(memory->name(), memoryName); armem::CoreSegment& coreSegment = memory->addCoreSegment("TestCoreSegment"); BOOST_CHECK_EQUAL(coreSegment.name(), "TestCoreSegment"); BOOST_CHECK(memory->hasCoreSegment(coreSegment.name())); - { - update.entityID = armem::MemoryID::fromString("Memory/OtherCoreSegment"); - BOOST_CHECK_THROW(coreSegment.update(update), armem::error::ContainerNameMismatch); - update.entityID = armem::MemoryID::fromString("Memory/TestCoreSegment/MissingProvider"); - BOOST_CHECK_THROW(coreSegment.update(update), armem::error::MissingEntry); - } armem::ProviderSegment& providerSegment = coreSegment.addProviderSegment("TestProvider"); BOOST_CHECK_EQUAL(providerSegment.name(), "TestProvider"); BOOST_CHECK(coreSegment.hasProviderSegment(providerSegment.name())); - { - update.entityID = armem::MemoryID::fromString("Memory/TestCoreSegment/OtherTestProvider"); - BOOST_CHECK_THROW(providerSegment.update(update), armem::error::ContainerNameMismatch); - } + aron::typenavigator::AronObjectTypeNavigatorPtr t(new aron::typenavigator::AronObjectTypeNavigator(aron::AronPath())); + t->setObjectName("TestObjectType1"); + + aron::typenavigator::AronIntTypeNavigatorPtr tm1(new aron::typenavigator::AronIntTypeNavigator(aron::AronPath())); + aron::typenavigator::AronIntTypeNavigatorPtr tm2(new aron::typenavigator::AronIntTypeNavigator(aron::AronPath())); + aron::typenavigator::AronIntTypeNavigatorPtr tm3(new aron::typenavigator::AronIntTypeNavigator(aron::AronPath())); + aron::typenavigator::AronIntTypeNavigatorPtr tm4(new aron::typenavigator::AronIntTypeNavigator(aron::AronPath())); + t->addAcceptedType("theInt1", tm1); + t->addAcceptedType("theInt2", tm2); + t->addAcceptedType("theInt3", tm3); + t->addAcceptedType("theInt4", tm4); - // A successful update. - aron::datanavigator::AronDictDataNavigatorPtr update1instace1(new aron::datanavigator::AronDictDataNavigator()); - aron::datanavigator::AronIntDataNavigatorPtr update1instance1data1(new aron::datanavigator::AronIntDataNavigator()); - update1instance1data1->setValue(42); - aron::datanavigator::AronIntDataNavigatorPtr update1instance1data2(new aron::datanavigator::AronIntDataNavigator()); - update1instance1data2->setValue(66); - update1instace1->addElement("data1", update1instance1data1); - update1instace1->addElement("data2", update1instance1data2); - - aron::datanavigator::AronDictDataNavigatorPtr update1instace2(new aron::datanavigator::AronDictDataNavigator()); - aron::datanavigator::AronFloatDataNavigatorPtr update1instance2data1(new aron::datanavigator::AronFloatDataNavigator()); - update1instance2data1->setValue(0.123); - aron::datanavigator::AronStringDataNavigatorPtr update1instance2data2(new aron::datanavigator::AronStringDataNavigator()); - update1instance2data2->setValue("Lorem ipsum dolor sit amet :)"); - update1instace2->addElement("data1", update1instance2data1); - update1instace2->addElement("data2", update1instance2data2); - - update.entityID = armem::MemoryID::fromString("Memory/TestCoreSegment/TestProvider/test"); - update.instancesData = + providerSegment.aronType() = t; + + for (int i = 0; i < 25; ++i) { - update1instace1, - update1instace2 - }; - update.timeCreated = armem::Time::milliSeconds(1000); - BOOST_CHECK_NO_THROW(providerSegment.update(update)); - - BOOST_CHECK_EQUAL(providerSegment.entities.size(), 1); - BOOST_CHECK(providerSegment.hasEntity("test")); - BOOST_CHECK(!providerSegment.hasEntity("other_test")); - - armem::Entity& entity = providerSegment.getEntity("test"); - BOOST_CHECK_EQUAL(entity.name(), "test"); - BOOST_CHECK_EQUAL(entity.history.size(), 1); - BOOST_CHECK_EQUAL(entity.history.count(update.timeCreated), 1); - - armem::EntitySnapshot* entitySnapshot = entity.history.at(update.timeCreated).get(); - BOOST_CHECK_EQUAL(entitySnapshot->instances.size(), update.instancesData.size()); - - - // Another update (on memory). - aron::datanavigator::AronDictDataNavigatorPtr update2instace1(new aron::datanavigator::AronDictDataNavigator()); - aron::datanavigator::AronListDataNavigatorPtr update2instance1data1(new aron::datanavigator::AronListDataNavigator()); - aron::datanavigator::AronIntDataNavigatorPtr update2instance1data1data1(new aron::datanavigator::AronIntDataNavigator()); - update2instance1data1data1->setValue(1); - aron::datanavigator::AronIntDataNavigatorPtr update2instance1data1data2(new aron::datanavigator::AronIntDataNavigator()); - update2instance1data1data2->setValue(2); - aron::datanavigator::AronIntDataNavigatorPtr update2instance1data1data3(new aron::datanavigator::AronIntDataNavigator()); - update2instance1data1data3->setValue(3); - aron::datanavigator::AronIntDataNavigatorPtr update2instance1data1data4(new aron::datanavigator::AronIntDataNavigator()); - update2instance1data1data4->setValue(4); - update2instance1data1->addElement(update2instance1data1data1); - update2instance1data1->addElement(update2instance1data1data2); - update2instance1data1->addElement(update2instance1data1data3); - update2instance1data1->addElement(update2instance1data1data4); - update2instace1->addElement("data1", update2instance1data1); - - update.instancesData = { update2instace1 }; - update.timeCreated = armem::Time::milliSeconds(2000); - memory->update(update); - BOOST_CHECK_EQUAL(entity.history.size(), 2); - BOOST_CHECK_EQUAL(entity.history.count(update.timeCreated), 1); - BOOST_CHECK_EQUAL(entity.history.at(update.timeCreated)->instances.size(), update.instancesData.size()); - - - // A third update (on entity). - aron::datanavigator::AronDictDataNavigatorPtr update3instace1(new aron::datanavigator::AronDictDataNavigator()); - aron::datanavigator::AronIntDataNavigatorPtr update3instance1data1(new aron::datanavigator::AronIntDataNavigator()); - update3instance1data1->setValue(1001); - aron::datanavigator::AronIntDataNavigatorPtr update3instance1data2(new aron::datanavigator::AronIntDataNavigator()); - update3instance1data2->setValue(110); - update3instace1->addElement("data1", update3instance1data1); - update3instace1->addElement("data2", update3instance1data2); - - update.instancesData = { update3instace1 }; - update.timeCreated = armem::Time::milliSeconds(3000); - entity.update(update); - BOOST_CHECK_EQUAL(entity.history.size(), 3); + armem::InternalEntityUpdate update; + aron::datanavigator::AronDictDataNavigatorPtr m = aron::datanavigator::AronDictDataNavigator::DynamicCastAndCheck(r.generateAronDataFromType(t)); + update.entityID = armem::MemoryID::fromString(memoryName + "/TestCoreSegment/TestProvider/TestEntity"); + update.instancesData = + { + m + }; + update.timeCreated = armem::Time::now(); + BOOST_CHECK_NO_THROW(providerSegment.update(update)); + BOOST_CHECK_EQUAL(providerSegment.entities.size(), 1); + } + BOOST_CHECK(providerSegment.hasEntity("TestEntity")); // export memory - armem::ltm::LongTermMemoryLUT ltm("/tmp/MemoryExport/"); - ltm.writeOnDisk(memory); + std::string storagePath = "/tmp/MemoryExport"; // important! without tailing / + armem::ltm::MemoryFileSystemStorage ltm(storagePath, true); + ltm.writeOnDisk(*memory); + + std::string ltm_str = ltm.getInternalMemoryAsString(); + + armem::ltm::MemoryFileSystemStorage ltm2(storagePath); + ltm2.updateInternalMemoryFromFileSystem(); + + std::string ltm2_str = ltm2.getInternalMemoryAsString(); + + std::cout << "LTM1: " << std::endl; + std::cout << ltm_str << std::endl; + std::cout << "LTM2: " << std::endl; + std::cout << ltm2_str << std::endl; + BOOST_CHECK_EQUAL(ltm_str == ltm2_str, true); + + //std::map<std::string, armem::MemoryPtr> loaded_memories = ltm.getInternalMemoryAsRealMemory(); + //armem::MemoryPtr& memory2 = loaded_memories["Memory"]; - std::cout << ltm.getLUTasString() << std::endl; + //BOOST_CHECK_EQUAL(memory->equalsDeep(*memory2), true); } diff --git a/source/RobotAPI/libraries/aron/aroncore/AronConfig.h b/source/RobotAPI/libraries/aron/aroncore/AronConfig.h index a40e3cd54aba357fc66fed2534ae5833e5bed52a..aad8d12956613c317c9d1dcf2b8c382a33db96f1 100644 --- a/source/RobotAPI/libraries/aron/aroncore/AronConfig.h +++ b/source/RobotAPI/libraries/aron/aroncore/AronConfig.h @@ -225,3 +225,14 @@ namespace armarx::aron #define HANDLE_ALL_ARON_DATA_ARON_ABSTRACT_TYPE_CORRESPONDING \ HANDLE_CONTAINER_ARON_DATA_ARON_ABSTRACT_TYPE_CORRESPONDING \ HANDLE_COMPLEX_ARON_DATA_ARON_ABSTRACT_TYPE_CORRESPONDING + +/************************************ + * Related ************************** + ***********************************/ +#define HANDLE_ARON_PRIMITIVE_DATA_RELATED \ + RUN_ARON_MACRO(Int, int, INT, Long, long, LONG) \ + RUN_ARON_MACRO(Int, int, INT, Float, float, FLOAT) \ + RUN_ARON_MACRO(Int, int, INT, Double, double, DOUBLE) \ + RUN_ARON_MACRO(Long, long, LONG, Double, double, DOUBLE) \ + RUN_ARON_MACRO(Long, long, LONG, Float, float, FLOAT) \ + RUN_ARON_MACRO(Float, float, FLOAT, Double, double, DOUBLE) diff --git a/source/RobotAPI/libraries/aron/aroncore/AronDescriptor.h b/source/RobotAPI/libraries/aron/aroncore/AronDescriptor.h index b75037b120edd0c4ef9d4aeb1a6a80afe43675a7..04b547c2516e12f398376a02c1370f8bbe91cd0e 100644 --- a/source/RobotAPI/libraries/aron/aroncore/AronDescriptor.h +++ b/source/RobotAPI/libraries/aron/aroncore/AronDescriptor.h @@ -47,6 +47,7 @@ namespace armarx HANDLE_ALL_ARON_TYPES #undef RUN_ARON_MACRO + eAronUnknownType = -1 }; enum AronDataDescriptor @@ -56,6 +57,7 @@ namespace armarx HANDLE_ALL_ARON_DATA #undef RUN_ARON_MACRO + eAronUnknown = -1 }; const std::map<AronTypeDescriptor, std::string> AronTypeDescriptorToString = @@ -65,6 +67,7 @@ namespace armarx HANDLE_ALL_ARON_TYPES #undef RUN_ARON_MACRO + {eAronUnknownType, "eAronUnknownType"} }; const std::map<AronDataDescriptor, std::string> AronDataDescriptorToString = @@ -74,6 +77,7 @@ namespace armarx HANDLE_ALL_ARON_DATA #undef RUN_ARON_MACRO + {eAronUnknown, "eAronUnknown"} }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/AronRandomizer.h b/source/RobotAPI/libraries/aron/aroncore/AronRandomizer.h new file mode 100644 index 0000000000000000000000000000000000000000..166eb496abb99e28f5bb8f229e1d95a085f86bc1 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/AronRandomizer.h @@ -0,0 +1,349 @@ +/* + * This file is part of ArmarX. + * + * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), + * Karlsruhe Institute of Technology (KIT), all rights reserved. + * + * 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/>. + * + * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu) + * @copyright http://www.gnu.org/licenses/gpl-2.0.txt + * GNU General Public License + */ + +#pragma once + +// STD/STL +#include <memory> +#include <numeric> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronException.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronAllDataNavigators.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronAllTypeNavigators.h> + +namespace armarx +{ + namespace aron + { + class AronRandomizer + { + public: + AronRandomizer() + { + initialize_random(); + }; + + typenavigator::AronTypeNavigatorPtr generateRandomType(bool mustBeObject = false) const + { + AronTypeDescriptor nextType = eAronObjectType; + if (!mustBeObject) + { + std::vector<AronTypeDescriptor> descriptors = + { +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + eAron##upperType##Type, + + HANDLE_ALL_ARON_TYPES +#undef RUN_ARON_MACRO + }; + + int randomType = generateRandom(descriptors.size(), 0); + nextType = descriptors[randomType]; + } + + switch (nextType) + { + case eAronObjectType: + { + typenavigator::AronObjectTypeNavigatorPtr t = typenavigator::AronObjectTypeNavigatorPtr(new typenavigator::AronObjectTypeNavigator(AronPath())); + std::string objectName = generateRandomWord(); + t->setObjectName(objectName); + + std::vector<std::string> usedNames({objectName}); + int members = generateRandom(4, 2); + std::set<std::string> usedKeys; + for (int i = 0; i < members; ++i) + { + std::string key = generateRandomWord(usedKeys); + usedKeys.insert(key); + + typenavigator::AronTypeNavigatorPtr m = generateRandomType(false); + t->addAcceptedType(key, m); + } + return t; + } + case eAronDictType: + { + typenavigator::AronDictTypeNavigatorPtr t = typenavigator::AronDictTypeNavigatorPtr(new typenavigator::AronDictTypeNavigator(AronPath())); + typenavigator::AronTypeNavigatorPtr a = generateRandomType(false); + + t->setAcceptedType(a); + return t; + } + case eAronTupleType: + { + typenavigator::AronTupleTypeNavigatorPtr t = typenavigator::AronTupleTypeNavigatorPtr(new typenavigator::AronTupleTypeNavigator(AronPath())); + + int members = generateRandom(4, 2); + for (int i = 0; i < members; ++i) + { + typenavigator::AronTypeNavigatorPtr m = generateRandomType(false); + t->addAcceptedType(m); + } + return t; + } + case eAronListType: + { + typenavigator::AronListTypeNavigatorPtr t = typenavigator::AronListTypeNavigatorPtr(new typenavigator::AronListTypeNavigator(AronPath())); + typenavigator::AronTypeNavigatorPtr a = generateRandomType(false); + + t->setAcceptedType(a); + return t; + } +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigatorPtr(new typenavigator::Aron##upperType##TypeNavigator(AronPath())); \ + return t; \ +} + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + default: + { + throw armarx::aron::exception::AronTypeDescriptorNotValidException("AronTest", "generateRandomType", "No valid type found!", nextType); + } + } + } + + datanavigator::AronDataNavigatorPtr generateAronDataFromType(const typenavigator::AronTypeNavigatorPtr& type) const + { + const AronTypeDescriptor desc = type->getDescriptor(); + switch (desc) + { + // In an object, we do not want to edit the keys. + case eAronObjectType: + { + typenavigator::AronObjectTypeNavigatorPtr t = typenavigator::AronObjectTypeNavigator::DynamicCastAndCheck(type); + datanavigator::AronDictDataNavigatorPtr d = datanavigator::AronDictDataNavigatorPtr(new datanavigator::AronDictDataNavigator()); + for (const auto& [k, tt] : t->getAcceptedTypes()) + { + d->addElement(k, generateAronDataFromType(tt)); + } + return d; + } + + // here all totally random + case eAronDictType: + { + typenavigator::AronDictTypeNavigatorPtr t = typenavigator::AronDictTypeNavigator::DynamicCastAndCheck(type); + return datanavigator::AronDataNavigatorPtr(new datanavigator::AronDictDataNavigator()); + } + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCastAndCheck(type); \ + return datanavigator::AronDataNavigatorPtr(new datanavigator::AronListDataNavigator()); \ +} + + HANDLE_LIST_SERIALIZER_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCastAndCheck(type); \ + return datanavigator::AronDataNavigatorPtr(new datanavigator::AronNDArrayDataNavigator()); \ +} + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCastAndCheck(type); \ + return datanavigator::AronDataNavigatorPtr(new datanavigator::Aron##upperType##DataNavigator()); \ +} + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + default: + { + throw armarx::aron::exception::AronTypeDescriptorNotValidException("AronTest", "generateAronDataFromType", "No valid type found!", desc); + } + } + } + + + void initializeRandomly(datanavigator::AronDictDataNavigatorPtr& data, const typenavigator::AronObjectTypeNavigatorPtr& type) const + { + for (auto& [key, nextData] : data->getElements()) + { + initializeRandomly(nextData, type->getAcceptedTypes()[key]); + } + } + + void initializeRandomly(datanavigator::AronDictDataNavigatorPtr& data, const typenavigator::AronDictTypeNavigatorPtr& type) const + { + data->clear(); + int numElements = generateRandom(5, 1); + std::set<std::string> usedKeys; + for (int i = 0; i < numElements; ++i) + { + std::string key = generateRandomWord(usedKeys); + usedKeys.insert(key); + datanavigator::AronDataNavigatorPtr newData = generateAronDataFromType(type->getAcceptedType()); + initializeRandomly(newData, type->getAcceptedType()); + data->addElement(key, newData); + } + } + + void initializeRandomly(datanavigator::AronListDataNavigatorPtr& data, const typenavigator::AronTupleTypeNavigatorPtr& type) const + { + unsigned int i = 0; + for (auto& nextData : data->getElements()) + { + initializeRandomly(nextData, type->getAcceptedTypes()[i++]); + } + } + + void initializeRandomly(datanavigator::AronListDataNavigatorPtr& data, const typenavigator::AronListTypeNavigatorPtr& type) const + { + data->clear(); + int numElements = generateRandom(5, 1); + for (int i = 0; i < numElements; ++i) + { + datanavigator::AronDataNavigatorPtr newData = generateAronDataFromType(type->getAcceptedType()); + initializeRandomly(newData, type->getAcceptedType()); + data->addElement(newData); + } + } + + void initializeRandomly(datanavigator::AronNDArrayDataNavigatorPtr& data, const typenavigator::AronNDArraySerializerTypeNavigatorPtr& type) const + { + std::vector<int> dims = data->getDimensions(); + int bytes = std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>()); + std::vector<unsigned char> blob = generateRandomBlob(bytes); + data->setData(bytes, blob.data()); + } + + void initializeRandomly(datanavigator::AronIntDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) const + { + data->setValue(generateRandom(1000, -1000)); + } + + void initializeRandomly(datanavigator::AronLongDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) const + { + data->setValue(generateRandom(1000, -1000)); + } + + void initializeRandomly(datanavigator::AronFloatDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) const + { + data->setValue(generateRandom(1000, -1000)); + } + + void initializeRandomly(datanavigator::AronDoubleDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) const + { + data->setValue(generateRandom(1000, -1000)); + } + + void initializeRandomly(datanavigator::AronBoolDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) const + { + data->setValue(generateRandom(1, 0)); + } + + void initializeRandomly(datanavigator::AronStringDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) const + { + data->setValue(generateRandomWord()); + } + + void initializeRandomly(datanavigator::AronDataNavigatorPtr& data, const typenavigator::AronTypeNavigatorPtr& type) const + { + // Containers + AronTypeDescriptor desc = type->getDescriptor(); + switch (desc) + { + +#define RUN_ARON_MACRO(upperType, lowerType, capsType, upperData, lowerData, capsData) \ +case eAron##upperType##Type: \ +{ \ + typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCastAndCheck(type); \ + datanavigator::Aron##upperData##DataNavigatorPtr d = datanavigator::Aron##upperData##DataNavigator::DynamicCastAndCheck(data); \ + initializeRandomly(d, t); \ + break; \ +} + + HANDLE_ALL_CORRESPONDING +#undef RUN_ARON_MACRO + + default: + { + throw armarx::aron::exception::AronTypeDescriptorNotValidException("AronTest", "initializeRandomly", "No valid type found!", desc); + } + } + } + + int generateRandom(int max, int min) const + { + max += 1; + int random = (std::rand() % (max - min)) + min; + return random; + } + + std::string generateRandomWord(const std::set<std::string>& usedKeys = {}) const + { + std::vector<string> words = + { + "Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr" + "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et" + "dolore", "magna", "aliquyam", "eratsed" + }; + + int i = generateRandom(words.size() - 1, 0); + std::string key = words[i]; + + while (usedKeys.count(key) > 0) + { + key = generateRandomWord(usedKeys); + } + + return key; + } + + std::vector<unsigned char> generateRandomBlob(unsigned int size) const + { + std::vector<unsigned char> new_blob(size, 0); + for (unsigned int i = 0; i < size; ++i) + { + new_blob[i] = (generateRandom(127, 0)); + } + return new_blob; + } + + protected: + void initialize_random() const + { + std::srand(std::time(nullptr)); + } + }; + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/AronResolver.cpp b/source/RobotAPI/libraries/aron/aroncore/AronResolver.cpp index 8e9cb5b13e0c380127f8445d34e672a12d7298f0..ad82cfb7bcbaff9b8c60a545626895ca74cdd0e0 100644 --- a/source/RobotAPI/libraries/aron/aroncore/AronResolver.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/AronResolver.cpp @@ -31,7 +31,7 @@ namespace armarx { namespace aron { - AronTypeDescriptor AronResolver::GetTypeDescriptorForAronType(const type::AronTypePtr& a) + AronTypeDescriptor AronResolver::GetDescriptorForAronType(const type::AronTypePtr& a) { #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ { \ @@ -46,7 +46,7 @@ namespace armarx throw exception::AronTypeNotValidException("AronTypeResolver", "GetTypeForAronAbstractType", "Could not cast AronAbstractType to a valid type", a); } - AronTypeDescriptor AronResolver::GetTypeDescriptorForAronTypeId(const std::type_index& a) + AronTypeDescriptor AronResolver::GetDescriptorForAronTypeId(const std::type_index& a) { #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ { \ @@ -60,7 +60,7 @@ namespace armarx throw exception::AronException("AronTypeResolver", "GetTypeForAronTypeId", "Could not cast AronAbstractTypeId to a valid type. The typeid was " + simox::meta::get_type_name(a)); } - AronDataDescriptor AronResolver::GetTypeDescriptorForAronData(const data::AronDataPtr& a) + AronDataDescriptor AronResolver::GetDescriptorForAronData(const data::AronDataPtr& a) { #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ { \ @@ -75,7 +75,7 @@ namespace armarx throw exception::AronDataNotValidException("AronTypeResolver", "GetTypeForAronData", "Could not cast AronData to a valid type", a); } - AronDataDescriptor AronResolver::GetTypeDescriptorForAronDataId(const std::type_index& a) + AronDataDescriptor AronResolver::GetDescriptorForAronDataId(const std::type_index& a) { #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ { \ @@ -101,5 +101,77 @@ namespace armarx #undef RUN_ARON_MACRO return false; } + + AronDataDescriptor AronResolver::GetCorrespondingDataDescriptor(const AronTypeDescriptor& d) + { +#define RUN_ARON_MACRO(typeUpperType, typeLowerType, typeCapsType, dataUpperType, dataLowerType, dataCapsType) \ + { \ + if(d == eAron##typeUpperType##Type) \ + { \ + return eAron##dataUpperType; \ + } \ + } + HANDLE_ALL_CORRESPONDING +#undef RUN_ARON_MACRO + return eAronUnknown; + } + + bool AronResolver::DescriptorsAreRelated(const AronDataDescriptor& d, const AronDataDescriptor& d2) + { + if (d == d2) + { + return true; + } +#define RUN_ARON_MACRO(data1UpperType, data1LowerType, data1CapsType, data2UpperType, data2LowerType, data2CapsType) \ + if( \ + (d == AronDataDescriptor::eAron##data1UpperType && d2 == AronDataDescriptor::eAron##data2UpperType) || \ + (d2 == AronDataDescriptor::eAron##data1UpperType && d == AronDataDescriptor::eAron##data2UpperType) \ + ) \ + { \ + return true; \ + } + + HANDLE_ARON_PRIMITIVE_DATA_RELATED +#undef RUN_ARON_MACRO + return false; + } + + bool AronResolver::DescriptorsAreRelated(const AronTypeDescriptor& t, const AronTypeDescriptor& t2) + { + if (t == t2) + { + return true; + } +#define RUN_ARON_MACRO(type1UpperType, type1LowerType, type1CapsType, type2UpperType, type2LowerType, type2CapsType) \ + if( \ + (t == AronTypeDescriptor::eAron##type1UpperType##Type && t2 == AronTypeDescriptor::eAron##type2UpperType##Type) || \ + (t2 == AronTypeDescriptor::eAron##type1UpperType##Type && t == AronTypeDescriptor::eAron##type2UpperType##Type) \ + ) \ + { \ + return true; \ + } + + HANDLE_ARON_PRIMITIVE_DATA_RELATED +#undef RUN_ARON_MACRO + return false; + } + + AronDataDescriptor AronResolver::GetFirstIfDescriptorsAreRelated(const AronDataDescriptor& d, const AronDataDescriptor& d2) + { + if (DescriptorsAreRelated(d, d2)) + { + return d; + } + return d2; + } + + AronTypeDescriptor AronResolver::GetFirstIfDescriptorsAreRelated(const AronTypeDescriptor& t, const AronTypeDescriptor& t2) + { + if (DescriptorsAreRelated(t, t2)) + { + return t; + } + return t2; + } } } diff --git a/source/RobotAPI/libraries/aron/aroncore/AronResolver.h b/source/RobotAPI/libraries/aron/aroncore/AronResolver.h index b2764da2b9a4db63fca0953f8a1b37f462df3085..9bbdf6e15bcb98e5de63e5b93f49a8b0ba5ad481 100644 --- a/source/RobotAPI/libraries/aron/aroncore/AronResolver.h +++ b/source/RobotAPI/libraries/aron/aroncore/AronResolver.h @@ -41,18 +41,26 @@ namespace armarx class AronResolver { private: // disallow creating this object. static use only - AronResolver() = default; + AronResolver() = delete; public: - static AronTypeDescriptor GetTypeDescriptorForAronType(const type::AronTypePtr&); + static AronTypeDescriptor GetDescriptorForAronType(const type::AronTypePtr&); - static AronTypeDescriptor GetTypeDescriptorForAronTypeId(const std::type_index&); + static AronTypeDescriptor GetDescriptorForAronTypeId(const std::type_index&); - static AronDataDescriptor GetTypeDescriptorForAronData(const data::AronDataPtr&); + static AronDataDescriptor GetDescriptorForAronData(const data::AronDataPtr&); - static AronDataDescriptor GetTypeDescriptorForAronDataId(const std::type_index&); + static AronDataDescriptor GetDescriptorForAronDataId(const std::type_index&); static bool TypesAreCorresponding(const AronTypeDescriptor&, const AronDataDescriptor&); + + static AronDataDescriptor GetCorrespondingDataDescriptor(const AronTypeDescriptor&); + + static bool DescriptorsAreRelated(const AronDataDescriptor&, const AronDataDescriptor&); + static bool DescriptorsAreRelated(const AronTypeDescriptor&, const AronTypeDescriptor&); + + static AronDataDescriptor GetFirstIfDescriptorsAreRelated(const AronDataDescriptor&, const AronDataDescriptor&); + static AronTypeDescriptor GetFirstIfDescriptorsAreRelated(const AronTypeDescriptor&, const AronTypeDescriptor&); }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/CMakeLists.txt b/source/RobotAPI/libraries/aron/aroncore/CMakeLists.txt index 62e0379543ce0aa74f485c2997f16a380c916cf2..c512246f2793b22f8d59e078b5c54a22468a1fac 100644 --- a/source/RobotAPI/libraries/aron/aroncore/CMakeLists.txt +++ b/source/RobotAPI/libraries/aron/aroncore/CMakeLists.txt @@ -53,14 +53,21 @@ set(LIB_FILES io/AronDataIO/classWriters/NlohmannJSONWriter/AronDataNlohmannJSONWriter.cpp io/AronDataIO/classWriters/RapidXMLWriter/AronDataRapidXMLWriter.cpp + io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.cpp + io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.cpp + io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.cpp + io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.cpp io/AronTypeIO/classWriters/NavigatorWriter/AronTypeNavigatorWriter.cpp io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriter.cpp io/AronTypeIO/classWriters/RapidXMLWriter/AronTypeRapidXMLWriter.cpp - io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.cpp - io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.cpp + io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.cpp + io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.cpp + + io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.cpp + io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.cpp codegenerator/codeWriters/cppWriter/AronCppWriter.cpp codegenerator/codeWriters/cppWriter/typeSerializers/AronTypeCppSerializer.cpp @@ -86,6 +93,7 @@ set(LIB_HEADERS AronConcepts.h AronDescriptor.h AronResolver.h + AronRandomizer.h AronException.h AronFactory.h AronPath.h @@ -116,15 +124,12 @@ set(LIB_HEADERS navigators/visitors/AronDataVisitor.h - io/AronWriter.h - io/AronReader.h - io/AronClassWriter.h - io/AronClassReader.h - io/AronTextWriter.h - io/AronTextReader.h + io/AronReaderWriter.h + io/AronReaderWriterToken.h io/AronWriterToken.h io/AronReaderToken.h + io/AronDataIO/AronDataReaderWriter.h io/AronDataIO/AronDataWriter.h io/AronDataIO/classWriters/AronDataClassWriter.h io/AronDataIO/classWriters/AronDataClassWriterToken.h @@ -143,10 +148,10 @@ set(LIB_HEADERS io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReaderToken.h io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.h - io/AronDataIO/textWriters/AronDataTextWriter.h - io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.h + io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.h + io/AronTypeIO/AronTypeReaderWriter.h io/AronTypeIO/AronTypeWriter.h io/AronTypeIO/classWriters/AronTypeClassWriter.h io/AronTypeIO/classWriters/AronTypeClassWriterToken.h @@ -158,8 +163,15 @@ set(LIB_HEADERS io/AronTypeIO/classWriters/RapidXMLWriter/AronTypeRapidXMLWriterToken.h io/AronTypeIO/AronTypeReader.h - - io/AronTypeIO/textWriters/AronTypeTextWriter.h + io/AronTypeIO/classReaders/AronTypeClassReader.h + io/AronTypeIO/classReaders/AronTypeClassReaderToken.h + io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.h + io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReaderToken.h + io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.h + io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReaderToken.h + + io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.h + io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.h codegenerator/AronCppClass.h codegenerator/AronWriterInfo.h diff --git a/source/RobotAPI/libraries/aron/aroncore/codegenerator/AronCppClass.h b/source/RobotAPI/libraries/aron/aroncore/codegenerator/AronCppClass.h index 7f8f233af8d72ce03079f7d76c703c4042298542..61d72f288131240ebd45103cf39b72939128726f 100644 --- a/source/RobotAPI/libraries/aron/aroncore/codegenerator/AronCppClass.h +++ b/source/RobotAPI/libraries/aron/aroncore/codegenerator/AronCppClass.h @@ -33,7 +33,6 @@ #include <RobotAPI/libraries/aron/aroncore/AronConcepts.h> #include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReader.h> #include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataWriter.h> -#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReader.h> #include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeWriter.h> diff --git a/source/RobotAPI/libraries/aron/aroncore/codegenerator/codeWriters/cppWriter/typeSerializers/AronTypeCppSerializerFactory.cpp b/source/RobotAPI/libraries/aron/aroncore/codegenerator/codeWriters/cppWriter/typeSerializers/AronTypeCppSerializerFactory.cpp index b613400462b0f8acb938c81d2319221d79943a3b..9bc980d9fc8816a2c4a40f6fb1a6cb4f1bf6eefb 100644 --- a/source/RobotAPI/libraries/aron/aroncore/codegenerator/codeWriters/cppWriter/typeSerializers/AronTypeCppSerializerFactory.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/codegenerator/codeWriters/cppWriter/typeSerializers/AronTypeCppSerializerFactory.cpp @@ -54,7 +54,7 @@ namespace armarx CheckIfInputIsNull("AronDataNavigatorFactory", "create", path, n); auto res = n->getResult(); - auto factory_iterator = FACTORIES.find(AronResolver::GetTypeDescriptorForAronType(res)); + auto factory_iterator = FACTORIES.find(AronResolver::GetDescriptorForAronType(res)); if (factory_iterator == FACTORIES.end()) { throw exception::AronExceptionWithPathInfo("AronDataNavigatorFactory", "create", "Cannot find the desired factory for input (ice_id): " + res->ice_id() + ". Cannot create navigator", path); diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReader.h index 9d5652744805a7fe490ade7fbb1e8ebf60e55f7d..38b9c9123ed1dc858dfc6bd2970a4a8e72f8aa0e 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReader.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReader.h @@ -25,11 +25,11 @@ #include <string> // BaseClass -#include <RobotAPI/libraries/aron/aroncore/io/AronReader.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReaderWriter.h> // ArmarX #include <RobotAPI/libraries/aron/aroncore/AronConfig.h> - +#include <RobotAPI/libraries/aron/aroncore/AronDescriptor.h> namespace armarx { @@ -41,7 +41,7 @@ namespace armarx typedef std::shared_ptr<AronDataReader> AronDataReaderPtr; class AronDataReader : - public AronReader + public AronDataReaderWriter { public: AronDataReader() = default; @@ -55,7 +55,7 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ virtual std::pair<std::vector<int>, std::string> readStart##upperType() = 0; \ - virtual bool readEnd##upperType(unsigned char*) = 0; \ + virtual void readEnd##upperType(unsigned char*) = 0; \ HANDLE_COMPLEX_DATA #undef RUN_ARON_MACRO @@ -65,6 +65,7 @@ namespace armarx HANDLE_PRIMITIVE_TYPES #undef RUN_ARON_MACRO + template<class T> T readPrimitive() { @@ -75,6 +76,9 @@ namespace armarx virtual std::string readKey() = 0; virtual bool readKey(const std::string&) = 0; + + // Helper functions + virtual AronDataDescriptor getTypeOfNextAron(const AronTypeDescriptor) const = 0; }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReaderWriter.h similarity index 66% rename from source/RobotAPI/libraries/aron/aroncore/io/AronWriter.h rename to source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReaderWriter.h index f097a8c77f01b2893ad5994f73b572ab67127aee..c240203e487d27f0fcf9967f22e0b41e5b755556 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReaderWriter.h @@ -37,13 +37,18 @@ namespace armarx { namespace io { - class AronWriter; - typedef std::shared_ptr<AronWriter> AronWriterPtr; + class AronDataReaderWriter; + typedef std::shared_ptr<AronDataReaderWriter> AronDataReaderWriterPtr; - class AronWriter + class AronDataReaderWriter { public: - AronWriter() = default; + AronDataReaderWriter() = default; + + protected: + const std::string ARON_DATA_READER_WRITER_NDARRAY_DIMENSIONS_SLUG = "ARON_NDARRAY_DIMESIONS"; + const std::string ARON_DATA_READER_WRITER_NDARRAY_TYPE_SLUG = "ARON_NDARRAY_TYPE"; + const std::string ARON_DATA_READER_WRITER_NDARRAY_DATA_SLUG = "ARON_NDARRAY_DATA"; }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataWriter.h index 4d5ec291bc96b588e2d6e9fac3f29e04c72b666c..c7d204840e15a42e18d05b7387eaa7d56c11e171 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataWriter.h @@ -25,7 +25,7 @@ #include <string> // BaseClass -#include <RobotAPI/libraries/aron/aroncore/io/AronWriter.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReaderWriter.h> // ArmarX #include <ArmarXCore/core/exceptions/Exception.h> @@ -44,7 +44,7 @@ namespace armarx typedef std::shared_ptr<AronDataWriter> AronDataWriterPtr; class AronDataWriter : - public AronWriter + public AronDataReaderWriter { public: AronDataWriter() = default; diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90d4359dc98689650283b8b19fc5d82f19ddef44 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.cpp @@ -0,0 +1,107 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +// STD/STL +#include <memory> +#include <numeric> + +// Header +#include "LegacyAronDataConverter.h" + + + + +namespace armarx +{ + namespace aron + { + namespace io + { + void LegacyAronDataConverter::ConvertFromReader(AronDataReader& reader, AronDataWriter& writer, const aron::typenavigator::AronTypeNavigatorPtr& expectedStructure) + { + AronDataDescriptor desc = reader.getTypeOfNextAron(expectedStructure != nullptr ? expectedStructure->getDescriptor() : eAronUnknownType); + switch (desc) + { + case eAronDict: + { + int elements = reader.readStartDict(); + writer.writeStartDict(); + + for(int i = 0; i < elements; ++i) + { + std::string key = reader.readKey(); + writer.writeKey(key); + LegacyAronDataConverter::ConvertFromReader(reader, writer); + } + + writer.writeEndDict(); + reader.readEndDict(); + + break; + } + case eAronList: + { + int elements = reader.readStartList(); + writer.writeStartList(); + + for(int i = 0; i < elements; ++i) + { + LegacyAronDataConverter::ConvertFromReader(reader, writer); + } + + writer.writeEndList(); + reader.readEndList(); + break; + } +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType: \ +{ \ + auto [dims, type] = reader.readStart##upperType(); \ + int elements = std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>()); \ + std::vector<unsigned char> data(elements); \ + reader.readEnd##upperType(data.data()); \ + \ + writer.write##upperType(dims, type, data.data()); \ + break; \ +} + + HANDLE_COMPLEX_DATA +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType: \ +{ \ + lowerType val = reader.readPrimitive<lowerType>(); \ + writer.writePrimitive(val); \ + break; \ +} + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + + default: + { + throw exception::AronDataDescriptorNotValidException("LegacyAronDataReader", "SetupReaderAndGetResult", "Data-Type could not be resolved", desc); + } + } + } + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/textWriters/AronDataTextWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.h similarity index 56% rename from source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/textWriters/AronDataTextWriter.h rename to source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.h index eba653345ed20572e05dc9d6456f6c763f2da7d8..b3b38e3e35371c026deafa59324e4c1d11ab1e56 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/textWriters/AronDataTextWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataConverter/LegacyAronDataConverter.h @@ -22,12 +22,18 @@ // STD/STL #include <memory> -#include <stack> -#include <sstream> +#include <string> + +// BaseClass +#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataReader.h> // ArmarX -#include <RobotAPI/libraries/aron/aroncore/io/AronTextWriter.h> -#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/AronDataWriter.h> +#include <ArmarXCore/core/exceptions/Exception.h> + +#include <RobotAPI/interface/aron.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigator.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NavigatorWriter/AronDataNavigatorWriter.h> + namespace armarx { @@ -35,22 +41,18 @@ namespace armarx { namespace io { - class AronDataTextWriter; - typedef std::shared_ptr<AronDataTextWriter> AronDataTextWriterPtr; + class LegacyAronDataConverter; + typedef std::shared_ptr<LegacyAronDataConverter> LegacyAronDataReaderPtr; - class AronDataTextWriter : - virtual public AronTextWriter, - virtual public AronDataWriter + class LegacyAronDataConverter { - protected: - enum OpenedTag - { - eOpenDict, - eOpenList - }; - - protected: - std::stack<std::pair<OpenedTag, int>> openedTagsStack; + private: + LegacyAronDataConverter() = delete; + + public: + static void ConvertFromReader(AronDataReader&, AronDataWriter&, const aron::typenavigator::AronTypeNavigatorPtr& expectedStructure = nullptr); + + public: }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.cpp index 13ae8a8ca8297df6f1a15c644caebbd906b9377b..d4201a143b30dd525aada69c1cbca28c091ad1ac 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.cpp @@ -37,9 +37,14 @@ namespace armarx namespace io { + void LegacyAronDataWriter::SetupWriterFromAronDataPtr(AronDataWriter& writer, const datanavigator::AronDataNavigatorPtr& aron) + { + SetupWriterFromAronDataPtr(writer, aron->getResult()); + } + void LegacyAronDataWriter::SetupWriterFromAronDataPtr(AronDataWriter& writer, const data::AronDataPtr& aron) { - AronDataDescriptor desc = AronResolver::GetTypeDescriptorForAronData(aron); + AronDataDescriptor desc = AronResolver::GetDescriptorForAronData(aron); switch (desc) { case eAronDict: diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.h index 7d96ef4015547419e5e11a9bb8405ef66630d19b..5bb3e3166c3cbce6738a8a6e7080be7e2de3ca69 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/LegacyAronDataWriter/LegacyAronDataWriter.h @@ -31,7 +31,7 @@ #include <ArmarXCore/core/exceptions/Exception.h> #include <RobotAPI/interface/aron.h> - +#include <RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigator.h> namespace armarx @@ -50,6 +50,7 @@ namespace armarx LegacyAronDataWriter() = delete; public: + static void SetupWriterFromAronDataPtr(AronDataWriter&, const datanavigator::AronDataNavigatorPtr&); static void SetupWriterFromAronDataPtr(AronDataWriter&, const data::AronDataPtr&); public: diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReader.h index d1f00246f9873a4805e506297e36d543d87d6062..16fdc8c3712e756e4b65ff8d767e863eb27c56e9 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReader.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReader.h @@ -54,20 +54,32 @@ namespace armarx } // ReadKey - virtual std::string readKey() override + std::string readKey() override { TokenPtrTypename token = stack.top(); std::string k = token->resolveCurrentKey(); return k; } - virtual bool readKey(const std::string& k) override + bool readKey(const std::string& k) override { TokenPtrTypename token = stack.top(); token->setCurrentKey(k); return true; } + AronDataDescriptor getTypeOfNextAron(const AronTypeDescriptor hint) const override + { + if (stack.empty()) + { + // completely unknown. We assume a dict on the top level + return eAronDict; + } + + TokenPtrTypename lastToken = stack.top(); + return lastToken->getTypeOfNextElement(hint); + } + protected: InputTypename data; diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReaderToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReaderToken.h index 11daabc82189526f25850bf577777b32d0c194fc..472551a1d2e5ae0cfffefd75645c24cb35599ed4 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReaderToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/AronDataClassReaderToken.h @@ -59,27 +59,16 @@ namespace armarx AronReaderToken<AronDataDescriptor, ElementReturnPtrTypename, ElementAddTypename>(desc, data) { } - virtual std::string resolveCurrentKey() const override + bool currentIsEligableForResolvingKey() const override { - if (this->descriptor != AronDataDescriptor::eAronDict) + if (this->descriptor == AronDataDescriptor::eAronDict) { - throw exception::AronDataDescriptorNotValidException("AronDataClassReaderToken", "resolveCurrentKey", "Cant get key of a non-dict token", this->descriptor); + return true; } - - // ugly workaround - if (this->currentKey == "") // probably in a dict where the caller doues not know which keys are there - { - if (this->allKeys.size() == 0) - { - throw exception::AronException("AronDataClassReaderToken", "resolveCurrentKey", "The currentKey is not set and the list of all keys is empty!"); - } - return this->allKeys[this->currentIndex]; - } - return this->currentKey; + return false; } - virtual ElementReturnPtrTypename getElementAndIncreaseCnt() override = 0; - + virtual AronDataDescriptor getTypeOfNextElement(const AronTypeDescriptor) const = 0; private: }; } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.cpp index b37dde676c4bd036a5b4d0347a1f3272906cac4d..36e59e623fd1a669b73f2d7291d6dfd68828e279 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.cpp @@ -50,21 +50,32 @@ namespace armarx { } - datanavigator::AronDataNavigatorPtr AronDataNavigatorReader::getNextNavigator() + datanavigator::AronDataNavigatorPtr AronDataNavigatorReader::getCurrentAndGoToNext() { if (!readInitialStart) { readInitialStart = true; return data; } - AronDataNavigatorReaderTokenPtr lastToken = stack.top(); - return lastToken->getElementAndIncreaseCnt(); + auto lastToken = stack.top(); + return lastToken->getCurrentElementAndIncreaseCnt(); + } + + datanavigator::AronDataNavigatorPtr AronDataNavigatorReader::getCurrent() + { + if (!readInitialStart) + { + readInitialStart = true; + return data; + } + auto lastToken = stack.top(); + return lastToken->getCurrentElement(); } #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ int AronDataNavigatorReader::readStart##upperType() \ { \ - datanavigator::AronDataNavigatorPtr current_nav = getNextNavigator(); \ + datanavigator::AronDataNavigatorPtr current_nav = getCurrentAndGoToNext(); \ auto desc = current_nav->getDescriptor(); \ if (desc != AronDataDescriptor::eAron##upperType) \ { \ @@ -103,22 +114,21 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ std::pair<std::vector<int>, std::string> AronDataNavigatorReader::readStart##upperType() \ { \ - datanavigator::AronDataNavigatorPtr nav = getNextNavigator(); \ + datanavigator::AronDataNavigatorPtr nav = getCurrent(); \ datanavigator::AronNDArrayDataNavigatorPtr casted = datanavigator::AronNDArrayDataNavigator::DynamicCast(nav); \ return std::make_pair(casted->getDimensions(), casted->getType()); \ } \ \ - bool AronDataNavigatorReader::readEnd##upperType(unsigned char* data) \ + void AronDataNavigatorReader::readEnd##upperType(unsigned char* data) \ { \ if(data == NULL) \ { \ throw exception::AronException("AronDataNavigatorReader", "readEnd" + std::string(#upperType), "The corresponding data pointer of an complex aron ptr is NULL."); \ } \ - datanavigator::AronDataNavigatorPtr nav = getNextNavigator(); \ + datanavigator::AronDataNavigatorPtr nav = getCurrentAndGoToNext(); \ datanavigator::AronNDArrayDataNavigatorPtr casted = datanavigator::AronNDArrayDataNavigator::DynamicCast(nav); \ std::vector<int> dims = casted->getDimensions(); \ memcpy(data, casted->getData(), std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>())); \ - return true; \ } HANDLE_COMPLEX_DATA @@ -128,8 +138,7 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ void AronDataNavigatorReader::readPrimitive(lowerType& t) \ { \ - AronDataNavigatorReaderTokenPtr token = stack.top(); \ - datanavigator::AronDataNavigatorPtr nav = token->getElementAndIncreaseCnt(); \ + datanavigator::AronDataNavigatorPtr nav = getCurrentAndGoToNext(); \ datanavigator::Aron##upperType##DataNavigatorPtr casted = datanavigator::Aron##upperType##DataNavigator::DynamicCast(nav); \ t = casted->getValue(); \ } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.h index e6edac332832c30c706648fe29ff1eecb095e53d..80ae71d9955f8375a745d78c03084d4277bde873 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.h @@ -31,6 +31,7 @@ #include <RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigator.h> #include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReaderToken.h> + namespace armarx { namespace aron @@ -61,7 +62,7 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ virtual std::pair<std::vector<int>, std::string> readStart##upperType() override; \ - virtual bool readEnd##upperType(unsigned char*) override; \ + virtual void readEnd##upperType(unsigned char*) override; \ HANDLE_COMPLEX_DATA #undef RUN_ARON_MACRO @@ -73,7 +74,8 @@ namespace armarx #undef RUN_ARON_MACRO private: - datanavigator::AronDataNavigatorPtr getNextNavigator(); + datanavigator::AronDataNavigatorPtr getCurrent(); + datanavigator::AronDataNavigatorPtr getCurrentAndGoToNext(); private: }; diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReaderToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReaderToken.h index be68ba2a5d427da87052481610f77228d90e29c8..3dd510087246330e2a23e3668502f0eb99d09286 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReaderToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReaderToken.h @@ -58,12 +58,13 @@ namespace armarx { case eAronDict: { - allKeys = datanavigator::AronDictDataNavigator::DynamicCast(data)->getAllKeys(); + allKeys = datanavigator::AronDictDataNavigator::DynamicCastAndCheck(data)->getAllKeys(); + maxIndex = allKeys.size(); break; } case eAronList: { - maxIndex = datanavigator::AronListDataNavigator::DynamicCast(data)->childrenSize(); + maxIndex = datanavigator::AronListDataNavigator::DynamicCastAndCheck(data)->childrenSize(); break; } default: @@ -71,26 +72,30 @@ namespace armarx } } - virtual datanavigator::AronDataNavigatorPtr getElementAndIncreaseCnt() override + AronDataDescriptor getTypeOfNextElement(const AronTypeDescriptor) const override + { + const datanavigator::AronDataNavigatorPtr next = getCurrentElement(); + return next->getDescriptor(); + } + + datanavigator::AronDataNavigatorPtr getCurrentElement() const override { switch (descriptor) { case eAronDict: { - datanavigator::AronDictDataNavigatorPtr casted = datanavigator::AronDictDataNavigator::DynamicCast(data); + datanavigator::AronDictDataNavigatorPtr casted = datanavigator::AronDictDataNavigator::DynamicCast(element); datanavigator::AronDataNavigatorPtr ret = casted->getElement(resolveCurrentKey()); - currentIndex++; return ret; } case eAronList: { - datanavigator::AronListDataNavigatorPtr casted = datanavigator::AronListDataNavigator::DynamicCast(data); + datanavigator::AronListDataNavigatorPtr casted = datanavigator::AronListDataNavigator::DynamicCast(element); datanavigator::AronDataNavigatorPtr ret = casted->getElement(currentIndex); - currentIndex++; return ret; } default: - throw exception::AronDataDescriptorNotValidException("AronDataNavigatorReaderToken", "getElementAndIncreaseCnt", "Could not resove a type of a navigator. Allowed are only container and complex types due to performance", descriptor); + throw exception::AronDataDescriptorNotValidException("AronDataNavigatorReaderToken", "getNextElement", "Could not resove a type of a navigator. Allowed are only container types due to performance", descriptor); } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.cpp index 105361bc47d16974dbc2216416b382169b2de7e9..9ed694579537a17152bd63a8af48b82c06b3dd91 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.cpp @@ -48,7 +48,7 @@ namespace armarx { } - nlohmann::json AronDataNlohmannJSONReader::getNextJson() + nlohmann::json AronDataNlohmannJSONReader::getCurrentAndGoToNext() { if (!readInitialStart) { @@ -56,13 +56,24 @@ namespace armarx return data; } AronDataNlohmannJSONReaderTokenPtr lastToken = stack.top(); - return lastToken->getElementAndIncreaseCnt(); + return lastToken->getCurrentElementAndIncreaseCnt(); + } + + nlohmann::json AronDataNlohmannJSONReader::getCurrent() + { + if (!readInitialStart) + { + readInitialStart = true; + return data; + } + AronDataNlohmannJSONReaderTokenPtr lastToken = stack.top(); + return lastToken->getCurrentElement(); } #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ int AronDataNlohmannJSONReader::readStart##upperType() \ { \ - nlohmann::json current_json = getNextJson(); \ + nlohmann::json current_json = getCurrentAndGoToNext(); \ int c = current_json.size(); \ AronDataNlohmannJSONReaderTokenPtr newToken = AronDataNlohmannJSONReaderTokenPtr(new AronDataNlohmannJSONReaderToken(eAron##upperType, current_json)); \ stack.push(newToken); \ @@ -94,23 +105,22 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ std::pair<std::vector<int>, std::string> AronDataNlohmannJSONReader::readStart##upperType() \ { \ - nlohmann::json j = getNextJson(); \ - std::vector<int> dims = j["ARON_NDARRAY_DIMENSIONS"]; \ - std::string type = j["ARON_NDARRAY_TYPE"]; \ + nlohmann::json j = getCurrent(); \ + std::vector<int> dims = j[ARON_DATA_READER_WRITER_NDARRAY_DIMENSIONS_SLUG]; \ + std::string type = j[ARON_DATA_READER_WRITER_NDARRAY_TYPE_SLUG]; \ return std::make_pair(dims, type); \ } \ \ - bool AronDataNlohmannJSONReader::readEnd##upperType(unsigned char* data) \ + void AronDataNlohmannJSONReader::readEnd##upperType(unsigned char* data) \ { \ if(data == NULL) \ { \ throw exception::AronException("AronDataNlohmannJSONReader", "readEnd" + std::string(#upperType), "The corresponding data pointer of an complex aron ptr is NULL."); \ } \ - nlohmann::json j = getNextJson(); \ - std::vector<int> dims = j["ARON_NDARRAY_DIMENSIONS"]; \ - std::vector<unsigned char> d = j["ARON_NDARRAY_DATA"]; \ + nlohmann::json j = getCurrentAndGoToNext(); \ + std::vector<int> dims = j[ARON_DATA_READER_WRITER_NDARRAY_DIMENSIONS_SLUG]; \ + std::vector<unsigned char> d = j[ARON_DATA_READER_WRITER_NDARRAY_DATA_SLUG]; \ memcpy(data, d.data(), std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>())); \ - return true; \ } HANDLE_COMPLEX_DATA @@ -120,8 +130,7 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ void AronDataNlohmannJSONReader::readPrimitive(lowerType& t) \ { \ - AronDataNlohmannJSONReaderTokenPtr token = stack.top(); \ - nlohmann::json j = token->getElementAndIncreaseCnt(); \ + nlohmann::json j = getCurrentAndGoToNext(); \ t = j; \ } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.h index f279ddd1aaf40a0afe480feaf3fa05789ba97c89..7cfee40ab0ed2c5ce6da420c826be04876c2dea6 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReader.h @@ -60,7 +60,7 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ virtual std::pair<std::vector<int>, std::string> readStart##upperType() override; \ - virtual bool readEnd##upperType(unsigned char*) override; \ + virtual void readEnd##upperType(unsigned char*) override; \ HANDLE_COMPLEX_DATA #undef RUN_ARON_MACRO @@ -72,7 +72,8 @@ namespace armarx #undef RUN_ARON_MACRO private: - nlohmann::json getNextJson(); + nlohmann::json getCurrent(); + nlohmann::json getCurrentAndGoToNext(); private: }; diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReaderToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReaderToken.h index 3ee8d3c541761c333adf090007a3549bbd8752bf..175d2d9d61e5564e8251579237217716a6ebd82b 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReaderToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NlohmannJSONReader/AronDataNlohmannJSONReaderToken.h @@ -32,6 +32,7 @@ // ArmarX #include <RobotAPI/libraries/aron/aroncore/AronConfig.h> +#include <RobotAPI/libraries/aron/aroncore/AronResolver.h> namespace armarx { @@ -64,6 +65,7 @@ namespace armarx { allKeys.push_back(it.key()); } + maxIndex = allKeys.size(); break; } case eAronList: @@ -76,24 +78,61 @@ namespace armarx } } - virtual nlohmann::json getElementAndIncreaseCnt() override + AronDataDescriptor getTypeOfNextElement(const AronTypeDescriptor hint) const override + { + const nlohmann::json next = getCurrentElement(); + if (next.is_object()) + { + // Check if specific NDArray key exists + if (next.find(ARON_READER_WRITER_TOKEN_NDARRAY_DATA_SLUG) != next.end()) + { + return eAronNDArray; + } + return eAronDict; + } + if (next.is_array()) + { + return eAronList; + } + if (next.is_number_integer()) + { + return AronResolver::GetFirstIfDescriptorsAreRelated( + AronResolver::GetCorrespondingDataDescriptor(hint), + eAronLong); + } + if (next.is_number_float()) + { + return AronResolver::GetFirstIfDescriptorsAreRelated( + AronResolver::GetCorrespondingDataDescriptor(hint), + eAronDouble); + } + if (next.is_boolean()) + { + return eAronBool; + } + if (next.is_string()) + { + return eAronString; + } + throw exception::AronException("AronDataNlohmannJSONReaderToken", "getTypeOfNextElement", "Could not determine the type of an nlohmann::json object. Could not convert to AronDataDescriptor enum."); + } + + nlohmann::json getCurrentElement() const override { switch (descriptor) { case eAronDict: { - nlohmann::json ret = data[resolveCurrentKey()]; - currentIndex++; + nlohmann::json ret = element[resolveCurrentKey()]; return ret; } case eAronList: { - nlohmann::json ret = data[currentIndex]; - currentIndex++; + nlohmann::json ret = element[currentIndex]; return ret; } default: - throw exception::AronDataDescriptorNotValidException("AronDataNlohmannJSONReaderToken", "getElementAndIncreaseCnt", "Could not resove a type of a navigator. Allowed are only container and complex types due to performance", descriptor); + throw exception::AronDataDescriptorNotValidException("AronDataNlohmannJSONReaderToken", "getNextElement", "Could not resove a type of a navigator. Allowed are only container types due to performance", descriptor); } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NavigatorWriter/AronDataNavigatorWriterToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NavigatorWriter/AronDataNavigatorWriterToken.h index 5c19455997000d2f3d48e9ac98c6f185b406bace..4ec86cfe30b7068ab1d7a00d8441e64deae609f5 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NavigatorWriter/AronDataNavigatorWriterToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NavigatorWriter/AronDataNavigatorWriterToken.h @@ -64,13 +64,13 @@ namespace armarx { case eAronDict: { - datanavigator::AronDictDataNavigatorPtr casted = datanavigator::AronDictDataNavigator::DynamicCast(element); + datanavigator::AronDictDataNavigatorPtr casted = datanavigator::AronDictDataNavigator::DynamicCastAndCheck(element); casted->addElement(currentKey, n); break; } case eAronList: { - datanavigator::AronListDataNavigatorPtr casted = datanavigator::AronListDataNavigator::DynamicCast(element); + datanavigator::AronListDataNavigatorPtr casted = datanavigator::AronListDataNavigator::DynamicCastAndCheck(element); casted->addElement(n); currentIndex++; break; diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NlohmannJSONWriter/AronDataNlohmannJSONWriter.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NlohmannJSONWriter/AronDataNlohmannJSONWriter.cpp index ccf86759e115e6af3c6894baf90fda75e6f5830e..f3e54dbba44d39b157a4bff869a6a4ad38bf84a5 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NlohmannJSONWriter/AronDataNlohmannJSONWriter.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NlohmannJSONWriter/AronDataNlohmannJSONWriter.cpp @@ -64,12 +64,12 @@ namespace armarx { \ AronDataNlohmannJSONWriterTokenPtr token = stack.top(); \ nlohmann::json j; \ - j["ARON_NDARRAY_DIMENSIONS"] = dims; \ - j["ARON_NDARRAY_TYPE"] = t; \ + j[ARON_DATA_READER_WRITER_NDARRAY_DIMENSIONS_SLUG] = dims; \ + j[ARON_DATA_READER_WRITER_NDARRAY_TYPE_SLUG] = t; \ int elements = std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>()); \ std::vector<unsigned char> d = std::vector<unsigned char>(elements); \ memcpy(d.data(), data, elements); \ - j["ARON_NDARRAY_DATA"] = d; \ + j[ARON_DATA_READER_WRITER_NDARRAY_DATA_SLUG] = d; \ token->addElement(j); \ return true; \ } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronReaderToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronReaderToken.h index 39ebce21cacdd9eded7c72c8f2e693f350780a0a..b6887e34f9f8a1a2bcbfb44e3c620c3e6fe49c83 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronReaderToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronReaderToken.h @@ -24,10 +24,13 @@ #include <memory> #include <string> +// Base class +#include <RobotAPI/libraries/aron/aroncore/io/AronReaderWriterToken.h> + // ArmarX #include <RobotAPI/libraries/aron/aroncore/AronConfig.h> #include <RobotAPI/libraries/aron/aroncore/AronException.h> - +#include <RobotAPI/libraries/aron/aroncore/AronResolver.h> namespace armarx @@ -43,26 +46,27 @@ namespace armarx using AronReaderTokenPtr = std::shared_ptr<AronReaderToken<AronDescriptorTypename, ElementReturnPtrTypename, ElementTypename>> ; template<typename AronDescriptorTypename, typename ElementReturnPtrTypename, typename ElementTypename> - class AronReaderToken + class AronReaderToken : + public AronReaderWriterToken { public: AronReaderToken() = delete; AronReaderToken(const AronDescriptorTypename& desc, const ElementTypename& e) : - descriptor(desc), data(e), currentIndex(0), maxIndex(0), currentKey("") + descriptor(desc), element(e), currentIndex(0), maxIndex(0), currentKey("") { }; - virtual bool checkedAllChildren() const + bool checkedAllChildren() const { return currentIndex >= allKeys.size() && currentIndex >= maxIndex; } - virtual AronDescriptorTypename getDescriptor() const + AronDescriptorTypename getDescriptor() const { return descriptor; } - virtual void setCurrentKey(const std::string& s) + void setCurrentKey(const std::string& s) { if (s.empty()) { @@ -71,18 +75,47 @@ namespace armarx currentKey = s; } - virtual std::string resolveCurrentKey() const = 0; + virtual bool currentIsEligableForResolvingKey() const = 0; + std::string resolveCurrentKey() const + { + if (!this->currentIsEligableForResolvingKey()) + { + throw exception::AronException("AronReaderToken", "resolveCurrentKey", "Cant get key of token."); + } + + // check outOfBounds + if (this->currentIndex >= this->maxIndex) + { + throw exception::AronSizeNotValidExceptionException("AronReaderToken", "resolveCurrentKey", "Index out of bounds.", this->currentIndex, this->maxIndex); + } + + // ugly workaround + if (this->currentKey == "") // probably in a dict where the caller does not know which keys are there + { + if (this->allKeys.size() == 0) + { + throw exception::AronException("AronReaderToken", "resolveCurrentKey", "The currentKey is not set and the list of all keys is empty!"); + } + return this->allKeys[this->currentIndex]; + } + return this->currentKey; + } + - virtual ElementReturnPtrTypename getElementAndIncreaseCnt() = 0; + virtual ElementReturnPtrTypename getCurrentElement() const = 0; + ElementReturnPtrTypename getCurrentElementAndIncreaseCnt() + { + ElementReturnPtrTypename next = getCurrentElement(); + this->currentIndex++; + return next; + } protected: // members AronDescriptorTypename descriptor; - ElementTypename data; + ElementTypename element; unsigned int currentIndex; - - // list members unsigned int maxIndex; // dict and object members diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronClassReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronReaderWriter.h similarity index 53% rename from source/RobotAPI/libraries/aron/aroncore/io/AronClassReader.h rename to source/RobotAPI/libraries/aron/aroncore/io/AronReaderWriter.h index 636ffefefb24bc7e48c13ca9e2c7394fcb4bdd29..df0104fc12f05cda59c098d0e772335894ca3589 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronClassReader.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronReaderWriter.h @@ -24,8 +24,11 @@ #include <memory> #include <string> -// Base Class -#include <RobotAPI/libraries/aron/aroncore/io/AronReader.h> +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronException.h> + +#include <RobotAPI/interface/aron.h> + namespace armarx @@ -34,25 +37,24 @@ namespace armarx { namespace io { - template <typename T> - class AronClassReader; - - template <typename T> - using AronClassReaderPtr = std::shared_ptr<AronClassReader<T>>; + class AronTypeConverterWriter; + typedef std::shared_ptr<AronTypeConverterWriter> AronTypeConverterWriterPtr; - template <typename T> - class AronClassReader : - virtual public AronReader + class AronTypeConverterWriter { public: - AronClassReader(const T& input) : - data(input) - { - } - + AronTypeConverterWriter() = default; protected: - T data; + const std::string ARON_TYPE_READER_WRITER_NDARRAY_DIMENSIONS_SLUG = "ARON_NDARRAY_DIMESIONS"; + const std::string ARON_TYPE_READER_WRITER_NDARRAY_TYPE_SLUG = "ARON_NDARRAY_TYPE"; + const std::string ARON_TYPE_READER_WRITER_NDARRAY_DATA_SLUG = "ARON_NDARRAY_DATA"; + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + const std::string ARON_TYPE_READER_WRITER_##capsType##_TYPENAME = "ARON_" + std::string(#capsType) + "_TYPE"; + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronReaderWriterToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronReaderWriterToken.h new file mode 100644 index 0000000000000000000000000000000000000000..cb17f130376e100f46be680c86603e045e607ece --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronReaderWriterToken.h @@ -0,0 +1,59 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> + +// ArmarX +#include <ArmarXCore/core/exceptions/Exception.h> + +#include <RobotAPI/libraries/aron/aroncore/AronConfig.h> + + + +namespace armarx +{ + namespace aron + { + namespace io + { + class AronReaderWriterToken; + typedef std::shared_ptr<AronReaderWriterToken> AronReaderWriterTokenPtr; + + class AronReaderWriterToken + { + public: + AronReaderWriterToken() = default; + + protected: + const std::string ARON_READER_WRITER_TOKEN_OBJECT_NAME_SLUG = "ARON_OBJECT_NAME"; + const std::string ARON_READER_WRITER_TOKEN_DICT_ACCEPTED_TYPE_SLUG = "ARON_DICT_ACCEPTED_TYPE"; + const std::string ARON_READER_WRITER_TOKEN_LIST_ACCEPTED_TYPE_SLUG = "ARON_LIST_ACCEPTED_TYPE"; + + const std::string ARON_READER_WRITER_TOKEN_NDARRAY_DIMENSIONS_SLUG = "ARON_NDARRAY_DIMESIONS"; + const std::string ARON_READER_WRITER_TOKEN_NDARRAY_TYPE_SLUG = "ARON_NDARRAY_TYPE"; + const std::string ARON_READER_WRITER_TOKEN_NDARRAY_DATA_SLUG = "ARON_NDARRAY_DATA"; + }; + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTextReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTextReader.h deleted file mode 100644 index 0b2aa76f1503762dba03c4505a3ad18735cec905..0000000000000000000000000000000000000000 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTextReader.h +++ /dev/null @@ -1,47 +0,0 @@ -/* -* 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/>. -* -* @author Fabian Peller (fabian dot peller at kit dot edu) -* @copyright http://www.gnu.org/licenses/gpl-2.0.txt -* GNU General Public License -*/ - -#pragma once - -// STD/STL -#include <memory> -#include <stack> -#include <sstream> - -// ArmarX -#include <RobotAPI/libraries/aron/aroncore/io/AronClassReader.h> - -namespace armarx -{ - namespace aron - { - namespace io - { - class AronTextReader; - typedef std::shared_ptr<AronTextReader> AronTextReaderPtr; - - class AronTextReader : - virtual public AronClassReader<std::string> - { - public: - - } - } - } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTextWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTextWriter.h deleted file mode 100644 index 75baa7a4065fef0ea3bf4f25536b73760cff2803..0000000000000000000000000000000000000000 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTextWriter.h +++ /dev/null @@ -1,135 +0,0 @@ -/* -* 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/>. -* -* @author Fabian Peller (fabian dot peller at kit dot edu) -* @copyright http://www.gnu.org/licenses/gpl-2.0.txt -* GNU General Public License -*/ - -#pragma once - -// STD/STL -#include <memory> -#include <stack> -#include <sstream> - -// Simox -#include <SimoxUtility/algorithm/string.h> -#include <SimoxUtility/meta/type_name.h> - -// ArmarX -#include <RobotAPI/libraries/aron/aroncore/io/AronClassWriter.h> - -namespace armarx -{ - namespace aron - { - namespace io - { - class AronTextWriter; - typedef std::shared_ptr<AronTextWriter> AronTextWriterPtr; - - class AronTextWriter : - virtual public AronClassWriter<std::string> - { - public: - AronTextWriter() : - level(0), - indentChars(" ") - { - - } - - AronTextWriter(int level, const std::string& indentChars) : - level(level), - indentChars(indentChars) - { - - } - - virtual std::string getResult() const override - { - return ss.str(); - } - - protected: - void increaseIndent() - { - level++; - } - - void decreaseIndent() - { - if (level == 0) - { - return; - } - level--; - } - - void writeNewLine() - { - ss << "\n"; - indent(); - } - - void writeNewLineWithIndentIncrease() - { - ss << "\n"; - increaseIndent(); - indent(); - } - - void writeNewLineWithIndentDecrease() - { - ss << "\n"; - decreaseIndent(); - indent(); - } - - void indent() - { - for (unsigned int i = 0; i < level; ++i) - { - ss << indentChars; - } - } - - bool removeLastOccurenceOf(const std::string& occ) - { - std::string s = ss.str(); - bool contains = simox::alg::contains(s, occ); - s = simox::alg::replace_last(s, occ, ""); - ss = std::stringstream(); - ss << s; - return contains; - } - - void trimLastWhitespaces() - { - std::string s = ss.str(); - s = simox::alg::trim_copy(s); - ss = std::stringstream(); - ss << s; - } - - protected: - std::stringstream ss; - unsigned int level; - std::string indentChars; - }; - } - } -} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReader.h index 378113708011c61cc9e8867297eec27d252f2fac..5d8034c983c76a5c2b32de7e9b92d2a0e5b17b5e 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReader.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReader.h @@ -24,11 +24,13 @@ #include <memory> #include <string> +// BaseClass +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReaderWriter.h> + // ArmarX #include <ArmarXCore/core/exceptions/Exception.h> - #include <RobotAPI/libraries/aron/aroncore/AronConfig.h> - +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/AronTypeClassWriter.h> namespace armarx @@ -40,31 +42,37 @@ namespace armarx class AronTypeReader; typedef std::shared_ptr<AronTypeReader> AronTypeReaderPtr; - class AronTypeReader + class AronTypeReader : + public AronTypeReaderWriter { public: AronTypeReader() = default; #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - virtual bool readStart##upperType##Type() = 0; \ + virtual int readStart##upperType##Type() = 0; \ virtual bool readEnd##upperType##Type() = 0; \ HANDLE_CONTAINER_TYPES #undef RUN_ARON_MACRO #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - virtual unsigned char* read##upperType##Type() = 0; \ + virtual std::pair<std::vector<int>, std::string> read##upperType##Type() = 0; \ HANDLE_COMPLEX_TYPES #undef RUN_ARON_MACRO #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - virtual lowerType read##upperType##Type() = 0; \ + virtual void read##upperType##Type() = 0; \ HANDLE_PRIMITIVE_TYPES #undef RUN_ARON_MACRO virtual std::string readKey() = 0; + virtual bool readKey(const std::string&) = 0; + virtual std::string readObjectName() = 0; + + // Helper functions + virtual AronTypeDescriptor getTypeOfNextAron() const = 0; }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronClassWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReaderWriter.h similarity index 54% rename from source/RobotAPI/libraries/aron/aroncore/io/AronClassWriter.h rename to source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReaderWriter.h index bf2a54056dc4fba2ab7e73f07c225d30753a2de5..a5fb9663b8c37fe9827ec2f8f872db0f3e5ef8f6 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronClassWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReaderWriter.h @@ -25,7 +25,10 @@ #include <string> // ArmarX -#include <RobotAPI/libraries/aron/aroncore/io/AronWriter.h> +#include <RobotAPI/libraries/aron/aroncore/AronException.h> + +#include <RobotAPI/interface/aron.h> + namespace armarx @@ -34,22 +37,24 @@ namespace armarx { namespace io { - template <typename T> - class AronClassWriter; - - template <typename T> - using AronClassWriterPtr = std::shared_ptr<AronClassWriter<T>>; + class AronTypeReaderWriter; + typedef std::shared_ptr<AronTypeReaderWriter> AronTypeReaderWriterPtr; - template <typename T> - class AronClassWriter : - virtual public AronWriter + class AronTypeReaderWriter { public: - AronClassWriter() = default; - - virtual T getResult() const = 0; + AronTypeReaderWriter() = default; protected: + const std::string ARON_TYPE_READER_WRITER_NDARRAY_DIMENSIONS_SLUG = "ARON_NDARRAY_DIMESIONS"; + const std::string ARON_TYPE_READER_WRITER_NDARRAY_TYPE_SLUG = "ARON_NDARRAY_TYPE"; + const std::string ARON_TYPE_READER_WRITER_NDARRAY_DATA_SLUG = "ARON_NDARRAY_DATA"; + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + const std::string ARON_TYPE_READER_WRITER_##capsType##_TYPENAME = "ARON_" + std::string(#capsType) + "_TYPE"; + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeWriter.h index 26fabcb0eee5c141230379b3097b4720fc4b5e86..1a9f64515ac3c1631bc527c1d3f1d1296b7b2852 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeWriter.h @@ -24,6 +24,9 @@ #include <memory> #include <string> +// BaseClass +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReaderWriter.h> + // ArmarX #include <RobotAPI/libraries/aron/aroncore/AronException.h> #include <RobotAPI/interface/aron.h> @@ -40,7 +43,8 @@ namespace armarx class AronTypeWriter; typedef std::shared_ptr<AronTypeWriter> AronTypeWriterPtr; - class AronTypeWriter + class AronTypeWriter : + public AronTypeReaderWriter { public: AronTypeWriter() = default; diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cba697d877adde64d86ea8c64a8db76e4226f893 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.cpp @@ -0,0 +1,127 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +// STD/STL +#include <memory> +#include <numeric> + +// Header +#include "LegacyAronTypeConverter.h" + + + + +namespace armarx +{ + namespace aron + { + namespace io + { + void LegacyAronTypeConverter::ConvertFromReader(AronTypeReader& reader, AronTypeWriter& writer) + { + AronTypeDescriptor desc = reader.getTypeOfNextAron(); + switch (desc) + { + case eAronObjectType: + { + const int elements = reader.readStartObjectType(); + const std::string name = reader.readObjectName(); + + writer.writeStartObjectType(); + writer.writeObjectName(name); + + for(int i = 0; i < elements; ++i) + { + std::string key = reader.readKey(); + writer.writeKey(key); + LegacyAronTypeConverter::ConvertFromReader(reader, writer); + } + + writer.writeEndObjectType(); + reader.readEndObjectType(); + break; + } + case eAronDictType: + { + reader.readStartDictType(); + writer.writeStartDictType(); + + LegacyAronTypeConverter::ConvertFromReader(reader, writer); + + writer.writeEndDictType(); + reader.readEndDictType(); + break; + } + case eAronTupleType: + { + int elements = reader.readStartTupleType(); + writer.writeStartTupleType(); + + for(int i = 0; i < elements; ++i) + { + LegacyAronTypeConverter::ConvertFromReader(reader, writer); + } + + writer.writeEndTupleType(); + reader.readEndTupleType(); + break; + } + case eAronListType: + { + reader.readStartListType(); + writer.writeStartListType(); + + LegacyAronTypeConverter::ConvertFromReader(reader, writer); + + writer.writeEndListType(); + reader.readEndListType(); + break; + } +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + auto [dims, type] = reader.read##upperType##Type(); \ + writer.write##upperType##Type(dims, type); \ + break; \ +} + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + reader.read##upperType##Type(); \ + writer.write##upperType##Type(); \ + break; \ +} + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + + default: + { + throw exception::AronTypeDescriptorNotValidException("LegacyAronTypeReader", "SetupReaderAndGetResult", "Type-Type could not be resolved", desc); + } + } + } + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.h similarity index 60% rename from source/RobotAPI/libraries/aron/aroncore/io/AronReader.h rename to source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.h index cbcc1c4dfef60e8bd7fbb02100d5566096fb4130..e58739ac5e3edece43cedea7007ae8dfc1c15445 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronReader.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeConverter/LegacyAronTypeConverter.h @@ -24,11 +24,15 @@ #include <memory> #include <string> +// BaseClass +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReader.h> + // ArmarX #include <ArmarXCore/core/exceptions/Exception.h> -#include <RobotAPI/libraries/aron/aroncore/AronConfig.h> - +#include <RobotAPI/interface/aron.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NavigatorWriter/AronTypeNavigatorWriter.h> namespace armarx @@ -37,13 +41,18 @@ namespace armarx { namespace io { - class AronReader; - typedef std::shared_ptr<AronReader> AronReaderPtr; + class LegacyAronTypeConverter; + typedef std::shared_ptr<LegacyAronTypeConverter> LegacyAronTypeReaderPtr; - class AronReader + class LegacyAronTypeConverter { + private: + LegacyAronTypeConverter() = delete; + + public: + static void ConvertFromReader(AronTypeReader&, AronTypeWriter&); + public: - AronReader() = default; }; } } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..efd5a1218294f047b0b2d31bd8cb088f4749dbc0 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.cpp @@ -0,0 +1,121 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +// STD/STL +#include <memory> +#include <numeric> + +// Header +#include "LegacyAronTypeWriter.h" + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronResolver.h> + + + +namespace armarx +{ + namespace aron + { + namespace io + { + + void LegacyAronTypeWriter::SetupWriterFromAronTypePtr(AronTypeWriter& writer, const typenavigator::AronTypeNavigatorPtr& aron) + { + SetupWriterFromAronTypePtr(writer, aron->getResult()); + } + + void LegacyAronTypeWriter::SetupWriterFromAronTypePtr(AronTypeWriter& writer, const type::AronTypePtr& aron) + { + AronTypeDescriptor desc = AronResolver::GetDescriptorForAronType(aron); + switch (desc) + { + case eAronObjectType: + { + type::AronObjectTypePtr casted = type::AronObjectTypePtr::dynamicCast(aron); + writer.writeStartObjectType(); + writer.writeObjectName(casted->objectName); + for (const auto& [key, value] : casted->elementTypes) + { + writer.writeKey(key); + LegacyAronTypeWriter::SetupWriterFromAronTypePtr(writer, value); + } + writer.writeEndObjectType(); + break; + } + case eAronDictType: + { + type::AronDictTypePtr casted = type::AronDictTypePtr::dynamicCast(aron); + writer.writeStartDictType(); + LegacyAronTypeWriter::SetupWriterFromAronTypePtr(writer, casted->acceptedType); + writer.writeEndDictType(); + break; + } + case eAronTupleType: + { + type::AronTupleTypePtr casted = type::AronTupleTypePtr::dynamicCast(aron); + writer.writeStartTupleType(); + for (const auto& value : casted->elementTypes) + { + LegacyAronTypeWriter::SetupWriterFromAronTypePtr(writer, value); + } + writer.writeEndTupleType(); + break; + } + case eAronListType: + { + type::AronListTypePtr casted = type::AronListTypePtr::dynamicCast(aron); + writer.writeStartListType(); + LegacyAronTypeWriter::SetupWriterFromAronTypePtr(writer, casted->acceptedType); + writer.writeEndListType(); + break; + } +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + type::Aron##upperType##TypePtr casted = type::Aron##upperType##TypePtr::dynamicCast(aron); \ + writer.write##upperType##Type(casted->dimensions, casted->typeName); \ + break; \ +} + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ +case eAron##upperType##Type: \ +{ \ + type::Aron##upperType##TypePtr casted = type::Aron##upperType##TypePtr::dynamicCast(aron); \ + writer.write##upperType##Type(); \ + break; \ +} + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + + default: + { + throw exception::AronTypeDescriptorNotValidException("LegacyAronTypeWriter", "SetupWriterFromAronTypePtr", "Type-Type could not be resolved. The ice_id of the input was: " + aron->ice_id(), desc); + } + } + } + + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/textWriters/AronTypeTextWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.h similarity index 61% rename from source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/textWriters/AronTypeTextWriter.h rename to source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.h index 91f556659c326063994ad5dd344936544ca835fa..d1a29bddadc2752be0564388db7989e7629e098c 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/textWriters/AronTypeTextWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/LegacyAronTypeWriter/LegacyAronTypeWriter.h @@ -22,26 +22,37 @@ // STD/STL #include <memory> -#include <stack> -#include <sstream> +#include <string> -// ArmarX -#include <RobotAPI/libraries/aron/aroncore/io/AronTextWriter.h> +// BaseClass #include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeWriter.h> +// ArmarX +#include <ArmarXCore/core/exceptions/Exception.h> + +#include <RobotAPI/interface/aron.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.h> + + namespace armarx { namespace aron { namespace io { - class AronTypeTextWriter; - typedef std::shared_ptr<AronTypeTextWriter> AronTypeTextWriterPtr; + class LegacyAronTypeWriter; + typedef std::shared_ptr<LegacyAronTypeWriter> LegacyAronTypeWriterPtr; - class AronTypeTextWriter : - virtual public AronTextWriter, + class LegacyAronTypeWriter : virtual public AronTypeWriter { + private: + LegacyAronTypeWriter() = delete; + + public: + static void SetupWriterFromAronTypePtr(AronTypeWriter&, const typenavigator::AronTypeNavigatorPtr&); + static void SetupWriterFromAronTypePtr(AronTypeWriter&, const type::AronTypePtr&); + public: }; } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReader.h new file mode 100644 index 0000000000000000000000000000000000000000..2c011b1bbeb572ba4f3045e2d44a888405949cb5 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReader.h @@ -0,0 +1,97 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> + +// BaseClass +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/AronTypeReader.h> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronConfig.h> +#include <RobotAPI/libraries/aron/aroncore/AronDescriptor.h> + +namespace armarx +{ + namespace aron + { + namespace io + { + template <typename InputTypename, typename TokenPtrTypename> + class AronTypeClassReader; + + template <typename InputTypename, typename TokenPtrTypename> + using AronTypeClassReaderPtr = std::shared_ptr<AronTypeClassReader<InputTypename, TokenPtrTypename>> ; + + template <typename InputTypename, typename TokenPtrTypename> + class AronTypeClassReader : + public AronTypeReader + { + public: + AronTypeClassReader() = delete; + AronTypeClassReader(const InputTypename& input) : + input(input) + { + } + + // ReadKey + std::string readKey() override + { + TokenPtrTypename token = stack.top(); + std::string k = token->resolveCurrentKey(); + return k; + } + + bool readKey(const std::string& k) override + { + TokenPtrTypename token = stack.top(); + token->setCurrentKey(k); + return true; + } + + std::string readObjectName() override + { + TokenPtrTypename token = stack.top(); + return token->getObjectName(); + } + + virtual AronTypeDescriptor getTypeOfNextAron() const override + { + if (stack.empty()) + { + // completely unknown. We assume a object on the top level + return eAronObjectType; + } + + TokenPtrTypename lastToken = stack.top(); + return lastToken->getTypeOfNextElement(); + } + + protected: + InputTypename input; + bool readInitialStart = false; + std::stack<TokenPtrTypename> stack; + }; + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReaderToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReaderToken.h new file mode 100644 index 0000000000000000000000000000000000000000..4d4879c82cd6e7cbde4426be8fbedcdce2b4de43 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReaderToken.h @@ -0,0 +1,78 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> + +// Base Class +#include <RobotAPI/libraries/aron/aroncore/io/AronReaderToken.h> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronConfig.h> +#include <RobotAPI/libraries/aron/aroncore/AronDescriptor.h> + + + +namespace armarx +{ + namespace aron + { + namespace io + { + template<typename ElementReturnPtrTypename, typename ElementAddTypename> + class AronTypeClassReaderToken; + + template<typename ElementReturnPtrTypename, typename ElementAddTypename> + using AronTypeClassReaderTokenPtr = std::shared_ptr<AronTypeClassReaderToken<ElementReturnPtrTypename, ElementAddTypename>>; + + template<typename ElementReturnPtrTypename, typename ElementAddTypename> + class AronTypeClassReaderToken : + virtual public AronReaderToken<AronTypeDescriptor, ElementReturnPtrTypename, ElementAddTypename> + { + public: + using PointerType = AronTypeClassReaderTokenPtr<ElementReturnPtrTypename, ElementAddTypename>; + + public: + // constructors + AronTypeClassReaderToken() = delete; + AronTypeClassReaderToken(const AronTypeDescriptor desc, const ElementAddTypename& type) : + AronReaderToken<AronTypeDescriptor, ElementReturnPtrTypename, ElementAddTypename>(desc, type) + { } + + bool currentIsEligableForResolvingKey() const override + { + if (this->descriptor == AronTypeDescriptor::eAronObjectType) + { + return true; + } + return false; + } + + virtual std::string getObjectName() const = 0; + virtual AronTypeDescriptor getTypeOfNextElement() const = 0; + + private: + }; + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.cpp new file mode 100644 index 0000000000000000000000000000000000000000..efa16685f8ae197c1e73e43f020a680b7595ff71 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.cpp @@ -0,0 +1,137 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +// STD/STL +#include <memory> +#include <numeric> + +// Header +#include "AronTypeNavigatorReader.h" + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronException.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.h> + + + +namespace armarx +{ + namespace aron + { + namespace io + { + AronTypeNavigatorReader::AronTypeNavigatorReader(const typenavigator::AronContainerTypeNavigatorPtr& n) : + AronTypeClassReader<typenavigator::AronContainerTypeNavigatorPtr, AronTypeNavigatorReaderTokenPtr>(n) + { + } + + AronTypeNavigatorReader::AronTypeNavigatorReader(const type::AronContainerTypePtr& n) : + AronTypeClassReader<typenavigator::AronContainerTypeNavigatorPtr, AronTypeNavigatorReaderTokenPtr>( + typenavigator::AronContainerTypeNavigator::DynamicCast(typenavigator::AronTypeNavigator::FromAronType(n)) + ) + { + } + + typenavigator::AronTypeNavigatorPtr AronTypeNavigatorReader::getCurrentAndGoToNext() + { + if (!readInitialStart) + { + readInitialStart = true; + return input; + } + AronTypeNavigatorReaderTokenPtr lastToken = stack.top(); + return lastToken->getCurrentElementAndIncreaseCnt(); + } + + typenavigator::AronTypeNavigatorPtr AronTypeNavigatorReader::getCurrent() + { + if (!readInitialStart) + { + readInitialStart = true; + return input; + } + AronTypeNavigatorReaderTokenPtr lastToken = stack.top(); + return lastToken->getCurrentElement(); + } + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + int AronTypeNavigatorReader::readStart##upperType##Type() \ + { \ + typenavigator::AronTypeNavigatorPtr current_nav = getCurrentAndGoToNext(); \ + auto desc = current_nav->getDescriptor(); \ + if (desc != AronTypeDescriptor::eAron##upperType##Type) \ + { \ + throw exception::AronTypeDescriptorNotValidException("AronTypeNavigatorReader", "readStart" + std::string(#upperType), "A token in the stack has the wrong type. Expected " + std::string(#upperType), desc); \ + return false; \ + } \ + typenavigator::Aron##upperType##TypeNavigatorPtr current_nav_casted = typenavigator::Aron##upperType##TypeNavigator::DynamicCast(current_nav); \ + int c = current_nav->childrenSize(); \ + AronTypeNavigatorReaderTokenPtr newToken = AronTypeNavigatorReaderTokenPtr(new AronTypeNavigatorReaderToken(current_nav->getDescriptor(), current_nav_casted)); \ + stack.push(newToken); \ + return c; \ + } \ + \ + bool AronTypeNavigatorReader::readEnd##upperType##Type() \ + { \ + AronTypeNavigatorReaderTokenPtr token = stack.top(); \ + auto desc = token->getDescriptor(); \ + if (desc != AronTypeDescriptor::eAron##upperType##Type) \ + { \ + throw exception::AronTypeDescriptorNotValidException("AronTypeNavigatorReader", "readEnd" + std::string(#upperType), "A token in the stack has the wrong type. Expected " + std::string(#upperType), desc); \ + return false; \ + } \ + \ + if (token->checkedAllChildren()) \ + { \ + stack.pop(); \ + return true; \ + } \ + return false; \ + } + + HANDLE_CONTAINER_TYPES +#undef RUN_ARON_MACRO + + // Complex Type +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + std::pair<std::vector<int>, std::string> AronTypeNavigatorReader::read##upperType##Type() \ + { \ + typenavigator::AronTypeNavigatorPtr nav = getCurrentAndGoToNext(); \ + typenavigator::Aron##upperType##TypeNavigatorPtr casted = typenavigator::Aron##upperType##TypeNavigator::DynamicCastAndCheck(nav); \ + return std::make_pair(casted->getDimensions(), casted->getUsedType()); \ + } + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + + // Read primitives +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + void AronTypeNavigatorReader::read##upperType##Type() \ + { \ + typenavigator::AronTypeNavigatorPtr nav = getCurrentAndGoToNext(); \ + typenavigator::Aron##upperType##TypeNavigatorPtr casted = typenavigator::Aron##upperType##TypeNavigator::DynamicCastAndCheck(nav); \ + } + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.h new file mode 100644 index 0000000000000000000000000000000000000000..29788d39f0f4c73265bb86bdbe0abed33cf635a7 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReader.h @@ -0,0 +1,83 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <stack> + +// BaseClass +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReader.h> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReaderToken.h> + + +namespace armarx +{ + namespace aron + { + namespace io + { + class AronTypeNavigatorReader; + typedef std::shared_ptr<AronTypeNavigatorReader> AronTypeNavigatorReaderPtr; + + class AronTypeNavigatorReader : + virtual public AronTypeClassReader<typenavigator::AronContainerTypeNavigatorPtr, AronTypeNavigatorReaderTokenPtr> + { + public: + using PointerType = AronTypeNavigatorReaderPtr; + + public: + // constructors + AronTypeNavigatorReader() = delete; + AronTypeNavigatorReader(const typenavigator::AronContainerTypeNavigatorPtr& n); + AronTypeNavigatorReader(const type::AronContainerTypePtr& n); + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + virtual int readStart##upperType##Type() override; \ + virtual bool readEnd##upperType##Type() override; \ + + HANDLE_CONTAINER_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + virtual std::pair<std::vector<int>, std::string> read##upperType##Type() override; \ + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + virtual void read##upperType##Type() override; \ + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + + private: + typenavigator::AronTypeNavigatorPtr getCurrentAndGoToNext(); + typenavigator::AronTypeNavigatorPtr getCurrent(); + + private: + }; + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReaderToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReaderToken.h new file mode 100644 index 0000000000000000000000000000000000000000..12cca75857b599f5338040e8cca6fe5770ed0076 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NavigatorReader/AronTypeNavigatorReaderToken.h @@ -0,0 +1,138 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> + +// Base Class +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReaderToken.h> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronConfig.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronAllTypeNavigators.h> + +namespace armarx +{ + namespace aron + { + namespace io + { + // BaseClass + class AronTypeNavigatorReaderToken; + typedef std::shared_ptr<AronTypeNavigatorReaderToken> AronTypeNavigatorReaderTokenPtr; + + class AronTypeNavigatorReaderToken : + virtual public AronTypeClassReaderToken<typenavigator::AronTypeNavigatorPtr, typenavigator::AronContainerTypeNavigatorPtr> + { + public: + using PointerType = AronTypeNavigatorReaderTokenPtr; + + public: + // constructors + AronTypeNavigatorReaderToken() = delete; + AronTypeNavigatorReaderToken(const AronTypeDescriptor desc, const typenavigator::AronContainerTypeNavigatorPtr& type) : + AronReaderToken<AronTypeDescriptor, typenavigator::AronTypeNavigatorPtr, typenavigator::AronContainerTypeNavigatorPtr>(desc, type), + AronTypeClassReaderToken<typenavigator::AronTypeNavigatorPtr, typenavigator::AronContainerTypeNavigatorPtr>(desc, type) + { + switch (descriptor) + { + case eAronObjectType: + { + allKeys = typenavigator::AronObjectTypeNavigator::DynamicCast(element)->getAllKeys(); + maxIndex = allKeys.size(); + break; + } + case eAronDictType: + { + maxIndex = 1; + break; + } + case eAronTupleType: + { + maxIndex = typenavigator::AronTupleTypeNavigator::DynamicCast(element)->childrenSize(); + break; + } + case eAronListType: + { + maxIndex = 1; + break; + } + default: + throw exception::AronTypeDescriptorNotValidException("AronTypeNavigatorReaderToken", "AronTypeNavigatorReaderToken", "Received an invalid instance for a reader token.", desc); + } + } + + std::string getObjectName() const override + { + if(descriptor == eAronObjectType) + { + const typenavigator::AronObjectTypeNavigatorPtr next = typenavigator::AronObjectTypeNavigator::DynamicCastAndCheck(getCurrentElement()); + return next->getObjectName(); + } + throw exception::AronTypeDescriptorNotValidException("AronTypeNavigatorReaderToken", "AronTypeNavigatorReaderToken", "Could not get object nme of a non-object token", descriptor); + } + + AronTypeDescriptor getTypeOfNextElement() const override + { + const typenavigator::AronTypeNavigatorPtr next = getCurrentElement(); + return next->getDescriptor(); + } + + typenavigator::AronTypeNavigatorPtr getCurrentElement() const override + { + switch (descriptor) + { + case eAronObjectType: + { + typenavigator::AronObjectTypeNavigatorPtr casted = typenavigator::AronObjectTypeNavigator::DynamicCastAndCheck(element); + typenavigator::AronTypeNavigatorPtr ret = casted->getAcceptedType(resolveCurrentKey()); + return ret; + } + case eAronDictType: + { + typenavigator::AronDictTypeNavigatorPtr casted = typenavigator::AronDictTypeNavigator::DynamicCastAndCheck(element); + typenavigator::AronTypeNavigatorPtr ret = casted->getAcceptedType(); + return ret; + } + case eAronTupleType: + { + typenavigator::AronTupleTypeNavigatorPtr casted = typenavigator::AronTupleTypeNavigator::DynamicCastAndCheck(element); + typenavigator::AronTypeNavigatorPtr ret = casted->getAcceptedType(currentIndex); + return ret; + } + case eAronListType: + { + typenavigator::AronListTypeNavigatorPtr casted = typenavigator::AronListTypeNavigator::DynamicCastAndCheck(element); + typenavigator::AronTypeNavigatorPtr ret = casted->getAcceptedType(); + return ret; + } + default: + throw exception::AronTypeDescriptorNotValidException("AronTypeNavigatorReaderToken", "getNextElement", "Could not resove a type of a navigator. Allowed are only container types due to performance", descriptor); + } + } + + private: + }; + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afc3784b9925f52d5d1f845bbfeff730d6d2e909 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.cpp @@ -0,0 +1,128 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +// STD/STL +#include <memory> +#include <numeric> + +// Header +#include "AronTypeNlohmannJSONReader.h" + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronException.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.h> +#include <RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.h> + + + +namespace armarx +{ + namespace aron + { + namespace io + { + AronTypeNlohmannJSONReader::AronTypeNlohmannJSONReader(const nlohmann::json& n) : + AronTypeClassReader<nlohmann::json, AronTypeNlohmannJSONReaderTokenPtr>(n) + { + } + + AronTypeNlohmannJSONReader::AronTypeNlohmannJSONReader(const std::string& n) : + AronTypeClassReader<nlohmann::json, AronTypeNlohmannJSONReaderTokenPtr>(nlohmann::json::parse(n)) + { + } + + nlohmann::json AronTypeNlohmannJSONReader::getCurrentAndGoToNext() + { + if (!readInitialStart) + { + readInitialStart = true; + return input; + } + AronTypeNlohmannJSONReaderTokenPtr lastToken = stack.top(); + return lastToken->getCurrentElementAndIncreaseCnt(); + } + + nlohmann::json AronTypeNlohmannJSONReader::getCurrent() + { + if (!readInitialStart) + { + readInitialStart = true; + return input; + } + AronTypeNlohmannJSONReaderTokenPtr lastToken = stack.top(); + return lastToken->getCurrentElement(); + } + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + int AronTypeNlohmannJSONReader::readStart##upperType##Type() \ + { \ + nlohmann::json current_json = getCurrentAndGoToNext(); \ + int c = current_json.size(); \ + AronTypeNlohmannJSONReaderTokenPtr newToken = AronTypeNlohmannJSONReaderTokenPtr(new AronTypeNlohmannJSONReaderToken(eAron##upperType##Type, current_json)); \ + stack.push(newToken); \ + return c; \ + } \ + \ + bool AronTypeNlohmannJSONReader::readEnd##upperType##Type() \ + { \ + AronTypeNlohmannJSONReaderTokenPtr token = stack.top(); \ + auto desc = token->getDescriptor(); \ + if (desc != AronTypeDescriptor::eAron##upperType##Type) \ + { \ + throw exception::AronTypeDescriptorNotValidException("AronTypeNlohmannJSONReader", "readEnd" + std::string(#upperType), "A token in the stack has the wrong type. Expected " + std::string(#upperType) + "Type", desc); \ + return false; \ + } \ + \ + if (token->checkedAllChildren()) \ + { \ + stack.pop(); \ + return true; \ + } \ + return false; \ + } + + HANDLE_CONTAINER_TYPES +#undef RUN_ARON_MACRO + + // Complex Type +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + std::pair<std::vector<int>, std::string> AronTypeNlohmannJSONReader::read##upperType##Type() \ + { \ + nlohmann::json j = getCurrent(); \ + std::vector<int> dims = j[ARON_TYPE_READER_WRITER_NDARRAY_DIMENSIONS_SLUG]; \ + std::string type = j[ARON_TYPE_READER_WRITER_NDARRAY_TYPE_SLUG]; \ + return std::make_pair(dims, type); \ + } + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + + // Read primitives +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + void AronTypeNlohmannJSONReader::read##upperType##Type() \ + { \ + nlohmann::json j = getCurrentAndGoToNext(); \ + } + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.h new file mode 100644 index 0000000000000000000000000000000000000000..ee52d331d6377fa6014eb8d2d2d159d32407e52d --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReader.h @@ -0,0 +1,81 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <stack> + +// BaseClass +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReader.h> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReaderToken.h> + +namespace armarx +{ + namespace aron + { + namespace io + { + class AronTypeNlohmannJSONReader; + typedef std::shared_ptr<AronTypeNlohmannJSONReader> AronTypeNlohmannJSONReaderPtr; + + class AronTypeNlohmannJSONReader : + virtual public AronTypeClassReader<nlohmann::json, AronTypeNlohmannJSONReaderTokenPtr> + { + public: + using PointerType = AronTypeNlohmannJSONReaderPtr; + + public: + // constructors + AronTypeNlohmannJSONReader() = delete; + AronTypeNlohmannJSONReader(const nlohmann::json& n); + AronTypeNlohmannJSONReader(const std::string& n); + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + virtual int readStart##upperType##Type() override; \ + virtual bool readEnd##upperType##Type() override; \ + + HANDLE_CONTAINER_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + virtual std::pair<std::vector<int>, std::string> read##upperType##Type() override; \ + + HANDLE_COMPLEX_TYPES +#undef RUN_ARON_MACRO + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ + virtual void read##upperType##Type() override; \ + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + + private: + nlohmann::json getCurrent(); + nlohmann::json getCurrentAndGoToNext(); + + private: + }; + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReaderToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReaderToken.h new file mode 100644 index 0000000000000000000000000000000000000000..802ea928acbaf1b748fea55a7b9e91b862743c13 --- /dev/null +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/NlohmannJSONReader/AronTypeNlohmannJSONReaderToken.h @@ -0,0 +1,141 @@ +/* +* 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/>. +* +* @author Fabian Peller (fabian dot peller at kit dot edu) +* @copyright http://www.gnu.org/licenses/gpl-2.0.txt +* GNU General Public License +*/ + +#pragma once + +// STD/STL +#include <memory> +#include <string> + +// Simox +#include <SimoxUtility/json.h> + +// Base Class +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classReaders/AronTypeClassReaderToken.h> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronConfig.h> +#include <RobotAPI/libraries/aron/aroncore/AronResolver.h> + +namespace armarx +{ + namespace aron + { + namespace io + { + // BaseClass + class AronTypeNlohmannJSONReaderToken; + typedef std::shared_ptr<AronTypeNlohmannJSONReaderToken> AronTypeNlohmannJSONReaderTokenPtr; + + class AronTypeNlohmannJSONReaderToken : + virtual public AronTypeClassReaderToken<nlohmann::json, nlohmann::json> + { + public: + using PointerType = AronTypeNlohmannJSONReaderTokenPtr; + + public: + // constructors + AronTypeNlohmannJSONReaderToken() = delete; + AronTypeNlohmannJSONReaderToken(const AronTypeDescriptor desc, const nlohmann::json& type) : + AronReaderToken<AronTypeDescriptor, nlohmann::json, nlohmann::json>(desc, type), + AronTypeClassReaderToken<nlohmann::json, nlohmann::json>(desc, type) + { + switch (descriptor) + { + case eAronObjectType: + { + for (auto it = type.begin(); it != type.end(); ++it) + { + allKeys.push_back(it.key()); + } + maxIndex = allKeys.size(); + break; + } + case eAronDictType: + { + maxIndex = 1; + break; + } + case eAronTupleType: + { + maxIndex = type.size(); + break; + } + case eAronListType: + { + maxIndex = 1; + break; + } + default: + throw exception::AronTypeDescriptorNotValidException("AronTypeNlohmannJSONReaderToken", "AronTypeNlohmannJSONReaderToken", "Received an invalid instance for a reader token.", desc); + } + } + + std::string getObjectName() const override + { + if(descriptor == eAronObjectType) + { + const nlohmann::json next = getCurrentElement(); + return next[ARON_READER_WRITER_TOKEN_OBJECT_NAME_SLUG]; + } + throw exception::AronTypeDescriptorNotValidException("AronTypeNavigatorReaderToken", "AronTypeNavigatorReaderToken", "Could not get object nme of a non-object token", descriptor); + } + + AronTypeDescriptor getTypeOfNextElement() const override + { + const nlohmann::json next = getCurrentElement(); + + throw exception::AronException("AronTypeNlohmannJSONReaderToken", "getTypeOfNextElement", "Could not determine the type of an nlohmann::json object. Could not convert to AronTypeDescriptor enum."); + } + + nlohmann::json getCurrentElement() const override + { + switch (descriptor) + { + case eAronObjectType: + { + nlohmann::json ret = element[resolveCurrentKey()]; + return ret; + } + case eAronDictType: + { + nlohmann::json ret = element[0]; + return ret; + } + case eAronTupleType: + { + nlohmann::json ret = element[currentIndex]; + return ret; + } + case eAronListType: + { + nlohmann::json ret = element[0]; + return ret; + } + default: + throw exception::AronTypeDescriptorNotValidException("AronTypeNlohmannJSONReaderToken", "getNextElement", "Could not resove a type of a navigator. Allowed are only container types due to performance", descriptor); + } + } + + private: + }; + } + } +} diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NavigatorWriter/AronTypeNavigatorWriterToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NavigatorWriter/AronTypeNavigatorWriterToken.h index 393f297ababa21a07b35350fc22407026fcb1a4b..dd1fc3caff7d836ec136f46797539af9f4df14f8 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NavigatorWriter/AronTypeNavigatorWriterToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NavigatorWriter/AronTypeNavigatorWriterToken.h @@ -62,25 +62,25 @@ namespace armarx { case eAronDictType: { - typenavigator::AronDictTypeNavigatorPtr casted = typenavigator::AronDictTypeNavigator::DynamicCast(element); + typenavigator::AronDictTypeNavigatorPtr casted = typenavigator::AronDictTypeNavigator::DynamicCastAndCheck(element); casted->setAcceptedType(n); break; } case eAronListType: { - typenavigator::AronListTypeNavigatorPtr casted = typenavigator::AronListTypeNavigator::DynamicCast(element); + typenavigator::AronListTypeNavigatorPtr casted = typenavigator::AronListTypeNavigator::DynamicCastAndCheck(element); casted->setAcceptedType(n); break; } case eAronObjectType: { - typenavigator::AronObjectTypeNavigatorPtr casted = typenavigator::AronObjectTypeNavigator::DynamicCast(element); + typenavigator::AronObjectTypeNavigatorPtr casted = typenavigator::AronObjectTypeNavigator::DynamicCastAndCheck(element); casted->addAcceptedType(currentKey, n); break; } case eAronTupleType: { - typenavigator::AronTupleTypeNavigatorPtr casted = typenavigator::AronTupleTypeNavigator::DynamicCast(element); + typenavigator::AronTupleTypeNavigatorPtr casted = typenavigator::AronTupleTypeNavigator::DynamicCastAndCheck(element); casted->addAcceptedType(n); currentIndex++; break; diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriter.cpp b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriter.cpp index 84c0016cd11aa8ab8a63ac0a09818c063557eeeb..6a6c2b436677dc808379a14e87206d43dc465e52 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriter.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriter.cpp @@ -58,8 +58,8 @@ namespace armarx { \ AronTypeNlohmannJSONWriterTokenPtr token = stack.top(); \ nlohmann::json j; \ - j["ARON_NDARRAY_DIMENSIONS"] = dims; \ - j["ARON_NDARRAY_TYPE"] = t; \ + j[ARON_TYPE_READER_WRITER_NDARRAY_DIMENSIONS_SLUG] = dims; \ + j[ARON_TYPE_READER_WRITER_NDARRAY_TYPE_SLUG] = t; \ token->addElement(j); \ return true; \ } @@ -71,8 +71,7 @@ namespace armarx bool AronTypeNlohmannJSONWriter::write##upperType##Type() \ { \ AronTypeNlohmannJSONWriterTokenPtr token = stack.top(); \ - lowerType x = {}; \ - nlohmann::json j(x); \ + nlohmann::json j(ARON_TYPE_READER_WRITER_##capsType##_TYPENAME); \ token->addElement(j); \ return true; \ } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriterToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriterToken.h index 71c8d03a8d214fb899932efae0e9adbc9eb0ef4e..0635697480291309988873a91fe8a3946719bd4a 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriterToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/NlohmannJSONWriter/AronTypeNlohmannJSONWriterToken.h @@ -65,12 +65,12 @@ namespace armarx { case eAronDictType: { - element["ARON_DICT_ACCEPTED_TYPE"] = n; + element[ARON_READER_WRITER_TOKEN_DICT_ACCEPTED_TYPE_SLUG] = n; break; } case eAronListType: { - element["ARON_LIST_ACCEPTED_TYPE"] = n; + element[ARON_READER_WRITER_TOKEN_LIST_ACCEPTED_TYPE_SLUG] = n; break; } case eAronObjectType: @@ -90,14 +90,14 @@ namespace armarx // name functions - void setName(const std::string& n) + void setName(const std::string& n) override { auto desc = getDescriptor(); if (desc != AronTypeDescriptor::eAronObjectType) { throw exception::AronTypeDescriptorNotValidException("AronTypeNlohmannJSONWriterToken", "setName", "Cant set the name of a non-object token.", desc); } - element["OBJECT_NAME"] = n; + element[ARON_READER_WRITER_TOKEN_OBJECT_NAME_SLUG] = n; } }; } diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/RapidXMLWriter/AronTypeRapidXMLWriter.h b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/RapidXMLWriter/AronTypeRapidXMLWriter.h index 540ef6e2b5297898cb496949ba1a092f9548bda5..f449391e69f453b22d906501145ecc49cdc53725 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/RapidXMLWriter/AronTypeRapidXMLWriter.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/RapidXMLWriter/AronTypeRapidXMLWriter.h @@ -20,11 +20,17 @@ #pragma once +// STD / STL #include <memory> #include <stack> #include <sstream> -#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/textWriters/AronTypeTextWriter.h> +// Base Class +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/AronTypeClassWriter.h> + +// ArmarX +#include <RobotAPI/libraries/aron/aroncore/AronConcepts.h> +#include <RobotAPI/libraries/aron/aroncore/io/AronTypeIO/classWriters/RapidXMLWriter/AronTypeRapidXMLWriterToken.h> namespace armarx { @@ -33,7 +39,7 @@ namespace armarx namespace io { class AronTypeXMLWriter : - virtual public AronTypeTextWriter + virtual public AronTypeClassWriter<RapidXmlReaderNode, AronTypeRapidXMLWriterTokenPtr> { public: #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ diff --git a/source/RobotAPI/libraries/aron/aroncore/io/AronWriterToken.h b/source/RobotAPI/libraries/aron/aroncore/io/AronWriterToken.h index f2621ee034d22975644a4f29341da3e39d60c81e..271d18054bb6631078d9eec67b1b8955255bb085 100644 --- a/source/RobotAPI/libraries/aron/aroncore/io/AronWriterToken.h +++ b/source/RobotAPI/libraries/aron/aroncore/io/AronWriterToken.h @@ -24,6 +24,9 @@ #include <memory> #include <string> +// Base class +#include <RobotAPI/libraries/aron/aroncore/io/AronReaderWriterToken.h> + // ArmarX #include <ArmarXCore/core/exceptions/Exception.h> @@ -45,7 +48,8 @@ namespace armarx using AronWriterTokenPtr = std::shared_ptr<AronWriterToken<AronDescriptorTypename, ElementTypename, ElementAddTypename>> ; template<typename AronDescriptorTypename, typename ElementTypename, typename ElementAddTypename> - class AronWriterToken + class AronWriterToken : + public AronReaderWriterToken { public: AronWriterToken() = delete; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigator.h index 93347c1e85ef993d7da83a23194e6c89d79efce4..e08088b990e89350488fb3735d199f1504765e01 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigator.h @@ -57,6 +57,9 @@ namespace armarx AronDataNavigator() = delete; AronDataNavigator(const AronDataDescriptor&, const AronPath& p = AronPath()); + // operators + virtual bool equalsDataNavigator(const AronDataNavigatorPtr&) const = 0; + // static methods static AronDataNavigatorPtr FromAronData(const data::AronDataPtr&, const AronPath& = AronPath()); static std::vector<AronDataNavigatorPtr> FromAronData(const std::vector<data::AronDataPtr>&, const AronPath& = AronPath()); @@ -68,6 +71,12 @@ namespace armarx return AronNavigatorType::DynamicCast(n); } + template<typename AronNavigatorType> + static typename AronNavigatorType::PointerType DynamicCastAndCheck(const AronDataNavigatorPtr& n) + { + return AronNavigatorType::DynamicCastAndCheck(n); + } + // virtual definitions of base class virtual data::AronDataPtr getResult() const override = 0; virtual std::string getName() const override = 0; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigatorFactory.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigatorFactory.cpp index 1f341cca20bdcae9117205ed8354b36c21e9c90a..a344953d9e79df70031fbeaef172f08e976b725a 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigatorFactory.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDataNavigatorFactory.cpp @@ -37,7 +37,7 @@ namespace armarx const std::map<AronDataDescriptor, AronDataNavigatorFactoryPtr> AronDataNavigatorFactory::FACTORIES = { #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - {AronResolver::GetTypeDescriptorForAronDataId(typeid(data::Aron##upperType)), AronDataNavigatorFactoryPtr(new Aron##upperType##DataNavigatorFactory())}, \ + {AronResolver::GetDescriptorForAronDataId(typeid(data::Aron##upperType)), AronDataNavigatorFactoryPtr(new Aron##upperType##DataNavigatorFactory())}, \ HANDLE_CONTAINER_DATA HANDLE_COMPLEX_DATA @@ -50,7 +50,7 @@ namespace armarx { CheckIfInputIsNull("AronDataNavigatorFactory", "create", path, aron); - auto typeDescriptor = AronResolver::GetTypeDescriptorForAronData(aron); + auto typeDescriptor = AronResolver::GetDescriptorForAronData(aron); auto factory_iterator = FACTORIES.find(typeDescriptor); if (factory_iterator == FACTORIES.end()) { diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.cpp index 9648916b27196a4a931acc188bda0d25ebce3c34..04870be678397c694a018cc857b97142a38bd99a 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.cpp @@ -71,24 +71,63 @@ namespace armarx } } + // operators + bool AronDictDataNavigator::operator==(const AronDictDataNavigator& other) const + { + for (const auto& [key, nav] : childrenNavigators) + { + if (not(other.hasElement(key))) + { + return false; + } + if (not(nav->equalsDataNavigator(other.getElement(key)))) + { + return false; + } + } + return true; + } + + bool AronDictDataNavigator::equalsDataNavigator(const AronDataNavigatorPtr& other) const + { + if (other == nullptr) + { + return false; + } + AronDictDataNavigatorPtr casted = DynamicCast(other); + return *this == *casted; + } + // static methods AronDictDataNavigatorPtr AronDictDataNavigator::DynamicCast(const AronDataNavigatorPtr& n) { - AronDictDataNavigatorPtr casted = std::dynamic_pointer_cast<AronDictDataNavigator>(n); + return std::dynamic_pointer_cast<AronDictDataNavigator>(n); + } + + AronDictDataNavigator AronDictDataNavigator::DynamicCast(AronDataNavigator& n) + { + return dynamic_cast<AronDictDataNavigator&>(n); + } + + AronDictDataNavigatorPtr AronDictDataNavigator::DynamicCastAndCheck(const AronDataNavigatorPtr& n) + { + CheckDataNavigatorPtrForNull("AronDictDataNavigator", "DynamicCastAndCheck[Before]", n); + AronDictDataNavigatorPtr casted = AronDictDataNavigator::DynamicCast(n); + CheckDataNavigatorPtrForNull("AronDictDataNavigator", "DynamicCastAndCheck[After]", n->getPath(), casted); return casted; } - AronDictDataNavigatorPtr AronDictDataNavigator::FromAronDict(const data::AronDictPtr& aron) + AronDictDataNavigatorPtr AronDictDataNavigator::FromAronDictPtr(const data::AronDictPtr& aron) { return std::make_shared<AronDictDataNavigator>(aron); } - data::AronDictPtr AronDictDataNavigator::ToAronDict(const AronDictDataNavigatorPtr& navigator) + data::AronDictPtr AronDictDataNavigator::ToAronDictPtr(const AronDictDataNavigatorPtr& navigator) { - return navigator ? navigator->toAronDict() : nullptr; + return navigator ? navigator->toAronDictPtr() : nullptr; } - data::AronDictPtr AronDictDataNavigator::toAronDict() const + data::AronDictPtr AronDictDataNavigator::toAronDictPtr() const { return aron; } @@ -110,6 +149,11 @@ namespace armarx this->aron->elements[key] = data->getResult(); } + bool AronDictDataNavigator::hasElement(const std::string& key) const + { + return childrenNavigators.count(key) > 0; + } + AronDataNavigatorPtr AronDictDataNavigator::getElement(const std::string& key) const { auto it = childrenNavigators.find(key); @@ -136,15 +180,10 @@ namespace armarx aron->elements.clear(); } - data::AronDictPtr AronDictDataNavigator::getCastedResult() const - { - return aron; - } - // virtual implementations data::AronDataPtr AronDictDataNavigator::getResult() const { - return getCastedResult(); + return toAronDictPtr(); } std::string AronDictDataNavigator::getName() const @@ -178,15 +217,36 @@ namespace armarx return false; } - typenavigator::AronDictSerializerTypeNavigatorPtr dictTypenav = typenavigator::AronDictSerializerTypeNavigator::DynamicCast(type); - for (const auto& [_, nav] : childrenNavigators) { - if (!nav->fullfillsType(dictTypenav->getAcceptedType())) + typenavigator::AronSingleAcceptedTypeHavingTypeNavigatorPtr dictTypenav = typenavigator::AronSingleAcceptedTypeHavingTypeNavigator::DynamicCast(type); + if (dictTypenav) { - return false; + for (const auto& [_, nav] : childrenNavigators) + { + if (!nav->fullfillsType(dictTypenav->getAcceptedType())) + { + return false; + } + } + return true; } } - return true; + + { + typenavigator::AronMultipleDictAcceptedTypeHavingTypeNavigatorPtr dictTypenav = typenavigator::AronMultipleDictAcceptedTypeHavingTypeNavigator::DynamicCast(type); + if (dictTypenav) + { + for (const auto& [key, nav] : childrenNavigators) + { + if (!nav->fullfillsType(dictTypenav->getAcceptedType(key))) + { + return false; + } + } + return true; + } + } + return false; } std::vector<AronDataNavigatorPtr> AronDictDataNavigator::getChildren() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.h index 8cbcdb81bac7c35c57e3db9a0947c5c44a0eccf6..fc8c053c9044c708be0209d49b5aa4e8296e369e 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronDictDataNavigator.h @@ -54,21 +54,28 @@ namespace armarx AronDictDataNavigator(const data::AronDataDict&, const AronPath& path = AronPath()); AronDictDataNavigator(const std::map<std::string, AronDataNavigatorPtr>&, const AronPath& path = AronPath()); + // operators + bool operator==(const AronDictDataNavigator&) const; + virtual bool equalsDataNavigator(const AronDataNavigatorPtr&) const override; + // static methods static AronDictDataNavigatorPtr DynamicCast(const AronDataNavigatorPtr& n); - static AronDictDataNavigatorPtr FromAronDict(const data::AronDictPtr& aron); - static data::AronDictPtr ToAronDict(const AronDictDataNavigatorPtr& navigator); + static AronDictDataNavigator DynamicCast(AronDataNavigator& n); + static AronDictDataNavigatorPtr DynamicCastAndCheck(const AronDataNavigatorPtr& n); + + static AronDictDataNavigatorPtr FromAronDictPtr(const data::AronDictPtr& aron); + static data::AronDictPtr ToAronDictPtr(const AronDictDataNavigatorPtr& navigator); // public member functions - /// Get the derived aron type (in contrast to getResult()). - data::AronDictPtr toAronDict() const; + data::AronDictPtr toAronDictPtr() const; std::vector<std::string> getAllKeys() const; + void addElement(const std::string& key, const AronDataNavigatorPtr&); + bool hasElement(const std::string&) const; AronDataNavigatorPtr getElement(const std::string&) const; std::map<std::string, AronDataNavigatorPtr> getElements() const; - void clear(); - data::AronDictPtr getCastedResult() const; + void clear(); // virtual implementations virtual data::AronDataPtr getResult() const override; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.cpp index 4db8327fe8b380027cb85739de83f1c6554f6e87..6fd7b6babc4aaed8e8907d7c4385cd2cf799477a 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.cpp @@ -71,6 +71,36 @@ namespace armarx } } + // operators + bool AronListDataNavigator::operator==(const AronListDataNavigator& other) const + { + unsigned int i = 0; + for (const auto& nav : childrenNavigators) + { + if (not(other.hasElement(i))) + { + return false; + } + if (not(nav->equalsDataNavigator(other.getElement(i)))) + { + return false; + } + i++; + } + return true; + } + + bool AronListDataNavigator::equalsDataNavigator(const AronDataNavigatorPtr& other) const + { + if (other == nullptr) + { + return false; + } + AronListDataNavigatorPtr casted = DynamicCast(other); + return *this == *casted; + } + + // static methods AronListDataNavigatorPtr AronListDataNavigator::DynamicCast(const AronDataNavigatorPtr& n) { @@ -78,6 +108,29 @@ namespace armarx return casted; } + AronListDataNavigator AronListDataNavigator::DynamicCast(AronDataNavigator& n) + { + return dynamic_cast<AronListDataNavigator&>(n); + } + + AronListDataNavigatorPtr AronListDataNavigator::DynamicCastAndCheck(const AronDataNavigatorPtr& n) + { + CheckDataNavigatorPtrForNull("AronListDataNavigator", "DynamicCastAndCheck[Before]", n); + AronListDataNavigatorPtr casted = AronListDataNavigator::DynamicCast(n); + CheckDataNavigatorPtrForNull("AronListDataNavigator", "DynamicCastAndCheck[After]", n->getPath(), casted); + return casted; + } + + AronListDataNavigatorPtr AronListDataNavigator::FromAronListPtr(const data::AronListPtr& aron) + { + return std::make_shared<AronListDataNavigator>(aron); + } + + data::AronListPtr AronListDataNavigator::ToAronListPtr(const AronListDataNavigatorPtr& navigator) + { + return navigator ? navigator->toAronListPtr() : nullptr; + } + // public member functions void AronListDataNavigator::addElement(const AronDataNavigatorPtr& n) { @@ -85,6 +138,11 @@ namespace armarx aron->elements.push_back(n->getResult()); } + bool AronListDataNavigator::hasElement(unsigned int i) const + { + return i < childrenNavigators.size(); + } + AronDataNavigatorPtr AronListDataNavigator::getElement(unsigned int i) const { if (i >= childrenNavigators.size()) @@ -105,7 +163,7 @@ namespace armarx aron->elements.clear(); } - data::AronListPtr AronListDataNavigator::getCastedResult() const + data::AronListPtr AronListDataNavigator::toAronListPtr() const { return aron; } @@ -113,7 +171,7 @@ namespace armarx // virtual implementations data::AronDataPtr AronListDataNavigator::getResult() const { - return getCastedResult(); + return toAronListPtr(); } std::string AronListDataNavigator::getName() const @@ -149,15 +207,38 @@ namespace armarx return false; } - typenavigator::AronListSerializerTypeNavigatorPtr listTypenav = typenavigator::AronListSerializerTypeNavigator::DynamicCast(type); - for (const auto& nav : childrenNavigators) { - if (!nav->fullfillsType(listTypenav->getAcceptedType())) + typenavigator::AronSingleAcceptedTypeHavingTypeNavigatorPtr listTypenav = typenavigator::AronSingleAcceptedTypeHavingTypeNavigator::DynamicCast(type); + if (listTypenav) { - return false; + for (const auto& nav : childrenNavigators) + { + if (!nav->fullfillsType(listTypenav->getAcceptedType())) + { + return false; + } + } + return true; } } - return true; + + { + typenavigator::AronMultipleListAcceptedTypeHavingTypeNavigatorPtr listTypenav = typenavigator::AronMultipleListAcceptedTypeHavingTypeNavigator::DynamicCast(type); + if (listTypenav) + { + unsigned int i = 0; + for (const auto& nav : childrenNavigators) + { + if (!nav->fullfillsType(listTypenav->getAcceptedType(i))) + { + return false; + } + } + return true; + } + } + + return false; } std::vector<AronDataNavigatorPtr> AronListDataNavigator::getChildren() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.h index 03151c2cfe616f239253d6864619596c3d82b0bf..d53ad0bb37ca55159a9f13afc8fb5feaa2230fe7 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronListDataNavigator.h @@ -53,16 +53,27 @@ namespace armarx AronListDataNavigator(const data::AronDataList&, const AronPath& path = AronPath()); AronListDataNavigator(const std::vector<AronDataNavigatorPtr>&, const AronPath& path = AronPath()); + // operators + bool operator==(const AronListDataNavigator&) const; + virtual bool equalsDataNavigator(const AronDataNavigatorPtr&) const override; + // static methods static AronListDataNavigatorPtr DynamicCast(const AronDataNavigatorPtr& n); + static AronListDataNavigator DynamicCast(AronDataNavigator& n); + static AronListDataNavigatorPtr DynamicCastAndCheck(const AronDataNavigatorPtr& n); + + static AronListDataNavigatorPtr FromAronListPtr(const data::AronListPtr& aron); + static data::AronListPtr ToAronListPtr(const AronListDataNavigatorPtr& navigator); // public member functions + data::AronListPtr toAronListPtr() const; + void addElement(const AronDataNavigatorPtr&); AronDataNavigatorPtr getElement(unsigned int) const; + bool hasElement(unsigned int) const; std::vector<AronDataNavigatorPtr> getElements() const; - void clear(); - data::AronListPtr getCastedResult() const; + void clear(); // virtual implementations virtual data::AronDataPtr getResult() const override; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.cpp index 13ef492d76a54535c6f3a0d7982b6c457e606284..666adcc6b5540bd081a9fbf068439dd4a5b5a02b 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.cpp @@ -55,13 +55,41 @@ namespace armarx CheckAronPtrForNull("AronNDArrayDataNavigator", "AronNDArrayDataNavigator", getPath(), aron); } - - AronNDArrayDataNavigator::AronNDArrayDataNavigator(const std::vector<int>& dim, const std::string& t, const std::vector<unsigned char>& data, const AronPath& path) : AronNDArrayDataNavigator(data::AronNDArrayPtr(new data::AronNDArray(dim, t, data)), path) { } + // operators + bool AronNDArrayDataNavigator::operator==(const AronNDArrayDataNavigator& other) const + { + const auto otherAron = other.toAronNDArrayPtr(); + if (aron->dimensions != otherAron->dimensions) + { + return false; + } + if (aron->type != otherAron->type) + { + return false; + } + if (aron->data != otherAron->data) + { + return false; + } + return true; + } + + bool AronNDArrayDataNavigator::equalsDataNavigator(const AronDataNavigatorPtr& other) const + { + if (other == nullptr) + { + return false; + } + AronNDArrayDataNavigatorPtr casted = DynamicCast(other); + return *this == *casted; + } + + // static methods AronNDArrayDataNavigatorPtr AronNDArrayDataNavigator::DynamicCast(const AronDataNavigatorPtr& n) { @@ -69,6 +97,29 @@ namespace armarx return casted; } + AronNDArrayDataNavigator AronNDArrayDataNavigator::DynamicCast(AronDataNavigator& n) + { + return dynamic_cast<AronNDArrayDataNavigator&>(n); + } + + AronNDArrayDataNavigatorPtr AronNDArrayDataNavigator::DynamicCastAndCheck(const AronDataNavigatorPtr& n) + { + CheckDataNavigatorPtrForNull("AronNDArrayDataNavigator", "DynamicCastAndCheck[Before]", n); + AronNDArrayDataNavigatorPtr casted = AronNDArrayDataNavigator::DynamicCast(n); + CheckDataNavigatorPtrForNull("AronNDArrayDataNavigator", "DynamicCastAndCheck[After]", n->getPath(), casted); + return casted; + } + + AronNDArrayDataNavigatorPtr AronNDArrayDataNavigator::FromAronNDArrayPtr(const data::AronNDArrayPtr& aron) + { + return std::make_shared<AronNDArrayDataNavigator>(aron); + } + + data::AronNDArrayPtr AronNDArrayDataNavigator::ToAronNDArrayPtr(const AronNDArrayDataNavigatorPtr& navigator) + { + return navigator ? navigator->toAronNDArrayPtr() : nullptr; + } + // public member functions unsigned char* AronNDArrayDataNavigator::getData() const { @@ -111,7 +162,7 @@ namespace armarx aron->type = t; } - data::AronNDArrayPtr AronNDArrayDataNavigator::getCastedResult() const + data::AronNDArrayPtr AronNDArrayDataNavigator::toAronNDArrayPtr() const { return aron; } @@ -119,7 +170,7 @@ namespace armarx // virtual implementations data::AronDataPtr AronNDArrayDataNavigator::getResult() const { - return getCastedResult(); + return toAronNDArrayPtr(); } std::string AronNDArrayDataNavigator::getName() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.h index b5182d9d3f06933486a99b3a851ab84436cfcf31..23e734a86c6fa76f64ba5808c2187c9fbbf7b4a9 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronNDArrayDataNavigator.h @@ -52,8 +52,17 @@ namespace armarx AronNDArrayDataNavigator(const data::AronNDArrayPtr&, const AronPath& path = AronPath()); AronNDArrayDataNavigator(const std::vector<int>&, const std::string&, const std::vector<unsigned char>&, const AronPath& path = AronPath()); + // operators + bool operator==(const AronNDArrayDataNavigator&) const; + virtual bool equalsDataNavigator(const AronDataNavigatorPtr&) const override; + // static methods static AronNDArrayDataNavigatorPtr DynamicCast(const AronDataNavigatorPtr& n); + static AronNDArrayDataNavigator DynamicCast(AronDataNavigator& n); + static AronNDArrayDataNavigatorPtr DynamicCastAndCheck(const AronDataNavigatorPtr& n); + + static AronNDArrayDataNavigatorPtr FromAronNDArrayPtr(const data::AronNDArrayPtr& aron); + static data::AronNDArrayPtr ToAronNDArrayPtr(const AronNDArrayDataNavigatorPtr& navigator); // public member functions unsigned char* getData() const; @@ -66,7 +75,7 @@ namespace armarx std::string getType() const; void setType(const std::string&); - data::AronNDArrayPtr getCastedResult() const; + data::AronNDArrayPtr toAronNDArrayPtr() const; // virtual implementations virtual data::AronDataPtr getResult() const override; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.cpp index 7a2e1f755c2e4cceeba8bd60901b04a4b9f38d5d..e5d5a29c29b4dbe6e7878a9c59440a3c972525c1 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.cpp @@ -54,6 +54,26 @@ namespace armarx Aron##upperType##DataNavigator(data::Aron##upperType##Ptr(new data::Aron##upperType(d)), path) \ { \ } \ + \ + /* operators */ \ + bool Aron##upperType##DataNavigator::operator==(const Aron##upperType##DataNavigator& other) const \ + { \ + const auto& otherAron = other.toAron##upperType##Ptr(); \ + if(aron->value != otherAron->value) \ + return false; \ + return true; \ + } \ + \ + bool Aron##upperType##DataNavigator::equalsDataNavigator(const AronDataNavigatorPtr& other) const \ + { \ + if(other == nullptr) \ + { \ + return false; \ + } \ + Aron##upperType##DataNavigatorPtr casted = DynamicCast(other); \ + return *this == *casted; \ + } \ + \ /* static methods */ \ Aron##upperType##DataNavigatorPtr Aron##upperType##DataNavigator::DynamicCast(const AronDataNavigatorPtr& n) \ { \ @@ -61,6 +81,29 @@ namespace armarx return casted; \ } \ \ + Aron##upperType##DataNavigator Aron##upperType##DataNavigator::DynamicCast(AronDataNavigator& n) \ + { \ + return dynamic_cast<Aron##upperType##DataNavigator&>(n); \ + } \ + \ + Aron##upperType##DataNavigatorPtr Aron##upperType##DataNavigator::DynamicCastAndCheck(const AronDataNavigatorPtr& n) \ + { \ + CheckDataNavigatorPtrForNull("Aron"+std::string(#upperType)+"DataNavigator", "DynamicCastAndCheck[Before]", n); \ + Aron##upperType##DataNavigatorPtr casted = Aron##upperType##DataNavigator::DynamicCast(n); \ + CheckDataNavigatorPtrForNull("Aron"+std::string(#upperType)+"DataNavigator", "DynamicCastAndCheck[After]", n->getPath(), casted); \ + return casted; \ + } \ + \ + Aron##upperType##DataNavigatorPtr Aron##upperType##DataNavigator::FromAron##upperType##Ptr(const data::Aron##upperType##Ptr& aron) \ + { \ + return std::make_shared<Aron##upperType##DataNavigator>(aron); \ + } \ + \ + data::Aron##upperType##Ptr Aron##upperType##DataNavigator::ToAron##upperType##Ptr(const Aron##upperType##DataNavigatorPtr& navigator) \ + { \ + return navigator ? navigator->toAron##upperType##Ptr() : nullptr; \ + } \ + \ /* public member functions */ \ void Aron##upperType##DataNavigator::setValue(const lowerType& x) \ { \ @@ -72,7 +115,7 @@ namespace armarx return aron->value; \ } \ \ - data::Aron##upperType##Ptr Aron##upperType##DataNavigator::getCastedResult() const \ + data::Aron##upperType##Ptr Aron##upperType##DataNavigator::toAron##upperType##Ptr() const \ { \ return aron; \ } \ @@ -80,7 +123,7 @@ namespace armarx /* virtual implementations */ \ data::AronDataPtr Aron##upperType##DataNavigator::getResult() const \ { \ - return getCastedResult(); \ + return aron; \ } \ \ std::string Aron##upperType##DataNavigator::getName() const \ diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.h index 8a0ee59dab2078c15a152e3ed5d94ae1135b5754..bb6eb11530db92001f0b5f450fcf74e02ad1ef90 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/datanavigator/AronPrimitiveDataNavigator.h @@ -39,8 +39,13 @@ namespace armarx #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ class Aron##upperType##DataNavigator; \ - typedef std::shared_ptr<Aron##upperType##DataNavigator> Aron##upperType##DataNavigatorPtr; \ - \ + typedef std::shared_ptr<Aron##upperType##DataNavigator> Aron##upperType##DataNavigatorPtr; + + HANDLE_PRIMITIVE_TYPES +#undef RUN_ARON_MACRO + + +#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ class Aron##upperType##DataNavigator : \ virtual public AronPrimitiveDataNavigator \ { \ @@ -52,14 +57,23 @@ namespace armarx Aron##upperType##DataNavigator(const data::Aron##upperType##Ptr&, const AronPath& = AronPath()); \ Aron##upperType##DataNavigator(const lowerType&, const AronPath& = AronPath()); \ \ + /* operators */ \ + bool operator==(const Aron##upperType##DataNavigator&) const; \ + virtual bool equalsDataNavigator(const AronDataNavigatorPtr&) const override; \ + \ /* static methods */ \ static Aron##upperType##DataNavigatorPtr DynamicCast(const AronDataNavigatorPtr& n); \ + static Aron##upperType##DataNavigator DynamicCast(AronDataNavigator& n); \ + static Aron##upperType##DataNavigatorPtr DynamicCastAndCheck(const AronDataNavigatorPtr& n); \ + \ + static Aron##upperType##DataNavigatorPtr FromAron##upperType##Ptr(const data::Aron##upperType##Ptr& aron); \ + static data::Aron##upperType##Ptr ToAron##upperType##Ptr(const Aron##upperType##DataNavigatorPtr& navigator); \ \ /* public member functions */ \ void setValue(const lowerType& x); \ lowerType getValue() const; \ \ - data::Aron##upperType##Ptr getCastedResult() const; \ + data::Aron##upperType##Ptr toAron##upperType##Ptr() const; \ \ /* virtual implementations */ \ virtual data::AronDataPtr getResult() const override; \ diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.cpp index 9d1ba674f32d4043274efe63ac7b94092ab8610c..997d1bf8cf73dda37bce8938d202e621188c659b 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.cpp @@ -48,21 +48,6 @@ namespace armarx CheckAronPtrForNull("AronDictTypeNavigator", "AronDictTypeNavigator", getPath(), o); } - void AronDictTypeNavigator::addAcceptedType(const std::string&, const AronTypeNavigatorPtr&) - { - throw exception::AronExceptionWithPathInfo("AronDictTypeNavigator", "addAcceptedType", "Called invalid function!", getPath()); - } - - std::map<std::string, AronTypeNavigatorPtr> AronDictTypeNavigator::getAcceptedTypes() const - { - throw exception::AronExceptionWithPathInfo("AronDictTypeNavigator", "getAcceptedTypes", "Called invalid function!", getPath()); - } - - AronTypeNavigatorPtr AronDictTypeNavigator::getAcceptedType() const - { - return acceptedTypeNavigator; - } - void AronDictTypeNavigator::setAcceptedType(const AronTypeNavigatorPtr& a) { type->acceptedType = a->getResult(); @@ -72,9 +57,14 @@ namespace armarx // static methods AronDictTypeNavigatorPtr AronDictTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) { - CheckTypeNavigatorPtrForNull("AronDictTypeNavigator", "DynamicCast[Before]", n); - AronDictTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronDictTypeNavigator>(n); - CheckTypeNavigatorPtrForNull("AronDictTypeNavigator", "DynamicCast[After]", casted); + return std::dynamic_pointer_cast<AronDictTypeNavigator>(n); + } + + AronDictTypeNavigatorPtr AronDictTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) + { + CheckTypeNavigatorPtrForNull("AronDictTypeNavigator", "DynamicCastAndCheck[Before]", n); + AronDictTypeNavigatorPtr casted = AronDictTypeNavigator::DynamicCast(n); + CheckTypeNavigatorPtrForNull("AronDictTypeNavigator", "DynamicCastAndCheck[After]", n->getPath(), casted); return casted; } diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.h index b633eab3b6d41fe9a56042cd7d5de3aba22780d6..b1de1358f8205c823ab05915e1ce82f046ba468f 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronDictTypeNavigator.h @@ -40,6 +40,7 @@ namespace armarx typedef std::shared_ptr<AronDictTypeNavigator> AronDictTypeNavigatorPtr; class AronDictTypeNavigator : + virtual public AronSingleAcceptedTypeHavingTypeNavigator, virtual public AronDictSerializerTypeNavigator { public: @@ -51,24 +52,20 @@ namespace armarx AronDictTypeNavigator(const AronPath& path); AronDictTypeNavigator(const type::AronDictTypePtr&, const AronPath& path); - type::AronDictTypePtr getCastedResult() const; + type::AronDictTypePtr toAronDictTypePtr() const; // static methods static AronDictTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronDictTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations virtual void setAcceptedType(const AronTypeNavigatorPtr&) override; - virtual AronTypeNavigatorPtr getAcceptedType() const override; - - virtual void addAcceptedType(const std::string&, const AronTypeNavigatorPtr&) override; - virtual std::map<std::string, AronTypeNavigatorPtr> getAcceptedTypes() const override; virtual type::AronTypePtr getResult() const override; virtual std::string getName() const override; private: // members - AronTypeNavigatorPtr acceptedTypeNavigator; type::AronDictTypePtr type; }; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.cpp index d26786cd29041438eb29c649efe0678bcdbb2fac..ebf633d874581fe6105cbdd0861c824b56c6fc67 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.cpp @@ -89,7 +89,7 @@ namespace armarx type->dimensions[1] = h; } - type::AronEigenMatrixTypePtr AronEigenMatrixTypeNavigator::getCastedResult() const + type::AronEigenMatrixTypePtr AronEigenMatrixTypeNavigator::toAronEigenMatrixTypePtr() const { if (type->dimensions.empty()) { @@ -155,17 +155,22 @@ namespace armarx // static methods AronEigenMatrixTypeNavigatorPtr AronEigenMatrixTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) + { + return std::dynamic_pointer_cast<AronEigenMatrixTypeNavigator>(n); + } + + AronEigenMatrixTypeNavigatorPtr AronEigenMatrixTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) { CheckTypeNavigatorPtrForNull("AronEigenMatrixTypeNavigator", "DynamicCast[Before]", n); - AronEigenMatrixTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronEigenMatrixTypeNavigator>(n); - CheckTypeNavigatorPtrForNull("AronEigenMatrixTypeNavigator", "DynamicCast[After]", casted); + AronEigenMatrixTypeNavigatorPtr casted = AronEigenMatrixTypeNavigator::DynamicCast(n); + CheckTypeNavigatorPtrForNull("AronEigenMatrixTypeNavigator", "DynamicCast[After]", n->getPath(), casted); return casted; } // virtual implementations type::AronTypePtr AronEigenMatrixTypeNavigator::getResult() const { - return getCastedResult(); + return toAronEigenMatrixTypePtr(); } std::string AronEigenMatrixTypeNavigator::getName() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.h index 14a24b99881938dd05af0893415091c1d308d4b2..2409b97ae583ffcfa5bc0eadc25b07d33c58eb1d 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronEigenMatrixTypeNavigator.h @@ -58,10 +58,11 @@ namespace armarx void setRows(const unsigned int&); void setCols(const unsigned int&); - type::AronEigenMatrixTypePtr getCastedResult() const; + type::AronEigenMatrixTypePtr toAronEigenMatrixTypePtr() const; // static methods static AronEigenMatrixTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronEigenMatrixTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations virtual void setDimensions(const std::vector<int>&) override; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.cpp index 49670a0c2a29e30382a7582a138efa924e649051..3b6ab6e9a2bc071281a474a12a1833f59dace60c 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.cpp @@ -57,7 +57,7 @@ namespace armarx } } - type::AronIVTCByteImageTypePtr AronIVTCByteImageTypeNavigator::getCastedResult() const + type::AronIVTCByteImageTypePtr AronIVTCByteImageTypeNavigator::toAronIVTCByteImageTypePtr() const { if (type->dimensions.size() != 2) { @@ -145,17 +145,22 @@ namespace armarx // static methods AronIVTCByteImageTypeNavigatorPtr AronIVTCByteImageTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) + { + return std::dynamic_pointer_cast<AronIVTCByteImageTypeNavigator>(n); + } + + AronIVTCByteImageTypeNavigatorPtr AronIVTCByteImageTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) { CheckTypeNavigatorPtrForNull("AronIVTCByteImageTypeNavigator", "DynamicCast[Before]", n); - AronIVTCByteImageTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronIVTCByteImageTypeNavigator>(n); - CheckTypeNavigatorPtrForNull("AronIVTCByteImageTypeNavigator", "DynamicCast[After]", casted); + AronIVTCByteImageTypeNavigatorPtr casted = AronIVTCByteImageTypeNavigator::DynamicCast(n); + CheckTypeNavigatorPtrForNull("AronIVTCByteImageTypeNavigator", "DynamicCast[After]", n->getPath(), casted); return casted; } // virtual implementations type::AronTypePtr AronIVTCByteImageTypeNavigator::getResult() const { - return getCastedResult(); + return toAronIVTCByteImageTypePtr(); } std::string AronIVTCByteImageTypeNavigator::getName() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.h index 9d1ba206d4f615d76ad797f2a4147e8b030c7d9a..b0ebf28bbed62632a41f757348de7687ee9d7abb 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronIVTCByteImageTypeNavigator.h @@ -58,10 +58,11 @@ namespace armarx void setWidth(const unsigned int&); void setHeight(const unsigned int&); - type::AronIVTCByteImageTypePtr getCastedResult() const; + type::AronIVTCByteImageTypePtr toAronIVTCByteImageTypePtr() const; // static methods static AronIVTCByteImageTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronIVTCByteImageTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations virtual void setDimensions(const std::vector<int>&) override; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.cpp index 34a31e4352a5820c8502e98ad7d0141462e88eb8..d77033eccc99b96ea2c57db99f36179c2a7b9b75 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.cpp @@ -48,14 +48,19 @@ namespace armarx // static methods AronListTypeNavigatorPtr AronListTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) + { + return std::dynamic_pointer_cast<AronListTypeNavigator>(n); + } + + AronListTypeNavigatorPtr AronListTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) { CheckTypeNavigatorPtrForNull("AronListTypeNavigator", "DynamicCast[Before]", n); - AronListTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronListTypeNavigator>(n); - CheckTypeNavigatorPtrForNull("AronListTypeNavigator", "DynamicCast[After]", casted); + AronListTypeNavigatorPtr casted = AronListTypeNavigator::DynamicCast(n); + CheckTypeNavigatorPtrForNull("AronListTypeNavigator", "DynamicCast[After]", n->getPath(), casted); return casted; } - type::AronListTypePtr AronListTypeNavigator::getCastedResult() const + type::AronListTypePtr AronListTypeNavigator::toAronListTypePtr() const { CheckAronPtrForNull("AronListTypeNavigator", "getCastedResult", getPath(), type->acceptedType); return type; @@ -64,7 +69,7 @@ namespace armarx // virtual implementations type::AronTypePtr AronListTypeNavigator::getResult() const { - return getCastedResult(); + return toAronListTypePtr(); } std::string AronListTypeNavigator::getName() const @@ -72,21 +77,6 @@ namespace armarx return "AronListType<" + acceptedTypeNavigator->getName() + ">"; } - void AronListTypeNavigator::addAcceptedType(const AronTypeNavigatorPtr&) - { - throw exception::AronExceptionWithPathInfo("AronListTypeNavigator", "addAcceptedType", "Called invalid function!", getPath()); - } - - std::vector<AronTypeNavigatorPtr> AronListTypeNavigator::getAcceptedTypes() const - { - throw exception::AronExceptionWithPathInfo("AronListTypeNavigator", "getAcceptedTypes", "Called invalid function!", getPath()); - } - - AronTypeNavigatorPtr AronListTypeNavigator::getAcceptedType() const - { - return acceptedTypeNavigator; - } - void AronListTypeNavigator::setAcceptedType(const AronTypeNavigatorPtr& a) { CheckTypeNavigatorPtrForNull("AronListTypeNavigator", "setAcceptedType", a); diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.h index 3982291ffef4373e68fe061d144291e3bceba44b..2bb8fe8c3847c7f8c9d723c6a97ce7340d9ac782 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronListTypeNavigator.h @@ -40,7 +40,8 @@ namespace armarx typedef std::shared_ptr<AronListTypeNavigator> AronListTypeNavigatorPtr; class AronListTypeNavigator : - public AronListSerializerTypeNavigator + virtual public AronSingleAcceptedTypeHavingTypeNavigator, + virtual public AronListSerializerTypeNavigator { public: using PointerType = AronListTypeNavigatorPtr; @@ -51,24 +52,20 @@ namespace armarx AronListTypeNavigator(const AronPath& path); AronListTypeNavigator(const type::AronListTypePtr&, const AronPath& path); - type::AronListTypePtr getCastedResult() const; + type::AronListTypePtr toAronListTypePtr() const; // static methods static AronListTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronListTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations - virtual AronTypeNavigatorPtr getAcceptedType() const override; - virtual std::vector<AronTypeNavigatorPtr> getAcceptedTypes() const override; - virtual void setAcceptedType(const AronTypeNavigatorPtr&) override; - virtual void addAcceptedType(const AronTypeNavigatorPtr&) override; virtual type::AronTypePtr getResult() const override; virtual std::string getName() const override; private: // members - AronTypeNavigatorPtr acceptedTypeNavigator; type::AronListTypePtr type; }; } diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.cpp index 01b87e9f7bb0adfe51a69fcbb62c4db0634e0a85..63b13137ed73427745069b8032f8f03488a8ccff 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.cpp @@ -50,24 +50,19 @@ namespace armarx // static methods AronObjectTypeNavigatorPtr AronObjectTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) + { + return std::dynamic_pointer_cast<AronObjectTypeNavigator>(n); + } + + AronObjectTypeNavigatorPtr AronObjectTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) { CheckTypeNavigatorPtrForNull("AronObjectTypeNavigator", "DynamicCast[Before]", n); - AronObjectTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronObjectTypeNavigator>(n); + AronObjectTypeNavigatorPtr casted = AronObjectTypeNavigator::DynamicCast(n); CheckTypeNavigatorPtrForNull("AronObjectTypeNavigator", "DynamicCast[After]", n->getPath(), casted); return casted; } // public member functions - void AronObjectTypeNavigator::setAcceptedType(const AronTypeNavigatorPtr&) - { - throw exception::AronExceptionWithPathInfo("AronObjectTypeNavigator", "setAcceptedType", "Called invalid function!", getPath()); - } - - AronTypeNavigatorPtr AronObjectTypeNavigator::getAcceptedType() const - { - throw exception::AronExceptionWithPathInfo("AronObjectTypeNavigator", "getAcceptedType", "Called invalid function!", getPath()); - } - void AronObjectTypeNavigator::addAcceptedType(const std::string& k, const AronTypeNavigatorPtr& v) { if (k.empty()) @@ -76,7 +71,7 @@ namespace armarx } CheckTypeNavigatorPtrForNull("AronObjectTypeNavigator", "addAcceptedType", v); type->elementTypes[k] = v->getResult(); - memberNavigators[k] = v; + acceptedTypeNavigators[k] = v; } void AronObjectTypeNavigator::setObjectName(const std::string& n) @@ -98,17 +93,12 @@ namespace armarx return type->objectName; } - std::map<std::string, AronTypeNavigatorPtr> AronObjectTypeNavigator::getAcceptedTypes() const - { - return memberNavigators; - } - AronObjectTypeNavigatorPtr AronObjectTypeNavigator::getExtends() const { return extends; } - type::AronObjectTypePtr AronObjectTypeNavigator::getCastedResult() const + type::AronObjectTypePtr AronObjectTypeNavigator::toAronObjectTypePtr() const { if (type->objectName.empty()) { @@ -124,7 +114,7 @@ namespace armarx // virtual implementations type::AronTypePtr AronObjectTypeNavigator::getResult() const { - return getCastedResult(); + return toAronObjectTypePtr(); } std::string AronObjectTypeNavigator::getName() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.h index e1fdc5ed2a86b47e4157271c14aebbcc02ab0d37..0f41663b47d5a8dce4b198a09ee52b8e5410b4f8 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronObjectTypeNavigator.h @@ -42,6 +42,7 @@ namespace armarx typedef std::shared_ptr<AronObjectTypeNavigator> AronObjectTypeNavigatorPtr; class AronObjectTypeNavigator : + virtual public AronMultipleDictAcceptedTypeHavingTypeNavigator, virtual public AronDictSerializerTypeNavigator { public: @@ -60,24 +61,20 @@ namespace armarx void setObjectName(const std::string&); void setExtends(const AronObjectTypeNavigatorPtr&); - type::AronObjectTypePtr getCastedResult() const; + type::AronObjectTypePtr toAronObjectTypePtr() const; // static methods static AronObjectTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronObjectTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations virtual void addAcceptedType(const std::string&, const AronTypeNavigatorPtr&) override; - virtual void setAcceptedType(const AronTypeNavigatorPtr&) override; - - virtual std::map<std::string, AronTypeNavigatorPtr> getAcceptedTypes() const override; - virtual AronTypeNavigatorPtr getAcceptedType() const override; virtual type::AronTypePtr getResult() const override; virtual std::string getName() const override; private: // members - std::map<std::string, AronTypeNavigatorPtr> memberNavigators; AronObjectTypeNavigatorPtr extends; type::AronObjectTypePtr type; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.cpp index c165e101a6aead04881b1a3d74c2c0f7b50f28b3..1272f55361c90c96b948da56409802b595509976 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.cpp @@ -57,7 +57,7 @@ namespace armarx CheckAronPtrForNull("AronOpenCVMatTypeNavigator", "AronOpenCVMatTypeNavigator", getPath(), o); } - type::AronOpenCVMatTypePtr AronOpenCVMatTypeNavigator::getCastedResult() const + type::AronOpenCVMatTypePtr AronOpenCVMatTypeNavigator::toAronOpenCVMatTypePtr() const { if (type->dimensions.empty()) { @@ -124,17 +124,22 @@ namespace armarx // static methods AronOpenCVMatTypeNavigatorPtr AronOpenCVMatTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) + { + return std::dynamic_pointer_cast<AronOpenCVMatTypeNavigator>(n); + } + + AronOpenCVMatTypeNavigatorPtr AronOpenCVMatTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) { CheckTypeNavigatorPtrForNull("AronOpenCVMatTypeNavigator", "DynamicCast[Before]", n); - AronOpenCVMatTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronOpenCVMatTypeNavigator>(n); - CheckTypeNavigatorPtrForNull("AronOpenCVMatTypeNavigator", "DynamicCast[After]", casted); + AronOpenCVMatTypeNavigatorPtr casted = AronOpenCVMatTypeNavigator::DynamicCast(n); + CheckTypeNavigatorPtrForNull("AronOpenCVMatTypeNavigator", "DynamicCast[After]", n->getPath(), casted); return casted; } // virtual implementations type::AronTypePtr AronOpenCVMatTypeNavigator::getResult() const { - return getCastedResult(); + return toAronOpenCVMatTypePtr(); } std::string AronOpenCVMatTypeNavigator::getName() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.h index 2af188f766ff93c78060ec63841210542fe4ed1c..5aca82d138a4fb445b4cfb1595824dba5aea7d8b 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronOpenCVMatTypeNavigator.h @@ -52,10 +52,11 @@ namespace armarx AronOpenCVMatTypeNavigator(const AronPath& path); AronOpenCVMatTypeNavigator(const type::AronOpenCVMatTypePtr&, const AronPath& path); - type::AronOpenCVMatTypePtr getCastedResult() const; + type::AronOpenCVMatTypePtr toAronOpenCVMatTypePtr() const; // static methods static AronOpenCVMatTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronOpenCVMatTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations virtual void setDimensions(const std::vector<int>&) override; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.cpp index a1bf3e4e17eecbd8599d61c02cdfc8f3dff2e37a..07e57afe18380804569aed5a76804cc4771752cc 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.cpp @@ -57,7 +57,7 @@ namespace armarx CheckAronPtrForNull("AronPCLPointCloudTypeNavigator", "AronPCLPointCloudTypeNavigator", getPath(), o); } - type::AronPCLPointCloudTypePtr AronPCLPointCloudTypeNavigator::getCastedResult() const + type::AronPCLPointCloudTypePtr AronPCLPointCloudTypeNavigator::toAronPCLPointCloudTypePtr() const { if (type->dimensions.size() != 2) { @@ -145,17 +145,22 @@ namespace armarx // static methods AronPCLPointCloudTypeNavigatorPtr AronPCLPointCloudTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) + { + return std::dynamic_pointer_cast<AronPCLPointCloudTypeNavigator>(n); + } + + AronPCLPointCloudTypeNavigatorPtr AronPCLPointCloudTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) { CheckTypeNavigatorPtrForNull("AronPCLPointCloudTypeNavigator", "DynamicCast[Before]", n); - AronPCLPointCloudTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronPCLPointCloudTypeNavigator>(n); - CheckTypeNavigatorPtrForNull("AronPCLPointCloudTypeNavigator", "DynamicCast[After]", casted); + AronPCLPointCloudTypeNavigatorPtr casted = AronPCLPointCloudTypeNavigator::DynamicCast(n); + CheckTypeNavigatorPtrForNull("AronPCLPointCloudTypeNavigator", "DynamicCast[After]", n->getPath(), casted); return casted; } // virtual implementations type::AronTypePtr AronPCLPointCloudTypeNavigator::getResult() const { - return getCastedResult(); + return toAronPCLPointCloudTypePtr(); } std::string AronPCLPointCloudTypeNavigator::getName() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.h index ae22bfcae6d91ce75c9a193e1ce6b581fd65d470..0ed1bb7b19a79df7ac7896623cccb59f8797f426 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPCLPointCloudTypeNavigator.h @@ -58,10 +58,11 @@ namespace armarx void setWidth(const unsigned int&); void setHeight(const unsigned int&); - type::AronPCLPointCloudTypePtr getCastedResult() const; + type::AronPCLPointCloudTypePtr toAronPCLPointCloudTypePtr() const; // static methods static AronPCLPointCloudTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronPCLPointCloudTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations virtual void setDimensions(const std::vector<int>&) override; diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.cpp index d3eb95da63885fc84b253cd57f83afaae74f7867..559af3fcc77bcaf15a0dab1718d165f597b03198 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.cpp @@ -55,24 +55,29 @@ namespace armarx CheckAronPtrForNull("Aron" + std::string(#upperType) + "TypeNavigator", "Aron" + std::string(#upperType) + "TypeNavigator", getPath(), o); \ } \ \ - type::Aron##upperType##TypePtr Aron##upperType##TypeNavigator::getCastedResult() const \ + type::Aron##upperType##TypePtr Aron##upperType##TypeNavigator::toAron##upperType##TypePtr() const \ { \ return type; \ } \ \ /* static methods */ \ - Aron##upperType##TypeNavigatorPtr Aron##upperType##TypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) \ + Aron##upperType##TypeNavigatorPtr Aron##upperType##TypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) \ {\ CheckTypeNavigatorPtrForNull("Aron" + std::string(#upperType) + "TypeNavigator", "DynamicCast[Before]", n); \ - Aron##upperType##TypeNavigatorPtr casted = std::dynamic_pointer_cast<Aron##upperType##TypeNavigator>(n); \ - CheckTypeNavigatorPtrForNull("Aron" + std::string(#upperType) + "TypeNavigator", "DynamicCast[After]", casted); \ + Aron##upperType##TypeNavigatorPtr casted = Aron##upperType##TypeNavigator::DynamicCast(n); \ + CheckTypeNavigatorPtrForNull("Aron" + std::string(#upperType) + "TypeNavigator", "DynamicCast[After]", n->getPath(), casted); \ return casted; \ }\ \ + Aron##upperType##TypeNavigatorPtr Aron##upperType##TypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) \ + {\ + return std::dynamic_pointer_cast<Aron##upperType##TypeNavigator>(n); \ + }\ + \ /* virtual implementations */\ type::AronTypePtr Aron##upperType##TypeNavigator::getResult() const\ {\ - return getCastedResult(); \ + return toAron##upperType##TypePtr(); \ }\ \ std::string Aron##upperType##TypeNavigator::getName() const \ diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.h index deaebb49c9bd8865bb8aa314eff721f9cec15927..e4770a328cc23c6e2fb64850c865a27efb086ec4 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronPrimitiveTypeNavigator.h @@ -54,10 +54,11 @@ namespace armarx Aron##upperType##TypeNavigator(const AronPath&); \ Aron##upperType##TypeNavigator(const type::Aron##upperType##TypePtr&, const AronPath&); \ \ - type::Aron##upperType##TypePtr getCastedResult() const; \ + type::Aron##upperType##TypePtr toAron##upperType##TypePtr() const; \ \ /* static methods */ \ static Aron##upperType##TypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr&); \ + static Aron##upperType##TypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); \ \ /* virtual implementations */ \ virtual type::AronTypePtr getResult() const override; \ diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.cpp index b0b463456fff30d59b692099bb291ed389f734d3..a58ea955e6b80287c79934bd6a73486c67f99a4a 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.cpp @@ -47,7 +47,7 @@ namespace armarx CheckAronPtrForNull("AronTupleTypeNavigator", "AronTupleTypeNavigator", getPath(), o); } - type::AronTupleTypePtr AronTupleTypeNavigator::getCastedResult() const + type::AronTupleTypePtr AronTupleTypeNavigator::toAronTupleTypePtr() const { if (acceptedTypeNavigators.empty()) { @@ -59,23 +59,18 @@ namespace armarx // static methods AronTupleTypeNavigatorPtr AronTupleTypeNavigator::DynamicCast(const AronTypeNavigatorPtr& n) { - CheckTypeNavigatorPtrForNull("AronTupleTypeNavigator", "DynamicCast[Before]", n); - AronTupleTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronTupleTypeNavigator>(n); - CheckTypeNavigatorPtrForNull("AronTupleTypeNavigator", "DynamicCast[After]", casted); - return casted; - } - - // public member functions - void AronTupleTypeNavigator::setAcceptedType(const AronTypeNavigatorPtr&) - { - throw exception::AronExceptionWithPathInfo("AronTupleTypeNavigator", "setAcceptedType", "Called invalid function!", getPath()); + return std::dynamic_pointer_cast<AronTupleTypeNavigator>(n); } - AronTypeNavigatorPtr AronTupleTypeNavigator::getAcceptedType() const + AronTupleTypeNavigatorPtr AronTupleTypeNavigator::DynamicCastAndCheck(const AronTypeNavigatorPtr& n) { - throw exception::AronExceptionWithPathInfo("AronTupleTypeNavigator", "getAcceptedType", "Called invalid function!", getPath()); + CheckTypeNavigatorPtrForNull("AronTupleTypeNavigator", "DynamicCast[Before]", n); + AronTupleTypeNavigatorPtr casted = AronTupleTypeNavigator::DynamicCast(n); + CheckTypeNavigatorPtrForNull("AronTupleTypeNavigator", "DynamicCast[After]", n->getPath(), casted); + return casted; } + // public member functions void AronTupleTypeNavigator::addAcceptedType(const AronTypeNavigatorPtr& v) { CheckTypeNavigatorPtrForNull("AronTupleTypeNavigator", "addAcceptedType", getPath(), v); @@ -83,15 +78,10 @@ namespace armarx acceptedTypeNavigators.push_back(v); } - std::vector<AronTypeNavigatorPtr> AronTupleTypeNavigator::getAcceptedTypes() const - { - return acceptedTypeNavigators; - } - // virtual implementations type::AronTypePtr AronTupleTypeNavigator::getResult() const { - return getCastedResult(); + return toAronTupleTypePtr(); } std::string AronTupleTypeNavigator::getName() const diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.h index 0435f61c89cf8c60ecf7135a7d85cbc6f4dfe402..785438e1d7ca42c659460cf3508495498f54360a 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTupleTypeNavigator.h @@ -41,6 +41,7 @@ namespace armarx typedef std::shared_ptr<AronTupleTypeNavigator> AronTupleTypeNavigatorPtr; class AronTupleTypeNavigator : + virtual public AronMultipleListAcceptedTypeHavingTypeNavigator, virtual public AronListSerializerTypeNavigator { public: @@ -52,24 +53,19 @@ namespace armarx AronTupleTypeNavigator(const AronPath& path); AronTupleTypeNavigator(const type::AronTupleTypePtr&, const AronPath& path); - type::AronTupleTypePtr getCastedResult() const; + type::AronTupleTypePtr toAronTupleTypePtr() const; // static methods static AronTupleTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n); + static AronTupleTypeNavigatorPtr DynamicCastAndCheck(const AronTypeNavigatorPtr& n); // virtual implementations - virtual void setAcceptedType(const AronTypeNavigatorPtr&) override; - virtual AronTypeNavigatorPtr getAcceptedType() const override; - virtual void addAcceptedType(const AronTypeNavigatorPtr&) override; - virtual std::vector<AronTypeNavigatorPtr> getAcceptedTypes() const override; virtual type::AronTypePtr getResult() const override; virtual std::string getName() const override; private: - // members - std::vector<AronTypeNavigatorPtr> acceptedTypeNavigators; type::AronTupleTypePtr type; }; } diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.cpp index 8d139e9c104fbcc308395a15a56130452c9cde03..1335da5e78177acfe9d0ef940a1f08064e13190e 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.cpp @@ -36,6 +36,7 @@ namespace armarx { namespace typenavigator { + /* AronTypeNavigator */ // static data members const AronTypeNavigatorFactoryPtr AronTypeNavigator::FACTORY = AronTypeNavigatorFactoryPtr(new AronTypeNavigatorFactory()); @@ -65,6 +66,80 @@ namespace armarx throw exception::AronExceptionWithPathInfo(c, m, "Could not cast an AronTypeNavigatorPtr. The Ptr was NULL.", p); } } + + /* AronSingleAcceptedTypeHavingTypeNavigator */ + AronTypeNavigatorPtr AronSingleAcceptedTypeHavingTypeNavigator::getAcceptedType() const + { + return acceptedTypeNavigator; + } + + size_t AronSingleAcceptedTypeHavingTypeNavigator::childrenSize() const + { + return 1; + } + + /* AronMultipleListAcceptedTypeHavingTypeNavigator */ + std::vector<AronTypeNavigatorPtr> AronMultipleListAcceptedTypeHavingTypeNavigator::getAcceptedTypes() const + { + return acceptedTypeNavigators; + } + + AronTypeNavigatorPtr AronMultipleListAcceptedTypeHavingTypeNavigator::getAcceptedType(unsigned int i) const + { + if (i >= acceptedTypeNavigators.size()) + { + throw exception::AronExceptionWithPathInfo("AronMultipleListAcceptedTypeHavingTypeNavigator", "getAcceptedType", "Index out of bounds for getting accepted type.", getPath()); + } + return acceptedTypeNavigators[i]; + } + + size_t AronMultipleListAcceptedTypeHavingTypeNavigator::childrenSize() const + { + return acceptedTypeNavigators.size(); + } + + /* AronMultipleDictAcceptedTypeHavingTypeNavigator */ + std::map<std::string, AronTypeNavigatorPtr> AronMultipleDictAcceptedTypeHavingTypeNavigator::getAcceptedTypes() const + { + return acceptedTypeNavigators; + } + + AronTypeNavigatorPtr AronMultipleDictAcceptedTypeHavingTypeNavigator::getAcceptedType(const std::string& s) const + { + if (acceptedTypeNavigators.find(s) == acceptedTypeNavigators.end()) + { + throw exception::AronExceptionWithPathInfo("AronMultipleDictAcceptedTypeHavingTypeNavigator", "getAcceptedType", "Could not resolve key in accepted types.", getPath()); + } + return acceptedTypeNavigators.at(s); + } + + size_t AronMultipleDictAcceptedTypeHavingTypeNavigator::childrenSize() const + { + return acceptedTypeNavigators.size(); + } + + std::vector<std::string> AronMultipleDictAcceptedTypeHavingTypeNavigator::getAllKeys() const + { + std::vector<std::string> ret; + for (const auto& [key, _] : acceptedTypeNavigators) + { + ret.push_back(key); + } + return ret; + } + + /* AronComplexTypeNavigator */ + size_t AronComplexTypeNavigator::childrenSize() const + { + return 0; + } + + /* AronPrimitiveTypeNavigator */ + size_t AronPrimitiveTypeNavigator::childrenSize() const + { + return 0; + } + } } } diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.h b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.h index 6de37ba39979f6cb80485766a2946c5a3556c200..cd16a8f86387f67771366161140864af083aa6e4 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.h +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigator.h @@ -46,9 +46,6 @@ namespace armarx class AronTypeNavigatorFactory; typedef std::shared_ptr<AronTypeNavigatorFactory> AronTypeNavigatorFactoryPtr; - class AronObjectTypeNavigator; - typedef std::shared_ptr<AronObjectTypeNavigator> AronObjectTypeNavigatorPtr; - class AronTypeNavigator; typedef std::shared_ptr<AronTypeNavigator> AronTypeNavigatorPtr; @@ -66,14 +63,21 @@ namespace armarx // virtual methods virtual type::AronTypePtr getResult() const override = 0; virtual std::string getName() const override = 0; + virtual size_t childrenSize() const = 0; // static methods static AronTypeNavigatorPtr FromAronType(const type::AronTypePtr&, const AronPath& = AronPath()); - template<typename AronAbstractTypeNavigatorClass> - static typename AronAbstractTypeNavigatorClass::PointerType DynamicCast(const AronTypeNavigatorPtr& n) + template<typename AronNavigatorType> + static typename AronNavigatorType::PointerType DynamicCast(const AronTypeNavigatorPtr& n) { - return AronAbstractTypeNavigatorClass::DynamicCast(n); + return AronNavigatorType::DynamicCast(n); + } + + template<typename AronNavigatorType> + static typename AronNavigatorType::PointerType DynamicCastAndCheck(const AronTypeNavigatorPtr& n) + { + return AronNavigatorType::DynamicCastAndCheck(n); } protected: @@ -85,6 +89,7 @@ namespace armarx }; + /* More hierarchy */ class AronContainerTypeNavigator; typedef std::shared_ptr<AronContainerTypeNavigator> AronContainerTypeNavigatorPtr; @@ -94,6 +99,7 @@ namespace armarx public: using PointerType = AronContainerTypeNavigatorPtr; + public: // static methods static AronContainerTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) { @@ -103,23 +109,106 @@ namespace armarx }; - class AronDictSerializerTypeNavigator; - typedef std::shared_ptr<AronDictSerializerTypeNavigator> AronDictSerializerTypeNavigatorPtr; - class AronDictSerializerTypeNavigator : + class AronSingleAcceptedTypeHavingTypeNavigator; + typedef std::shared_ptr<AronSingleAcceptedTypeHavingTypeNavigator> AronSingleAcceptedTypeHavingTypeNavigatorPtr; + + class AronSingleAcceptedTypeHavingTypeNavigator : virtual public AronContainerTypeNavigator { public: - using PointerType = AronDictSerializerTypeNavigatorPtr; + using PointerType = AronSingleAcceptedTypeHavingTypeNavigatorPtr; public: // virtual methods virtual void setAcceptedType(const AronTypeNavigatorPtr&) = 0; + virtual AronTypeNavigatorPtr getAcceptedType() const; + + virtual size_t childrenSize() const override; + + + // static methods + static AronSingleAcceptedTypeHavingTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) + { + AronSingleAcceptedTypeHavingTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronSingleAcceptedTypeHavingTypeNavigator>(n); + return casted; + } + protected: + AronTypeNavigatorPtr acceptedTypeNavigator; + }; + + + + class AronMultipleListAcceptedTypeHavingTypeNavigator; + typedef std::shared_ptr<AronMultipleListAcceptedTypeHavingTypeNavigator> AronMultipleListAcceptedTypeHavingTypeNavigatorPtr; + + class AronMultipleListAcceptedTypeHavingTypeNavigator : + virtual public AronContainerTypeNavigator + { + public: + using PointerType = AronMultipleListAcceptedTypeHavingTypeNavigatorPtr; + + public: + virtual void addAcceptedType(const AronTypeNavigatorPtr&) = 0; + virtual std::vector<AronTypeNavigatorPtr> getAcceptedTypes() const; + virtual AronTypeNavigatorPtr getAcceptedType(unsigned int) const; + + virtual size_t childrenSize() const override; + + // static methods + static AronMultipleListAcceptedTypeHavingTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) + { + AronMultipleListAcceptedTypeHavingTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronMultipleListAcceptedTypeHavingTypeNavigator>(n); + return casted; + } + protected: + // members + std::vector<AronTypeNavigatorPtr> acceptedTypeNavigators; + }; + + + + class AronMultipleDictAcceptedTypeHavingTypeNavigator; + typedef std::shared_ptr<AronMultipleDictAcceptedTypeHavingTypeNavigator> AronMultipleDictAcceptedTypeHavingTypeNavigatorPtr; + + class AronMultipleDictAcceptedTypeHavingTypeNavigator : + virtual public AronContainerTypeNavigator + { + public: + using PointerType = AronMultipleDictAcceptedTypeHavingTypeNavigatorPtr; + + public: virtual void addAcceptedType(const std::string&, const AronTypeNavigatorPtr&) = 0; + virtual std::map<std::string, AronTypeNavigatorPtr> getAcceptedTypes() const; + virtual AronTypeNavigatorPtr getAcceptedType(const std::string&) const; + + virtual size_t childrenSize() const override; + + std::vector<std::string> getAllKeys() const; + + // static methods + static AronMultipleDictAcceptedTypeHavingTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) + { + AronMultipleDictAcceptedTypeHavingTypeNavigatorPtr casted = std::dynamic_pointer_cast<AronMultipleDictAcceptedTypeHavingTypeNavigator>(n); + return casted; + } + protected: + // members + std::map<std::string, AronTypeNavigatorPtr> acceptedTypeNavigators; + }; + + - virtual AronTypeNavigatorPtr getAcceptedType() const = 0; - virtual std::map<std::string, AronTypeNavigatorPtr> getAcceptedTypes() const = 0; + class AronDictSerializerTypeNavigator; + typedef std::shared_ptr<AronDictSerializerTypeNavigator> AronDictSerializerTypeNavigatorPtr; + + class AronDictSerializerTypeNavigator : + virtual public AronContainerTypeNavigator + { + public: + using PointerType = AronDictSerializerTypeNavigatorPtr; + public: // static methods static AronDictSerializerTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) { @@ -140,13 +229,6 @@ namespace armarx using PointerType = AronListSerializerTypeNavigatorPtr; public: - // virtual methods - virtual void setAcceptedType(const AronTypeNavigatorPtr&) = 0; - virtual void addAcceptedType(const AronTypeNavigatorPtr&) = 0; - - virtual AronTypeNavigatorPtr getAcceptedType() const = 0; - virtual std::vector<AronTypeNavigatorPtr> getAcceptedTypes() const = 0; - // static methods static AronListSerializerTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) { @@ -156,6 +238,7 @@ namespace armarx }; + class AronComplexTypeNavigator; typedef std::shared_ptr<AronComplexTypeNavigator> AronComplexTypeNavigatorPtr; @@ -165,6 +248,8 @@ namespace armarx public: using PointerType = AronComplexTypeNavigatorPtr; + virtual size_t childrenSize() const override; + // static methods static AronComplexTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) { @@ -174,6 +259,7 @@ namespace armarx }; + class AronNDArraySerializerTypeNavigator; typedef std::shared_ptr<AronNDArraySerializerTypeNavigator> AronNDArraySerializerTypeNavigatorPtr; @@ -200,6 +286,7 @@ namespace armarx }; + class AronPrimitiveTypeNavigator; typedef std::shared_ptr<AronPrimitiveTypeNavigator> AronPrimitiveTypeNavigatorPtr; @@ -209,6 +296,8 @@ namespace armarx public: using PointerType = AronPrimitiveTypeNavigatorPtr; + virtual size_t childrenSize() const override; + // static methods static AronPrimitiveTypeNavigatorPtr DynamicCast(const AronTypeNavigatorPtr& n) { diff --git a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigatorFactory.cpp b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigatorFactory.cpp index b7f17ccbdcc0006809175c0748c7cad0b5933543..139a9e146429c4c1362371d65c2e3cabf8385172 100644 --- a/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigatorFactory.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/navigators/typenavigator/AronTypeNavigatorFactory.cpp @@ -37,7 +37,7 @@ namespace armarx const std::map<AronTypeDescriptor, AronTypeNavigatorFactoryPtr> AronTypeNavigatorFactory::FACTORIES = { #define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - {AronResolver::GetTypeDescriptorForAronTypeId(typeid(type::Aron##upperType##Type)), AronTypeNavigatorFactoryPtr(new Aron##upperType##TypeNavigatorFactory())}, \ + {AronResolver::GetDescriptorForAronTypeId(typeid(type::Aron##upperType##Type)), AronTypeNavigatorFactoryPtr(new Aron##upperType##TypeNavigatorFactory())}, \ HANDLE_CONTAINER_TYPES HANDLE_COMPLEX_TYPES @@ -50,7 +50,7 @@ namespace armarx { CheckIfInputIsNull("AronTypeNavigatorFactory", "create", path, aron); - auto typeDescriptor = AronResolver::GetTypeDescriptorForAronType(aron); + auto typeDescriptor = AronResolver::GetDescriptorForAronType(aron); auto factory_iterator = FACTORIES.find(typeDescriptor); if (factory_iterator == FACTORIES.end()) { diff --git a/source/RobotAPI/libraries/aron/aroncore/test/aronTest.cpp b/source/RobotAPI/libraries/aron/aroncore/test/aronTest.cpp index 14bd92cebc7f196c966c4d6c70643c5fae12896f..a4bb8061f36c6b6f63d8ecde6c13c25695725652 100644 --- a/source/RobotAPI/libraries/aron/aroncore/test/aronTest.cpp +++ b/source/RobotAPI/libraries/aron/aroncore/test/aronTest.cpp @@ -56,6 +56,7 @@ // Aron #include <RobotAPI/libraries/aron/aroncore/AronDebug.h> +#include <RobotAPI/libraries/aron/aroncore/AronRandomizer.h> #include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/classReaders/NavigatorReader/AronDataNavigatorReader.h> #include <RobotAPI/libraries/aron/aroncore/io/AronDataIO/classWriters/NavigatorWriter/AronDataNavigatorWriter.h> @@ -81,316 +82,28 @@ using namespace armarx; using namespace aron; -void initialize_random() -{ - std::srand(std::time(nullptr)); -} - -int generateRandom(int max, int min) -{ - max += 1; - int random = (std::rand() % (max - min)) + min; - return random; -} - -std::string generateRandomWord() -{ - std::vector<string> words = - { - "Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr" - "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et" - "dolore", "magna", "aliquyam", "eratsed" - }; - - int i = generateRandom(words.size() - 1, 0); - return words[i]; -} - -std::vector<unsigned char> generateRandomBlob(unsigned int size) -{ - std::vector<unsigned char> new_blob(size, 0); - for (unsigned int i = 0; i < size; ++i) - { - new_blob[i] = (generateRandom(127, 0)); - } - return new_blob; -} - -datanavigator::AronDataNavigatorPtr generateAronDataFromType(const typenavigator::AronTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - const AronTypeDescriptor desc = type->getDescriptor(); - switch (desc) - { - case eAronObjectType: - { - typenavigator::AronObjectTypeNavigatorPtr t = typenavigator::AronObjectTypeNavigator::DynamicCast(type); - BOOST_CHECK_NE(t, nullptr); - BOOST_CHECK_NE(t.get(), nullptr); - - datanavigator::AronDictDataNavigatorPtr d = datanavigator::AronDictDataNavigatorPtr(new datanavigator::AronDictDataNavigator()); - for (const auto& [k, tt] : t->getAcceptedTypes()) - { - d->addElement(k, generateAronDataFromType(tt)); - } - return d; - } - case eAronDictType: - { - typenavigator::AronDictTypeNavigatorPtr t = typenavigator::AronDictTypeNavigator::DynamicCast(type); - BOOST_CHECK_NE(t, nullptr); - BOOST_CHECK_NE(t.get(), nullptr); - - return datanavigator::AronDataNavigatorPtr(new datanavigator::AronDictDataNavigator()); - } -#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - case eAron##upperType##Type: \ - { \ - typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCast(type); \ - BOOST_CHECK_NE(t, nullptr); \ - BOOST_CHECK_NE(t.get(), nullptr); \ - \ - return datanavigator::AronDataNavigatorPtr(new datanavigator::AronListDataNavigator()); \ - } - - HANDLE_LIST_SERIALIZER_TYPES -#undef RUN_ARON_MACRO - -#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - case eAron##upperType##Type: \ - { \ - typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCast(type); \ - BOOST_CHECK_NE(t, nullptr); \ - BOOST_CHECK_NE(t.get(), nullptr); \ - \ - return datanavigator::AronDataNavigatorPtr(new datanavigator::AronNDArrayDataNavigator()); \ - } - - HANDLE_COMPLEX_TYPES -#undef RUN_ARON_MACRO - -#define RUN_ARON_MACRO(upperType, lowerType, capsType) \ - case eAron##upperType##Type: \ - { \ - typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCast(type); \ - BOOST_CHECK_NE(t, nullptr); \ - BOOST_CHECK_NE(t.get(), nullptr); \ - \ - return datanavigator::AronDataNavigatorPtr(new datanavigator::Aron##upperType##DataNavigator()); \ - } - - HANDLE_PRIMITIVE_TYPES -#undef RUN_ARON_MACRO - default: - { - throw armarx::aron::exception::AronTypeDescriptorNotValidException("AronTest", "generateAronDataFromType", "No valid type found!", desc); - } - } -} - -// Prototype recursion -void initializeRandomly(datanavigator::AronDataNavigatorPtr& data, const typenavigator::AronTypeNavigatorPtr& type); - -void initializeRandomly(datanavigator::AronDictDataNavigatorPtr& data, const typenavigator::AronObjectTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - for (auto& [key, nextData] : data->getElements()) - { - initializeRandomly(nextData, type->getAcceptedTypes()[key]); - } -} - -void initializeRandomly(datanavigator::AronDictDataNavigatorPtr& data, const typenavigator::AronDictTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->clear(); - int numElements = generateRandom(5, 1); - std::set<std::string> usedKeys; - for (int i = 0; i < numElements; ++i) - { - std::string key = generateRandomWord(); - while (usedKeys.count(key) > 0) - { - key = generateRandomWord(); - } - usedKeys.insert(key); - datanavigator::AronDataNavigatorPtr newData = generateAronDataFromType(type->getAcceptedType()); - initializeRandomly(newData, type->getAcceptedType()); - data->addElement(key, newData); - } -} - -void initializeRandomly(datanavigator::AronListDataNavigatorPtr& data, const typenavigator::AronTupleTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - unsigned int i = 0; - for (auto& nextData : data->getElements()) - { - initializeRandomly(nextData, type->getAcceptedTypes()[i++]); - } -} - -void initializeRandomly(datanavigator::AronListDataNavigatorPtr& data, const typenavigator::AronListTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->clear(); - int numElements = generateRandom(5, 1); - for (int i = 0; i < numElements; ++i) - { - datanavigator::AronDataNavigatorPtr newData = generateAronDataFromType(type->getAcceptedType()); - initializeRandomly(newData, type->getAcceptedType()); - data->addElement(newData); - } -} - -void initializeRandomly(datanavigator::AronNDArrayDataNavigatorPtr& data, const typenavigator::AronNDArraySerializerTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - std::vector<int> dims = data->getDimensions(); - int bytes = std::accumulate(std::begin(dims), std::end(dims), 1, std::multiplies<int>()); - std::vector<unsigned char> blob = generateRandomBlob(bytes); - data->setData(bytes, blob.data()); -} - -void initializeRandomly(datanavigator::AronIntDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->setValue(generateRandom(32, -32)); -} - -void initializeRandomly(datanavigator::AronLongDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->setValue(generateRandom(32, -32)); -} - -void initializeRandomly(datanavigator::AronFloatDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->setValue(generateRandom(32, -32)); -} - -void initializeRandomly(datanavigator::AronDoubleDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->setValue(generateRandom(32, -32)); -} - -void initializeRandomly(datanavigator::AronBoolDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->setValue(generateRandom(1, 0)); -} - -void initializeRandomly(datanavigator::AronStringDataNavigatorPtr& data, const typenavigator::AronPrimitiveTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - data->setValue(generateRandomWord()); -} - -void initializeRandomly(datanavigator::AronDataNavigatorPtr& data, const typenavigator::AronTypeNavigatorPtr& type) -{ - BOOST_CHECK_NE(data, nullptr); - BOOST_CHECK_NE(type, nullptr); - BOOST_CHECK_NE(data.get(), nullptr); - BOOST_CHECK_NE(type.get(), nullptr); - - // Containers - AronTypeDescriptor desc = type->getDescriptor(); - switch (desc) - { - -#define RUN_ARON_MACRO(upperType, lowerType, capsType, upperData, lowerData, capsData) \ - case eAron##upperType##Type: \ - { \ - typenavigator::Aron##upperType##TypeNavigatorPtr t = typenavigator::Aron##upperType##TypeNavigator::DynamicCast(type); \ - BOOST_CHECK_NE(t, nullptr); \ - BOOST_CHECK_NE(t.get(), nullptr); \ - \ - datanavigator::Aron##upperData##DataNavigatorPtr d = datanavigator::Aron##upperData##DataNavigator::DynamicCast(data); \ - BOOST_CHECK_NE(d, nullptr); \ - BOOST_CHECK_NE(d.get(), nullptr); \ - \ - initializeRandomly(d, t); \ - break; \ - } - - HANDLE_ALL_CORRESPONDING -#undef RUN_ARON_MACRO - - default: - { - throw armarx::aron::exception::AronTypeDescriptorNotValidException("AronTest", "initializeRandomly", "No valid type found!", desc); - } - } -} - template <typename T> void runTestWithInstances(T& k1, T& k2) { + AronRandomizer r; + std::cout << "\t getting type 1" << std::endl; typenavigator::AronObjectTypeNavigatorPtr k1_type_nav = k1.toInitialAronType(); - type::AronObjectTypePtr k1_type = k1_type_nav->getCastedResult(); + type::AronObjectTypePtr k1_type = k1_type_nav->toAronObjectTypePtr(); BOOST_CHECK_NE(k1_type.get(), nullptr); std::cout << "\t getting type 2" << std::endl; typenavigator::AronObjectTypeNavigatorPtr k2_type_nav = k2.toInitialAronType(); - type::AronObjectTypePtr k2_type = k2_type_nav->getCastedResult(); + type::AronObjectTypePtr k2_type = k2_type_nav->toAronObjectTypePtr(); BOOST_CHECK_NE(k2_type.get(), nullptr); std::cout << "\t getting aron 1" << std::endl; datanavigator::AronDictDataNavigatorPtr k1_aron_nav = k1.toAron(); - data::AronDictPtr k1_aron = k1_aron_nav->getCastedResult(); + data::AronDictPtr k1_aron = k1_aron_nav->toAronDictPtr(); BOOST_CHECK_NE(k1_aron.get(), nullptr); std::cout << "\t initialize aron 1 randomly" << std::endl; - initializeRandomly(k1_aron_nav, k1_type_nav); + r.initializeRandomly(k1_aron_nav, k1_type_nav); //std::cout << "K Aron:" << std::endl; //std::cout << AronDebug::AronDataPtrToString(k_aron) << std::endl; @@ -400,7 +113,7 @@ void runTestWithInstances(T& k1, T& k2) std::cout << "\t getting aron 2" << std::endl; datanavigator::AronDictDataNavigatorPtr k2_aron_nav = k2.toAron(); - data::AronDictPtr k2_aron = k2_aron_nav->getCastedResult(); + data::AronDictPtr k2_aron = k2_aron_nav->toAronDictPtr(); BOOST_CHECK_NE(k2_aron.get(), nullptr); //std::cout << "K2 Aron:" << std::endl; @@ -421,7 +134,7 @@ void runTestWithInstances(T& k1, T& k2) //type::AronTypePtr k2_current_type = k2_current_type_nav->getResult(); //BOOST_CHECK_NE(k2_current_type.get(), nullptr); - initializeRandomly(k1_aron_nav, k1_type_nav); + r.initializeRandomly(k1_aron_nav, k1_type_nav); k1.fromAron(k1_aron); std::cout << "\t check JSON export of k and k2 for equality" << std::endl; @@ -466,8 +179,6 @@ void runTestWithInstances(T& k1, T& k2) BOOST_AUTO_TEST_CASE(AronNaturalIKTest) { - initialize_random(); - std::cout << "Running NaturalIK test" << std::endl; NaturalIKResult k; NaturalIKResult k2; @@ -476,8 +187,6 @@ BOOST_AUTO_TEST_CASE(AronNaturalIKTest) BOOST_AUTO_TEST_CASE(AronListTest) { - initialize_random(); - std::cout << "Running List test" << std::endl; ListTest l; ListTest l2; @@ -486,8 +195,6 @@ BOOST_AUTO_TEST_CASE(AronListTest) BOOST_AUTO_TEST_CASE(AronDictTest) { - initialize_random(); - std::cout << "Running Dict test" << std::endl; DictTest d; DictTest d2; @@ -496,8 +203,6 @@ BOOST_AUTO_TEST_CASE(AronDictTest) BOOST_AUTO_TEST_CASE(AronPrimitiveTest) { - initialize_random(); - std::cout << "Running Primitive test" << std::endl; PrimitiveTest p; PrimitiveTest p2; @@ -506,8 +211,6 @@ BOOST_AUTO_TEST_CASE(AronPrimitiveTest) BOOST_AUTO_TEST_CASE(AronSimpleClassTest) { - initialize_random(); - std::cout << "Running SimpleClass test" << std::endl; SimpleClass o1; SimpleClass o12; @@ -516,8 +219,6 @@ BOOST_AUTO_TEST_CASE(AronSimpleClassTest) BOOST_AUTO_TEST_CASE(AronObjectTest) { - initialize_random(); - std::cout << "Running Object test" << std::endl; ObjectTest o2; ObjectTest o22; @@ -526,8 +227,6 @@ BOOST_AUTO_TEST_CASE(AronObjectTest) BOOST_AUTO_TEST_CASE(AronImageTest) { - initialize_random(); - std::cout << "Running Image test" << std::endl; ImageTest i; ImageTest i2; @@ -536,8 +235,6 @@ BOOST_AUTO_TEST_CASE(AronImageTest) BOOST_AUTO_TEST_CASE(AronImageChangeSizeTest) { - initialize_random(); - std::cout << "Running Image test with changed sizes" << std::endl; ImageTest ii; ImageTest ii2; @@ -548,8 +245,6 @@ BOOST_AUTO_TEST_CASE(AronImageChangeSizeTest) BOOST_AUTO_TEST_CASE(AronEigenMatrixTest) { - initialize_random(); - std::cout << "Running EigenMatrix test" << std::endl; EigenMatrixTest em; EigenMatrixTest em2; @@ -558,8 +253,6 @@ BOOST_AUTO_TEST_CASE(AronEigenMatrixTest) BOOST_AUTO_TEST_CASE(AronOpenCVTest) { - initialize_random(); - std::cout << "Running OpenCVMat test" << std::endl; OpenCVMatTest ocv; OpenCVMatTest ocv2; @@ -568,8 +261,6 @@ BOOST_AUTO_TEST_CASE(AronOpenCVTest) BOOST_AUTO_TEST_CASE(AronPCLPointCloudTest) { - initialize_random(); - std::cout << "Running PCLPointCloud test" << std::endl; PointCloudTest pc; PointCloudTest pc2;