From 0305749540c7b349596846cb22d3a851a8b42170 Mon Sep 17 00:00:00 2001
From: Joana Plewnia <joana.plewnia@.kit.edu>
Date: Wed, 30 Oct 2024 09:50:35 -0700
Subject: [PATCH] added try-catch

---
 .../Component.cpp                             |  2 +-
 .../core/ObjectLastSeenAt.cpp                 | 72 +++++++++----------
 .../core/ObjectLastSeenAt.h                   |  2 +-
 .../skills/ObjectLastSeenAt.cpp               |  2 +-
 .../skills/ObjectLastSeenAt.h                 |  2 +-
 5 files changed, 36 insertions(+), 44 deletions(-)

diff --git a/source/armarx/speech/components/episodic_verbalization_skill_provider/Component.cpp b/source/armarx/speech/components/episodic_verbalization_skill_provider/Component.cpp
index 2b7a956..75b1707 100644
--- a/source/armarx/speech/components/episodic_verbalization_skill_provider/Component.cpp
+++ b/source/armarx/speech/components/episodic_verbalization_skill_provider/Component.cpp
@@ -71,7 +71,7 @@ namespace armarx::speech::components::episodic_verbalization::skill_provider
             //Add a new skillFacory for your skill with the remote and properties structs needed.
             skills::ObjectLastSeenAt::Properties properties;
             skills::ObjectLastSeenAt::Remote remote{
-                //.memoryNameSystem = memoryNameSystem(),
+                .memoryNameSystem = memoryNameSystem(),
                 .pythonProxy = objectLastSeenAtPrx,
                 .symbolicSceneReader =
                     std::experimental::make_observer(&symbolicSceneReaderPlugin_->get()),
diff --git a/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.cpp b/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.cpp
index d8b9632..dc3d9fe 100644
--- a/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.cpp
+++ b/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.cpp
@@ -212,6 +212,7 @@ namespace armarx::speech::object_last_seen_at::core
             armem::MemoryID newObjectPoseClassID = armem::MemoryID("Object", "Class", "", "");
 
             ARMARX_DEBUG << object_description_name;
+
             if (object_description_name.find(object_name) != std::string::npos)
             { // object is in symbolic scene memory at the moment
 
@@ -229,49 +230,37 @@ namespace armarx::speech::object_last_seen_at::core
                 return true;  // for now return here, later put this below
 
                 /**
-                //obtain the object pose from the object memory:
-                const armem::MemoryID sourceId = armem::MemoryID::fromString(object_description.second.source);
-                const armem::MemoryID coreSegmentID("Object", "Instance");
-                armem::client::Reader reader = this->remote.memoryNameSystem.getReader(coreSegmentID);
-                armem::client::QueryBuilder builder;
-                builder.coreSegments().withName(coreSegmentID.coreSegmentName)
-                    .providerSegments().withName(sourceId.providerSegmentName)
-                    .entities().withName(sourceId.entityName)
-                    .snapshots().atTime(sourceId.timestamp);
-                armem::client::QueryResult result = reader.query(builder.buildQueryInput());
-
-                if(result.success)
-                {
-                    armem::wm::Memory& memory = result.memory;
-
-                    memory.forEachInstance([](const armem::wm::EntityInstance& instance)
+                try{
+                    //obtain the object pose from the object memory:
+                    const armem::MemoryID sourceId = armem::MemoryID::fromString(object_description.second.source);
+                    const armem::MemoryID coreSegmentID("Object", "Instance");
+                    armem::client::Reader reader = this->remote.memoryNameSystem.getReader(coreSegmentID);
+                    armem::client::QueryBuilder builder;
+                    builder.coreSegments().withName(coreSegmentID.coreSegmentName)
+                        .providerSegments().withName(sourceId.providerSegmentName)
+                        .entities().withName(sourceId.entityName)
+                        .snapshots().atTime(sourceId.timestamp);
+                    armem::client::QueryResult result = reader.query(builder.buildQueryInput());
+
+                    if(result.success)
                     {
+                        armem::wm::Memory& memory = result.memory;
 
-                        armarx::objpose::ObjectPose pose = 
-                            armarx::aron::fromAron<armarx::objpose::ObjectPose>(
-                                 armarx::armem::arondto::ObjectInstance::FromAron(instance.data())
-                            );
-
-                        auto currentTime = armarx::armem::Time::Now();
-                        auto ageOfObjectInformation = currentTime.toSecondsSinceEpoch() - instance.metadata().sentTime.toSecondsSinceEpoch();
-                        
-                        // TODO: create an object instance or pose element with the new timestamp 
-                        // TODO: maybe use ObjectInstanceWriter (needs ObjectID, provider name and time)
-                    });   
-                }
+                        memory.forEachInstance([this](const armem::wm::EntityInstance& instance)
+                        {
 
+                            armarx::objpose::ObjectPose pose = 
+                                armarx::aron::fromAron<armarx::objpose::ObjectPose>(
+                                    armarx::armem::arondto::ObjectInstance::FromAron(instance.data())
+                                );
+
+                            auto currentTime = armarx::armem::Time::Now();
+                            auto ageOfObjectInformation = currentTime.toSecondsSinceEpoch() - instance.metadata().sentTime.toSecondsSinceEpoch();
 
-                    for (const auto& providerName :
-                        remote.objectInstanceReader->getObjectPoseStorage()->getAvailableProviderNames())
-                    {
-                        remote.objectInstanceReader->getObjectPoseStorage()->getObjectPoses()
-                        // obtain the object instance from the object memory and store it there again but with updated timestamp and provider name
-                        if (auto objectPose = remote.objectInstanceReader->queryLatestObjectInstance(object_description.second.id))
-                        {
                             armarx::armem::arondto::ObjectInstance objectInstance;
-                            armarx::objpose::toAron(objectInstance.pose, objectPose.value());
+                            armarx::objpose::toAron(objectInstance.pose, pose);
 
-                            objectInstance.classID = "";
+                            objectInstance.classID = ""; //objectClassId;
                             objectInstance.pose.confidence = 1.0; // we are sure about the location
                             objectInstance.pose.timestamp =
                                 armarx::Clock::Now(); // we must update the timestamp
@@ -280,11 +269,14 @@ namespace armarx::speech::object_last_seen_at::core
                             ARMARX_CHECK_NOT_NULL(this->remote.objectInstanceWriter);
                             ARMARX_CHECK(this->remote.objectInstanceWriter->commitObject(
                                 objectInstance, properties.objectProviderName, armarx::Clock::Now()));
-                            
-                        }
+                        });   
                     }
-                    */
+                } catch (const std::exception& e)
+                {
+                    ARMARX_ERROR << "Error while updating object instance in object memory: " << e.what();
+                }
 
+                */
                 }
 
 
diff --git a/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.h b/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.h
index 532e3f4..48ff45d 100644
--- a/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.h
+++ b/source/armarx/speech/skills/object_last_seen_at/core/ObjectLastSeenAt.h
@@ -57,7 +57,7 @@ namespace armarx::speech::object_last_seen_at::core
         /// Remote parameters of this skill implementation
         struct Remote
         {
-            //armarx::armem::client::MemoryNameSystem memoryNameSystem;
+            armarx::armem::client::MemoryNameSystem memoryNameSystem;
             armarx::speech::skills::object_last_seen_at::core::ObjectLastSeenAtPythonInterfacePrx pythonProxy; 
 
             std::experimental::observer_ptr<
diff --git a/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.cpp b/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.cpp
index 6f4eb61..26fb31d 100644
--- a/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.cpp
+++ b/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.cpp
@@ -64,7 +64,7 @@ namespace armarx::speech::object_last_seen_at::skills
     {
         // Enter main code of the skill here.
         core::ObjectLastSeenAt::Remote r{
-            //.memoryNameSystem = this->remote.memoryNameSystem,
+            .memoryNameSystem = this->remote.memoryNameSystem,
             .pythonProxy = this->remote.pythonProxy,
             .symbolicSceneReader = this->remote.symbolicSceneReader,
             .objectClassReader = this->remote.objectClassReader,
diff --git a/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.h b/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.h
index 151e262..71a46a6 100644
--- a/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.h
+++ b/source/armarx/speech/skills/object_last_seen_at/skills/ObjectLastSeenAt.h
@@ -62,7 +62,7 @@ namespace armarx::speech::object_last_seen_at::skills
         /// Remote parameters of this skill implementation
         struct Remote
         {
-            //armarx::armem::client::MemoryNameSystem memoryNameSystem;
+            armarx::armem::client::MemoryNameSystem memoryNameSystem;
             armarx::speech::skills::object_last_seen_at::core::ObjectLastSeenAtPythonInterfacePrx
                 pythonProxy;
 
-- 
GitLab