Reader.h
Go to the documentation of this file.
1#pragma once
2
3
4// STD/STL
5#include <optional>
6#include <vector>
7
8// RobotAPI
9#include <RobotAPI/interface/armem/server/ReadingMemoryInterface.h>
10#include <RobotAPI/interface/armem/server/RecordingMemoryInterface.h>
11#include <RobotAPI/interface/armem/server/ReadingLongTermMemoryInterface.h>
15
16#include "Query.h"
17
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 server::ReadingLongTermMemoryInterfacePrx readingLtmMemory = nullptr);
36
37 void setReadingMemory(server::ReadingMemoryInterfacePrx readingMemory);
38 void setPredictingMemory(server::PredictingMemoryInterfacePrx predictingMemory);
39 void setReadingLtmMemory(server::ReadingLongTermMemoryInterfacePrx readingLtmMemory);
40
41 // Perform a query on the LTM
42
43 /**
44 * @brief Queries the linked ltms
45 * @param storeIntoWM if true the ltm result is also commited to the wm en if recording mode is disabled
46 */
47 QueryResult queryLTM(const QueryInput& input, bool storeIntoWM = false) const;
48
49 /**
50 * @brief Queries the linked ltms
51 * @param storeIntoWM if true the ltm result is also commited to the wm en if recording mode is disabled
52 */
53 armem::query::data::Result queryLTM(const armem::query::data::Input& input, bool storeIntoWM = false) const;
54
55
56 /// Perform a query on the WM.
57 QueryResult query(const QueryInput& input) const;
58 armem::query::data::Result query(const armem::query::data::Input& input) const;
59
60 QueryResult query(armem::query::data::MemoryQueryPtr query,
62 QueryResult query(const armem::query::data::MemoryQuerySeq& queries,
64
65 QueryResult query(const QueryBuilder& queryBuilder) const;
66
67 /** Perform a query with recursive memory link resolution.
68 *
69 * Resolves the links in the query result using the given MemoryNameSystem
70 * and inserts the data into the MemoryLink objects' data fields.
71 * If the inserted data also contains links, those will be resolved as well
72 * up to and including the given recursion depth. Link cycles are detected;
73 * the first repeated memory ID will not be inserted into the data.
74 * Setting the recursion depth to `-1` is equivalent to an infinite
75 * recursion depth.
76 *
77 * Standalone `MemoryID` subobjects are ignored; only MemoryIDs inside of
78 * MemoryLink subobjects are resolved. Empty or unresolvable MemoryIDs
79 * are ignored - this includes MemoryIDs whose Entity fields are not set.
80 * If the data associated with a MemoryID could not be retrieved or
81 * its type does not match the template type of the MemoryLink,
82 * it is ignored.
83 *
84 * @param input the query to perform
85 * @param mns the MemoryNameSystem to use when resolving MemoryLinks
86 * @param recursionDepth how many layers of MemoryLinks to resolve
87 */
88 QueryResult query(const QueryInput& input,
90 int recursionDepth = -1) const;
91 /**
92 * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
93 */
94 armem::query::data::Result query(const armem::query::data::Input& input,
96 int recursionDepth = -1) const;
97
98 /**
99 * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
100 */
101 QueryResult query(armem::query::data::MemoryQueryPtr query,
103 int recursionDepth = -1) const;
104 /**
105 * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
106 */
107 QueryResult query(const armem::query::data::MemoryQuerySeq& queries,
109 int recursionDepth = -1) const;
110
111 /**
112 * @sa Reader::query(const QueryInput&, armem::client::MemoryNameSystem, int)
113 */
114 QueryResult query(const QueryBuilder& queryBuilder,
116 int recursionDepth = -1) const;
117
118
119 /**
120 * @brief Query a specific set of memory IDs.
121 *
122 * Each ID can refer to an entity, a snapshot or an instance. When not
123 * referring to an entity instance, the latest snapshot and first
124 * instance will be queried, respectively.
125 *
126 * All memory IDs must refer to the memory this reader is reading from.
127 * If an ID refers to another memory, the query will not find it and it
128 * will not be part of the result.
129 *
130 * @param ids The entity, snapshot or instance IDs.
131 * @param dataMode Whether to include instance data or just meta data.
132 * @return The query result.
133 */
135 queryMemoryIDs(const std::vector<MemoryID>& ids,
137
138
139 /**
140 * @brief Query the given snapshot and return the latest existing snapshot.
141 * @param snapshotIDs The snapshot (or entity) IDs.
142 * @return The latest snapshot contained in the query result, if any.
143 */
144 std::optional<wm::EntitySnapshot>
145 getLatestSnapshotOf(const std::vector<MemoryID>& snapshotIDs) const;
146
147
148 /**
149 * @brief Get the latest snapshots under the given memory ID.
150 * @param id A memory, core segment, provider segment or entity ID.
151 * @param dataMode With or without data.
152 * @return The query result.
153 */
155 const MemoryID& id,
157
158 /**
159 * @brief Get the latest snapshot under the given memory ID.
160 * @param id A memory, core segment, provider segment or entity ID.
161 * @param dataMode With or without data.
162 * @return The latest contained snapshot, if any.
163 */
164 std::optional<wm::EntitySnapshot> getLatestSnapshotIn(
165 const MemoryID& id,
167
168
169 /**
170 * @brief Get all latest snapshots in the memory.
171 * @param dataMode With or without data.
172 * @return The query result.
173 */
176
177
178 /**
179 * @brief Get the whole memory content.
180 * @param dataMode With or without data.
181 * @return The query result.
182 */
185
186
187 server::dto::DirectlyStoreResult
188 directlyStore(const server::dto::DirectlyStoreInput& input) const;
189 void startRecording() const;
190 void stopRecording() const;
191
192 /**
193 * @brief Get a prediction for the state of multiple entity instances in the future.
194 *
195 * The timestamp to predict the entities' state at is given via the snapshot
196 * given in the MemoryID.
197 *
198 * The `predictionPrx` must not be null when calling this function.
199 *
200 * @param requests a list of requests for entity instance predictions
201 * @return the vector of prediction results
202 */
203 std::vector<PredictionResult> predict(const std::vector<PredictionRequest>& requests) const;
204
205 /**
206 * @brief Get the list of prediction engines supported by the memory.
207 *
208 * The `predictionPrx` must not be null when calling this function.
209 *
210 * @return a map from memory containers to their supported engines
211 */
212 std::map<MemoryID, std::vector<PredictionEngine>> getAvailablePredictionEngines() const;
213
214 inline
215 operator bool() const
216 {
217 return bool(readingPrx);
218 }
219
220 private:
221 static void resolveMemoryLinks(armem::wm::EntityInstance& instance,
222 armem::wm::Memory& input,
224 int recursionDepth);
225
226 public:
227 server::ReadingMemoryInterfacePrx readingPrx;
228 server::PredictingMemoryInterfacePrx predictionPrx;
229 server::ReadingLongTermMemoryInterfacePrx readingLtmPrx;
230 };
231
232} // namespace armarx::armem::client
The memory name system (MNS) client.
std::map< MemoryID, std::vector< PredictionEngine > > getAvailablePredictionEngines() const
Get the list of prediction engines supported by the memory.
Definition Reader.cpp:621
QueryResult query(const QueryInput &input) const
Perform a query on the WM.
Definition Reader.cpp:119
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:449
Reader(const Reader &)=default
Construct a memory reader.
server::ReadingMemoryInterfacePrx readingPrx
Definition Reader.h:227
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:427
QueryResult getAll(armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get the whole memory content.
Definition Reader.cpp:489
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:374
void setReadingMemory(server::ReadingMemoryInterfacePrx readingMemory)
Definition Reader.cpp:565
void setPredictingMemory(server::PredictingMemoryInterfacePrx predictingMemory)
Definition Reader.cpp:571
server::dto::DirectlyStoreResult directlyStore(const server::dto::DirectlyStoreInput &input) const
Definition Reader.cpp:500
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:577
server::PredictingMemoryInterfacePrx predictionPrx
Definition Reader.h:228
QueryResult getAllLatestSnapshots(armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get all latest snapshots in the memory.
Definition Reader.cpp:478
QueryResult queryLTM(const QueryInput &input, bool storeIntoWM=false) const
Queries the linked ltms.
Definition Reader.cpp:83
server::ReadingLongTermMemoryInterfacePrx readingLtmPrx
Definition Reader.h:229
void setReadingLtmMemory(server::ReadingLongTermMemoryInterfacePrx readingLtmMemory)
std::optional< wm::EntitySnapshot > getLatestSnapshotOf(const std::vector< MemoryID > &snapshotIDs) const
Query the given snapshot and return the latest existing snapshot.
Definition Reader.cpp:397
Client-side working entity instance.
Client-side working memory.
This file is part of ArmarX.
@ WithData
Get structure and ARON data.
Definition DataMode.h:9
A query for parts of a memory.
Definition Query.h:24
Result of a QueryInput.
Definition Query.h:51