26 #include <unordered_set>
28 #include <SimoxUtility/json.h>
47 namespace fs = std::filesystem;
58 const std::vector<armarx::ObjectInfo>& objectInfos,
60 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
61 const std::string& mongoDbName)
const
67 const fs::path xmlFile = objectInfo.simoxXML().absolutePath;
69 if (fs::is_regular_file(xmlFile))
71 const std::string className = objectInfo.idStr();
73 ARMARX_INFO <<
"Importing " << objectInfo.id() <<
" with Simox XML " << xmlFile;
76 newClass->setName(className);
78 ARMARX_INFO <<
"\tStoring Simox XML file: " << xmlFile;
84 simoxWrapper->setAndStoreManipulationFile(xmlFile.string(), mongoDbName);
87 memoryx::EntityBasePtr oldClass =
88 objectClassesSegment->getEntityByName(newClass->getName());
91 ARMARX_INFO <<
"\tUpdating existing entity " << oldClass->getName()
92 <<
"(ID: " << oldClass->getId() <<
")";
95 objectClassesSegment->updateEntity(oldClass->getId(), newClass);
100 ARMARX_INFO <<
"\tCreating new entity " << newClass->getName();
103 objectClassesSegment->addEntity(newClass);
116 const std::filesystem::path& scenesDirectory,
117 const LongtermMemoryInterfacePrx& longtermMemory,
118 const WorkingMemoryInterfacePrx& workingMemory,
119 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
120 const std::unordered_set<std::string>& scenes)
const
128 memoryx::ObjectInstanceMemorySegmentBasePrx objectInstancesSegment =
129 workingMemory->getObjectInstancesSegment();
133 fs::recursive_directory_iterator end_iter;
134 for (fs::recursive_directory_iterator dir_iter(scenesDirectory); dir_iter != end_iter;
137 if (fs::is_regular_file(dir_iter->status()) &&
138 (dir_iter->path().extension() ==
".json"))
140 const std::filesystem::path sceneJsonFile = dir_iter->path();
141 const std::string snapshotName = sceneJsonFile.stem();
144 if (not scenes.empty() and scenes.count(snapshotName) == 0)
146 ARMARX_INFO <<
"Skipping snapshot " << snapshotName
147 <<
" as it has not been selected";
157 objectClassesSegment,
158 objectInstancesSegment);
164 ARMARX_IMPORTANT <<
"Found " << fileCount <<
" valid *.json files in the scenes directory '"
165 << scenesDirectory <<
"'";
170 const std::string& snapshotName,
171 const std::filesystem::path& jsonFile,
172 const LongtermMemoryInterfacePrx& longtermMemory,
173 const WorkingMemoryInterfacePrx& workingMemory,
174 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
175 const memoryx::ObjectInstanceMemorySegmentBasePrx& objectInstancesSegment)
const
182 workingMemory->clear();
185 const nlohmann::json j = nlohmann::read_json(jsonFile);
189 std::map<std::string, std::unordered_set<std::string>> instancesPerClass;
192 for (
const auto&
object : scene.
objects)
195 object, objectClassesSegment, objectInstancesSegment, instancesPerClass);
198 ARMARX_INFO <<
"Saving snapshot from working memory to longterm memory: "
199 <<
"'" << snapshotName <<
"'";
203 longtermMemory->saveWorkingMemorySnapshot(snapshotName, workingMemory);
219 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
220 const ObjectInstanceMemorySegmentBasePrx& objectInstancesSegment,
221 std::map<std::string, std::unordered_set<std::string>>& instancesPerClass)
const
225 const std::string& className =
object.className;
226 const Eigen::Vector3f& position =
object.position;
230 const auto obj = objectClassesSegment->getObjectClassByName(className);
233 ARMARX_WARNING <<
"Object class " <<
object.className <<
" for object "
234 <<
object.getObjectID() <<
" not found in class segment.";
243 auto& instancesOfThisClass = instancesPerClass[className];
245 auto getObjectName = [&]() -> std::string
248 if (not
object.instanceName.empty())
250 if (instancesOfThisClass.count(
object.instanceName) > 0)
253 <<
" already in use.";
256 return object.instanceName;
260 for (
int i = 0;; i++)
263 if (instancesOfThisClass.count(objectName) == 0)
272 const std::string objectName = getObjectName();
273 if (objectName.empty())
275 throw armarx::LocalException(
"No object name could be set!");
278 instancesOfThisClass.insert(objectName);
282 NameList attributeNames = objectClass->getAttributeNames();
283 for (
auto it = attributeNames.begin(); it != attributeNames.end(); ++it)
285 newObject->putAttribute(objectClass->getAttribute(*it));
288 newObject->setClass(objectClass->getName(), 1.0f);
289 newObject->setExistenceCertainty(1.0f);
293 newObject->setPosition(framedPosition);
297 newObject->setOrientation(newOrient);
299 std::string objectID = objectInstancesSegment->addEntity(newObject);
300 newObject->setId(objectID);
302 ARMARX_INFO <<
"Added object of class '" << className <<
"' with ID '" << objectID