GraspCandidateReader.cpp
Go to the documentation of this file.
2
4
5#include <RobotAPI/libraries/GraspingUtility/aron/GraspCandidate.aron.generated.h>
10
11namespace armarx::armem
12{
16
17 void
19 {
20 // Wait for the memory to become available and add it as dependency.
21 ARMARX_IMPORTANT << "GraspCandidateReader: Waiting for memory '" << properties.memoryName
22 << "' ...";
23 try
24 {
25 memoryReader = use ? memoryNameSystem.useReader(properties.memoryName)
26 : memoryNameSystem.getReader(MemoryID(properties.memoryName));
27 ARMARX_IMPORTANT << "GraspCandidateReader: Connected to memory '"
28 << properties.memoryName;
29 }
31 {
32 ARMARX_ERROR << e.what();
33 return;
34 }
35 }
36
37 armarx::grasping::GraspCandidate
39 {
40 armarx::grasping::GraspCandidate candidate;
41
42 grasping::arondto::GraspCandidate aronTransform;
43
44 aronTransform.fromAron(instance.data());
45
46 fromAron(aronTransform, candidate);
47
48 return candidate;
49 }
50
51 armarx::grasping::BimanualGraspCandidate
53 {
54 armarx::grasping::BimanualGraspCandidate candidate;
55
56 grasping::arondto::BimanualGraspCandidate aronTransform;
57 aronTransform.fromAron(instance.data());
58
59 fromAron(aronTransform, candidate);
60
61 return candidate;
62 }
63
64 grasping::GraspCandidatePtr
66 {
67 auto dict = queryGraspCandidateInstancesByID({id});
68 if (auto it = dict.find(id.str()); it != dict.end())
69 {
70 return it->second;
71 }
72 else
73 {
74 return nullptr;
75 }
76 }
77
78 grasping::GraspCandidateDict
79 GraspCandidateReader::queryGraspCandidateInstancesByID(const std::vector<MemoryID>& ids) const
80 {
82
83 ARMARX_DEBUG << "Query for memory name: " << properties.memoryName;
84
86
87 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
88
89 if (!qResult.success)
90 {
91 // todo catch in provider
92 throw armem::error::QueryFailed(properties.memoryName, qResult.errorMessage);
93 }
94
95 armarx::grasping::GraspCandidateDict candidates;
96 for (const MemoryID& id : ids)
97 {
98 if (const armem::wm::EntityInstance* instance = qResult.memory.findInstance(id))
99 {
100 candidates[id.str()] = new grasping::GraspCandidate(asGraspCandidate(*instance));
101 }
102 }
103
104 return candidates;
105 }
106
107 grasping::BimanualGraspCandidatePtr
109 {
111
112 ARMARX_DEBUG << "Query for memory name: " << properties.memoryName;
113
114 qb.singleEntitySnapshot(id.getEntitySnapshotID());
115
116 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
117
118 if (!qResult.success)
119 {
120 // todo catch in provider
121 throw armem::error::QueryFailed(properties.memoryName, qResult.errorMessage);
122 }
123
124
125 armarx::grasping::BimanualGraspCandidatePtr candidate;
126
128 visitor.instanceConstFn = [id, &candidate](armem::wm::EntityInstance const& instance)
129 {
130 if (instance.id() == id)
131 {
132 candidate =
133 new grasping::BimanualGraspCandidate(asBimanualGraspCandidate(instance));
134 }
135 return true;
136 };
137
138 visitor.applyTo(qResult.memory);
139
140 return candidate;
141 }
142
143 grasping::GraspCandidateDict
145 const std::string& entity) const
146 {
148
149 ARMARX_DEBUG << "Query for memory name: " << properties.memoryName;
150
151 qb.coreSegments()
152 .withName(properties.graspCandidateMemoryName)
154 .withName(provider)
155 .entities()
156 .withName(entity)
157 .snapshots()
158 .latest();
159
160 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
161
162 return getGraspCandidatesFromResultSet(qResult);
163 }
164
165 std::map<std::string, grasping::BimanualGraspCandidatePtr>
167 const std::string& entity) const
168 {
170
171 ARMARX_DEBUG << "Query for memory name: " << properties.memoryName;
172
173 qb.coreSegments()
174 .withName(properties.bimanualGraspCandidateMemoryName)
176 .withName(provider)
177 .entities()
178 .withName(entity)
179 .snapshots()
180 .latest();
181
182 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
183
184 return getBimanualGraspCandidatesFromResultSet(qResult);
185 }
186
187 grasping::GraspCandidateDict
188 GraspCandidateReader::queryLatestGraspCandidates(const std::string& provider) const
189 {
190
192
193 ARMARX_DEBUG << "Query for memory name: " << properties.memoryName;
194
195
196 if (!provider.empty())
197 {
198 qb.coreSegments()
199 .withName(properties.graspCandidateMemoryName)
201 .withName(provider)
202 .entities()
203 .all()
204 .snapshots()
205 .latest();
206 }
207 else
208 {
209 qb.coreSegments()
210 .withName(properties.graspCandidateMemoryName)
212 .all()
213 .entities()
214 .all()
215 .snapshots()
216 .latest();
217 }
218
219 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
220
221 return getGraspCandidatesFromResultSet(qResult);
222 }
223
224 std::map<std::string, grasping::BimanualGraspCandidatePtr>
226 {
228
229 ARMARX_DEBUG << "Query for memory name: " << properties.memoryName;
230
231
232 if (!provider.empty())
233 {
234 qb.coreSegments()
235 .withName(properties.bimanualGraspCandidateMemoryName)
237 .withName(provider)
238 .entities()
239 .all()
240 .snapshots()
241 .latest();
242 }
243 else
244 {
245 qb.coreSegments()
246 .withName(properties.bimanualGraspCandidateMemoryName)
248 .all()
249 .entities()
250 .all()
251 .snapshots()
252 .latest();
253 }
254
255 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
256
257 return getBimanualGraspCandidatesFromResultSet(qResult);
258 }
259
260 void
262 {
263 ARMARX_DEBUG << "GraspCandidateReader: registerPropertyDefinitions";
264
265 const std::string prefix = propertyPrefix;
266
267 def->optional(properties.graspCandidateMemoryName,
268 prefix + "GraspCandidateMemoryName",
269 "Name of the grasping memory core segment to use.");
270
271 def->optional(properties.memoryName, prefix + "MemoryName");
272 }
273
274 grasping::GraspCandidateDict
275 GraspCandidateReader::getGraspCandidatesFromResultSet(
276 const armem::client::QueryResult& qResult) const
277 {
278 if (!qResult.success)
279 {
280 throw armem::error::QueryFailed(properties.memoryName, qResult.errorMessage);
281 }
282
283
284 std::map<std::string, armarx::grasping::GraspCandidatePtr> candidates;
285
287 visitor.instanceConstFn = [&candidates](armem::wm::EntityInstance const& instance)
288 {
289 candidates[instance.id().str()] =
290 new grasping::GraspCandidate(asGraspCandidate(instance));
291 return true;
292 };
293
294 visitor.applyTo(qResult.memory);
295
296
297 return candidates;
298 }
299
300 std::map<std::string, grasping::BimanualGraspCandidatePtr>
301 GraspCandidateReader::getBimanualGraspCandidatesFromResultSet(
302 const armem::client::QueryResult& qResult) const
303 {
304 if (!qResult.success)
305 {
306 throw armem::error::QueryFailed(properties.memoryName, qResult.errorMessage);
307 }
308
309
310 std::map<std::string, armarx::grasping::BimanualGraspCandidatePtr> candidates;
311
312 armem::wm::FunctionalVisitor visitor;
313 visitor.instanceConstFn = [&candidates](armem::wm::EntityInstance const& instance)
314 {
315 candidates[instance.id().str()] =
316 new grasping::BimanualGraspCandidate(asBimanualGraspCandidate(instance));
317 return true;
318 };
319
320 visitor.applyTo(qResult.memory);
321
322
323 return candidates;
324 }
325
326 grasping::GraspCandidateDict
328 const DateTime& timestamp) const
329 {
331
332 ARMARX_DEBUG << "Query for memory name: " << properties.memoryName;
333
334
335 if (!provider.empty())
336 {
337 qb.coreSegments()
338 .withName(properties.graspCandidateMemoryName)
340 .withName(provider)
341 .entities()
342 .all()
343 .snapshots()
345 }
346 else
347 {
348 qb.coreSegments()
349 .withName(properties.graspCandidateMemoryName)
351 .all()
352 .entities()
353 .all()
354 .snapshots()
356 }
357
358 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
359 return getGraspCandidatesFromResultSet(qResult);
360 }
361
362
363} // namespace armarx::armem
std::string timestamp()
std::map< std::string, ::armarx::grasping::BimanualGraspCandidatePtr > queryLatestBimanualGraspCandidateEntity(std::string const &provider, std::string const &entity) const
std::map< std::string, ::armarx::grasping::BimanualGraspCandidatePtr > queryLatestBimanualGraspCandidates(std::string const &provider="") const
::armarx::grasping::GraspCandidatePtr queryGraspCandidateInstanceByID(armem::MemoryID const &id) const
::armarx::grasping::BimanualGraspCandidatePtr queryBimanualGraspCandidateInstanceByID(armem::MemoryID const &id) const
::armarx::grasping::GraspCandidateDict queryLatestGraspCandidateEntity(std::string const &provider, std::string const &entity) const
::armarx::grasping::GraspCandidateDict queryGraspCandidateInstancesByID(std::vector< armem::MemoryID > const &ids) const
void connect(armem::client::MemoryNameSystem &memoryNameSystem, bool use=true)
::armarx::grasping::GraspCandidateDict queryGraspCandidatesNewerThan(std::string const &provider="", const armarx::DateTime &timestamp=armarx::DateTime::Now()) const
::armarx::grasping::GraspCandidateDict queryLatestGraspCandidates(std::string const &provider="") const
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
The memory name system (MNS) client.
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
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
void multipleEntitySnapshots(const std::vector< MemoryID > &snapshotIDs)
Definition Builder.cpp:157
void singleEntitySnapshot(const MemoryID &snapshotID)
Definition Builder.cpp:144
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition Builder.cpp:42
CoreSegmentSelector & withName(const std::string &name) override
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
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
SnapshotSelector & timeRange(Time min, Time max)
Definition selectors.cpp:46
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
Indicates that a query resulted in an Error.
Definition ArMemError.h:191
Client-side working entity instance.
A Visitor which can be parametrized by std::function instead of inheriting and overriding.
std::function< bool(const EntityInstance &instance)> instanceConstFn
bool applyTo(Memory &memory)
Definition Visitor.cpp:21
Represents a point in time.
Definition DateTime.h:25
static DateTime Now()
Definition DateTime.cpp:51
#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
void fromAron(const arondto::MemoryID &dto, MemoryID &bo)
armarx::grasping::GraspCandidate asGraspCandidate(const armem::wm::EntityInstance &instance)
armarx::grasping::BimanualGraspCandidate asBimanualGraspCandidate(const armem::wm::EntityInstance &instance)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
auto * findInstance(const MemoryID &instanceID)
Find an entity instance.
Result of a QueryInput.
Definition Query.h:51
wm::Memory memory
The slice of the memory that matched the query.
Definition Query.h:58