Reader.cpp
Go to the documentation of this file.
1#include "Reader.h"
2
3#include <mutex>
4#include <optional>
5
8
14// #include <RobotAPI/libraries/armem_robot_state/robot_conversions.h>
15// #include <RobotAPI/libraries/armem_robot_state/aron_conversions.h>
16// #include <RobotAPI/libraries/armem_robot_state/aron/Robot.aron.generated.h>
17#include <RobotAPI/libraries/armem_objects/aron/Attachment.aron.generated.h>
20
22{
23
24 namespace
25 {
26
27 template <typename AronClass, typename ArmemClass>
28 auto
29 getAttachments(const armarx::armem::wm::Memory& memory)
30 {
31 // using ArmemClass = decltype(fromAron(AronClass()));
32 using ArmemClassVector = std::vector<ArmemClass>;
33
34 ArmemClassVector attachments;
35 memory.forEachEntity(
36 [&attachments](const wm::Entity& entity)
37 {
38 if (entity.empty())
39 {
40 ARMARX_WARNING << "No entity snapshot found";
41 return true;
42 }
43
44 const armem::wm::EntityInstance& instance =
45 entity.getFirstSnapshot().getInstance(0);
46 try
47 {
48 AronClass aronAttachment;
49 aronAttachment.fromAron(instance.data());
50
51 ArmemClass attachment;
52 fromAron(aronAttachment, attachment);
53
54 if (attachment.active)
55 {
56 attachments.push_back(attachment);
57 }
58 }
60 {
61 return true;
62 }
63 return true;
64 });
65
66 return attachments;
67 }
68 } // namespace
69
71 memoryNameSystem(memoryNameSystem)
72 {
73 }
74
75 void
77 {
78 ARMARX_DEBUG << "Reader: registerPropertyDefinitions";
79
80 const std::string prefix = propertyPrefix;
81
82 def->optional(properties.memoryName, prefix + "MemoryName");
83
84 def->optional(properties.coreAttachmentsSegmentName,
85 prefix + "CoreSegment",
86 "Name of the memory core segment to use for object attachments.");
87 }
88
89 void
91 {
92 // Wait for the memory to become available and add it as dependency.
93 ARMARX_IMPORTANT << "Reader: Waiting for memory '" << properties.memoryName << "' ...";
94 try
95 {
96 memoryReader = memoryNameSystem.useReader(properties.memoryName);
97 ARMARX_IMPORTANT << "Reader: Connected to memory '" << properties.memoryName << "'";
98 }
100 {
101 ARMARX_ERROR << e.what();
102 return;
103 }
104 }
105
106 std::vector<ObjectAttachment>
108 {
109 // Query all entities from all provider.
111
112 // clang-format off
113 qb
114 .coreSegments().withName(properties.coreAttachmentsSegmentName)
116 .entities().all()
118 // clang-format on
119
120 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
121
122 ARMARX_DEBUG << "Lookup result in reader: " << qResult;
123
124 if (not qResult.success) /* c++20 [[unlikely]] */
125 {
126 return {};
127 }
128
129 return getAttachments<::armarx::armem::arondto::attachment::ObjectAttachment,
131 }
132
133 std::vector<ObjectAttachment>
135 const std::string& providerName) const
136 {
137 // Query all entities from provider.
139
140 // clang-format off
141 qb
142 .coreSegments().withName(properties.coreAttachmentsSegmentName)
143 .providerSegments().withName(providerName)
144 .entities().all()
146 // clang-format on
147
148 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
149
150 ARMARX_DEBUG << "Lookup result in reader: " << qResult;
151
152 if (not qResult.success) /* c++20 [[unlikely]] */
153 {
154 return {};
155 }
156
157 return getAttachments<::armarx::armem::arondto::attachment::ObjectAttachment,
159 }
160
161 std::vector<ArticulatedObjectAttachment>
163 {
164 // Query all entities from all provider.
166
167 // clang-format off
168 qb
169 .coreSegments().withName(properties.coreAttachmentsSegmentName)
171 .entities().all()
173 // clang-format on
174
175 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
176
177 ARMARX_DEBUG << "Lookup result in reader: " << qResult;
178
179 if (not qResult.success) /* c++20 [[unlikely]] */
180 {
181 return {};
182 }
183
184 return getAttachments<::armarx::armem::arondto::attachment::ArticulatedObjectAttachment,
186 qResult.memory);
187 }
188
189 std::vector<ArticulatedObjectAttachment>
191 const std::string& providerName) const
192 {
193 // Query all entities from provider.
195
196 // clang-format off
197 qb
198 .coreSegments().withName(properties.coreAttachmentsSegmentName)
199 .providerSegments().withName(providerName)
200 .entities().all()
202 // clang-format on
203
204 const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
205
206 ARMARX_DEBUG << "Lookup result in reader: " << qResult;
207
208 if (not qResult.success) /* c++20 [[unlikely]] */
209 {
210 return {};
211 }
212
213 return getAttachments<::armarx::armem::arondto::attachment::ArticulatedObjectAttachment,
215 qResult.memory);
216 }
217
218
219} // namespace armarx::armem::attachment
std::string timestamp()
Reader(armem::client::MemoryNameSystem &memoryNameSystem)
Definition Reader.cpp:70
std::vector< ObjectAttachment > queryObjectAttachments(const armem::Time &timestamp) const
Definition Reader.cpp:107
std::vector< ArticulatedObjectAttachment > queryArticulatedObjectAttachments(const armem::Time &timestamp) const
Definition Reader.cpp:162
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition Reader.cpp:76
EntitySnapshotT & getFirstSnapshot()
Return the snapshot with the least recent timestamp.
Definition EntityBase.h:262
The memory name system (MNS) client.
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
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
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 & beforeOrAtTime(Time timestamp)
Definition selectors.cpp:73
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
Client-side working entity instance.
Client-side working memory.
A base class for aron exceptions.
Definition Exception.h:37
Brief description of class memory.
Definition memory.h:39
#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
void fromAron(const arondto::MemoryID &dto, MemoryID &bo)
armarx::core::time::DateTime Time
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
ArticulatedObjectAttachment describes a fixed transformation between an agent and an articulated obje...
Definition types.h:122
ObjectAttachment describes a fixed transformation between an agent and an object.
Definition types.h:101
Result of a QueryInput.
Definition Query.h:51
wm::Memory memory
The slice of the memory that matched the query.
Definition Query.h:58