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