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 
70  Reader::Reader(armem::client::MemoryNameSystem& memoryNameSystem) :
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)
115  .providerSegments().all()
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)
170  .providerSegments().all()
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
armarx::armem::client::query::EntitySelector::all
EntitySelector & all() override
Definition: selectors.cpp:104
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:36
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:41
armarx::armem::client::query::ProviderSegmentSelector::entities
EntitySelector & entities()
Start specifying entities.
Definition: selectors.cpp:135
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
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:12
armarx::armem::client::query::EntitySelector::snapshots
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition: selectors.cpp:92
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:27
armarx::memory
Brief description of class memory.
Definition: memory.h:38
armarx::armem::attachment::Reader::connect
void connect()
Definition: Reader.cpp:90
armarx::armem::attachment::Reader::queryObjectAttachments
std::vector< ObjectAttachment > queryObjectAttachments(const armem::Time &timestamp) const
Definition: Reader.cpp:107
armarx::armem::client::query::Builder::coreSegments
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition: Builder.cpp:42
armarx::armem::attachment::ArticulatedObjectAttachment
ArticulatedObjectAttachment describes a fixed transformation between an agent and an articulated obje...
Definition: types.h:121
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:198
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:267
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
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:21
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
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:155
Reader.h
armarx::armem::client::query::SnapshotSelector::beforeOrAtTime
SnapshotSelector & beforeOrAtTime(Time timestamp)
Definition: selectors.cpp:73
IceUtil::Handle< class PropertyDefinitionContainer >
Time.h
Builder.h
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:73
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:24
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:21
armarx::armem::client::query::CoreSegmentSelector::providerSegments
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
Definition: selectors.cpp:178
util.h
Logging.h
aron_conversions.h
armarx::armem::attachment::Reader::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: Reader.cpp:76
armarx::armem::client::query::ProviderSegmentSelector::all
ProviderSegmentSelector & all() override
Definition: selectors.cpp:147
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query on the WM.
Definition: Reader.cpp:69
armarx::armem::attachment::Reader::queryArticulatedObjectAttachments
std::vector< ArticulatedObjectAttachment > queryArticulatedObjectAttachments(const armem::Time &timestamp) const
Definition: Reader.cpp:162
PackagePath.h