ObjectClassMemorySegment.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::WorkingMemory
17 * @author Kai Welke ( welke 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 
27 
30 
31 #include <MemoryX/interface/core/EntityBase.h>
32 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
33 #include <MemoryX/interface/memorytypes/MemorySegments.h>
34 #include <MemoryX/interface/components/PriorKnowledgeInterface.h>
35 
36 namespace memoryx
37 {
38  /**
39  * The object class segment is a specialized segment of the SegmentedMemory.
40  * It keeps object classes which are usually held in short term memory.
41  * This segment is usually part of the working memory.
42  */
44  virtual public WorkingMemoryEntitySegment<ObjectClass>,
45  virtual public ObjectClassMemorySegmentBase
46  {
47  public:
48  /**
49  * Creates a new object class segment.
50  * Object classes are usually fetched from prior knowledge. Use the method addPriorClassWithSubclasses() in order to fetch
51  * parts of the class ontology into the object classes segment.
52  * Object classes in this segment use reference counting so use the methods provided in this class to add, update and remove
53  * classes.
54  * The ObjectClass MongoDB-collections that are accessed can be configured via PriorKnowledge config files (e.g. Prior_Objects)
55  *
56  * @param priorKnowledgePrx proxy to prior knowledge
57  *
58  */
59  ObjectClassMemorySegment(const PriorKnowledgeInterfacePrx& priorKnowledgePrx) :
61  ObjectClassMemorySegmentBase::ObjectClassMemorySegmentBase()
62  {
63  this->priorKnowledgePrx = priorKnowledgePrx;
64 
65  // get classes from prior knowledge
66  classesSegmentPrx = priorKnowledgePrx->getObjectClassesSegment();
67  }
68 
69  /**
70  * Fetches a class and its ontological subclasses from prior knowledge and inserts the complete
71  * subtree in this segment.
72  * If a class with the same name already exists in this segment, its reference count is increased.
73  *
74  * @param className name of the class in prior knowledge
75  */
76  ObjectClassList addPriorClassWithSubclasses(const std::string& className, const ::Ice::Current& = Ice::emptyCurrent) override
77  {
78  ObjectClassList touchedClasses;
79 
80  // iterate through unique classes and add them to classes segment
81  ObjectClassList objectClasses = classesSegmentPrx->getClassWithSubclasses(className);
82 
83  ObjectClassList::iterator iter = objectClasses.begin();
84 
85  while (iter != objectClasses.end())
86  {
87  ObjectClassBasePtr priorObjectClass = *iter;
88 
89  ObjectClassBasePtr wmObjectClass = addClass(priorObjectClass);
90  touchedClasses.push_back(wmObjectClass);
91 
92  iter++;
93  }
94 
95  return touchedClasses;
96  }
97 
98  /**
99  * Retrieves complete ontological tree from this segment
100  *
101  * @param rootClassName name of the root class
102  */
103  ObjectClassList getClassWithSubclasses(const std::string& rootClassName, const ::Ice::Current& = Ice::emptyCurrent) override
104  {
105  // iterate through classes
106  ObjectClassList relevantClasses;
107  ObjectClassPtr root = ObjectClassPtr::dynamicCast(getEntityByName(rootClassName));
108 
109  if (!root)
110  {
111  return ObjectClassList();
112  }
113 
114  relevantClasses.push_back(root);
115 
116  int index = 0;
117 
118  while (index != int(relevantClasses.size()))
119  {
120  ObjectClassBasePtr current = relevantClasses.at(index);
121 
122  // add children of this class
123  ObjectClassList childs = getChildClasses(current->getName());
124 
125  if (childs.size() != 0)
126  {
127  std::copy(childs.begin(), childs.end(), std::back_inserter(relevantClasses));
128  }
129 
130  index++;
131  }
132 
133  return relevantClasses;
134  }
135 
136  /**
137  * Update a class with the given entity. Copys reference counter to updated entity.
138  *
139  * @param className name of the class in prior knowledge
140  * @param update entity replacing the old one
141  */
142  ObjectClassBasePtr updateClass(const std::string& className, const ObjectClassBasePtr& update, const ::Ice::Current& = Ice::emptyCurrent) override
143  {
144  ARMARX_DEBUG_S << "updateClass(): " << className;
145 
146  if (update->getName() != className)
147  {
148  return NULL;
149  }
150 
151  // check whether entity already present in segment
152  EntityBasePtr storedObjectClass = getEntityByName(className);
153 
154  if (!storedObjectClass)
155  {
156  return NULL;
157  }
158 
159  // copy reference counting
160  std::string refCountAttrName = "referenceCounter";
161 
162  if (storedObjectClass->hasAttribute(refCountAttrName))
163  {
164 
165  int counter = storedObjectClass->getAttribute(refCountAttrName)->getValue()->getInt();
166  update->putAttribute(new EntityAttribute(refCountAttrName, new armarx::Variant(counter)));
167  }
168 
169  // update the entity
170  updateEntity(storedObjectClass->getId(), update);
171 
172  return ObjectClassBasePtr::dynamicCast(getEntityById(storedObjectClass->getId()));
173  }
174 
175  /**
176  * Removes a class and its ontological subclasses. The ontology is fetched from prior knowledge.
177  *
178  * Reference counting is used, so classes are only removed, if they are not referenced anymore.
179  *
180  * @param className name of the class in prior knowledge
181  */
182  void removePriorClassWithSubclasses(const std::string& className, const ::Ice::Current& = Ice::emptyCurrent) override
183  {
184  // iterate through unique classes and add them to classes segment
185  ObjectClassList objectClasses = classesSegmentPrx->getClassWithSubclasses(className);
186 
187  ObjectClassList::iterator iter = objectClasses.begin();
188 
189 
190  while (iter != objectClasses.end())
191  {
192  removeClass((*iter)->getName());
193  iter++;
194  }
195  }
196 
197  static std::string ObjectsToString(const ObjectClassList& objects)
198  {
199  std::string result;
200 
201  for (ObjectClassList::const_iterator it = objects.begin(); it != objects.end(); it++)
202  {
203  if (!result.empty())
204  {
205  result += ", ";
206  }
207 
208  if (*it)
209  {
210  result += (*it)->getName();
211  }
212  else
213  {
214  result += "NULL";
215  }
216  }
217 
218  return result;
219  }
220  static Ice::StringSeq ObjectsToStringList(const ObjectClassList& objects)
221  {
222  Ice::StringSeq result;
223 
224  for (ObjectClassList::const_iterator it = objects.begin(); it != objects.end(); it++)
225  {
226  if (*it)
227  {
228  result.push_back((*it)->getName());
229  }
230  else
231  {
232  result.push_back("NULL");
233  }
234  }
235 
236  return result;
237  }
238 
239  static bool ListContainsObject(const ObjectClassList& objects, ObjectClassBasePtr object)
240  {
241  for (ObjectClassList::const_iterator it = objects.begin(); it != objects.end(); it++)
242  {
243  if ((*it)->getName() == object->getName())
244  {
245  return true;
246  }
247  }
248 
249  return false;
250  }
251 
252  private:
253  ObjectClassBasePtr addClass(const ObjectClassBasePtr& objectClass)
254  {
255  std::string refCountAttrName = "referenceCounter";
256 
257  // check whether entity already present in segment
258  EntityBasePtr storedObjectClass = getEntityByName(objectClass->getName());
259 
260  if (storedObjectClass)
261  {
262  // increase reference counter
263  if (storedObjectClass->hasAttribute(refCountAttrName))
264  {
265  int counter = storedObjectClass->getAttribute(refCountAttrName)->getValue()->getInt();
266  counter++;
267  storedObjectClass->putAttribute(new EntityAttribute(refCountAttrName, new armarx::Variant(counter)));
268  }
269  else
270  {
271  storedObjectClass->putAttribute(new EntityAttribute(refCountAttrName, new armarx::Variant(2)));
272  }
273  }
274  else
275  {
276  // add entity
277  objectClass->putAttribute(new EntityAttribute(refCountAttrName, new armarx::Variant(1)));
278  std::string id = addEntity(objectClass);
279  storedObjectClass = getEntityById(id);
280  }
281 
282  return ObjectClassBasePtr::dynamicCast(storedObjectClass);
283  }
284 
285  void removeClass(const std::string& className)
286  {
287  std::string refCountAttrName = "referenceCounter";
288 
289  // check whether entity already present in segment
290  EntityBasePtr storedObjectClass = getEntityByName(className);
291 
292  if (storedObjectClass)
293  {
294  // storedObjectClass reference counter
295  if (storedObjectClass->hasAttribute(refCountAttrName))
296  {
297 
298  int counter = storedObjectClass->getAttribute(refCountAttrName)->getValue()->getInt();
299  counter--;
300 
301  if (counter != 0)
302  {
303  storedObjectClass->putAttribute(new EntityAttribute(refCountAttrName, new armarx::Variant(counter)));
304  }
305  else
306  {
307  // remove entity
308  ARMARX_INFO_S << "Removing object class " << storedObjectClass->getName();
309  removeEntity(storedObjectClass->getId());
310  }
311  }
312  }
313  }
314 
315  ObjectClassList getChildClasses(const std::string& parentClassName)
316  {
317  ObjectClassList result;
318  EntityBaseList children = getEntitiesByAttrValue("parentClasses", parentClassName);
319 
320  for (EntityBaseList::const_iterator it = children.begin(); it != children.end(); ++it)
321  {
322  result.push_back(ObjectClassBasePtr::dynamicCast(*it));
323  }
324 
325  return result;
326  }
327 
328  PriorKnowledgeInterfacePrx priorKnowledgePrx;
329  memoryx::PersistentObjectClassSegmentBasePrx classesSegmentPrx;
330  };
331 
333 
334 
335 
336 }
337 
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
memoryx::ObjectClassMemorySegment::updateClass
ObjectClassBasePtr updateClass(const std::string &className, const ObjectClassBasePtr &update, const ::Ice::Current &=Ice::emptyCurrent) override
Update a class with the given entity.
Definition: ObjectClassMemorySegment.h:142
index
uint8_t index
Definition: EtherCATFrame.h:59
memoryx::ObjectClassMemorySegment::removePriorClassWithSubclasses
void removePriorClassWithSubclasses(const std::string &className, const ::Ice::Current &=Ice::emptyCurrent) override
Removes a class and its ontological subclasses.
Definition: ObjectClassMemorySegment.h:182
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::ObjectClassMemorySegment
The object class segment is a specialized segment of the SegmentedMemory.
Definition: ObjectClassMemorySegment.h:43
ObjectClass.h
memoryx::ObjectClassMemorySegment::ListContainsObject
static bool ListContainsObject(const ObjectClassList &objects, ObjectClassBasePtr object)
Definition: ObjectClassMemorySegment.h:239
memoryx::ObjectClassMemorySegment::ObjectClassMemorySegment
ObjectClassMemorySegment(const PriorKnowledgeInterfacePrx &priorKnowledgePrx)
Creates a new object class segment.
Definition: ObjectClassMemorySegment.h:59
IceInternal::Handle< ObjectClass >
memoryx::WorkingMemoryEntitySegment
Definition: WorkingMemoryEntitySegment.h:44
ARMARXCOMPONENT_IMPORT_EXPORT
#define ARMARXCOMPONENT_IMPORT_EXPORT
Definition: ImportExportComponent.h:38
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
memoryx::ObjectClassMemorySegment::ObjectsToStringList
static Ice::StringSeq ObjectsToStringList(const ObjectClassList &objects)
Definition: ObjectClassMemorySegment.h:220
memoryx::ObjectClass
Definition: ObjectClass.h:37
WorkingMemoryEntitySegment.h
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
memoryx::ObjectClassMemorySegment::addPriorClassWithSubclasses
ObjectClassList addPriorClassWithSubclasses(const std::string &className, const ::Ice::Current &=Ice::emptyCurrent) override
Fetches a class and its ontological subclasses from prior knowledge and inserts the complete subtree ...
Definition: ObjectClassMemorySegment.h:76
memoryx::EntityAttribute
Attribute of MemoryX entities.
Definition: EntityAttribute.h:48
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
memoryx::ObjectClassMemorySegment::ObjectsToString
static std::string ObjectsToString(const ObjectClassList &objects)
Definition: ObjectClassMemorySegment.h:197
memoryx::ObjectClassMemorySegment::getClassWithSubclasses
ObjectClassList getClassWithSubclasses(const std::string &rootClassName, const ::Ice::Current &=Ice::emptyCurrent) override
Retrieves complete ontological tree from this segment.
Definition: ObjectClassMemorySegment.h:103
Variant.h
ImportExportComponent.h