Entity.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::Core
17 * @author Alexey Kozlov ( kozlov at kit dot edu), Kai Welke ( welke at kit dot 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 "AbstractEntityWrapper.h"
26 #include "EntityAttribute.h"
27 
28 #include <MemoryX/interface/core/EntityBase.h>
29 #include <MemoryX/interface/core/ProbabilityMeasures.h>
30 
32 
33 #include <type_traits>
34 #include <shared_mutex>
35 #include <mutex>
36 #include <list>
37 
38 namespace memoryx
39 {
40 
41  class Entity;
42  class EntityRef;
47 
246  class Entity :
247  virtual public memoryx::EntityBase
248  {
249  protected:
250  Entity(const Entity& source);
251  public:
257 
262  ::std::string getId(const ::Ice::Current& = Ice::emptyCurrent) const override;
263 
268  void setId(const ::std::string& id, const ::Ice::Current& = Ice::emptyCurrent) override;
269 
274  ::std::string getName(const ::Ice::Current& = Ice::emptyCurrent) const override;
275 
280  void setName(const ::std::string& name, const ::Ice::Current& = Ice::emptyCurrent) override;
281 
286  bool isMetaEntity(const ::Ice::Current& = Ice::emptyCurrent) const override;
287 
292  void setMetaEntity(bool isMetaEntity, const ::Ice::Current& = Ice::emptyCurrent) override;
293 
298  virtual EntityRefBaseList getDirectParentRefs() const;
299 
304  virtual EntityRefBaseList getAllParentRefs(bool includeMetaEntities = true) const;
305 
306  virtual std::vector<std::string> getAllParentsAsStringList() const;
307 
312  virtual void setDirectParentRefs(const EntityRefBaseList& entityRefs);
313 
319  bool hasAttribute(const ::std::string& attrName, const ::Ice::Current& = Ice::emptyCurrent) const override;
320 
326  EntityAttributeBasePtr getAttribute(const ::std::string& attrName, const ::Ice::Current& = Ice::emptyCurrent) const override;
327 
333  virtual armarx::VariantPtr getAttributeValue(const ::std::string& attrName) const;
334 
340  void putAttribute(const ::memoryx::EntityAttributeBasePtr& attr, const ::Ice::Current& = Ice::emptyCurrent) override;
341 
349  template<typename T>
350  void putAttribute(const std::string& attrName, T attrValue,
351  ProbabilityMeasureBasePtr uncertainty = ProbabilityMeasureBasePtr())
352  {
353  const ::memoryx::EntityAttributeBasePtr attr = new EntityAttribute(attrName);
354  attr->setValueWithUncertainty(armarx::VariantPtr(new armarx::Variant(attrValue)), uncertainty);
355 
356  putAttribute(attr);
357  }
358 
364  void removeAttribute(const ::std::string& attrName, const ::Ice::Current& = Ice::emptyCurrent) override;
365 
371  memoryx::NameList getAttributeNames(const ::Ice::Current& = Ice::emptyCurrent) const override;
372 
382  bool equals(const EntityBasePtr& otherEntity, const ::Ice::Current& = Ice::emptyCurrent) const override;
383 
393  bool equalsAttributes(const EntityBasePtr& otherEntity, const ::Ice::Current& = Ice::emptyCurrent) const override;
394 
407  template<typename T>
409  {
410  static_assert(!std::is_same_v<T, EntityWrappers::AbstractEntityWrapper>,
411  "The template parameter for Entity::addWrapper should not be of base class AbstractEntityWrapper");
412 
413  static_assert(!std::is_base_of_v<T, EntityWrappers::AbstractEntityWrapper>,
414  "The template parameter for Entity::addWrapper needs to inherit from AbstractEntityWrapper");
415 
416  IceInternal::Handle<T> result = getWrapper<T>();
417 
418  if (!result)
419  {
420  wrapper->setEntity(this);
421  std::scoped_lock lock(wrappersMutex);
422  wrappers.push_back(wrapper);
423  return wrapper;
424  }
425 
426  return result;
427  }
428 
429 
436  template<typename T>
438  {
439  static_assert(!std::is_same_v<T, EntityWrappers::AbstractEntityWrapper>,
440  "The template parameter for Entity::getWrapper should not be of base class AbstractEntityWrapper");
441 
442  static_assert(!std::is_base_of_v<T, EntityWrappers::AbstractEntityWrapper>,
443  "The template parameter for Entity::getWrapper needs to inherit from AbstractEntityWrapper");
444  std::scoped_lock lock(wrappersMutex);
445  AbstractEntityWrapperBaseList::iterator iter = wrappers.begin();
446 
447  while (iter != wrappers.end())
448  {
450 
451  if (h)
452  {
453  return h;
454  }
455 
456  iter++;
457  }
458 
459  // ARMARX_ERROR_S << "No Wrapper found - " << wrappers.size() << " in list";
460  return NULL;
461  }
462 
463  // cloning
464  Ice::ObjectPtr ice_clone() const override;
465  EntityPtr clone(const Ice::Current& c = Ice::emptyCurrent) const;
466 
478  void serialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) const override;
479 
491  void deserialize(const armarx::ObjectSerializerBasePtr& serializer, const ::Ice::Current& = Ice::emptyCurrent) override;
492 
493 
494  protected:
495  template <class BaseClass, class VariantClass>
497 
498  Entity() {}
499  ~Entity() override; // only shared pointers allowed
500  void output(std::ostream& stream) const;
501 
502  AbstractEntityWrapperBaseList wrappers;
503 
504  mutable std::mutex attributesMutex;
505  mutable std::shared_mutex entityMutex;
506  mutable std::recursive_mutex wrappersMutex;
507 
508  void ice_preMarshal() override;
509  void ice_postUnmarshal() override;
510  private:
515  void deepCopy(const Entity& source);
516  public: // streaming operator
517  friend std::ostream& operator<<(std::ostream& stream, const Entity& rhs)
518  {
519  rhs.output(stream);
520  return stream;
521  }
522 
523  friend std::ostream& operator<<(std::ostream& stream, const EntityPtr& rhs)
524  {
525  rhs->output(stream);
526  return stream;
527  }
528 
529  friend std::ostream& operator<<(std::ostream& stream, const EntityBasePtr& rhs)
530  {
531  stream << EntityPtr::dynamicCast(rhs);
532  return stream;
533  }
534 
535  // // Shared interface
536  public:
537  void __decRef() override;
538  };
539 
540 }
memoryx::Entity::ice_preMarshal
void ice_preMarshal() override
Definition: Entity.cpp:421
memoryx::Entity::setMetaEntity
void setMetaEntity(bool isMetaEntity, const ::Ice::Current &=Ice::emptyCurrent) override
Mark this entity as meta (i.e.
Definition: Entity.cpp:196
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::VariantType::EntityRef
const armarx::VariantTypeId EntityRef
Definition: EntityRef.h:30
memoryx::Entity::putAttribute
void putAttribute(const std::string &attrName, T attrValue, ProbabilityMeasureBasePtr uncertainty=ProbabilityMeasureBasePtr())
Create and store attribute from name, value, and optionally uncertainty measure.
Definition: Entity.h:350
memoryx::Entity::putAttribute
void putAttribute(const ::memoryx::EntityAttributeBasePtr &attr, const ::Ice::Current &=Ice::emptyCurrent) override
Store attribute in entity.
Definition: Entity.cpp:327
memoryx::Entity::getName
::std::string getName(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve name of this entity.
Definition: Entity.cpp:173
memoryx::Entity::getId
::std::string getId(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve id of this entity which is an integer in string representation.
Definition: Entity.cpp:161
memoryx::Entity::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Subclasses should use serializable attributes and consider these methods final.
Definition: Entity.cpp:470
memoryx::Entity::getAttribute
EntityAttributeBasePtr getAttribute(const ::std::string &attrName, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve attribute from entity.
Definition: Entity.cpp:293
memoryx::Entity::Entity
Entity()
Definition: Entity.h:498
memoryx::Entity::removeAttribute
void removeAttribute(const ::std::string &attrName, const ::Ice::Current &=Ice::emptyCurrent) override
Remove attribute with given name from entity.
Definition: Entity.cpp:333
memoryx::Entity::~Entity
~Entity() override
Definition: Entity.cpp:142
memoryx::Entity::clone
EntityPtr clone(const Ice::Current &c=Ice::emptyCurrent) const
Definition: Entity.cpp:402
memoryx::Entity::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Subclasses should use serializable attributes and consider these methods final.
Definition: Entity.cpp:441
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::Entity::getAttributeNames
memoryx::NameList getAttributeNames(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve list of all attribute names.
Definition: Entity.cpp:346
memoryx::Entity::CreateGenericEntity
static EntityPtr CreateGenericEntity()
Creates an entity without any convenience getter/setter functions.
Definition: Entity.cpp:42
memoryx::Entity::operator<<
friend std::ostream & operator<<(std::ostream &stream, const EntityBasePtr &rhs)
Definition: Entity.h:529
memoryx::Entity::ice_postUnmarshal
void ice_postUnmarshal() override
Definition: Entity.cpp:433
memoryx::Entity::output
void output(std::ostream &stream) const
Definition: Entity.cpp:409
memoryx::Entity::getAttributeValue
virtual armarx::VariantPtr getAttributeValue(const ::std::string &attrName) const
Retrieve value of an attribute from entity.
Definition: Entity.cpp:308
memoryx::Entity::operator<<
friend std::ostream & operator<<(std::ostream &stream, const EntityPtr &rhs)
Definition: Entity.h:523
IceInternal::Handle
Definition: forward_declarations.h:8
memoryx::Entity::setDirectParentRefs
virtual void setDirectParentRefs(const EntityRefBaseList &entityRefs)
Replace parent entity references.
Definition: Entity.cpp:276
memoryx::Entity::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: Entity.cpp:397
EntityAttribute.h
memoryx::Entity::getWrapper
IceInternal::Handle< T > getWrapper()
Retrieve EntityWrapper that has previously been added with Entity::addWrapper().
Definition: Entity.h:437
memoryx::Entity::getDirectParentRefs
virtual EntityRefBaseList getDirectParentRefs() const
Retrieve parent entity references.
Definition: Entity.cpp:208
memoryx::Entity::attributesMutex
std::mutex attributesMutex
Definition: Entity.h:504
memoryx::Entity::addWrapper
IceInternal::Handle< T > addWrapper(T *wrapper)
Add EntityWrapper to entity.
Definition: Entity.h:408
memoryx::Entity::__decRef
void __decRef() override
Definition: Entity.cpp:91
memoryx::Entity::getAllParentsAsStringList
virtual std::vector< std::string > getAllParentsAsStringList() const
Definition: Entity.cpp:264
AbstractEntityWrapper.h
memoryx::Entity::getAllParentRefs
virtual EntityRefBaseList getAllParentRefs(bool includeMetaEntities=true) const
Retrieve all parents by traversing the whole hierarchy.
Definition: Entity.cpp:227
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
memoryx::Entity::setId
void setId(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) override
Set id of this entity.
Definition: Entity.cpp:167
memoryx::Entity::wrappers
AbstractEntityWrapperBaseList wrappers
Definition: Entity.h:502
memoryx::Entity::equalsAttributes
bool equalsAttributes(const EntityBasePtr &otherEntity, const ::Ice::Current &=Ice::emptyCurrent) const override
equalsAttributes computes if two Entity instances are equal.
Definition: Entity.cpp:369
memoryx::Entity::entityMutex
std::shared_mutex entityMutex
Definition: Entity.h:505
memoryx::Entity::hasAttribute
bool hasAttribute(const ::std::string &attrName, const ::Ice::Current &=Ice::emptyCurrent) const override
Check whether this entity has an attribute with the given name.
Definition: Entity.cpp:339
memoryx::Entity::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Entity &rhs)
Definition: Entity.h:517
memoryx::EntityAttribute
Attribute of MemoryX entities.
Definition: EntityAttribute.h:48
armarx::GenericFactory
Definition: FactoryCollectionBase.h:51
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:37
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
memoryx::Entity
Definition: Entity.h:246
memoryx::Entity::equals
bool equals(const EntityBasePtr &otherEntity, const ::Ice::Current &=Ice::emptyCurrent) const override
equals computes if two Entity instances are equal.
Definition: Entity.cpp:360
Variant.h
memoryx::Entity::setName
void setName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Set name of this entity.
Definition: Entity.cpp:179
memoryx::Entity::wrappersMutex
std::recursive_mutex wrappersMutex
Definition: Entity.h:506
memoryx::Entity::isMetaEntity
bool isMetaEntity(const ::Ice::Current &=Ice::emptyCurrent) const override
Indicates whether this entity only contains meta information.
Definition: Entity.cpp:185