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 
26 
30 
31 using namespace memoryx;
32 
34  putAwayLocation(PredicateInfo{"putAwayLocation", 2})
35 {
36 }
37 
38 void
40 {
41  usingProxy("WorkingMemory");
42  usingProxy("PriorKnowledge");
43  usingProxy("LongtermMemory");
44  usingProxy("GraphNodePoseResolver");
45  usingProxy(getProperty<std::string>("CommonPlacesLearnerName").getValue());
46 
47  cplSegmentName = getProperty<std::string>("CPLSegmentName").getValue();
48  sceneName = getProperty<std::string>("PlatformGraphSceneName").getValue();
49 }
50 
51 void
53 {
54  getProxy(wm, "WorkingMemory");
55  getProxy(prior, "PriorKnowledge");
56  getProxy(ltm, "LongtermMemory");
57  getProxy(psr, "GraphNodePoseResolver");
58  getProxy(cpl, getProperty<std::string>("CommonPlacesLearnerName").getValue());
59 
60  cpl->setLTMSegmentName(cplSegmentName);
61  objectInstances = wm->getObjectInstancesSegment();
62  graphSegment = prior->getGraphSegment();
63 }
64 
65 std::string
67 {
68  return "PutAwayLocationPredicateProvider";
69 }
70 
71 PredicateInfoList
73 {
74  return {putAwayLocation};
75 }
76 
77 PredicateInstanceList
79 {
80  PredicateInstanceList result;
81  const std::string predicateName = putAwayLocation.name;
82 
83  for (const auto& entity : objectInstances->getAllEntities())
84  {
85  auto entry = getCacheEntry(ObjectInstancePtr::dynamicCast(entity)->getMostProbableClass());
86  if (entry.putAwayNodeRef)
87  {
88  result.push_back(PredicateInstance{
89  predicateName,
90  {objectInstances->getEntityRefById(entity->getId()), entry.putAwayNodeRef},
91  true});
92  }
93  }
94 
95  return result;
96 }
97 
98 PutAwayLocationPredicateProvider::CachedCommonPlace
99 PutAwayLocationPredicateProvider::getCacheEntry(const std::string& className)
100 {
101  auto it = cpCache.find(className);
102  if (it != cpCache.end())
103  {
104  return it->second;
105  }
106 
107  if (cplObjects.empty())
108  {
109  cplObjectsSegment = ltm->getCustomInstancesSegment(cplSegmentName, false);
110  if (!cplObjectsSegment)
111  {
112  throw armarx::LocalException("No LTM segment named '") << cplSegmentName << "'";
113  }
114 
115  auto entities = cplObjectsSegment->getAllEntities();
116  std::transform(entities.cbegin(),
117  entities.cend(),
118  std::back_inserter(cplObjects),
119  [](const EntityBasePtr& e) { return ObjectInstancePtr::dynamicCast(e); });
120  }
121 
122  EntityRefBasePtr nodeRef;
123  for (const auto& cplInstance : cplObjects)
124  {
125  if (cplInstance->getMostProbableClass() == className)
126  {
127  memoryx::Cluster3DList clusterList =
128  cpl->getPositionClustersByComponentCount(cplInstance->getName(), 1);
129  if (!clusterList.empty())
130  {
131  auto cluster = clusterList.front();
132  nodeRef = graphSegment->getEntityRefById(psr->getNearestNodeIdToPosition(
133  sceneName, "putdownlocation", cluster.center.x, cluster.center.y));
134  break;
135  }
136  }
137  }
138 
139  if (nodeRef)
140  {
141  ARMARX_IMPORTANT << "caching putaway location for '" << className << "': '"
142  << nodeRef->entityName << "'";
143  }
144 
145  CachedCommonPlace result{className, nodeRef};
146  cpCache.insert({className, result});
147  return result;
148 }
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
memoryx::PutAwayLocationPredicateProvider::PutAwayLocationPredicateProvider
PutAwayLocationPredicateProvider()
Definition: PutAwayLocationPredicateProvider.cpp:33
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::PutAwayLocationPredicateProvider::onConnectWorldStateUpdater
void onConnectWorldStateUpdater() override
Definition: PutAwayLocationPredicateProvider.cpp:52
memoryx::PutAwayLocationPredicateProvider::onInitWorldStateUpdater
void onInitWorldStateUpdater() override
Definition: PutAwayLocationPredicateProvider.cpp:39
FramedPose.h
MemoryXCoreObjectFactories.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:351
memoryx::PutAwayLocationPredicateProvider::calcPredicates
PredicateInstanceList calcPredicates(const Ice::Current &) override
Definition: PutAwayLocationPredicateProvider.cpp:78
ObjectInstance.h
PutAwayLocationPredicateProvider.h
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:407
memoryx::PutAwayLocationPredicateProvider::getPredicateInfos
PredicateInfoList getPredicateInfos(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PutAwayLocationPredicateProvider.cpp:72
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:154
memoryx::PutAwayLocationPredicateProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: PutAwayLocationPredicateProvider.cpp:66