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>
67 defs->topic(debugObserver);
69 defs->optional(p.usedMemoryName,
"mem.UsedMemoryName",
"Name of the memory to use.");
70 defs->optional(p.commitFrequency,
72 "Frequency in which example data is commited. (max = 50Hz)");
80 return "ExampleMemoryClient";
91 p.commitFrequency = std::min(p.commitFrequency, 50.f);
111 exampleProviderID = addProviderSegment();
113 exampleEntityID = exampleProviderID.withEntityName(
"example_entity");
120 const std::vector<armem::MemoryID>& snapshotIDs)
123 <<
" was updated by " << snapshotIDs.size()
128 exampleEntityID,
this, &ExampleMemoryClient::processExampleEntityUpdate);
154 commitMultipleSnapshots(exampleEntityID, 3);
162 queryExactSnapshot(snapshotID);
171 commitExamplesWithIDs();
175 commitExamplesWithLinks();
183 commitExamplesWithUntypedData();
187 queryPredictionEngines();
192 loadDataFromLTMExport();
195 CycleUtil c(
static_cast<int>(1000 / p.commitFrequency));
196 while (!task->isStopped())
198 commitSingleSnapshot(exampleEntityID);
200 c.waitForCycleDuration();
205 ExampleMemoryClient::addProviderSegment()
207 armem::data::AddSegmentInput input;
209 input.providerSegmentName =
"FancyMethodModality";
212 armem::data::AddSegmentResult result = memoryWriter.
addSegment(input);
221 ExampleMemoryClient::commitSingleSnapshot(
const armem::MemoryID& entityID)
223 std::default_random_engine gen(std::random_device{}());
224 std::uniform_int_distribution<int> distrib(-20, 20);
227 armem::EntityUpdate
update;
228 update.entityID = entityID;
231 double diff = (
update.referencedTime - runStarted).toMilliSecondsDouble() / 1000;
233 auto dict1 = std::make_shared<aron::data::Dict>();
234 auto dict2 = std::make_shared<aron::data::Dict>();
236 auto sin = std::make_shared<aron::data::Float>(std::sin(diff));
237 auto cos = std::make_shared<aron::data::Float>(std::cos(diff));
239 auto sqrt = std::make_shared<aron::data::Double>(std::sqrt(diff));
240 auto lin = std::make_shared<aron::data::Long>(
static_cast<long>(diff * 1000));
241 auto rand = std::make_shared<aron::data::Int>(distrib(gen));
243 dict1->addElement(
"sin", sin);
244 dict1->addElement(
"cos", cos);
246 dict2->addElement(
"sqrt", sqrt);
247 dict2->addElement(
"lin", lin);
248 dict2->addElement(
"rand", rand);
250 update.instancesData = {dict1, dict2};
253 armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
255 if (!updateResult.success)
264 ExampleMemoryClient::commitMultipleSnapshots(
const armem::MemoryID& entityID,
int num)
267 armem::Commit commit;
268 for (
int i = 0; i < num; ++i)
270 armem::EntityUpdate&
update = commit.
add();
271 update.entityID = entityID;
273 for (
int j = 0; j < i; ++j)
275 update.instancesData.push_back(std::make_shared<aron::data::Dict>());
279 armem::CommitResult commitResult = memoryWriter.commit(commit);
281 if (!commitResult.allSuccess())
290 ExampleMemoryClient::queryLatestSnapshot(
const armem::MemoryID& entityID)
293 <<
"\n- entityID: \t'" << entityID <<
"'";
295 armem::client::query::Builder builder;
305 armem::client::QueryResult qResult = memoryReader.query(builder.
buildQueryInput());
311 armem::wm::Memory& memory = qResult.memory;
316 const armem::wm::Entity* entity = memory.
findEntity(entityID);
318 <<
"Entity " << entityID <<
" was not found in " <<
armem::print(memory);
325 <<
"\n- entity: \t" << entity->
name() <<
"\n- snapshot: \t"
326 << snapshot.
time() <<
"\n- #instances: \t" << snapshot.
size();
329 tab.queryResult = std::move(memory);
339 ExampleMemoryClient::queryExactSnapshot(
const armem::MemoryID& snapshotID)
342 <<
"\n- snapshotID: \t'" << snapshotID <<
"'";
344 namespace qf = armem::client::query_fns;
345 armem::client::query::Builder qb;
348 armem::client::QueryResult qResult = memoryReader.query(qb.
buildQueryInput());
353 armem::wm::Memory memory = std::move(qResult.memory);
358 <<
"\n- time: \t" << entitySnapshot.
time()
359 <<
"\n- # instances: \t" << entitySnapshot.
size();
362 const armem::wm::EntitySnapshot& entitySnapshot =
363 memory.
getEntity(snapshotID).getLatestSnapshot();
366 <<
"\n- time: \t" << entitySnapshot.
time()
367 <<
"\n- # instances: \t" << entitySnapshot.
size();
378 ExampleMemoryClient::commitExampleData()
384 auto addSegmentResult = memoryWriter.addSegment(
"ExampleData",
getName());
385 if (!addSegmentResult.success)
390 exampleDataProviderID = armem::MemoryID(addSegmentResult.segmentID);
392 addSegmentResult = memoryWriter.addSegment(
"LinkedData",
getName());
393 if (!addSegmentResult.success)
398 linkedDataProviderID = armem::MemoryID(addSegmentResult.segmentID);
401 armem::Commit commit;
405 armem::EntityUpdate&
update = commit.
add();
406 update.entityID = exampleDataProviderID.withEntityName(
"default");
407 update.referencedTime = time;
409 armem::example::ExampleData data_example;
410 toAron(data_example.memoryID, armem::MemoryID());
411 toAron(data_example.memoryLink.memoryID, armem::MemoryID());
412 auto instance = data_example.toAron();
414 update.instancesData = {instance};
421 armem::EntityUpdate& update_default = commit.
add();
425 armem::example::ExampleData data_default;
426 toAron(data_default.memoryID, armem::MemoryID());
427 toAron(data_default.memoryLink.memoryID, armem::MemoryID());
435 armem::EntityUpdate& update_answer = commit.
add();
439 armem::example::ExampleData
data;
440 data.the_bool =
true;
442 data.the_float = 21.5;
444 data.the_long = 424242;
445 data.the_string =
"fourty two";
446 data.the_float_list = {21, 42, 84};
447 data.the_int_list = {21, 42, 84};
448 data.the_string_list = simox::alg::multi_to_string(
data.the_int_list);
449 data.the_object_list.emplace_back();
451 data.the_float_dict = {
456 data.the_int_dict = {
462 data.the_position = {42, 24, 4224};
463 data.the_orientation = Eigen::AngleAxisf(1.57f, Eigen::Vector3f(1, 1, 1).normalized());
464 data.the_pose = simox::math::pose(
data.the_position,
data.the_orientation);
466 data.the_3x1_vector = {24, 42, 2442};
467 data.the_4x4_matrix = 42 * Eigen::Matrix4f::Identity();
470 toAron(
data.memoryLink.memoryID, armem::MemoryID());
477 armem::EntityUpdate& update_linked = commit.
add();
481 armem::example::LinkedData data_linked;
482 data_linked.yet_another_int = 42;
483 data_linked.yet_another_string =
"Hi! I'm from another core segment!";
484 data_linked.yet_another_object.element_int = 8349;
485 data_linked.yet_another_object.element_float = -1e3;
486 data_linked.yet_another_object.element_string =
487 "I'm a nested object in some linked data.";
492 armem::CommitResult commitResult = memoryWriter.commit(commit);
497 auto results_size = commitResult.
results.size();
498 ARMARX_INFO <<
"size(commitResults): " << results_size;
499 auto commitResults = commitResult.
results;
500 if (results_size > 2)
502 theAnswerSnapshotID = commitResults.at(1).snapshotID;
503 yetMoreDataSnapshotID = commitResults.at(2).snapshotID;
507 ARMARX_INFO <<
"Only " << results_size <<
" committed elements, instaed of "
511 catch (
const std::exception& e)
523 ExampleMemoryClient::queryExampleData()
526 armem::client::query::Builder qb;
528 .
withID(exampleProviderID)
530 .
withID(exampleProviderID)
536 armem::client::QueryResult result = memoryReader.query(qb.
buildQueryInput());
539 tab.queryResult = std::move(result.
memory);
549 ExampleMemoryClient::commitExamplesWithIDs()
554 armem::Commit commit;
556 armem::EntityUpdate&
update = commit.
add();
557 update.entityID = exampleDataProviderID.withEntityName(
"id to the_answer");
558 update.referencedTime = time;
560 armem::example::ExampleData
data;
567 armem::EntityUpdate&
update = commit.
add();
568 update.entityID = exampleDataProviderID.withEntityName(
"id to self");
569 update.referencedTime = time;
571 armem::example::ExampleData
data;
579 armem::EntityUpdate&
update = commit.
add();
580 update.entityID = exampleDataProviderID.withEntityName(
"id to previous snapshot");
583 armem::example::ExampleData
data;
590 armem::EntityUpdate&
update = commit.
add();
591 update.entityID = exampleDataProviderID.withEntityName(
"id to previous snapshot");
592 update.referencedTime = time;
594 armem::example::ExampleData
data;
603 armem::CommitResult commitResult = memoryWriter.commit(commit);
612 ARMARX_IMPORTANT <<
"Resolving multiple memory IDs via Memory Name System:";
614 std::vector<armem::MemoryID> ids;
615 for (armem::EntityUpdateResult& result : commitResult.
results)
621 std::map<armem::MemoryID, armem::wm::EntityInstance> instances =
625 std::stringstream ss;
626 for (
const auto& [
id, instance] : instances)
628 ss <<
"- Snapshot " <<
id <<
" "
629 <<
"\n--> Instance" << instance.id()
630 <<
" (# keys in data: " << instance.data()->childrenSize() <<
")"
638 ExampleMemoryClient::commitExamplesWithLinks()
644 armem::Commit commit;
646 armem::EntityUpdate&
update = commit.
add();
647 update.entityID = exampleDataProviderID.withEntityName(
"link to yet_more_data");
648 update.referencedTime = time;
650 armem::example::ExampleData
data;
658 armem::CommitResult commitResult = memoryWriter.commit(commit);
667 ARMARX_IMPORTANT <<
"Resolving multiple memory IDs via Memory Name System:";
669 std::vector<armem::MemoryID> ids;
670 for (armem::EntityUpdateResult& result : commitResult.
results)
676 std::map<armem::MemoryID, armem::wm::EntityInstance> instances =
680 std::stringstream ss;
681 for (
const auto& [
id, instance] : instances)
683 ss <<
"- Snapshot " <<
id <<
" "
684 <<
"\n--> Instance" << instance.id()
685 <<
" (# keys in data: " << instance.data()->childrenSize() <<
")"
693 ExampleMemoryClient::commitExampleImages()
697 armem::Commit commit;
699 armem::EntityUpdate&
update = commit.
add();
700 update.entityID = exampleDataProviderID.withEntityName(
"some_new_fancy_entity_id");
701 update.referencedTime = time;
703 auto currentFolder = std::filesystem::current_path();
704 auto opencv_img = cv::imread(
705 (currentFolder /
"images" / (std::to_string(imageCounter + 1) +
".jpg")).
string());
709 auto data = std::make_shared<aron::data::Dict>();
719 ExampleMemoryClient::commitExamplesWithUntypedData()
723 armem::Commit commit;
725 armem::EntityUpdate&
update = commit.
add();
726 update.entityID = exampleDataProviderID.withEntityName(
"unexpected_data");
727 update.referencedTime = time;
729 armem::example::ExampleData
data;
731 toAron(
data.memoryLink.memoryID, armem::MemoryID());
734 aron->addElement(
"unexpectedString",
735 std::make_shared<aron::data::String>(
"unexpected value"));
738 std::make_shared<aron::data::Dict>(std::map<std::string, aron::data::VariantPtr>{
739 {
"key43", std::make_shared<aron::data::Int>(43)},
740 {
"keyABC", std::make_shared<aron::data::String>(
"ABC")},
742 update.instancesData = {aron};
745 armem::CommitResult commitResult = memoryWriter.commit(commit);
753 ExampleMemoryClient::queryPredictionEngines()
755 const std::map<armem::MemoryID, std::vector<armem::PredictionEngine>> predictionEngines =
756 memoryReader.getAvailablePredictionEngines();
758 std::stringstream ss;
759 ss <<
"Prediction engines available in the server:" << std::endl;
760 for (
const auto& [
id, engines] : predictionEngines)
762 ss <<
" - " <<
id <<
": ";
763 for (
const armem::PredictionEngine& engine : engines)
765 ss << engine.engineID <<
", ";
774 ExampleMemoryClient::loadDataFromLTMExport()
777 std::string export_path =
"$HOME";
779 std::vector<std::string> coreSegmentNames = {
"ExampleData"};
780 bool addNonExistingCoreSegments =
false;
781 int amountOfSnapshotsPerSegmentToLoad = -1;
782 ARMARX_INFO <<
"Loading all data from " << export_path <<
"into core segment "
783 << coreSegmentNames[0];
784 memoryLoader.loadExportIntoWM(export_path,
787 addNonExistingCoreSegments,
788 amountOfSnapshotsPerSegmentToLoad);
792 ExampleMemoryClient::processExampleEntityUpdate(
const armem::MemoryID& subscriptionID,
793 const std::vector<armem::MemoryID>& snapshotIDs)
795 std::stringstream ss;
796 ss <<
"example_entity got updated: " << subscriptionID <<
"\n";
797 ss <<
"Updated snapshots: \n";
798 for (
const auto&
id : snapshotIDs)
800 ss <<
"- " <<
id <<
"\n";
822 if (tab.rebuild.exchange(
false))
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
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.