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;
455 <<
"' available for commit. Create new virtual memory.";
460 std::string virtualMemoryName = name;
461 memory.id().memoryName = virtualMemoryName;
472 MemoryViewer::startQueries()
478 MemoryViewer::processQueryResults()
480 const std::map<std::string, client::QueryResult> results = collectQueryResults();
483 applyQueryResults(results, &errorCount);
486 updateStatusLabel(errorCount);
490 MemoryViewer::updateStatusLabel(
int errorCount)
495 auto now = std::chrono::system_clock::now();
496 auto in_time_t = std::chrono::system_clock::to_time_t(now);
498 std::stringstream ss;
499 ss <<
"Last update: " << std::put_time(std::localtime(&in_time_t),
"%Y-%m-%d %X");
500 ss <<
"\nThe query produced " << errorCount <<
" errors! Please check log.";
502 statusLabel->setText(QString::fromStdString(ss.str()));
507 MemoryViewer::startDueQueries()
519 if (std::find(enabledMemories.begin(), enabledMemories.end(), pair.first) ==
520 enabledMemories.end())
525 const auto& name = pair.first;
526 const auto& reader = pair.second;
538 [reader,
input, recursionDepth,
this]()
542 ? reader.query(
input.toIce())
543 : reader.query(
input.toIce(),
mns, recursionDepth);
548 std::map<std::string, client::QueryResult>
549 MemoryViewer::collectQueryResults()
554 std::map<std::string, client::QueryResult> results;
557 const std::string& name = it->first;
558 std::future<armem::query::data::Result>* queryPromise = &it->second;
560 if (queryPromise->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
568 catch (
const Ice::ConnectionRefusedException&)
590 {
"t Collect Query Results [ms]",
591 new Variant(tCollectQueryResults.toMilliSecondsDouble())},
592 {
"# Collected Query Results",
new Variant(
static_cast<int>(results.size()))},
600 MemoryViewer::applyQueryResults(
const std::map<std::string, client::QueryResult>& results,
604 for (
const auto& [name, result] : results)
612 ARMARX_WARNING <<
"Querying memory server '" << name <<
"' produced an error: \n"
613 << result.errorMessage;
640 if (std::find(enabledMemories.begin(), enabledMemories.end(), it->first) ==
641 enabledMemories.end())
664 {
"t Process Query Results [ms]",
665 new Variant(tProcessQueryResults.toMilliSecondsDouble())},
666 {
"# Processed Query Results",
new Variant(
static_cast<int>(results.size()))},
683 if (not
id.hasTimestamp())
691 id.timestamp = snapshot->
time();
693 if (not
id.hasInstanceIndex())
699 snapshot = &
data->getSnapshot(
id);
709 if (snapshot && snapshot->size() > 0)
711 id.instanceIndex = 0;
714 if (
id.hasInstanceIndex())
726 auto handleError = [
this](
const std::string& msg)
734 handleError(
"Memory name is empty.");
738 std::optional<wm::EntityInstance> instance;
743 segmentType =
data->getProviderSegment(
id).aronType();
745 if (
id.hasInstanceIndex())
747 instance =
data->getInstance(
id);
749 else if (
id.hasTimestamp())
751 instance =
data->getSnapshot(
id).getInstance(0);
755 instance =
data->getEntity(
id).getLatestSnapshot().getInstance(0);
782 view->update(*instance, segmentType);
792 MemoryViewer::updateListOfActiveMemories()
801 std::vector<std::string> activeMemoryNames;
806 std::back_inserter(activeMemoryNames),
807 [](
const auto& p) { return p.first; });
823 <<
"MNS not ready yet. Skip update of available memories in query widget.";
828 MemoryViewer::updateMemoryTree()
830 std::map<std::string, const armem::wm::Memory*> memoriesToUpdate;
835 memoriesToUpdate[name] = &
data;
849 new Variant(GuiUpdateMemoryTree.toMilliSecondsDouble()));
851 catch (
const Ice::Exception&)
865 auto showMenu = [menu, pos]()
878 mns::dto::MemoryServerInterfaces prx;
886 QString::fromStdString(e.
makeMsg(
memoryID,
"Could not resolve memory server.")));
893 std::stringstream ss;
894 ss <<
"Memory server " <<
memoryID <<
" does not support actions or is offline.";
895 statusLabel->setText(QString::fromStdString(ss.str()));
900 actions::GetActionsOutputSeq result;
903 result = prx.actions->getActions({{armarx::toIce<data::MemoryID>(
memoryID)}});
905 catch (
const Ice::LocalException& e)
907 std::stringstream ss;
908 ss <<
"Could not get actions for " <<
memoryID <<
".";
909 statusLabel->setText(QString::fromStdString(ss.str()));
914 if (result.size() == 0)
924 actions::data::ExecuteActionOutputSeq result;
927 result = prx.actions->executeActions(
928 {{armarx::toIce<armem::data::MemoryID>(
memoryID), path}});
930 catch (
const Ice::LocalException& e)
932 std::stringstream ss;
933 ss <<
"Failed to execute action: " << e.what();
934 statusLabel->setText(QString::fromStdString(ss.str()));
937 for (
const auto& [
success, errorMessage] : result)
941 std::stringstream ss;
942 ss <<
"Failed to execute action: " << errorMessage;
943 statusLabel->setText(QString::fromStdString(ss.str()));
952 actionsMenu->exec(pos);
956 menu->addMenu(actionsMenu);
965 const std::string& engineID)
968 std::stringstream errorStream;
969 auto showError = [
this, &errorStream]()
970 {
statusLabel->setText(QString::fromStdString(errorStream.str())); };
974 errorStream <<
"Could not convert " << entityID <<
" to valid entity ID.";
980 errorStream <<
"Not connected to memory '" << entityID.
memoryName
981 <<
"', cannot make prediction.";
988 errorStream <<
"Predictions are not available for memory '" << entityID.
memoryName
999 result = reader.
predict({request}).at(0);
1001 catch (
const Ice::LocalException& e)
1003 errorStream <<
"Could not make prediction request: " << e.what();
1010 errorStream <<
"Prediction failed: " << result.
errorMessage;
1020 const static std::string CONFIG_KEY_MEMORY =
"MemoryViewer.MemoryNameSystem";
1021 const static std::string CONFIG_KEY_DEBUG_OBSERVER =
"MemoryViewer.DebugObserverName";
1026 mnsName = settings->value(QString::fromStdString(CONFIG_KEY_MEMORY),
"MemoryNameSystem")
1030 settings->value(QString::fromStdString(CONFIG_KEY_DEBUG_OBSERVER),
"DebugObserver")
1038 settings->setValue(QString::fromStdString(CONFIG_KEY_MEMORY),
1039 QString::fromStdString(
mnsName));
1040 settings->setValue(QString::fromStdString(CONFIG_KEY_DEBUG_OBSERVER),
1047 dialog->
addProxyFinder<armarx::armem::mns::MemoryNameSystemInterfacePrx>(
1048 {CONFIG_KEY_MEMORY,
"MemoryNameSystem",
"MemoryNameSystem"});
1050 {CONFIG_KEY_DEBUG_OBSERVER,
"Debug Observer",
"DebugObserver"});