Add mj_parseXML and mj_saveXML to xml_api.

PiperOrigin-RevId: 640099466
Change-Id: I75f763523c5bc8f6f7c2f2de6806d82ba37ebd4e
This commit is contained in:
Alessio Quaglino
2024-06-04 11:59:32 +01:00
committed by Saran Tunyasuvunakool
parent 1bf90af0e0
commit b35ae973e1
11 changed files with 143 additions and 37 deletions
+3 -3
View File
@@ -100,7 +100,7 @@ class LocaleOverride {
} // namespace } // namespace
// Main writer function - calls mjXWrite // Main writer function - calls mjXWrite
std::string mjWriteXML(mjSpec* spec, char* error, int error_sz) { std::string mjWriteXML(const mjSpec* spec, char* error, int error_sz) {
LocaleOverride locale_override; LocaleOverride locale_override;
// check for empty model // check for empty model
@@ -424,9 +424,9 @@ static void RegisterResourceProvider() {
mjSpec* ParseSpecFromString(std::string_view xml, char* error, mjSpec* ParseSpecFromString(std::string_view xml, char* error,
int error_size, mjVFS* vfs) { int error_size) {
RegisterResourceProvider(); RegisterResourceProvider();
std::string xml2 = {xml.begin(), xml.end()}; std::string xml2 = {xml.begin(), xml.end()};
std::string str = "LoadModelFromString:" + xml2; std::string str = "LoadModelFromString:" + xml2;
return mjParseXML(str.c_str(), vfs, error, error_size); return mjParseXML(str.c_str(), nullptr, error, error_size);
} }
+3 -4
View File
@@ -24,15 +24,14 @@
// Top level API // Top level API
// Main writer function // Main writer function
std::string mjWriteXML(mjSpec* spec, char* error, int error_sz); std::string mjWriteXML(const mjSpec* spec, char* error, int error_sz);
// Main parser function // Main parser function
MJAPI mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, char* error, int error_sz); mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, char* error, int error_sz);
// Returns a newly-allocated mjSpec, loaded from the contents of xml. // Returns a newly-allocated mjSpec, loaded from the contents of xml.
// On failure returns nullptr and populates the error array if present. // On failure returns nullptr and populates the error array if present.
MJAPI mjSpec* ParseSpecFromString(std::string_view xml, char* error = nullptr, mjSpec* ParseSpecFromString(std::string_view xml, char* error = nullptr, int error_size = 0);
int error_size = 0, mjVFS* vfs = nullptr);
#endif // MUJOCO_SRC_XML_XML_H_ #endif // MUJOCO_SRC_XML_XML_H_
+50
View File
@@ -209,3 +209,53 @@ mjModel* mj_loadModel(const char* filename, const mjVFS* vfs) {
return m; return m;
} }
// parse spec from file
mjSpec* mj_parseXML(const char* filename, const mjVFS* vfs, char* error, int error_sz) {
return mjParseXML(filename, vfs, error, error_sz);
}
// parse spec from string
mjSpec* mj_parseXMLString(const char* xml, const mjVFS* vfs, char* error, int error_sz) {
return ParseSpecFromString(xml, error, error_sz);
}
// save spec to XML file, return 1 on success, 0 otherwise
int mj_saveXML(const mjSpec* s, const char* filename, char* error, int error_sz) {
std::string result = mjWriteXML(s, error, error_sz);
if (result.empty()) {
return 0;
}
std::ofstream file;
file.open(filename);
file << result;
file.close();
return 1;
}
// save spec to string, return 1 on success, 0 otherwise
int mj_saveXMLString(const mjSpec* s, char* xml, int xml_sz, char* error, int error_sz) {
std::string result = mjWriteXML(s, error, error_sz);
if (result.size() >= xml_sz) {
std::string error_msg = "Output string too short, should be at least " +
std::to_string(result.size()+1);
mjCopyError(error, error_msg.c_str(), error_sz);
return 0;
}
if (result.empty()) {
return 0;
}
result.copy(xml, xml_sz);
xml[result.size()] = 0;
return 1;
}
+9
View File
@@ -17,6 +17,7 @@
#include <mujoco/mjexport.h> #include <mujoco/mjexport.h>
#include <mujoco/mjmodel.h> #include <mujoco/mjmodel.h>
#include "user/user_api.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -43,6 +44,14 @@ MJAPI int mj_printSchema(const char* filename, char* buffer, int buffer_sz,
// if vfs is not NULL, look up file in vfs before reading from disk // if vfs is not NULL, look up file in vfs before reading from disk
MJAPI mjModel* mj_loadModel(const char* filename, const mjVFS* vfs); MJAPI mjModel* mj_loadModel(const char* filename, const mjVFS* vfs);
// parse spec from file or XML string.
MJAPI mjSpec* mj_parseXML(const char* filename, const mjVFS* vfs, char* error, int error_sz);
MJAPI mjSpec* mj_parseXMLString(const char* xml, const mjVFS* vfs, char* error, int error_sz);
// Save spec to XML file and/or string, return 1 on success, 0 otherwise.
MJAPI int mj_saveXML(const mjSpec* s, const char* filename, char* error, int error_sz);
MJAPI int mj_saveXMLString(const mjSpec* s, char* xml, int xml_sz, char* error, int error_sz);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+2 -2
View File
@@ -45,8 +45,8 @@ mjXBase::mjXBase() {
// set model field // set model field
void mjXBase::SetModel(mjSpec* _model) { void mjXBase::SetModel(const mjSpec* _model) {
model = _model; model = (mjSpec*)_model;
} }
+1 -1
View File
@@ -87,7 +87,7 @@ class mjXBase : public mjXUtil {
}; };
// set the model allocated externally // set the model allocated externally
virtual void SetModel(mjSpec*); virtual void SetModel(const mjSpec*);
// read alternative orientation specification // read alternative orientation specification
static int ReadAlternative(tinyxml2::XMLElement* elem, mjsOrientation& alt); static int ReadAlternative(tinyxml2::XMLElement* elem, mjsOrientation& alt);
+1 -1
View File
@@ -769,7 +769,7 @@ mjXWriter::mjXWriter(void) {
// cast model // cast model
void mjXWriter::SetModel(mjSpec* spec) { void mjXWriter::SetModel(const mjSpec* spec) {
if (spec) { if (spec) {
model = (mjCModel*)spec->element; model = (mjCModel*)spec->element;
} }
+1 -1
View File
@@ -27,7 +27,7 @@ class mjXWriter : public mjXBase {
public: public:
mjXWriter(); // constructor mjXWriter(); // constructor
virtual ~mjXWriter() = default; // destructor virtual ~mjXWriter() = default; // destructor
void SetModel(mjSpec* spec); void SetModel(const mjSpec* spec);
// write XML document to string // write XML document to string
std::string Write(char *error, std::size_t error_sz); std::string Write(char *error, std::size_t error_sz);
+8 -8
View File
@@ -25,7 +25,7 @@
#include <absl/strings/match.h> #include <absl/strings/match.h>
#include <mujoco/mujoco.h> #include <mujoco/mujoco.h>
#include "src/user/user_api.h" #include "src/user/user_api.h"
#include "src/xml/xml.h" #include "src/xml/xml_api.h"
#include "src/xml/xml_numeric_format.h" #include "src/xml/xml_numeric_format.h"
#include "test/fixture.h" #include "test/fixture.h"
@@ -125,7 +125,7 @@ TEST_F(PluginTest, RecompileCompare) {
// load spec // load spec
std::array<char, 1000> err; std::array<char, 1000> err;
mjSpec* s = mjParseXML(xml.c_str(), nullptr, err.data(), err.size()); mjSpec* s = mj_parseXML(xml.c_str(), 0, err.data(), err.size());
ASSERT_THAT(s, NotNull()) ASSERT_THAT(s, NotNull())
<< "Failed to load " << xml << ": " << err.data(); << "Failed to load " << xml << ": " << err.data();
@@ -417,7 +417,7 @@ TEST_F(MujocoTest, AttachSame) {
</mujoco>)"; </mujoco>)";
// create parent // create parent
mjSpec* parent = ParseSpecFromString(xml_child, er.data(), er.size()); mjSpec* parent = mj_parseXMLString(xml_child, 0, er.data(), er.size());
EXPECT_THAT(parent, NotNull()) << er.data(); EXPECT_THAT(parent, NotNull()) << er.data();
// get frame // get frame
@@ -530,7 +530,7 @@ TEST_F(MujocoTest, AttachDifferent) {
</mujoco>)"; </mujoco>)";
// model with one free sphere and a frame // model with one free sphere and a frame
mjSpec* parent = ParseSpecFromString(xml_parent, er.data(), er.size()); mjSpec* parent = mj_parseXMLString(xml_parent, 0, er.data(), er.size());
EXPECT_THAT(parent, NotNull()) << er.data(); EXPECT_THAT(parent, NotNull()) << er.data();
// get frame // get frame
@@ -538,7 +538,7 @@ TEST_F(MujocoTest, AttachDifferent) {
EXPECT_THAT(frame, NotNull()); EXPECT_THAT(frame, NotNull());
// model with one cylinder and a hinge // model with one cylinder and a hinge
mjSpec* child = ParseSpecFromString(xml_child, er.data(), er.size()); mjSpec* child = mj_parseXMLString(xml_child, 0, er.data(), er.size());
EXPECT_THAT(child, NotNull()) << er.data(); EXPECT_THAT(child, NotNull()) << er.data();
// get subtree // get subtree
@@ -642,7 +642,7 @@ TEST_F(MujocoTest, AttachFrame) {
</mujoco>)"; </mujoco>)";
// model with one free sphere and a frame // model with one free sphere and a frame
mjSpec* parent = ParseSpecFromString(xml_parent, er.data(), er.size()); mjSpec* parent = mj_parseXMLString(xml_parent, 0, er.data(), er.size());
EXPECT_THAT(parent, NotNull()) << er.data(); EXPECT_THAT(parent, NotNull()) << er.data();
// get frame // get frame
@@ -650,7 +650,7 @@ TEST_F(MujocoTest, AttachFrame) {
EXPECT_THAT(body, NotNull()); EXPECT_THAT(body, NotNull());
// model with one cylinder and a hinge // model with one cylinder and a hinge
mjSpec* child = ParseSpecFromString(xml_child, er.data(), er.size()); mjSpec* child = mj_parseXMLString(xml_child, 0, er.data(), er.size());
EXPECT_THAT(child, NotNull()) << er.data(); EXPECT_THAT(child, NotNull()) << er.data();
// get subtree // get subtree
@@ -711,7 +711,7 @@ void TestDetachBody(bool compile) {
</mujoco>)"; </mujoco>)";
// model with one cylinder and a hinge // model with one cylinder and a hinge
mjSpec* child = ParseSpecFromString(xml_child, er.data(), er.size()); mjSpec* child = mj_parseXMLString(xml_child, 0, er.data(), er.size());
EXPECT_THAT(child, NotNull()) << er.data(); EXPECT_THAT(child, NotNull()) << er.data();
// compile model (for testing double compilation) // compile model (for testing double compilation)
+63 -15
View File
@@ -24,6 +24,8 @@
#include <mujoco/mjdata.h> #include <mujoco/mjdata.h>
#include <mujoco/mjmodel.h> #include <mujoco/mjmodel.h>
#include <mujoco/mujoco.h> #include <mujoco/mujoco.h>
#include "src/user/user_api.h"
#include "src/xml/xml_api.h"
#include "test/fixture.h" #include "test/fixture.h"
namespace mujoco { namespace mujoco {
@@ -33,6 +35,21 @@ using ::testing::IsNull;
using ::testing::NotNull; using ::testing::NotNull;
using ::testing::StartsWith; using ::testing::StartsWith;
static constexpr char xml[] = R"(
<mujoco>
<worldbody>
<body>
<joint/>
<geom size="1"/>
</body>
<body>
<joint/>
<geom size="0.5"/>
</body>
</worldbody>
</mujoco>
)";
// ---------------------------- test mj_loadXML -------------------------------- // ---------------------------- test mj_loadXML --------------------------------
using LoadXmlTest = MujocoTest; using LoadXmlTest = MujocoTest;
@@ -66,20 +83,6 @@ TEST_F(LoadXmlTest, InvalidXmlFailsToLoad) {
} }
TEST_F(LoadXmlTest, MultipleBodies) { TEST_F(LoadXmlTest, MultipleBodies) {
static constexpr char xml[] = R"(
<mujoco>
<worldbody>
<body>
<joint/>
<geom size="1"/>
</body>
<body>
<joint/>
<geom size="0.5"/>
</body>
</worldbody>
</mujoco>
)";
std::array<char, 1000> error; std::array<char, 1000> error;
mjModel* model = LoadModelFromString(xml, error.data(), error.size()); mjModel* model = LoadModelFromString(xml, error.data(), error.size());
@@ -92,7 +95,6 @@ TEST_F(LoadXmlTest, MultipleBodies) {
mj_deleteData(data); mj_deleteData(data);
mj_deleteModel(model); mj_deleteModel(model);
} }
using SaveLastXmlTest = MujocoTest; using SaveLastXmlTest = MujocoTest;
TEST_F(SaveLastXmlTest, EmptyModel) { TEST_F(SaveLastXmlTest, EmptyModel) {
@@ -112,5 +114,51 @@ TEST_F(SaveLastXmlTest, EmptyModel) {
mj_deleteModel(model); mj_deleteModel(model);
} }
TEST_F(MujocoTest, SaveXmlShortString) {
std::array<char, 1000> error;
mjSpec* spec = mj_parseXMLString(xml, 0, error.data(), error.size());
EXPECT_THAT(spec, NotNull()) << "Failed to parse spec: " << error.data();
mjModel* model = mjs_compile(spec, 0);
EXPECT_THAT(model, NotNull()) << "Failed to compile model: " << error.data();
std::array<char, 10> out;
EXPECT_THAT(mj_saveXMLString(spec, out.data(), out.size(),
error.data(), error.size()), 0);
EXPECT_STREQ(error.data(), "Output string too short, should be at least 273");
mjs_deleteSpec(spec);
mj_deleteModel(model);
}
TEST_F(MujocoTest, SaveXml) {
std::array<char, 1000> error;
mjSpec* spec = mj_parseXMLString(xml, 0, error.data(), error.size());
EXPECT_THAT(spec, NotNull()) << "Failed to parse spec: " << error.data();
mjModel* model = mjs_compile(spec, 0);
EXPECT_THAT(model, NotNull()) << "Failed to compile model: " << error.data();
std::array<char, 273> out;
EXPECT_THAT(mj_saveXMLString(spec, out.data(), out.size(), error.data(),
error.size()), 1) << error.data();
mjSpec* saved_spec = mj_parseXMLString(xml, 0, error.data(), error.size());
EXPECT_THAT(saved_spec, NotNull()) << "Invalid saved spec: " << error.data();
mjModel* saved_model = mjs_compile(saved_spec, 0);
EXPECT_THAT(saved_model, NotNull()) << "Invalid model: " << error.data();
mjtNum tol = 0;
std::string field = "";
EXPECT_LE(CompareModel(model, saved_model, field), tol)
<< "Expected and attached models are different!\n"
<< "Different field: " << field << '\n';
mjs_deleteSpec(spec);
mjs_deleteSpec(saved_spec);
mj_deleteModel(model);
mj_deleteModel(saved_model);
}
} // namespace } // namespace
} // namespace mujoco } // namespace mujoco
+2 -2
View File
@@ -28,7 +28,7 @@
#include "src/cc/array_safety.h" #include "src/cc/array_safety.h"
#include "src/engine/engine_util_errmem.h" #include "src/engine/engine_util_errmem.h"
#include "src/user/user_api.h" #include "src/user/user_api.h"
#include "src/xml/xml.h" #include "src/xml/xml_api.h"
#include "test/fixture.h" #include "test/fixture.h"
namespace mujoco { namespace mujoco {
@@ -1084,7 +1084,7 @@ TEST_F(XMLReaderTest, ParseReplicateDefaultPropagate) {
</mujoco> </mujoco>
)"; )";
std::array<char, 1024> error; std::array<char, 1024> error;
mjSpec* spec = ParseSpecFromString(xml, error.data(), error.size()); mjSpec* spec = mj_parseXMLString(xml, 0, error.data(), error.size());
EXPECT_THAT(spec, NotNull()) << error.data(); EXPECT_THAT(spec, NotNull()) << error.data();
mjsBody* torso = mjs_findBody(spec, "torso-0"); mjsBody* torso = mjs_findBody(spec, "torso-0");