PriorKnowledgeImporter.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::PriorKnowledgeEditor
17 * @author Alexey Kozlov (kozlov at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "PriorKnowledgeImporter.h"
24 
26 
34 
35 #include <filesystem>
36 #include <fstream>
37 
38 namespace memoryx
39 {
41  {
42  usingProxy("PriorKnowledge");
43  usingProxy("CommonStorage");
44  }
45 
47  {
48  ARMARX_LOG << "Starting PriorKnowledgeImporter";
49 
50  dataBasePrx = getProxy<CommonStorageInterfacePrx>("CommonStorage");
51  memoryPrx = getProxy<PriorKnowledgeInterfacePrx>("PriorKnowledge");
52  classesSegmentPrx = memoryPrx->getObjectClassesSegment();
53 
54  const Ice::CommunicatorPtr ic = getIceManager()->getCommunicator();
55 
56  dbSerializer = new MongoSerializer(ic, true);
57  fileManager.reset(new GridFileManager(dataBasePrx));
58 
59  filesDBName = getProperty<std::string>("FilesDbName").getValue();
60  filesDir = getProperty<std::string>("FilesDirectory").getValue();
61 
62  const std::string taskName = getProperty<std::string>("TaskName").getValue();
63 
64  if (taskName == "ImportFiles")
65  {
66  importFiles();
67  }
68  else if (taskName == "AddParents")
69  {
70  addParents();
71  }
72  else if (taskName == "ConvertFormat")
73  {
74  convertToNewFormat();
75  }
76  else if (taskName == "AddRelations")
77  {
78  addRelations();
79  }
80  else
81  {
82  ARMARX_ERROR << "Unknown TaskName: " << taskName;
83  }
84  }
85 
86 
87  void PriorKnowledgeImporter::importFiles()
88  {
89  ARMARX_INFO << "Importing object models from " << filesDir;
90 
91  namespace fs = std::filesystem;
92  fs::path ivDir(filesDir);
93  fs::directory_iterator end_iter;
94 
95  int fileCount = 0;
96 
97  if (!fs::exists(ivDir) || !fs::is_directory(ivDir))
98  {
99  return;
100  }
101 
102  for (fs::directory_iterator dir_iter(ivDir) ; dir_iter != end_iter ; ++dir_iter)
103  {
104  if (fs::is_regular_file(dir_iter->status()) && (dir_iter->path().extension() == ".iv" || dir_iter->path().extension() == ".wrl"))
105  {
106  fs::path ivFile = dir_iter->path();
107  // use filename without .iv as className
108  importObjectClass(ivFile.string(), ivFile.stem().string());
109  fileCount++;
110  }
111  }
112 
113  ARMARX_INFO << "Import complete, # of loaded files: " << fileCount;
114  }
115 
116 
117  void PriorKnowledgeImporter::importObjectClass(const std::string& ivFile, const std::string& className)
118  {
119  ObjectClassPtr newClass = new ObjectClass();
120  newClass->setName(className);
121 
122  EntityWrappers::SimoxObjectWrapperPtr simoxWrapper = newClass->addWrapper(new EntityWrappers::SimoxObjectWrapper(fileManager));
123  ARMARX_INFO << "Store file: " << ivFile;
124  simoxWrapper->setAndStoreModelIVFiles(ivFile, ivFile, filesDBName);
125 
126  EntityBasePtr ent = classesSegmentPrx->getEntityByName(newClass->getName());
127 
128  if (ent)
129  {
130  ARMARX_IMPORTANT << "Updating existing entity " << ent->getName() << "(Id: " << ent->getId() << ")";
131  classesSegmentPrx->updateEntity(ent->getId(), ent);
132  }
133  else
134  {
135  classesSegmentPrx->addEntity(newClass);
136  }
137  }
138 
139 
140  void PriorKnowledgeImporter::addParents()
141  {
142  EntityIdList ids = classesSegmentPrx->getAllEntityIds();
143 
144  for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
145  {
146  ObjectClassBasePtr cls = classesSegmentPrx->getObjectClassById(*it);
147 
148  if (cls)
149  {
150  cls->clearParentClasses();
151  cls->addParentClass("Entity");
152 
153  if (cls->getName() == "Cup")
154  {
155  cls->addParentClass("Dishes");
156  }
157  else if (cls->getName() != "coffeefilters")
158  {
159  cls->addParentClass("Food");
160  }
161 
162  classesSegmentPrx->updateEntity(cls->getId(), cls);
163  }
164  }
165  }
166 
167  void PriorKnowledgeImporter::convertToNewFormat()
168  {
169  const std::string collNS = classesSegmentPrx->getWriteCollectionNS();
170  CollectionInterfacePrx coll = dataBasePrx->requestCollection(collNS);
171  EntityIdList ids = classesSegmentPrx->getAllEntityIds();
172 
173  int countConverted = 0, countFailed = 0;
174 
175  for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
176  {
177  ObjectClassPtr cls = new ObjectClass();
178  DBStorableData dbEntity = coll->findByMongoId(*it);
179  dbSerializer->deserialize(dbEntity, cls);
180 
181  if (cls)
182  {
183  classesSegmentPrx->updateEntity(cls->getId(), cls);
184  ++countConverted;
185  }
186  else
187  {
188  ARMARX_ERROR << "Error deserializing entity: id =" << *it << ", json: " << dbEntity.JSON << armarx::flush;
189  ++countFailed;
190  }
191  }
192 
193  ARMARX_INFO << "Conversion completed. " << armarx::flush;
194  ARMARX_INFO << "Entities converted: " << countConverted << ", failed: " << countFailed << armarx::flush;
195  }
196 
197  void PriorKnowledgeImporter::addRelations()
198  {
199  PersistentRelationSegmentBasePrx relSegmentPrx = memoryPrx->getRelationsSegment();
200  RelationPtr rel = new Relation("isOn");
201  EntityRefBasePtr tableRef = classesSegmentPrx->getEntityRefByName("TableWithRolls");
202  EntityRefBasePtr cupRef = classesSegmentPrx->getEntityRefByName("Cup");
203 
204  if (tableRef && cupRef)
205  {
206  rel->setEntities({cupRef, tableRef});
207  rel->setProb(0.75);
208  relSegmentPrx->addEntity(rel);
209  ARMARX_INFO << "Relation saved: " << rel << armarx::flush;
210  }
211  else
212  {
213  ARMARX_ERROR << "Failed to create relation: object class(es) not found!" << armarx::flush;
214  }
215  }
216 
217 
218 }
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:353
cyberglove_with_calib_22dof.ic
ic
Definition: cyberglove_with_calib_22dof.py:22
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
JSONObject.h
MongoDBRef.h
memoryx::PriorKnowledgeImporter::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: PriorKnowledgeImporter.cpp:40
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
ObjectClass.h
memoryx::RelationPtr
IceInternal::Handle< Relation > RelationPtr
Definition: Relation.h:35
Relation.h
IceInternal::Handle< ::Ice::Communicator >
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
MemoryXCoreObjectFactories.h
memoryx::ObjectClass
Definition: ObjectClass.h:37
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:163
memoryx::MongoSerializer
Definition: MongoSerializer.h:40
SimoxObjectWrapper.h
ObjectInstance.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:42
MemoryXTypesObjectFactories.h
memoryx::ObjectClassPtr
IceInternal::Handle< ObjectClass > ObjectClassPtr
Definition: ObjectClass.h:35
memoryx::PriorKnowledgeImporter::importObjectClass
void importObjectClass(const std::string &ivFile, const std::string &className)
Definition: PriorKnowledgeImporter.cpp:117
PriorKnowledgeImporter.h
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
memoryx::PriorKnowledgeImporter::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: PriorKnowledgeImporter.cpp:46