ObjectSpokenNames.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::ArmarXObjects::OpossumLogger
17 * @author Joana Plewnia ( joana dot plewnia at kit dot edu )
18 * @date 2026
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "ObjectSpokenNames.h"
24
26
28
30{
31
32 std::optional<std::string>
33 ObjectSpokenNames::operator()(const std::string& dataset, const std::string& className)
34 {
35 const auto key = std::make_pair(dataset, className);
36
37 std::scoped_lock lock(mutex);
38 if (const auto cached = cache.find(key); cached != cache.end())
39 {
40 return cached->second;
41 }
42
43 std::optional<std::string> spokenName;
44 try
45 {
46 spokenName = load(dataset, className);
47 }
48 catch (const std::exception& e)
49 {
50 ARMARX_WARNING << deactivateSpam(60) << "Failed to look up the spoken name of '"
51 << dataset << "/" << className << "': " << e.what()
52 << ". Falling back to the class name.";
53 }
54
55 cache.emplace(key, spokenName);
56 return spokenName;
57 }
58
59 std::optional<std::string>
60 ObjectSpokenNames::load(const std::string& dataset, const std::string& className)
61 {
62 // Note that findObject() with a non-empty dataset does not check whether
63 // that dataset exists -- an unknown dataset simply yields an ObjectInfo
64 // without a names file, which is exactly the miss we then retry below.
65 for (const std::string& datasetToTry : {dataset, std::string()})
66 {
67 const std::optional<ObjectInfo> info = finder.findObject(datasetToTry, className);
68 if (not info.has_value())
69 {
70 continue;
71 }
72 const std::optional<std::vector<std::string>> spokenNames = info->loadSpokenNames();
73 if (spokenNames.has_value() and not spokenNames->empty())
74 {
75 return spokenNames->front();
76 }
77 if (datasetToTry.empty())
78 {
79 break;
80 }
81 }
82 return std::nullopt;
83 }
84
87 {
88 return [this](const std::string& dataset, const std::string& className)
89 { return (*this)(dataset, className); };
90 }
91
92} // namespace armarx::opossum
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
std::optional< std::string > operator()(const std::string &dataset, const std::string &className)
The spoken name of an object class, or std::nullopt if it has none.
SpokenNameLookup asLookup()
This resolver as a SpokenNameLookup. Must not outlive this object.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:191
std::function< std::optional< std::string >( const std::string &dataset, const std::string &className)> SpokenNameLookup
Resolves an object class to its natural-language ("spoken") name.