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
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
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 {
69 return {.memoryName = memory::constants::NavigationMemoryName,
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 =
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
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
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
QueryResult query(const QueryInput &input) const
Perform a query on the WM.
Definition Reader.cpp:119
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition Builder.h:22
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition Builder.cpp:42
CoreSegmentSelector & withName(const std::string &name) override
EntitySelector & withName(const std::string &name) override
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition selectors.cpp:92
EntitySelector & entities()
Start specifying entities.
SnapshotSelector & beforeOrAtTime(Time timestamp)
Definition selectors.cpp:73
const armem::client::Reader & memoryReader() const
Client-side working entity instance.
Client-side working memory entity.
Client-side working memory provider segment.
Properties defaultProperties() const override
Definition Reader.cpp:67
std::string propertyPrefix() const override
Definition Reader.cpp:61
::armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition Reader.cpp:27
Result query(const Query &query) const
Definition Reader.cpp:103
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#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...
#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...
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
std::optional< AronClass > tryCast(const wm::EntityInstance &item)
Tries to cast a armem::EntityInstance to AronClass.
Definition util.h:45
armarx::navigation::algorithms::Room asRoom(const armem::wm::ProviderSegment &providerSegment)
Definition Reader.cpp:74
constexpr const char * RoomsCoreSegmentName
Definition constants.h:44
constexpr const char * NavigationMemoryName
Definition constants.h:29
void fromAron(const arondto::Circle &dto, Circle &bo)
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.
Result of a QueryInput.
Definition Query.h:51
wm::Memory memory
The slice of the memory that matched the query.
Definition Query.h:58
std::vector< armarx::navigation::algorithms::Room > rooms
Definition Reader.h:52
enum armarx::navigation::memory::client::rooms::Reader::Result::Status status