From 357d5e63e04853ade82e712a3482967ccfd93d29 Mon Sep 17 00:00:00 2001
From: Joana <joana.plewnia@kit.edu>
Date: Sun, 27 Oct 2024 14:27:20 -0700
Subject: [PATCH] using spoken name for objects, tested in simulation

---
 .../object_last_seen_at/core/CMakeLists.txt   |  3 ++
 .../core/ObjectLastSeenAt.cpp                 | 54 ++++++++++++-------
 2 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/source/armarx/speech/skills/object_last_seen_at/core/CMakeLists.txt b/source/armarx/speech/skills/object_last_seen_at/core/CMakeLists.txt
index 98c2df1..4d26edd 100644
--- a/source/armarx/speech/skills/object_last_seen_at/core/CMakeLists.txt
+++ b/source/armarx/speech/skills/object_last_seen_at/core/CMakeLists.txt
@@ -26,6 +26,9 @@ armarx_add_library(object_last_seen_at_core
         armarx_symbolic_planning::symbolic_scene_memory_client
         armarx_speech::object_last_seen_at_core_ice
 
+        # for object memory queries
+        armem_objects
+
         #armarx speech skill:
         #armarx_speech:skills
 
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 15a3ae1..2fd7729 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
@@ -31,6 +31,7 @@
 #include <memory>
 #include <armarx/symbolic_planning/armem/symbolic_scene_memory/client/reader/SymbolicSceneReader.h>
 
+#include <RobotAPI/libraries/armem_objects/client/class/ClassReader.h>
 #include <RobotAPI/libraries/armem/client/Reader.h>
 
 #include <algorithm> 
@@ -93,29 +94,42 @@ namespace armarx::speech::object_last_seen_at::core
                             secondPart = secondPart.substr(0, colonPos);
                         }
 
-                        /**
-                        size_t firstSlash = object_location.find('/');
-                        std::string location_name_human_readable = "";
-                        if (firstSlash != std::string::npos) {
-                            // Find the second slash, starting after the first one
-                            size_t secondSlash = object_location.find('/', firstSlash + 1);
-                            
-                            // If there is a second slash, extract the part between
-                            if (secondSlash != std::string::npos) {
-                                location_name_human_readable = object_location.substr(firstSlash + 1, secondSlash - firstSlash - 1);
+                        //as a spoken name take the spoken name of the object from the object memory (class core segment):
+                        std::string object_id = object_description.second.id;
+
+                        //find the last / in the object id and use the part before it as a string:
+                        size_t lastSlashPosLocation = object_id.find_last_of('/');
+                        std::string object_name = object_id.substr(lastSlashPosLocation + 1);
+                        //use this ID to query the Object Memory 
+                        try{
+                            armem::obj::clazz::ClassReader object_class_reader;
+                            auto object_class = object_class_reader.getObjectClass("PriorKnowledgeData", object_name);
+
+                            std::string answer;
+
+                            if(object_class.has_value()) {
+                                auto object_spoken_names = object_class->names.spoken;
+                                // if the object (of the location) has a spoken name, use it for verbalization:
+                                if (object_spoken_names.empty()) {
+                                    ARMARX_DEBUG << "No spoken name found for object " << object_name;
+                                    answer = "I have last seen the " + object_name + " " + firstPart + " the " + secondPart + ".";
+                                } else {
+                                    auto first_spoken_name = object_spoken_names[0];
+                                    answer = "I have last seen the " + object_name + " " + firstPart + " the " + first_spoken_name + ".";
+                                }
                             } else {
-                                // If no second slash, the second part goes till the end of the string
-                                location_name_human_readable = object_location.substr(firstSlash + 1);
+                                // object class query did not work correctly, as this always should have a value if the object class exists:
+                                ARMARX_DEBUG << "You are trying to query the object class info of an object, but the object class object has no value";
+                                answer = "I have last seen the " + object_name + " " + firstPart + " the " + secondPart + ".";
                             }
-                        }
-
-                        */
-
 
-                        ARMARX_INFO << "Object " << object_name << " (" << object_description.second.id << ") is at " << object_description.second.objectAt;
-                        //std::string answer = "I have last seen the " + object_name + " at " + location_name_human_readable + ".";
-                        std::string answer = "I have last seen the " + object_name + " " + firstPart + " the " + secondPart + ".";
-                        this->subskills.say(answer);
+                            ARMARX_INFO << "Object " << object_name << " (" << object_description.second.id << ") is at " << object_description.second.objectAt;
+                            this->subskills.say(answer);
+                        } catch (const std::exception& e) {
+                            ARMARX_DEBUG << "Error while querying the object class info of an object: " << e.what();
+                            std::string answer = "I have last seen the " + object_name + " " + firstPart + " the " + secondPart + ".";
+                            this->subskills.say(answer);
+                        }
                         return true;
                     } else {  // if the location or object could not be found it is currently not visible and we need to leverage the LTM
                         // TODO: LTM integration
-- 
GitLab