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
29namespace memoryx
30{
31
33 const PriorKnowledgeInterfacePrx& priorKnowledgePrx,
34 bool forceOverwrite) :
35 EntityFusionMethod("PriorAttributeEnrichmentFusion"),
36 priorKnowledgePrx(priorKnowledgePrx),
37 forceOverwrite(forceOverwrite)
38 {
39 }
40
41 void
42 PriorAttributeEnrichmentFusion::initEntityInPlace(const EntityBasePtr& updateEntity)
43 {
44 if (!priorKnowledgePrx)
45 {
46 return;
47 }
48
49 addPriorAttrs(updateEntity);
50 }
51
52 EntityBasePtr
53 PriorAttributeEnrichmentFusion::initEntity(const EntityBasePtr& updateEntity,
54 const Ice::Current&)
55 {
56 if (!priorKnowledgePrx)
57 {
58 return updateEntity;
59 }
60
61 EntityBasePtr fusedEntity = EntityBasePtr::dynamicCast(updateEntity->ice_clone());
62 addPriorAttrs(fusedEntity);
63
64 return fusedEntity;
65 }
66
67 EntityBasePtr
68 PriorAttributeEnrichmentFusion::fuseEntity(const EntityBasePtr& oldEntity,
69 const EntityBasePtr& newEntity,
70 const Ice::Current&)
71 {
72 ARMARX_DEBUG_S << "PriorAttributeEnrichmentFusion::fuseEntity() called";
73
74 copyMissingAttrs(oldEntity, newEntity);
75 addPriorAttrs(newEntity);
76
77 return newEntity;
78 }
79
80 void
81 PriorAttributeEnrichmentFusion::addPriorAttrs(const EntityBasePtr& entity) const
82 {
83 ObjectInstancePtr inst = ObjectInstancePtr::dynamicCast(entity);
84
85 if (inst)
86 {
87 std::string classname = inst->getMostProbableClass();
88 //ARMARX_INFO_S << entity->getName() << " has most probable class: " << classname;
89 if (!classname.empty())
90 {
91 EntityBasePtr priorEntity =
92 priorKnowledgePrx->getObjectClassesSegment()->getEntityByName(classname);
93
94 if (priorEntity)
95 {
96 copyMissingAttrs(priorEntity, entity);
97 }
98 }
99 else
100 {
101 ARMARX_VERBOSE << "No classname set - could not enrich entity";
102 }
103 }
104 }
105
106 void
107 PriorAttributeEnrichmentFusion::copyMissingAttrs(const EntityBasePtr& srcEntity,
108 const EntityBasePtr& destEntity) const
109 {
110 NameList attrNames = srcEntity->getAttributeNames();
111
112 for (NameList::const_iterator it = attrNames.begin(); it != attrNames.end(); ++it)
113 if (forceOverwrite || !destEntity->hasAttribute(*it))
114 {
115 ARMARX_VERBOSE << "Adding attribute: " << srcEntity->getAttribute(*it)->getName();
116 destEntity->putAttribute(srcEntity->getAttribute(*it));
117 }
118 }
119
120} // namespace memoryx
EntityFusionMethod(std::string methodName)
Constructs a new fusion method.
PriorAttributeEnrichmentFusion(const PriorKnowledgeInterfacePrx &priorKnowledgePrx, bool forceOverwrite=false)
Creates a new PriorAttributeEnrichmentFusion.
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...
void initEntityInPlace(const EntityBasePtr &updateEntity)
EntityBasePtr fuseEntity(const EntityBasePtr &oldEntity, const EntityBasePtr &newEntity, const ::Ice::Current &) override
This method should be called each time an existing entity gets updated.
#define ARMARX_DEBUG_S
The logging level for output that is only interesting while debugging.
Definition Logging.h:205
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
VirtualRobot headers.
IceInternal::Handle< ObjectInstance > ObjectInstancePtr