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 
26 
27 #include "EntityAttribute.h"
28 
29 namespace 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 
46  EntityAttribute::EntityAttribute(const EntityAttribute& other) :
47  IceUtil::Shared(other),
48  EntityAttributeBase()
49  // EntityAttributeBase(other)
50  {
51  this->name = other.name;
52  // this->clear();
53  std::scoped_lock lock(other.valuesMutex);
54  size_t size = other.values.size();
55  this->values.reserve(size);
56 
57  for (size_t i = 0; i < size; ++i)
58  {
59  AttributeElement elem;
60  const AttributeElement& origElem = other.values.at(i);
61  armarx::VariantPtr var = armarx::VariantPtr::dynamicCast(origElem.value);
62  ProbabilityMeasureBasePtr uncertainty = origElem.uncertainty;
63 
64  if (uncertainty)
65  {
66  elem.uncertainty = ProbabilityMeasureBasePtr::dynamicCast(uncertainty->clone());
67  }
68 
69  elem.value = var->clone();
70  values.push_back(elem);
71  }
72  }
73 
75  {
76  // ARMARX_INFO_S << "~EntityAttribute() " << getName();
77  }
78 
79  // ********************************************************************
80  // member access
81  // ********************************************************************
82  ::std::string EntityAttribute::getName(const ::Ice::Current&) const
83  {
84  return name;
85  }
86 
87  void EntityAttribute::setValue(const armarx::VariantBasePtr& val, const ::Ice::Current& c)
88  {
89  std::scoped_lock lock(valuesMutex);
90 
91  // only resize if neccessary!!! Issues with garbage collection
92  if (size() != 1)
93  {
94  clear();
95  addValue(val);
96  }
97  else
98  {
99  setValueWithUncertainty(val, ProbabilityMeasureBasePtr(), c);
100  }
101  }
102 
104  const ProbabilityMeasureBasePtr& uncertainty,
105  const ::Ice::Current&)
106  {
107  std::scoped_lock lock(valuesMutex);
108 
109  // only resize if neccessary!!! Issues with garbage collection
110  if (size() != 1)
111  {
112  clear();
113  addValueWithUncertainty(val, uncertainty);
114  }
115  else
116  {
117  AttributeElement elem;
118  elem.value = armarx::VariantPtr::dynamicCast(val)->clone();
119 
120  if (uncertainty)
121  {
122  elem.uncertainty = ProbabilityMeasureBasePtr::dynamicCast(uncertainty->ice_clone());
123  }
124 
125  {
126 
127  values.at(0) = elem;
128  }
129  }
130  }
131 
132  void EntityAttribute::addValue(const armarx::VariantBasePtr& val, const ::Ice::Current& c)
133  {
134  addValueWithUncertainty(val, ProbabilityMeasureBasePtr(), c);
135  }
136 
138  const ProbabilityMeasureBasePtr& uncertainty,
139  const ::Ice::Current&)
140  {
141  AttributeElement elem;
142  elem.value = armarx::VariantPtr::dynamicCast(val)->clone();
143 
144  if (uncertainty)
145  {
146  elem.uncertainty = ProbabilityMeasureBasePtr::dynamicCast(uncertainty->ice_clone());
147  }
148 
149  {
150  std::scoped_lock lock(valuesMutex);
151  values.push_back(elem);
152  }
153  }
154 
155  bool EntityAttribute::hasValue(const ::armarx::VariantBasePtr& val, const ::Ice::Current& c) const
156  {
157  // TODO extend for other types: compare operator for armarx::Variant needed
158  if (val->getType() != armarx::VariantType::String)
159  {
160  throw armarx::NotImplementedYetException();
161  }
162 
163  const std::string valStr = val->getString();
164 
165  std::scoped_lock lock(valuesMutex);
166 
167  for (AttributeElementList::const_iterator it = values.begin(); it != values.end(); ++it)
168  {
169  if (it->value->getType() == armarx::VariantType::String && it->value->getString() == valStr)
170  {
171  return true;
172  }
173  }
174 
175  return false;
176  }
177 
179  {
180  return getValueAt(0);
181  }
182 
183  ProbabilityMeasureBasePtr EntityAttribute::getUncertainty(const ::Ice::Current&) const
184  {
185  return getUncertaintyAt(0);
186  }
187 
189  {
190  std::scoped_lock lock(valuesMutex);
191  return values.size() > (size_t) index ? values.at(index).value : armarx::VariantBasePtr();
192  }
193 
194  ProbabilityMeasureBasePtr EntityAttribute::getUncertaintyAt(
195  ::Ice::Int index, const ::Ice::Current&) const
196  {
197  std::scoped_lock lock(valuesMutex);
198  return values.size() > (size_t) index ? values.at(index).uncertainty : ProbabilityMeasureBasePtr();
199  }
200 
201 
202  void EntityAttribute::removeValueAt(::Ice::Int index, const ::Ice::Current&)
203  {
204  std::scoped_lock lock(valuesMutex);
205 
206  if (values.size() > (size_t) index)
207  {
208  values.erase(values.begin() + index);
209  }
210  }
211 
212  void EntityAttribute::setElement(const AttributeElement& elem,
213  const ::Ice::Current&)
214  {
215  std::scoped_lock lock(valuesMutex);
216  clear();
217  addElement(elem);
218  }
219 
220  void EntityAttribute::addElement(const AttributeElement& elem,
221  const ::Ice::Current&)
222  {
223  addValueWithUncertainty(elem.value, elem.uncertainty);
224  }
225 
226  AttributeElement EntityAttribute::getElement(const ::Ice::Current&) const
227  {
228  return getElementAt(0);
229  }
230 
231  AttributeElement EntityAttribute::getElementAt(::Ice::Int index, const ::Ice::Current&) const
232  {
233  std::scoped_lock lock(valuesMutex);
234  return (values.size() > (size_t) index) ? values.at(index) : AttributeElement();
235  }
236 
237  ::Ice::Int EntityAttribute::size(const ::Ice::Current&) const
238  {
239  std::scoped_lock lock(valuesMutex);
240  return values.size();
241  }
242 
243  void EntityAttribute::clear(const ::Ice::Current&)
244  {
245  std::scoped_lock lock(valuesMutex);
246  values.clear();
247  }
248 
250  {
251  return this->clone();
252  }
253 
254  EntityAttributePtr EntityAttribute::clone(const Ice::Current& c) const
255  {
256  std::scoped_lock lock(valuesMutex);
257  return new EntityAttribute(*this);
258  }
259 
260  // ********************************************************************
261  // private methods
262  // ********************************************************************
263  void EntityAttribute::output(std::ostream& stream) const
264  {
265  std::scoped_lock lock(valuesMutex);
266 
267  for (AttributeElementList::const_iterator it = values.begin(); it != values.end(); ++it)
268  {
269  stream << "value: " << armarx::VariantPtr::dynamicCast(it->value);
270 
271  if (it->uncertainty)
272  {
273  stream << ", uncertainty: " << ProbabilityMeasureBasePtr::dynamicCast(it->uncertainty)->output();
274  }
275 
276  stream << std::endl;
277  }
278  }
279 
280  void EntityAttribute::serialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& c) const
281  {
282  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
283  obj->setElementType(armarx::ElementTypes::eArray);
284 
285  std::scoped_lock lock(valuesMutex);
286 
287  for (AttributeElementList::const_iterator it = values.begin(); it != values.end(); ++it)
288  {
289  armarx::AbstractObjectSerializerPtr objValue = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer)->createElement();
290  objValue->setVariant("value", armarx::VariantPtr::dynamicCast(it->value));
291 
292  if (it->uncertainty)
293  {
294  armarx::VariantPtr uncertaintyVar = new armarx::Variant(armarx::VariantDataClassPtr::dynamicCast(it->uncertainty));
295  objValue->setVariant("uncertainty", uncertaintyVar);
296  }
297 
298  obj->append(objValue);
299  }
300  }
301 
302  void EntityAttribute::deserialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
303  {
304  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
305 
306  std::scoped_lock lock(valuesMutex);
307  values.clear();
308  // values.reserve(localObj->size());
309 
310  for (unsigned int i = 0; i < obj->size(); ++i)
311  {
312  const armarx::AbstractObjectSerializerPtr objValue = obj->getElement(i);
313  AttributeElement elem;
314 
315  try
316  {
317  elem.value = objValue->getVariant("value");
318  }
319  catch (std::exception& e)
320  {
321  ARMARX_IMPORTANT_S << "Could not get variant for: " << objValue->toString().substr(0, 2000) << ": " << e.what() << "\n Backtrace: \n" << armarx::LogSender::CreateBackTrace();
322  }
323  catch (...)
324  {
325  ARMARX_IMPORTANT_S << "Could not get variant for: " << objValue->toString().substr(0, 2000) << "\n Backtrace: \n" << armarx::LogSender::CreateBackTrace();
326  }
327 
328  if (objValue->hasElement("uncertainty"))
329  {
330  armarx::VariantPtr uncertaintyVar = objValue->getVariant("uncertainty");
331  elem.uncertainty = uncertaintyVar->getClass<ProbabilityMeasureBase>();
332  }
333 
334  values.push_back(elem);
335  }
336  }
337 
338 }
memoryx::EntityAttribute::hasValue
bool hasValue(const ::armarx::VariantBasePtr &val, const ::Ice::Current &=Ice::emptyCurrent) const override
Checks whether the value val is currently stored in the attribute.
Definition: EntityAttribute.cpp:155
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
ARMARX_IMPORTANT_S
#define ARMARX_IMPORTANT_S
Definition: Logging.h:203
memoryx::EntityAttribute::getUncertaintyAt
::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...
Definition: EntityAttribute.cpp:194
memoryx::EntityAttribute::getUncertainty
::memoryx::ProbabilityMeasureBasePtr getUncertainty(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves the uncertainty of the attribute value.
Definition: EntityAttribute.cpp:183
memoryx::EntityAttribute::addValueWithUncertainty
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.
Definition: EntityAttribute.cpp:137
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::VariantBasePtr
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
Definition: ManagedIceObject.h:109
armarx::ElementTypes::eArray
@ eArray
Definition: AbstractObjectSerializer.h:36
memoryx::EntityAttribute::addElement
void addElement(const ::memoryx::AttributeElement &elem, const ::Ice::Current &=Ice::emptyCurrent) override
Adds attribute element (=value+uncertainty) to the attribute.
Definition: EntityAttribute.cpp:220
memoryx::EntityAttribute::removeValueAt
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.
Definition: EntityAttribute.cpp:202
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
AbstractObjectSerializer.h
memoryx::EntityAttribute::getValue
::armarx::VariantBasePtr getValue(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves the value of the attribute.
Definition: EntityAttribute.cpp:178
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::EntityAttribute::getName
::std::string getName(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve name of the attribute.
Definition: EntityAttribute.cpp:82
IceUtil
Definition: Instance.h:21
memoryx::EntityAttribute::getElement
::memoryx::AttributeElement getElement(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves the attribute element (=value+uncertainty).
Definition: EntityAttribute.cpp:226
IceInternal::Handle<::armarx::VariantBase >
memoryx::EntityAttribute::getElementAt
::memoryx::AttributeElement getElementAt(::Ice::Int index, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieves an attribute element (=value+uncertainty) with a given index.
Definition: EntityAttribute.cpp:231
EntityAttribute.h
armarx::LogSender::CreateBackTrace
static std::string CreateBackTrace(int linesToSkip=1)
Definition: LogSender.cpp:508
memoryx::EntityAttribute::getValueAt
::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.
Definition: EntityAttribute.cpp:188
memoryx::EntityAttribute::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: EntityAttribute.cpp:249
memoryx::EntityAttribute::setElement
void setElement(const ::memoryx::AttributeElement &elem, const ::Ice::Current &=Ice::emptyCurrent) override
Sets (single) attribute element (=value+uncertainty) to the attribute.
Definition: EntityAttribute.cpp:212
memoryx::EntityAttribute::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: EntityAttribute.cpp:280
memoryx::EntityAttribute::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: EntityAttribute.cpp:302
memoryx::EntityAttribute::addValue
void addValue(const ::armarx::VariantBasePtr &val, const ::Ice::Current &=Ice::emptyCurrent) override
Adds value to the end of attribute values list.
Definition: EntityAttribute.cpp:132
memoryx::EntityAttribute::~EntityAttribute
~EntityAttribute() override
Definition: EntityAttribute.cpp:74
memoryx::EntityAttribute::clone
EntityAttributePtr clone(const Ice::Current &c=Ice::emptyCurrent) const
Definition: EntityAttribute.cpp:254
memoryx::EntityAttribute::size
::Ice::Int size(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve number of values stored with the attribute.
Definition: EntityAttribute.cpp:237
memoryx::EntityAttribute
Attribute of MemoryX entities.
Definition: EntityAttribute.h:48
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
memoryx::EntityAttribute::setValueWithUncertainty
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.
Definition: EntityAttribute.cpp:103
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
Logging.h
armarx::VariantType::String
const VariantTypeId String
Definition: Variant.h:920
memoryx::EntityAttribute::clear
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
Clears all values.
Definition: EntityAttribute.cpp:243
Variant.h
memoryx::EntityAttribute::setValue
void setValue(const ::armarx::VariantBasePtr &val, const ::Ice::Current &=Ice::emptyCurrent) override
Sets (single) value of the attribute.
Definition: EntityAttribute.cpp:87
memoryx::EntityAttribute::valuesMutex
std::recursive_mutex valuesMutex
Definition: EntityAttribute.h:222