WorkingMemorySnapshot.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package MemoryX::LongtermMemory
17 * @author Alexey Kozlov ( kozlov at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "WorkingMemorySnapshot.h"
24 
27 
29 
30 #include <Ice/ObjectAdapter.h>
31 #include <IceUtil/UUID.h>
33 #include <MemoryX/interface/workingmemory/WorkingMemoryListenerInterface.h>
34 
35 namespace memoryx
36 {
37  WorkingMemorySnapshot::WorkingMemorySnapshot(const std::string& name, Ice::CommunicatorPtr ic, const DatabaseInterfacePrx& databasePrx) :
38  name(name), ic(ic),
39  databasePrx(databasePrx)
40  {
41  iceId.name = IceUtil::generateUUID();
42  }
43 
44  PersistentEntitySegmentBasePrx WorkingMemorySnapshot::addSegment(const std::string& segName, const ::Ice::Current& c)
45  {
46  addSegmentToMap(segName);
47  return getSegment(segName, c);
48  }
49 
50  PersistentEntitySegmentPtr WorkingMemorySnapshot::addSegmentPtr(const std::string& segName)
51  {
52  addSegmentToMap(segName);
53  return getSegmentPtr(segName);
54  }
55 
56  void WorkingMemorySnapshot::addSegmentToMap(const std::string& segName)
57  {
58  segmentNames[segName] = getSegmentCollectionName(segName);
59  }
60 
61  PersistentEntitySegmentBasePrx WorkingMemorySnapshot::getSegment(const ::std::string& segName, const ::Ice::Current& c)
62  {
63  PersistentEntitySegmentPtr segment = getSegmentPtr(segName);
64 
65  if (segment)
66  {
67  Ice::Identity segId = segment->getIceId();
68  segId.name += "_" + IceUtil::generateUUID();
69 
70  openedSegments[segId] = segment;
71  ARMARX_CHECK_EXPRESSION(c.adapter);
72  Ice::ObjectPrx node = c.adapter->add(segment, segId);
73  return PersistentEntitySegmentBasePrx::uncheckedCast(node);
74  }
75  else
76  {
77  return PersistentEntitySegmentBasePrx();
78  }
79  }
80 
81  PersistentEntitySegmentPtr WorkingMemorySnapshot::getSegmentPtr(const ::std::string& segName)
82  {
83  SegmentMap::const_iterator itSeg = segmentNames.find(segName);
84 
85  if (itSeg != segmentNames.end())
86  {
87  ARMARX_CHECK_EXPRESSION(databasePrx);
88  CollectionInterfacePrx segmentColl = databasePrx->requestCollection(itSeg->second);
89 
90  if (segmentColl)
91  {
92  PersistentEntitySegmentBasePtr segment = new PersistentEntitySegment(segmentColl, ic, false);
93  segment->setSegmentName(segName);
94  return PersistentEntitySegmentPtr::dynamicCast(segment);
95  }
96  else
97  {
99  }
100  }
101  else
102  {
104  }
105  }
106 
107 
108  bool WorkingMemorySnapshot::hasSegment(const ::std::string& segName, const ::Ice::Current&)
109  {
110  return segmentNames.count(segName);
111  }
112 
113  NameList WorkingMemorySnapshot::getSegmentNames(const ::Ice::Current&)
114  {
115  NameList result;
116 
117  for (SegmentMap::const_iterator it = segmentNames.begin(); it != segmentNames.end(); ++it)
118  {
119  result.push_back(it->first);
120  }
121 
122  return result;
123  }
124 
125  void WorkingMemorySnapshot::clear(const ::Ice::Current&)
126  {
127  for (SegmentMap::const_iterator it = segmentNames.begin(); it != segmentNames.end(); ++it)
128  {
129  databasePrx->dropCollection(it->second);
130  }
131 
132  segmentNames.clear();
133  }
134 
136  {
137  return segmentNames;
138  }
139 
140  void WorkingMemorySnapshot::saveWorkingMemory(const AbstractWorkingMemoryInterfacePrx& workingMemory, const ::Ice::Current&)
141  {
142  NameList wmSegNames = workingMemory->getSegmentNames();
143 
144  for (NameList::const_iterator itSeg = wmSegNames.begin(); itSeg != wmSegNames.end(); ++itSeg)
145  {
146  WorkingMemoryEntitySegmentBasePrx wmSegment = WorkingMemoryEntitySegmentBasePrx::uncheckedCast(workingMemory->getSegment(*itSeg));
147 
148  if (wmSegment)
149  {
150  PersistentEntitySegmentBasePtr snapSegment = addSegmentPtr(*itSeg);
151  EntityIdList ids = wmSegment->getAllEntityIds();
152 
153  for (EntityIdList::const_iterator itEntity = ids.begin(); itEntity != ids.end(); ++itEntity)
154  {
155  snapSegment->addEntity(wmSegment->getEntityById(*itEntity));
156  }
157  }
158  }
159  }
160 
161  void WorkingMemorySnapshot::saveWorkingMemorySubset(const AbstractWorkingMemoryInterfacePrx& workingMemory, const Ice::StringSeq& entityIdList, const Ice::Current&)
162  {
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());
168 
169  for (const auto& segName : wmSegNames)
170  {
171  WorkingMemoryEntitySegmentBasePrx wmSegment = WorkingMemoryEntitySegmentBasePrx::uncheckedCast(workingMemory->getSegment(segName));
172 
173  if (wmSegment)
174  {
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());
185 
186  for (EntityIdList::const_iterator itEntity = intersectionList.begin(); itEntity != intersectionList.end(); ++itEntity)
187  {
188  snapSegment->addEntity(wmSegment->getEntityById(*itEntity));
189  }
190  }
191  }
192  }
193 
194  void WorkingMemorySnapshot::restoreWorkingMemory(const AbstractWorkingMemoryInterfacePrx& workingMemory, const ::Ice::Current&)
195  {
196  std::set<WorkingMemoryListenerInterfacePrx> listeners;
197  for (SegmentMap::const_iterator itSeg = segmentNames.begin(); itSeg != segmentNames.end(); ++itSeg)
198  {
199  PersistentEntitySegmentBasePtr snapSegment = getSegmentPtr(itSeg->first);
200  WorkingMemoryEntitySegmentBasePrx wmSegment = WorkingMemoryEntitySegmentBasePrx::uncheckedCast(workingMemory->getSegment(itSeg->first));
201 
202  if (wmSegment)
203  {
204  // wmSegment->clear();
205  EntityIdList ids = snapSegment->getAllEntityIds();
206 
207  for (EntityIdList::const_iterator itEntity = ids.begin(); itEntity != ids.end(); ++itEntity)
208  {
209  // std::cout << "Restoring entity, id = " << *itEntity << std::endl;
210  wmSegment->addEntity(snapSegment->getEntityById(*itEntity));
211  }
212 
213  ARMARX_INFO << "Loaded " << wmSegment->size() << " elements into segment " << itSeg->first;
214 
215  WorkingMemoryListenerInterfacePrx l = wmSegment->getListenerProxy();
216  if (l)
217  {
218  listeners.insert(l);
219  l->reportSnapshotLoaded(itSeg->first);
220  }
221  }
222  else
223  ARMARX_WARNING << "Unable to load segment " << itSeg->first
224  << " from snapshot: Corresponding WM segment not exists or has invalid type";
225  }
226  for (auto& l : listeners)
227  {
228  l->reportSnapshotCompletelyLoaded();
229  }
230  }
231 
233  {
234  return iceId;
235  }
236 
237  std::string WorkingMemorySnapshot::getSegmentCollectionName(const std::string& segName)
238  {
239  return name + "_" + segName;
240  }
241 
242  // serialization
243  void WorkingMemorySnapshot::serialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& c) const
244  {
245  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
246 
247  obj->setString("name", name);
248 
249  armarx::AbstractObjectSerializerPtr objSegments = obj->createElement();
250 
251  for (SegmentMap::const_iterator it = segmentNames.begin(); it != segmentNames.end(); ++it)
252  {
253  objSegments->setString(it->first, it->second);
254  }
255 
256  obj->setElement("segments", objSegments);
257  }
258 
259  void WorkingMemorySnapshot::deserialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
260  {
261  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
262 
263  name = obj->getString("name");
264 
265  const armarx::AbstractObjectSerializerPtr objSegments = obj->getElement("segments");
266  const std::vector<std::string> segNames = objSegments->getElementNames();
267  segmentNames.clear();
268 
269  for (std::vector<std::string>::const_iterator it = segNames.begin(); it != segNames.end(); ++it)
270  {
271  segmentNames[*it] = objSegments->getString(*it);
272  }
273  }
274 }
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
memoryx::WorkingMemorySnapshot::hasSegment
bool hasSegment(const ::std::string &segName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshot.cpp:108
WorkingMemorySnapshot.h
memoryx::WorkingMemorySnapshot::getSegmentNames
NameList getSegmentNames(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshot.cpp:113
AbstractObjectSerializer.h
PersistentEntitySegment.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::WorkingMemorySnapshot::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshot.cpp:259
memoryx::WorkingMemorySnapshot::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: WorkingMemorySnapshot.cpp:243
IceInternal::Handle< ::Ice::Communicator >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
memoryx::WorkingMemorySnapshot::WorkingMemorySnapshot
WorkingMemorySnapshot(const std::string &name, Ice::CommunicatorPtr ic, const DatabaseInterfacePrx &databasePrx)
Definition: WorkingMemorySnapshot.cpp:37
memoryx::WorkingMemorySnapshot::saveWorkingMemorySubset
void saveWorkingMemorySubset(const AbstractWorkingMemoryInterfacePrx &workingMemory, const Ice::StringSeq &entityIdList, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshot.cpp:161
memoryx::SegmentMap
std::map< std::string, std::string > SegmentMap
Definition: WorkingMemorySnapshot.h:34
memoryx::WorkingMemorySnapshot::getSegmentAndCollectionNames
SegmentMap getSegmentAndCollectionNames() const
Definition: WorkingMemorySnapshot.cpp:135
memoryx::WorkingMemorySnapshot::restoreWorkingMemory
void restoreWorkingMemory(const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
Restore all segments with all entities of this snapshot to the WorkingMemory.
Definition: WorkingMemorySnapshot.cpp:194
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle
Definition: forward_declarations.h:29
memoryx::WorkingMemorySnapshot::getIceId
Ice::Identity getIceId() const
Definition: WorkingMemorySnapshot.cpp:232
memoryx::WorkingMemorySnapshot::getSegment
PersistentEntitySegmentBasePrx getSegment(const ::std::string &segName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshot.cpp:61
memoryx::PersistentEntitySegment
The PersistentEntitySegment class is the base class for all memory segments containing memoryx::Entit...
Definition: PersistentEntitySegment.h:107
memoryx::WorkingMemorySnapshot::addSegment
PersistentEntitySegmentBasePrx addSegment(const ::std::string &segName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshot.cpp:44
Logging.h
memoryx::PersistentEntitySegmentPtr
IceUtil::Handle< PersistentEntitySegment > PersistentEntitySegmentPtr
Definition: PersistentEntitySegment.h:239
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
memoryx::WorkingMemorySnapshot::clear
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshot.cpp:125
memoryx::WorkingMemorySnapshot::saveWorkingMemory
void saveWorkingMemory(const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
Save all segments with all entities of the WorkingMemory to this snapshot.
Definition: WorkingMemorySnapshot.cpp:140