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
24
26
27#include <unordered_set>
28
31
33
35
36namespace memoryx
37{
38
41 {
44
45 def->component(proxies.commonStorage);
46 def->component(proxies.priorKnowledge);
47 def->component(proxies.workingMemory);
48 def->component(proxies.longtermMemory);
49
50 def->optional(properties.mongoDbName,
51 "p.MongoDbName",
52 "Name of snapshot Mongo database to load files into");
53 def->optional(properties.dryRun, "p.dryRun", "If true, don't actually change anything.");
54
55
56 def->optional(properties.importObjects, "p.obj.00_DoImport", "Whether to import objects.");
57
58 def->optional(properties.objectsRepository,
59 "p.obj.10_Repository",
60 "Repository such as ArmarXObjects containing the object classes.");
61
62 def->optional(properties.datasetName,
63 "p.obj.20_DatasetName",
64 "If set, only the given dataset in ArmarXObjects is imported.\n"
65 "Otherwise (default), all objects are imported.");
66 def->optional(properties.className,
67 "p.obj.21_ClassName",
68 "If set, only the given dataset in ArmarXObjects is imported.\n"
69 "Otherwise (default), all objects are imported.");
70
71
72 def->optional(properties.importScenes, "p.scenes.00_DoImport", "Whether to import scenes.");
73 def->optional(properties.scenesDirectory,
74 "p.scenes.10_Directory",
75 "Directory containing the scene files (Scene_*.json).");
76 def->optional(properties.sceneNames,
77 "p.scenes.20_Scenes",
78 "Scenes to load by name. Comma separated. ({scene_name}.json).");
79
80 return def;
81 }
82
83 void
87
88 void
90 {
91 gridFileManager.reset(new memoryx::GridFileManager(proxies.commonStorage));
92
93 memoryx::PersistentObjectClassSegmentBasePrx objectClassesSegment =
94 proxies.priorKnowledge->getObjectClassesSegment();
95
96
97 ArmarXObjectsToMemory importer;
98 importer.dryRun = properties.dryRun;
99
100 if (properties.dryRun)
101 {
102 ARMARX_IMPORTANT << "Performing dry run - not going to change anything!";
103 }
104
105 if (properties.importObjects)
106 {
107 armarx::ObjectFinder finder(properties.objectsRepository);
108 std::vector<armarx::ObjectInfo> objectInfos;
109 if (properties.datasetName.size() > 0)
110 {
111 if (properties.className.size() > 0)
112 {
113 ARMARX_INFO << "Finding " << properties.datasetName << properties.className
114 << "...";
115 if (auto info = finder.findObject(properties.datasetName, properties.className))
116 {
117 objectInfos.push_back(info.value());
118 }
119 }
120 else
121 {
122 ARMARX_INFO << "Finding objects in dataset " << properties.datasetName
123 << " ...";
124 objectInfos = finder.findAllObjectsOfDataset(properties.datasetName, false);
125 }
126 }
127 else
128 {
129 ARMARX_INFO << "Finding all objects ...";
130 objectInfos = finder.findAllObjects(false);
131 }
132 ARMARX_INFO << "Found " << objectInfos.size() << " object classes in "
133 << properties.objectsRepository << ".";
134
135 ARMARX_INFO << "Importing " << objectInfos.size() << " object classes ...";
137 objectInfos, gridFileManager, objectClassesSegment, properties.mongoDbName);
138 }
139
140 if (properties.importScenes)
141 {
142 const std::filesystem::path scenesDir =
143 armarx::ArmarXDataPath::resolvePath(properties.scenesDirectory);
144 std::unordered_set<std::string> scenes;
145 if (not properties.sceneNames.empty())
146 {
147 bool trim = true, removeEmpty = true;
148 const auto scenesTmp = armarx::split(properties.sceneNames, ",", trim, removeEmpty);
149 ARMARX_IMPORTANT << properties.sceneNames << "\n" << scenesTmp;
150 scenes = std::unordered_set<std::string>(scenesTmp.begin(), scenesTmp.end());
151 }
152
153 if (not scenes.empty())
154 {
155 ARMARX_INFO << "The following scenes will be loaded (if available): "
156 << std::vector<std::string>(scenes.begin(), scenes.end());
157 }
158
159 ARMARX_INFO << "Importing scenes in " << scenesDir;
160 importer.importScenesAsSnapshots(scenesDir,
161 proxies.longtermMemory,
162 proxies.workingMemory,
163 objectClassesSegment,
164 scenes);
165 }
166 }
167
168 void
172
173 void
177
178 std::string
180 {
181 return "ArmarXObjectsImporter";
182 }
183
184 std::string
186 {
187 return "ArmarXObjectsImporter";
188 }
189
190 ArmarXObjectsImporter::Properties::Properties() :
191 objectsRepository(armarx::ObjectFinder::DefaultObjectsPackageName),
192 scenesDirectory(armarx::ObjectFinder::DefaultObjectsPackageName + "/scenes")
193 {
194 }
195
196} // namespace memoryx
197
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
static std::string resolvePath(const std::string &path, bool verbose=true)
Resolves environment variables and home paths and tries to make path absolute.
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
std::vector< ObjectInfo > findAllObjects(bool checkPaths=true) const
std::vector< ObjectInfo > findAllObjectsOfDataset(const std::string &dataset, bool checkPaths=true) const
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Brief description of class ArmarXObjectsImporter.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
std::string getDefaultName() const override
void importObjectsToPriorKnowledge(std::vector< armarx::ObjectInfo > const &objectInfos, memoryx::GridFileManagerPtr const &gridFileManager, memoryx::PersistentObjectClassSegmentBasePrx const &objectClassesSegment, std::string const &mongoDbName) const
bool dryRun
If true, don't actually change anything.
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
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
VirtualRobot headers.