25 #include <Ice/ObjectAdapter.h>
35 #include <MemoryX/interface/memorytypes/MemorySegments.h>
46 virtual public WorkingMemoryEntitySegmentBase,
54 this->objectTypeId = T::ice_staticId();
70 const ::Ice::Current&
c = Ice::emptyCurrent)
override
74 throw armarx::LocalException(
"FusionMethod Ptr null");
77 fusionMethods.push_back(fusionMethod);
87 throw InvalidEntityException(
"entity is null");
91 if (!entity->ice_isA(objectTypeId))
93 throw InvalidEntityException(
"invalid object id");
97 EntityBasePtr newEntity = entity;
99 for (FusionMethodList::iterator it = fusionMethods.begin(); it != fusionMethods.end();
102 newEntity = (*it)->initEntity(newEntity);
118 std::string
id = newEntity->getId();
125 if (
id.
empty() || entityMap.find(
id) != entityMap.end())
129 newEntity->setId(
id);
135 const int intId = std::stoi(
id);
137 if (lastEntityId < intId)
139 lastEntityId = intId;
142 catch (std::exception
const&)
147 entityMap[id] = newEntity;
152 listenerProxy->reportEntityCreated(segmentName, newEntity);
172 addEntity(
const EntityBasePtr& entity, const ::Ice::Current&
c = Ice::emptyCurrent)
override
176 std::scoped_lock<std::mutex> lock(mutex);
177 return addEntityThreadUnsafe(entity);
181 addEntityList(
const EntityBaseList& entityList, const ::Ice::Current&
c)
override
183 EntityIdList entityIds(entityList.size(),
"");
185 for (
size_t i = 0; i < entityList.size(); i++)
189 entityIds[i] = addEntity(entityList[i],
c);
191 catch (
const memoryx::InvalidEntityException& e)
202 const EntityBasePtr& newEntity,
203 const ::Ice::Current&
c = Ice::emptyCurrent)
override
205 std::scoped_lock<std::mutex> lock(mutex);
206 if (hasEntityByIdThreadUnsafe(entityId))
208 updateEntityThreadUnsafe(entityId, newEntity, Ice::StringSeq());
213 return addEntityThreadUnsafe(newEntity);
220 EntityIdList entityIds(entityList.size(),
"");
222 for (
size_t i = 0; i < entityList.size(); i++)
226 entityIds[i] = upsertEntity(entityList[i]->getId(), entityList[i],
c);
228 catch (
const memoryx::InvalidEntityException& e)
239 const EntityBasePtr& newEntity,
240 const ::Ice::Current&
c = Ice::emptyCurrent)
override
242 std::scoped_lock<std::mutex> lock(mutex);
243 auto oldEntity = getEntityByNameThreadUnsafe(entityName);
246 updateEntityThreadUnsafe(oldEntity->getId(), newEntity, Ice::StringSeq());
247 return oldEntity->getId();
251 return addEntityThreadUnsafe(newEntity);
257 const EntityBasePtr&
update,
258 const ::Ice::Current&
c = Ice::emptyCurrent)
override
260 updateEntity(entityId,
update, Ice::StringSeq(),
c);
265 const EntityBasePtr&
update,
266 const Ice::StringSeq& deactivatedFusionMethods,
267 const ::Ice::Current&
c = Ice::emptyCurrent)
269 std::scoped_lock<std::mutex> lock(mutex);
270 updateEntityThreadUnsafe(entityId,
update, deactivatedFusionMethods);
276 const EntityBasePtr&
update,
277 const Ice::StringSeq& deactivatedFusionMethods)
284 throw InvalidEntityException();
288 EntityBasePtr oldEntity = getEntityByIdUnsafe(entityId);
292 throw EntityNotFoundException();
296 if (
update->getName() != oldEntity->getName())
299 <<
"', Old name = '" << oldEntity->getName()
300 <<
"', Entity id = " << entityId;
301 throw InvalidEntityException();
308 EntityBasePtr newEntity =
update;
311 for (FusionMethodList::iterator it = fusionMethods.begin(); it != fusionMethods.end();
314 if (std::find(deactivatedFusionMethods.begin(),
315 deactivatedFusionMethods.end(),
316 (*it)->getMethodName()) == deactivatedFusionMethods.end())
318 newEntity = (*it)->fuseEntity(oldEntity, newEntity);
322 entityMap[entityId] = newEntity;
327 listenerProxy->reportEntityUpdated(
329 EntityPtr::dynamicCast(oldEntity->ice_clone()),
330 EntityPtr::dynamicCast(newEntity->ice_clone()));
336 removeEntity(const ::std::string&
id, const ::Ice::Current&
c = Ice::emptyCurrent)
override
338 std::scoped_lock<std::mutex> lock(mutex);
339 IdEntityMap::iterator it = entityMap.find(
id);
341 if (it == entityMap.end())
343 throw EntityNotFoundException();
346 EntityBasePtr entity = it->second;
351 listenerProxy->reportEntityRemoved(segmentName, entity);
363 const EntityAttributeBasePtr& attribute,
364 const ::Ice::Current&
c)
override
366 std::scoped_lock<std::mutex> lock(mutex);
367 IdEntityMap::iterator it = entityMap.find(entityId);
369 if (it == entityMap.end())
371 throw EntityNotFoundException();
374 it->second->putAttribute(attribute);
379 const EntityAttributeList& attributeMap,
380 const ::Ice::Current&
c)
override
382 std::scoped_lock<std::mutex> lock(mutex);
383 IdEntityMap::iterator it = entityMap.find(entityId);
385 if (it == entityMap.end())
387 throw EntityNotFoundException();
390 for (
auto attribute : attributeMap)
392 it->second->putAttribute(attribute);
397 clear(const ::Ice::Current&
c = Ice::emptyCurrent)
override
400 std::scoped_lock<std::mutex> lock(mutex);
402 for (
auto entityMapIter = entityMap.begin(); entityMapIter != entityMap.end();)
404 EntityBasePtr entity = entityMapIter->second;
408 listenerProxy->reportEntityRemoved(segmentName, entity);
411 entityMapIter = entityMap.erase(entityMapIter);
419 listenerProxy->reportMemoryCleared(segmentName);
424 size(const ::Ice::Current&
c = Ice::emptyCurrent)
const override
426 std::scoped_lock<std::mutex> lock(mutex);
431 hasEntityById(
const std::string& entityId, const ::Ice::Current&
c)
const override
433 std::scoped_lock<std::mutex> lock(mutex);
434 return hasEntityByIdThreadUnsafe(entityId);
441 IdEntityMap::const_iterator it = entityMap.find(entityId);
442 return it != entityMap.end();
449 std::scoped_lock<std::mutex> lock(mutex);
451 for (
const auto& entity : entityMap)
453 if (entity.second && entity.second->getName() == entityName)
465 std::scoped_lock<std::mutex> lock(mutex);
466 return getEntityByIdUnsafe(
id);
472 std::scoped_lock<std::mutex> lock(mutex);
473 IdEntityMap::const_iterator it = entityMap.find(
id);
475 if (it != entityMap.end())
478 return EntityBasePtr::dynamicCast(it->second->ice_clone());
484 return EntityBasePtr();
491 std::scoped_lock<std::mutex> lock(mutex);
492 return getEntityByNameThreadUnsafe(name);
499 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
502 if (it->second->getName() == name)
504 return EntityBasePtr::dynamicCast(it->second);
508 std::string entities;
510 for (
auto e : entityMap)
512 entities += e.second->getName() +
" ";
515 ARMARX_INFO_S <<
"Could not find entity named " << name <<
" in entity list { "
517 return EntityBasePtr();
524 std::scoped_lock<std::mutex> lock(mutex);
526 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
529 if (it->second->getName() == name)
531 return EntityBasePtr::dynamicCast(it->second->ice_clone());
537 std::string entities;
539 for (
auto e : entityMap)
541 entities += e.second->getName() +
" ";
544 ARMARX_INFO_S <<
"Could not find entity named " << name <<
" in entity list { "
546 return EntityBasePtr();
551 const ::std::string& attrValue,
552 const ::Ice::Current&
c)
const override
554 NameList attrValueList;
555 attrValueList.push_back(attrValue);
556 return getEntitiesByAttrValueList(attrName, attrValueList,
c);
562 NameList attrValueList;
563 attrValueList.push_back(attrValue);
564 return getEntitiesByAttrValueList(attrName, attrValueList);
569 const NameList& attrValueList)
const
571 EntityBaseList result;
573 std::scoped_lock<std::mutex> lock(mutex);
574 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
576 const EntityAttributeBasePtr attr = it->second->getAttribute(attrName);
580 for (NameList::const_iterator itVal = attrValueList.begin();
581 itVal != attrValueList.end();
586 result.push_back(EntityBasePtr::dynamicCast(it->second));
598 const NameList& attrValueList,
599 const ::Ice::Current&
c)
const override
601 EntityBaseList result;
603 std::scoped_lock<std::mutex> lock(mutex);
605 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
607 const EntityAttributeBasePtr attr = it->second->getAttribute(attrName);
611 for (NameList::const_iterator itVal = attrValueList.begin();
612 itVal != attrValueList.end();
618 result.push_back(EntityBasePtr::dynamicCast(it->second->ice_clone()));
632 std::scoped_lock<std::mutex> lock(mutex);
633 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
635 result.push_back(it->first);
644 EntityBaseList result;
645 std::scoped_lock<std::mutex> lock(mutex);
646 result.reserve(entityMap.size());
648 for (
const auto& entry : entityMap)
650 result.push_back(EntityBasePtr::dynamicCast(entry.second->ice_clone()));
660 std::scoped_lock<std::mutex> lock(mutex);
662 for (
const auto& entry : entityMap)
664 result.insert({entry.first, EntityBasePtr::dynamicCast(entry.second->ice_clone())});
671 print(const ::Ice::Current&
c = Ice::emptyCurrent)
const override
673 std::scoped_lock<std::mutex> lock(mutex);
675 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
677 ARMARX_INFO_S << EntityPtr::dynamicCast(it->second) << std::endl;
691 WorkingMemoryListenerInterfacePrx
694 return this->listenerProxy;
705 const ::Ice::Current&
c = Ice::emptyCurrent)
override
707 this->listenerProxy = listenerProxy;
713 const ::Ice::Current&
c = Ice::emptyCurrent)
override
715 this->segmentName = segmentName;
721 this->parentMemory = memory;
733 auto entity = getEntityById(
id,
c);
741 EntityMemorySegmentInterfacePrx::uncheckedCast(
c.adapter->createProxy(
c.id));
743 std::string memName = mem->getMemoryName();
744 auto memoryProxy = MemoryInterfacePrx::checkedCast(
745 c.adapter->getCommunicator()->stringToProxy(memName));
758 auto entity = getEntityByName(name,
c);
773 std::string memName = mem->getMemoryName();
775 EntityMemorySegmentInterfacePrx::uncheckedCast(
c.adapter->createProxy(
c.id));
776 auto memoryProxy = MemoryInterfacePrx::checkedCast(
777 c.adapter->getCommunicator()->stringToProxy(memName));
788 id.name = parentMemory->getMemoryName() +
"_" + segmentName;
796 IdEntityMap::const_iterator it = entityMap.find(
id);
798 if (it != entityMap.end())
800 return EntityBasePtr::dynamicCast(it->second);
805 return EntityBasePtr();