30 #include <Ice/ObjectAdapter.h>
31 #include <IceUtil/UUID.h>
33 #include <MemoryX/interface/workingmemory/WorkingMemoryListenerInterface.h>
39 databasePrx(databasePrx)
41 iceId.name = IceUtil::generateUUID();
46 addSegmentToMap(segName);
52 addSegmentToMap(segName);
53 return getSegmentPtr(segName);
56 void WorkingMemorySnapshot::addSegmentToMap(
const std::string& segName)
58 segmentNames[segName] = getSegmentCollectionName(segName);
68 segId.name +=
"_" + IceUtil::generateUUID();
70 openedSegments[segId] = segment;
72 Ice::ObjectPrx node =
c.adapter->add(segment, segId);
73 return PersistentEntitySegmentBasePrx::uncheckedCast(node);
77 return PersistentEntitySegmentBasePrx();
83 SegmentMap::const_iterator itSeg = segmentNames.find(segName);
85 if (itSeg != segmentNames.end())
88 CollectionInterfacePrx segmentColl = databasePrx->requestCollection(itSeg->second);
93 segment->setSegmentName(segName);
94 return PersistentEntitySegmentPtr::dynamicCast(segment);
110 return segmentNames.count(segName);
117 for (SegmentMap::const_iterator it = segmentNames.begin(); it != segmentNames.end(); ++it)
119 result.push_back(it->first);
127 for (SegmentMap::const_iterator it = segmentNames.begin(); it != segmentNames.end(); ++it)
129 databasePrx->dropCollection(it->second);
132 segmentNames.clear();
142 NameList wmSegNames = workingMemory->getSegmentNames();
144 for (NameList::const_iterator itSeg = wmSegNames.begin(); itSeg != wmSegNames.end(); ++itSeg)
146 WorkingMemoryEntitySegmentBasePrx wmSegment = WorkingMemoryEntitySegmentBasePrx::uncheckedCast(workingMemory->getSegment(*itSeg));
150 PersistentEntitySegmentBasePtr snapSegment = addSegmentPtr(*itSeg);
151 EntityIdList ids = wmSegment->getAllEntityIds();
153 for (EntityIdList::const_iterator itEntity = ids.begin(); itEntity != ids.end(); ++itEntity)
155 snapSegment->addEntity(wmSegment->getEntityById(*itEntity));
163 NameList wmSegNames = workingMemory->getSegmentNames();
164 auto sortedEntities = entityIdList;
165 std::sort(sortedEntities.begin(), sortedEntities.end());
166 auto newEnd = std::unique(sortedEntities.begin(), sortedEntities.end());
167 sortedEntities.erase(newEnd, sortedEntities.end());
169 for (
const auto& segName : wmSegNames)
171 WorkingMemoryEntitySegmentBasePrx wmSegment = WorkingMemoryEntitySegmentBasePrx::uncheckedCast(workingMemory->getSegment(segName));
175 PersistentEntitySegmentBasePtr snapSegment = addSegmentPtr(segName);
176 EntityIdList ids = wmSegment->getAllEntityIds();
177 std::sort(ids.begin(), ids.end());
178 Ice::StringSeq intersectionList(ids.size());
179 auto intersectionEnd = std::set_intersection(ids.begin(), ids.end(),
180 sortedEntities.begin(), sortedEntities.end(),
181 intersectionList.begin());
182 intersectionList.resize(intersectionEnd - intersectionList.begin());
183 auto newEnd = std::unique(intersectionList.begin(), intersectionList.end());
184 intersectionList.erase(newEnd, intersectionList.end());
186 for (EntityIdList::const_iterator itEntity = intersectionList.begin(); itEntity != intersectionList.end(); ++itEntity)
188 snapSegment->addEntity(wmSegment->getEntityById(*itEntity));
196 std::set<WorkingMemoryListenerInterfacePrx> listeners;
197 for (SegmentMap::const_iterator itSeg = segmentNames.begin(); itSeg != segmentNames.end(); ++itSeg)
199 PersistentEntitySegmentBasePtr snapSegment = getSegmentPtr(itSeg->first);
200 WorkingMemoryEntitySegmentBasePrx wmSegment = WorkingMemoryEntitySegmentBasePrx::uncheckedCast(workingMemory->getSegment(itSeg->first));
205 EntityIdList ids = snapSegment->getAllEntityIds();
207 for (EntityIdList::const_iterator itEntity = ids.begin(); itEntity != ids.end(); ++itEntity)
210 wmSegment->addEntity(snapSegment->getEntityById(*itEntity));
213 ARMARX_INFO <<
"Loaded " << wmSegment->size() <<
" elements into segment " << itSeg->first;
215 WorkingMemoryListenerInterfacePrx l = wmSegment->getListenerProxy();
219 l->reportSnapshotLoaded(itSeg->first);
224 <<
" from snapshot: Corresponding WM segment not exists or has invalid type";
226 for (
auto& l : listeners)
228 l->reportSnapshotCompletelyLoaded();
237 std::string WorkingMemorySnapshot::getSegmentCollectionName(
const std::string& segName)
239 return name +
"_" + segName;
247 obj->setString(
"name", name);
251 for (SegmentMap::const_iterator it = segmentNames.begin(); it != segmentNames.end(); ++it)
253 objSegments->setString(it->first, it->second);
256 obj->setElement(
"segments", objSegments);
263 name = obj->getString(
"name");
266 const std::vector<std::string> segNames = objSegments->getElementNames();
267 segmentNames.clear();
269 for (std::vector<std::string>::const_iterator it = segNames.begin(); it != segNames.end(); ++it)
271 segmentNames[*it] = objSegments->getString(*it);