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 namespace 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
83  {
84  }
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
168  {
169  }
170 
171  void
173  {
174  }
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
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:538
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::ObjectFinder::findAllObjects
std::vector< ObjectInfo > findAllObjects(bool checkPaths=true) const
Definition: ObjectFinder.cpp:149
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:84
memoryx::ArmarXObjectsToMemory
Definition: ArmarXObjectsToMemory.h:39
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:22
armarx::ObjectFinder::findAllObjectsOfDataset
std::vector< ObjectInfo > findAllObjectsOfDataset(const std::string &dataset, bool checkPaths=true) const
Definition: ObjectFinder.cpp:208
memoryx::ArmarXObjectsImporter::onDisconnectComponent
void onDisconnectComponent() override
Definition: ArmarXObjectsImporter.cpp:167
memoryx::ArmarXObjectsImporter::onConnectComponent
void onConnectComponent() override
Definition: ArmarXObjectsImporter.cpp:87
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
ArmarXObjectsImporter.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
IceUtil::Handle
Definition: forward_declarations.h:30
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:57
armarx::ObjectFinder::findObject
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Definition: ObjectFinder.cpp:64
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:41
memoryx::ArmarXObjectsImporter::onExitComponent
void onExitComponent() override
Definition: ArmarXObjectsImporter.cpp:172
memoryx::ArmarXObjectsImporter::getDefaultName
std::string getDefaultName() const override
Definition: ArmarXObjectsImporter.cpp:177
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:115
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
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:38
memoryx::ArmarXObjectsImporter::onInitComponent
void onInitComponent() override
Definition: ArmarXObjectsImporter.cpp:82