28#include <Eigen/Geometry>
30#include <opencv2/imgcodecs.hpp>
31#include <opencv2/imgproc.hpp>
32#include <opencv2/opencv.hpp>
34#include <SimoxUtility/algorithm/string/string_tools.h>
35#include <SimoxUtility/color/cmaps.h>
36#include <SimoxUtility/math/pose/pose.h>
41#include <RobotAPI/components/armem/server/ExampleMemory/aron/ExampleData.aron.generated.h>
63 defs->topic(debugObserver);
65 defs->optional(p.usedMemoryName,
"mem.UsedMemoryName",
"Name of the memory to use.");
66 defs->optional(p.commitFrequency,
68 "Frequency in which example data is commited. (max = 50Hz)");
76 return "ExampleMemoryClient";
87 p.commitFrequency = std::min(p.commitFrequency, 50.f);
107 exampleProviderID = addProviderSegment();
109 exampleEntityID = exampleProviderID.withEntityName(
"example_entity");
116 const std::vector<armem::MemoryID>& snapshotIDs)
119 <<
" was updated by " << snapshotIDs.size()
124 exampleEntityID,
this, &ExampleMemoryClient::processExampleEntityUpdate);
150 commitMultipleSnapshots(exampleEntityID, 3);
158 queryExactSnapshot(snapshotID);
167 commitExamplesWithIDs();
171 commitExamplesWithLinks();
179 commitExamplesWithUntypedData();
183 queryPredictionEngines();
188 loadDataFromLTMExport();
191 CycleUtil c(
static_cast<int>(1000 / p.commitFrequency));
192 while (!task->isStopped())
194 commitSingleSnapshot(exampleEntityID);
196 c.waitForCycleDuration();
201 ExampleMemoryClient::addProviderSegment()
203 armem::data::AddSegmentInput input;
205 input.providerSegmentName =
"FancyMethodModality";
208 armem::data::AddSegmentResult result = memoryWriter.
addSegment(input);
217 ExampleMemoryClient::commitSingleSnapshot(
const armem::MemoryID& entityID)
219 std::default_random_engine gen(std::random_device{}());
220 std::uniform_int_distribution<int> distrib(-20, 20);
223 armem::EntityUpdate
update;
224 update.entityID = entityID;
227 double diff = (
update.referencedTime - runStarted).toMilliSecondsDouble() / 1000;
229 auto dict1 = std::make_shared<aron::data::Dict>();
230 auto dict2 = std::make_shared<aron::data::Dict>();
232 auto sin = std::make_shared<aron::data::Float>(std::sin(diff));
233 auto cos = std::make_shared<aron::data::Float>(std::cos(diff));
235 auto sqrt = std::make_shared<aron::data::Double>(std::sqrt(diff));
236 auto lin = std::make_shared<aron::data::Long>(
static_cast<long>(diff * 1000));
237 auto rand = std::make_shared<aron::data::Int>(distrib(gen));
239 dict1->addElement(
"sin", sin);
240 dict1->addElement(
"cos", cos);
242 dict2->addElement(
"sqrt", sqrt);
243 dict2->addElement(
"lin", lin);
244 dict2->addElement(
"rand", rand);
246 update.instancesData = {dict1, dict2};
249 armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
251 if (!updateResult.success)
260 ExampleMemoryClient::commitMultipleSnapshots(
const armem::MemoryID& entityID,
int num)
263 armem::Commit commit;
264 for (
int i = 0; i < num; ++i)
266 armem::EntityUpdate&
update = commit.
add();
267 update.entityID = entityID;
269 for (
int j = 0; j < i; ++j)
271 update.instancesData.push_back(std::make_shared<aron::data::Dict>());
275 armem::CommitResult commitResult = memoryWriter.commit(commit);
277 if (!commitResult.allSuccess())
286 ExampleMemoryClient::queryLatestSnapshot(
const armem::MemoryID& entityID)
289 <<
"\n- entityID: \t'" << entityID <<
"'";
291 armem::client::query::Builder builder;
301 armem::client::QueryResult qResult = memoryReader.query(builder.
buildQueryInput());
307 armem::wm::Memory& memory = qResult.memory;
312 const armem::wm::Entity* entity = memory.
findEntity(entityID);
314 <<
"Entity " << entityID <<
" was not found in " <<
armem::print(memory);
321 <<
"\n- entity: \t" << entity->
name() <<
"\n- snapshot: \t"
322 << snapshot.
time() <<
"\n- #instances: \t" << snapshot.
size();
325 tab.queryResult = std::move(memory);
335 ExampleMemoryClient::queryExactSnapshot(
const armem::MemoryID& snapshotID)
338 <<
"\n- snapshotID: \t'" << snapshotID <<
"'";
340 namespace qf = armem::client::query_fns;
341 armem::client::query::Builder qb;
344 armem::client::QueryResult qResult = memoryReader.query(qb.
buildQueryInput());
349 armem::wm::Memory memory = std::move(qResult.memory);
354 <<
"\n- time: \t" << entitySnapshot.
time()
355 <<
"\n- # instances: \t" << entitySnapshot.
size();
358 const armem::wm::EntitySnapshot& entitySnapshot =
359 memory.
getEntity(snapshotID).getLatestSnapshot();
362 <<
"\n- time: \t" << entitySnapshot.
time()
363 <<
"\n- # instances: \t" << entitySnapshot.
size();
374 ExampleMemoryClient::commitExampleData()
380 auto addSegmentResult = memoryWriter.addSegment(
"ExampleData",
getName());
381 if (!addSegmentResult.success)
386 exampleDataProviderID = armem::MemoryID(addSegmentResult.segmentID);
388 addSegmentResult = memoryWriter.addSegment(
"LinkedData",
getName());
389 if (!addSegmentResult.success)
394 linkedDataProviderID = armem::MemoryID(addSegmentResult.segmentID);
397 armem::Commit commit;
401 armem::EntityUpdate&
update = commit.
add();
402 update.entityID = exampleDataProviderID.withEntityName(
"default");
403 update.referencedTime = time;
405 armem::example::ExampleData data_example;
406 toAron(data_example.memoryID, armem::MemoryID());
407 toAron(data_example.memoryLink.memoryID, armem::MemoryID());
408 auto instance = data_example.toAron();
410 update.instancesData = {instance};
417 armem::EntityUpdate& update_default = commit.
add();
421 armem::example::ExampleData data_default;
422 toAron(data_default.memoryID, armem::MemoryID());
423 toAron(data_default.memoryLink.memoryID, armem::MemoryID());
431 armem::EntityUpdate& update_answer = commit.
add();
435 armem::example::ExampleData
data;
436 data.the_bool =
true;
438 data.the_float = 21.5;
440 data.the_long = 424242;
441 data.the_string =
"fourty two";
442 data.the_float_list = {21, 42, 84};
443 data.the_int_list = {21, 42, 84};
444 data.the_string_list = simox::alg::multi_to_string(
data.the_int_list);
445 data.the_object_list.emplace_back();
447 data.the_float_dict = {
452 data.the_int_dict = {
458 data.the_position = {42, 24, 4224};
459 data.the_orientation = Eigen::AngleAxisf(1.57f, Eigen::Vector3f(1, 1, 1).normalized());
460 data.the_pose = simox::math::pose(
data.the_position,
data.the_orientation);
462 data.the_3x1_vector = {24, 42, 2442};
463 data.the_4x4_matrix = 42 * Eigen::Matrix4f::Identity();
466 toAron(
data.memoryLink.memoryID, armem::MemoryID());
473 armem::EntityUpdate& update_linked = commit.
add();
477 armem::example::LinkedData data_linked;
478 data_linked.yet_another_int = 42;
479 data_linked.yet_another_string =
"Hi! I'm from another core segment!";
480 data_linked.yet_another_object.element_int = 8349;
481 data_linked.yet_another_object.element_float = -1e3;
482 data_linked.yet_another_object.element_string =
483 "I'm a nested object in some linked data.";
488 armem::CommitResult commitResult = memoryWriter.commit(commit);
493 auto results_size = commitResult.
results.size();
494 ARMARX_INFO <<
"size(commitResults): " << results_size;
495 auto commitResults = commitResult.
results;
496 if (results_size > 2)
498 theAnswerSnapshotID = commitResults.at(1).snapshotID;
499 yetMoreDataSnapshotID = commitResults.at(2).snapshotID;
503 ARMARX_INFO <<
"Only " << results_size <<
" committed elements, instaed of "
507 catch (
const std::exception& e)
519 ExampleMemoryClient::queryExampleData()
522 armem::client::query::Builder qb;
524 .
withID(exampleProviderID)
526 .
withID(exampleProviderID)
532 armem::client::QueryResult result = memoryReader.query(qb.
buildQueryInput());
535 tab.queryResult = std::move(result.
memory);
545 ExampleMemoryClient::commitExamplesWithIDs()
550 armem::Commit commit;
552 armem::EntityUpdate&
update = commit.
add();
553 update.entityID = exampleDataProviderID.withEntityName(
"id to the_answer");
554 update.referencedTime = time;
556 armem::example::ExampleData
data;
563 armem::EntityUpdate&
update = commit.
add();
564 update.entityID = exampleDataProviderID.withEntityName(
"id to self");
565 update.referencedTime = time;
567 armem::example::ExampleData
data;
575 armem::EntityUpdate&
update = commit.
add();
576 update.entityID = exampleDataProviderID.withEntityName(
"id to previous snapshot");
579 armem::example::ExampleData
data;
586 armem::EntityUpdate&
update = commit.
add();
587 update.entityID = exampleDataProviderID.withEntityName(
"id to previous snapshot");
588 update.referencedTime = time;
590 armem::example::ExampleData
data;
599 armem::CommitResult commitResult = memoryWriter.commit(commit);
608 ARMARX_IMPORTANT <<
"Resolving multiple memory IDs via Memory Name System:";
610 std::vector<armem::MemoryID> ids;
611 for (armem::EntityUpdateResult& result : commitResult.
results)
617 std::map<armem::MemoryID, armem::wm::EntityInstance> instances =
621 std::stringstream ss;
622 for (
const auto& [
id, instance] : instances)
624 ss <<
"- Snapshot " <<
id <<
" "
625 <<
"\n--> Instance" << instance.id()
626 <<
" (# keys in data: " << instance.data()->childrenSize() <<
")"
634 ExampleMemoryClient::commitExamplesWithLinks()
640 armem::Commit commit;
642 armem::EntityUpdate&
update = commit.
add();
643 update.entityID = exampleDataProviderID.withEntityName(
"link to yet_more_data");
644 update.referencedTime = time;
646 armem::example::ExampleData
data;
654 armem::CommitResult commitResult = memoryWriter.commit(commit);
663 ARMARX_IMPORTANT <<
"Resolving multiple memory IDs via Memory Name System:";
665 std::vector<armem::MemoryID> ids;
666 for (armem::EntityUpdateResult& result : commitResult.
results)
672 std::map<armem::MemoryID, armem::wm::EntityInstance> instances =
676 std::stringstream ss;
677 for (
const auto& [
id, instance] : instances)
679 ss <<
"- Snapshot " <<
id <<
" "
680 <<
"\n--> Instance" << instance.id()
681 <<
" (# keys in data: " << instance.data()->childrenSize() <<
")"
689 ExampleMemoryClient::commitExampleImages()
693 armem::Commit commit;
695 armem::EntityUpdate&
update = commit.
add();
696 update.entityID = exampleDataProviderID.withEntityName(
"some_new_fancy_entity_id");
697 update.referencedTime = time;
699 auto currentFolder = std::filesystem::current_path();
700 auto opencv_img = cv::imread(
701 (currentFolder /
"images" / (std::to_string(imageCounter + 1) +
".jpg")).
string());
705 auto data = std::make_shared<aron::data::Dict>();
715 ExampleMemoryClient::commitExamplesWithUntypedData()
719 armem::Commit commit;
721 armem::EntityUpdate&
update = commit.
add();
722 update.entityID = exampleDataProviderID.withEntityName(
"unexpected_data");
723 update.referencedTime = time;
725 armem::example::ExampleData
data;
727 toAron(
data.memoryLink.memoryID, armem::MemoryID());
730 aron->addElement(
"unexpectedString",
731 std::make_shared<aron::data::String>(
"unexpected value"));
734 std::make_shared<aron::data::Dict>(std::map<std::string, aron::data::VariantPtr>{
735 {
"key43", std::make_shared<aron::data::Int>(43)},
736 {
"keyABC", std::make_shared<aron::data::String>(
"ABC")},
738 update.instancesData = {aron};
741 armem::CommitResult commitResult = memoryWriter.commit(commit);
749 ExampleMemoryClient::queryPredictionEngines()
751 const std::map<armem::MemoryID, std::vector<armem::PredictionEngine>> predictionEngines =
752 memoryReader.getAvailablePredictionEngines();
754 std::stringstream ss;
755 ss <<
"Prediction engines available in the server:" << std::endl;
756 for (
const auto& [
id, engines] : predictionEngines)
758 ss <<
" - " <<
id <<
": ";
759 for (
const armem::PredictionEngine& engine : engines)
761 ss << engine.engineID <<
", ";
770 ExampleMemoryClient::loadDataFromLTMExport()
773 std::string export_path =
"$HOME";
775 std::vector<std::string> coreSegmentNames = {
"ExampleData"};
776 bool addNonExistingCoreSegments =
false;
777 int amountOfSnapshotsPerSegmentToLoad = -1;
778 ARMARX_INFO <<
"Loading all data from " << export_path <<
"into core segment "
779 << coreSegmentNames[0];
780 memoryLoader.loadExportIntoWM(export_path,
783 addNonExistingCoreSegments,
784 amountOfSnapshotsPerSegmentToLoad);
788 ExampleMemoryClient::processExampleEntityUpdate(
const armem::MemoryID& subscriptionID,
789 const std::vector<armem::MemoryID>& snapshotIDs)
791 std::stringstream ss;
792 ss <<
"example_entity got updated: " << subscriptionID <<
"\n";
793 ss <<
"Updated snapshots: \n";
794 for (
const auto&
id : snapshotIDs)
796 ss <<
"- " <<
id <<
"\n";
818 if (tab.rebuild.exchange(
false))
Default component property definition container.
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
This util class helps with keeping a cycle time during a control cycle.
void onInitComponent() override
Pure virtual hook for the subclass.
void onDisconnectComponent() override
Hook for subclass.
void RemoteGui_update() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onConnectComponent() override
Pure virtual hook for the subclass.
void onExitComponent() override
Hook for subclass.
void createRemoteGuiTab()
std::string getDefaultName() const override
std::string getName() const
Retrieve name of object.
std::string coreSegmentName
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
MemoryID withEntityName(const std::string &name) const
MemoryID getEntityID() const
Loader useLoader(const MemoryID &memoryID)
Use a memory server and get a configurator for it.
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
std::map< MemoryID, wm::EntityInstance > resolveEntityInstances(const std::vector< MemoryID > &ids)
data::AddSegmentResult addSegment(const std::string &coreSegmentName, const std::string &providerSegmentName, bool clearWhenExists=false) const
MemoryNameSystem & memoryNameSystem()
void singleEntitySnapshot(const MemoryID &snapshotID)
QueryInput buildQueryInput() const
CoreSegmentSelector & coreSegments()
Start specifying core segments.
CoreSegmentSelector & withID(const MemoryID &id) override
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
EntitySelector & withID(const MemoryID &id) override
EntitySelector & all() override
SnapshotSelector & snapshots()
Start specifying entity snapshots.
ProviderSegmentSelector & withID(const MemoryID &id) override
EntitySelector & entities()
Start specifying entities.
SnapshotSelector & all() override
SnapshotSelector & latest()
SubscriptionHandle subscribe(const MemoryID &subscriptionID, Callback Callback)
Indicates that a query to the Memory Name System failed.
static data::NDArrayPtr ConvertFromMat(const cv::Mat &, const armarx::aron::Path &={})
Duration toDurationSinceEpoch() const
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
double toSecondsDouble() const
Returns the amount of seconds.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
const std::string memoryName
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
std::string print(const wm::Memory &data, int maxDepth=-1, int depth=0)
armarx::core::time::DateTime Time
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
std::shared_ptr< Dict > DictPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
void toAron(arondto::PackagePath &dto, const PackageFileLocation &bo)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void RemoteGui_startRunningTask()
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)
std::vector< std::string > allErrorMessages() const
std::vector< EntityUpdateResult > results
std::vector< EntityUpdate > updates
The entity updates.
MemoryID entityID
The entity's ID.
Time referencedTime
Time when this entity update was created (e.g.
std::vector< aron::data::DictPtr > instancesData
The entity data.
auto & getEntity(const MemoryID &entityID)
Retrieve an entity.
auto * findEntity(const MemoryID &entityID)
Find an entity.
bool hasInstances() const
Indicate whether this container contains at least one entity instance.
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.
wm::Memory memory
The slice of the memory that matched the query.