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