RelationsMemorySegment.cpp
Go to the documentation of this file.
2 
3 namespace memoryx
4 {
5 
6  RelationBasePtr RelationMemorySegment::getRelationById(const std::string& id, const Ice::Current&) const
7  {
8  return RelationBasePtr::dynamicCast(getEntityById(id));
9  }
10 
11  RelationList RelationMemorySegment::getRelationsByName(const std::string& name, const Ice::Current&) const
12  {
13  RelationList result;
14  // Relations do not have an attribute 'name'. The name is stored as a std::string member
15  // Therefore, the method getEntitiesByName cannot be used...
16  EntityBaseList allEntities = getAllEntities();
17  for (EntityBasePtr const& entity : allEntities)
18  {
19  if (entity->getName() == name)
20  {
21  result.push_back(RelationBasePtr::dynamicCast(entity));
22  }
23  }
24 
25  return result;
26  }
27 
28  RelationList RelationMemorySegment::getRelationsBySign(bool sign, const Ice::Current&) const
29  {
30  RelationList result;
31 
32  for (const auto& id : getAllEntityIds())
33  {
34  RelationBasePtr rel = RelationBasePtr::dynamicCast(getEntityById(id));
35 
36  if (rel->getSign() == sign)
37  {
38  result.push_back(rel);
39  }
40  }
41 
42  return result;
43  }
44 
45  RelationList RelationMemorySegment::getRelationsByEntityId(const std::string& entityId, const Ice::Current&) const
46  {
47  RelationList result;
48 
49  for (const auto& id : getAllEntityIds())
50  {
51  RelationBasePtr rel = RelationBasePtr::dynamicCast(getEntityById(id));
52  EntityRefList relEntities = rel->getEntities();
53 
54  for (EntityRefBasePtr& relEntity : relEntities)
55  {
56  if (relEntity->entityId == entityId)
57  {
58  result.push_back(rel);
59  break;
60  }
61  }
62  }
63 
64  return result;
65  }
66 
67  RelationList RelationMemorySegment::getRelationsByEntityRef(const EntityRefBasePtr& entityRef, const Ice::Current&) const
68  {
69  return getRelationsByEntityId(entityRef->entityId);
70  }
71 
72  RelationList RelationMemorySegment::getRelationsByEntityRefs(const EntityRefList& entities, const Ice::Current&) const
73  {
74  return getRelationsByAttrValues(entities);
75  }
76 
77  RelationBasePtr RelationMemorySegment::getRelationByAttrValues(const std::string& name, const EntityRefList& entities, bool sign, const Ice::Current&) const
78  {
79  RelationBasePtr result;
80  RelationList resultList = getRelationsByAttrValues(entities, name, true, sign);
81 
82  if (!resultList.empty())
83  {
84  result = resultList[0];
85  }
86 
87  return result;
88  }
89 
90  RelationList RelationMemorySegment::getRelationsByAttrValues(const EntityRefList& entities, const std::string& name, bool considerSign, bool sign, const Ice::Current&) const
91  {
92  RelationList result;
93 
94  ARMARX_INFO_S << "Checking for relation: " << name << " sign: " << sign;
95  for (const auto& entity : getAllEntities())
96  {
97  RelationBasePtr rel = RelationBasePtr::dynamicCast(entity);
98 
99  if (!name.empty() && (rel->getName() != name))
100  {
101  continue;
102  }
103 
104  if (considerSign && (sign != rel->getSign()))
105  {
106  continue;
107  }
108 
109  EntityRefList relEntities = rel->getEntities();
110 
111  bool foundArgs = true;
112 
113  for (const auto& entityRef : entities)
114  {
115  foundArgs &= std::find_if(relEntities.cbegin(), relEntities.cend(), [&](const memoryx::EntityRefBasePtr & e)
116  {
117  return entityRef->equals(e);
118  }) != relEntities.cend();
119  }
120 
121  if (foundArgs)
122  {
123  result.push_back(rel);
124  }
125  }
126 
127  return result;
128  }
129 
130  std::string RelationMemorySegment::addEntity(const EntityBasePtr& entity, const Ice::Current&)
131  {
132  RelationPtr rel = RelationPtr::dynamicCast(entity);
133  ARMARX_INFO_S << "Adding relation " << rel->getName() << " with sign " << rel->getSign();
135  }
136 
137  void RelationMemorySegment::updateEntity(const std::string& id, const EntityBasePtr& entity, const Ice::Current&)
138  {
139  RelationPtr rel = RelationPtr::dynamicCast(entity);
140  ARMARX_INFO_S << "updating relation " << rel->getName() << " with sign " << rel->getSign();
142  }
143 
144  void RelationMemorySegment::removeRelations(const std::string& name, const Ice::Current&)
145  {
146  RelationList relations = getRelationsByName(name);
147  for (auto& relation : relations)
148  {
149  removeEntity(relation->getId());
150  }
151  }
152 
153  void RelationMemorySegment::replaceRelations(const RelationList& newRelations, const Ice::Current& c)
154  {
155  std::set<std::string> names;
156  for (auto& relation : newRelations)
157  {
158  names.insert(relation->getName());
159  }
160  for (std::string const& name : names)
161  {
162  removeRelations(name, c);
163  }
164  for (auto& relation : newRelations)
165  {
166  addEntity(relation, c);
167  }
168  }
169 
170 }
memoryx::RelationMemorySegment::getRelationsByEntityId
RelationList getRelationsByEntityId(const ::std::string &entityId, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: RelationsMemorySegment.cpp:45
memoryx::WorkingMemoryEntitySegment< Relation >::getAllEntities
EntityBaseList getAllEntities(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: WorkingMemoryEntitySegment.h:585
memoryx::RelationMemorySegment::replaceRelations
void replaceRelations(const RelationList &newRelations, const Ice::Current &) override
Definition: RelationsMemorySegment.cpp:153
memoryx::RelationMemorySegment::getRelationsBySign
RelationList getRelationsBySign(bool sign, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: RelationsMemorySegment.cpp:28
memoryx::WorkingMemoryEntitySegment::updateEntity
void updateEntity(const std::string &entityId, const EntityBasePtr &update, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:246
armarx::sign
T sign(T t)
Definition: algorithm.h:194
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::RelationMemorySegment::addEntity
std::string addEntity(const EntityBasePtr &entity, const Ice::Current &) override
Definition: RelationsMemorySegment.cpp:130
memoryx::WorkingMemoryEntitySegment< Relation >::getAllEntityIds
EntityIdList getAllEntityIds(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: WorkingMemoryEntitySegment.h:573
memoryx::RelationMemorySegment::removeRelations
void removeRelations(const std::string &name, const Ice::Current &) override
Definition: RelationsMemorySegment.cpp:144
IceInternal::Handle
Definition: forward_declarations.h:8
memoryx::RelationMemorySegment::updateEntity
void updateEntity(const std::string &id, const EntityBasePtr &entity, const Ice::Current &) override
Definition: RelationsMemorySegment.cpp:137
memoryx::WorkingMemoryEntitySegment< Relation >::removeEntity
void removeEntity(const ::std::string &id, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: WorkingMemoryEntitySegment.h:312
memoryx::WorkingMemoryEntitySegment::addEntity
std::string addEntity(const EntityBasePtr &entity, const ::Ice::Current &c=Ice::emptyCurrent) override
addEntity addes an entity to this segment. The registered fusion methods are applied in order to fuse...
Definition: WorkingMemoryEntitySegment.h:164
memoryx::RelationMemorySegment::getRelationByAttrValues
RelationBasePtr getRelationByAttrValues(const std::string &name, const EntityRefList &entities, bool sign, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: RelationsMemorySegment.cpp:77
memoryx::RelationMemorySegment::getRelationsByEntityRef
RelationList getRelationsByEntityRef(const EntityRefBasePtr &entityRef, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: RelationsMemorySegment.cpp:67
memoryx::RelationMemorySegment::getRelationsByEntityRefs
RelationList getRelationsByEntityRefs(const EntityRefList &entities, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: RelationsMemorySegment.cpp:72
memoryx::RelationMemorySegment::getRelationsByName
RelationList getRelationsByName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: RelationsMemorySegment.cpp:11
RelationsMemorySegment.h
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
memoryx::WorkingMemoryEntitySegment< Relation >::getEntityById
EntityBasePtr getEntityById(const ::std::string &id) const
Definition: WorkingMemoryEntitySegment.h:427
memoryx::RelationMemorySegment::getRelationById
RelationBasePtr getRelationById(const ::std::string &id, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: RelationsMemorySegment.cpp:6