45 objectInstances = wm->getObjectInstancesSegment();
46 objectClasses = prior->getObjectClassesSegment();
47 objectRelations = wm->getRelationsSegment();
53 PredicateInstanceList worldState;
54 observablePredicateInstances.clear();
56 std::unique_lock lock(updaterMutex);
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();
61 for (
const auto& pair : updaters)
63 PredicateInstanceList allowedPredicates;
65 ARMARX_INFO <<
"calculating predicates of '" << pair.first <<
"'";
68 for (
const PredicateInstance& predicate : pair.second->calcPredicates())
70 if (areAllowed(predicate.argValues))
72 allowedPredicates.push_back(predicate);
81 addListToList(worldState, allowedPredicates);
86 addListToList(worldState, getNonobservableRelationsAndPredicates());
93 auto it = std::find_if(updaters.cbegin(), updaters.cend(), [&](
const std::pair<std::string, WorldStateUpdaterInterfacePrx>& arg)
95 for (const auto& predicateInfo : arg.second->getPredicateInfos())
97 if (predicateInfo.name == predicateName)
105 return it != updaters.end();
108 bool WorldStateObserver::updatePredicateValue(
const PredicateInstance&
pi,
bool removePredicate,
const Ice::Current&)
110 memoryx::RelationBasePtr relation = objectRelations->getRelationByAttrValues(
pi.name,
pi.argValues,
pi.sign);
117 objectRelations->removeEntity(relation->getId());
121 ARMARX_INFO <<
"Trying to remove the relation " <<
pi.name <<
", but it doesn't exist";
130 memoryx::RelationBasePtr relation =
new Relation(
pi.name,
pi.argValues,
pi.sign);
131 objectRelations->addEntity(relation);
135 ARMARX_VERBOSE <<
"Trying to add the relation " << relation->getName() <<
", but it is already there";
143 void WorldStateObserver::setPredicateArgumentWhitelist(
const EntityBaseList& argumentWhitelist,
const Ice::Current&)
145 this->argumentWhitelist = argumentWhitelist;
148 void WorldStateObserver::resetPredicateArgumentWhitelist(
const Ice::Current&)
150 argumentWhitelist.clear();
153 bool WorldStateObserver::areAllowed(
const std::vector<EntityRefBasePtr>& entityRefs)
155 if (argumentWhitelist.empty())
160 for (
const auto& ref : entityRefs)
162 auto id = ref->entityId;
164 if (
auto instance = ObjectInstancePtr::dynamicCast(ref->getEntity()))
166 id = objectClasses->getEntityByName(instance->getMostProbableClass())->getId();
169 bool whitelisted = std::find_if(argumentWhitelist.cbegin(), argumentWhitelist.cend(), [&](
const EntityBasePtr & entity)
171 return entity->getId() == id;
172 }) != argumentWhitelist.cend();
183 void WorldStateObserver::addListToList(PredicateInstanceList&
target,
const PredicateInstanceList&
source)
186 for (
size_t i = 0; i <
source.size(); i++)
188 std::string entityNames;
190 for (
const auto& entity :
source.at(i).argValues)
192 entityNames += entity->entityName +
", ";
195 ARMARX_DEBUG <<
"adding to target: " <<
source.at(i).name <<
" args: " << entityNames;
200 PredicateInstanceList WorldStateObserver::getNonobservableRelationsAndPredicates()
202 PredicateInstanceList returnPIList;
204 EntityIdList allEntityIDs = objectRelations->getAllEntityIds();
205 ARMARX_VERBOSE <<
"Adding " << allEntityIDs.size() <<
" relations from the relations segment to the world state";
207 for (
const std::string&
id : allEntityIDs)
209 const RelationBasePtr relation = objectRelations->getRelationById(
id);
211 returnPIList.push_back(PredicateInstance {relation->getName(), relation->getEntities(), relation->getSign()});
219 observablePredicateInstances.insert(observablePredicateInstances.end(), predicates.begin(), predicates.end());
225 std::unique_lock lock(updaterMutex);
227 if (updaters.find(name) != updaters.end())
229 throw armarx::LocalException() <<
"Updater with name " << name <<
" already registered.";
232 updaters.insert(std::make_pair(name, updater));
237 WorldStateUpdaterInterfaceList result;
239 for (
const auto& updater : updaters)
241 result.push_back(updater.second);