Reader.cpp
Go to the documentation of this file.
1 #include "Reader.h"
2 
3 #include <string>
4 
8 
16 
18 #include <armarx/navigation/algorithms/aron/Room.aron.generated.h>
21 
23 {
24  Reader::~Reader() = default;
25 
27  Reader::buildQuery(const Query& query) const
28  {
30 
31  auto& coreSegmentSelector =
33 
34  armem::client::query::ProviderSegmentSelector& providerSegmentSelector =
36  {
37  if (query.providerName)
38  {
39  return coreSegmentSelector.providerSegments().withName(*query.providerName);
40  }
41  return coreSegmentSelector.providerSegments().all();
42  }();
43 
44  armem::client::query::EntitySelector& entitiySelector =
46  {
47  if (query.name)
48  {
49  return providerSegmentSelector.entities().withName(*query.name);
50  }
51 
52  return providerSegmentSelector.entities().all();
53  }();
54 
55  entitiySelector.snapshots().beforeOrAtTime(query.timestamp);
56 
57  return qb;
58  }
59 
60  std::string
62  {
63  return "mem.nav.rooms.";
64  }
65 
68  {
70  .coreSegmentName = memory::constants::RoomsCoreSegmentName};
71  }
72 
74  asRoom(const armem::wm::ProviderSegment& providerSegment)
75  {
76  ARMARX_CHECK(not providerSegment.empty()) << "No entities";
77  ARMARX_CHECK_EQUAL(providerSegment.size(), 1) << "There should be only one entity!";
78 
79  const armem::wm::EntityInstance* entityInstance = nullptr;
80  providerSegment.forEachEntity(
81  [&entityInstance](const armem::wm::Entity& entity)
82  {
83  const auto& entitySnapshot = entity.getLatestSnapshot();
84  ARMARX_CHECK(not entitySnapshot.empty()) << "No entity snapshot instances";
85 
86  entityInstance = &entitySnapshot.getInstance(0);
87  return false;
88  });
89  ARMARX_CHECK_NOT_NULL(entityInstance);
90 
91  const auto aronDto =
92  armem::tryCast<armarx::navigation::algorithms::arondto::Room>(*entityInstance);
93  ARMARX_CHECK(aronDto) << "Failed casting to Room";
94  const armarx::navigation::algorithms::arondto::Room& dto = *aronDto;
95 
97  fromAron(dto, room);
98 
99  return room;
100  }
101 
102  Reader::Result
103  Reader::query(const Query& query) const
104  {
105  const auto qb = buildQuery(query);
106 
107  ARMARX_DEBUG << "[rooms::Reader] query ... ";
108 
109  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
110 
111  ARMARX_DEBUG << "[rooms::Reader] result: " << qResult;
112 
113  if (not qResult.success)
114  {
115  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
116  return {
117  .rooms = {}, .status = Result::Status::Error, .errorMessage = qResult.errorMessage};
118  }
119 
120 
121  Reader::Result result;
122  // result.rooms
123 
124  qResult.memory.forEachInstance(
125  [&](const armem::wm::EntityInstance& instance)
126  {
127  const auto dto = algorithms::arondto::Room::FromAron(instance.data());
128 
130  fromAron(dto, room);
131  result.rooms.push_back(room);
132  });
133 
134  if (result.rooms.empty())
135  {
136  ARMARX_WARNING << "No entities.";
137  return {.rooms = {}, .status = Result::Status::NoData};
138  }
139 
140  result.status = Result::Status::Success;
141  return result;
142 
143 
144  // const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
145 
146  // if (not coreSegment.hasProviderSegment(query.providerName))
147  // {
148  // ARMARX_WARNING << "Provider segment `" << query.providerName
149  // << "` does not exist (yet).";
150  // return {.rooms = {}, .status = Result::Status::NoData};
151  // }
152 
153  // const armem::wm::ProviderSegment& providerSegment =
154  // coreSegment.getProviderSegment(query.providerName);
155 
156  // if (providerSegment.empty())
157  // {
158  // ARMARX_WARNING << "No entities.";
159  // return {.room = std::nullopt,
160  // .status = Result::Status::NoData,
161  // .errorMessage = "No entities"};
162  // }
163 
164  // try
165  // {
166  // return Result{.room = asRoom(providerSegment), .status = Result::Status::Success};
167  // }
168  // catch (...)
169  // {
170  // return Result{.status = Result::Status::Error,
171  // .errorMessage = GetHandledExceptionString()};
172  // }
173  }
174 
175 } // namespace armarx::navigation::memory::client::rooms
armarx::navigation::memory::client::rooms::Reader::~Reader
~Reader() override
armarx::armem::client::query::EntitySelector::all
EntitySelector & all() override
Definition: selectors.cpp:104
armarx::armem::client::query::ProviderSegmentSelector
Definition: selectors.h:79
armarx::navigation::memory::client::rooms
Definition: Reader.cpp:22
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:41
armarx::navigation::memory::constants::RoomsCoreSegmentName
const std::string RoomsCoreSegmentName
Definition: constants.h:43
armarx::navigation::memory::client::rooms::Reader::Result::status
enum armarx::navigation::memory::client::rooms::Reader::Result::Status status
armarx::armem::client::query::ProviderSegmentSelector::entities
EntitySelector & entities()
Start specifying entities.
Definition: selectors.cpp:135
armarx::navigation::memory::client::rooms::Reader::Result
Definition: Reader.h:50
Reader.h
LocalException.h
armarx::armem::wm::ProviderSegment
Client-side working memory provider segment.
Definition: memory_definitions.h:105
armarx::navigation::memory::fromAron
void fromAron(const arondto::Circle &dto, Circle &bo)
Definition: aron_conversions.cpp:56
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
aron_conversions.h
armarx::navigation::memory::client::rooms::Reader::Result::rooms
std::vector< armarx::navigation::algorithms::Room > rooms
Definition: Reader.h:52
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:43
armarx::armem::client::query::EntitySelector::snapshots
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition: selectors.cpp:92
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:29
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:27
armarx::navigation::memory::client::rooms::Reader::defaultProperties
Properties defaultProperties() const override
Definition: Reader.cpp:67
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::armem::base::detail::MemoryContainerBase::size
std::size_t size() const
Definition: MemoryContainerBase.h:47
armarx::armem::client::query::Builder::coreSegments
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition: Builder.cpp:42
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
selectors.h
armarx::navigation::algorithms::Room
Definition: Room.h:33
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:198
armarx::armem::client::util::SimpleReaderBase::memoryReader
const armem::client::Reader & memoryReader() const
Definition: SimpleReaderBase.cpp:49
memory_definitions.h
ExpressionException.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:112
armarx::armem::client::query::ProviderSegmentSelector::withName
ProviderSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:155
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:73
armarx::navigation::memory::client::rooms::asRoom
armarx::navigation::algorithms::Room asRoom(const armem::wm::ProviderSegment &providerSegment)
Definition: Reader.cpp:74
Builder.h
constants.h
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:21
util.h
armarx::armem::client::query::EntitySelector
Definition: selectors.h:39
armarx::armem::wm::Entity
Client-side working memory entity.
Definition: memory_definitions.h:93
Logging.h
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:193
armarx::armem::base::EntityInstanceBase::data
const DataT & data() const
Definition: EntityInstanceBase.h:129
SimpleReaderBase.h
Room.h
armarx::navigation::memory::client::rooms::Reader::query
Result query(const Query &query) const
Definition: Reader.cpp:103
armarx::armem::client::query::ProviderSegmentSelector::all
ProviderSegmentSelector & all() override
Definition: selectors.cpp:147
armarx::navigation::memory::client::rooms::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: Reader.cpp:61
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query.
Definition: Reader.cpp:32