6 #include <Ice/LocalException.h>
11 #include <RobotAPI/libraries/armem/aron/MemoryLink.aron.generated.h>
26 server::PredictingMemoryInterfacePrx predictingMemory,
27 server::ReadingLongTermMemoryInterfacePrx readingLtmMemory) :
28 readingPrx(readingMemory), predictionPrx(predictingMemory), readingLtmPrx(readingLtmMemory)
38 armem::query::data::Result
41 armem::query::data::Result result;
45 "Reading interface proxy must be set to perform a query.");
52 catch (const ::Ice::ConnectionRefusedException& e)
57 catch (
const Ice::LocalException& e)
59 std::stringstream sstream;
60 sstream <<
"Memory query failed.\nReason: " << e.what();
61 result.errorMessage = sstream.str();
74 armem::query::data::Result
77 armem::query::data::Result result;
81 "Reading interface proxy must be set to perform a query.");
88 catch (const ::Ice::ConnectionRefusedException& e)
93 catch (
const Ice::LocalException& e)
95 std::stringstream sstream;
96 sstream <<
"Memory query failed.\nReason: " << e.what();
97 result.errorMessage = sstream.str();
107 return this->
query(armem::query::data::MemoryQuerySeq{
query}, dataMode);
115 input.memoryQueries = queries;
116 input.dataMode = dataMode;
117 return this->
query(input);
129 int recursionDepth)
const
134 armem::query::data::Result
137 int recursionDepth)
const
142 "Reading interface proxy must be set to perform a query.");
145 armem::query::data::Result result;
152 { resolveMemoryLinks(instance, bObj.
memory, mns, recursionDepth); });
153 result = bObj.
toIce();
155 catch (
const Ice::LocalException& e)
157 std::stringstream sstream;
158 sstream <<
"Memory query failed.\nReason: " << e.what();
159 result.errorMessage = sstream.str();
163 std::stringstream sstream;
164 sstream <<
"Encountered malformed MemoryLink: " << e.what();
165 result.errorMessage = sstream.str();
174 int recursionDepth)
const
176 return this->
query(armem::query::data::MemoryQuerySeq{
query}, mns, recursionDepth);
182 int recursionDepth)
const
185 input.memoryQueries = queries;
186 return this->
query(input, mns, recursionDepth);
192 int recursionDepth)
const
207 std::optional<std::pair<MemoryID, aron::data::VariantPtr>>
212 static const aron::Path memoryLinkIDPath{{
"memoryID"}};
213 static const aron::Path memoryLinkDataPath{{
"data"}};
217 dto.fromAron(memoryIDDict);
218 auto memoryID = aron::fromAron<armem::MemoryID>(dto);
233 if (!nextDataAndType)
236 <<
" not available in memory containing that MemoryID.";
239 auto [nextData, nextType] = nextDataAndType.value();
242 std::string nextObjectName = nextType->getObjectNameWithTemplateInstantiations();
243 if (nextType->getObjectName() != linkTemplate)
246 << nextType->getObjectName() <<
", but the link requires the type "
253 nextData->toAronDTO(),
254 linkData->getPath().withElement(memoryLinkDataPath.toString()))}};
263 static const aron::Path memoryLinkDataPath{{
"data"}};
266 static const std::string linkType =
267 arondto::MemoryLink<arondto::MemoryID>::ToAronType()->getFullName();
269 std::set<armem::MemoryID> seenIDs{instance.
id()};
270 std::vector<aron::Path> currentPaths{{{
""}}};
271 std::vector<aron::Path> nextPaths;
277 ARMARX_INFO <<
"Instance '" << instance.
id().
str() <<
"' does not have a defined type.";
280 auto [instanceData, instanceType] = dataAndType.value();
281 while (!currentPaths.empty() && (recursionDepth == -1 || depth < recursionDepth))
283 for (
const auto& path : currentPaths)
285 aron::SubObjectFinder finder(linkType);
286 if (path.getFirstElement().empty())
293 instanceData->navigateAbsolute(path),
294 instanceType->navigateAbsolute(path));
297 for (
auto& [linkPathStr, linkDataAndType] : finder.getFoundObjects())
299 auto [linkData, linkType] = linkDataAndType;
303 auto [
memoryID, dataToAppend] = result.value();
304 if (seenIDs.find(
memoryID) != seenIDs.end())
311 memoryLinkDataPath.toString(), dataToAppend);
313 linkData->getPath().withElement(memoryLinkDataPath.toString()));
317 currentPaths = std::move(nextPaths);
326 using namespace client::query_fns;
334 if (
id.hasTimestamp())
346 std::optional<wm::EntitySnapshot>
349 std::vector<MemoryID> snapshotIDs = _snapshotIDs;
355 for (
const MemoryID& snapshotID : snapshotIDs)
370 ARMARX_INFO <<
"Error querying " << snapshotIDs.size() <<
" STT snapshots:\n"
379 using namespace client::query_fns;
380 if (!
id.isWellDefined())
398 std::optional<wm::EntitySnapshot>
404 std::optional<wm::EntitySnapshot>
latest = std::nullopt;
408 if (not entity.
empty())
410 const wm::EntitySnapshot& snapshot = entity.getLatestSnapshot();
411 if (not latest.has_value() or latest->time() < snapshot.time())
422 ARMARX_INFO <<
"Error querying latest snapshot in " << id;
430 using namespace client::query_fns;
435 return this->
query(qb);
441 using namespace client::query_fns;
446 return this->
query(qb);
449 server::dto::DirectlyStoreResult
452 server::RecordingMemoryInterfacePrx storingMemoryPrx =
453 server::RecordingMemoryInterfacePrx::checkedCast(
readingPrx);
454 if (storingMemoryPrx)
456 return storingMemoryPrx->directlyStore(
input);
460 ARMARX_WARNING <<
"Could not store a query into the LTM. It seems like the Memory does "
461 "not implement the StoringMemoryInterface.";
469 server::RecordingMemoryInterfacePrx storingMemoryPrx =
470 server::RecordingMemoryInterfacePrx::checkedCast(
readingPrx);
471 if (storingMemoryPrx)
473 server::dto::StartRecordInput i;
474 i.executionTime = armarx::core::time::dto::DateTime();
475 i.startTime = armarx::core::time::dto::DateTime();
477 i.configuration.clear();
479 storingMemoryPrx->startRecord(i);
484 ARMARX_WARNING <<
"Could not store a query into the LTM. It seems like the Memory does "
485 "not implement the StoringMemoryInterface.";
492 server::RecordingMemoryInterfacePrx storingMemoryPrx =
493 server::RecordingMemoryInterfacePrx::checkedCast(
readingPrx);
494 if (storingMemoryPrx)
496 storingMemoryPrx->stopRecord();
501 ARMARX_WARNING <<
"Could not store a query into the LTM. It seems like the Memory does "
502 "not implement the StoringMemoryInterface.";
518 std::vector<PredictionResult>
524 "PredictingMemoryInterfacePrx",
525 "Prediction interface proxy must be set to request a prediction.");
528 armem::prediction::data::PredictionRequestSeq iceRequests;
529 for (
const auto& request : requests)
531 iceRequests.push_back(request.toIce());
534 armem::prediction::data::PredictionResultSeq results;
539 catch (
const Ice::LocalException& e)
541 armem::prediction::data::PredictionResult
failure;
543 std::stringstream sstream;
544 sstream <<
"Prediction request failed. Reason: " << e.what();
545 failure.errorMessage = sstream.str();
547 for (
size_t i = 0; i < requests.size(); ++i)
553 std::vector<PredictionResult> boResults;
554 for (
const auto& result : results)
562 std::map<MemoryID, std::vector<PredictionEngine>>
568 "PredictingMemoryInterfacePrx",
569 "Prediction interface proxy must be set to request a prediction.");
572 armem::prediction::data::EngineSupportMap engines;
577 catch (
const Ice::LocalException& e)
582 std::map<MemoryID, std::vector<PredictionEngine>> boMap;