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/rooms/aron/Room.aron.generated.h>
22 
24 {
25  Reader::~Reader() = default;
26 
28  Reader::buildQuery(const Query& query) const
29  {
31 
32  // clang-format off
33  qb
35  .providerSegments().withName(query.providerName)
36  .entities().withName(query.name)
37  .snapshots().beforeOrAtTime(query.timestamp);
38  // clang-format on
39 
40  return qb;
41  }
42 
43  std::string
45  {
46  return "mem.nav.rooms.";
47  }
48 
51  {
53  .coreSegmentName = memory::constants::RoomsCoreSegmentName};
54  }
55 
57  asRoom(const armem::wm::ProviderSegment& providerSegment)
58  {
59  ARMARX_CHECK(not providerSegment.empty()) << "No entities";
60  ARMARX_CHECK_EQUAL(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 
74  const auto aronDto = armem::tryCast<navigation::rooms::arondto::Room>(*entityInstance);
75  ARMARX_CHECK(aronDto) << "Failed casting to Room";
76  const navigation::rooms::arondto::Room& dto = *aronDto;
77 
79  navigation::rooms::fromAron(dto, room);
80 
81  return room;
82  }
83 
84  Reader::Result
85  Reader::query(const Query& query) const
86  {
87  const auto qb = buildQuery(query);
88 
89  ARMARX_DEBUG << "[rooms::Reader] query ... ";
90 
91  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
92 
93  ARMARX_DEBUG << "[rooms::Reader] result: " << qResult;
94 
95  if (not qResult.success)
96  {
97  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
98  return {.room = std::nullopt,
99  .status = Result::Status::Error,
100  .errorMessage = qResult.errorMessage};
101  }
102 
103  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
104 
105  if (not coreSegment.hasProviderSegment(query.providerName))
106  {
107  ARMARX_WARNING << "Provider segment `" << query.providerName
108  << "` does not exist (yet).";
109  return {.room = std::nullopt, .status = Result::Status::NoData};
110  }
111 
112  const armem::wm::ProviderSegment& providerSegment =
113  coreSegment.getProviderSegment(query.providerName);
114 
115  if (providerSegment.empty())
116  {
117  ARMARX_WARNING << "No entities.";
118  return {.room = std::nullopt,
119  .status = Result::Status::NoData,
120  .errorMessage = "No entities"};
121  }
122 
123  try
124  {
125  return Result{.room = asRoom(providerSegment), .status = Result::Status::Success};
126  }
127  catch (...)
128  {
129  return Result{.status = Result::Status::Error,
130  .errorMessage = GetHandledExceptionString()};
131  }
132  }
133 
134 } // namespace armarx::navigation::memory::client::rooms
armarx::navigation::memory::client::rooms::Reader::~Reader
~Reader() override
armarx::navigation::memory::client::rooms
Definition: Reader.cpp:23
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:44
armarx::navigation::memory::constants::RoomsCoreSegmentName
const std::string RoomsCoreSegmentName
Definition: constants.h:40
armarx::armem::client::util::SimpleReaderBase::properties
const Properties & properties() const
Definition: SimpleReaderBase.cpp:55
armarx::navigation::memory::client::rooms::Reader::Result::status
enum armarx::navigation::memory::client::rooms::Reader::Result::Status status
armarx::navigation::memory::client::rooms::asRoom
armarx::navigation::rooms::Room asRoom(const armem::wm::ProviderSegment &providerSegment)
Definition: Reader.cpp:57
armarx::armem::client::query::ProviderSegmentSelector::entities
EntitySelector & entities()
Start specifying entities.
Definition: selectors.cpp:123
armarx::navigation::memory::client::rooms::Reader::Result
Definition: Reader.h:48
Reader.h
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::navigation::memory::client::rooms::Reader::Query
Definition: Reader.h:41
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::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::armem::client::util::SimpleReaderBase::Properties::memoryName
std::string memoryName
Definition: SimpleReaderBase.h:45
armarx::navigation::memory::client::rooms::Reader::buildQuery
::armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition: Reader.cpp:28
armarx::navigation::memory::client::rooms::Reader::defaultProperties
Properties defaultProperties() const override
Definition: Reader.cpp:50
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
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::rooms::Reader::Result::room
std::optional< armarx::navigation::rooms::Room > room
Definition: Reader.h:50
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
memory_definitions.h
ExpressionException.h
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::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:189
armarx::navigation::rooms::fromAron
void fromAron(const arondto::Room &dto, Room &bo)
Definition: aron_conversions.cpp:19
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_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::navigation::memory::client::rooms::Reader::query
Result query(const Query &query) const
Definition: Reader.cpp:85
armarx::navigation::rooms::Room
Definition: types.h:31
armarx::navigation::memory::client::rooms::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: Reader.cpp:44
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query.
Definition: Reader.cpp:33
aron_conversions.h
NDArray.h