30 #include <MemoryX/interface/memorytypes/MemorySegments.h>
35 #include <Ice/ObjectAdapter.h>
45 virtual public WorkingMemoryEntitySegmentBase,
54 this->objectTypeId = T::ice_staticId();
62 std::string
getObjectTypeId(const ::Ice::Current&
c = Ice::emptyCurrent)
const override
67 void addFusionMethod(const ::memoryx::EntityFusionMethodBasePtr& fusionMethod, const ::Ice::Current&
c = Ice::emptyCurrent)
override
71 throw armarx::LocalException(
"FusionMethod Ptr null");
74 fusionMethods.push_back(fusionMethod);
83 throw InvalidEntityException(
"entity is null");
87 if (!entity->ice_isA(objectTypeId))
89 throw InvalidEntityException(
"invalid object id");
93 EntityBasePtr newEntity = entity;
95 for (FusionMethodList::iterator it = fusionMethods.begin(); it != fusionMethods.end(); ++it)
97 newEntity = (*it)->initEntity(newEntity);
113 std::string
id = newEntity->getId();
120 if (
id.
empty() || entityMap.find(
id) != entityMap.end())
124 newEntity->setId(
id);
130 const int intId = std::stoi(
id);
132 if (lastEntityId < intId)
134 lastEntityId = intId;
137 catch (std::exception
const&) {}
140 entityMap[id] = newEntity;
145 listenerProxy->reportEntityCreated(segmentName, newEntity);
164 std::string
addEntity(
const EntityBasePtr& entity, const ::Ice::Current&
c = Ice::emptyCurrent)
override
168 std::scoped_lock<std::mutex> lock(mutex);
169 return addEntityThreadUnsafe(entity);
174 EntityIdList
addEntityList(
const EntityBaseList& entityList, const ::Ice::Current&
c)
override
176 EntityIdList entityIds(entityList.size(),
"");
178 for (
size_t i = 0; i < entityList.size(); i++)
182 entityIds[i] = addEntity(entityList[i],
c);
184 catch (
const memoryx::InvalidEntityException& e)
194 std::string
upsertEntity(
const std::string& entityId,
const EntityBasePtr& newEntity, const ::Ice::Current&
c = Ice::emptyCurrent)
override
196 std::scoped_lock<std::mutex> lock(mutex);
197 if (hasEntityByIdThreadUnsafe(entityId))
199 updateEntityThreadUnsafe(entityId, newEntity, Ice::StringSeq());
204 return addEntityThreadUnsafe(newEntity);
209 EntityIdList
upsertEntityList(
const EntityBaseList& entityList, const ::Ice::Current&
c)
override
211 EntityIdList entityIds(entityList.size(),
"");
213 for (
size_t i = 0; i < entityList.size(); i++)
217 entityIds[i] = upsertEntity(entityList[i]->getId(), entityList[i],
c);
219 catch (
const memoryx::InvalidEntityException& e)
229 std::string
upsertEntityByName(
const std::string& entityName,
const EntityBasePtr& newEntity, const ::Ice::Current&
c = Ice::emptyCurrent)
override
231 std::scoped_lock<std::mutex> lock(mutex);
232 auto oldEntity = getEntityByNameThreadUnsafe(entityName);
235 updateEntityThreadUnsafe(oldEntity->getId(), newEntity, Ice::StringSeq());
236 return oldEntity->getId();
240 return addEntityThreadUnsafe(newEntity);
246 void updateEntity(
const std::string& entityId,
const EntityBasePtr&
update, const ::Ice::Current&
c = Ice::emptyCurrent)
override
248 updateEntity(entityId,
update, Ice::StringSeq(),
c);
252 void updateEntity(
const std::string& entityId,
const EntityBasePtr&
update,
const Ice::StringSeq& deactivatedFusionMethods, const ::Ice::Current&
c = Ice::emptyCurrent)
254 std::scoped_lock<std::mutex> lock(mutex);
255 updateEntityThreadUnsafe(entityId,
update, deactivatedFusionMethods);
266 throw InvalidEntityException();
270 EntityBasePtr oldEntity = getEntityByIdUnsafe(entityId);
274 throw EntityNotFoundException();
278 if (
update->getName() != oldEntity->getName())
280 ARMARX_FATAL_S <<
"Entity names do not match: New name = '" <<
update->getName() <<
"', Old name = '" << oldEntity->getName() <<
"', Entity id = " << entityId;
281 throw InvalidEntityException();
288 EntityBasePtr newEntity =
update;
291 for (FusionMethodList::iterator it = fusionMethods.begin(); it != fusionMethods.end(); ++it)
293 if (std::find(deactivatedFusionMethods.begin(), deactivatedFusionMethods.end(), (*it)->getMethodName()) == deactivatedFusionMethods.end())
295 newEntity = (*it)->fuseEntity(oldEntity, newEntity);
299 entityMap[entityId] = newEntity;
304 listenerProxy->reportEntityUpdated(segmentName,
305 EntityPtr::dynamicCast(oldEntity->ice_clone()),
306 EntityPtr::dynamicCast(newEntity->ice_clone()));
312 void removeEntity(const ::std::string&
id, const ::Ice::Current&
c = Ice::emptyCurrent)
override
314 std::scoped_lock<std::mutex> lock(mutex);
315 IdEntityMap::iterator it = entityMap.find(
id);
317 if (it == entityMap.end())
319 throw EntityNotFoundException();
322 EntityBasePtr entity = it->second;
327 listenerProxy->reportEntityRemoved(segmentName, entity);
336 void setEntityAttribute(
const std::string& entityId,
const EntityAttributeBasePtr& attribute, const ::Ice::Current&
c)
override
338 std::scoped_lock<std::mutex> lock(mutex);
339 IdEntityMap::iterator it = entityMap.find(entityId);
341 if (it == entityMap.end())
343 throw EntityNotFoundException();
346 it->second->putAttribute(attribute);
349 void setEntityAttributes(
const std::string& entityId,
const EntityAttributeList& attributeMap, const ::Ice::Current&
c)
override
351 std::scoped_lock<std::mutex> lock(mutex);
352 IdEntityMap::iterator it = entityMap.find(entityId);
354 if (it == entityMap.end())
356 throw EntityNotFoundException();
359 for (
auto attribute : attributeMap)
361 it->second->putAttribute(attribute);
365 void clear(const ::Ice::Current&
c = Ice::emptyCurrent)
override
368 std::scoped_lock<std::mutex> lock(mutex);
370 for (
auto entityMapIter = entityMap.begin(); entityMapIter != entityMap.end();)
372 EntityBasePtr entity = entityMapIter->second;
376 listenerProxy->reportEntityRemoved(segmentName, entity);
379 entityMapIter = entityMap.erase(entityMapIter);
387 listenerProxy->reportMemoryCleared(segmentName);
393 std::scoped_lock<std::mutex> lock(mutex);
397 bool hasEntityById(
const std::string& entityId, const ::Ice::Current&
c)
const override
399 std::scoped_lock<std::mutex> lock(mutex);
400 return hasEntityByIdThreadUnsafe(entityId);
405 IdEntityMap::const_iterator it = entityMap.find(entityId);
406 return it != entityMap.end();
410 bool hasEntityByName(
const std::string& entityName, const ::Ice::Current&
c)
const override
412 std::scoped_lock<std::mutex> lock(mutex);
414 for (
const auto& entity : entityMap)
416 if (entity.second && entity.second->getName() == entityName)
429 std::scoped_lock<std::mutex> lock(mutex);
430 return getEntityByIdUnsafe(
id);
433 EntityBasePtr
getEntityById(const ::std::string&
id, const ::Ice::Current&
c)
const override
435 std::scoped_lock<std::mutex> lock(mutex);
436 IdEntityMap::const_iterator it = entityMap.find(
id);
438 if (it != entityMap.end())
441 return EntityBasePtr::dynamicCast(it->second->ice_clone());
447 return EntityBasePtr();
453 std::scoped_lock<std::mutex> lock(mutex);
454 return getEntityByNameThreadUnsafe(name);
459 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
462 if (it->second->getName() == name)
464 return EntityBasePtr::dynamicCast(it->second);
468 std::string entities;
470 for (
auto e : entityMap)
472 entities += e.second->getName() +
" ";
475 ARMARX_INFO_S <<
"Could not find entity named " << name <<
" in entity list { " << entities <<
" }";
476 return EntityBasePtr();
480 EntityBasePtr
getEntityByName(const ::std::string& name, const ::Ice::Current&
c)
const override
482 std::scoped_lock<std::mutex> lock(mutex);
484 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
487 if (it->second->getName() == name)
489 return EntityBasePtr::dynamicCast(it->second->ice_clone());
495 std::string entities;
497 for (
auto e : entityMap)
499 entities += e.second->getName() +
" ";
502 ARMARX_INFO_S <<
"Could not find entity named " << name <<
" in entity list { " << entities <<
" }";
503 return EntityBasePtr();
507 EntityBaseList
getEntitiesByAttrValue(const ::std::string& attrName, const ::std::string& attrValue, const ::Ice::Current&
c)
const override
509 NameList attrValueList;
510 attrValueList.push_back(attrValue);
511 return getEntitiesByAttrValueList(attrName, attrValueList,
c);
516 NameList attrValueList;
517 attrValueList.push_back(attrValue);
518 return getEntitiesByAttrValueList(attrName, attrValueList);
523 EntityBaseList result;
525 std::scoped_lock<std::mutex> lock(mutex);
526 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
528 const EntityAttributeBasePtr attr = it->second->getAttribute(attrName);
532 for (NameList::const_iterator itVal = attrValueList.begin(); itVal != attrValueList.end(); ++itVal)
536 result.push_back(EntityBasePtr::dynamicCast(it->second));
548 EntityBaseList result;
550 std::scoped_lock<std::mutex> lock(mutex);
552 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
554 const EntityAttributeBasePtr attr = it->second->getAttribute(attrName);
558 for (NameList::const_iterator itVal = attrValueList.begin(); itVal != attrValueList.end(); ++itVal)
563 result.push_back(EntityBasePtr::dynamicCast(it->second->ice_clone()));
576 std::scoped_lock<std::mutex> lock(mutex);
577 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
579 result.push_back(it->first);
585 EntityBaseList
getAllEntities(
const Ice::Current&
c = Ice::emptyCurrent)
const override
587 EntityBaseList result;
588 std::scoped_lock<std::mutex> lock(mutex);
589 result.reserve(entityMap.size());
591 for (
const auto& entry : entityMap)
593 result.push_back(EntityBasePtr::dynamicCast(entry.second->ice_clone()));
602 std::scoped_lock<std::mutex> lock(mutex);
604 for (
const auto& entry : entityMap)
606 result.insert({entry.first, EntityBasePtr::dynamicCast(entry.second->ice_clone())});
612 void print(const ::Ice::Current&
c = Ice::emptyCurrent)
const override
614 std::scoped_lock<std::mutex> lock(mutex);
616 for (IdEntityMap::const_iterator it = entityMap.begin(); it != entityMap.end(); ++it)
618 ARMARX_INFO_S << EntityPtr::dynamicCast(it->second) << std::endl;
622 std::string
getSegmentName(const ::Ice::Current&
c = Ice::emptyCurrent)
const override
631 WorkingMemoryListenerInterfacePrx
getListenerProxy(const ::Ice::Current&
c = Ice::emptyCurrent)
override
633 return this->listenerProxy;
642 void setListenerProxy(
const WorkingMemoryListenerInterfacePrx& listenerProxy, const ::Ice::Current&
c = Ice::emptyCurrent)
override
644 this->listenerProxy = listenerProxy;
648 void setSegmentName(
const std::string& segmentName, const ::Ice::Current&
c = Ice::emptyCurrent)
override
650 this->segmentName = segmentName;
655 this->parentMemory = memory;
666 auto entity = getEntityById(
id,
c);
673 auto proxy = EntityMemorySegmentInterfacePrx::uncheckedCast(
c.adapter->createProxy(
c.id));
675 std::string memName = mem->getMemoryName();
676 auto memoryProxy = MemoryInterfacePrx::checkedCast(
c.adapter->getCommunicator()->stringToProxy(memName));
691 auto entity = getEntityByName(name,
c);
706 std::string memName = mem->getMemoryName();
707 auto proxy = EntityMemorySegmentInterfacePrx::uncheckedCast(
c.adapter->createProxy(
c.id));
708 auto memoryProxy = MemoryInterfacePrx::checkedCast(
c.adapter->getCommunicator()->stringToProxy(memName));
722 id.name = parentMemory->getMemoryName() +
"_" + segmentName;
728 IdEntityMap::const_iterator it = entityMap.find(
id);
730 if (it != entityMap.end())
732 return EntityBasePtr::dynamicCast(it->second);
737 return EntityBasePtr();