Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4125, Fix #4135 - GbXML ForwardTranslator: clear map&set, and add a modelToGbXMLString method #4136

Merged
merged 3 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/gbxml/ForwardTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,31 @@ namespace gbxml {
return false;
}

std::string ForwardTranslator::modelToGbXMLString(const openstudio::model::Model& model, ProgressBar* progressBar)
{
std::string gbXML_str;

m_progressBar = progressBar;

m_logSink.setThreadId(std::this_thread::get_id());

m_logSink.resetStringStream();

pugi::xml_document doc;
//doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
bool result = this->translateModel(model, doc);

if (result) {
// doc.save allows any ostream, so use a stringstream
std::stringstream ss;
doc.save(ss, " ");
gbXML_str = ss.str();
}

return gbXML_str;
}


std::vector<LogMessage> ForwardTranslator::warnings() const
{
std::vector<LogMessage> result;
Expand Down Expand Up @@ -174,6 +199,11 @@ namespace gbxml {

bool ForwardTranslator::translateModel(const openstudio::model::Model& model, pugi::xml_document& document)
{

// Clear the map & set
m_translatedObjects.clear();
m_materials.clear();
Comment on lines +203 to +205
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #4135


auto gbXMLElement = document.append_child("gbXML");
gbXMLElement.append_attribute("xmlns") = "http://www.gbxml.org/schema";
gbXMLElement.append_attribute("xmlns:xhtml") = "http://www.w3.org/1999/xhtml";
Expand Down
4 changes: 4 additions & 0 deletions src/gbxml/ForwardTranslator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ namespace gbxml {

virtual ~ForwardTranslator();

// Save the GbXML to a file
bool modelToGbXML(const openstudio::model::Model& model, const openstudio::path& path, ProgressBar* progressBar = nullptr);

// Return a string representation of the GbXML document
std::string modelToGbXMLString(const openstudio::model::Model& model, ProgressBar* progressBar = nullptr);
Comment on lines +84 to +85
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix #4125


/** Get warning messages generated by the last translation. */
std::vector<LogMessage> warnings() const;

Expand Down
16 changes: 16 additions & 0 deletions src/gbxml/Test/ForwardTranslator_GTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,19 @@ TEST_F(gbXMLFixture, ForwardTranslator_surfaceType_4001)
std::string expectedSurfaceType("SlabOnGrade");
EXPECT_EQ(expectedSurfaceType, surfaceType);
}

TEST_F(gbXMLFixture, ForwardTranslator_exampleModel_State)
{
// Test for #4135: translating a model twice should produce the same result
Model model = exampleModel();

ForwardTranslator forwardTranslator;
std::string gbXML_str1 = forwardTranslator.modelToGbXMLString(model);
EXPECT_FALSE(gbXML_str1.empty());

std::string gbXML_str2 = forwardTranslator.modelToGbXMLString(model);
EXPECT_FALSE(gbXML_str2.empty());

EXPECT_EQ(gbXML_str1.length(), gbXML_str2.length());
EXPECT_GT(gbXML_str1.length(), 50000);
}