PriorAttributeEnrichmentFusion.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::Core
17 * @author Alexey Kozlov <kozlov@kit.edu>
18 * @copyright 2012 Alexey Kozlov
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
24 
26 
28 
29 
30 namespace memoryx
31 {
32 
33  PriorAttributeEnrichmentFusion::PriorAttributeEnrichmentFusion(const PriorKnowledgeInterfacePrx& priorKnowledgePrx, bool forceOverwrite) :
34  EntityFusionMethod("PriorAttributeEnrichmentFusion"), priorKnowledgePrx(priorKnowledgePrx), forceOverwrite(forceOverwrite)
35  {
36  }
37 
38  void PriorAttributeEnrichmentFusion::initEntityInPlace(const EntityBasePtr& updateEntity)
39  {
40  if (!priorKnowledgePrx)
41  {
42  return;
43  }
44 
45  addPriorAttrs(updateEntity);
46  }
47 
48  EntityBasePtr PriorAttributeEnrichmentFusion::initEntity(const EntityBasePtr& updateEntity, const Ice::Current&)
49  {
50  if (!priorKnowledgePrx)
51  {
52  return updateEntity;
53  }
54 
55  EntityBasePtr fusedEntity = EntityBasePtr::dynamicCast(updateEntity->ice_clone());
56  addPriorAttrs(fusedEntity);
57 
58  return fusedEntity;
59  }
60 
61  EntityBasePtr PriorAttributeEnrichmentFusion::fuseEntity(const EntityBasePtr& oldEntity, const EntityBasePtr& newEntity, const Ice::Current&)
62  {
63  ARMARX_DEBUG_S << "PriorAttributeEnrichmentFusion::fuseEntity() called";
64 
65  copyMissingAttrs(oldEntity, newEntity);
66  addPriorAttrs(newEntity);
67 
68  return newEntity;
69  }
70 
71  void PriorAttributeEnrichmentFusion::addPriorAttrs(const EntityBasePtr& entity) const
72  {
73  ObjectInstancePtr inst = ObjectInstancePtr::dynamicCast(entity);
74 
75  if (inst)
76  {
77  std::string classname = inst->getMostProbableClass();
78  //ARMARX_INFO_S << entity->getName() << " has most probable class: " << classname;
79  if (!classname.empty())
80  {
81  EntityBasePtr priorEntity = priorKnowledgePrx->getObjectClassesSegment()->getEntityByName(classname);
82 
83  if (priorEntity)
84  {
85  copyMissingAttrs(priorEntity, entity);
86  }
87  }
88  else
89  {
90  ARMARX_VERBOSE << "No classname set - could not enrich entity";
91  }
92  }
93  }
94 
95  void PriorAttributeEnrichmentFusion::copyMissingAttrs(const EntityBasePtr& srcEntity, const EntityBasePtr& destEntity) const
96  {
97  NameList attrNames = srcEntity->getAttributeNames();
98 
99  for (NameList::const_iterator it = attrNames.begin(); it != attrNames.end(); ++it)
100  if (forceOverwrite || !destEntity->hasAttribute(*it))
101  {
102  ARMARX_VERBOSE << "Adding attribute: " << srcEntity->getAttribute(*it)->getName();
103  destEntity->putAttribute(srcEntity->getAttribute(*it));
104  }
105  }
106 
107 }
108 
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
memoryx::PriorAttributeEnrichmentFusion::initEntity
EntityBasePtr initEntity(const EntityBasePtr &updateEntity, const ::Ice::Current &=Ice::emptyCurrent) override
This method should be called each time a new entity (currently: object instance) is created and perfo...
Definition: PriorAttributeEnrichmentFusion.cpp:48
PriorAttributeEnrichmentFusion.h
memoryx::PriorAttributeEnrichmentFusion::initEntityInPlace
void initEntityInPlace(const EntityBasePtr &updateEntity)
Definition: PriorAttributeEnrichmentFusion.cpp:38
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::PriorAttributeEnrichmentFusion::fuseEntity
EntityBasePtr fuseEntity(const EntityBasePtr &oldEntity, const EntityBasePtr &newEntity, const ::Ice::Current &) override
This method should be called each time an existing entity gets updated.
Definition: PriorAttributeEnrichmentFusion.cpp:61
memoryx::EntityFusionMethod
Interface for fusion methods used for entities in working memory.
Definition: EntityFusionMethod.h:43
IceInternal::Handle< ObjectInstance >
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
ObjectInstance.h
Logging.h
memoryx::PriorAttributeEnrichmentFusion::PriorAttributeEnrichmentFusion
PriorAttributeEnrichmentFusion(const PriorKnowledgeInterfacePrx &priorKnowledgePrx, bool forceOverwrite=false)
Creates a new PriorAttributeEnrichmentFusion.
Definition: PriorAttributeEnrichmentFusion.cpp:33