WorkingMemorySnapshotListSegment.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 2013
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
24 #include "WorkingMemorySnapshot.h"
25 
26 #include <Ice/ObjectAdapter.h>
27 
29 
32 //#include <MemoryX/updater/ObjectLocalization/MemoryXUpdaterObjectFactories.h>
33 //#include <ArmarXCore/observers/ObserverObjectFactories.h>
34 
35 namespace memoryx
36 {
37  const std::string SNAPSHOT_NAME_FIELD = "name";
38 
39  WorkingMemorySnapshotListSegment::WorkingMemorySnapshotListSegment(const DatabaseInterfacePrx& databasePrx, const CollectionInterfacePrx& collection,
41  databasePrx(databasePrx),
42  snapshotListCollection(collection),
43  ic(ic)
44  {
45  dbSerializer = new MongoSerializer(ic, true);
46  snapshotListCollection->ensureIndex(SNAPSHOT_NAME_FIELD, true);
47  }
48 
50  {
51  }
52 
53  WorkingMemorySnapshotInterfacePrx WorkingMemorySnapshotListSegment::createSnapshot(const std::string& name, const AbstractWorkingMemoryInterfacePrx& workingMemory,
54  const Ice::Current& c)
55  {
56  removeSnapshot(name);
57  WorkingMemorySnapshotPtr snapshot = new WorkingMemorySnapshot(name, ic, databasePrx);
58 
59  // store all WM segments
60  snapshot->saveWorkingMemory(workingMemory);
61 
62  const DBStorableData dbSnapshot = dbSerializer->serialize(snapshot);
63  snapshotListCollection->saveWithUserKey(dbSnapshot, SNAPSHOT_NAME_FIELD);
64  return createSnapshotProxy(snapshot, c);
65  }
66 
67  WorkingMemorySnapshotInterfacePrx WorkingMemorySnapshotListSegment::createSubsetSnapshot(const std::string& name, const AbstractWorkingMemoryInterfacePrx& workingMemory, const Ice::StringSeq& entityIdList, const Ice::Current& c)
68  {
69  removeSnapshot(name);
70  WorkingMemorySnapshotPtr snapshot = new WorkingMemorySnapshot(name, ic, databasePrx);
71 
72  // store all WM segments
73  snapshot->saveWorkingMemorySubset(workingMemory, entityIdList);
74 
75  const DBStorableData dbSnapshot = dbSerializer->serialize(snapshot);
76  snapshotListCollection->saveWithUserKey(dbSnapshot, SNAPSHOT_NAME_FIELD);
77  return createSnapshotProxy(snapshot, c);
78  }
79 
80  WorkingMemorySnapshotInterfacePrx WorkingMemorySnapshotListSegment::openSnapshot(const std::string& name, const Ice::Current& c)
81  {
82  WorkingMemorySnapshotPtr snapshot = findSnapshot(name);
83 
84  if (snapshot)
85  {
86  return createSnapshotProxy(snapshot, c);
87  }
88  else
89  throw SnapshotNotFoundException("Snapshot not found, check that corresponding record exists in " +
90  snapshotListCollection->getNS() + " collection", name);
91  }
92 
93  void WorkingMemorySnapshotListSegment::closeSnapshot(const WorkingMemorySnapshotInterfacePrx& snapshot, const Ice::Current&)
94  {
95  SnapshotMap::iterator itSnap = openedSnapshots.find(snapshot->ice_getIdentity());
96 
97  if (itSnap != openedSnapshots.end())
98  {
99  openedSnapshots.erase(itSnap);
100  }
101  }
102 
103  void WorkingMemorySnapshotListSegment::loadSnapshot(const std::string& name, const AbstractWorkingMemoryInterfacePrx& workingMemory,
104  const Ice::Current& c)
105  {
106  try
107  {
108  WorkingMemorySnapshotPtr snapshot = findSnapshot(name);
109 
110  if (snapshot)
111  {
112  snapshot->restoreWorkingMemory(workingMemory);
113  }
114  else
115  {
116  throw SnapshotNotFoundException("SnapshotNotFound", name);
117  }
118  }
119  catch (...)
120  {
122  }
123  }
124 
125 
126  bool WorkingMemorySnapshotListSegment::removeSnapshot(const std::string& name, const Ice::Current&)
127  {
128  WorkingMemorySnapshotPtr snapshot = findSnapshot(name);
129 
130  if (snapshot)
131  {
132  snapshot->clear();
133  snapshotListCollection->removeByFieldValue(SNAPSHOT_NAME_FIELD, name);
134  return true;
135  }
136  else
137  {
138  return false;
139  }
140  }
141 
142  NameList WorkingMemorySnapshotListSegment::getSnapshotNames(const ::Ice::Current&) const
143  {
144  return snapshotListCollection->findAllFieldValues(SNAPSHOT_NAME_FIELD);
145  }
146 
147  WorkingMemorySnapshotInterfacePrx WorkingMemorySnapshotListSegment::createSnapshotProxy(const WorkingMemorySnapshotPtr& snapshot, const Ice::Current& c)
148  {
149  Ice::Identity snapId = snapshot->getIceId();
150  openedSnapshots[snapId] = snapshot;
151  Ice::ObjectPrx node = c.adapter->add(snapshot, snapId);
152  return WorkingMemorySnapshotInterfacePrx::uncheckedCast(node);
153  }
154 
155  WorkingMemorySnapshotPtr WorkingMemorySnapshotListSegment::findSnapshot(const std::string& name)
156  {
157  DBStorableData dbSnapshot = snapshotListCollection->findOneByFieldValue(SNAPSHOT_NAME_FIELD, name);
158 
159  if (!dbSnapshot.JSON.empty())
160  {
161  WorkingMemorySnapshotPtr snapshot = new WorkingMemorySnapshot(name, ic, databasePrx);
162  dbSerializer->deserialize(dbSnapshot, snapshot);
163  return snapshot;
164  }
165  else
166  {
167  return WorkingMemorySnapshotPtr();
168  }
169  }
170 
172  {
173  Ice::Identity id;
174  id.name = (parentMemory ? parentMemory->getMemoryName() : "") + "_" + segmentName;
175  return id;
176  }
177 
179  {
180  return snapshotListCollection->count();
181  }
182 
183  void WorkingMemorySnapshotListSegment::clear(const ::Ice::Current&)
184  {
185  const NameList snapshotNames = getSnapshotNames();
186 
187  for (NameList::const_iterator it = snapshotNames.begin(); it != snapshotNames.end(); ++it)
188  {
189  removeSnapshot(*it);
190  }
191  }
192 
193  void WorkingMemorySnapshotListSegment::print(const ::Ice::Current&) const
194  {
195  // TODO implement
196  }
197 }
memoryx::WorkingMemorySnapshotListSegment::createSubsetSnapshot
WorkingMemorySnapshotInterfacePrx createSubsetSnapshot(const ::std::string &name, const AbstractWorkingMemoryInterfacePrx &workingMemory, const Ice::StringSeq &entityIdList, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshotListSegment.cpp:67
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
WorkingMemorySnapshot.h
memoryx::WorkingMemorySnapshotListSegment::clear
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshotListSegment.cpp:183
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::WorkingMemorySnapshotListSegment::print
void print(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: WorkingMemorySnapshotListSegment.cpp:193
memoryx::WorkingMemorySnapshotListSegment::WorkingMemorySnapshotListSegment
WorkingMemorySnapshotListSegment(const DatabaseInterfacePrx &databasePrx, const CollectionInterfacePrx &collection, Ice::CommunicatorPtr ic)
Definition: WorkingMemorySnapshotListSegment.cpp:39
memoryx::WorkingMemorySnapshotListSegment::createSnapshot
WorkingMemorySnapshotInterfacePrx createSnapshot(const ::std::string &name, const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshotListSegment.cpp:53
memoryx::WorkingMemorySnapshotListSegment::openSnapshot
WorkingMemorySnapshotInterfacePrx openSnapshot(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshotListSegment.cpp:80
IceInternal::Handle< ::Ice::Communicator >
WorkingMemorySnapshotListSegment.h
memoryx::WorkingMemorySnapshotListSegment::loadSnapshot
void loadSnapshot(const ::std::string &name, const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshotListSegment.cpp:103
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
memoryx::WorkingMemorySnapshot
The WorkingMemorySnapshot class handles snapshot IO methods.
Definition: WorkingMemorySnapshot.h:42
memoryx::WorkingMemorySnapshotListSegment::getIceId
Ice::Identity getIceId(const Ice::Current &) const override
Definition: WorkingMemorySnapshotListSegment.cpp:171
memoryx::WorkingMemorySnapshotListSegment::size
Ice::Int size(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: WorkingMemorySnapshotListSegment.cpp:178
memoryx::WorkingMemorySnapshotListSegment::~WorkingMemorySnapshotListSegment
~WorkingMemorySnapshotListSegment() override
Definition: WorkingMemorySnapshotListSegment.cpp:49
MemoryXCoreObjectFactories.h
memoryx::WorkingMemorySnapshotListSegment::getSnapshotNames
NameList getSnapshotNames(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: WorkingMemorySnapshotListSegment.cpp:142
memoryx::WorkingMemorySnapshotListSegment::removeSnapshot
bool removeSnapshot(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshotListSegment.cpp:126
memoryx::MongoSerializer
Definition: MongoSerializer.h:40
memoryx::WorkingMemorySnapshotListSegment::closeSnapshot
void closeSnapshot(const WorkingMemorySnapshotInterfacePrx &snapshot, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: WorkingMemorySnapshotListSegment.cpp:93
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
MemoryXTypesObjectFactories.h
memoryx::SNAPSHOT_NAME_FIELD
const std::string SNAPSHOT_NAME_FIELD
Definition: WorkingMemorySnapshotListSegment.cpp:37
memoryx::WorkingMemorySnapshotPtr
IceUtil::Handle< WorkingMemorySnapshot > WorkingMemorySnapshotPtr
Definition: WorkingMemorySnapshotListSegment.h:36
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
Exception.h