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