Skip to content

Commit

Permalink
Extend oms::Snapshot (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Mar 1, 2021
1 parent 69d29f1 commit 08f253c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ oms_status_enu_t oms::Model::loadSnapshot(const pugi::xml_node& node)
system = NULL;

Snapshot snapshot; // this is a temporary workaroud, loadSnapshot will be removed later
snapshot.importResourcesXML("SystemStructure.ssd", node);
snapshot.importResourceNode("SystemStructure.ssd", node);
//snapshot.debugPrintAll();

bool old_copyResources = copyResources();
Expand Down Expand Up @@ -188,7 +188,7 @@ oms_status_enu_t oms::Model::importSnapshot(const char* snapshot_)
//snapshot.debugPrintAll();

// get ssd:SystemStructureDescription
pugi::xml_node ssdNode = snapshot.getResourcesFile( "SystemStructure.ssd");
pugi::xml_node ssdNode = snapshot.getResourceNode("SystemStructure.ssd");

ComRef new_cref = ComRef(ssdNode.attribute("name").as_string());
std::string ssdVersion = ssdNode.attribute("version").as_string();
Expand Down Expand Up @@ -608,7 +608,7 @@ oms_status_enu_t oms::Model::exportToSSD(pugi::xml_node& node, pugi::xml_node& s

oms_status_enu_t oms::Model::importFromSnapshot(const Snapshot& snapshot)
{
pugi::xml_node ssdNode = snapshot.getResourcesFile("SystemStructure.ssd");
pugi::xml_node ssdNode = snapshot.getResourceNode("SystemStructure.ssd");
if (!ssdNode)
return logError("loading <oms:file> \"SystemStructure.ssd\" from <oms:snapshot> failed");

Expand Down
4 changes: 2 additions & 2 deletions src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,9 +1641,9 @@ oms_status_enu_t oms_extractFMIKind(const char* filename, oms_fmi_kind_enu_t* ki
return logError("failed to extract modelDescription.xml from \"" + std::string(filename) + "\"");

oms::Snapshot snapshot;
if (oms_status_ok != snapshot.importResourcesFile("modelDescription.xml", oms::Scope::GetInstance().getTempDirectory()))
if (oms_status_ok != snapshot.importResourceFile("modelDescription.xml", oms::Scope::GetInstance().getTempDirectory()))
return logError("Failed to import");
const pugi::xml_node node = snapshot.getResourcesFile("modelDescription.xml");
const pugi::xml_node node = snapshot.getResourceNode("modelDescription.xml");

bool cs = (std::string(node.child("CoSimulation").attribute("modelIdentifier").as_string()) != "");
bool me = (std::string(node.child("ModelExchange").attribute("modelIdentifier").as_string()) != "");
Expand Down
6 changes: 3 additions & 3 deletions src/OMSimulatorLib/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ oms_status_enu_t oms::Scope::importModel(const std::string& filename, char** _cr
return logError("failed to extract \"SystemStructure.ssd\" from \"" + filename + "\"");

Snapshot snapshot;
snapshot.importResourcesFile("SystemStructure.ssd", temp_root);
const pugi::xml_node node = snapshot.getResourcesFile("SystemStructure.ssd");
snapshot.importResourceFile("SystemStructure.ssd", temp_root);
const pugi::xml_node node = snapshot.getResourceNode("SystemStructure.ssd");
if (!node)
return logError("failed to load \"SystemStructure.ssd\"");

Expand All @@ -229,7 +229,7 @@ oms_status_enu_t oms::Scope::importModel(const std::string& filename, char** _cr
for (const auto &entry : OMS_RECURSIVE_DIRECTORY_ITERATOR(model->getTempDirectory()))
if (entry.path().has_extension())
if (".ssv" == entry.path().extension() || ".ssm" == entry.path().extension() || ".xml" == entry.path().extension())
snapshot.importResourcesFile(naive_uncomplete(entry.path(), model->getTempDirectory()), model->getTempDirectory());
snapshot.importResourceFile(naive_uncomplete(entry.path(), model->getTempDirectory()), model->getTempDirectory());

// snapshot.debugPrintAll();

Expand Down
50 changes: 39 additions & 11 deletions src/OMSimulatorLib/Snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ oms::Snapshot::~Snapshot()
{
}

pugi::xml_node oms::Snapshot::newResourceNode(const filesystem::path& filename)
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node node = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", filename.generic_string().c_str());

if (node)
{
logError("Node \"" + filename.generic_string() + "\" does already exist");
return node.first_child();
}

pugi::xml_node new_node = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
new_node.append_attribute("name") = filename.generic_string().c_str();
return new_node;
}

oms_status_enu_t oms::Snapshot::import(const char* snapshot)
{
doc.reset();
Expand All @@ -54,7 +70,7 @@ oms_status_enu_t oms::Snapshot::import(const char* snapshot)
return oms_status_ok;
}

oms_status_enu_t oms::Snapshot::importResourcesFile(const filesystem::path& filename, const filesystem::path& root)
oms_status_enu_t oms::Snapshot::importResourceFile(const filesystem::path& filename, const filesystem::path& root)
{
filesystem::path p = root / filename;

Expand All @@ -63,20 +79,20 @@ oms_status_enu_t oms::Snapshot::importResourcesFile(const filesystem::path& file
if (!result)
return logError("loading resource \"" + p.generic_string() + "\" failed (" + std::string(result.description()) + ")");

return importResourcesXML(filename, tmp_doc.document_element());
return importResourceNode(filename, tmp_doc.document_element());
}

oms_status_enu_t oms::Snapshot::importResourcesMemory(const filesystem::path& filename, const char* contents)
oms_status_enu_t oms::Snapshot::importResourceMemory(const filesystem::path& filename, const char* contents)
{
pugi::xml_document tmp_doc;
pugi::xml_parse_result result = tmp_doc.load_string(contents);
if (!result)
return logError("loading resource \"" + filename.generic_string() + "\" failed (" + std::string(result.description()) + ")");

return importResourcesXML(filename, tmp_doc.document_element());
return importResourceNode(filename, tmp_doc.document_element());
}

oms_status_enu_t oms::Snapshot::importResourcesXML(const filesystem::path& filename, const pugi::xml_node& node)
oms_status_enu_t oms::Snapshot::importResourceNode(const filesystem::path& filename, const pugi::xml_node& node)
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node oms_file = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
Expand All @@ -86,27 +102,39 @@ oms_status_enu_t oms::Snapshot::importResourcesXML(const filesystem::path& filen
return oms_status_ok;
}

void oms::Snapshot::getResources(std::vector<std::string>& resources)
void oms::Snapshot::getResources(std::vector<std::string>& resources) const
{
pugi::xml_node oms_snapshot = doc.document_element();
for (const auto& it : oms_snapshot.children())
resources.push_back(it.attribute("name").as_string());
}

pugi::xml_node oms::Snapshot::getResourcesFile(const filesystem::path& filename) const
pugi::xml_node oms::Snapshot::getResourceNode(const filesystem::path& filename) const
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node node = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", filename.generic_string().c_str());

if (node)
return node.first_child();

logError("Failed to find node \"" + filename.generic_string() + "\"");
return node;
}

pugi::xml_node oms::Snapshot::operator[](const filesystem::path& filename)
{
pugi::xml_node oms_snapshot = doc.document_element();
pugi::xml_node node = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", filename.generic_string().c_str());

if (!node)
logError("Failed to find node \"" + filename.generic_string() + "\"");
if (node)
return node.first_child();

return node.first_child();
return newResourceNode(filename);
}

void oms::Snapshot::debugPrintNode(const filesystem::path& filename) const
{
pugi::xml_node node = getResourcesFile(filename);
pugi::xml_node node = getResourceNode(filename);

if (node)
node.print(std::cout, " ");
Expand Down
14 changes: 9 additions & 5 deletions src/OMSimulatorLib/Snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ namespace oms
~Snapshot();

oms_status_enu_t import(const char* snapshot);
oms_status_enu_t importResourcesFile(const filesystem::path& filename, const filesystem::path& root);
oms_status_enu_t importResourcesMemory(const filesystem::path& filename, const char* contents);
oms_status_enu_t importResourcesXML(const filesystem::path& filename, const pugi::xml_node& node);
void getResources(std::vector<std::string>& resources);
pugi::xml_node getResourcesFile(const filesystem::path& filename) const;
oms_status_enu_t importResourceFile(const filesystem::path& filename, const filesystem::path& root);
oms_status_enu_t importResourceMemory(const filesystem::path& filename, const char* contents);
oms_status_enu_t importResourceNode(const filesystem::path& filename, const pugi::xml_node& node);

pugi::xml_node newResourceNode(const filesystem::path& filename);
pugi::xml_node operator[](const filesystem::path& filename);

void getResources(std::vector<std::string>& resources) const;
pugi::xml_node getResourceNode(const filesystem::path& filename) const;

void debugPrintNode(const filesystem::path& filename) const;
void debugPrintAll() const;
Expand Down
4 changes: 2 additions & 2 deletions src/OMSimulatorLib/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ oms_status_enu_t oms::System::importStartValuesFromSSV(const std::string& ssvPat
if (!ssmPath.empty())
importParameterMappingFromSSM(ssmPath, snapshot, mappedEntry);

pugi::xml_node parameterSet = snapshot.getResourcesFile(ssvPath);
pugi::xml_node parameterSet = snapshot.getResourceNode(ssvPath);
pugi::xml_node parameters = parameterSet.child(oms::ssp::Version1_0::ssv::parameters);

for (pugi::xml_node_iterator it = parameters.begin(); it != parameters.end(); ++it)
Expand Down Expand Up @@ -2511,7 +2511,7 @@ oms_status_enu_t oms::System::updateAlgebraicLoops(const std::vector< oms_ssc_t

void oms::System::importParameterMappingFromSSM(const std::string& ssmPath, const Snapshot& snapshot, std::multimap<ComRef, ComRef>& mappedEntry)
{
pugi::xml_node parameterMapping = snapshot.getResourcesFile(ssmPath);
pugi::xml_node parameterMapping = snapshot.getResourceNode(ssmPath);

for (pugi::xml_node_iterator it = parameterMapping.begin(); it != parameterMapping.end(); ++it)
{
Expand Down
8 changes: 4 additions & 4 deletions src/OMSimulatorLib/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ oms_status_enu_t oms::Values::importFromSnapshot(const pugi::xml_node& node, con

if (!ssvFile.empty()) // parameter binding provided with .ssv file
{
pugi::xml_node parameterSet = snapshot.getResourcesFile(ssvFile);
pugi::xml_node parameterSet = snapshot.getResourceNode(ssvFile);
if (!parameterSet)
return logError("loading <oms:file> \"" + ssvFile + "\" from <oms:snapshot> failed");

Expand All @@ -145,7 +145,7 @@ oms_status_enu_t oms::Values::importFromSnapshot(const pugi::xml_node& node, con
std::string ssmFileSource = ssd_parameterMapping.attribute("source").as_string();
if (!ssmFileSource.empty())
{
pugi::xml_node ssm_parameterMapping = snapshot.getResourcesFile(ssmFileSource);
pugi::xml_node ssm_parameterMapping = snapshot.getResourceNode(ssmFileSource);
if (!ssm_parameterMapping)
return logError("loading <oms:file> \"" + ssmFileSource + "\" from <oms:snapshot> failed");

Expand Down Expand Up @@ -438,8 +438,8 @@ oms_status_enu_t oms::Values::importStartValuesHelper(const pugi::xml_node& para
oms_status_enu_t oms::Values::parseModelDescription(const filesystem::path& root)
{
Snapshot snapshot;
snapshot.importResourcesFile("modelDescription.xml", root);
const pugi::xml_node node = snapshot.getResourcesFile("modelDescription.xml");
snapshot.importResourceFile("modelDescription.xml", root);
const pugi::xml_node node = snapshot.getResourceNode("modelDescription.xml");

if (!node)
return logError("Failed to load modelDescription.xml");
Expand Down

0 comments on commit 08f253c

Please sign in to comment.