EntityAttribute.h
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 #pragma once
24 
25 #include <MemoryX/interface/core/EntityBase.h>
28 
29 #include <type_traits>
30 #include <mutex>
31 
32 namespace memoryx
33 {
34 
35  class EntityAttribute;
36  /**
37  * Typedef of EntityAttributePtr as IceInternal::Handle<EntityAttribute> for convenience.
38  */
40 
41  /**
42  * Attribute of MemoryX entities.
43  *
44  * Each memory entity can have an arbitraty number of attributes, identified by their names. Essentially, they are stored as a map std::string -> EntityAttribute.
45  * EntityAttribute has one or many AttributeElements, each of them representing a value with uncertainty as a pair (value, probabilityMeasure).
46  * See documentation of memoryx::AttributeElement for details on what types are supported as values.
47  */
49  public memoryx::EntityAttributeBase
50  {
51  // required for the object factory
52  template <class IceBaseClass, class DerivedClass>
53  friend class ::armarx::GenericFactory;
54  public:
55  /** Constructs a new EntityAttribute.
56  *
57  * @param name the attribute is identified via this name
58  **/
59  EntityAttribute(const std::string& name);
60 
61  /** Constructs a new EntityAttribute and assigns a value
62  *
63  * @param name the attribute is identified via this name
64  * @param value value for this attribute
65  **/
66  EntityAttribute(const std::string& name, const armarx::VariantBasePtr& val);
67 
68  /** Constructs a copy from another EntityAttribute instance.
69  *
70  * @param other EntityAttribute to copy from
71  **/
72  EntityAttribute(const EntityAttribute& other);
73 
74  ~EntityAttribute() override;
75 
76  /**
77  * Retrieve name of the attribute.
78  *
79  * @return attribute name
80  **/
81  ::std::string getName(const ::Ice::Current& = Ice::emptyCurrent) const override;
82 
83  /**
84  * Sets (single) value of the attribute. Uncertainty assumed to be empty (=unknown/not specified).
85  * This is equivalent to clearing the attribute value list (clear()) and calling addValue().
86  *
87  * @param val value to set
88  **/
89  void setValue(const ::armarx::VariantBasePtr& val, const ::Ice::Current& = Ice::emptyCurrent) override;
90 
91  /**
92  * Sets (single) value of the attribute along with the corresponding uncertainty.
93  * This is equivalent to clearing the attribute values list (clear()) and calling addValueWithUncertainty().
94  *
95  * @param val value to set
96  * @param uncertainty probability distribution representing the value uncertainty
97  *
98  **/
100  const ::memoryx::ProbabilityMeasureBasePtr& uncertainty, const ::Ice::Current& = Ice::emptyCurrent) override;
101 
102  /**
103  * Adds value to the end of attribute values list. Uncertainty assumed to be empty (=unknown/not specified).
104  *
105  * @param val value to add
106  **/
107  void addValue(const ::armarx::VariantBasePtr& val, const ::Ice::Current& = Ice::emptyCurrent) override;
108 
109  /**
110  * Adds value along with the corresponding uncertainty to the end of attribute values list.
111  * This is equivalent to clearing the attribute value list (clear()) and calling addValueWithUncertainty().
112  *
113  * @param val value to set
114  * @param uncertainty probability distribution representing the value uncertainty
115  *
116  **/
118  const ::memoryx::ProbabilityMeasureBasePtr& uncertainty, const ::Ice::Current& = Ice::emptyCurrent) override;
119 
120  /**
121  * Removes the value at a given index from the list of values stored in the attribute.
122  *
123  * @param index of value
124  **/
125  void removeValueAt(::Ice::Int index, const ::Ice::Current& = Ice::emptyCurrent) override;
126 
127  /**
128  * Checks whether the value val is currently stored in the attribute. If attribute stores multiple values,
129  * each of them will be compared with val and in case of at least one match the result will be positive.
130  *
131  * NOTE: currently, this method can be used for String values ONLY! To make it applicable for other datatypes,
132  * general comparison method for armarx::Variant should be implemented in Core first.
133  *
134  * @return true if attribute has a value val, false otherwise
135  **/
136  bool hasValue(const ::armarx::VariantBasePtr& val, const ::Ice::Current& = Ice::emptyCurrent) const override;
137 
138  /**
139  * Retrieves the value of the attribute. Corresponds to calling getValueAt(0)
140  *
141  * @return value of attribute
142  **/
143  ::armarx::VariantBasePtr getValue(const ::Ice::Current& = Ice::emptyCurrent) const override;
144 
145  /**
146  * Retrieves the uncertainty of the attribute value. Corresponds to calling getUncertaintyAt(0)
147  *
148  * @return uncertainty of attribute value
149  **/
150  ::memoryx::ProbabilityMeasureBasePtr getUncertainty(const ::Ice::Current& = Ice::emptyCurrent) const override;
151 
152  /**
153  * Retrieve the value at a given index from the list of values stored in the attribute.
154  *
155  * @param index of value
156  * @return value at index
157  **/
158  ::armarx::VariantBasePtr getValueAt(::Ice::Int index, const ::Ice::Current& = Ice::emptyCurrent) const override;
159 
160  /**
161  * Retrieve the uncertainty of the value at a given index from the list of values stored in the attribute.
162  *
163  * @param index of value
164  * @return uncertainty of value at index
165  **/
166  ::memoryx::ProbabilityMeasureBasePtr getUncertaintyAt(::Ice::Int index, const ::Ice::Current& = Ice::emptyCurrent) const override;
167 
168  /**
169  * Sets (single) attribute element (=value+uncertainty) to the attribute.
170  * This is equivalent to clearing the attribute value list (clear()) and calling addValue().
171  *
172  * @param elem attribute element to set
173  **/
174  void setElement(const ::memoryx::AttributeElement& elem, const ::Ice::Current& = Ice::emptyCurrent) override;
175 
176  /**
177  * Adds attribute element (=value+uncertainty) to the attribute.
178  *
179  * @param elem attribute element to add
180  **/
181  void addElement(const ::memoryx::AttributeElement& elem, const ::Ice::Current& = Ice::emptyCurrent) override;
182 
183  /**
184  * Retrieves the attribute element (=value+uncertainty).
185  * Corresponds to calling getElementAt(0).
186  *
187  * @return uncertainty of attribute value
188  **/
189  ::memoryx::AttributeElement getElement(const ::Ice::Current& = Ice::emptyCurrent) const override;
190 
191  /**
192  * Retrieves an attribute element (=value+uncertainty) with a given index.
193  *
194  * @return uncertainty of attribute value
195  **/
196  ::memoryx::AttributeElement getElementAt(::Ice::Int index, const ::Ice::Current& = Ice::emptyCurrent) const override;
197 
198  /**
199  * Retrieve number of values stored with the attribute
200  *
201  * @return number of values
202  **/
203  ::Ice::Int size(const ::Ice::Current& = Ice::emptyCurrent) const override;
204 
205  /**
206  * Clears all values. The attribute will not contain any value after calling clear.
207  **/
208  void clear(const ::Ice::Current& = Ice::emptyCurrent) override;
209 
210 
211 
212  // TODO: cloning does not work since clone needs to be called explicilety on each Variant!!!
213  // But need to solve Value issue before cloning
214  Ice::ObjectPtr ice_clone() const override;
215  EntityAttributePtr clone(const Ice::Current& c = Ice::emptyCurrent) const;
216 
217  public: // serialization
218  void serialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) const override;
219  void deserialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) override;
220 
221  protected:
222  mutable std::recursive_mutex valuesMutex;
223 
224  private:
225  void output(std::ostream& stream) const;
226 
227  public: // streaming operator
228  friend std::ostream& operator<<(std::ostream& stream, const EntityAttribute& rhs)
229  {
230  rhs.output(stream);
231  return stream;
232  }
233 
234  friend std::ostream& operator<<(std::ostream& stream, const EntityAttributePtr& rhs)
235  {
236  rhs->output(stream);
237  return stream;
238  }
239 
240  friend std::ostream& operator<<(std::ostream& stream, const EntityAttributeBasePtr& rhs)
241  {
242  stream << EntityAttributePtr::dynamicCast(rhs);
243  return stream;
244  }
245 
246  private:
248  {
249  }
250  };
251 
252 }
253 
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
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
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
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
FactoryCollectionBase.h
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
Definition: forward_declarations.h:8
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
memoryx::EntityAttribute::operator<<
friend std::ostream & operator<<(std::ostream &stream, const EntityAttributeBasePtr &rhs)
Definition: EntityAttribute.h:240
memoryx::EntityAttribute::operator<<
friend std::ostream & operator<<(std::ostream &stream, const EntityAttributePtr &rhs)
Definition: EntityAttribute.h:234
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::operator<<
friend std::ostream & operator<<(std::ostream &stream, const EntityAttribute &rhs)
Definition: EntityAttribute.h:228
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
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