Skip to content

Commit

Permalink
create oms:snapshot from ssp (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 committed Feb 25, 2021
1 parent cca18f0 commit 8fb97f5
Show file tree
Hide file tree
Showing 21 changed files with 257 additions and 211 deletions.
4 changes: 2 additions & 2 deletions src/OMSimulatorLib/ComponentFMUCS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ oms::Component* oms::ComponentFMUCS::NewComponent(const oms::ComRef& cref, oms::
return component;
}

oms::Component* oms::ComponentFMUCS::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion)
oms::Component* oms::ComponentFMUCS::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot)
{
ComRef cref = ComRef(node.attribute("name").as_string());
std::string type = node.attribute("type").as_string();
Expand Down Expand Up @@ -297,7 +297,7 @@ oms::Component* oms::ComponentFMUCS::NewComponent(const pugi::xml_node& node, om
{
// set parameter bindings associated with the component
std::string tempdir = parentSystem->getModel()->getTempDirectory();
component->values.importFromSSD(*it, sspVersion, tempdir);
component->values.importFromSnapshot(*it, sspVersion, oms_snapshot);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentFMUCS.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace oms
~ComponentFMUCS();

static Component* NewComponent(const ComRef& cref, System* parentSystem, const std::string& fmuPath);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);
const FMUInfo* getFMUInfo() const {return &(this->fmuInfo);}

oms_status_enu_t exportToSSD(pugi::xml_node& node, pugi::xml_node& ssvNode) const;
Expand Down
4 changes: 2 additions & 2 deletions src/OMSimulatorLib/ComponentFMUME.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ oms::Component* oms::ComponentFMUME::NewComponent(const oms::ComRef& cref, oms::
return component;
}

oms::Component* oms::ComponentFMUME::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion)
oms::Component* oms::ComponentFMUME::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot)
{
ComRef cref = ComRef(node.attribute("name").as_string());
std::string type = node.attribute("type").as_string();
Expand Down Expand Up @@ -299,7 +299,7 @@ oms::Component* oms::ComponentFMUME::NewComponent(const pugi::xml_node& node, om
{
// set parameter bindings associated with the component
std::string tempdir = parentSystem->getModel()->getTempDirectory();
component->values.importFromSSD(*it, sspVersion, tempdir);
component->values.importFromSnapshot(*it, sspVersion, oms_snapshot);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentFMUME.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace oms
~ComponentFMUME();

static Component* NewComponent(const oms::ComRef& cref, System* parentSystem, const std::string& fmuPath);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);
const FMUInfo* getFMUInfo() const {return &(this->fmuInfo);}

oms_status_enu_t exportToSSD(pugi::xml_node& node, pugi::xml_node& ssvNode) const;
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ oms::Component* oms::ComponentTable::NewComponent(const oms::ComRef& cref, oms::
return component;
}

oms::Component* oms::ComponentTable::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion)
oms::Component* oms::ComponentTable::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot)
{
ComRef cref = ComRef(node.attribute("name").as_string());
std::string type = node.attribute("type").as_string();
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace oms
~ComponentTable();

static Component* NewComponent(const oms::ComRef& cref, System* parentSystem, const std::string& path);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);

oms_status_enu_t exportToSSD(pugi::xml_node& node, pugi::xml_node& ssvNode) const;
oms_status_enu_t exportToSSVTemplate(pugi::xml_node& ssvNode) {return oms_status_ok;}
Expand Down
43 changes: 33 additions & 10 deletions src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,20 @@ oms_status_enu_t oms::Model::loadSnapshot(const pugi::xml_node node)
System* old_root_system = system;
system = NULL;

// internally create the oms:snapshot from snapshot
pugi::xml_document snapshot;
pugi::xml_node oms_snapshot = snapshot.append_child(oms::ssp::Version1_0::snap_shot);
pugi::xml_node ssd_file = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
ssd_file.append_attribute("name") = "SystemStructure.ssd";
ssd_file.append_copy(node);

std::unordered_map<std::string, pugi::xml_node> snapshotFiles;
for (const auto &it : oms_snapshot.children())
snapshotFiles[it.attribute("name").as_string()] = it;

bool old_copyResources = copyResources();
copyResources(false);
oms_status_enu_t status = importFromSSD(node);
oms_status_enu_t status = importFromSnapshot(snapshotFiles);
copyResources(old_copyResources);

if (oms_status_ok != status)
Expand Down Expand Up @@ -184,10 +195,15 @@ oms_status_enu_t oms::Model::importSnapshot(const char* snapshot)
if (!result)
return logError("loading snapshot failed (" + std::string(result.description()) + ")");

snapShot = doc.document_element(); // oms:snapshot
pugi::xml_node oms_snapshot = doc.document_element(); // oms:snapshot

std::unordered_map<std::string, pugi::xml_node> snapshotFiles;
for (const auto& it : oms_snapshot.children())
snapshotFiles[it.attribute("name").as_string()] = it;

// get ssd:SystemStructureDescription
pugi::xml_node ssdNode = snapShot.child(oms::ssp::Version1_0::ssd_file).child(oms::ssp::Draft20180219::ssd::system_structure_description);
pugi::xml_node ssd_file = oms_snapshot.find_child_by_attribute(oms::ssp::Version1_0::oms_file, "name", "SystemStructure.ssd");
pugi::xml_node ssdNode = ssd_file.child(oms::ssp::Draft20180219::ssd::system_structure_description);

ComRef new_cref = ComRef(ssdNode.attribute("name").as_string());
std::string ssdVersion = ssdNode.attribute("version").as_string();
Expand All @@ -203,7 +219,7 @@ oms_status_enu_t oms::Model::importSnapshot(const char* snapshot)

bool old_copyResources = copyResources();
copyResources(false);
oms_status_enu_t status = importFromSSD(ssdNode);
oms_status_enu_t status = importFromSnapshot(snapshotFiles);
copyResources(old_copyResources);

if (oms_status_ok != status)
Expand Down Expand Up @@ -385,7 +401,7 @@ oms_status_enu_t oms::Model::exportSnapshot(const oms::ComRef& cref, char** cont

// list model
pugi::xml_node snapshotnode = doc.append_child(oms::ssp::Version1_0::snap_shot);
pugi::xml_node ssdfilenode = snapshotnode.append_child(oms::ssp::Version1_0::ssd_file);
pugi::xml_node ssdfilenode = snapshotnode.append_child(oms::ssp::Version1_0::oms_file);
ssdfilenode.append_attribute("name") = "SystemStructure.ssd";

pugi::xml_node node = ssdfilenode.append_child(oms::ssp::Draft20180219::ssd::system_structure_description);
Expand All @@ -400,7 +416,7 @@ oms_status_enu_t oms::Model::exportSnapshot(const oms::ComRef& cref, char** cont

// update <oms:ssv_file> after </oms:ssd_file>
pugi::xml_node last = doc.last_child();
pugi::xml_node ssvfilenode = last.append_child(oms::ssp::Version1_0::ssv_file);
pugi::xml_node ssvfilenode = last.append_child(oms::ssp::Version1_0::oms_file);
std::string ssvFilePath = "resources/" + std::string(this->getCref()) + ".ssv";
ssvfilenode.append_attribute("name") = ssvFilePath.c_str();
// dump all the ssv file contents
Expand Down Expand Up @@ -605,16 +621,23 @@ oms_status_enu_t oms::Model::exportToSSD(pugi::xml_node& node, pugi::xml_node& s
return oms_status_ok;
}

oms_status_enu_t oms::Model::importFromSSD(const pugi::xml_node& node)
oms_status_enu_t oms::Model::importFromSnapshot(const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot)
{
std::string sspVersion = node.attribute("version").as_string();
auto oms_file_ssd = oms_snapshot.find("SystemStructure.ssd");
if (oms_file_ssd == oms_snapshot.end())
{
return logError("loading <oms:file> \"SystemStructure.ssd\" from <oms:snapshot> failed");
}

pugi::xml_node ssdNode = oms_file_ssd->second.child(oms::ssp::Draft20180219::ssd::system_structure_description);
std::string sspVersion = ssdNode.attribute("version").as_string();

if(sspVersion == "Draft20180219")
{
logWarning_deprecated;
}

for(pugi::xml_node_iterator it = node.begin(); it != node.end(); ++it)
for(pugi::xml_node_iterator it = ssdNode.begin(); it != ssdNode.end(); ++it)
{
std::string name = it->name();
if (name == oms::ssp::Draft20180219::ssd::system)
Expand All @@ -631,7 +654,7 @@ oms_status_enu_t oms::Model::importFromSSD(const pugi::xml_node& node)
if (!system)
return oms_status_error;

if (oms_status_ok != system->importFromSSD(*it, sspVersion))
if (oms_status_ok != system->importFromSnapshot(*it, sspVersion, oms_snapshot))
return oms_status_error;
}
else if (name == oms::ssp::Draft20180219::ssd::default_experiment)
Expand Down
7 changes: 2 additions & 5 deletions src/OMSimulatorLib/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "Types.h"
#include <assert.h>
#include <pugixml.hpp>
#include <unordered_map>

#if (BOOST_VERSION >= 105300)
#include <ctpl.h>
Expand Down Expand Up @@ -75,7 +76,7 @@ namespace oms
oms_status_enu_t exportSnapshot(const ComRef& cref, char** contents);
oms_status_enu_t exportSSVTemplate(const ComRef& cref, const std::string& filename);
oms_status_enu_t exportSSMTemplate(const ComRef& cref, const std::string& filename);
oms_status_enu_t importFromSSD(const pugi::xml_node& node);
oms_status_enu_t importFromSnapshot(const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);
oms_status_enu_t importSnapshot(const char* snapshot);
oms_status_enu_t exportToFile(const std::string& filename) const;
oms_system_enu_t getSystemType(const pugi::xml_node& node, const std::string& sspVersion);
Expand Down Expand Up @@ -123,8 +124,6 @@ namespace oms

oms_status_enu_t loadSnapshot(const pugi::xml_node node);

pugi::xml_node getSnapshot() {return snapShot;}

private:
Model(const ComRef& cref, const std::string& tempDir);

Expand Down Expand Up @@ -157,8 +156,6 @@ namespace oms

std::string signalFilter = ".*"; ///< default

pugi::xml_node snapShot; ///< top level snapshot node which contains ssd, ssv and ssm as child nodes

bool isolatedFMU = false;

ctpl::thread_pool* pool = nullptr;
Expand Down
16 changes: 15 additions & 1 deletion src/OMSimulatorLib/OMSFileSystem.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _OMS_FILESYSTEM_H_

#if !defined(WITHOUT_FS) && defined(__has_include)

#if __has_include(<filesystem>)
#include <filesystem>
#if __cpp_lib_filesystem >= 201703
Expand All @@ -16,14 +17,19 @@ namespace filesystem = std::experimental::filesystem::v1;
#endif

#if OMC_STD_FS == 1

static inline filesystem::path oms_temp_directory_path(void) {
return filesystem::temp_directory_path();
}
static inline filesystem::path oms_canonical(filesystem::path p) {
return filesystem::canonical(p);
}

#else
#define OMS_RECURSIVE_DIRECTORY_ITERATOR(path) (filesystem::recursive_directory_iterator{path})

#else // boost part


#include <string>
#include <boost/version.hpp>
// boost version < 1.57 has issues linking boost::filesystem::copy_file
Expand All @@ -40,6 +46,14 @@ static inline filesystem::path oms_canonical(filesystem::path p) {
#include <boost/lockfree/queue.hpp>
#endif

#if (BOOST_VERSION < 105500)
#include <boost/range.hpp>
#define OMS_RECURSIVE_DIRECTORY_ITERATOR(path) (boost::make_iterator_range(filesystem::recursive_directory_iterator{path}, {}))
#else // older boost
#define OMS_RECURSIVE_DIRECTORY_ITERATOR(path) (filesystem::recursive_directory_iterator{path})
#endif


#include <boost/filesystem.hpp>
namespace filesystem = boost::filesystem;

Expand Down
66 changes: 65 additions & 1 deletion src/OMSimulatorLib/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <miniunz.h>
#include <OMSFileSystem.h>
#include <time.h>
#include "ssd/Tags.h"
#include <unordered_map>

oms::Scope::Scope()
: tempDir(".")
Expand Down Expand Up @@ -206,6 +208,21 @@ oms_status_enu_t oms::Scope::importModel(const std::string& filename, char** _cr

const pugi::xml_node node = doc.document_element(); // ssd:SystemStructureDescription

// internally create the oms:snapshot from ssp
pugi::xml_document snapshot;
pugi::xml_node oms_snapshot = snapshot.append_child(oms::ssp::Version1_0::snap_shot);

pugi::xml_node ssd_file = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
ssd_file.append_attribute("name") = "SystemStructure.ssd";
ssd_file.append_copy(node);

/*construct mappedSnapshot from oms_snapshot
eg: filename -> <oms:file name="SystemStructure.ssd"
filename -> <oms:file name = "resources/model.ssv"
*/
std::unordered_map<std::string, pugi::xml_node> mappedSnapshot;
mappedSnapshot["SystemStructure.ssd"] = ssd_file;

ComRef cref = ComRef(node.attribute("name").as_string());
std::string ssdVersion = node.attribute("version").as_string();

Expand All @@ -224,7 +241,33 @@ oms_status_enu_t oms::Scope::importModel(const std::string& filename, char** _cr

bool old_copyResources = model->copyResources();
model->copyResources(false);
oms_status_enu_t status = model->importFromSSD(node);

// add the remaining resources (e.g) .ssv, .ssm and signalFilter.xml to oms:snapshot
for (const auto &entry : OMS_RECURSIVE_DIRECTORY_ITERATOR(model->getTempDirectory()))
{
if (entry.path().has_extension())
{
if (entry.path().extension() == ".ssv")
{
// ssv files
addSnapshotResources(oms_snapshot, entry.path().string(), mappedSnapshot);
}
else if (entry.path().extension() == ".ssm")
{
// ssm files
addSnapshotResources(oms_snapshot, entry.path().string(), mappedSnapshot);
}
else if (entry.path().filename()== "signalFilter.xml")
{
// signalFilter.xml
addSnapshotResources(oms_snapshot, entry.path().string(), mappedSnapshot);
}
}
}

// snapshot.save(std::cout);

oms_status_enu_t status = model->importFromSnapshot(mappedSnapshot);
model->copyResources(old_copyResources);

Scope::GetInstance().setWorkingDirectory(cd);
Expand All @@ -241,6 +284,27 @@ oms_status_enu_t oms::Scope::importModel(const std::string& filename, char** _cr
return oms_status_ok;
}

oms_status_enu_t oms::Scope::addSnapshotResources(pugi::xml_node& oms_snapshot, std::string path, std::unordered_map<std::string, pugi::xml_node> &mappedSnapshot)
{
filesystem::path p(path);
std::string filename = "resources/" + p.filename().string();

pugi::xml_node oms_file = oms_snapshot.append_child(oms::ssp::Version1_0::oms_file);
oms_file.append_attribute("name") = filename.c_str();
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(path.c_str());
if (!result)
return logError("loading \"" + filename + "\" to <oms:snapshot> failed (" + std::string(result.description()) + ")");

pugi::xml_node oms_node = doc.document_element();
oms_file.append_copy(oms_node);

// map the filename with oms_file node
mappedSnapshot[filename] = oms_file;

return oms_status_ok;
}

oms_status_enu_t oms::Scope::setTempDirectory(const std::string& newTempDir)
{
try
Expand Down
3 changes: 3 additions & 0 deletions src/OMSimulatorLib/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <map>
#include <string>
#include <vector>
#include <unordered_map>

namespace oms
{
Expand Down Expand Up @@ -72,6 +73,8 @@ namespace oms

oms_status_enu_t loadSnapshot(const ComRef& cref, const char* snapshot, char** newCref);

oms_status_enu_t addSnapshotResources(pugi::xml_node& oms_snapshot, std::string filename, std::unordered_map<std::string, pugi::xml_node>& mappedSnapshot);

const std::string& getTempDirectory() const {return GetInstance().tempDir;}
std::string getWorkingDirectory();

Expand Down
Loading

0 comments on commit 8fb97f5

Please sign in to comment.