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