Reader.cpp
Go to the documentation of this file.
1 #include "Reader.h"
2 
5 
6 #include <RobotAPI/interface/armem/mns/MemoryNameSystemInterface.h>
7 #include <RobotAPI/interface/armem/server/ReadingMemoryInterface.h>
16 
20 #include <armarx/navigation/human/aron/Human.aron.generated.h>
23 
25 {
26  Reader::~Reader() = default;
27 
29  Reader::buildHumansQuery(const Query& query) const
30  {
32 
33  // clang-format off
34  qb
35  .coreSegments().withName(properties().coreSegmentName)
37  .entities().withName("humans")
39  // clang-format on
40 
41  return qb;
42  }
43 
46  {
48 
49  // clang-format off
50  qb
51  .coreSegments().withName(properties().coreSegmentName)
53  .entities().withName("groups")
55  // clang-format on
56 
57  return qb;
58  }
59 
60 
61  std::string
63  {
64  return "mem.nav.human.";
65  }
66 
69  {
71  .coreSegmentName = memory::constants::HumanCoreSegmentName};
72  }
73 
75  asHumans(const armem::wm::ProviderSegment& providerSegment,
76  const DateTime& timestamp,
77  const Duration& maxAge)
78  {
80 
81  ARMARX_CHECK(not providerSegment.empty()) << "No entities";
82  ARMARX_CHECK(providerSegment.size() == 1) << "There should be only one entity!";
83 
84  providerSegment.forEachEntity(
85  [&humans, &timestamp, &maxAge](const armem::wm::Entity& entity)
86  {
87  const auto& entitySnapshot = entity.getLatestSnapshot();
88  ARMARX_CHECK(not entitySnapshot.empty()) << "No entity snapshot instances";
89 
90  entitySnapshot.forEachInstance(
91  [&](const armem::wm::EntityInstance& entityInstance)
92  {
93  const Duration dtToNow = timestamp - entityInstance.metadata().referencedTime;
94 
95  if (dtToNow < maxAge and dtToNow.isPositive())
96  {
97  const auto dto =
98  navigation::human::arondto::Human::FromAron(entityInstance.data());
99 
100  navigation::human::Human human;
101  fromAron(dto, human);
102  humans.push_back(human);
103  }
104  });
105  });
106 
107  return humans;
108  }
109 
111  asGroups(const armem::wm::ProviderSegment& providerSegment,
112  const DateTime& timestamp,
113  const Duration& maxAge)
114  {
116 
117  ARMARX_CHECK(not providerSegment.empty()) << "No entities";
118  ARMARX_CHECK(providerSegment.size() == 1) << "There should be only one entity!";
119 
120  providerSegment.forEachEntity(
121  [&humans, &timestamp, &maxAge](const armem::wm::Entity& entity)
122  {
123  const auto& entitySnapshot = entity.getLatestSnapshot();
124  ARMARX_CHECK(not entitySnapshot.empty()) << "No entity snapshot instances";
125 
126  entitySnapshot.forEachInstance(
127  [&](const armem::wm::EntityInstance& entityInstance)
128  {
129  const Duration dtToNow = timestamp - entityInstance.metadata().referencedTime;
130 
131  if (dtToNow < maxAge and dtToNow.isPositive())
132  {
133  const auto dto = navigation::human::arondto::HumanGroup::FromAron(
134  entityInstance.data());
135 
136  navigation::human::HumanGroup human;
137  fromAron(dto, human);
138  humans.push_back(human);
139  }
140  });
141  });
142 
143  return humans;
144  }
145 
146  Reader::HumanGroupResult
147  Reader::queryHumanGroups(const Query& query) const
148  {
149  const auto qb = buildHumansQuery(query);
150 
151  ARMARX_DEBUG << "[MappingDataReader] query ... ";
152 
153  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
154 
155  ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
156 
157  if (not qResult.success)
158  {
159  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
160  return {.groups = {},
161  .status = HumanGroupResult::Status::Error,
162  .errorMessage = qResult.errorMessage};
163  }
164 
165  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
166 
167  if (not coreSegment.hasProviderSegment(query.providerName))
168  {
169  ARMARX_DEBUG << "Provider segment `" << query.providerName << "` does not exist (yet).";
170  return {.groups = {}, .status = HumanGroupResult::Status::NoData};
171  }
172 
173  const armem::wm::ProviderSegment& providerSegment =
174  coreSegment.getProviderSegment(query.providerName);
175 
176  if (providerSegment.empty())
177  {
178  ARMARX_DEBUG << "No entities.";
179  return {.groups = {},
180  .status = HumanGroupResult::Status::NoData,
181  .errorMessage = "No entities"};
182  }
183 
184  try
185  {
186  return HumanGroupResult{.groups =
187  asGroups(providerSegment, query.timestamp, query.maxAge),
188  .status = HumanGroupResult::Status::Success};
189  }
190  catch (...)
191  {
192  return HumanGroupResult{.groups = {},
193  .status = HumanGroupResult::Status::Error,
194  .errorMessage = GetHandledExceptionString()};
195  }
196  }
197 
198 
200  Reader::queryHumans(const Query& query) const
201  {
202  const auto qb = buildHumansQuery(query);
203 
204  ARMARX_DEBUG << "[MappingDataReader] query ... ";
205 
206  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
207 
208  ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
209 
210  if (not qResult.success)
211  {
212  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
213  return {.humans = {},
214  .status = HumanResult::Status::Error,
215  .errorMessage = qResult.errorMessage};
216  }
217 
218  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
219 
220  if (not coreSegment.hasProviderSegment(query.providerName))
221  {
222  ARMARX_DEBUG << "Provider segment `" << query.providerName
223  << "` does not exist (yet).";
224  return {.humans = {}, .status = HumanResult::Status::NoData};
225  }
226 
227  const armem::wm::ProviderSegment& providerSegment =
228  coreSegment.getProviderSegment(query.providerName);
229 
230  if (providerSegment.empty())
231  {
232  ARMARX_VERBOSE << "No entities.";
233  return {
234  .humans = {}, .status = HumanResult::Status::NoData, .errorMessage = "No entities"};
235  }
236 
237  try
238  {
239  return HumanResult{.humans = asHumans(providerSegment, query.timestamp, query.maxAge),
240  .status = HumanResult::Status::Success};
241  }
242  catch (...)
243  {
244  return HumanResult{.humans = {},
245  .status = HumanResult::Status::Error,
246  .errorMessage = GetHandledExceptionString()};
247  }
248  }
249 
250 } // namespace armarx::navigation::memory::client::human
armarx::navigation::memory::client::human::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: Reader.cpp:62
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:44
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::armem::client::util::SimpleReaderBase::properties
const Properties & properties() const
Definition: SimpleReaderBase.cpp:55
armarx::armem::client::query::ProviderSegmentSelector::entities
EntitySelector & entities()
Start specifying entities.
Definition: selectors.cpp:123
Reader.h
armarx::navigation::memory::client::human::Reader::Query
Definition: Reader.h:42
LocalException.h
armarx::armem::wm::ProviderSegment
Client-side working memory provider segment.
Definition: memory_definitions.h:105
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
aron_conversions.h
armarx::armem::base::detail::GetLatestSnapshotMixin::getLatestSnapshot
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.
Definition: lookup_mixins.h:199
armarx::armem::client::query::EntitySelector::snapshots
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition: selectors.cpp:86
armarx::navigation::memory::client::human
Definition: Reader.cpp:24
armarx::navigation::memory::client::human::Reader::Query::providerName
std::string providerName
Definition: Reader.h:44
armarx::navigation::memory::client::human::Reader::HumanResult
Definition: Reader.h:49
armarx::navigation::memory::client::human::Reader::HumanGroupResult
Definition: Reader.h:68
types.h
armarx::navigation::memory::constants::NavigationMemoryName
const std::string NavigationMemoryName
Definition: constants.h:27
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
Query.h
armarx::navigation::memory::client::human::Reader::defaultProperties
Properties defaultProperties() const override
Definition: Reader.cpp:68
armarx::armem::client::util::SimpleReaderBase::Properties::memoryName
std::string memoryName
Definition: SimpleReaderBase.h:45
armarx::navigation::human::Humans
std::vector< Human > Humans
Definition: types.h:45
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::navigation::memory::client::human::Reader::queryHumans
HumanResult queryHumans(const Query &query) const
Definition: Reader.cpp:200
armarx::navigation::memory::client::human::asHumans
navigation::human::Humans asHumans(const armem::wm::ProviderSegment &providerSegment, const DateTime &timestamp, const Duration &maxAge)
Definition: Reader.cpp:75
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:195
Costmap.h
armarx::armem::base::detail::MemoryContainerBase::size
std::size_t size() const
Definition: MemoryContainerBase.h:48
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:147
armarx::armem::client::query::Builder::coreSegments
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition: Builder.cpp:38
armarx::navigation::memory::client::human::asGroups
navigation::human::HumanGroups asGroups(const armem::wm::ProviderSegment &providerSegment, const DateTime &timestamp, const Duration &maxAge)
Definition: Reader.cpp:111
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
selectors.h
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:177
aron_conversions.h
armarx::armem::client::util::SimpleReaderBase::memoryReader
const armem::client::Reader & memoryReader() const
Definition: SimpleReaderBase.cpp:49
armarx::navigation::memory::constants::HumanCoreSegmentName
const std::string HumanCoreSegmentName
Definition: constants.h:35
memory_definitions.h
armarx::navigation::memory::client::human::Reader::HumanGroupResult::groups
armarx::navigation::human::HumanGroups groups
Definition: Reader.h:70
ExpressionException.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::navigation::memory::client::human::Reader::HumanResult::humans
armarx::navigation::human::Humans humans
Definition: Reader.h:51
Exception.h
armarx::armem::client::util::SimpleReaderBase::Properties
Definition: SimpleReaderBase.h:43
armarx::armem::client::query::EntitySelector::withName
EntitySelector & withName(const std::string &name) override
Definition: selectors.cpp:103
armarx::armem::client::query::ProviderSegmentSelector::withName
ProviderSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:140
armarx::navigation::memory::client::human::Reader::buildHumanGroupsQuery
::armarx::armem::client::query::Builder buildHumanGroupsQuery(const Query &query) const
Definition: Reader.cpp:45
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:189
armarx::armem::client::query::SnapshotSelector::beforeOrAtTime
SnapshotSelector & beforeOrAtTime(Time timestamp)
Definition: selectors.cpp:68
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
Builder.h
constants.h
armarx::navigation::memory::client::human::Reader::~Reader
~Reader() override
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
armarx::armem::wm::Entity
Client-side working memory entity.
Definition: memory_definitions.h:93
armarx::armem::base::EntityInstanceBase::metadata
MetadataT & metadata()
Definition: EntityInstanceBase.h:117
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::navigation::memory::client::human::Reader::buildHumansQuery
::armarx::armem::client::query::Builder buildHumansQuery(const Query &query) const
Definition: Reader.cpp:29
armarx::navigation::memory::client::human::Reader::queryHumanGroups
HumanGroupResult queryHumanGroups(const Query &query) const
Definition: Reader.cpp:147
armarx::navigation::human::HumanGroups
std::vector< HumanGroup > HumanGroups
Definition: types.h:58
armarx::navigation::memory::client::human::Reader::Query::timestamp
armem::Time timestamp
Definition: Reader.h:45
armarx::navigation::memory::client::human::Reader::Query::maxAge
Duration maxAge
Definition: Reader.h:46
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query.
Definition: Reader.cpp:33
NDArray.h