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