AgentAtPredicateProvider.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 
28 
32 
33 using namespace memoryx;
34 
35 AgentAtPredicateProvider::AgentAtPredicateProvider() : agentAt(PredicateInfo{"agentAt", 2})
36 {
37 }
38 
39 void
41 {
42  usingProxy("WorkingMemory");
43  usingProxy("PriorKnowledge");
44  usingProxy("GraphNodePoseResolver");
45  distanceThreshold = getProperty<float>("DistanceThreshold").getValue();
46  humanDistanceThreshold = getProperty<float>("HumanDistanceThreshold").getValue();
47  sceneName = getProperty<std::string>("PlatformGraphSceneName").getValue();
48 }
49 
50 void
52 {
53  wm = getProxy<WorkingMemoryInterfacePrx>("WorkingMemory");
54  prior = getProxy<PriorKnowledgeInterfacePrx>("PriorKnowledge");
55  psr = getProxy<GraphNodePoseResolverInterfacePrx>("GraphNodePoseResolver");
56 
57  agentInstances = wm->getAgentInstancesSegment();
58  graphSegment = prior->getGraphSegment();
59 
60  graphNodes = graphSegment->getNodesByScene(sceneName);
61 }
62 
63 std::string
65 {
66  return "AgentAtPredicateProvider";
67 }
68 
69 PredicateInfoList
71 {
72  return {agentAt};
73 }
74 
75 PredicateInstanceList
77 {
78  PredicateInstanceList result;
79  const std::string predicateName = agentAt.name;
80 
81  for (const auto& agentEntity : agentInstances->getAllEntities())
82  {
83  AgentInstanceBasePtr agent = AgentInstanceBasePtr::dynamicCast(agentEntity);
84  EntityRefBasePtr agentRef =
85  wm->getAgentInstancesSegment()->getEntityRefById(agentEntity->getId());
86 
87  GraphNodeBasePtr closestNode =
88  psr->getNearestNodeToPose(sceneName, "robotlocation", agent->getPoseBase());
89 
90  const auto agentPoseEigen =
91  armarx::FramedPosePtr::dynamicCast(agent->getPoseBase())->toEigen();
92  auto closestPoseEigen =
93  armarx::FramedPosePtr::dynamicCast(closestNode->getPose())->toEigen();
94  auto dist = (closestPoseEigen.block<2, 1>(0, 3) - agentPoseEigen.block<2, 1>(0, 3)).norm();
95 
96  ARMARX_INFO << "for agent '" << agent->getName() << "', closest node: '"
97  << closestNode->getName() << "', dist: " << dist;
98  float selectedDistanceThreshold = armarx::Contains(agent->getName(), "human", true)
99  ? humanDistanceThreshold
100  : distanceThreshold;
101  if (dist <= selectedDistanceThreshold)
102  {
103  const CachedNodeInfo& cacheEntry = getCacheEntry(closestNode->getId());
104 
105  for (const auto& nodeRef : cacheEntry.agentAtNodeRefs)
106  {
107  ARMARX_DEBUG << "agentAt(" << agent->getName() << ", " << nodeRef->entityName
108  << ")";
109  result.push_back(PredicateInstance{predicateName, {agentRef, nodeRef}, true});
110  }
111  }
112  }
113 
114  return result;
115 }
116 
117 AgentAtPredicateProvider::CachedNodeInfo
118 AgentAtPredicateProvider::getCacheEntry(const std::string& nodeId)
119 {
120  auto it = nodeInfoCache.find(nodeId);
121  if (it != nodeInfoCache.end())
122  {
123  return it->second;
124  }
125 
126  CachedNodeInfo newEntry{nodeId, {}};
127  std::vector<std::pair<EntityRefBasePtr, bool>> agentAtCandidates;
128 
129  bool allRobotLocations = true;
130  for (const GraphNodeBasePtr& nodeBase : graphNodes)
131  {
132  GraphNodePtr node = GraphNodePtr::dynamicCast(nodeBase);
133  if (node->isMetaEntity())
134  {
135  continue;
136  }
137 
138  auto it = nodeParentsCache.find(node->getName());
139  if (it == nodeParentsCache.end())
140  {
141  it =
142  nodeParentsCache.insert({node->getName(), node->getAllParentsAsStringList()}).first;
143  }
144 
145  auto parents = it->second;
146 
147  auto n = psr->getNearestRobotLocationNode(node);
148  ARMARX_DEBUG << "to '" << node->getName() << "', closest robot location is '"
149  << n->getName() << "'";
150  if (n->getId() == nodeId)
151  {
152  // newEntry.agentAtNodeRefs.push_back(graphSegment->getEntityRefById(node->getId()));
153  bool isRobotLocation =
154  std::find(parents.cbegin(), parents.cend(), "robotlocation") != parents.cend();
155  allRobotLocations &= isRobotLocation;
156  ARMARX_DEBUG << "for query node '" << graphSegment->getEntityRefById(nodeId)->entityName
157  << "', considering node '" << node->getName() << "'";
158  agentAtCandidates.push_back(
159  {graphSegment->getEntityRefById(node->getId()), isRobotLocation});
160  }
161  }
162 
163  if (!agentAtCandidates.empty())
164  {
165  if (allRobotLocations)
166  {
167  ARMARX_DEBUG << "for query '" << nodeId << "', result is '"
168  << agentAtCandidates.front().first->entityName << "'";
169  newEntry.agentAtNodeRefs.push_back(agentAtCandidates.front().first);
170  }
171  else
172  {
173  for (const auto& entry : agentAtCandidates)
174  {
175  if (!entry.second)
176  {
177  ARMARX_DEBUG << "for query '" << nodeId << "', result is '"
178  << entry.first->entityName << "'";
179  newEntry.agentAtNodeRefs.push_back(entry.first);
180  // break; //not sure if only one agentAt should be generated or more
181  }
182  }
183  }
184  }
185 
186  nodeInfoCache.insert({nodeId, newEntry});
187  return newEntry;
188 }
memoryx::AgentAtPredicateProvider::getPredicateInfos
PredicateInfoList getPredicateInfos(const Ice::Current &c=Ice::emptyCurrent) override
Definition: AgentAtPredicateProvider.cpp:70
armarx::Contains
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition: algorithm.h:330
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
StringHelpers.h
IceInternal::Handle< GraphNode >
AgentAtPredicateProvider.h
FramedPose.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
MemoryXCoreObjectFactories.h
memoryx::AgentAtPredicateProvider::calcPredicates
PredicateInstanceList calcPredicates(const Ice::Current &) override
Definition: AgentAtPredicateProvider.cpp:76
memoryx::AgentAtPredicateProvider::onInitWorldStateUpdater
void onInitWorldStateUpdater() override
Definition: AgentAtPredicateProvider.cpp:40
ObjectInstance.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
memoryx::AgentAtPredicateProvider::AgentAtPredicateProvider
AgentAtPredicateProvider()
Definition: AgentAtPredicateProvider.cpp:35
memoryx::AgentAtPredicateProvider::onConnectWorldStateUpdater
void onConnectWorldStateUpdater() override
Definition: AgentAtPredicateProvider.cpp:51
MemoryXTypesObjectFactories.h
memoryx::AgentAtPredicateProvider::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: AgentAtPredicateProvider.cpp:64
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
norm
double norm(const Point &a)
Definition: point.hpp:102