AbstractWorkingMemory.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::CommonStorage
17 * @author Alexey Kozlov ( kozlov at kit dot edu), Kai Welke (welke at kit dot edu)
18 * @date
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "AbstractWorkingMemory.h"
24 
25 #include <IceUtil/UUID.h>
26 
28 #include <ArmarXCore/interface/core/Log.h>
29 
32 
33 
34 #define COMMONSTORAGE_NAME "CommonStorage"
35 
36 namespace memoryx
37 {
38  void
40  {
41  usePriorMemory = getProperty<bool>("UsePriorMemory").getValue();
42  priorMemoryName = getProperty<std::string>("PriorMemoryName").getValue();
43 
44  if (usePriorMemory)
45  {
47  }
48 
49  useLongtermMemory = getProperty<bool>("UseLongtermMemory").getValue();
50  longtermMemoryName = getProperty<std::string>("LongtermMemoryName").getValue();
51 
53  {
55  }
56 
58 
59  if (useCommonStorage)
60  {
61  usingProxy("CommonStorage");
62  }
63 
64  publishUpdates = getProperty<bool>("PublishUpdates").getValue();
65  updatesTopicName = getProperty<std::string>("UpdatesTopicName").getValue();
66 
67  if (publishUpdates)
68  {
70  }
71 
72  // call subclass hook
74  }
75 
76  void
78  {
79  ARMARX_INFO << "Starting MemoryX::AbstractWorkingMemory";
80 
81  const Ice::CommunicatorPtr ic = getIceManager()->getCommunicator();
82 
83 
85 
86  if (usePriorMemory)
87  {
88  priorKnowledgePrx = getProxy<PriorKnowledgeInterfacePrx>(priorMemoryName);
89  }
90 
92  {
93  longtermMemoryPrx = getProxy<LongtermMemoryInterfacePrx>(longtermMemoryName);
94  }
95 
96  if (useCommonStorage)
97  {
98  dataBasePrx = getProxy<CommonStorageInterfacePrx>(COMMONSTORAGE_NAME);
99  }
100 
101  if (publishUpdates)
102  {
103  listenerPrx = getTopic<WorkingMemoryListenerInterfacePrx>(updatesTopicName);
104  }
105 
106  // call subclass hook
108 
109  if (longtermMemoryPrx && !getProperty<std::string>("SnapshotToLoad").getValue().empty())
110  {
111  longtermMemoryPrx->loadWorkingMemorySnapshot(
112  getProperty<std::string>("SnapshotToLoad").getValue(),
113  AbstractWorkingMemoryInterfacePrx::uncheckedCast(getProxy()));
114  }
115  }
116 
117  AbstractMemorySegmentPrx
118  AbstractWorkingMemory::addSegment(const std::string& segmentName,
119  const AbstractMemorySegmentPtr& segment,
120  const ::Ice::Current& c)
121  {
122  AbstractWorkingMemorySegmentPtr wmSegment =
123  AbstractWorkingMemorySegmentPtr::dynamicCast(segment);
124 
125  if (wmSegment && listenerPrx)
126  {
127  // set observer proxy
128  wmSegment->setListenerProxy(listenerPrx);
129  }
130 
131  AbstractMemorySegmentPrx segmentProxy = SegmentedMemory::addSegment(segmentName, segment);
132 
133  ARMARX_INFO << "Added AbstractWorkingMemorySegment: " << segmentName;
134 
135  return segmentProxy;
136  }
137 
138  WorkingMemoryUpdaterBasePrx
139  AbstractWorkingMemory::registerUpdater(const std::string& updaterName,
140  const WorkingMemoryUpdaterBasePtr& updater,
141  const ::Ice::Current&)
142  {
143  // init updater
144  WorkingMemoryUpdaterPtr updaterImpl = WorkingMemoryUpdaterPtr::dynamicCast(updater);
145  updaterImpl->setWorkingMemory(this);
146  getArmarXManager()->addObject(updaterImpl, false);
147 
148  // insert into known updaters
149  std::pair<std::string, MemoryUpdaterEntry> updaterEntry;
150  updaterEntry.first = updaterName;
151  updaterEntry.second.proxy =
152  WorkingMemoryUpdaterBasePrx::uncheckedCast(updaterImpl->getProxy());
153  updaterEntry.second.pointer = updaterImpl;
154 
155  std::unique_lock lock(updaterMutex);
156  updaters.insert(updaterEntry);
157 
158  ARMARX_INFO << "Registered AbstractWorkingMemoryUpdater: " << updaterName;
159 
160  return updaterEntry.second.proxy;
161  }
162 
163  WorkingMemoryUpdaterBasePrx
164  AbstractWorkingMemory::getUpdater(const std::string& updaterName, const ::Ice::Current&)
165  {
166  std::unique_lock lock(updaterMutex);
167 
168  // find segment
169  MemoryUpdaterMap::iterator iter = updaters.find(updaterName);
170 
171  if (iter == updaters.end())
172  {
173  throw InvalidEntityException();
174  }
175 
176  return iter->second.proxy;
177  }
178 
179  void
180  AbstractWorkingMemory::unregisterUpdater(const std::string& updaterName, const ::Ice::Current&)
181  {
182  std::unique_lock lock(updaterMutex);
183 
184  MemoryUpdaterMap::iterator iter = updaters.find(updaterName);
185 
186  if (iter != updaters.end())
187  {
188  WorkingMemoryUpdaterPtr updater =
189  WorkingMemoryUpdaterPtr::dynamicCast(iter->second.pointer);
190 
191  // remove from manager
192  getArmarXManager()->removeObjectBlocking(updater);
193 
194  // remove from known updaters
195  updaters.erase(iter);
196 
197  ARMARX_INFO << "Unregistered AbstractWorkingMemoryUpdater: " << updaterName;
198  }
199  }
200 
201  void
202  AbstractWorkingMemory::clear(const ::Ice::Current&)
203  {
204  std::unique_lock lock(segmentsMutex);
205 
206  for (MemorySegmentMap::iterator it = segments.begin(); it != segments.end(); ++it)
207  {
208  it->second.pointer->clear();
209  }
210  }
211 
212  void
213  AbstractWorkingMemory::print(const ::Ice::Current&) const
214  {
215  std::unique_lock lock(segmentsMutex);
216 
217  std::cout << "Memory contains " << segments.size() << " segments" << std::endl;
218 
219  for (MemorySegmentMap::const_iterator it = segments.begin(); it != segments.end(); ++it)
220  {
221  std::cout << "Memory segment " << it->first << " contains "
222  << it->second.pointer->size() << " entities" << std::endl;
223  it->second.pointer->print();
224  }
225  }
226 } // namespace memoryx
memoryx::SegmentedMemory::segments
MemorySegmentMap segments
Definition: SegmentedMemory.h:77
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:366
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
memoryx::AbstractWorkingMemory::useCommonStorage
bool useCommonStorage
Definition: AbstractWorkingMemory.h:167
memoryx::AbstractWorkingMemory::registerUpdater
WorkingMemoryUpdaterBasePrx registerUpdater(const std::string &updaterName, const WorkingMemoryUpdaterBasePtr &updater, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:139
ArmarXManager.h
memoryx::AbstractWorkingMemory::priorMemoryName
std::string priorMemoryName
Definition: AbstractWorkingMemory.h:170
memoryx::AbstractWorkingMemory::print
void print(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AbstractWorkingMemory.cpp:213
memoryx::SegmentedMemory::addSegment
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: SegmentedMemory.cpp:41
memoryx::AbstractWorkingMemory::dataBasePrx
CommonStorageInterfacePrx dataBasePrx
Definition: AbstractWorkingMemory.h:156
memoryx::SegmentedMemory::segmentsMutex
std::shared_mutex segmentsMutex
Definition: SegmentedMemory.h:78
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:360
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
memoryx::AbstractWorkingMemory::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: AbstractWorkingMemory.cpp:77
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:234
IceInternal::Handle<::Ice::Communicator >
memoryx::AbstractWorkingMemory::onInitWorkingMemory
virtual void onInitWorkingMemory()=0
memoryx::AbstractWorkingMemory::onConnectWorkingMemory
virtual void onConnectWorkingMemory()=0
memoryx::AbstractWorkingMemory::priorKnowledgePrx
PriorKnowledgeInterfacePrx priorKnowledgePrx
Definition: AbstractWorkingMemory.h:157
memoryx::AbstractWorkingMemory::longtermMemoryPrx
LongtermMemoryInterfacePrx longtermMemoryPrx
Definition: AbstractWorkingMemory.h:158
memoryx::AbstractWorkingMemory::longtermMemoryName
std::string longtermMemoryName
Definition: AbstractWorkingMemory.h:171
WorkingMemoryUpdater.h
memoryx::AbstractWorkingMemory::getUpdater
WorkingMemoryUpdaterBasePrx getUpdater(const std::string &updaterName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:164
COMMONSTORAGE_NAME
#define COMMONSTORAGE_NAME
Definition: AbstractWorkingMemory.cpp:34
WorkingMemoryEntitySegment.h
memoryx::AbstractWorkingMemory::clear
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:202
memoryx::MongoSerializer
Definition: MongoSerializer.h:40
memoryx::AbstractWorkingMemory::publishUpdates
bool publishUpdates
Definition: AbstractWorkingMemory.h:168
memoryx::AbstractWorkingMemory::unregisterUpdater
void unregisterUpdater(const std::string &updaterName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:180
memoryx::AbstractWorkingMemory::updaters
MemoryUpdaterMap updaters
Definition: AbstractWorkingMemory.h:162
memoryx::AbstractWorkingMemory::listenerPrx
WorkingMemoryListenerInterfacePrx listenerPrx
Definition: AbstractWorkingMemory.h:159
AbstractWorkingMemory.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:300
memoryx::AbstractWorkingMemory::addSegment
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:118
memoryx::AbstractWorkingMemory::useLongtermMemory
bool useLongtermMemory
Definition: AbstractWorkingMemory.h:166
memoryx::AbstractWorkingMemory::dbSerializer
MongoSerializerPtr dbSerializer
Definition: AbstractWorkingMemory.h:160
memoryx::AbstractWorkingMemory::usePriorMemory
bool usePriorMemory
Definition: AbstractWorkingMemory.h:165
memoryx::AbstractWorkingMemory::updatesTopicName
std::string updatesTopicName
Definition: AbstractWorkingMemory.h:172
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:407
memoryx::AbstractWorkingMemory::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: AbstractWorkingMemory.cpp:39
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:154
memoryx::AbstractWorkingMemory::updaterMutex
std::shared_mutex updaterMutex
Definition: AbstractWorkingMemory.h:163