From 2633f10f6fc1384f34ffbad164b49aed16dbf086 Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Wed, 21 Jul 2021 15:12:23 +0200
Subject: [PATCH] Add SpecializedSegment to reduce duplicated code

---
 .../RobotAPI/libraries/armem/CMakeLists.txt   |  2 +
 .../server/segment/SpecializedSegment.cpp     | 79 +++++++++++++++++++
 .../armem/server/segment/SpecializedSegment.h | 77 ++++++++++++++++++
 3 files changed, 158 insertions(+)
 create mode 100644 source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.cpp
 create mode 100644 source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.h

diff --git a/source/RobotAPI/libraries/armem/CMakeLists.txt b/source/RobotAPI/libraries/armem/CMakeLists.txt
index 7dd0264f1..ae63606b7 100644
--- a/source/RobotAPI/libraries/armem/CMakeLists.txt
+++ b/source/RobotAPI/libraries/armem/CMakeLists.txt
@@ -104,6 +104,7 @@ set(LIB_FILES
     server/MemoryRemoteGui.cpp
     server/RemoteGuiAronDataVisitor.cpp
 
+    server/segment/SpecializedSegment.cpp
     server/query_proc/base/BaseQueryProcessorBase.cpp
     server/query_proc/base/EntityQueryProcessorBase.cpp
     server/query_proc/base/ProviderSegmentQueryProcessorBase.cpp
@@ -225,6 +226,7 @@ set(LIB_HEADERS
     server/MemoryRemoteGui.h
     server/RemoteGuiAronDataVisitor.h
 
+    server/segment/SpecializedSegment.h
     server/query_proc.h
     server/query_proc/base/BaseQueryProcessorBase.h
     server/query_proc/base/EntityQueryProcessorBase.h
diff --git a/source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.cpp b/source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.cpp
new file mode 100644
index 000000000..8184e27c9
--- /dev/null
+++ b/source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.cpp
@@ -0,0 +1,79 @@
+#include "SpecializedSegment.h"
+
+#include <ArmarXCore/core/application/properties/PropertyDefinitionContainer.h>
+#include <ArmarXCore/core/application/properties/PluginAll.h>
+#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
+
+#include <RobotAPI/libraries/aron/core/navigator/type/container/Object.h>
+#include <RobotAPI/libraries/armem/server/MemoryToIceAdapter.h>
+
+
+namespace armarx::armem::server::segment
+{
+
+    SpecializedSegment::SpecializedSegment(
+            server::MemoryToIceAdapter& iceMemory,
+            aron::typenavigator::ObjectNavigatorPtr aronType,
+            const std::string& defaultCoreSegmentName,
+            int64_t defaultMaxHistorySize
+            ) :
+        iceMemory(iceMemory), aronType(aronType)
+    {
+        setDefaultCoreSegmentName(defaultCoreSegmentName);
+        setDefaultMaxHistorySize(defaultMaxHistorySize);
+    }
+
+
+    SpecializedSegment::~SpecializedSegment()
+    {
+    }
+
+
+    void SpecializedSegment::defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string& prefix)
+    {
+        defs->optional(properties.coreSegmentName,
+                       prefix + "seg.CoreSegmentName",
+                       "Name of the " + properties.coreSegmentName + " core segment.");
+
+        defs->optional(properties.maxHistorySize,
+                       prefix + "seg.MaxHistorySize",
+                       "Maximal size of the " + properties.coreSegmentName + " entity histories (-1 for infinite).");
+    }
+
+
+    void SpecializedSegment::init()
+    {
+        ARMARX_CHECK_NOT_NULL(iceMemory.workingMemory);
+        Logging::setTag(properties.coreSegmentName + " Core Segment");
+
+        ARMARX_INFO << "Adding core segment '" << properties.coreSegmentName << "'";
+        coreSegment = &iceMemory.workingMemory->addCoreSegment(properties.coreSegmentName, aronType);
+        coreSegment->setMaxHistorySize(properties.maxHistorySize);
+    }
+
+
+    std::mutex& SpecializedSegment::mutex() const
+    {
+        ARMARX_CHECK_NOT_NULL(coreSegment);
+        return coreSegment->mutex();
+    }
+
+
+    void SpecializedSegment::setDefaultCoreSegmentName(const std::string& coreSegmentName)
+    {
+        this->properties.coreSegmentName = coreSegmentName;
+    }
+
+
+    void SpecializedSegment::setDefaultMaxHistorySize(int64_t maxHistorySize)
+    {
+        this->properties.maxHistorySize = maxHistorySize;
+    }
+
+
+    void SpecializedSegment::setAronType(aron::typenavigator::ObjectNavigatorPtr aronType)
+    {
+        this->aronType = aronType;
+    }
+
+}
diff --git a/source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.h b/source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.h
new file mode 100644
index 000000000..fd3a456c5
--- /dev/null
+++ b/source/RobotAPI/libraries/armem/server/segment/SpecializedSegment.h
@@ -0,0 +1,77 @@
+#pragma once
+
+#include <mutex>
+#include <string>
+
+#include <ArmarXCore/core/application/properties/forward_declarations.h>
+#include <ArmarXCore/core/logging/Logging.h>
+
+
+namespace armarx::aron::typenavigator
+{
+    using ObjectNavigatorPtr = std::shared_ptr<class ObjectNavigator>;
+}
+namespace armarx::armem
+{
+    namespace server
+    {
+        class MemoryToIceAdapter;
+    }
+
+    namespace wm
+    {
+        class CoreSegment;
+    }
+}
+
+namespace armarx::armem::server::segment
+{
+
+    /**
+     * @brief Specialized management of a core segment.
+     */
+    class SpecializedSegment :
+            public armarx::Logging
+    {
+    public:
+
+        SpecializedSegment(
+                server::MemoryToIceAdapter& iceMemory,
+                aron::typenavigator::ObjectNavigatorPtr aronType = nullptr,
+                const std::string& defaultCoreSegmentName = "",
+                int64_t defaultMaxHistorySize = -1);
+
+        virtual ~SpecializedSegment();
+
+
+        virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string& prefix = "");
+        virtual void init();
+        // void connect();
+
+        std::mutex& mutex() const;
+
+
+    protected:
+
+        void setDefaultCoreSegmentName(const std::string& coreSegmentName);
+        void setDefaultMaxHistorySize(int64_t maxHistorySize);
+        void setAronType(aron::typenavigator::ObjectNavigatorPtr aronType);
+
+
+    protected:
+
+        server::MemoryToIceAdapter& iceMemory;
+        wm::CoreSegment* coreSegment = nullptr;
+        aron::typenavigator::ObjectNavigatorPtr aronType;
+
+        struct Properties
+        {
+            std::string coreSegmentName = "";
+            int64_t maxHistorySize = -1;
+        };
+        Properties properties;
+
+
+    };
+
+}
-- 
GitLab