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"
27 #include <ArmarXCore/interface/core/Log.h>
28 
29 #include <IceUtil/UUID.h>
30 
31 
32 #define COMMONSTORAGE_NAME "CommonStorage"
33 
34 namespace memoryx
35 {
37  {
38  usePriorMemory = getProperty<bool>("UsePriorMemory").getValue();
39  priorMemoryName = getProperty<std::string>("PriorMemoryName").getValue();
40 
41  if (usePriorMemory)
42  {
44  }
45 
46  useLongtermMemory = getProperty<bool>("UseLongtermMemory").getValue();
47  longtermMemoryName = getProperty<std::string>("LongtermMemoryName").getValue();
48 
50  {
52  }
53 
55 
56  if (useCommonStorage)
57  {
58  usingProxy("CommonStorage");
59  }
60 
61  publishUpdates = getProperty<bool>("PublishUpdates").getValue();
62  updatesTopicName = getProperty<std::string>("UpdatesTopicName").getValue();
63 
64  if (publishUpdates)
65  {
67  }
68 
69  // call subclass hook
71  }
72 
73 
75  {
76  ARMARX_INFO << "Starting MemoryX::AbstractWorkingMemory";
77 
78  const Ice::CommunicatorPtr ic = getIceManager()->getCommunicator();
79 
80 
82 
83  if (usePriorMemory)
84  {
85  priorKnowledgePrx = getProxy<PriorKnowledgeInterfacePrx>(priorMemoryName);
86  }
87 
89  {
90  longtermMemoryPrx = getProxy<LongtermMemoryInterfacePrx>(longtermMemoryName);
91  }
92 
93  if (useCommonStorage)
94  {
95  dataBasePrx = getProxy<CommonStorageInterfacePrx>(COMMONSTORAGE_NAME);
96  }
97 
98  if (publishUpdates)
99  {
100  listenerPrx = getTopic<WorkingMemoryListenerInterfacePrx>(updatesTopicName);
101  }
102 
103  // call subclass hook
105 
106  if (longtermMemoryPrx && !getProperty<std::string>("SnapshotToLoad").getValue().empty())
107  {
108  longtermMemoryPrx->loadWorkingMemorySnapshot(getProperty<std::string>("SnapshotToLoad").getValue(), AbstractWorkingMemoryInterfacePrx::uncheckedCast(getProxy()));
109  }
110  }
111 
112 
113  AbstractMemorySegmentPrx AbstractWorkingMemory::addSegment(const std::string& segmentName, const AbstractMemorySegmentPtr& segment, const ::Ice::Current& c)
114  {
115  AbstractWorkingMemorySegmentPtr wmSegment = AbstractWorkingMemorySegmentPtr::dynamicCast(segment);
116 
117  if (wmSegment && listenerPrx)
118  {
119  // set observer proxy
120  wmSegment->setListenerProxy(listenerPrx);
121  }
122 
123  AbstractMemorySegmentPrx segmentProxy = SegmentedMemory::addSegment(segmentName, segment);
124 
125  ARMARX_INFO << "Added AbstractWorkingMemorySegment: " << segmentName;
126 
127  return segmentProxy;
128  }
129 
130  WorkingMemoryUpdaterBasePrx AbstractWorkingMemory::registerUpdater(const std::string& updaterName, const WorkingMemoryUpdaterBasePtr& updater, const ::Ice::Current&)
131  {
132  // init updater
133  WorkingMemoryUpdaterPtr updaterImpl = WorkingMemoryUpdaterPtr::dynamicCast(updater);
134  updaterImpl->setWorkingMemory(this);
135  getArmarXManager()->addObject(updaterImpl, false);
136 
137  // insert into known updaters
138  std::pair<std::string, MemoryUpdaterEntry> updaterEntry;
139  updaterEntry.first = updaterName;
140  updaterEntry.second.proxy = WorkingMemoryUpdaterBasePrx::uncheckedCast(updaterImpl->getProxy());
141  updaterEntry.second.pointer = updaterImpl;
142 
143  std::unique_lock lock(updaterMutex);
144  updaters.insert(updaterEntry);
145 
146  ARMARX_INFO << "Registered AbstractWorkingMemoryUpdater: " << updaterName;
147 
148  return updaterEntry.second.proxy;
149  }
150 
151  WorkingMemoryUpdaterBasePrx AbstractWorkingMemory::getUpdater(const std::string& updaterName, const ::Ice::Current&)
152  {
153  std::unique_lock lock(updaterMutex);
154 
155  // find segment
156  MemoryUpdaterMap::iterator iter = updaters.find(updaterName);
157 
158  if (iter == updaters.end())
159  {
160  throw InvalidEntityException();
161  }
162 
163  return iter->second.proxy;
164  }
165 
166  void AbstractWorkingMemory::unregisterUpdater(const std::string& updaterName, const ::Ice::Current&)
167  {
168  std::unique_lock lock(updaterMutex);
169 
170  MemoryUpdaterMap::iterator iter = updaters.find(updaterName);
171 
172  if (iter != updaters.end())
173  {
174  WorkingMemoryUpdaterPtr updater = WorkingMemoryUpdaterPtr::dynamicCast(iter->second.pointer);
175 
176  // remove from manager
177  getArmarXManager()->removeObjectBlocking(updater);
178 
179  // remove from known updaters
180  updaters.erase(iter);
181 
182  ARMARX_INFO << "Unregistered AbstractWorkingMemoryUpdater: " << updaterName;
183  }
184  }
185 
186  void AbstractWorkingMemory::clear(const ::Ice::Current&)
187  {
188  std::unique_lock lock(segmentsMutex);
189 
190  for (MemorySegmentMap::iterator it = segments.begin(); it != segments.end(); ++it)
191  {
192  it->second.pointer->clear();
193  }
194  }
195 
196  void AbstractWorkingMemory::print(const ::Ice::Current&) const
197  {
198  std::unique_lock lock(segmentsMutex);
199 
200  std::cout << "Memory contains " << segments.size() << " segments" << std::endl;
201 
202  for (MemorySegmentMap::const_iterator it = segments.begin(); it != segments.end(); ++it)
203  {
204  std::cout << "Memory segment " << it->first << " contains " << it->second.pointer->size() << " entities" << std::endl;
205  it->second.pointer->print();
206  }
207  }
208 }
memoryx::SegmentedMemory::segments
MemorySegmentMap segments
Definition: SegmentedMemory.h:68
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:353
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
memoryx::AbstractWorkingMemory::useCommonStorage
bool useCommonStorage
Definition: AbstractWorkingMemory.h:146
memoryx::AbstractWorkingMemory::registerUpdater
WorkingMemoryUpdaterBasePrx registerUpdater(const std::string &updaterName, const WorkingMemoryUpdaterBasePtr &updater, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:130
ArmarXManager.h
memoryx::AbstractWorkingMemory::priorMemoryName
std::string priorMemoryName
Definition: AbstractWorkingMemory.h:149
memoryx::AbstractWorkingMemory::print
void print(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: AbstractWorkingMemory.cpp:196
memoryx::SegmentedMemory::addSegment
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: SegmentedMemory.cpp:40
memoryx::AbstractWorkingMemory::dataBasePrx
CommonStorageInterfacePrx dataBasePrx
Definition: AbstractWorkingMemory.h:135
memoryx::SegmentedMemory::segmentsMutex
std::shared_mutex segmentsMutex
Definition: SegmentedMemory.h:69
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:348
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::AbstractWorkingMemory::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: AbstractWorkingMemory.cpp:74
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:255
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:136
memoryx::AbstractWorkingMemory::longtermMemoryPrx
LongtermMemoryInterfacePrx longtermMemoryPrx
Definition: AbstractWorkingMemory.h:137
memoryx::AbstractWorkingMemory::longtermMemoryName
std::string longtermMemoryName
Definition: AbstractWorkingMemory.h:150
WorkingMemoryUpdater.h
memoryx::AbstractWorkingMemory::getUpdater
WorkingMemoryUpdaterBasePrx getUpdater(const std::string &updaterName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:151
COMMONSTORAGE_NAME
#define COMMONSTORAGE_NAME
Definition: AbstractWorkingMemory.cpp:32
WorkingMemoryEntitySegment.h
memoryx::AbstractWorkingMemory::clear
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:186
memoryx::MongoSerializer
Definition: MongoSerializer.h:40
memoryx::AbstractWorkingMemory::publishUpdates
bool publishUpdates
Definition: AbstractWorkingMemory.h:147
memoryx::AbstractWorkingMemory::unregisterUpdater
void unregisterUpdater(const std::string &updaterName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:166
memoryx::AbstractWorkingMemory::updaters
MemoryUpdaterMap updaters
Definition: AbstractWorkingMemory.h:141
memoryx::AbstractWorkingMemory::listenerPrx
WorkingMemoryListenerInterfacePrx listenerPrx
Definition: AbstractWorkingMemory.h:138
AbstractWorkingMemory.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:290
memoryx::AbstractWorkingMemory::addSegment
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: AbstractWorkingMemory.cpp:113
memoryx::AbstractWorkingMemory::useLongtermMemory
bool useLongtermMemory
Definition: AbstractWorkingMemory.h:145
memoryx::AbstractWorkingMemory::dbSerializer
MongoSerializerPtr dbSerializer
Definition: AbstractWorkingMemory.h:139
memoryx::AbstractWorkingMemory::usePriorMemory
bool usePriorMemory
Definition: AbstractWorkingMemory.h:144
memoryx::AbstractWorkingMemory::updatesTopicName
std::string updatesTopicName
Definition: AbstractWorkingMemory.h:151
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:393
memoryx::AbstractWorkingMemory::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: AbstractWorkingMemory.cpp:36
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::AbstractWorkingMemory::updaterMutex
std::shared_mutex updaterMutex
Definition: AbstractWorkingMemory.h:142