ArmarXObjectsImporter.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::ArmarXObjects::ArmarXObjectsImporter
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ArmarXObjectsImporter.h"
24 
25 #include <unordered_set>
26 
29 
31 
33 
34 
35 namespace memoryx
36 {
37 
39  {
41 
42  def->component(proxies.commonStorage);
43  def->component(proxies.priorKnowledge);
44  def->component(proxies.workingMemory);
45  def->component(proxies.longtermMemory);
46 
47  def->optional(properties.mongoDbName, "p.MongoDbName",
48  "Name of snapshot Mongo database to load files into");
49  def->optional(properties.dryRun, "p.dryRun",
50  "If true, don't actually change anything.");
51 
52 
53  def->optional(properties.importObjects, "p.obj.00_DoImport",
54  "Whether to import objects.");
55 
56  def->optional(properties.objectsRepository, "p.obj.10_Repository",
57  "Repository such as ArmarXObjects containing the object classes.");
58 
59  def->optional(properties.datasetName, "p.obj.20_DatasetName",
60  "If set, only the given dataset in ArmarXObjects is imported.\n"
61  "Otherwise (default), all objects are imported.");
62  def->optional(properties.className, "p.obj.21_ClassName",
63  "If set, only the given dataset in ArmarXObjects is imported.\n"
64  "Otherwise (default), all objects are imported.");
65 
66 
67  def->optional(properties.importScenes, "p.scenes.00_DoImport",
68  "Whether to import scenes.");
69  def->optional(properties.scenesDirectory, "p.scenes.10_Directory",
70  "Directory containing the scene files (Scene_*.json).");
71  def->optional(properties.sceneNames, "p.scenes.20_Scenes",
72  "Scenes to load by name. Comma separated. ({scene_name}.json).");
73 
74  return def;
75  }
76 
77 
79  {
80  }
81 
82 
84  {
85  gridFileManager.reset(new memoryx::GridFileManager(proxies.commonStorage));
86 
87  memoryx::PersistentObjectClassSegmentBasePrx objectClassesSegment =
88  proxies.priorKnowledge->getObjectClassesSegment();
89 
90 
91  ArmarXObjectsToMemory importer;
92  importer.dryRun = properties.dryRun;
93 
94  if (properties.dryRun)
95  {
96  ARMARX_IMPORTANT << "Performing dry run - not going to change anything!";
97  }
98 
99  if (properties.importObjects)
100  {
101  armarx::ObjectFinder finder(properties.objectsRepository);
102  std::vector<armarx::ObjectInfo> objectInfos;
103  if (properties.datasetName.size() > 0)
104  {
105  if (properties.className.size() > 0)
106  {
107  ARMARX_INFO << "Finding " << properties.datasetName << properties.className << "...";
108  if (auto info = finder.findObject(properties.datasetName, properties.className))
109  {
110  objectInfos.push_back(info.value());
111  }
112  }
113  else
114  {
115  ARMARX_INFO << "Finding objects in dataset " << properties.datasetName << " ...";
116  objectInfos = finder.findAllObjectsOfDataset(properties.datasetName, false);
117  }
118  }
119  else
120  {
121  ARMARX_INFO << "Finding all objects ...";
122  objectInfos = finder.findAllObjects(false);
123  }
124  ARMARX_INFO << "Found " << objectInfos.size() << " object classes in " << properties.objectsRepository << ".";
125 
126  ARMARX_INFO << "Importing " << objectInfos.size() << " object classes ...";
128  objectInfos, gridFileManager, objectClassesSegment, properties.mongoDbName);
129  }
130 
131  if (properties.importScenes)
132  {
133  const std::filesystem::path scenesDir = armarx::ArmarXDataPath::resolvePath(properties.scenesDirectory);
134  std::unordered_set<std::string> scenes;
135  if (not properties.sceneNames.empty())
136  {
137  bool trim = true, removeEmpty = true;
138  const auto scenesTmp = armarx::split(properties.sceneNames, ",", trim, removeEmpty);
139  ARMARX_IMPORTANT << properties.sceneNames << "\n" << scenesTmp;
140  scenes = std::unordered_set<std::string> (scenesTmp.begin(), scenesTmp.end());
141  }
142 
143  if (not scenes.empty())
144  {
145  ARMARX_INFO << "The following scenes will be loaded (if available): " << std::vector<std::string>(scenes.begin(), scenes.end());
146  }
147 
148  ARMARX_INFO << "Importing scenes in " << scenesDir;
149  importer.importScenesAsSnapshots(
150  scenesDir, proxies.longtermMemory, proxies.workingMemory, objectClassesSegment, scenes);
151  }
152  }
153 
154 
156  {
157  }
158 
159 
161  {
162  }
163 
164 
166  {
167  return "ArmarXObjectsImporter";
168  }
169 
170  ArmarXObjectsImporter::Properties::Properties() :
171  objectsRepository(armarx::ObjectFinder::DefaultObjectsPackageName),
172  scenesDirectory(armarx::ObjectFinder::DefaultObjectsPackageName + "/scenes")
173  {
174  }
175 
176 }
armarx::ArmarXDataPath::resolvePath
static std::string resolvePath(const std::string &path, bool verbose=true)
Resolves environment variables and home paths and tries to make path absolute.
Definition: ArmarXDataPath.cpp:542
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::ObjectFinder::findAllObjects
std::vector< ObjectInfo > findAllObjects(bool checkPaths=true) const
Definition: ObjectFinder.cpp:144
memoryx::ArmarXObjectsImporter::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ArmarXObjectsImporter.cpp:38
memoryx::ArmarXObjectsToMemory::dryRun
bool dryRun
If true, don't actually change anything.
Definition: ArmarXObjectsToMemory.h:92
memoryx::ArmarXObjectsToMemory
Definition: ArmarXObjectsToMemory.h:40
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
ArmarXObjectsToMemory.h
StringHelpers.h
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:23
armarx::ObjectFinder::findAllObjectsOfDataset
std::vector< ObjectInfo > findAllObjectsOfDataset(const std::string &dataset, bool checkPaths=true) const
Definition: ObjectFinder.cpp:201
memoryx::ArmarXObjectsImporter::onDisconnectComponent
void onDisconnectComponent() override
Definition: ArmarXObjectsImporter.cpp:155
memoryx::ArmarXObjectsImporter::onConnectComponent
void onConnectComponent() override
Definition: ArmarXObjectsImporter.cpp:83
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ArmarXObjectsImporter.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
memoryx::ArmarXObjectsToMemory::importObjectsToPriorKnowledge
void importObjectsToPriorKnowledge(std::vector< armarx::ObjectInfo > const &objectInfos, memoryx::GridFileManagerPtr const &gridFileManager, memoryx::PersistentObjectClassSegmentBasePrx const &objectClassesSegment, std::string const &mongoDbName) const
Definition: ArmarXObjectsToMemory.cpp:59
armarx::ObjectFinder::findObject
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Definition: ObjectFinder.cpp:65
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:42
memoryx::ArmarXObjectsImporter::onExitComponent
void onExitComponent() override
Definition: ArmarXObjectsImporter.cpp:160
memoryx::ArmarXObjectsImporter::getDefaultName
std::string getDefaultName() const override
Definition: ArmarXObjectsImporter.cpp:165
ArmarXDataPath.h
memoryx::ArmarXObjectsToMemory::importScenesAsSnapshots
void importScenesAsSnapshots(std::filesystem::path const &scenesDirectory, memoryx::LongtermMemoryInterfacePrx const &longtermMemory, memoryx::WorkingMemoryInterfacePrx const &workingMemory, memoryx::PersistentObjectClassSegmentBasePrx const &objectClassesSegment, const std::unordered_set< std::string > &scenes) const
Definition: ArmarXObjectsToMemory.cpp:114
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ObjectFinder.h
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36
memoryx::ArmarXObjectsImporter::onInitComponent
void onInitComponent() override
Definition: ArmarXObjectsImporter.cpp:78