EntityAttribute.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::WorkingMemory
17* @author ALexey Kozlov ( kozlov at kit dot edu), Kai Welke (welke at kit got edu)
18* @date 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "EntityAttribute.h"
24
28
29namespace memoryx
30{
31
32 // ********************************************************************
33 // construction
34 // ********************************************************************
35 EntityAttribute::EntityAttribute(const std::string& name)
36 {
37 this->name = name;
38 }
39
40 EntityAttribute::EntityAttribute(const std::string& name, const armarx::VariantBasePtr& val)
41 {
42 this->name = name;
43 setValue(val);
44 }
45
47 IceUtil::Shared(other), EntityAttributeBase()
48 // EntityAttributeBase(other)
49 {
50 this->name = other.name;
51 // this->clear();
52 std::scoped_lock lock(other.valuesMutex);
53 size_t size = other.values.size();
54 this->values.reserve(size);
55
56 for (size_t i = 0; i < size; ++i)
57 {
58 AttributeElement elem;
59 const AttributeElement& origElem = other.values.at(i);
60 armarx::VariantPtr var = armarx::VariantPtr::dynamicCast(origElem.value);
61 ProbabilityMeasureBasePtr uncertainty = origElem.uncertainty;
62
63 if (uncertainty)
64 {
65 elem.uncertainty = ProbabilityMeasureBasePtr::dynamicCast(uncertainty->clone());
66 }
67
68 elem.value = var->clone();
69 values.push_back(elem);
70 }
71 }
72
74 {
75 // ARMARX_INFO_S << "~EntityAttribute() " << getName();
76 }
77
78 // ********************************************************************
79 // member access
80 // ********************************************************************
81 ::std::string
82 EntityAttribute::getName(const ::Ice::Current&) const
83 {
84 return name;
85 }
86
87 void
88 EntityAttribute::setValue(const armarx::VariantBasePtr& val, const ::Ice::Current& c)
89 {
90 std::scoped_lock lock(valuesMutex);
91
92 // only resize if neccessary!!! Issues with garbage collection
93 if (size() != 1)
94 {
95 clear();
96 addValue(val);
97 }
98 else
99 {
100 setValueWithUncertainty(val, ProbabilityMeasureBasePtr(), c);
101 }
102 }
103
104 void
106 const ProbabilityMeasureBasePtr& uncertainty,
107 const ::Ice::Current&)
108 {
109 std::scoped_lock lock(valuesMutex);
110
111 // only resize if neccessary!!! Issues with garbage collection
112 if (size() != 1)
113 {
114 clear();
115 addValueWithUncertainty(val, uncertainty);
116 }
117 else
118 {
119 AttributeElement elem;
120 elem.value = armarx::VariantPtr::dynamicCast(val)->clone();
121
122 if (uncertainty)
123 {
124 elem.uncertainty = ProbabilityMeasureBasePtr::dynamicCast(uncertainty->ice_clone());
125 }
126
127 {
128
129 values.at(0) = elem;
130 }
131 }
132 }
133
134 void
135 EntityAttribute::addValue(const armarx::VariantBasePtr& val, const ::Ice::Current& c)
136 {
137 addValueWithUncertainty(val, ProbabilityMeasureBasePtr(), c);
138 }
139
140 void
142 const ProbabilityMeasureBasePtr& uncertainty,
143 const ::Ice::Current&)
144 {
145 AttributeElement elem;
146 elem.value = armarx::VariantPtr::dynamicCast(val)->clone();
147
148 if (uncertainty)
149 {
150 elem.uncertainty = ProbabilityMeasureBasePtr::dynamicCast(uncertainty->ice_clone());
151 }
152
153 {
154 std::scoped_lock lock(valuesMutex);
155 values.push_back(elem);
156 }
157 }
158
159 bool
160 EntityAttribute::hasValue(const ::armarx::VariantBasePtr& val, const ::Ice::Current& c) const
161 {
162 // TODO extend for other types: compare operator for armarx::Variant needed
163 if (val->getType() != armarx::VariantType::String)
164 {
165 throw armarx::NotImplementedYetException();
166 }
167
168 const std::string valStr = val->getString();
169
170 std::scoped_lock lock(valuesMutex);
171
172 for (AttributeElementList::const_iterator it = values.begin(); it != values.end(); ++it)
173 {
174 if (it->value->getType() == armarx::VariantType::String &&
175 it->value->getString() == valStr)
176 {
177 return true;
178 }
179 }
180
181 return false;
182 }
183
185 EntityAttribute::getValue(const ::Ice::Current&) const
186 {
187 return getValueAt(0);
188 }
189
190 ProbabilityMeasureBasePtr
191 EntityAttribute::getUncertainty(const ::Ice::Current&) const
192 {
193 return getUncertaintyAt(0);
194 }
195
197 EntityAttribute::getValueAt(::Ice::Int index, const ::Ice::Current&) const
198 {
199 std::scoped_lock lock(valuesMutex);
200 return values.size() > (size_t)index ? values.at(index).value : armarx::VariantBasePtr();
201 }
202
203 ProbabilityMeasureBasePtr
204 EntityAttribute::getUncertaintyAt(::Ice::Int index, const ::Ice::Current&) const
205 {
206 std::scoped_lock lock(valuesMutex);
207 return values.size() > (size_t)index ? values.at(index).uncertainty
208 : ProbabilityMeasureBasePtr();
209 }
210
211 void
212 EntityAttribute::removeValueAt(::Ice::Int index, const ::Ice::Current&)
213 {
214 std::scoped_lock lock(valuesMutex);
215
216 if (values.size() > (size_t)index)
217 {
218 values.erase(values.begin() + index);
219 }
220 }
221
222 void
223 EntityAttribute::setElement(const AttributeElement& elem, const ::Ice::Current&)
224 {
225 std::scoped_lock lock(valuesMutex);
226 clear();
227 addElement(elem);
228 }
229
230 void
231 EntityAttribute::addElement(const AttributeElement& elem, const ::Ice::Current&)
232 {
233 addValueWithUncertainty(elem.value, elem.uncertainty);
234 }
235
236 AttributeElement
237 EntityAttribute::getElement(const ::Ice::Current&) const
238 {
239 return getElementAt(0);
240 }
241
242 AttributeElement
243 EntityAttribute::getElementAt(::Ice::Int index, const ::Ice::Current&) const
244 {
245 std::scoped_lock lock(valuesMutex);
246 return (values.size() > (size_t)index) ? values.at(index) : AttributeElement();
247 }
248
249 ::Ice::Int
250 EntityAttribute::size(const ::Ice::Current&) const
251 {
252 std::scoped_lock lock(valuesMutex);
253 return values.size();
254 }
255
256 void
257 EntityAttribute::clear(const ::Ice::Current&)
258 {
259 std::scoped_lock lock(valuesMutex);
260 values.clear();
261 }
262
263 Ice::ObjectPtr
265 {
266 return this->clone();
267 }
268
270 EntityAttribute::clone(const Ice::Current& c) const
271 {
272 std::scoped_lock lock(valuesMutex);
273 return new EntityAttribute(*this);
274 }
275
276 // ********************************************************************
277 // private methods
278 // ********************************************************************
279 void
280 EntityAttribute::output(std::ostream& stream) const
281 {
282 std::scoped_lock lock(valuesMutex);
283
284 for (AttributeElementList::const_iterator it = values.begin(); it != values.end(); ++it)
285 {
286 stream << "value: " << armarx::VariantPtr::dynamicCast(it->value);
287
288 if (it->uncertainty)
289 {
290 stream << ", uncertainty: "
291 << ProbabilityMeasureBasePtr::dynamicCast(it->uncertainty)->output();
292 }
293
294 stream << std::endl;
295 }
296 }
297
298 void
299 EntityAttribute::serialize(const armarx::ObjectSerializerBasePtr& serializer,
300 const ::Ice::Current& c) const
301 {
303 armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
304 obj->setElementType(armarx::ElementTypes::eArray);
305
306 std::scoped_lock lock(valuesMutex);
307
308 for (AttributeElementList::const_iterator it = values.begin(); it != values.end(); ++it)
309 {
311 armarx::AbstractObjectSerializerPtr::dynamicCast(serializer)->createElement();
312 objValue->setVariant("value", armarx::VariantPtr::dynamicCast(it->value));
313
314 if (it->uncertainty)
315 {
316 armarx::VariantPtr uncertaintyVar =
317 new armarx::Variant(armarx::VariantDataClassPtr::dynamicCast(it->uncertainty));
318 objValue->setVariant("uncertainty", uncertaintyVar);
319 }
320
321 obj->append(objValue);
322 }
323 }
324
325 void
326 EntityAttribute::deserialize(const armarx::ObjectSerializerBasePtr& serializer,
327 const ::Ice::Current& c)
328 {
330 armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
331
332 std::scoped_lock lock(valuesMutex);
333 values.clear();
334 // values.reserve(localObj->size());
335
336 for (unsigned int i = 0; i < obj->size(); ++i)
337 {
338 const armarx::AbstractObjectSerializerPtr objValue = obj->getElement(i);
339 AttributeElement elem;
340
341 try
342 {
343 elem.value = objValue->getVariant("value");
344 }
345 catch (std::exception& e)
346 {
348 << "Could not get variant for: " << objValue->toString().substr(0, 2000) << ": "
349 << e.what() << "\n Backtrace: \n"
351 }
352 catch (...)
353 {
355 << "Could not get variant for: " << objValue->toString().substr(0, 2000)
356 << "\n Backtrace: \n"
358 }
359
360 if (objValue->hasElement("uncertainty"))
361 {
362 armarx::VariantPtr uncertaintyVar = objValue->getVariant("uncertainty");
363 elem.uncertainty = uncertaintyVar->getClass<ProbabilityMeasureBase>();
364 }
365
366 values.push_back(elem);
367 }
368 }
369
370} // namespace memoryx
uint8_t index
constexpr T c
static std::string CreateBackTrace(int linesToSkip=1)
The Variant class is described here: Variants.
Definition Variant.h:224
void setValueWithUncertainty(const ::armarx::VariantBasePtr &val, const ::memoryx::ProbabilityMeasureBasePtr &uncertainty, const ::Ice::Current &=Ice::emptyCurrent) override
Sets (single) value of the attribute along with the corresponding uncertainty.
void removeValueAt(::Ice::Int index, const ::Ice::Current &=Ice::emptyCurrent) override
Removes the value at a given index from the list of values stored in the attribute.
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
Clears all values.
bool hasValue(const ::armarx::VariantBasePtr &val, const ::Ice::Current &=Ice::emptyCurrent) const override
Checks whether the value val is currently stored in the attribute.
void setValue(const ::armarx::VariantBasePtr &val, const ::Ice::Current &=Ice::emptyCurrent) override
Sets (single) value of the attribute.
std::recursive_mutex valuesMutex
::memoryx::AttributeElement getElement(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves the attribute element (=value+uncertainty).
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
void addValue(const ::armarx::VariantBasePtr &val, const ::Ice::Current &=Ice::emptyCurrent) override
Adds value to the end of attribute values list.
::armarx::VariantBasePtr getValue(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves the value of the attribute.
Ice::ObjectPtr ice_clone() const override
::memoryx::ProbabilityMeasureBasePtr getUncertaintyAt(::Ice::Int index, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve the uncertainty of the value at a given index from the list of values stored in the attribut...
void setElement(const ::memoryx::AttributeElement &elem, const ::Ice::Current &=Ice::emptyCurrent) override
Sets (single) attribute element (=value+uncertainty) to the attribute.
EntityAttributePtr clone(const Ice::Current &c=Ice::emptyCurrent) const
::memoryx::AttributeElement getElementAt(::Ice::Int index, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves an attribute element (=value+uncertainty) with a given index.
void addValueWithUncertainty(const ::armarx::VariantBasePtr &val, const ::memoryx::ProbabilityMeasureBasePtr &uncertainty, const ::Ice::Current &=Ice::emptyCurrent) override
Adds value along with the corresponding uncertainty to the end of attribute values list.
::memoryx::ProbabilityMeasureBasePtr getUncertainty(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves the uncertainty of the attribute value.
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
::armarx::VariantBasePtr getValueAt(::Ice::Int index, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve the value at a given index from the list of values stored in the attribute.
::Ice::Int size(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve number of values stored with the attribute.
void addElement(const ::memoryx::AttributeElement &elem, const ::Ice::Current &=Ice::emptyCurrent) override
Adds attribute element (=value+uncertainty) to the attribute.
EntityAttribute(const std::string &name)
Constructs a new EntityAttribute.
::std::string getName(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve name of the attribute.
#define ARMARX_IMPORTANT_S
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:210
const VariantTypeId String
Definition Variant.h:921
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
IceInternal::Handle< AbstractObjectSerializer > AbstractObjectSerializerPtr
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
VirtualRobot headers.
IceInternal::Handle< EntityAttribute > EntityAttributePtr
Typedef of EntityAttributePtr as IceInternal::Handle<EntityAttribute> for convenience.