Reader.cpp
Go to the documentation of this file.
1#include "Reader.h"
2
3#include <optional>
4#include <string>
5
9
16
20
22{
23 Reader::~Reader() = default;
24
27 {
29
30 // clang-format off
31 auto& sel = qb
32 .coreSegments().withName(properties().coreSegmentName)
33 .providerSegments().withName(query.providerName)
34 .entities();
35 if (query.name.empty()){
36 sel = sel.all();
37 }else{
38 sel = sel.withName(query.name);
39 }
40 sel.snapshots().beforeOrAtTime(query.timestamp);
41 // clang-format on
42
43 return qb;
44 }
45
46 std::string
48 {
49 return "mem.nav.costmap.";
50 }
51
58
61 {
62 ARMARX_CHECK_NOT_EMPTY(providerSegment) << "No entities";
63 ARMARX_CHECK_EQUAL(providerSegment.size(), 1) << "There should be only one entity!";
64
65 const armem::wm::EntityInstance* entityInstance = nullptr;
66 providerSegment.forEachEntity(
67 [&entityInstance](const armem::wm::Entity& entity)
68 {
69 const auto& entitySnapshot = entity.getLatestSnapshot();
70 ARMARX_CHECK(not entitySnapshot.empty()) << "No entity snapshot instances";
71
72 entityInstance = &entitySnapshot.getInstance(0);
73 return false;
74 });
75 ARMARX_CHECK_NOT_NULL(entityInstance);
76 return algorithms::costmap3dFromAron(*entityInstance);
77 }
78
79 Reader::Result
81 {
82 const auto qb = buildQuery(query);
83
84 ARMARX_DEBUG << "[MappingDataReader] query ... ";
85
86 const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
87
88 ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
89
90 if (not qResult.success)
91 {
92 ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
93 return {.costmap = std::nullopt,
94 .status = Result::Status::Error,
95 .errorMessage = qResult.errorMessage};
96 }
97
98 const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
99
100 if (not coreSegment.hasProviderSegment(query.providerName))
101 {
102 ARMARX_VERBOSE << "Provider segment `" << query.providerName
103 << "` does not exist (yet).";
104 return {.costmap = std::nullopt, .status = Result::Status::NoData};
105 }
106
107 const armem::wm::ProviderSegment& providerSegment =
108 coreSegment.getProviderSegment(query.providerName);
109
110 if (providerSegment.empty())
111 {
112 ARMARX_VERBOSE << "No entities.";
113 return {.costmap = std::nullopt,
114 .status = Result::Status::NoData,
115 .errorMessage = "No entities"};
116 }
117
118 try
119 {
120 return Result{.costmap = asCostmap3D(providerSegment), .status = Result::Status::Success};
121 }
122 catch (...)
123 {
124 return Result{.status = Result::Status::Error,
125 .errorMessage = GetHandledExceptionString()};
126 }
127 }
128
129} // namespace armarx::navigation::memory::client::costmap3d
#define ARMARX_CHECK_NOT_EMPTY(c)
CoreSegmentT & getCoreSegment(const std::string &name)
Definition MemoryBase.h:134
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
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition selectors.cpp:92
ProviderSegmentSelector & withName(const std::string &name) override
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.
::armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition Reader.cpp:26
Result query(const Query &query) const
Definition Reader.cpp:80
#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
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
orientation_aware::Costmap3D costmap3dFromAron(const armem::wm::EntityInstance &entityInstance)
algorithms::orientation_aware::Costmap3D asCostmap3D(const armem::wm::ProviderSegment &providerSegment)
Definition Reader.cpp:60
constexpr const char * Costmap3DCoreSegmentName
Definition constants.h:37
constexpr const char * NavigationMemoryName
Definition constants.h:29
std::string GetHandledExceptionString()
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