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 
21 
23 {
24 
25  namespace
26  {
27 
28  template <typename AronClass, typename ArmemClass>
29  auto 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([&attachments](const wm::Entity & entity)
36  {
37  if (entity.empty())
38  {
39  ARMARX_WARNING << "No entity snapshot found";
40  return true;
41  }
42 
43  const armem::wm::EntityInstance& instance = entity.getFirstSnapshot().getInstance(0);
44  try
45  {
46  AronClass aronAttachment;
47  aronAttachment.fromAron(instance.data());
48 
49  ArmemClass attachment;
50  fromAron(aronAttachment, attachment);
51 
52  if (attachment.active)
53  {
54  attachments.push_back(attachment);
55  }
56 
57  }
59  {
60  return true;
61  }
62  return true;
63  });
64 
65  return attachments;
66  }
67  } // namespace
68 
69  Reader::Reader(armem::client::MemoryNameSystem& memoryNameSystem) :
70  memoryNameSystem(memoryNameSystem)
71  {}
72 
74  {
75  ARMARX_DEBUG << "Reader: registerPropertyDefinitions";
76 
77  const std::string prefix = propertyPrefix;
78 
79  def->optional(properties.memoryName, prefix + "MemoryName");
80 
81  def->optional(properties.coreAttachmentsSegmentName,
82  prefix + "CoreSegment",
83  "Name of the memory core segment to use for object attachments.");
84  }
85 
86 
88  {
89  // Wait for the memory to become available and add it as dependency.
90  ARMARX_IMPORTANT << "Reader: Waiting for memory '" << properties.memoryName << "' ...";
91  try
92  {
93  memoryReader = memoryNameSystem.useReader(properties.memoryName);
94  ARMARX_IMPORTANT << "Reader: Connected to memory '" << properties.memoryName << "'";
95  }
97  {
98  ARMARX_ERROR << e.what();
99  return;
100  }
101  }
102 
103 
104  std::vector<ObjectAttachment> Reader::queryObjectAttachments(const armem::Time& timestamp) const
105  {
106  // Query all entities from all provider.
108 
109  // clang-format off
110  qb
111  .coreSegments().withName(properties.coreAttachmentsSegmentName)
112  .providerSegments().all()
113  .entities().all()
114  .snapshots().beforeOrAtTime(timestamp);
115  // clang-format on
116 
117  const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
118 
119  ARMARX_DEBUG << "Lookup result in reader: " << qResult;
120 
121  if (not qResult.success) /* c++20 [[unlikely]] */
122  {
123  return {};
124  }
125 
126  return getAttachments <
127  ::armarx::armem::arondto::attachment::ObjectAttachment,
129  }
130 
131  std::vector<ObjectAttachment> Reader::queryObjectAttachments(const armem::Time& timestamp, const std::string& providerName) const
132  {
133  // Query all entities from provider.
135 
136  // clang-format off
137  qb
138  .coreSegments().withName(properties.coreAttachmentsSegmentName)
139  .providerSegments().withName(providerName)
140  .entities().all()
141  .snapshots().beforeOrAtTime(timestamp);
142  // clang-format on
143 
144  const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
145 
146  ARMARX_DEBUG << "Lookup result in reader: " << qResult;
147 
148  if (not qResult.success) /* c++20 [[unlikely]] */
149  {
150  return {};
151  }
152 
153  return getAttachments<::armarx::armem::arondto::attachment::ObjectAttachment, ::armarx::armem::attachment::ObjectAttachment>(qResult.memory);
154  }
155 
156  std::vector<ArticulatedObjectAttachment> Reader::queryArticulatedObjectAttachments(const armem::Time& timestamp) const
157  {
158  // Query all entities from all provider.
160 
161  // clang-format off
162  qb
163  .coreSegments().withName(properties.coreAttachmentsSegmentName)
164  .providerSegments().all()
165  .entities().all()
166  .snapshots().beforeOrAtTime(timestamp);
167  // clang-format on
168 
169  const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
170 
171  ARMARX_DEBUG << "Lookup result in reader: " << qResult;
172 
173  if (not qResult.success) /* c++20 [[unlikely]] */
174  {
175  return {};
176  }
177 
178  return getAttachments<::armarx::armem::arondto::attachment::ArticulatedObjectAttachment, ::armarx::armem::attachment::ArticulatedObjectAttachment>(qResult.memory);
179  }
180 
181  std::vector<ArticulatedObjectAttachment> Reader::queryArticulatedObjectAttachments(const armem::Time& timestamp, const std::string& providerName) const
182  {
183  // Query all entities from provider.
185 
186  // clang-format off
187  qb
188  .coreSegments().withName(properties.coreAttachmentsSegmentName)
189  .providerSegments().withName(providerName)
190  .entities().all()
191  .snapshots().beforeOrAtTime(timestamp);
192  // clang-format on
193 
194  const armem::client::QueryResult qResult = memoryReader.query(qb.buildQueryInput());
195 
196  ARMARX_DEBUG << "Lookup result in reader: " << qResult;
197 
198  if (not qResult.success) /* c++20 [[unlikely]] */
199  {
200  return {};
201  }
202 
203  return getAttachments<::armarx::armem::arondto::attachment::ArticulatedObjectAttachment, ::armarx::armem::attachment::ArticulatedObjectAttachment>(qResult.memory);
204  }
205 
206 
207 
208 } // namespace armarx::armem::attachment
armarx::armem::client::query::EntitySelector::all
EntitySelector & all() override
Definition: selectors.cpp:96
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:44
armarx::armem::client::query::ProviderSegmentSelector::entities
EntitySelector & entities()
Start specifying entities.
Definition: selectors.cpp:123
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::armem::attachment::ObjectAttachment
ObjectAttachment describes a fixed transformation between an agent and an object.
Definition: types.h:100
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::client::query::Builder::buildQueryInput
QueryInput buildQueryInput() const
Definition: Builder.cpp:11
armarx::armem::client::query::EntitySelector::snapshots
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition: selectors.cpp:86
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
armarx::memory
Brief description of class memory.
Definition: memory.h:39
armarx::armem::attachment::Reader::connect
void connect()
Definition: Reader.cpp:87
armarx::armem::attachment::Reader::queryObjectAttachments
std::vector< ObjectAttachment > queryObjectAttachments(const armem::Time &timestamp) const
Definition: Reader.cpp:104
armarx::armem::client::query::Builder::coreSegments
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition: Builder.cpp:38
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:177
error.h
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
armarx::armem::client::MemoryNameSystem::useReader
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
Definition: MemoryNameSystem.cpp:184
armarx::armem::base::EntityBase::getFirstSnapshot
EntitySnapshotT & getFirstSnapshot()
Return the snapshot with the least recent timestamp.
Definition: EntityBase.h:261
armarx::armem::attachment
Definition: Reader.cpp:22
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
memory_definitions.h
aron_conversions.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::client::query::ProviderSegmentSelector::withName
ProviderSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:140
Reader.h
armarx::armem::client::query::SnapshotSelector::beforeOrAtTime
SnapshotSelector & beforeOrAtTime(Time timestamp)
Definition: selectors.cpp:68
IceUtil::Handle< class PropertyDefinitionContainer >
Time.h
Builder.h
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:22
armarx::armem::client::query::CoreSegmentSelector::providerSegments
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
Definition: selectors.cpp:160
util.h
Logging.h
aron_conversions.h
armarx::armem::attachment::Reader::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: Reader.cpp:73
armarx::armem::client::query::ProviderSegmentSelector::all
ProviderSegmentSelector & all() override
Definition: selectors.cpp:133
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query.
Definition: Reader.cpp:33
armarx::armem::attachment::Reader::queryArticulatedObjectAttachments
std::vector< ArticulatedObjectAttachment > queryArticulatedObjectAttachments(const armem::Time &timestamp) const
Definition: Reader.cpp:156
PackagePath.h