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
25#include <unordered_set>
26
29
31
33
34namespace memoryx
35{
36
39 {
42
43 def->component(proxies.commonStorage);
44 def->component(proxies.priorKnowledge);
45 def->component(proxies.workingMemory);
46 def->component(proxies.longtermMemory);
47
48 def->optional(properties.mongoDbName,
49 "p.MongoDbName",
50 "Name of snapshot Mongo database to load files into");
51 def->optional(properties.dryRun, "p.dryRun", "If true, don't actually change anything.");
52
53
54 def->optional(properties.importObjects, "p.obj.00_DoImport", "Whether to import objects.");
55
56 def->optional(properties.objectsRepository,
57 "p.obj.10_Repository",
58 "Repository such as ArmarXObjects containing the object classes.");
59
60 def->optional(properties.datasetName,
61 "p.obj.20_DatasetName",
62 "If set, only the given dataset in ArmarXObjects is imported.\n"
63 "Otherwise (default), all objects are imported.");
64 def->optional(properties.className,
65 "p.obj.21_ClassName",
66 "If set, only the given dataset in ArmarXObjects is imported.\n"
67 "Otherwise (default), all objects are imported.");
68
69
70 def->optional(properties.importScenes, "p.scenes.00_DoImport", "Whether to import scenes.");
71 def->optional(properties.scenesDirectory,
72 "p.scenes.10_Directory",
73 "Directory containing the scene files (Scene_*.json).");
74 def->optional(properties.sceneNames,
75 "p.scenes.20_Scenes",
76 "Scenes to load by name. Comma separated. ({scene_name}.json).");
77
78 return def;
79 }
80
81 void
85
86 void
88 {
89 gridFileManager.reset(new memoryx::GridFileManager(proxies.commonStorage));
90
91 memoryx::PersistentObjectClassSegmentBasePrx objectClassesSegment =
92 proxies.priorKnowledge->getObjectClassesSegment();
93
94
95 ArmarXObjectsToMemory importer;
96 importer.dryRun = properties.dryRun;
97
98 if (properties.dryRun)
99 {
100 ARMARX_IMPORTANT << "Performing dry run - not going to change anything!";
101 }
102
103 if (properties.importObjects)
104 {
105 armarx::ObjectFinder finder(properties.objectsRepository);
106 std::vector<armarx::ObjectInfo> objectInfos;
107 if (properties.datasetName.size() > 0)
108 {
109 if (properties.className.size() > 0)
110 {
111 ARMARX_INFO << "Finding " << properties.datasetName << properties.className
112 << "...";
113 if (auto info = finder.findObject(properties.datasetName, properties.className))
114 {
115 objectInfos.push_back(info.value());
116 }
117 }
118 else
119 {
120 ARMARX_INFO << "Finding objects in dataset " << properties.datasetName
121 << " ...";
122 objectInfos = finder.findAllObjectsOfDataset(properties.datasetName, false);
123 }
124 }
125 else
126 {
127 ARMARX_INFO << "Finding all objects ...";
128 objectInfos = finder.findAllObjects(false);
129 }
130 ARMARX_INFO << "Found " << objectInfos.size() << " object classes in "
131 << properties.objectsRepository << ".";
132
133 ARMARX_INFO << "Importing " << objectInfos.size() << " object classes ...";
135 objectInfos, gridFileManager, objectClassesSegment, properties.mongoDbName);
136 }
137
138 if (properties.importScenes)
139 {
140 const std::filesystem::path scenesDir =
141 armarx::ArmarXDataPath::resolvePath(properties.scenesDirectory);
142 std::unordered_set<std::string> scenes;
143 if (not properties.sceneNames.empty())
144 {
145 bool trim = true, removeEmpty = true;
146 const auto scenesTmp = armarx::split(properties.sceneNames, ",", trim, removeEmpty);
147 ARMARX_IMPORTANT << properties.sceneNames << "\n" << scenesTmp;
148 scenes = std::unordered_set<std::string>(scenesTmp.begin(), scenesTmp.end());
149 }
150
151 if (not scenes.empty())
152 {
153 ARMARX_INFO << "The following scenes will be loaded (if available): "
154 << std::vector<std::string>(scenes.begin(), scenes.end());
155 }
156
157 ARMARX_INFO << "Importing scenes in " << scenesDir;
158 importer.importScenesAsSnapshots(scenesDir,
159 proxies.longtermMemory,
160 proxies.workingMemory,
161 objectClassesSegment,
162 scenes);
163 }
164 }
165
166 void
170
171 void
175
176 std::string
178 {
179 return "ArmarXObjectsImporter";
180 }
181
182 ArmarXObjectsImporter::Properties::Properties() :
183 objectsRepository(armarx::ObjectFinder::DefaultObjectsPackageName),
184 scenesDirectory(armarx::ObjectFinder::DefaultObjectsPackageName + "/scenes")
185 {
186 }
187
188} // namespace memoryx
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
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.