34#include <MemoryX/interface/memorytypes/MemoryEntities.h>
45 std::cout <<
"onInitComponent" << std::endl;
61 std::cout <<
"onStartComponent" << std::endl;
63 std::cout <<
"Starting WorkingMemoryExample" << std::endl;
69 robotStateComponentPrx =
76 if (!snapshotName.empty())
78 longtermMemoryPrx->loadWorkingMemorySnapshot(snapshotName, memoryPrx);
79 ARMARX_INFO <<
"Snapshot succcessfully loaded: " << snapshotName;
83 ARMARX_ERROR <<
"SnapshotName parameter must be specified!";
86 if (testToRun ==
"UpdateNotifications")
94 else if (testToRun ==
"FileTransfer")
98 else if (testToRun ==
"ObjectMove")
100 testObjectMovement();
102 else if (testToRun ==
"SnapshotLoad")
106 else if (testToRun ==
"CommonPlaces")
110 else if (testToRun ==
"Agent")
116 std::cout <<
"Unknown test name: " << testToRun << std::endl;
122 const memoryx::EntityBasePtr& entity,
125 std::cout <<
"Entity created: " << EntityPtr::dynamicCast(entity) << std::endl << std::endl;
130 const memoryx::EntityBasePtr& entityOld,
131 const memoryx::EntityBasePtr& entityNew,
134 std::cout <<
"Entity changed: " << std::endl;
135 std::cout <<
"Old: " << EntityPtr::dynamicCast(entityOld) << std::endl;
136 std::cout <<
"New: " << EntityPtr::dynamicCast(entityNew) << std::endl << std::endl;
141 const memoryx::EntityBasePtr& entity,
144 std::cout <<
"Entity removed: id = " << entity->getId() << std::endl << std::endl;
150 std::cout <<
"Snapshot loaded!" << std::endl << std::endl;
156 std::cout <<
"Scene cleared!" << std::endl << std::endl;
160 WorkingMemoryExample::testFileTransfer()
162 ObjectInstanceMemorySegmentBasePrx objectInstancesMemoryPrx =
163 memoryPrx->getObjectInstancesSegment();
167 size_t totalBytes = 0;
168 EntityIdList ids = objectInstancesMemoryPrx->getAllEntityIds();
170 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
172 const EntityBasePtr obj = objectInstancesMemoryPrx->getEntityById(*it);
174 if (obj->hasAttribute(
"IvFile"))
178 fileManager->getFileStream(obj->getAttribute(
"IvFile"), fs);
181 fs.getline(ivFile, 128);
183 std::cout << ivFile << std::endl;
190 std::cout <<
"# of fetched iv-files: " << ivCount << std::endl;
191 std::cout <<
"Bytes transfered: " << totalBytes << std::endl;
195 WorkingMemoryExample::testUpdateObserver()
198 ObjectInstanceMemorySegmentBasePrx objectInstancesMemoryPrx =
199 memoryPrx->getObjectInstancesSegment();
200 RelationMemorySegmentBasePrx relationsMemoryPrx = memoryPrx->getRelationsSegment();
204 cup->putAttribute(
"Description",
"my favourite cup");
206 cup->addClass(
"Cup", 0.6f);
207 cup->addClass(
"Mug", 0.4f);
208 std::string cupId = objectInstancesMemoryPrx->addEntity(cup);
211 std::cout <<
"Stored object " << cup->getName() <<
" in WorkingMemory with id = " << cupId
213 std::cout <<
"Most probable class is <" << cup->getMostProbableClass()
214 <<
"> with prob = " << cup->getClassProbability(cup->getMostProbableClass())
217 Eigen::Vector3f posVec(1., 2., 3.);
220 cup->setPosition(curPos);
222 FloatVector posStdVec(posVec.data(), posVec.data() + 3);
224 posDist->setMean(posStdVec);
225 posDist->setCovariance(0, 0, 0.5);
226 posDist->setCovariance(1, 1, 0.5);
227 posDist->setCovariance(2, 2, 0.5);
228 cup->setPositionUncertainty(posDist);
230 objectInstancesMemoryPrx->updateEntity(cupId, cup);
231 std::cout <<
"Updated object " << cup->getName() <<
" in WorkingMemory with id = " << cupId
235 std::cout <<
"Cloned object: " << cupClone << std::endl;
238 fridge->setClass(
"Fridge", 1.f);
239 const std::string fridgeId = objectInstancesMemoryPrx->addEntity(fridge);
241 std::cout <<
"Stored object " << fridge->getName()
242 <<
" in WorkingMemory with id = " << fridgeId << std::endl;
244 EntityPtr juice =
new ObjectInstance(
"Juice");
245 const std::string juiceId = objectInstancesMemoryPrx->addEntity(juice);
247 std::cout <<
"Stored object " << juice->getName()
248 <<
" in WorkingMemory with id = " << juiceId << std::endl;
250 EntityRefBasePtr cupRef = objectInstancesMemoryPrx->getEntityRefById(cupId);
252 EntityRefBasePtr fridgeRef = objectInstancesMemoryPrx->getEntityRefById(fridgeId);
253 EntityRefBasePtr juiceRef = objectInstancesMemoryPrx->getEntityRefById(juiceId);
255 RelationBasePtr rel1 =
new Relation(
"isIn", {cupRef, fridgeRef});
256 relationsMemoryPrx->addEntity(rel1);
258 RelationBasePtr rel2 =
new Relation(
"isIn", {juiceRef, fridgeRef},
true, 0.7);
259 relationsMemoryPrx->addEntity(rel2);
261 RelationList rels = relationsMemoryPrx->getRelationsByEntityId(cupRef->entityId);
262 std::cout <<
"Found relations: " << rels.size() << std::endl;
264 for (RelationList::const_iterator it = rels.begin(); it != rels.end(); ++it)
266 RelationBasePtr relation = *it;
267 std::cout << relation->getName() <<
": " << std::endl;
269 for (EntityRefBasePtr entityRef : relation->getEntities())
271 std::cout <<
" * " << entityRef->entityName << std::endl;
306 WorkingMemoryExample::testObjectMovement()
308 ObjectInstanceMemorySegmentBasePrx objectInstancesMemoryPrx =
309 memoryPrx->getObjectInstancesSegment();
312 cup->setClass(
"cup", 1.f);
314 Eigen::Vector3f posVec(-5000., 2500, 1000.);
317 cup->setPosition(curPos);
319 FloatVector posStdVec(posVec.data(), posVec.data() + 3);
321 posDist->setMean(posStdVec);
322 posDist->setCovariance(0, 0, 80.);
323 posDist->setCovariance(1, 1, 20.);
324 posDist->setCovariance(2, 2, 200.);
325 cup->setPositionUncertainty(posDist);
328 armarx::FramedOrientationBasePtr orient =
330 cup->setOrientation(orient);
334 const std::string cupId = objectInstancesMemoryPrx->addEntity(cup);
341 cup->removeAttribute(
"Weight");
343 std::this_thread::sleep_for(std::chrono::milliseconds(3000));
345 for (
int i = 0; i < 500; ++i)
348 posStdVec[0] = curPos->x;
349 posDist->setMean(posStdVec);
350 cup->setPosition(curPos);
351 cup->setPositionUncertainty(posDist);
352 cup->setExistenceCertainty(i / 500.);
353 objectInstancesMemoryPrx->updateEntity(cupId, cup);
354 std::this_thread::sleep_for(std::chrono::milliseconds(100));
358 std::this_thread::sleep_for(std::chrono::milliseconds(10000));
359 objectInstancesMemoryPrx->removeEntity(cupId);
363 WorkingMemoryExample::testSnapshots()
365 ARMARX_INFO <<
"Content of memory after loading snapshot: ";
370 WorkingMemoryExample::testCommonPlaces()
372 ObjectInstanceMemorySegmentBasePrx objectInstancesMemoryPrx =
373 memoryPrx->getObjectInstancesSegment();
375 const std::string snapshotName =
377 WorkingMemorySnapshotInterfacePrx snapshot =
378 longtermMemoryPrx->getWorkingMemorySnapshotListSegment()->openSnapshot(snapshotName);
379 PersistentEntitySegmentBasePrx segObjects = snapshot->getSegment(
"objectInstances");
383 EntityIdList ids = segObjects->getAllEntityIds();
385 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
387 objectInstancesMemoryPrx->addEntity(segObjects->getEntityById(*it));
393 WorkingMemoryExample::testAgent()
395 AgentInstancesSegmentBasePrx agentsMemoryPrx = memoryPrx->getAgentInstancesSegment();
398 Eigen::Vector3f positionVector(0.0, 0.0, 0.0);
402 quaternion.normalize();
403 armarx::FramedOrientationBasePtr currentOrientation =
406 auto robot = robotStateComponentPrx->getSynchronizedRobot();
408 agent->setSharedRobot(robot);
409 agent->setStringifiedSharedRobotInterfaceProxy(iceCommunicator->proxyToString(robot));
410 agent->setAgentFilePath(
"Armar3/robotmodel/ArmarIII.xml");
411 agent->setOrientation(currentOrientation);
412 agent->setPosition(currentPosition);
414 std::string agentId = agentsMemoryPrx->addEntity(agent);
416 std::cout <<
"Agent Armar3 created in AgentInstancesSegment of WorkingMemory" << std::endl;
418 std::this_thread::sleep_for(std::chrono::milliseconds(3000));
420 for (
int i = 0; i < 500; ++i)
422 currentPosition->x += 10;
423 agent->setPosition(currentPosition);
424 agentsMemoryPrx->updateEntity(agentId, agent);
425 std::this_thread::sleep_for(std::chrono::milliseconds(100));
Property< PropertyType > getProperty(const std::string &name)
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
IceManagerPtr getIceManager() const
Returns the IceManager.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
void onInitComponent() override
Pure virtual hook for the subclass.
void reportSnapshotLoaded(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
void reportSnapshotCompletelyLoaded(const Ice::Current &c=Ice::emptyCurrent) override
void reportEntityRemoved(const std::string &segmentName, const ::memoryx::EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
void reportEntityUpdated(const std::string &segmentName, const ::memoryx::EntityBasePtr &entityOld, const ::memoryx::EntityBasePtr &entityNew, const ::Ice::Current &=Ice::emptyCurrent) override
void reportEntityCreated(const std::string &segmentName, const ::memoryx::EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
void onConnectComponent() override
Pure virtual hook for the subclass.
void reportMemoryCleared(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
std::string const GlobalFrame
Variable of the global coordinate system.
Quaternion< float, 0 > Quaternionf
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
const armarx::VariantTypeId MultivariateNormalDistribution
const armarx::VariantTypeId UnivariateNormalDistribution
::std::vector<::Ice::Float > FloatVector
IceInternal::Handle< FramedPosition > FramedPositionPtr
IceInternal::Handle< AgentInstance > AgentInstancePtr
Typedef of AgentEntityPtr as IceInternal::Handle<AgentEntity> for convenience.
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
IceInternal::Handle< MultivariateNormalDistribution > MultivariateNormalDistributionPtr
IceInternal::Handle< Entity > EntityPtr
Typedef of EntityPtr as IceInternal::Handle<Entity> for convenience.
std::shared_ptr< GridFileManager > GridFileManagerPtr