Reader.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 // STD/STL
5 #include <mutex>
6 #include <optional>
7 #include <vector>
8 
9 // RobotAPI
10 #include <RobotAPI/interface/armem/server/ReadingMemoryInterface.h>
11 #include <RobotAPI/interface/armem/server/RecordingMemoryInterface.h>
15 
16 #include "Query.h"
17 
18 namespace armarx::armem::client
19 {
20 
21  /**
22  * @brief Reads data from a memory server.
23  */
24  class Reader
25  {
26 
27  public:
28  /**
29  * @brief Construct a memory reader.
30  * @param memory The memory proxy.
31  */
32  Reader(const Reader&) = default;
33  Reader(server::ReadingMemoryInterfacePrx readingMemory = nullptr,
34  server::PredictingMemoryInterfacePrx predictingMemory = nullptr);
35 
36  void setReadingMemory(server::ReadingMemoryInterfacePrx readingMemory);
37  void setPredictingMemory(server::PredictingMemoryInterfacePrx predictingMemory);
38 
39  /// Perform a query.
40  QueryResult query(const QueryInput& input) const;
41  armem::query::data::Result query(const armem::query::data::Input& input) const;
42 
43  QueryResult query(armem::query::data::MemoryQueryPtr query,
45  QueryResult query(const armem::query::data::MemoryQuerySeq& queries,
47 
48  QueryResult query(const QueryBuilder& queryBuilder) const;
49 
50  /** Perform a query with recursive memory link resolution.
51  *
52  * Resolves the links in the query result using the given MemoryNameSystem
53  * and inserts the data into the MemoryLink objects' data fields.
54  * If the inserted data also contains links, those will be resolved as well
55  * up to and including the given recursion depth. Link cycles are detected;
56  * the first repeated memory ID will not be inserted into the data.
57  * Setting the recursion depth to `-1` is equivalent to an infinite
58  * recursion depth.
59  *
60  * Standalone `MemoryID` subobjects are ignored; only MemoryIDs inside of
61  * MemoryLink subobjects are resolved. Empty or unresolvable MemoryIDs
62  * are ignored - this includes MemoryIDs whose Entity fields are not set.
63  * If the data associated with a MemoryID could not be retrieved or
64  * its type does not match the template type of the MemoryLink,
65  * it is ignored.
66  *
67  * @param input the query to perform
68  * @param mns the MemoryNameSystem to use when resolving MemoryLinks
69  * @param recursionDepth how many layers of MemoryLinks to resolve
70  */
73  int recursionDepth = -1) const;
74  /**
75  * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
76  */
77  armem::query::data::Result query(const armem::query::data::Input& input,
79  int recursionDepth = -1) const;
80 
81  /**
82  * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
83  */
84  QueryResult query(armem::query::data::MemoryQueryPtr query,
86  int recursionDepth = -1) const;
87  /**
88  * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
89  */
90  QueryResult query(const armem::query::data::MemoryQuerySeq& queries,
92  int recursionDepth = -1) const;
93 
94  /**
95  * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
96  */
97  QueryResult query(const QueryBuilder& queryBuilder,
99  int recursionDepth = -1) const;
100 
101 
102  /**
103  * @brief Query a specific set of memory IDs.
104  *
105  * Each ID can refer to an entity, a snapshot or an instance. When not
106  * referring to an entity instance, the latest snapshot and first
107  * instance will be queried, respectively.
108  *
109  * All memory IDs must refer to the memory this reader is reading from.
110  * If an ID refers to another memory, the query will not find it and it
111  * will not be part of the result.
112  *
113  * @param ids The entity, snapshot or instance IDs.
114  * @param dataMode Whether to include instance data or just meta data.
115  * @return The query result.
116  */
118  queryMemoryIDs(const std::vector<MemoryID>& ids,
120 
121 
122  /**
123  * @brief Query the given snapshot and return the latest existing snapshot.
124  * @param snapshotIDs The snapshot (or entity) IDs.
125  * @return The latest snapshot contained in the query result, if any.
126  */
127  std::optional<wm::EntitySnapshot>
128  getLatestSnapshotOf(const std::vector<MemoryID>& snapshotIDs) const;
129 
130 
131  /**
132  * @brief Get the latest snapshots under the given memory ID.
133  * @param id A memory, core segment, provider segment or entity ID.
134  * @param dataMode With or without data.
135  * @return The query result.
136  */
138  const MemoryID& id,
140 
141  /**
142  * @brief Get the latest snapshot under the given memory ID.
143  * @param id A memory, core segment, provider segment or entity ID.
144  * @param dataMode With or without data.
145  * @return The latest contained snapshot, if any.
146  */
147  std::optional<wm::EntitySnapshot> getLatestSnapshotIn(
148  const MemoryID& id,
150 
151 
152  /**
153  * @brief Get all latest snapshots in the memory.
154  * @param dataMode With or without data.
155  * @return The query result.
156  */
159 
160 
161  /**
162  * @brief Get the whole memory content.
163  * @param dataMode With or without data.
164  * @return The query result.
165  */
168 
169 
170  server::dto::DirectlyStoreResult
171  directlyStore(const server::dto::DirectlyStoreInput& input) const;
172  void startRecording() const;
173  void stopRecording() const;
174 
175  /**
176  * @brief Get a prediction for the state of multiple entity instances in the future.
177  *
178  * The timestamp to predict the entities' state at is given via the snapshot
179  * given in the MemoryID.
180  *
181  * The `predictionPrx` must not be null when calling this function.
182  *
183  * @param requests a list of requests for entity instance predictions
184  * @return the vector of prediction results
185  */
186  std::vector<PredictionResult> predict(const std::vector<PredictionRequest>& requests) const;
187 
188  /**
189  * @brief Get the list of prediction engines supported by the memory.
190  *
191  * The `predictionPrx` must not be null when calling this function.
192  *
193  * @return a map from memory containers to their supported engines
194  */
195  std::map<MemoryID, std::vector<PredictionEngine>> getAvailablePredictionEngines() const;
196 
197  inline operator bool() const
198  {
199  return bool(readingPrx);
200  }
201 
202  private:
203  static void resolveMemoryLinks(armem::wm::EntityInstance& instance,
206  int recursionDepth);
207 
208  public:
209  server::ReadingMemoryInterfacePrx readingPrx;
210  server::PredictingMemoryInterfacePrx predictionPrx;
211  };
212 
213 } // namespace armarx::armem::client
armarx::armem::client::Reader
Reads data from a memory server.
Definition: Reader.h:24
armarx::armem::client::Reader::getLatestSnapshotsIn
QueryResult getLatestSnapshotsIn(const MemoryID &id, armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get the latest snapshots under the given memory ID.
Definition: Reader.cpp:343
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::client::Reader::setPredictingMemory
void setPredictingMemory(server::PredictingMemoryInterfacePrx predictingMemory)
Definition: Reader.cpp:476
armarx::armem::client::Reader::stopRecording
void stopRecording() const
Definition: Reader.cpp:454
armarx::armem::query::DataMode::WithData
@ WithData
Get structure and ARON data.
Prediction.h
armarx::armem::client::Reader::Reader
Reader(const Reader &)=default
Construct a memory reader.
armarx::armem::client
This file is part of ArmarX.
Definition: forward_declarations.h:7
armarx::armem::client::Reader::predictionPrx
server::PredictingMemoryInterfacePrx predictionPrx
Definition: Reader.h:210
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
Query.h
armarx::armem::client::Reader::getAllLatestSnapshots
QueryResult getAllLatestSnapshots(armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get all latest snapshots in the memory.
Definition: Reader.cpp:398
armarx::armem::client::Reader::queryMemoryIDs
QueryResult queryMemoryIDs(const std::vector< MemoryID > &ids, armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Query a specific set of memory IDs.
Definition: Reader.cpp:290
armarx::armem::client::Reader::predict
std::vector< PredictionResult > predict(const std::vector< PredictionRequest > &requests) const
Get a prediction for the state of multiple entity instances in the future.
Definition: Reader.cpp:482
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::query::DataMode
DataMode
Definition: DataMode.h:7
armarx::armem::client::Reader::directlyStore
server::dto::DirectlyStoreResult directlyStore(const server::dto::DirectlyStoreInput &input) const
Definition: Reader.cpp:420
armarx::armem::client::QueryInput
A query for parts of a memory.
Definition: Query.h:23
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::armem::client::Reader::getAll
QueryResult getAll(armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get the whole memory content.
Definition: Reader.cpp:409
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
armarx::armem::client::Reader::startRecording
void startRecording() const
Definition: Reader.cpp:434
armarx::armem::client::Reader::getAvailablePredictionEngines
std::map< MemoryID, std::vector< PredictionEngine > > getAvailablePredictionEngines() const
Get the list of prediction engines supported by the memory.
Definition: Reader.cpp:526
memory_definitions.h
armarx::armem::client::Reader::setReadingMemory
void setReadingMemory(server::ReadingMemoryInterfacePrx readingMemory)
Definition: Reader.cpp:470
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:22
MemoryNameSystem.h
armarx::armem::client::Reader::readingPrx
server::ReadingMemoryInterfacePrx readingPrx
Definition: Reader.h:209
armarx::armem::client::Reader::getLatestSnapshotIn
std::optional< wm::EntitySnapshot > getLatestSnapshotIn(const MemoryID &id, armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get the latest snapshot under the given memory ID.
Definition: Reader.cpp:370
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query.
Definition: Reader.cpp:33
armarx::armem::client::Reader::getLatestSnapshotOf
std::optional< wm::EntitySnapshot > getLatestSnapshotOf(const std::vector< MemoryID > &snapshotIDs) const
Query the given snapshot and return the latest existing snapshot.
Definition: Reader.cpp:313