3 #include <QApplication>
15 #include <Ice/Exception.h>
17 #include <SimoxUtility/algorithm/get_map_keys_values.h>
18 #include <SimoxUtility/algorithm/string/string_tools.h>
27 #include <RobotAPI/interface/armem/actions.h>
28 #include <RobotAPI/interface/armem/memory.h>
29 #include <RobotAPI/interface/armem/mns/MemoryNameSystemInterface.h>
43 QGroupBox* memoryGroupBox,
44 QLayout* memoryGroupBoxParentLayout,
45 QGroupBox* instanceGroupBox,
46 QLayout* instanceGroupBoxParentLayout,
47 QBoxLayout* diskControlWidgetLayout,
53 this->statusLabel->clear();
55 statusLabel->setContextMenuPolicy(Qt::CustomContextMenu);
57 &QLabel::customContextMenuRequested,
61 menu.addAction(
"Copy to clipboard",
63 { QApplication::clipboard()->setText(
statusLabel->text()); });
83 std::stringstream sstream;
84 sstream <<
"Predictions are not available for memory '" << entityID.memoryName
86 this->statusLabel->setText(QString::fromStdString(sstream.str()));
90 std::map<MemoryID, std::vector<PredictionEngine>> predictionEngines;
97 catch (
const Ice::LocalException& e)
99 std::stringstream sstream;
100 sstream <<
"Could not get prediction engines and type from memory: " << e.what();
101 this->statusLabel->setText(QString::fromStdString(sstream.str()));
102 return {
nullptr, {}};
109 if (providerSegment !=
nullptr)
111 entityType = providerSegment->aronType();
130 if (diskControlWidgetLayout)
148 connect(
this, &
This::connected,
this, &This::startPeriodicUpdateTimer);
231 armem::mns::MemoryNameSystemInterfacePrx mnsProxy;
232 component.getProxy(mnsProxy,
mnsName);
233 mns = client::MemoryNameSystem(mnsProxy);
263 MemoryViewer::collapseAll()
269 MemoryViewer::startPeriodicUpdateTimer()
274 const armem::wm::Memory*
275 MemoryViewer::getSingleMemoryData(
const std::string&
memoryName)
280 std::stringstream ss;
281 ss <<
"Memory name '" <<
memoryName <<
"' is unknown. Known are: "
282 << simox::alg::join(simox::alg::get_keys(
memoryData),
", ");
283 statusLabel->setText(QString::fromStdString(ss.str()));
306 if (std::find(enabledMemories.begin(), enabledMemories.end(), name) ==
307 enabledMemories.end())
316 server::dto::DirectlyStoreInput
input;
317 input.memory = q_res.toIce().memory;
318 reader.directlyStore(
input);
322 std::string
s =
"Query of memory " + name +
" was unsuccessful.";
340 if (std::find(enabledMemories.begin(), enabledMemories.end(), name) ==
341 enabledMemories.end())
345 reader.startRecording();
361 if (std::find(enabledMemories.begin(), enabledMemories.end(), name) ==
362 enabledMemories.end())
366 reader.stopRecording();
382 ARMARX_INFO <<
"Committing to " << memoryIDStr <<
" the data: " << aronJSONStr;
389 <<
"' does not contain an entity.";
394 nlohmann::json json = nlohmann::json::parse(aronJSONStr);
404 <<
"' available for commit.";
409 auto& entityUpdate = comm.
add();
411 entityUpdate.confidence = 1.0;
412 entityUpdate.instancesData = {aron};
413 entityUpdate.referencedTime = now;
414 it->second.commit(comm);
427 std::vector<wm::Memory> memoryDataVec = simox::alg::get_values(
memoryData);
439 std::map<std::filesystem::path, wm::Memory>
data =
444 std::string name =
memory.id().memoryName;
456 <<
"' available for commit. Create new virtual memory.";
461 std::string virtualMemoryName = name;
462 memory.id().memoryName = virtualMemoryName;
473 MemoryViewer::startQueries()
479 MemoryViewer::processQueryResults()
481 const std::map<std::string, client::QueryResult> results = collectQueryResults();
484 applyQueryResults(results, &errorCount);
487 updateStatusLabel(errorCount);
491 MemoryViewer::updateStatusLabel(
int errorCount)
496 auto now = std::chrono::system_clock::now();
497 auto in_time_t = std::chrono::system_clock::to_time_t(now);
499 std::stringstream ss;
500 ss <<
"Last update: " << std::put_time(std::localtime(&in_time_t),
"%Y-%m-%d %X");
501 ss <<
"\nThe query produced " << errorCount <<
" errors! Please check log.";
503 statusLabel->setText(QString::fromStdString(ss.str()));
508 MemoryViewer::startDueQueries()
520 if (std::find(enabledMemories.begin(), enabledMemories.end(), pair.first) ==
521 enabledMemories.end())
526 const auto& name = pair.first;
527 const auto& reader = pair.second;
539 [reader,
input, recursionDepth,
this]()
543 ? reader.query(
input.toIce())
544 : reader.query(
input.toIce(),
mns, recursionDepth);
549 std::map<std::string, client::QueryResult>
550 MemoryViewer::collectQueryResults()
555 std::map<std::string, client::QueryResult> results;
558 const std::string& name = it->first;
559 std::future<armem::query::data::Result>* queryPromise = &it->second;
561 if (queryPromise->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
569 catch (
const Ice::ConnectionRefusedException&)
591 {
"t Collect Query Results [ms]",
592 new Variant(tCollectQueryResults.toMilliSecondsDouble())},
593 {
"# Collected Query Results",
new Variant(
static_cast<int>(results.size()))},
601 MemoryViewer::applyQueryResults(
const std::map<std::string, client::QueryResult>& results,
605 for (
const auto& [name, result] : results)
613 ARMARX_WARNING <<
"Querying memory server '" << name <<
"' produced an error: \n"
614 << result.errorMessage;
641 if (std::find(enabledMemories.begin(), enabledMemories.end(), it->first) ==
642 enabledMemories.end())
665 {
"t Process Query Results [ms]",
666 new Variant(tProcessQueryResults.toMilliSecondsDouble())},
667 {
"# Processed Query Results",
new Variant(
static_cast<int>(results.size()))},
684 if (not
id.hasTimestamp())
692 id.timestamp = snapshot->
time();
694 if (not
id.hasInstanceIndex())
700 snapshot = &
data->getSnapshot(
id);
710 if (snapshot && snapshot->size() > 0)
712 id.instanceIndex = 0;
715 if (
id.hasInstanceIndex())
727 auto handleError = [
this](
const std::string& msg)
735 handleError(
"Memory name is empty.");
739 std::optional<wm::EntityInstance> instance;
744 segmentType =
data->getProviderSegment(
id).aronType();
746 if (
id.hasInstanceIndex())
748 instance =
data->getInstance(
id);
750 else if (
id.hasTimestamp())
752 instance =
data->getSnapshot(
id).getInstance(0);
756 instance =
data->getEntity(
id).getLatestSnapshot().getInstance(0);
783 view->update(*instance, segmentType);
793 MemoryViewer::updateListOfActiveMemories()
802 std::vector<std::string> activeMemoryNames;
807 std::back_inserter(activeMemoryNames),
808 [](
const auto& p) { return p.first; });
824 <<
"MNS not ready yet. Skip update of available memories in query widget.";
829 MemoryViewer::updateMemoryTree()
831 std::map<std::string, const armem::wm::Memory*> memoriesToUpdate;
836 memoriesToUpdate[name] = &
data;
850 new Variant(GuiUpdateMemoryTree.toMilliSecondsDouble()));
852 catch (
const Ice::Exception&)
866 auto showMenu = [menu, pos]()
879 mns::dto::MemoryServerInterfaces prx;
887 QString::fromStdString(e.
makeMsg(
memoryID,
"Could not resolve memory server.")));
894 std::stringstream ss;
895 ss <<
"Memory server " <<
memoryID <<
" does not support actions or is offline.";
896 statusLabel->setText(QString::fromStdString(ss.str()));
901 actions::GetActionsOutputSeq result;
904 result = prx.actions->getActions({{armarx::toIce<data::MemoryID>(
memoryID)}});
906 catch (
const Ice::LocalException& e)
908 std::stringstream ss;
909 ss <<
"Could not get actions for " <<
memoryID <<
".";
910 statusLabel->setText(QString::fromStdString(ss.str()));
915 if (result.size() == 0)
925 actions::data::ExecuteActionOutputSeq result;
928 result = prx.actions->executeActions(
929 {{armarx::toIce<armem::data::MemoryID>(
memoryID), path}});
931 catch (
const Ice::LocalException& e)
933 std::stringstream ss;
934 ss <<
"Failed to execute action: " << e.what();
935 statusLabel->setText(QString::fromStdString(ss.str()));
938 for (
const auto& [
success, errorMessage] : result)
942 std::stringstream ss;
943 ss <<
"Failed to execute action: " << errorMessage;
944 statusLabel->setText(QString::fromStdString(ss.str()));
953 actionsMenu->exec(pos);
957 menu->addMenu(actionsMenu);
966 const std::string& engineID)
969 std::stringstream errorStream;
970 auto showError = [
this, &errorStream]()
971 {
statusLabel->setText(QString::fromStdString(errorStream.str())); };
975 errorStream <<
"Could not convert " << entityID <<
" to valid entity ID.";
981 errorStream <<
"Not connected to memory '" << entityID.
memoryName
982 <<
"', cannot make prediction.";
989 errorStream <<
"Predictions are not available for memory '" << entityID.
memoryName
1000 result = reader.
predict({request}).at(0);
1002 catch (
const Ice::LocalException& e)
1004 errorStream <<
"Could not make prediction request: " << e.what();
1011 errorStream <<
"Prediction failed: " << result.
errorMessage;
1021 const static std::string CONFIG_KEY_MEMORY =
"MemoryViewer.MemoryNameSystem";
1022 const static std::string CONFIG_KEY_DEBUG_OBSERVER =
"MemoryViewer.DebugObserverName";
1027 mnsName = settings->value(QString::fromStdString(CONFIG_KEY_MEMORY),
"MemoryNameSystem")
1031 settings->value(QString::fromStdString(CONFIG_KEY_DEBUG_OBSERVER),
"DebugObserver")
1039 settings->setValue(QString::fromStdString(CONFIG_KEY_MEMORY),
1040 QString::fromStdString(
mnsName));
1041 settings->setValue(QString::fromStdString(CONFIG_KEY_DEBUG_OBSERVER),
1048 dialog->
addProxyFinder<armarx::armem::mns::MemoryNameSystemInterfacePrx>(
1049 {CONFIG_KEY_MEMORY,
"MemoryNameSystem",
"MemoryNameSystem"});
1051 {CONFIG_KEY_DEBUG_OBSERVER,
"Debug Observer",
"DebugObserver"});