WorldStateObserver.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::WorldStateObserver
17 * @author David Schiebener ( schiebener 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 
23 #include "WorldStateObserver.h"
24 
30 
31 namespace memoryx
32 {
34  {
35  usingProxy("WorkingMemory");
36  usingProxy("PriorKnowledge");
37  // usingProxy("HandUnitObserver");
38  }
39 
41  {
42  getProxy(wm, "WorkingMemory");
43  getProxy(prior, "PriorKnowledge");
44 
45  objectInstances = wm->getObjectInstancesSegment();
46  objectClasses = prior->getObjectClassesSegment();
47  objectRelations = wm->getRelationsSegment();
48  }
49 
50  memoryx::PredicateInstanceList WorldStateObserver::getWorldState(const Ice::Current&)
51  {
52 
53  PredicateInstanceList worldState;
54  observablePredicateInstances.clear();
55  {
56  std::unique_lock lock(updaterMutex);
57  //std::vector<Ice::AsyncResultPtr> results;
58  auto start = IceUtil::Time::now().toMilliSeconds();
59 #define TM(msg) ARMARX_VERBOSE << msg << ": " << (IceUtil::Time::now().toMilliSeconds() - start) << "ms"; start = IceUtil::Time::now().toMilliSeconds();
60 
61  for (const auto& pair : updaters)
62  {
63  PredicateInstanceList allowedPredicates;
64 
65  ARMARX_INFO << "calculating predicates of '" << pair.first << "'";
66  try
67  {
68  for (const PredicateInstance& predicate : pair.second->calcPredicates())
69  {
70  if (areAllowed(predicate.argValues))
71  {
72  allowedPredicates.push_back(predicate);
73  }
74  }
75  }
76  catch (...)
77  {
79  }
80 
81  addListToList(worldState, allowedPredicates);
82  TM(pair.first)
83  }
84  }
85 
86  addListToList(worldState, getNonobservableRelationsAndPredicates());
87 
88  return worldState;
89  }
90 
91  bool WorldStateObserver::isObservable(const std::string& predicateName, const Ice::Current&)
92  {
93  auto it = std::find_if(updaters.cbegin(), updaters.cend(), [&](const std::pair<std::string, WorldStateUpdaterInterfacePrx>& arg)
94  {
95  for (const auto& predicateInfo : arg.second->getPredicateInfos())
96  {
97  if (predicateInfo.name == predicateName)
98  {
99  return true;
100  }
101  }
102 
103  return false;
104  });
105  return it != updaters.end();
106  }
107 
108  bool WorldStateObserver::updatePredicateValue(const PredicateInstance& pi, bool removePredicate, const Ice::Current&)
109  {
110  memoryx::RelationBasePtr relation = objectRelations->getRelationByAttrValues(pi.name, pi.argValues, pi.sign);
111 
112  if (removePredicate)
113  {
114  if (relation)
115  {
116  ARMARX_VERBOSE << "Removing relation " << relation->getName();
117  objectRelations->removeEntity(relation->getId());
118  }
119  else
120  {
121  ARMARX_INFO << "Trying to remove the relation " << pi.name << ", but it doesn't exist";
122  return false;
123  }
124  }
125  else
126  {
127  if (!relation)
128  {
129  ARMARX_VERBOSE << "Adding relation " << pi.name;
130  memoryx::RelationBasePtr relation = new Relation(pi.name, pi.argValues, pi.sign);
131  objectRelations->addEntity(relation);
132  }
133  else
134  {
135  ARMARX_VERBOSE << "Trying to add the relation " << relation->getName() << ", but it is already there";
136  return false;
137  }
138  }
139 
140  return true;
141  }
142 
143  void WorldStateObserver::setPredicateArgumentWhitelist(const EntityBaseList& argumentWhitelist, const Ice::Current&)
144  {
145  this->argumentWhitelist = argumentWhitelist;
146  }
147 
148  void WorldStateObserver::resetPredicateArgumentWhitelist(const Ice::Current&)
149  {
150  argumentWhitelist.clear();
151  }
152 
153  bool WorldStateObserver::areAllowed(const std::vector<EntityRefBasePtr>& entityRefs)
154  {
155  if (argumentWhitelist.empty())
156  {
157  return true;
158  }
159 
160  for (const auto& ref : entityRefs)
161  {
162  auto id = ref->entityId;
163 
164  if (auto instance = ObjectInstancePtr::dynamicCast(ref->getEntity()))
165  {
166  id = objectClasses->getEntityByName(instance->getMostProbableClass())->getId();
167  }
168 
169  bool whitelisted = std::find_if(argumentWhitelist.cbegin(), argumentWhitelist.cend(), [&](const EntityBasePtr & entity)
170  {
171  return entity->getId() == id;
172  }) != argumentWhitelist.cend();
173 
174  if (!whitelisted)
175  {
176  return false;
177  }
178  }
179 
180  return true;
181  }
182 
183  void WorldStateObserver::addListToList(PredicateInstanceList& target, const PredicateInstanceList& source)
184  {
185  // ARMARX_VERBOSE << "target list: " << target;
186  for (size_t i = 0; i < source.size(); i++)
187  {
188  std::string entityNames;
189 
190  for (const auto& entity : source.at(i).argValues)
191  {
192  entityNames += entity->entityName + ", ";
193  }
194 
195  ARMARX_DEBUG << "adding to target: " << source.at(i).name << " args: " << entityNames;
196  target.push_back(source.at(i));
197  }
198  }
199 
200  PredicateInstanceList WorldStateObserver::getNonobservableRelationsAndPredicates()
201  {
202  PredicateInstanceList returnPIList;
203 
204  EntityIdList allEntityIDs = objectRelations->getAllEntityIds();
205  ARMARX_VERBOSE << "Adding " << allEntityIDs.size() << " relations from the relations segment to the world state";
206 
207  for (const std::string& id : allEntityIDs)
208  {
209  const RelationBasePtr relation = objectRelations->getRelationById(id);
210 
211  returnPIList.push_back(PredicateInstance {relation->getName(), relation->getEntities(), relation->getSign()});
212  }
213 
214  return returnPIList;
215  }
216 
217  void memoryx::WorldStateObserver::addObservablePredicateInstances(const PredicateInstanceList& predicates, const Ice::Current&)
218  {
219  observablePredicateInstances.insert(observablePredicateInstances.end(), predicates.begin(), predicates.end());
220  }
221 
222 
223  void memoryx::WorldStateObserver::registerAsUpdater(const std::string& name, const WorldStateUpdaterInterfacePrx& updater, const Ice::Current&)
224  {
225  std::unique_lock lock(updaterMutex);
226 
227  if (updaters.find(name) != updaters.end())
228  {
229  throw armarx::LocalException() << "Updater with name " << name << " already registered.";
230  }
231 
232  updaters.insert(std::make_pair(name, updater));
233  }
234 
235  WorldStateUpdaterInterfaceList memoryx::WorldStateObserver::getRegisteredUpdaters(const Ice::Current&)
236  {
237  WorldStateUpdaterInterfaceList result;
238 
239  for (const auto& updater : updaters)
240  {
241  result.push_back(updater.second);
242  }
243 
244  return result;
245  }
246 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
memoryx::WorldStateObserver::addObservablePredicateInstances
void addObservablePredicateInstances(const PredicateInstanceList &predicates, const Ice::Current &) override
Definition: WorldStateObserver.cpp:217
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
memoryx::Relation
Relation class represents a directed relation between entities.
Definition: Relation.h:42
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::WorldStateObserver::registerAsUpdater
void registerAsUpdater(const std::string &name, const WorldStateUpdaterInterfacePrx &updater, const Ice::Current &) override
Definition: WorldStateObserver.cpp:223
memoryx::WorldStateObserver::isObservable
bool isObservable(const ::std::string &predicateName, const ::Ice::Current &) override
Definition: WorldStateObserver.cpp:91
pi
#define pi
Definition: Transition.cpp:37
Relation.h
EntityRef.h
memoryx::WorldStateObserver::getWorldState
memoryx::PredicateInstanceList getWorldState(const ::Ice::Current &) override
Definition: WorldStateObserver.cpp:50
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
memoryx::WorldStateObserver::getRegisteredUpdaters
WorldStateUpdaterInterfaceList getRegisteredUpdaters(const Ice::Current &) override
Definition: WorldStateObserver.cpp:235
WorldStateObserver.h
MemoryXCoreObjectFactories.h
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
memoryx::WorldStateObserver::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: WorldStateObserver.cpp:40
ObjectInstance.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
TM
#define TM(msg)
MemoryXTypesObjectFactories.h
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
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::WorldStateObserver::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: WorldStateObserver.cpp:33
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