From a89f6204a9cb2394b6d28f6380dfbcdff6b97acd Mon Sep 17 00:00:00 2001
From: Fabian Peller-Konrad <fabian.peller-konrad@kit.edu>
Date: Tue, 20 Feb 2024 15:33:54 +0100
Subject: [PATCH] fix loading of ltm from filesystem again

---
 .../GeneralPurposeMemory.cpp                  |  4 +-
 .../armem/server/ltm/CoreSegment.cpp          | 13 +++++
 .../libraries/armem/server/ltm/Memory.cpp     |  8 +--
 .../armem/server/ltm/ProviderSegment.cpp      | 13 +++++
 .../ltm/detail/mixins/util/filesystem.cpp     |  2 +-
 .../libraries/armem_gui/MemoryViewer.cpp      |  3 +-
 .../armem_gui/disk/ControlWidget.cpp          |  4 +-
 .../converter/json/NLohmannJSONConverter.cpp  |  3 +-
 .../aron/core/type/converter/Converter.h      | 38 ++++++++++++-
 .../nlohmannJSON/NlohmannJSONReader.cpp       | 54 ++++++++++++++++---
 10 files changed, 123 insertions(+), 19 deletions(-)

diff --git a/source/RobotAPI/components/armem/server/GeneralPurposeMemory/GeneralPurposeMemory.cpp b/source/RobotAPI/components/armem/server/GeneralPurposeMemory/GeneralPurposeMemory.cpp
index 756ffdcab..12d1848e5 100644
--- a/source/RobotAPI/components/armem/server/GeneralPurposeMemory/GeneralPurposeMemory.cpp
+++ b/source/RobotAPI/components/armem/server/GeneralPurposeMemory/GeneralPurposeMemory.cpp
@@ -39,9 +39,6 @@ namespace armarx
     armarx::PropertyDefinitionsPtr GeneralPurposeMemory::createPropertyDefinitions()
     {
         armarx::PropertyDefinitionsPtr defs = new ComponentPropertyDefinitions(getConfigIdentifier());
-
-        workingMemory().name() = "GeneralPurpose";
-
         return defs;
     }
 
@@ -54,6 +51,7 @@ namespace armarx
 
     void GeneralPurposeMemory::onInitComponent()
     {
+        setMemoryName(getName());
     }
 
 
diff --git a/source/RobotAPI/libraries/armem/server/ltm/CoreSegment.cpp b/source/RobotAPI/libraries/armem/server/ltm/CoreSegment.cpp
index 87575f5d3..6ec04595c 100644
--- a/source/RobotAPI/libraries/armem/server/ltm/CoreSegment.cpp
+++ b/source/RobotAPI/libraries/armem/server/ltm/CoreSegment.cpp
@@ -84,6 +84,19 @@ namespace armarx::armem::server::ltm
 
         e.id() = id().getCoreSegmentID().cleanID();
 
+        ARMARX_INFO << VAROUT(id());
+
+        auto& conv = processors->defaultTypeConverter;
+        if (fileExists(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix))
+        {
+            // load and set type
+            auto& conv = processors->defaultTypeConverter;
+
+            auto filecontent = readDataFromFile(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix);
+            auto aron = conv.convert(filecontent, "");
+            e.aronType() = aron;
+        }
+
         forEachProviderSegment(
             [&e](auto& x)
             {
diff --git a/source/RobotAPI/libraries/armem/server/ltm/Memory.cpp b/source/RobotAPI/libraries/armem/server/ltm/Memory.cpp
index 6ffd3b8c2..bb2fd2ceb 100644
--- a/source/RobotAPI/libraries/armem/server/ltm/Memory.cpp
+++ b/source/RobotAPI/libraries/armem/server/ltm/Memory.cpp
@@ -88,9 +88,7 @@ namespace armarx::armem::server::ltm
         {
             for (const auto& subdirName : getAllDirectories())
             {
-
                 std::string segmentName = util::fs::detail::unescapeName(subdirName);
-                ARMARX_INFO << VAROUT(id().withCoreSegmentName(segmentName));
                 CoreSegment c(getMemoryBasePath(),
                               getSettings(),
                               getExportName(),
@@ -99,6 +97,10 @@ namespace armarx::armem::server::ltm
                 func(c);
             }
         }
+        else
+        {
+            ARMARX_WARNING << "Could not load the core segments of LTM " << id().str() << " as the path " << getFullPath().string() << " does not exist.";
+        }
 
         ARMARX_INFO << "All CoreSegments handeled";
 
@@ -172,7 +174,7 @@ namespace armarx::armem::server::ltm
     {
         std::lock_guard l(ltm_mutex);
 
-        m.id() = id().getMemoryID();
+        m.id() = id().getMemoryID().cleanID();
 
         ARMARX_INFO << VAROUT(id());
 
diff --git a/source/RobotAPI/libraries/armem/server/ltm/ProviderSegment.cpp b/source/RobotAPI/libraries/armem/server/ltm/ProviderSegment.cpp
index cfb613221..983dfb894 100644
--- a/source/RobotAPI/libraries/armem/server/ltm/ProviderSegment.cpp
+++ b/source/RobotAPI/libraries/armem/server/ltm/ProviderSegment.cpp
@@ -108,6 +108,19 @@ namespace armarx::armem::server::ltm
 
         e.id() = id().getProviderSegmentID().cleanID();
 
+        auto& conv = processors->defaultTypeConverter;
+        if (fileExists(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix))
+        {
+            // load and set type
+            auto& conv = processors->defaultTypeConverter;
+
+            auto filecontent = readDataFromFile(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix);
+            auto aron = conv.convert(filecontent, "");
+            e.aronType() = aron;
+        }
+
+        ARMARX_INFO << VAROUT(id());
+
         /*
 
         auto& conv = processors->defaultTypeConverter;
diff --git a/source/RobotAPI/libraries/armem/server/ltm/detail/mixins/util/filesystem.cpp b/source/RobotAPI/libraries/armem/server/ltm/detail/mixins/util/filesystem.cpp
index 0b1f29b70..0e78e8f01 100644
--- a/source/RobotAPI/libraries/armem/server/ltm/detail/mixins/util/filesystem.cpp
+++ b/source/RobotAPI/libraries/armem/server/ltm/detail/mixins/util/filesystem.cpp
@@ -201,7 +201,7 @@ namespace armarx::armem::server::ltm ::util::fs
         std::sort(ret.begin(),
                   ret.end(),
                   [](const std::filesystem::path& a, const std::filesystem::path& b) -> bool
-                  { return a.string() > b.string(); });
+                  { return a.string() < b.string(); });
         return ret;
     }
 
diff --git a/source/RobotAPI/libraries/armem_gui/MemoryViewer.cpp b/source/RobotAPI/libraries/armem_gui/MemoryViewer.cpp
index c84f1a8b9..5cf0df437 100644
--- a/source/RobotAPI/libraries/armem_gui/MemoryViewer.cpp
+++ b/source/RobotAPI/libraries/armem_gui/MemoryViewer.cpp
@@ -441,6 +441,7 @@ namespace armarx::armem::gui
         for (auto& [path, memory] : data)
         {
             std::string name = memory.id().memoryName;
+
             auto commit = armem::toCommit(memory);
 
             if (memoryWriters.count(name) > 0)
@@ -458,7 +459,7 @@ namespace armarx::armem::gui
                 std::string virtualMemoryName = name + " (at " + path.string() + ")";
                 wm::Memory virtualMemory(virtualMemoryName);
                 virtualMemory.update(commit, true, false);
-                memoryData[virtualMemoryName] = virtualMemory;
+                memoryData[virtualMemoryName] = std::move(virtualMemory);
             }
         }
 
diff --git a/source/RobotAPI/libraries/armem_gui/disk/ControlWidget.cpp b/source/RobotAPI/libraries/armem_gui/disk/ControlWidget.cpp
index cb828b64f..868c315ad 100644
--- a/source/RobotAPI/libraries/armem_gui/disk/ControlWidget.cpp
+++ b/source/RobotAPI/libraries/armem_gui/disk/ControlWidget.cpp
@@ -178,10 +178,10 @@ namespace armarx::armem::gui::disk
         {
             if (std::filesystem::is_directory(p))
             {
-                armem::server::ltm::Memory ltm(p.parent_path(), "MemoryExport", p.filename());
+                armem::server::ltm::Memory ltm(p.parent_path().parent_path(), "MemoryExport", p.filename());
                 armem::wm::Memory memory =
                     ltm.loadAllAndResolve(); // load list of references and load data
-                memoryData[p] = memory;
+                memoryData[p] = std::move(memory);
 
                 numLoaded++;
             }
diff --git a/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.cpp b/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.cpp
index c94ff951a..efb14f9aa 100644
--- a/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.cpp
+++ b/source/RobotAPI/libraries/aron/converter/json/NLohmannJSONConverter.cpp
@@ -66,8 +66,7 @@ namespace armarx::aron::type::converter
     AronNlohmannJSONConverter::ConvertFromNlohmannJSONTypeObject(const nlohmann::json& j,
                                                                  const armarx::aron::Path& p)
     {
-        type::ObjectPtr foo = std::make_shared<aron::type::Object>();
-        type::VariantPtr aron = std::make_shared<aron::type::Object>(*foo, p);
+        type::VariantPtr aron = std::make_shared<aron::type::Object>(p);
         ConvertFromNlohmannJSON(aron, j);
         return type::Object::DynamicCastAndCheck(aron);
     }
diff --git a/source/RobotAPI/libraries/aron/core/type/converter/Converter.h b/source/RobotAPI/libraries/aron/core/type/converter/Converter.h
index 7e6f0c84d..d714842d4 100644
--- a/source/RobotAPI/libraries/aron/core/type/converter/Converter.h
+++ b/source/RobotAPI/libraries/aron/core/type/converter/Converter.h
@@ -73,6 +73,8 @@ namespace armarx::aron::type
         void
         visitObject(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitObject";
+
             std::map<std::string, ReaderInputTypeNonConst> elementsOfInput;
             std::string name;
             std::vector<std::string> templates;
@@ -94,6 +96,8 @@ namespace armarx::aron::type
         void
         visitDict(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitDict";
+
             ReaderInputTypeNonConst acceptedType;
             type::Maybe maybe;
             Path p;
@@ -106,7 +110,9 @@ namespace armarx::aron::type
 
         void
         visitList(ReaderInputType& o) final
-        {
+        {            
+            //ARMARX_INFO << "convert visitList";
+
             ReaderInputTypeNonConst acceptedType;
             type::Maybe maybe;
             Path p;
@@ -120,6 +126,8 @@ namespace armarx::aron::type
         void
         visitPair(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitPair";
+
             ReaderInputTypeNonConst acceptedType1;
             ReaderInputTypeNonConst acceptedType2;
             type::Maybe maybe;
@@ -135,6 +143,8 @@ namespace armarx::aron::type
         void
         visitTuple(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitTuple";
+
             std::vector<ReaderInputTypeNonConst> acceptedTypes;
             std::vector<WriterReturnType> elementsReturn;
             type::Maybe maybe;
@@ -153,6 +163,8 @@ namespace armarx::aron::type
         void
         visitNDArray(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitNDArray";
+
             type::Maybe maybe;
             type::ndarray::ElementType type;
             std::string defaultValue;
@@ -165,6 +177,8 @@ namespace armarx::aron::type
         void
         visitMatrix(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitMatrix";
+
             type::Maybe maybe;
             type::matrix::ElementType type;
             std::string defaultValue;
@@ -178,6 +192,8 @@ namespace armarx::aron::type
         void
         visitQuaternion(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitQuaternion";
+
             type::Maybe maybe;
             type::quaternion::ElementType type;
             std::string defaultValue;
@@ -189,6 +205,8 @@ namespace armarx::aron::type
         void
         visitImage(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitImage";
+
             type::Maybe maybe;
             type::image::PixelType type;
             std::string defaultValue;
@@ -200,6 +218,8 @@ namespace armarx::aron::type
         void
         visitPointCloud(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitPointCloud";
+
             type::Maybe maybe;
             type::pointcloud::VoxelType type;
             std::string defaultValue;
@@ -211,6 +231,8 @@ namespace armarx::aron::type
         void
         visitIntEnum(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitIntEnum";
+
             type::Maybe maybe;
             std::string name;
             std::map<std::string, int> values;
@@ -223,6 +245,8 @@ namespace armarx::aron::type
         void
         visitInt(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitInt";
+
             type::Maybe maybe;
             Path p;
             std::optional<int> defaultValue;
@@ -233,6 +257,8 @@ namespace armarx::aron::type
         void
         visitLong(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitLong";
+
             type::Maybe maybe;
             Path p;
             std::optional<long> defaultValue;
@@ -243,6 +269,8 @@ namespace armarx::aron::type
         void
         visitFloat(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitFloat";
+
             type::Maybe maybe;
             Path p;
             std::optional<float> defaultValue;
@@ -253,6 +281,8 @@ namespace armarx::aron::type
         void
         visitDouble(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitDouble";
+
             type::Maybe maybe;
             Path p;
             std::optional<double> defaultValue;
@@ -263,6 +293,8 @@ namespace armarx::aron::type
         void
         visitBool(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitBool";
+
             type::Maybe maybe;
             Path p;
             std::optional<bool> defaultValue;
@@ -273,6 +305,8 @@ namespace armarx::aron::type
         void
         visitString(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitString";
+
             type::Maybe maybe;
             Path p;
             std::optional<std::string> defaultValue;
@@ -283,6 +317,8 @@ namespace armarx::aron::type
         void
         visitUnknown(ReaderInputType& o) final
         {
+            //ARMARX_INFO << "convert visitUnknown";
+
             if (!r.readNull(o))
             {
                 throw error::AronException(__PRETTY_FUNCTION__,
diff --git a/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp b/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp
index 9b0ae2b3e..3421d3854 100644
--- a/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp
+++ b/source/RobotAPI/libraries/aron/core/type/rw/reader/nlohmannJSON/NlohmannJSONReader.cpp
@@ -257,7 +257,14 @@ namespace armarx::aron::type::reader
         getAronMetaInformationForType(input, rw::json::constantes::INT_TYPENAME_SLUG, p);
 
         maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]);
-        defaultValue = input[rw::json::constantes::DEFAULT_SLUG];
+
+        if (input.count(rw::json::constantes::DEFAULT_SLUG) && not input.at(rw::json::constantes::DEFAULT_SLUG).is_null())
+        {
+            if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
+            {
+                defaultValue = std::stoi(d);
+            }
+        }
     }
 
     void
@@ -269,7 +276,14 @@ namespace armarx::aron::type::reader
         getAronMetaInformationForType(input, rw::json::constantes::LONG_TYPENAME_SLUG, p);
 
         maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]);
-        defaultValue = input[rw::json::constantes::DEFAULT_SLUG];
+
+        if (input.count(rw::json::constantes::DEFAULT_SLUG) && not input.at(rw::json::constantes::DEFAULT_SLUG).is_null())
+        {
+            if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
+            {
+                defaultValue = std::stol(d);
+            }
+        }
     }
 
     void
@@ -281,7 +295,14 @@ namespace armarx::aron::type::reader
         getAronMetaInformationForType(input, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
 
         maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]);
-        defaultValue = input[rw::json::constantes::DEFAULT_SLUG];
+
+        if (input.count(rw::json::constantes::DEFAULT_SLUG) && not input.at(rw::json::constantes::DEFAULT_SLUG).is_null())
+        {
+            if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
+            {
+                defaultValue = std::stof(d);
+            }
+        }
     }
 
     void
@@ -293,7 +314,14 @@ namespace armarx::aron::type::reader
         getAronMetaInformationForType(input, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
 
         maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]);
-        defaultValue = input[rw::json::constantes::DEFAULT_SLUG];
+
+        if (input.count(rw::json::constantes::DEFAULT_SLUG) && not input.at(rw::json::constantes::DEFAULT_SLUG).is_null())
+        {
+            if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
+            {
+                defaultValue = std::stod(d);
+            }
+        }
     }
 
     void
@@ -305,7 +333,14 @@ namespace armarx::aron::type::reader
         getAronMetaInformationForType(input, rw::json::constantes::STRING_TYPENAME_SLUG, p);
 
         maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]);
-        defaultValue = input[rw::json::constantes::DEFAULT_SLUG];
+
+        if (input.count(rw::json::constantes::DEFAULT_SLUG) && not input.at(rw::json::constantes::DEFAULT_SLUG).is_null())
+        {
+            if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
+            {
+                defaultValue = d;
+            }
+        }
     }
 
     void
@@ -317,7 +352,14 @@ namespace armarx::aron::type::reader
         getAronMetaInformationForType(input, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
 
         maybe = rw::json::conversion::String2Maybe.at(input[rw::json::constantes::MAYBE_SLUG]);
-        defaultValue = input[rw::json::constantes::DEFAULT_SLUG];
+
+        if (input.count(rw::json::constantes::DEFAULT_SLUG) && not input.at(rw::json::constantes::DEFAULT_SLUG).is_null())
+        {
+            if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
+            {
+                defaultValue = std::stoi(d);
+            }
+        }
     }
 
     void
-- 
GitLab