Reader.cpp
Go to the documentation of this file.
1 #include "Reader.h"
2 
3 #include <cstring>
4 #include <optional>
5 #include <string>
6 #include <vector>
7 
9 
16 
17 #include <armarx/navigation/memory/aron/LaserScannerFeatures.aron.generated.h>
20 
22 {
23 
24  Reader::~Reader() = default;
25 
26  std::string
28  {
29  return "mem.nav.laser_scanner_features.";
30  }
31 
34  {
37  }
38 
40  Reader::buildQuery(const Query& query) const
41  {
43 
44  auto& sel = qb.coreSegments()
45  .withName(properties().coreSegmentName)
47  .withName(query.providerName);
48 
49  auto& entitySel = [&]() -> armem::client::query::EntitySelector&
50  {
51  if (query.name.empty())
52  {
53  ARMARX_DEBUG << "querying all entities";
54  return sel.entities().all();
55  }
56  return sel.entities().withName(query.name);
57  }();
58 
59  entitySel.snapshots().beforeOrAtTime(query.timestamp);
60 
61  return qb;
62  }
63 
64  std::vector<LaserScannerFeatures>
66  {
67  if (providerSegment.empty())
68  {
69  ARMARX_VERBOSE << "No entities!";
70  return {};
71  }
72 
73  // const auto convert = [](const auto& aronLaserScanStamped,
74  // const wm::EntityInstance& ei) -> LaserScanStamped
75  // {
76  // LaserScanStamped laserScanStamped;
77  // fromAron(aronLaserScanStamped, laserScanStamped);
78 
79  // const auto ndArrayNavigator =
80  // aron::data::NDArray::DynamicCast(ei.data()->getElement("scan"));
81 
82  // ARMARX_CHECK_NOT_NULL(ndArrayNavigator);
83 
84  // laserScanStamped.data = fromAron<LaserScanStep>(ndArrayNavigator);
85  // ARMARX_IMPORTANT << "4";
86 
87  // return laserScanStamped;
88  // };
89 
90  std::vector<LaserScannerFeatures> laserScannerFeatures;
91 
92  // loop over all entities and their snapshots
93  providerSegment.forEachEntity(
94  [&](const armem::wm::Entity& entity)
95  {
96  // If we don't need this warning, we could directly iterate over the snapshots.
97  if (entity.empty())
98  {
99  ARMARX_VERBOSE << "Empty history for " << entity.id();
100  }
101  ARMARX_DEBUG << "History size: " << entity.size();
102 
103  entity.forEachInstance(
104  [&](const armem::wm::EntityInstance& entityInstance)
105  {
106  if (const auto o =
107  armem::tryCast<arondto::LaserScannerFeatures>(entityInstance))
108  {
109  LaserScannerFeatures& f = laserScannerFeatures.emplace_back();
110  fromAron(o.value(), f);
111  }
112  return true;
113  });
114  return true;
115  });
116 
117  return laserScannerFeatures;
118  }
119 
120  Reader::Result
121  Reader::queryData(const Query& query) const
122  {
123  const auto qb = buildQuery(query);
124 
125  ARMARX_DEBUG << "[MappingDataReader] query ... ";
126 
127  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
128 
129  ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
130 
131  if (not qResult.success)
132  {
133  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
134  return {.features = {},
135  .status = Result::Status::Error,
136  .errorMessage = qResult.errorMessage};
137  }
138 
139  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
140  if (not coreSegment.hasProviderSegment(query.providerName))
141  {
142  ARMARX_DEBUG << "Provider segment `" << query.providerName << "` does not exist (yet).";
143  return {.features = {},
144  .status = Result::Status::Error,
145  .errorMessage = "Provider segment " + query.providerName + " does not exist"};
146  }
147 
148  // now create result from memory
149  const armem::wm::ProviderSegment& providerSegment =
150  qResult.memory.getCoreSegment(properties().coreSegmentName)
151  .getProviderSegment(query.providerName);
152 
153  const auto features = asFeaturesList(providerSegment);
154 
155  // const auto laserScans = asLaserScans(providerSegment);
156  // std::vector<std::string> sensors;
157  // providerSegment.forEachEntity(
158  // [&sensors](const wm::Entity& entity)
159  // {
160  // sensors.push_back(entity.name());
161  // return true;
162  // });
163 
164  return {.features = features,
165  // .sensors = sensors,
166  .status = Result::Status::Success,
167  .errorMessage = ""};
168  }
169 
170 } // namespace armarx::navigation::memory::client::laser_scanner_features
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:41
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::armem::client::util::SimpleReaderBase::properties
const Properties & properties() const
Definition: SimpleReaderBase.cpp:55
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
armarx::navigation::memory::constants::NavigationMemoryName
const std::string NavigationMemoryName
Definition: constants.h:29
armarx::navigation::memory::client::laser_scanner_features::Reader::Query::providerName
std::string providerName
Definition: Reader.h:58
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
Query.h
aron_conversions.h
armarx::armem::client::util::SimpleReaderBase::Properties::memoryName
std::string memoryName
Definition: SimpleReaderBase.h:45
Reader.h
armarx::armem::base::detail::MemoryContainerBase::size
std::size_t size() const
Definition: MemoryContainerBase.h:47
armarx::navigation::memory::client::laser_scanner_features::Reader::Query::name
std::string name
Definition: Reader.h:60
armarx::navigation::memory::client::laser_scanner_features::Reader::defaultProperties
Properties defaultProperties() const override
Definition: Reader.cpp:33
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::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:198
armarx::navigation::memory::client::laser_scanner_features::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: Reader.cpp:27
armarx::navigation::memory::client::laser_scanner_features::Reader::Query
Definition: Reader.h:56
armarx::navigation::memory::client::laser_scanner_features::Reader::Query::timestamp
armarx::core::time::DateTime timestamp
Definition: Reader.h:62
memory_definitions.h
armarx::navigation::memory::client::laser_scanner_features
Definition: Reader.cpp:21
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:155
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:189
armarx::navigation::memory::client::laser_scanner_features::Reader::~Reader
virtual ~Reader()
armarx::navigation::memory::client::laser_scanner_features::asFeaturesList
std::vector< LaserScannerFeatures > asFeaturesList(const armem::wm::ProviderSegment &providerSegment)
Definition: Reader.cpp:65
armarx::armem::base::detail::ForEachEntityInstanceMixin::forEachInstance
bool forEachInstance(InstanceFunctionT &&func)
Definition: iteration_mixins.h:147
Builder.h
armarx::navigation::memory::client::laser_scanner_features::Reader::buildQuery
armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition: Reader.cpp:40
constants.h
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:21
armarx::armem::client::query::CoreSegmentSelector::providerSegments
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
Definition: selectors.cpp:178
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_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
SimpleReaderBase.h
armarx::navigation::memory::constants::LaserScannerFeaturesCoreSegment
const std::string LaserScannerFeaturesCoreSegment
Definition: constants.h:38