26 #include <unordered_set>
28 #include <SimoxUtility/json.h>
48 namespace fs = std::filesystem;
60 const std::vector<armarx::ObjectInfo>& objectInfos,
62 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
63 const std::string& mongoDbName)
const
69 const fs::path xmlFile = objectInfo.simoxXML().absolutePath;
71 if (fs::is_regular_file(xmlFile))
73 const std::string className = objectInfo.idStr();
75 ARMARX_INFO <<
"Importing " << objectInfo.id() <<
" with Simox XML " << xmlFile;
78 newClass->setName(className);
80 ARMARX_INFO <<
"\tStoring Simox XML file: " << xmlFile;
85 simoxWrapper->setAndStoreManipulationFile(xmlFile.string(), mongoDbName);
88 memoryx::EntityBasePtr oldClass = objectClassesSegment->getEntityByName(newClass->getName());
91 ARMARX_INFO <<
"\tUpdating existing entity " << oldClass->getName() <<
"(ID: " << oldClass->getId() <<
")";
94 objectClassesSegment->updateEntity(oldClass->getId(), newClass);
99 ARMARX_INFO <<
"\tCreating new entity " << newClass->getName();
102 objectClassesSegment->addEntity(newClass);
115 const std::filesystem::path& scenesDirectory,
116 const LongtermMemoryInterfacePrx& longtermMemory,
117 const WorkingMemoryInterfacePrx& workingMemory,
118 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
119 const std::unordered_set<std::string>& scenes)
const
127 memoryx::ObjectInstanceMemorySegmentBasePrx objectInstancesSegment = workingMemory->getObjectInstancesSegment();
131 fs::recursive_directory_iterator end_iter;
132 for (fs::recursive_directory_iterator dir_iter(scenesDirectory) ; dir_iter != end_iter ; ++dir_iter)
134 if (fs::is_regular_file(dir_iter->status()) && (dir_iter->path().extension() ==
".json"))
136 const std::filesystem::path sceneJsonFile = dir_iter->path();
137 const std::string snapshotName = sceneJsonFile.stem();
140 if (not scenes.empty() and scenes.count(snapshotName) == 0)
142 ARMARX_INFO <<
"Skipping snapshot " << snapshotName <<
" as it has not been selected";
153 objectClassesSegment,
154 objectInstancesSegment
161 ARMARX_IMPORTANT <<
"Found " << fileCount <<
" valid *.json files in the scenes directory '"
162 << scenesDirectory <<
"'";
168 const std::string& snapshotName,
169 const std::filesystem::path& jsonFile,
170 const LongtermMemoryInterfacePrx& longtermMemory,
171 const WorkingMemoryInterfacePrx& workingMemory,
172 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
173 const memoryx::ObjectInstanceMemorySegmentBasePrx& objectInstancesSegment
181 workingMemory->clear();
184 const nlohmann::json j = nlohmann::read_json(jsonFile);
188 std::map<std::string, std::unordered_set<std::string>> instancesPerClass;
191 for (
const auto&
object : scene.
objects)
195 objectClassesSegment,
196 objectInstancesSegment,
201 ARMARX_INFO <<
"Saving snapshot from working memory to longterm memory: "
202 <<
"'" << snapshotName <<
"'";
206 longtermMemory->saveWorkingMemorySnapshot(snapshotName, workingMemory);
222 const PersistentObjectClassSegmentBasePrx& objectClassesSegment,
223 const ObjectInstanceMemorySegmentBasePrx& objectInstancesSegment,
224 std::map<std::string, std::unordered_set<std::string>>& instancesPerClass
229 const std::string& className =
object.className;
230 const Eigen::Vector3f& position =
object.position;
234 const auto obj = objectClassesSegment->getObjectClassByName(className);
237 ARMARX_WARNING <<
"Object class " <<
object.className <<
" for object " <<
object.getObjectID()
238 <<
" not found in class segment.";
247 auto& instancesOfThisClass = instancesPerClass[className];
249 auto getObjectName = [&]() -> std::string
252 if (not
object.instanceName.empty())
254 if (instancesOfThisClass.count(
object.instanceName) > 0)
256 ARMARX_WARNING <<
"Instance name " <<
object.instanceName <<
" already in use.";
259 return object.instanceName;
263 for (
int i = 0;; i++)
266 if (instancesOfThisClass.count(objectName) == 0)
275 const std::string objectName = getObjectName();
276 if (objectName.empty())
278 throw armarx::LocalException(
"No object name could be set!");
281 instancesOfThisClass.insert(objectName);
285 NameList attributeNames = objectClass->getAttributeNames();
286 for (
auto it = attributeNames.begin(); it != attributeNames.end(); ++it)
288 newObject->putAttribute(objectClass->getAttribute(*it));
291 newObject->setClass(objectClass->getName(), 1.0f);
292 newObject->setExistenceCertainty(1.0f);
295 newObject->setPosition(framedPosition);
298 newObject->setOrientation(newOrient);
300 std::string objectID = objectInstancesSegment->addEntity(newObject);
301 newObject->setId(objectID);
303 ARMARX_INFO <<
"Added object of class '" << className <<
"' with ID '" << objectID <<
"'.";