ObjectReader.cpp
Go to the documentation of this file.
1#include "ObjectReader.h"
2
3#include <optional>
4
8
15#include <RobotAPI/libraries/armem_objects/aron/Attachment.aron.generated.h>
16#include <RobotAPI/libraries/armem_objects/aron/FamiliarObjectInstance.aron.generated.h>
20
22{
23 void
25 {
26 const std::string prefix = propertyPrefix;
27
28 def->optional(p.memoryName, prefix + "MemoryName");
29 }
30
31 void
33 {
34 ARMARX_IMPORTANT << "Reader: Waiting for memory '" << objects::constants::MemoryName
35 << "' ...";
36 try
37 {
38 memoryReader = memoryNameSystem.useReader(objects::constants::MemoryName);
39 ARMARX_IMPORTANT << "Reader: Connected to memory '" << objects::constants::MemoryName
40 << "'";
41 }
43 {
44 ARMARX_ERROR << e.what();
45 return;
46 }
47 }
48
49 inline std::optional<arondto::FamiliarObjectInstance>
51 {
52 arondto::FamiliarObjectInstance aronBo;
53 try
54 {
55 aronBo.fromAron(instance.data());
56 }
57 catch (...)
58 {
59 ARMARX_WARNING << "Conversion to ObjectPose failed!";
60 return std::nullopt;
61 }
62
63 return aronBo;
64 }
65
66 std::optional<std::map<std::string, arondto::FamiliarObjectInstance>>
68 const std::optional<std::string>& providerName) const
69 {
70
72
74 {
75 auto& cs = qb.coreSegments().withName(
77
78 client::query::ProviderSegmentSelector* providerSegments = nullptr;
79 if (providerName.has_value())
80 {
81 providerSegments = &cs.providerSegments().withName(providerName.value());
82 }
83 else
84 {
85 providerSegments = &cs.providerSegments().all();
86 }
87
88 providerSegments->entities().withName(instanceId.str()).snapshots().latest();
89 }
90
91
92 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
93
94 ARMARX_DEBUG << "Lookup result in reader: " << qResult;
95
96 if (not qResult.success) /* c++20 [[unlikely]] */
97 {
99 return std::nullopt;
100 }
101
102
103 std::map<std::string, arondto::FamiliarObjectInstance> result;
104
106 [&result](const wm::ProviderSegment& ps)
107 {
108 auto& thisResult = result[ps.name()];
109
110 ARMARX_CHECK_EQUAL(ps.getEntityNames().size(), 1)
111 << "Query returned more than one entity";
112
114 [&thisResult](const wm::EntityInstance& e)
115 {
116 if (auto converted = convert(e))
117 {
118 thisResult = converted.value();
119 }
120 });
121 });
122
123 return result;
124 }
125
126 std::map<std::string, std::vector<arondto::FamiliarObjectInstance>>
128 const std::optional<std::string>& providerName) const
129 {
131
133 {
134 auto& cs = qb.coreSegments().withName(
136
137 client::query::ProviderSegmentSelector* providerSegments = nullptr;
138 if (providerName.has_value())
139 {
140 providerSegments = &cs.providerSegments().withName(providerName.value());
141 }
142 else
143 {
144 providerSegments = &cs.providerSegments().all();
145 }
146
147 providerSegments->entities().all().snapshots().latest();
148 }
149
150 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
151
152 ARMARX_DEBUG << "Lookup result in reader: " << qResult;
153
154 if (not qResult.success) /* c++20 [[unlikely]] */
155 {
156 ARMARX_WARNING << qResult.errorMessage;
157 return {};
158 }
159
160 std::map<std::string, std::vector<arondto::FamiliarObjectInstance>> result;
161
163 [&result](const wm::ProviderSegment& ps)
164 {
165 auto& thisResult = result[ps.name()];
166
168 [&thisResult](const wm::EntityInstance& e)
169 {
170 if (auto converted = convert(e))
171 {
172 thisResult.push_back(converted.value());
173 }
174 });
175 });
176
177 return result;
178 }
179
180 std::map<std::string, std::vector<arondto::FamiliarObjectInstance>>
182 const ObjectID& classId,
183 const std::optional<std::string>& providerName) const
184 {
186
188 {
189 auto& cs = qb.coreSegments().withName(
191
192 client::query::ProviderSegmentSelector* providerSegments = nullptr;
193 if (providerName.has_value())
194 {
195 providerSegments = &cs.providerSegments().withName(providerName.value());
196 }
197 else
198 {
199 providerSegments = &cs.providerSegments().all();
200 }
201
202 providerSegments->entities()
204 .snapshots()
205 .latest();
206 }
207
208 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
209
210 ARMARX_DEBUG << "Lookup result in reader: " << qResult;
211
212 if (not qResult.success) /* c++20 [[unlikely]] */
213 {
214 ARMARX_WARNING << qResult.errorMessage;
215 return {};
216 }
217
218 std::map<std::string, std::vector<arondto::FamiliarObjectInstance>> result;
219
221 [&result](const wm::ProviderSegment& ps)
222 {
223 auto& thisResult = result[ps.name()];
224
226 [&thisResult](const wm::EntityInstance& e)
227 {
228 if (auto converted = convert(e))
229 {
230 thisResult.push_back(converted.value());
231 }
232 });
233 });
234
235
236 return result;
237 }
238
239} // namespace armarx::armem::obj::familiar_object_instance
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
ObjectID getClassID() const
Return just the class ID without an intance name.
Definition ObjectID.cpp:71
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition ObjectID.cpp:60
std::vector< std::string > getEntityNames() const
The memory name system (MNS) client.
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition Builder.h:22
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition Builder.cpp:42
CoreSegmentSelector & withName(const std::string &name) override
EntitySelector & withName(const std::string &name) override
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition selectors.cpp:92
ProviderSegmentSelector & withName(const std::string &name) override
EntitySelector & entities()
Start specifying entities.
ProviderSegmentSelector & all() override
virtual DerivedT & withNamesStartingWith(const std::string &prefix)
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
void connect(armem::client::MemoryNameSystem &memoryNameSystem)
std::map< std::string, std::vector< arondto::FamiliarObjectInstance > > queryLatestFamiliarObjectInstancesFromClass(const ObjectID &classId, const std::optional< std::string > &providerName=std::nullopt) const
std::optional< std::map< std::string, arondto::FamiliarObjectInstance > > queryLatestFamiliarObjectInstance(const ObjectID &instanceId, const std::optional< std::string > &providerName=std::nullopt) const
std::map< std::string, std::vector< arondto::FamiliarObjectInstance > > queryAllLatestFamiliarObjectInstances(const std::optional< std::string > &providerName=std::nullopt) const
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::optional< arondto::FamiliarObjectInstance > convert(const wm::EntityInstance &instance)
const std::string CoreFamiliarObjectInstanceSegmentName
Definition constants.h:28
armem::wm::EntityInstance EntityInstance
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Result of a QueryInput.
Definition Query.h:51
wm::Memory memory
The slice of the memory that matched the query.
Definition Query.h:58
#define ARMARX_TRACE
Definition trace.h:77