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>
8 
11 
18 
22 
24 {
25  Reader::~Reader() = default;
26 
28  Reader::buildQuery(const Query& query) const
29  {
31 
32  // clang-format off
33  qb
34  .coreSegments().withName(properties().coreSegmentName)
35  .providerSegments().withName(query.providerName)
36  .entities().all()
37  .snapshots().beforeOrAtTime(query.timestamp);
38  // clang-format on
39 
40  return qb;
41  }
42 
43  std::string
45  {
46  return "mem.nav.costmap.";
47  }
48 
51  {
54  }
55 
57  asCostmap(const armem::wm::ProviderSegment& providerSegment)
58  {
59  ARMARX_CHECK(not providerSegment.empty()) << "No entities";
60  ARMARX_CHECK(providerSegment.size() == 1) << "There should be only one entity!";
61 
62  const armem::wm::EntityInstance* entityInstance = nullptr;
63  providerSegment.forEachEntity(
64  [&entityInstance](const armem::wm::Entity& entity)
65  {
66  const auto& entitySnapshot = entity.getLatestSnapshot();
67  ARMARX_CHECK(not entitySnapshot.empty()) << "No entity snapshot instances";
68 
69  entityInstance = &entitySnapshot.getInstance(0);
70  return false;
71  });
72  ARMARX_CHECK_NOT_NULL(entityInstance);
73  return algorithms::fromAron(*entityInstance);
74  }
75 
76  Reader::Result
77  Reader::query(const Query& query) const
78  {
79  const auto qb = buildQuery(query);
80 
81  ARMARX_DEBUG << "[MappingDataReader] query ... ";
82 
83  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
84 
85  ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
86 
87  if (not qResult.success)
88  {
89  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
90  return {.costmap = std::nullopt,
91  .status = Result::Status::Error,
92  .errorMessage = qResult.errorMessage};
93  }
94 
95  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
96 
97  if (not coreSegment.hasProviderSegment(query.providerName))
98  {
99  ARMARX_VERBOSE << "Provider segment `" << query.providerName
100  << "` does not exist (yet).";
101  return {.costmap = std::nullopt, .status = Result::Status::NoData};
102  }
103 
104  const armem::wm::ProviderSegment& providerSegment =
105  coreSegment.getProviderSegment(query.providerName);
106 
107  if (providerSegment.empty())
108  {
109  ARMARX_VERBOSE << "No entities.";
110  return {.costmap = std::nullopt,
111  .status = Result::Status::NoData,
112  .errorMessage = "No entities"};
113  }
114 
115  try
116  {
117  return Result{.costmap = asCostmap(providerSegment), .status = Result::Status::Success};
118  }
119  catch (...)
120  {
121  return Result{.status = Result::Status::Error,
122  .errorMessage = GetHandledExceptionString()};
123  }
124  }
125 
126 } // namespace armarx::navigation::memory::client::costmap
armarx::armem::client::query::EntitySelector::all
EntitySelector & all() override
Definition: selectors.cpp:96
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::costmap::Reader::buildQuery
::armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition: Reader.cpp:28
LocalException.h
armarx::armem::wm::ProviderSegment
Client-side working memory provider segment.
Definition: memory_definitions.h:105
armarx::navigation::memory::constants::CostmapCoreSegmentName
const std::string CostmapCoreSegmentName
Definition: constants.h:34
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
aron_conversions.h
armarx::navigation::algorithms::fromAron
Costmap fromAron(const armem::wm::EntityInstance &entityInstance)
Definition: aron_conversions.cpp:61
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_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::navigation::memory::client::costmap::Reader::~Reader
~Reader() override
armarx::navigation::memory::client::costmap
Definition: Reader.cpp:23
armarx::navigation::memory::client::costmap::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: Reader.cpp:44
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::costmap::Reader::query
Result query(const Query &query) const
Definition: Reader.cpp:77
armarx::armem::client::util::SimpleReaderBase::Properties::memoryName
std::string memoryName
Definition: SimpleReaderBase.h:45
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::navigation::memory::client::costmap::asCostmap
algorithms::Costmap asCostmap(const armem::wm::ProviderSegment &providerSegment)
Definition: Reader.cpp:57
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::navigation::memory::client::costmap::Reader::Result::costmap
std::optional< algorithms::Costmap > costmap
Definition: Reader.h:49
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
selectors.h
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:177
armarx::armem::client::util::SimpleReaderBase::memoryReader
const armem::client::Reader & memoryReader() const
Definition: SimpleReaderBase.cpp:49
armarx::navigation::memory::client::costmap::Reader::Result::status
enum armarx::navigation::memory::client::costmap::Reader::Result::Status status
armarx::navigation::memory::client::costmap::Reader::Query
Definition: Reader.h:40
memory_definitions.h
ExpressionException.h
armarx::navigation::memory::client::costmap::Reader::Result
Definition: Reader.h:47
Exception.h
armarx::armem::client::util::SimpleReaderBase::Properties
Definition: SimpleReaderBase.h:43
armarx::armem::client::query::ProviderSegmentSelector::withName
ProviderSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:140
armarx::navigation::memory::client::costmap::Reader::defaultProperties
Properties defaultProperties() const override
Definition: Reader.cpp:50
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
Builder.h
constants.h
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_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::navigation::algorithms::Costmap
Definition: Costmap.h:13
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query.
Definition: Reader.cpp:33
NDArray.h