SECObjectRelations.cpp
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::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2013
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "SECObjectRelations.h"
24 #include "SECRelation.h"
25 
26 #include "../../segment/ObjectClassMemorySegment.h"
27 
29 
30 namespace memoryx
31 {
32 
34  {
35  }
36 
37  armarx::VariantDataClassPtr SECObjectRelations::clone(const Ice::Current&) const
38  {
40 
41  for (SECRelationList::const_iterator it = relations.begin(); it != relations.end(); it++)
42  {
43  result->relations.push_back(SECRelationBasePtr::dynamicCast((*it)->ice_clone()));
44  }
45 
46  return result;
47  }
48 
50  {
51  return clone();
52  }
53 
54  Ice::Float SECObjectRelations::contains(const SECObjectRelationsBasePtr& other, const Ice::Current&) const
55  {
56  float matchingRelationCount = 0;
57 
58  for (SECRelationList::const_iterator itOther = other->relations.begin(); itOther != other->relations.end(); itOther++)
59  {
60  bool foundMatch = false;
61  SECRelationBasePtr otherRel = *itOther;
62 
63  for (SECRelationList::const_iterator it = relations.begin(); it != relations.end(); it++)
64  {
65  if (otherRel->isEqual(*it))
66  {
67  foundMatch = true;
68  break;
69  }
70  }
71 
72  if (foundMatch)
73  {
74  matchingRelationCount++;
75  }
76  }
77 
78  return matchingRelationCount / (float)relations.size();
79  }
80 
81 
82  bool SECObjectRelations::containsRelation(const SECRelationBasePtr& relation, const Ice::Current&) const
83  {
84  for (SECRelationList::const_iterator it = relations.begin(); it != relations.end(); it++)
85  {
86  if (relation->isEqual(*it))
87  {
88  return true;
89  }
90  }
91 
92  return false;
93 
94  }
95 
96 
97  Ice::Int SECObjectRelations::addRelations(const SECRelationList& newRelations, const Ice::Current&)
98  {
99  int insertCount = 0;
100 
101  for (unsigned int i = 0; i < newRelations.size(); i++)
102  {
103  SECRelationBasePtr rel = containsRelationBetweenObjects(newRelations.at(i)->objects1, newRelations.at(i)->objects2);
104 
105  // ARMARX_VERBOSE_S << "Adding relation: " << newRelations.at(i)->output();
106  if (rel)
107  {
108  rel->setName(newRelations.at(i)->getName());
109  }
110  else
111  {
112  relations.push_back(newRelations.at(i));
113  insertCount++;
114  }
115  }
116 
117  return insertCount;
118  }
119 
120  bool SECObjectRelations::addRelation(const SECRelationBasePtr& newRelation, const Ice::Current& c)
121  {
122  SECRelationList list;
123  list.push_back(newRelation);
124  return (addRelations(list) != 0);
125  }
126 
127  SECRelationBasePtr SECObjectRelations::containsRelationBetweenObjects(const ObjectClassList& objects1, const ObjectClassList& objects2, const Ice::Current&) const
128  {
129  SECRelation relation("", objects1, objects2);
130 
131  for (SECRelationList::const_iterator it = relations.begin(); it != relations.end(); it++)
132  {
133  if (relation.hasEqualObjects(*it))
134  {
135  return *it;
136  }
137 
138  }
139 
140  return NULL;
141  }
142 
143  ObjectClassList SECObjectRelations::getObjectsInRelation(const SECRelationBasePtr& relation, ObjectClassBasePtr object1)
144  {
145 
146  ObjectClassList result;
147 
148  for (SECRelationList::const_iterator it = relations.begin(); it != relations.end(); it++)
149  {
150  if ((*it)->getName() == relation->getName()
151  && ObjectClassMemorySegment::ListContainsObject((*it)->objects1, object1))
152  {
153  result.insert(result.end(), (*it)->objects2.begin(), (*it)->objects2.end());
154  }
155 
156  }
157 
158  return result;
159  }
160 
161  bool SECObjectRelations::isEqual(const SECObjectRelations& compareRelations) const
162  {
163  bool result = true;
164 
165  for (const SECRelationBasePtr& relation : compareRelations.relations)
166  {
167  result &= this->containsRelation(relation);
168  }
169 
170  return result;
171  }
172 
173 
174 
175  std::string SECObjectRelations::output(const Ice::Current&) const
176  {
177  std::stringstream result;
178 
179  for (size_t i = 0; i < relations.size(); i++)
180  {
181  result << "#" << i << ": " << relations.at(i)->output() << "\n";
182  }
183 
184  return result.str();
185  }
186 
187  Ice::Int SECObjectRelations::getType(const Ice::Current&) const
188  {
190  }
191 
192  bool SECObjectRelations::validate(const Ice::Current&)
193  {
194  return true;
195  }
196 
197  void SECObjectRelations::serialize(const armarx::ObjectSerializerBasePtr& serializer, const Ice::Current&) const
198  {
199  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
200  std::vector<armarx::VariantBasePtr> variants;
201 
202  for (unsigned int i = 0; i < relations.size(); i++)
203  {
204  armarx::VariantPtr var = new armarx::Variant(relations.at(i));
205  variants.push_back(var);
206  }
207 
208  obj->setVariantArray("relations", variants);
209 
210  }
211 
212  void SECObjectRelations::deserialize(const armarx::ObjectSerializerBasePtr& serializer, const Ice::Current&)
213  {
214  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
215  std::vector<armarx::VariantPtr> variants;
216 
217  obj->getVariantArray("relations", variants);
218  relations.resize(variants.size());
219 
220  for (unsigned int i = 0; i < variants.size(); i++)
221  {
222 
223  relations.at(i) = armarx::VariantPtr::dynamicCast(variants.at(i))->get<SECRelation>();
224  }
225  }
226 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
memoryx::SECObjectRelations::validate
bool validate(const Ice::Current &c=Ice::emptyCurrent) override
Definition: SECObjectRelations.cpp:192
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
memoryx::VariantType::SECObjectRelations
const armarx::VariantTypeId SECObjectRelations
Definition: SECObjectRelations.h:29
SECObjectRelations.h
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
AbstractObjectSerializer.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::ObjectClassMemorySegment::ListContainsObject
static bool ListContainsObject(const ObjectClassList &objects, ObjectClassBasePtr object)
Definition: ObjectClassMemorySegment.h:239
memoryx::SECObjectRelations::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SECObjectRelations.cpp:212
memoryx::SECObjectRelations::isEqual
bool isEqual(const SECObjectRelations &relations) const
Definition: SECObjectRelations.cpp:161
IceInternal::Handle
Definition: forward_declarations.h:8
memoryx::SECRelation::hasEqualObjects
bool hasEqualObjects(const SECRelationBasePtr &other, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SECRelation.cpp:71
memoryx::SECObjectRelations::contains
Ice::Float contains(const SECObjectRelationsBasePtr &other, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SECObjectRelations.cpp:54
memoryx::SECObjectRelations::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: SECObjectRelations.cpp:49
memoryx::SECObjectRelations::getObjectsInRelation
ObjectClassList getObjectsInRelation(const SECRelationBasePtr &relation, ObjectClassBasePtr object1)
Definition: SECObjectRelations.cpp:143
memoryx::SECObjectRelations::SECObjectRelations
SECObjectRelations()
Definition: SECObjectRelations.cpp:33
memoryx::SECObjectRelations::getType
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SECObjectRelations.cpp:187
memoryx::SECRelation
Definition: SECRelation.h:36
memoryx::SECObjectRelations
Definition: SECObjectRelations.h:34
memoryx::SECObjectRelations::clone
armarx::VariantDataClassPtr clone(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SECObjectRelations.cpp:37
float
#define float
Definition: 16_Level.h:22
memoryx::SECObjectRelations::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SECObjectRelations.cpp:197
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
memoryx::SECObjectRelations::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SECObjectRelations.cpp:175
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
memoryx::SECObjectRelations::containsRelationBetweenObjects
SECRelationBasePtr containsRelationBetweenObjects(const ObjectClassList &objects1, const ObjectClassList &objects2, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SECObjectRelations.cpp:127
memoryx::SECObjectRelations::addRelation
bool addRelation(const SECRelationBasePtr &newRelation, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SECObjectRelations.cpp:120
memoryx::SECObjectRelations::containsRelation
bool containsRelation(const SECRelationBasePtr &relation, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: SECObjectRelations.cpp:82
memoryx::SECObjectRelations::addRelations
Ice::Int addRelations(const SECRelationList &newRelations, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SECObjectRelations.cpp:97
SECRelation.h