PutAwayLocationPredicateProvider.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 ArmarX::
17 * @author Valerij Wittenbeck ( valerij.wittenbeck at kit dot edu)
18 * @date 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
24 
29 
30 using namespace memoryx;
31 
33  putAwayLocation(PredicateInfo {"putAwayLocation", 2})
34 {
35 }
36 
37 
39 {
40  usingProxy("WorkingMemory");
41  usingProxy("PriorKnowledge");
42  usingProxy("LongtermMemory");
43  usingProxy("GraphNodePoseResolver");
44  usingProxy(getProperty<std::string>("CommonPlacesLearnerName").getValue());
45 
46  cplSegmentName = getProperty<std::string>("CPLSegmentName").getValue();
47  sceneName = getProperty<std::string>("PlatformGraphSceneName").getValue();
48 }
49 
51 {
52  getProxy(wm, "WorkingMemory");
53  getProxy(prior, "PriorKnowledge");
54  getProxy(ltm, "LongtermMemory");
55  getProxy(psr, "GraphNodePoseResolver");
56  getProxy(cpl, getProperty<std::string>("CommonPlacesLearnerName").getValue());
57 
58  cpl->setLTMSegmentName(cplSegmentName);
59  objectInstances = wm->getObjectInstancesSegment();
60  graphSegment = prior->getGraphSegment();
61 }
62 
64 {
65  return "PutAwayLocationPredicateProvider";
66 }
67 
68 PredicateInfoList PutAwayLocationPredicateProvider::getPredicateInfos(const Ice::Current&)
69 {
70  return {putAwayLocation};
71 }
72 
73 PredicateInstanceList PutAwayLocationPredicateProvider::calcPredicates(const Ice::Current&)
74 {
75  PredicateInstanceList result;
76  const std::string predicateName = putAwayLocation.name;
77 
78  for (const auto& entity : objectInstances->getAllEntities())
79  {
80  auto entry = getCacheEntry(ObjectInstancePtr::dynamicCast(entity)->getMostProbableClass());
81  if (entry.putAwayNodeRef)
82  {
83  result.push_back(PredicateInstance {predicateName, {objectInstances->getEntityRefById(entity->getId()), entry.putAwayNodeRef}, true});
84  }
85  }
86 
87  return result;
88 }
89 
90 PutAwayLocationPredicateProvider::CachedCommonPlace PutAwayLocationPredicateProvider::getCacheEntry(const std::string& className)
91 {
92  auto it = cpCache.find(className);
93  if (it != cpCache.end())
94  {
95  return it->second;
96  }
97 
98  if (cplObjects.empty())
99  {
100  cplObjectsSegment = ltm->getCustomInstancesSegment(cplSegmentName, false);
101  if (!cplObjectsSegment)
102  {
103  throw armarx::LocalException("No LTM segment named '") << cplSegmentName << "'";
104  }
105 
106  auto entities = cplObjectsSegment->getAllEntities();
107  std::transform(entities.cbegin(), entities.cend(), std::back_inserter(cplObjects), [](const EntityBasePtr & e)
108  {
109  return ObjectInstancePtr::dynamicCast(e);
110  });
111  }
112 
113  EntityRefBasePtr nodeRef;
114  for (const auto& cplInstance : cplObjects)
115  {
116  if (cplInstance->getMostProbableClass() == className)
117  {
118  memoryx::Cluster3DList clusterList = cpl->getPositionClustersByComponentCount(cplInstance->getName(), 1);
119  if (!clusterList.empty())
120  {
121  auto cluster = clusterList.front();
122  nodeRef = graphSegment->getEntityRefById(psr->getNearestNodeIdToPosition(sceneName, "putdownlocation", cluster.center.x, cluster.center.y));
123  break;
124  }
125  }
126  }
127 
128  if (nodeRef)
129  {
130  ARMARX_IMPORTANT << "caching putaway location for '" << className << "': '" << nodeRef->entityName << "'";
131  }
132 
133  CachedCommonPlace result {className, nodeRef};
134  cpCache.insert({className, result});
135  return result;
136 }
137 
138 
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
memoryx::PutAwayLocationPredicateProvider::PutAwayLocationPredicateProvider
PutAwayLocationPredicateProvider()
Definition: PutAwayLocationPredicateProvider.cpp:32
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::PutAwayLocationPredicateProvider::onConnectWorldStateUpdater
void onConnectWorldStateUpdater() override
Definition: PutAwayLocationPredicateProvider.cpp:50
memoryx::PutAwayLocationPredicateProvider::onInitWorldStateUpdater
void onInitWorldStateUpdater() override
Definition: PutAwayLocationPredicateProvider.cpp:38
FramedPose.h
MemoryXCoreObjectFactories.h
memoryx::PutAwayLocationPredicateProvider::calcPredicates
PredicateInstanceList calcPredicates(const Ice::Current &) override
Definition: PutAwayLocationPredicateProvider.cpp:73
ObjectInstance.h
PutAwayLocationPredicateProvider.h
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
MemoryXTypesObjectFactories.h
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:393
memoryx::PutAwayLocationPredicateProvider::getPredicateInfos
PredicateInfoList getPredicateInfos(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PutAwayLocationPredicateProvider.cpp:68
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
memoryx::PutAwayLocationPredicateProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: PutAwayLocationPredicateProvider.cpp:63