PersistentRelationSegment.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::MemoryTypes
17* @author Alexey Kozlov ( kozlov 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 <vector>
26
29
32#include <MemoryX/interface/core/EntityBase.h>
33#include <MemoryX/interface/memorytypes/MemoryEntities.h>
34#include <MemoryX/interface/memorytypes/MemorySegments.h>
35
36namespace memoryx
37{
39 virtual public PersistentEntitySegment,
40 virtual public PersistentRelationSegmentBase
41 {
42 public:
43 PersistentRelationSegment(CollectionInterfacePrx entityCollection,
45 bool useMongoIds = true) :
46 PersistentEntitySegment(entityCollection, ic, useMongoIds),
47 PersistentRelationSegmentBase()
48 {
49 }
50
51 RelationBasePtr
52 getRelationById(const ::std::string& id,
53 const ::Ice::Current& = Ice::emptyCurrent) const override
54 {
55 return RelationBasePtr::dynamicCast(getEntityById(id));
56 }
57
58 RelationList
59 getRelationsByName(const ::std::string& name,
60 const ::Ice::Current& = Ice::emptyCurrent) const override
61 {
62 EntityBaseList rels = getEntitiesByAttrValue("name", name);
63 RelationList result;
64
65 for (EntityBaseList::const_iterator it = rels.begin(); it != rels.end(); ++it)
66 {
67 RelationBasePtr rel = RelationBasePtr::dynamicCast(*it);
68 result.push_back(rel);
69 }
70
71 return result;
72 }
73
74 RelationList
75 getRelationsBySign(bool sign, const ::Ice::Current& = Ice::emptyCurrent) const override
76 {
77 RelationList result;
78
79 for (const auto& id : getAllEntityIds())
80 {
81 RelationBasePtr rel = RelationBasePtr::dynamicCast(getEntityById(id));
82
83 if (rel->getSign() == sign)
84 {
85 result.push_back(rel);
86 }
87 }
88
89 return result;
90 }
91
92 RelationList
93 getRelationsByEntityId(const ::std::string& entityId,
94 const ::Ice::Current& = Ice::emptyCurrent) const override
95 {
96 RelationList result;
97
98 for (const auto& id : getAllEntityIds())
99 {
100 RelationBasePtr rel = RelationBasePtr::dynamicCast(getEntityById(id));
101 EntityRefList relEntities = rel->getEntities();
102
103 for (EntityRefBasePtr& relEntity : relEntities)
104 {
105 if (relEntity->entityId == entityId)
106 {
107 result.push_back(rel);
108 break;
109 }
110 }
111 }
112
113 return result;
114 }
115
116 RelationList
117 getRelationsByEntityRef(const EntityRefBasePtr& entityRef,
118 const ::Ice::Current& = Ice::emptyCurrent) const override
119 {
120 return getRelationsByEntityId(entityRef->entityId);
121 }
122
123 RelationList
124 getRelationsByEntityRefs(const EntityRefList& entities,
125 const ::Ice::Current& = Ice::emptyCurrent) const override
126 {
127 return getRelationsByAttrValues(entities);
128 }
129
130 RelationBasePtr
131 getRelationByAttrValues(const std::string& name,
132 const EntityRefList& entities,
133 bool sign,
134 const ::Ice::Current& = Ice::emptyCurrent) const override
135 {
136 RelationBasePtr result;
137 RelationList resultList = getRelationsByAttrValues(entities, name, true, sign);
138
139 if (resultList.size() != 0)
140 {
141 result = resultList[0];
142 }
143
144 return result;
145 }
146
147 private:
148 RelationList
149 getRelationsByAttrValues(const EntityRefList& entities,
150 const std::string& name = "",
151 bool considerSign = false,
152 bool sign = true,
153 const ::Ice::Current& = Ice::emptyCurrent) const
154 {
155 RelationList result;
156
157 for (const auto& entity : getAllEntities())
158 {
159 RelationBasePtr rel = RelationBasePtr::dynamicCast(entity);
160
161 if (!name.empty() && (rel->getName() != name))
162 {
163 continue;
164 }
165
166 if (considerSign && (sign != rel->getSign()))
167 {
168 continue;
169 }
170
171 EntityRefList relEntities = rel->getEntities();
172
173 bool foundArgs = true;
174
175 for (const auto& entityRef : entities)
176 {
177 foundArgs &= std::find_if(relEntities.cbegin(),
178 relEntities.cend(),
179 [&](const memoryx::EntityRefBasePtr& e) {
180 return entityRef->equals(e);
181 }) != relEntities.cend();
182 }
183
184 if (foundArgs)
185 {
186 result.push_back(rel);
187 }
188 }
189
190 return result;
191 }
192
193 // RelationSegmentInterface interface
194 public:
195 void
196 removeRelations(const std::string&, const Ice::Current&) override
197 {
198 throw armarx::LocalException("Not yet implemented");
199 }
200
201 void
202 replaceRelations(const RelationList&, const Ice::Current&) override
203 {
204 throw armarx::LocalException("Not yet implemented");
205 }
206 };
207
209
210} // namespace memoryx
#define ARMARXCOMPONENT_IMPORT_EXPORT
PersistentEntitySegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr ic, bool useMongoIds=true)
EntityBasePtr getEntityById(const ::std::string &entityId, const ::Ice::Current &=Ice::emptyCurrent) const override
EntityIdList getAllEntityIds(const ::Ice::Current &=Ice::emptyCurrent) const override
EntityBaseList getEntitiesByAttrValue(const ::std::string &attrName, const ::std::string &attrValue, const ::Ice::Current &=Ice::emptyCurrent) const override
RelationList getRelationsByEntityId(const ::std::string &entityId, const ::Ice::Current &=Ice::emptyCurrent) const override
void replaceRelations(const RelationList &, const Ice::Current &) override
PersistentRelationSegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr ic, bool useMongoIds=true)
RelationList getRelationsByEntityRef(const EntityRefBasePtr &entityRef, const ::Ice::Current &=Ice::emptyCurrent) const 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
void removeRelations(const std::string &, const Ice::Current &) override
RelationList getRelationsByName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
RelationList getRelationsBySign(bool sign, const ::Ice::Current &=Ice::emptyCurrent) const override
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
VirtualRobot headers.
IceInternal::Handle< PersistentRelationSegment > PersistentRelationSegmentPtr