3 #include <QApplication>
15 #include <Ice/Exception.h>
17 #include <SimoxUtility/algorithm/get_map_keys_values.h>
26 #include <RobotAPI/interface/armem/actions.h>
27 #include <RobotAPI/interface/armem/memory.h>
28 #include <RobotAPI/interface/armem/mns/MemoryNameSystemInterface.h>
42 QGroupBox* memoryGroupBox,
43 QLayout* memoryGroupBoxParentLayout,
44 QGroupBox* instanceGroupBox,
45 QLayout* instanceGroupBoxParentLayout,
46 QBoxLayout* diskControlWidgetLayout,
52 this->statusLabel->clear();
54 statusLabel->setContextMenuPolicy(Qt::CustomContextMenu);
56 &QLabel::customContextMenuRequested,
60 menu.addAction(
"Copy to clipboard",
62 { QApplication::clipboard()->setText(
statusLabel->text()); });
82 std::stringstream sstream;
83 sstream <<
"Predictions are not available for memory '" << entityID.memoryName
85 this->statusLabel->setText(QString::fromStdString(sstream.str()));
89 std::map<MemoryID, std::vector<PredictionEngine>> predictionEngines;
96 catch (
const Ice::LocalException& e)
98 std::stringstream sstream;
99 sstream <<
"Could not get prediction engines and type from memory: " << e.what();
100 this->statusLabel->setText(QString::fromStdString(sstream.str()));
101 return {
nullptr, {}};
108 if (providerSegment !=
nullptr)
110 entityType = providerSegment->aronType();
129 if (diskControlWidgetLayout)
147 connect(
this, &
This::connected,
this, &This::startPeriodicUpdateTimer);
230 armem::mns::MemoryNameSystemInterfacePrx mnsProxy;
231 component.getProxy(mnsProxy,
mnsName);
232 mns = client::MemoryNameSystem(mnsProxy);
262 MemoryViewer::collapseAll()
268 MemoryViewer::startPeriodicUpdateTimer()
273 const armem::wm::Memory*
274 MemoryViewer::getSingleMemoryData(
const std::string&
memoryName)
279 std::stringstream ss;
280 ss <<
"Memory name '" <<
memoryName <<
"' is unknown. Known are: "
281 << simox::alg::join(simox::alg::get_keys(
memoryData),
", ");
282 statusLabel->setText(QString::fromStdString(ss.str()));
305 if (std::find(enabledMemories.begin(), enabledMemories.end(), name) ==
306 enabledMemories.end())
315 server::dto::DirectlyStoreInput
input;
316 input.memory = q_res.toIce().memory;
317 reader.directlyStore(
input);
321 std::string
s =
"Query of memory " + name +
" was unsuccessful.";
339 if (std::find(enabledMemories.begin(), enabledMemories.end(), name) ==
340 enabledMemories.end())
344 reader.startRecording();
360 if (std::find(enabledMemories.begin(), enabledMemories.end(), name) ==
361 enabledMemories.end())
365 reader.stopRecording();
381 ARMARX_INFO <<
"Committing to " << memoryIDStr <<
" the data: " << aronJSONStr;
388 <<
"' does not contain an entity.";
393 nlohmann::json json = nlohmann::json::parse(aronJSONStr);
403 <<
"' available for commit.";
408 auto& entityUpdate = comm.
add();
410 entityUpdate.confidence = 1.0;
411 entityUpdate.instancesData = {aron};
412 entityUpdate.referencedTime = now;
413 it->second.commit(comm);
426 std::vector<wm::Memory> memoryDataVec = simox::alg::get_values(
memoryData);
438 std::map<std::filesystem::path, wm::Memory>
data =
443 std::string name =
memory.id().memoryName;
453 <<
"' available for commit. Create new virtual memory.";
458 std::string virtualMemoryName = name +
" (at " + path.string() +
")";
461 memoryData[virtualMemoryName] = virtualMemory;
471 MemoryViewer::startQueries()
477 MemoryViewer::processQueryResults()
479 const std::map<std::string, client::QueryResult> results = collectQueryResults();
482 applyQueryResults(results, &errorCount);
485 updateStatusLabel(errorCount);
489 MemoryViewer::updateStatusLabel(
int errorCount)
494 auto now = std::chrono::system_clock::now();
495 auto in_time_t = std::chrono::system_clock::to_time_t(now);
497 std::stringstream ss;
498 ss <<
"Last update: " << std::put_time(std::localtime(&in_time_t),
"%Y-%m-%d %X");
499 ss <<
"\nThe query produced " << errorCount <<
" errors! Please check log.";
501 statusLabel->setText(QString::fromStdString(ss.str()));
506 MemoryViewer::startDueQueries()
518 if (std::find(enabledMemories.begin(), enabledMemories.end(), pair.first) ==
519 enabledMemories.end())
524 const auto& name = pair.first;
525 const auto& reader = pair.second;
537 [reader,
input, recursionDepth,
this]()
541 ? reader.query(
input.toIce())
542 : reader.query(
input.toIce(),
mns, recursionDepth);
547 std::map<std::string, client::QueryResult>
548 MemoryViewer::collectQueryResults()
553 std::map<std::string, client::QueryResult> results;
556 const std::string& name = it->first;
557 std::future<armem::query::data::Result>* queryPromise = &it->second;
559 if (queryPromise->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
567 catch (
const Ice::ConnectionRefusedException&)
589 {
"t Collect Query Results [ms]",
590 new Variant(tCollectQueryResults.toMilliSecondsDouble())},
591 {
"# Collected Query Results",
new Variant(
static_cast<int>(results.size()))},
599 MemoryViewer::applyQueryResults(
const std::map<std::string, client::QueryResult>& results,
603 for (
const auto& [name, result] : results)
611 ARMARX_WARNING <<
"Querying memory server '" << name <<
"' produced an error: \n"
612 << result.errorMessage;
639 if (std::find(enabledMemories.begin(), enabledMemories.end(), it->first) ==
640 enabledMemories.end())
663 {
"t Process Query Results [ms]",
664 new Variant(tProcessQueryResults.toMilliSecondsDouble())},
665 {
"# Processed Query Results",
new Variant(
static_cast<int>(results.size()))},
682 if (not
id.hasTimestamp())
690 id.timestamp = snapshot->
time();
692 if (not
id.hasInstanceIndex())
698 snapshot = &
data->getSnapshot(
id);
708 if (snapshot && snapshot->size() > 0)
710 id.instanceIndex = 0;
713 if (
id.hasInstanceIndex())
725 auto handleError = [
this](
const std::string& msg)
733 handleError(
"Memory name is empty.");
737 std::optional<wm::EntityInstance> instance;
742 segmentType =
data->getProviderSegment(
id).aronType();
744 if (
id.hasInstanceIndex())
746 instance =
data->getInstance(
id);
748 else if (
id.hasTimestamp())
750 instance =
data->getSnapshot(
id).getInstance(0);
754 instance =
data->getEntity(
id).getLatestSnapshot().getInstance(0);
781 view->update(*instance, segmentType);
791 MemoryViewer::updateListOfActiveMemories()
800 std::vector<std::string> activeMemoryNames;
805 std::back_inserter(activeMemoryNames),
806 [](
const auto& p) { return p.first; });
822 <<
"MNS not ready yet. Skip update of available memories in query widget.";
827 MemoryViewer::updateMemoryTree()
829 std::map<std::string, const armem::wm::Memory*> memoriesToUpdate;
834 memoriesToUpdate[name] = &
data;
848 new Variant(GuiUpdateMemoryTree.toMilliSecondsDouble()));
850 catch (
const Ice::Exception&)
864 auto showMenu = [menu, pos]()
877 mns::dto::MemoryServerInterfaces prx;
885 QString::fromStdString(e.
makeMsg(
memoryID,
"Could not resolve memory server.")));
892 std::stringstream ss;
893 ss <<
"Memory server " <<
memoryID <<
" does not support actions or is offline.";
894 statusLabel->setText(QString::fromStdString(ss.str()));
899 actions::GetActionsOutputSeq result;
902 result = prx.actions->getActions({{armarx::toIce<data::MemoryID>(
memoryID)}});
904 catch (
const Ice::LocalException& e)
906 std::stringstream ss;
907 ss <<
"Could not get actions for " <<
memoryID <<
".";
908 statusLabel->setText(QString::fromStdString(ss.str()));
913 if (result.size() == 0)
923 actions::data::ExecuteActionOutputSeq result;
926 result = prx.actions->executeActions(
927 {{armarx::toIce<armem::data::MemoryID>(
memoryID), path}});
929 catch (
const Ice::LocalException& e)
931 std::stringstream ss;
932 ss <<
"Failed to execute action: " << e.what();
933 statusLabel->setText(QString::fromStdString(ss.str()));
936 for (
const auto& [
success, errorMessage] : result)
940 std::stringstream ss;
941 ss <<
"Failed to execute action: " << errorMessage;
942 statusLabel->setText(QString::fromStdString(ss.str()));
951 actionsMenu->exec(pos);
955 menu->addMenu(actionsMenu);
964 const std::string& engineID)
967 std::stringstream errorStream;
968 auto showError = [
this, &errorStream]()
969 {
statusLabel->setText(QString::fromStdString(errorStream.str())); };
973 errorStream <<
"Could not convert " << entityID <<
" to valid entity ID.";
979 errorStream <<
"Not connected to memory '" << entityID.
memoryName
980 <<
"', cannot make prediction.";
987 errorStream <<
"Predictions are not available for memory '" << entityID.
memoryName
998 result = reader.
predict({request}).at(0);
1000 catch (
const Ice::LocalException& e)
1002 errorStream <<
"Could not make prediction request: " << e.what();
1009 errorStream <<
"Prediction failed: " << result.
errorMessage;
1019 const static std::string CONFIG_KEY_MEMORY =
"MemoryViewer.MemoryNameSystem";
1020 const static std::string CONFIG_KEY_DEBUG_OBSERVER =
"MemoryViewer.DebugObserverName";
1025 mnsName = settings->value(QString::fromStdString(CONFIG_KEY_MEMORY),
"MemoryNameSystem")
1029 settings->value(QString::fromStdString(CONFIG_KEY_DEBUG_OBSERVER),
"DebugObserver")
1037 settings->setValue(QString::fromStdString(CONFIG_KEY_MEMORY),
1038 QString::fromStdString(
mnsName));
1039 settings->setValue(QString::fromStdString(CONFIG_KEY_DEBUG_OBSERVER),
1046 dialog->
addProxyFinder<armarx::armem::mns::MemoryNameSystemInterfacePrx>(
1047 {CONFIG_KEY_MEMORY,
"MemoryNameSystem",
"MemoryNameSystem"});
1049 {CONFIG_KEY_DEBUG_OBSERVER,
"Debug Observer",
"DebugObserver"});