LocalizationQuery.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::Observers
17 * @author Kai Welke (welke at kit dot edu), David Schiebener (david dot schiebener at kit dot edu)
18 * @copyright 2012 Humanoids Group, HIS, KIT
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 // core
24 #include "LocalizationQuery.h"
25 
30 
31 namespace memoryx
32 {
33  LocalizationQuery::LocalizationQuery(const std::string& queryName,
34  const std::string& objectClassName,
35  int cycleTimeMS,
36  int priority)
37  {
38  this->queryName = queryName;
39  this->className = objectClassName;
40  this->cycleTimeMS = cycleTimeMS;
41  this->priority = priority;
42  }
43 
44  std::vector<LocalizationJobPtr>
45  LocalizationQuery::createJobs(const ObjectClassMemorySegmentBasePtr& objectClassSegment)
46  {
47  std::scoped_lock lock(jobsMutex);
48 
49  std::vector<LocalizationJobPtr> jobs;
50 
51  ObjectClassList objectClasses = objectClassSegment->getClassWithSubclasses(className);
52 
53  // extract classes which are recognizable and fill in recognition method map
54  std::multimap<std::string, std::string> recognitionMethodMap;
55  std::set<std::string> recognitionMethods;
56 
57  for (ObjectClassList::iterator iter = objectClasses.begin(); iter != objectClasses.end();
58  iter++)
59  {
60  ObjectClassPtr objectClass = ObjectClassPtr::dynamicCast(*iter);
61 
62  if (getLocalizableObjectClass(objectClass))
63  {
65  objectClass->addWrapper(new EntityWrappers::ObjectRecognitionWrapper());
66  std::string recognitionMethod = recognitionWrapper->getRecognitionMethod();
67 
68  recognitionMethodMap.insert(
69  std::make_pair(recognitionMethod, objectClass->getName()));
70  recognitionMethods.insert(recognitionMethod);
71  }
72  }
73 
74  // create one job for each recognition method
75  for (std::set<std::string>::iterator iter = recognitionMethods.begin();
76  iter != recognitionMethods.end();
77  iter++)
78  {
79  std::string recognitionMethod = *iter;
80 
81  std::pair<std::multimap<std::string, std::string>::iterator,
82  std::multimap<std::string, std::string>::iterator>
83  classesForMethod;
84  classesForMethod = recognitionMethodMap.equal_range(recognitionMethod);
85 
86  std::vector<std::string> objectClassNames;
87 
88  for (std::multimap<std::string, std::string>::iterator iter = classesForMethod.first;
89  iter != classesForMethod.second;
90  iter++)
91  {
92  objectClassNames.push_back(iter->second);
93  }
94 
95  // create job
97 
98  if (cycleTimeMS <= 0)
99  {
100  job = new LocalizationStrategyOnce();
101  }
102  else
103  {
104  job = new LocalizationStrategyRepeated(cycleTimeMS);
105  }
106 
107  job->init(this, recognitionMethod, objectClassNames);
108 
109  jobsFinished[recognitionMethod] = false;
110  jobs.push_back(job);
111  }
112 
113 
114  return jobs;
115  }
116 
117  bool
119  {
120  std::scoped_lock lock(jobsMutex);
121 
122  if (jobsFinished.size() == 0)
123  {
124  return false;
125  }
126 
127  bool finished = true;
128 
129  for (std::map<std::string, bool>::iterator iter = jobsFinished.begin();
130  iter != jobsFinished.end();
131  iter++)
132  {
133  finished &= iter->second;
134  }
135 
136  return finished;
137  }
138 
139  void
140  LocalizationQuery::setJobFinished(std::string recognitionMethod)
141  {
142  std::scoped_lock lock(jobsMutex);
143  jobsFinished[recognitionMethod] = true;
144  }
145 
147  LocalizationQuery::getLocalizableObjectClass(const EntityBasePtr& entity)
148  {
149  // check if this is an entity of type objectClass
150  ObjectClassPtr objectClass = ObjectClassPtr::dynamicCast(entity);
151 
152  if (objectClass)
153  {
154  EntityWrappers::ObjectRecognitionWrapperPtr objectRecognitionWrapper =
155  objectClass->addWrapper(new EntityWrappers::ObjectRecognitionWrapper());
156  std::string recognitionMethod = objectRecognitionWrapper->getRecognitionMethod();
157 
158  // check if a recognition method is defined
159  if ((recognitionMethod != "") && (recognitionMethod != "<none>"))
160  {
161  return objectClass;
162  }
163  else
164  {
165  if (objectClass->getName() != "all")
166  {
167  ARMARX_WARNING_S << "Recognition method for object " << objectClass->getName()
168  << " is undefined";
169  }
170 
171  return NULL;
172  }
173  }
174  else
175  {
176  ARMARX_WARNING_S << "Entity " << entity->getName()
177  << " could not be casted to an ObjectClassPtr";
178  return NULL;
179  }
180  }
181 } // namespace memoryx
memoryx::LocalizationJobPtr
IceUtil::Handle< LocalizationJob > LocalizationJobPtr
Definition: LocalizationJob.h:74
LocalizationQuery.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
ObjectRecognitionWrapper.h
memoryx::EntityWrappers::ObjectRecognitionWrapperPtr
IceInternal::Handle< ObjectRecognitionWrapper > ObjectRecognitionWrapperPtr
Definition: ObjectRecognitionWrapper.h:57
IceInternal::Handle< ObjectClass >
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
LocalizationStrategyOnce.h
ObjectClassMemorySegment.h
memoryx::EntityWrappers::ObjectRecognitionWrapper
Definition: ObjectRecognitionWrapper.h:40
memoryx::ObjectClassPtr
IceInternal::Handle< ObjectClass > ObjectClassPtr
Definition: ObjectClass.h:35
LocalizationStrategyRepeated.h
memoryx::LocalizationQuery::getFinished
bool getFinished()
getFinished indicates if there are localization jobs in the queue which have not finished yet
Definition: LocalizationQuery.cpp:118