LongtermMemory.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 // core
24 #include "LongtermMemory.h"
25 
26 #include "../../libraries/longtermmemory/WorkingMemorySnapshotListSegment.h"
27 #include "../../libraries/memorytypes/segment/PersistentObjectInstanceSegment.h"
28 #include "../../libraries/memorytypes/segment/KBMSegment.h"
29 #include "../../libraries/memorytypes/segment/OacMemorySegment.h"
30 #include "../../libraries/memorytypes/segment/PersistentProfilerDataSegment.h"
31 #include "../../libraries/memorytypes/segment/PersistentResourceProfileSegment.h"
32 #include "../../libraries/memorytypes/segment/PersistentPredictionDataSegment.h"
33 #include "../../libraries/memorytypes/segment/PersistentObjectClassSegment.h"
34 #include "../../libraries/memorytypes/segment/PersistentDMPDataSegment.h"
35 
36 #include <MemoryX/interface/core/EntityBase.h>
37 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
38 
40 
41 namespace memoryx
42 {
43  std::string LongtermMemory::getDefaultName() const
44  {
45  return "LongtermMemory";
46  }
47 
49  {
50  usingProxy("PriorKnowledge");
51 
52  const std::string clsCollNamesStr = getProperty<std::string>("ClassCollections").getValue();
53  classCollNames = armarx::Split(clsCollNamesStr, ",");
54  }
55 
56 
58  {
59  ARMARX_INFO << "connecting long term memory";
60  priorKnowledgePrx = getProxy<PriorKnowledgeInterfacePrx>("PriorKnowledge");
61  const std::string snaplistCollectionName = getProperty<std::string>("SnapshotListCollection").getValue();
62  const std::string oacCollectionName = getProperty<std::string>("OacCollection").getValue();
63  const std::string kbmCollectionName = getProperty<std::string>("KbmCollection").getValue();
64  const std::string dmpCollectionName = getProperty<std::string>("DmpCollection").getValue();
65  const std::string profilerCollectionName = getProperty<std::string>("ProfilerDataCollection").getValue();
66  const std::string resourceProfileCollectionName = getProperty<std::string>("ResourceProfileCollection").getValue();
67  const std::string predictionDataCollectionName = getProperty<std::string>("PredictionDataCollection").getValue();
68  const std::string selfLocalisationCollectionName = getProperty<std::string>("SelfLocalisationCollection").getValue();
69 
70  databaseInterfacePrx = storagePrx->requestDatabase(dbName);
71 
72  CollectionInterfacePrx snapshotListCollection = databaseInterfacePrx->requestCollection(snaplistCollectionName);
73  CollectionInterfacePrx oacCollection = databaseInterfacePrx->requestCollection(oacCollectionName);
74  CollectionInterfacePrx kbmCollection = databaseInterfacePrx->requestCollection(kbmCollectionName);
75  CollectionInterfacePrx dmpCollection = databaseInterfacePrx->requestCollection(dmpCollectionName);
76 
77  WorkingMemorySnapshotListSegmentPtr snapshotListSegment = new WorkingMemorySnapshotListSegment(databaseInterfacePrx, snapshotListCollection, ic);
78  addSegment(LTM::SegmentNames::SNAPSHOTS, snapshotListSegment);
79 
80  OacMemorySegmentPtr oacSegment = new OacMemorySegment(priorKnowledgePrx->getObjectClassesSegment(), oacCollection, ic);
81  addSegment(LTM::SegmentNames::OACS, oacSegment);
82 
83  PersistentEntitySegmentBasePtr kbmSegment = new KBMSegment(kbmCollection, ic);
84  addSegment(LTM::SegmentNames::KBM, kbmSegment);
85 
86  CollectionInterfacePrx profilerCollection = databaseInterfacePrx->requestCollection(profilerCollectionName);
87  addSegment(LTM::SegmentNames::PROFILER, new PersistentProfilerDataSegment(profilerCollection, ic));
88 
89  CollectionInterfacePrx resourceProfileCollection = databaseInterfacePrx->requestCollection(resourceProfileCollectionName);
90  addSegment(LTM::SegmentNames::RESOURCE_PROFILES, new PersistentResourceProfileSegment(resourceProfileCollection, ic));
91 
92  CollectionInterfacePrx predictionDataCollection = databaseInterfacePrx->requestCollection(predictionDataCollectionName);
93  addSegment(LTM::SegmentNames::PREDICTION_DATA, new PersistentPredictionDataSegment(predictionDataCollection, ic));
94 
95  CollectionInterfacePrx classColl = storagePrx->requestCollection(classCollNames[0]);
96  PersistentObjectClassSegmentPtr classesSegment = new PersistentObjectClassSegment(classColl, ic);
97  addSegment(LTM::SegmentNames::OBJECTCLASSES, classesSegment);
98 
99  PersistentDMPDataSegmentPtr dmpSegment = new PersistentDMPDataSegment(dmpCollection, ic);
100  addSegment(LTM::SegmentNames::DMP, dmpSegment);
101 
102  CollectionInterfacePrx selfLocalisationCollection = databaseInterfacePrx->requestCollection(selfLocalisationCollectionName);
103  PersistentEntitySegmentPtr selfLocalisationSegment = new PersistentEntitySegment(selfLocalisationCollection, ic);
104  addSegment(LTM::SegmentNames::SELF_LOCALISATION, selfLocalisationSegment);
105 
106  ARMARX_INFO << "successfully connected long term memory";
107  }
108 
109 
110  WorkingMemorySnapshotListSegmentBasePrx LongtermMemory::getWorkingMemorySnapshotListSegment(const ::Ice::Current& c)
111  {
112  return WorkingMemorySnapshotListSegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::SNAPSHOTS, c));
113  }
114 
115 
116  PersistentObjectInstanceSegmentBasePrx LongtermMemory::getCustomInstancesSegment(const std::string& segmentName, bool createIfMissing, const ::Ice::Current& c)
117  {
118  if (hasSegment(segmentName))
119  {
120  return PersistentObjectInstanceSegmentBasePrx::uncheckedCast(getSegment(segmentName, c));
121  }
122  else if (createIfMissing)
123  {
124  PersistentObjectInstanceSegmentPtr customSegment = new PersistentObjectInstanceSegment(databaseInterfacePrx->requestCollection(segmentName), ic);
125  return PersistentObjectInstanceSegmentBasePrx::uncheckedCast(addSegment(segmentName, customSegment, c));
126  }
127  else
128  {
129  return PersistentObjectInstanceSegmentBasePrx();
130  }
131  }
132 
133 
134  void LongtermMemory::loadWorkingMemorySnapshot(const std::string& snapshotName, const AbstractWorkingMemoryInterfacePrx& workingMemory, const ::Ice::Current&)
135  {
136  WorkingMemorySnapshotListSegmentBasePrx snapshotListSegment = getWorkingMemorySnapshotListSegment();
137 
138  if (snapshotListSegment)
139  {
140  snapshotListSegment->loadSnapshot(snapshotName, workingMemory);
141  }
142  }
143 
144 
145  bool LongtermMemory::saveWorkingMemorySnapshot(const std::string& snapshotName, const AbstractWorkingMemoryInterfacePrx& workingMemory, const ::Ice::Current&)
146  {
147  WorkingMemorySnapshotListSegmentBasePrx snapshotListSegment = getWorkingMemorySnapshotListSegment();
148 
149  if (snapshotListSegment)
150  {
151  return snapshotListSegment->createSnapshot(snapshotName, workingMemory);
152  }
153  else
154  {
155  return false;
156  }
157  }
158 
159 
160  WorkingMemorySnapshotInterfacePrx LongtermMemory::openWorkingMemorySnapshot(const std::string& snapshotName, const ::Ice::Current&)
161  {
162  WorkingMemorySnapshotListSegmentBasePrx snapshotListSegment = getWorkingMemorySnapshotListSegment();
163 
164  if (snapshotListSegment)
165  {
166  return snapshotListSegment->openSnapshot(snapshotName);
167  }
168  else
169  {
170  return WorkingMemorySnapshotInterfacePrx();
171  }
172  }
173 
174 
175  bool LongtermMemory::removeWorkingMemorySnapshot(const std::string& snapshotName, const ::Ice::Current&)
176  {
177  WorkingMemorySnapshotListSegmentBasePrx snapshotListSegment = getWorkingMemorySnapshotListSegment();
178 
179  if (snapshotListSegment)
180  {
181  return snapshotListSegment->removeSnapshot(snapshotName);
182  }
183  else
184  {
185  return false;
186  }
187  }
188 
189 
190  NameList LongtermMemory::getSnapshotNames(const ::Ice::Current& c)
191  {
192  WorkingMemorySnapshotListSegmentBasePrx snapshotListSegment = getWorkingMemorySnapshotListSegment();
193 
194  if (snapshotListSegment)
195  {
196  return snapshotListSegment->getSnapshotNames();
197  }
198  else
199  {
200  throw SnapshotNotFoundException("Snapshot segment not found!", "");
201  }
202  }
203 
204 
205  std::string LongtermMemory::getMemoryName(const Ice::Current&) const
206  {
207  return getName();
208  }
209 
210 
211  PersistentProfilerDataSegmentBasePrx LongtermMemory::getProfilerDataSegment(const Ice::Current& c)
212  {
213  return PersistentProfilerDataSegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::PROFILER, c));
214  }
215 
216  PersistentPredictionDataSegmentBasePrx LongtermMemory::getPredictionDataSegment(const Ice::Current& c)
217  {
218  return PersistentPredictionDataSegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::PREDICTION_DATA, c));
219  }
220 
221 
222  PersistentResourceProfileSegmentBasePrx LongtermMemory::getResourceProfileSegment(const Ice::Current& c)
223  {
224  return PersistentResourceProfileSegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::RESOURCE_PROFILES, c));
225  }
226 
227 
228  OacMemorySegmentBasePrx LongtermMemory::getOacSegment(const ::Ice::Current& c)
229  {
230  return OacMemorySegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::OACS, c));
231  }
232 
233 
234  KBMSegmentBasePrx LongtermMemory::getKBMSegment(const Ice::Current& c)
235  {
236  return KBMSegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::KBM, c));
237  }
238 
239 
240  CommonStorageInterfacePrx LongtermMemory::getCommonStorage(const ::Ice::Current&) const
241  {
242  return storagePrx;
243  }
244 
245 
246  PersistentObjectClassSegmentBasePrx LongtermMemory::getObjectClassesSegment(const ::Ice::Current& c) const
247  {
248  return PersistentObjectClassSegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::OBJECTCLASSES, c));
249  }
250 
251 
252  PersistentDMPDataSegmentBasePrx LongtermMemory::getDMPSegment(const Ice::Current& c)
253  {
254  return PersistentDMPDataSegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::DMP, c));
255  }
256 
257 
258  PersistentEntitySegmentBasePrx memoryx::LongtermMemory::getSelfLocalisationSegment(const Ice::Current& c)
259  {
260  return PersistentEntitySegmentBasePrx::uncheckedCast(getSegment(LTM::SegmentNames::SELF_LOCALISATION, c));
261  }
262 
263 
264  AbstractMemorySegmentPrx memoryx::LongtermMemory::addGenericSegment(const std::string& segmentName, const Ice::Current&)
265  {
266  PersistentEntitySegmentPtr segment = new PersistentEntitySegment(databaseInterfacePrx->requestCollection(segmentName), getIceManager()->getCommunicator());
267  return addSegment(segmentName, segment);
268  }
269 }
memoryx::KBMSegment
Definition: KBMSegment.h:33
memoryx::PersistentProfilerDataSegment
Definition: PersistentProfilerDataSegment.h:36
memoryx::SegmentedMemory::addSegment
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: SegmentedMemory.cpp:40
memoryx::OacMemorySegment
Definition: OacMemorySegment.h:40
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
memoryx::AbstractLongtermMemory::storagePrx
CommonStorageInterfacePrx storagePrx
Definition: AbstractLongtermMemory.h:79
memoryx::AbstractLongtermMemory::dbName
std::string dbName
Definition: AbstractLongtermMemory.h:83
memoryx::LongtermMemory::getSnapshotNames
NameList getSnapshotNames(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:190
memoryx::LongtermMemory::removeWorkingMemorySnapshot
bool removeWorkingMemorySnapshot(const std::string &snapshotName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:175
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::PersistentObjectClassSegment
The persistent object class segment is a specialized segment of the SegmentedMemory.
Definition: PersistentObjectClassSegment.h:43
memoryx::LongtermMemory::getKBMSegment
KBMSegmentBasePrx getKBMSegment(const Ice::Current &) override
Definition: LongtermMemory.cpp:234
LongtermMemory.h
memoryx::LongtermMemory::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: LongtermMemory.cpp:43
memoryx::LongtermMemory::onConnectLongtermMemory
void onConnectLongtermMemory() override
Definition: LongtermMemory.cpp:57
memoryx::LongtermMemory::getOacSegment
OacMemorySegmentBasePrx getOacSegment(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:228
StringHelpers.h
memoryx::SegmentedMemory::hasSegment
bool hasSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: SegmentedMemory.cpp:69
IceInternal::Handle
Definition: forward_declarations.h:8
memoryx::WorkingMemorySnapshotListSegment
Definition: WorkingMemorySnapshotListSegment.h:38
memoryx::LongtermMemory::getProfilerDataSegment
PersistentProfilerDataSegmentBasePrx getProfilerDataSegment(const Ice::Current &c=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:211
memoryx::PersistentPredictionDataSegment
Definition: PersistentPredictionDataSegment.h:34
memoryx::LongtermMemory::getObjectClassesSegment
PersistentObjectClassSegmentBasePrx getObjectClassesSegment(const ::Ice::Current &c=Ice::emptyCurrent) const override
Definition: LongtermMemory.cpp:246
memoryx::LongtermMemory::getCommonStorage
CommonStorageInterfacePrx getCommonStorage(const ::Ice::Current &c=Ice::emptyCurrent) const override
Definition: LongtermMemory.cpp:240
memoryx::LongtermMemory::getResourceProfileSegment
PersistentResourceProfileSegmentBasePrx getResourceProfileSegment(const Ice::Current &c=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:222
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
memoryx::LongtermMemory::getPredictionDataSegment
PersistentPredictionDataSegmentBasePrx getPredictionDataSegment(const Ice::Current &c=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:216
IceUtil::Handle
Definition: forward_declarations.h:29
memoryx::LongtermMemory::getDMPSegment
PersistentDMPDataSegmentBasePrx getDMPSegment(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:252
memoryx::PersistentEntitySegment
The PersistentEntitySegment class is the base class for all memory segments containing memoryx::Entit...
Definition: PersistentEntitySegment.h:107
memoryx::LongtermMemory::getCustomInstancesSegment
PersistentObjectInstanceSegmentBasePrx getCustomInstancesSegment(const std::string &segmentName, bool createIfMissing, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:116
memoryx::AbstractLongtermMemory::ic
Ice::CommunicatorPtr ic
Definition: AbstractLongtermMemory.h:80
memoryx::LongtermMemory::openWorkingMemorySnapshot
WorkingMemorySnapshotInterfacePrx openWorkingMemorySnapshot(const std::string &snapshotName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:160
memoryx::PersistentObjectInstanceSegment
Definition: PersistentObjectInstanceSegment.h:39
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
memoryx::SegmentedMemory::getSegment
AbstractMemorySegmentPrx getSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: SegmentedMemory.cpp:75
memoryx::LongtermMemory::getSelfLocalisationSegment
PersistentEntitySegmentBasePrx getSelfLocalisationSegment(const Ice::Current &c=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:258
memoryx::LongtermMemory::getWorkingMemorySnapshotListSegment
WorkingMemorySnapshotListSegmentBasePrx getWorkingMemorySnapshotListSegment(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:110
memoryx::LongtermMemory::getMemoryName
std::string getMemoryName(const Ice::Current &=Ice::emptyCurrent) const override
Definition: LongtermMemory.cpp:205
memoryx::LongtermMemory::saveWorkingMemorySnapshot
bool saveWorkingMemorySnapshot(const std::string &snapshotName, const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:145
memoryx::LongtermMemory::loadWorkingMemorySnapshot
void loadWorkingMemorySnapshot(const std::string &snapshotName, const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: LongtermMemory.cpp:134
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
memoryx::LongtermMemory::onInitLongtermMemory
void onInitLongtermMemory() override
Definition: LongtermMemory.cpp:48
memoryx::LongtermMemory::addGenericSegment
AbstractMemorySegmentPrx addGenericSegment(const std::string &segmentName, const Ice::Current &) override
Definition: LongtermMemory.cpp:264
memoryx::PersistentResourceProfileSegment
Definition: PersistentResourceProfileSegment.h:36
memoryx::PersistentDMPDataSegment
Definition: PersistentDMPDataSegment.h:35