Skip to content

Commit

Permalink
Encapsulate the snapshot implementation (#966)
Browse files Browse the repository at this point in the history
Co-authored-by: @arun3688
  • Loading branch information
lochel committed Feb 26, 2021
1 parent 4bbcb87 commit b754feb
Show file tree
Hide file tree
Showing 34 changed files with 446 additions and 364 deletions.
7 changes: 3 additions & 4 deletions src/OMSimulatorLib/BusConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@ oms::BusConnector::~BusConnector()
}
}

oms_status_enu_t oms::BusConnector::exportToSSD(pugi::xml_node &root) const
oms_status_enu_t oms::BusConnector::exportToSSD(pugi::xml_node& root) const
{
pugi::xml_node bus_node = root.append_child(oms::ssp::Draft20180219::bus);
bus_node.append_attribute("name") = name;

pugi::xml_node signals_node = bus_node.append_child(oms::ssp::Draft20180219::signals);
for(auto& connector : conrefs) {
for(auto& connector : conrefs)
{
pugi::xml_node signal_node = signals_node.append_child(oms::ssp::Draft20180219::signal);
signal_node.append_attribute("name") = connector.c_str();
}

if (this->geometry)
{
return reinterpret_cast<oms::ssd::ConnectorGeometry*>(this->geometry)->exportToSSD(bus_node);
}

return oms_status_ok;
}
Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ list(APPEND OMSIMULATORLIB_SOURCES ResultReader.cpp)
list(APPEND OMSIMULATORLIB_SOURCES ResultWriter.cpp)
list(APPEND OMSIMULATORLIB_SOURCES Scope.cpp)
list(APPEND OMSIMULATORLIB_SOURCES SignalDerivative.cpp)
list(APPEND OMSIMULATORLIB_SOURCES Snapshot.cpp)
list(APPEND OMSIMULATORLIB_SOURCES StepSizeConfiguration.cpp)
list(APPEND OMSIMULATORLIB_SOURCES System.cpp)
list(APPEND OMSIMULATORLIB_SOURCES SystemSC.cpp)
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

#include "Flags.h"
#include "Model.h"
#include "OMSFileSystem.h"
#include "System.h"
#include "TLMBusConnector.h"
#include <OMSFileSystem.h>

void oms::fmiLogger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message)
{
Expand Down
8 changes: 4 additions & 4 deletions src/OMSimulatorLib/ComponentFMUCS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
#include "Flags.h"
#include "Logging.h"
#include "Model.h"
#include "OMSFileSystem.h"
#include "ssd/Tags.h"
#include "System.h"
#include "SystemTLM.h"
#include "SystemWC.h"

#include <fmilib.h>
#include <JM/jm_portability.h>
#include <OMSFileSystem.h>
#include <RegEx.h>

oms::ComponentFMUCS::ComponentFMUCS(const ComRef& cref, System* parentSystem, const std::string& fmuPath)
Expand Down Expand Up @@ -251,12 +251,12 @@ oms::Component* oms::ComponentFMUCS::NewComponent(const oms::ComRef& cref, oms::
}

// parse modelDescription.xml to get start values before instantiating fmu's
component->values.parseModelDescription((tempDir / "modelDescription.xml").string().c_str());
component->values.parseModelDescription(tempDir);

return component;
}

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)
oms::Component* oms::ComponentFMUCS::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion, const Snapshot& 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.importFromSnapshot(*it, sspVersion, oms_snapshot);
component->values.importFromSnapshot(*it, sspVersion, snapshot);
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/OMSimulatorLib/ComponentFMUCS.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "ComRef.h"
#include "Values.h"
#include "ResultWriter.h"
#include "Snapshot.h"
#include "Variable.h"

#include <fmilib.h>
Expand All @@ -55,7 +56,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, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion, const Snapshot& snapshot);
const FMUInfo* getFMUInfo() const {return &(this->fmuInfo);}

oms_status_enu_t exportToSSD(pugi::xml_node& node, pugi::xml_node& ssvNode) const;
Expand Down
8 changes: 4 additions & 4 deletions src/OMSimulatorLib/ComponentFMUME.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
#include "Flags.h"
#include "Logging.h"
#include "Model.h"
#include "OMSFileSystem.h"
#include "ssd/Tags.h"
#include "System.h"
#include "SystemSC.h"

#include <fmilib.h>
#include <JM/jm_portability.h>
#include <OMSFileSystem.h>
#include <RegEx.h>

oms::ComponentFMUME::ComponentFMUME(const ComRef& cref, System* parentSystem, const std::string& fmuPath)
Expand Down Expand Up @@ -253,12 +253,12 @@ oms::Component* oms::ComponentFMUME::NewComponent(const oms::ComRef& cref, oms::
}

// parse modelDescription.xml to get start values before instantiating fmu's
component->values.parseModelDescription((tempDir / "modelDescription.xml").string().c_str());
component->values.parseModelDescription(tempDir);

return component;
}

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)
oms::Component* oms::ComponentFMUME::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion, const Snapshot& 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.importFromSnapshot(*it, sspVersion, oms_snapshot);
component->values.importFromSnapshot(*it, sspVersion, snapshot);
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/OMSimulatorLib/ComponentFMUME.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "ComRef.h"
#include "Values.h"
#include "ResultWriter.h"
#include "Snapshot.h"
#include "Variable.h"

#include <fmilib.h>
Expand All @@ -53,7 +54,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, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion, const Snapshot& snapshot);
const FMUInfo* getFMUInfo() const {return &(this->fmuInfo);}

oms_status_enu_t exportToSSD(pugi::xml_node& node, pugi::xml_node& ssvNode) const;
Expand Down
5 changes: 3 additions & 2 deletions src/OMSimulatorLib/ComponentTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@

#include "Logging.h"
#include "Model.h"
#include "OMSFileSystem.h"
#include "ssd/Tags.h"
#include "System.h"
#include <OMSFileSystem.h>

#include <RegEx.h>

oms::ComponentTable::ComponentTable(const ComRef& cref, System* parentSystem, const std::string& path)
Expand Down Expand Up @@ -103,7 +104,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, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot)
oms::Component* oms::ComponentTable::NewComponent(const pugi::xml_node& node, oms::System* parentSystem, const std::string& sspVersion, const Snapshot& snapshot)
{
ComRef cref = ComRef(node.attribute("name").as_string());
std::string type = node.attribute("type").as_string();
Expand Down
3 changes: 2 additions & 1 deletion src/OMSimulatorLib/ComponentTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "ResultReader.h"
#include "ResultWriter.h"
#include "SignalDerivative.h"
#include "Snapshot.h"
#include <fmilib.h>
#include <map>
#include <pugixml.hpp>
Expand All @@ -52,7 +53,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, const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);
static Component* NewComponent(const pugi::xml_node& node, System* parentSystem, const std::string& sspVersion, const Snapshot& 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
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/Connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ oms::Connector* oms::Connector::NewConnector(const pugi::xml_node& node, const s
return connector;
}

std::string oms::Connector::getTypeString(const pugi::xml_node &node, const std::string& sspVersion)
std::string oms::Connector::getTypeString(const pugi::xml_node& node, const std::string& sspVersion)
{
if (sspVersion == "Draft20180219")
{
Expand Down
52 changes: 17 additions & 35 deletions src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@

#include "Model.h"

#include "Component.h"
#include "CSVWriter.h"
#include "Flags.h"
#include "MATWriter.h"
#include "OMSFileSystem.h"
#include "Scope.h"
#include "ssd/Tags.h"
#include "System.h"
#include "Component.h"

#include <OMSFileSystem.h>
#include <minizip.h>
#include <thread>

Expand Down Expand Up @@ -143,7 +143,7 @@ oms_status_enu_t oms::Model::rename(const ComRef& cref, const ComRef& newCref)
return logError("Model \"" + std::string(getCref()) + "\" does not contain system \"" + std::string(front) + "\"");
}

oms_status_enu_t oms::Model::loadSnapshot(const pugi::xml_node node)
oms_status_enu_t oms::Model::loadSnapshot(const pugi::xml_node& node)
{
// This method will not change the name of the model.
// If a renaming is requested then it will happen in Scope::loadSnapshot.
Expand All @@ -154,20 +154,13 @@ 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;
Snapshot snapshot; // this is a temporary workaroud, loadSnapshot will be removed later
snapshot.importResourcesXML("SystemStructure.ssd", node);
//snapshot.debugPrintAll();

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

if (oms_status_ok != status)
Expand All @@ -185,25 +178,17 @@ oms_status_enu_t oms::Model::loadSnapshot(const pugi::xml_node node)
return oms_status_ok;
}

oms_status_enu_t oms::Model::importSnapshot(const char* snapshot)
oms_status_enu_t oms::Model::importSnapshot(const char* snapshot_)
{
if (!validState(oms_modelState_virgin))
return logError_ModelInWrongState(this);

pugi::xml_document doc;
pugi::xml_parse_result result = doc.load(snapshot);
if (!result)
return logError("loading snapshot failed (" + std::string(result.description()) + ")");

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;
Snapshot snapshot;
snapshot.import(snapshot_);
//snapshot.debugPrintAll();

// get ssd:SystemStructureDescription
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);
pugi::xml_node ssdNode = snapshot.getResourcesFile( "SystemStructure.ssd");

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

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

if (oms_status_ok != status)
Expand Down Expand Up @@ -621,15 +606,12 @@ 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::importFromSnapshot(const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot)
oms_status_enu_t oms::Model::importFromSnapshot(const Snapshot& snapshot)
{
auto oms_file_ssd = oms_snapshot.find("SystemStructure.ssd");
if (oms_file_ssd == oms_snapshot.end())
{
pugi::xml_node ssdNode = snapshot.getResourcesFile("SystemStructure.ssd");
if (!ssdNode)
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")
Expand All @@ -654,7 +636,7 @@ oms_status_enu_t oms::Model::importFromSnapshot(const std::unordered_map<std::st
if (!system)
return oms_status_error;

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

#if (BOOST_VERSION >= 105300)
#include <ctpl.h>
Expand Down Expand Up @@ -76,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 importFromSnapshot(const std::unordered_map<std::string, pugi::xml_node>& oms_snapshot);
oms_status_enu_t importFromSnapshot(const Snapshot& 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 @@ -122,7 +122,7 @@ namespace oms
bool useThreadPool() {return (pool != nullptr);}
ctpl::thread_pool& getThreadPool() {assert(pool); return *pool;}

oms_status_enu_t loadSnapshot(const pugi::xml_node node);
oms_status_enu_t loadSnapshot(const pugi::xml_node& node);

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

0 comments on commit b754feb

Please sign in to comment.