From 97cff91e936c49f6c472e34b4bc312c7412d9afd Mon Sep 17 00:00:00 2001
From: Fabian Peller-Konrad <fabian.peller-konrad@kit.edu>
Date: Fri, 12 Aug 2022 12:57:27 +0200
Subject: [PATCH] added utility for memories to handle empty memory names

---
 .../armem/server/ltm/disk/Memory.cpp          | 23 ++++++++++++++-----
 .../libraries/armem/server/ltm/disk/Memory.h  |  1 +
 .../libraries/armem/server/plugins/Plugin.cpp | 22 ++++++++++++++----
 3 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.cpp b/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.cpp
index bc71c50fc..6d0b4e1fa 100644
--- a/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.cpp
+++ b/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.cpp
@@ -32,6 +32,23 @@ namespace armarx::armem::server::ltm::disk
     {
     }
 
+    void Memory::setMemoryID(const MemoryID &id)
+    {
+        Base::setMemoryID(id);
+        escapedMemoryName = EscapeSegmentName(id.memoryName);
+    }
+    void Memory::setMemoryID(const MemoryID& id, const std::string& componentNameFallback)
+    {
+        MemoryID _id = id;
+        if (id.memoryName.empty())
+        {
+            ARMARX_WARNING << "ATTENTION: A memory id passed to some LTM is empty. Either you used the wrong setter (always use setMemoryName(..) before onInitComponent()) or you emptied the scenario parameter. Using a the component name as fallback.";
+            _id.memoryName = componentNameFallback;
+        }
+
+        setMemoryID(_id);
+    }
+
     void Memory::init()
     {
         Base::init();
@@ -47,12 +64,6 @@ namespace armarx::armem::server::ltm::disk
             maxExportIndex = i;
         }
     }
-    void Memory::setMemoryID(const MemoryID& id)
-    {
-        Base::setMemoryID(id);
-
-        escapedMemoryName = EscapeSegmentName(id.memoryName);
-    }
 
     bool Memory::forEachCoreSegment(std::function<void(CoreSegment&)>&& func) const
     {
diff --git a/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.h b/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.h
index e2f8d4f14..c9f1e95b2 100644
--- a/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.h
+++ b/source/RobotAPI/libraries/armem/server/ltm/disk/Memory.h
@@ -25,6 +25,7 @@ namespace armarx::armem::server::ltm::disk
 
         void init() final;
         void setMemoryID(const MemoryID& id) final;
+        void setMemoryID(const MemoryID& id, const std::string& fallback);
 
         void configure(const nlohmann::json& config) final;
 
diff --git a/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp b/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp
index a0e13eddb..b8b422bff 100644
--- a/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp
+++ b/source/RobotAPI/libraries/armem/server/plugins/Plugin.cpp
@@ -28,7 +28,16 @@ namespace armarx::armem::server::plugins
     void Plugin::postCreatePropertyDefinitions(PropertyDefinitionsPtr& properties)
     {
         const std::string prefix = "mem.";
-        if (not workingMemory.name().empty() and not properties->hasDefinition(prefix + "MemoryName"))
+
+        // set Memory name if not already set
+        if (workingMemory.name().empty())
+        {
+            Component& parent = this->parent<Component>();
+            workingMemory.name() = parent.getName();
+        }
+
+        // also add scenario param to overwrite the chosen name
+        if (not properties->hasDefinition(prefix + "MemoryName"))
         {
             properties->optional(workingMemory.name(), prefix + "MemoryName", "Name of this memory server.");
         }
@@ -43,15 +52,18 @@ namespace armarx::armem::server::plugins
         ARMARX_TRACE;
         memoryTopicName = client::util::MemoryListener::MakeMemoryTopicName(MemoryID(workingMemory.name()));
         parent().offeringTopic(memoryTopicName);
-
-        ARMARX_TRACE;
-        longtermMemory.setMemoryID(workingMemory.id());
-        longtermMemory.init();
     }
 
 
     void Plugin::postOnInitComponent()
     {
+        Component& parent = this->parent<Component>();
+
+        // activate LTM
+        ARMARX_TRACE;
+        longtermMemory.setMemoryID(workingMemory.id(), parent.getName());
+        longtermMemory.init();
+
         initialized = true;
     }
 
-- 
GitLab