Reader.cpp
Go to the documentation of this file.
1 #include "Reader.h"
2 
3 // STD / STL
4 #include <algorithm>
5 #include <cstring>
6 #include <map>
7 #include <optional>
8 #include <ostream>
9 #include <type_traits>
10 #include <utility>
11 #include <vector>
12 
13 // ArmarXCore
15 
16 #include <armarx/navigation/memory/aron/LaserScannerFeatures.aron.generated.h>
17 
18 // RobotAPI Armem
25 #include <RobotAPI/libraries/armem_laser_scans/aron/LaserScan.aron.generated.h>
26 
29 
31 {
32 
33  Reader::~Reader() = default;
34 
35  std::string
37  {
38  return "mem.nav.laser_scanner_features.";
39  }
40 
43  {
46  }
47 
49  Reader::buildQuery(const Query& query) const
50  {
52 
53  auto& sel = qb.coreSegments()
54  .withName(properties().coreSegmentName)
56  .withName(query.providerName);
57 
58  auto& entitySel = [&]() -> armem::client::query::EntitySelector&
59  {
60  if (query.name.empty())
61  {
62  ARMARX_DEBUG << "querying all entities";
63  return sel.entities().all();
64  }
65  return sel.entities().withName(query.name);
66  }();
67 
68  entitySel.snapshots().beforeOrAtTime(query.timestamp);
69 
70  return qb;
71  }
72 
73  std::vector<LaserScannerFeatures>
75  {
76  if (providerSegment.empty())
77  {
78  ARMARX_WARNING << "No entities!";
79  return {};
80  }
81 
82  // const auto convert = [](const auto& aronLaserScanStamped,
83  // const wm::EntityInstance& ei) -> LaserScanStamped
84  // {
85  // LaserScanStamped laserScanStamped;
86  // fromAron(aronLaserScanStamped, laserScanStamped);
87 
88  // const auto ndArrayNavigator =
89  // aron::data::NDArray::DynamicCast(ei.data()->getElement("scan"));
90 
91  // ARMARX_CHECK_NOT_NULL(ndArrayNavigator);
92 
93  // laserScanStamped.data = fromAron<LaserScanStep>(ndArrayNavigator);
94  // ARMARX_IMPORTANT << "4";
95 
96  // return laserScanStamped;
97  // };
98 
99  std::vector<LaserScannerFeatures> laserScannerFeatures;
100 
101  // loop over all entities and their snapshots
102  providerSegment.forEachEntity(
103  [&](const armem::wm::Entity& entity)
104  {
105  // If we don't need this warning, we could directly iterate over the snapshots.
106  if (entity.empty())
107  {
108  ARMARX_WARNING << "Empty history for " << entity.id();
109  }
110  ARMARX_DEBUG << "History size: " << entity.size();
111 
112  entity.forEachInstance(
113  [&](const armem::wm::EntityInstance& entityInstance)
114  {
115  if (const auto o =
116  armem::tryCast<arondto::LaserScannerFeatures>(entityInstance))
117  {
118  LaserScannerFeatures& f = laserScannerFeatures.emplace_back();
119  fromAron(o.value(), f);
120  }
121  return true;
122  });
123  return true;
124  });
125 
126  return laserScannerFeatures;
127  }
128 
129  Reader::Result
130  Reader::queryData(const Query& query) const
131  {
132  const auto qb = buildQuery(query);
133 
134  ARMARX_DEBUG << "[MappingDataReader] query ... ";
135 
136  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
137 
138  ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
139 
140  if (not qResult.success)
141  {
142  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
143  return {.features = {},
144  .status = Result::Status::Error,
145  .errorMessage = qResult.errorMessage};
146  }
147 
148  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
149  if (not coreSegment.hasProviderSegment(query.providerName))
150  {
151  ARMARX_DEBUG << "Provider segment `" << query.providerName
152  << "` does not exist (yet).";
153  return {.features = {},
154  .status = Result::Status::Error,
155  .errorMessage = "Provider segment " + query.providerName + " does not exist"};
156  }
157 
158  // now create result from memory
159  const armem::wm::ProviderSegment& providerSegment =
160  qResult.memory.getCoreSegment(properties().coreSegmentName)
161  .getProviderSegment(query.providerName);
162 
163  const auto features = asFeaturesList(providerSegment);
164 
165  // const auto laserScans = asLaserScans(providerSegment);
166  // std::vector<std::string> sensors;
167  // providerSegment.forEachEntity(
168  // [&sensors](const wm::Entity& entity)
169  // {
170  // sensors.push_back(entity.name());
171  // return true;
172  // });
173 
174  return {.features = features,
175  // .sensors = sensors,
176  .status = Result::Status::Success,
177  .errorMessage = ""};
178  }
179 
180 } // namespace armarx::navigation::memory::client::laser_scanner_features
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:44
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:27
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:48
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:42
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
error.h
armarx::navigation::memory::client::laser_scanner_features::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: Reader.cpp:36
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:30
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:140
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:74
armarx::armem::base::detail::ForEachEntityInstanceMixin::forEachInstance
bool forEachInstance(InstanceFunctionT &&func)
Definition: iteration_mixins.h:146
Builder.h
armarx::navigation::memory::client::laser_scanner_features::Reader::buildQuery
armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition: Reader.cpp:49
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::client::query::EntitySelector
Definition: selectors.h:42
armarx::armem::wm::Entity
Client-side working memory entity.
Definition: memory_definitions.h:93
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::navigation::memory::constants::LaserScannerFeaturesCoreSegment
const std::string LaserScannerFeaturesCoreSegment
Definition: constants.h:36