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
24
25#include <IceUtil/UUID.h>
26
28#include <ArmarXCore/interface/core/Log.h>
29
32
33
34#define COMMONSTORAGE_NAME "CommonStorage"
35
36namespace memoryx
37{
38 void
40 {
41 usePriorMemory = getProperty<bool>("UsePriorMemory").getValue();
42 priorMemoryName = getProperty<std::string>("PriorMemoryName").getValue();
43
45 {
47 }
48
49 useLongtermMemory = getProperty<bool>("UseLongtermMemory").getValue();
50 longtermMemoryName = getProperty<std::string>("LongtermMemoryName").getValue();
51
53 {
55 }
56
58
60 {
61 usingProxy("CommonStorage");
62 }
63
64 publishUpdates = getProperty<bool>("PublishUpdates").getValue();
65 updatesTopicName = getProperty<std::string>("UpdatesTopicName").getValue();
66
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
87 {
89 }
90
92 {
94 }
95
97 {
99 }
100
101 if (publishUpdates)
102 {
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 {
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
#define COMMONSTORAGE_NAME
constexpr T c
Property< PropertyType > getProperty(const std::string &name)
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
IceManagerPtr getIceManager() const
Returns the IceManager.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
void onInitComponent() override
Pure virtual hook for the subclass.
CommonStorageInterfacePrx dataBasePrx
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
void print(const ::Ice::Current &=Ice::emptyCurrent) const override
WorkingMemoryUpdaterBasePrx getUpdater(const std::string &updaterName, const ::Ice::Current &=Ice::emptyCurrent) override
WorkingMemoryUpdaterBasePrx registerUpdater(const std::string &updaterName, const WorkingMemoryUpdaterBasePtr &updater, const ::Ice::Current &=Ice::emptyCurrent) override
PriorKnowledgeInterfacePrx priorKnowledgePrx
virtual void onConnectWorkingMemory()=0
WorkingMemoryListenerInterfacePrx listenerPrx
void onConnectComponent() override
Pure virtual hook for the subclass.
virtual void onInitWorkingMemory()=0
LongtermMemoryInterfacePrx longtermMemoryPrx
void unregisterUpdater(const std::string &updaterName, const ::Ice::Current &=Ice::emptyCurrent) override
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
MemorySegmentMap segments
std::shared_mutex segmentsMutex
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
VirtualRobot headers.
IceInternal::Handle< WorkingMemoryUpdater > WorkingMemoryUpdaterPtr