Skip to content

Commit

Permalink
import partial snapshot (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 committed Mar 4, 2021
1 parent 3c43075 commit 038f1c2
Show file tree
Hide file tree
Showing 7 changed files with 620 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ 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 oms::ComRef& cref, const char* snapshot_)
{
if (!validState(oms_modelState_virgin))
return logError_ModelInWrongState(this);
Expand All @@ -187,6 +187,14 @@ oms_status_enu_t oms::Model::importSnapshot(const char* snapshot_)
snapshot.import(snapshot_);
//snapshot.debugPrintAll();

if (snapshot.isPartialSnapshot())
{
char *fullsnapshot = NULL;
// get full snapshot
exportSnapshot("", &fullsnapshot);
snapshot.importPartialSnapshot(cref, fullsnapshot);
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace oms
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 Snapshot& snapshot);
oms_status_enu_t importSnapshot(const char* snapshot);
oms_status_enu_t importSnapshot(const oms::ComRef& cref, 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);
oms_system_enu_t getSystemTypeHelper(const pugi::xml_node& node, const std::string& sspVersion);
Expand Down
18 changes: 14 additions & 4 deletions src/OMSimulatorLib/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,24 @@ oms_status_enu_t oms::Scope::importSnapshot(const oms::ComRef& cref, const char*
if (newCref)
*newCref = NULL;

oms::Model* model = oms::Scope::GetInstance().getModel(cref);
oms::ComRef tail(cref);
oms::ComRef front = tail.pop_front();

oms::ComRef modelCref(front);
modelCref.pop_suffix();

oms::Model* model = oms::Scope::GetInstance().getModel(modelCref);
if (!model)
return logError_ModelNotInScope(cref);
return logError_ModelNotInScope(front);

oms_status_enu_t status = model->importSnapshot(snapshot);
oms_status_enu_t status;
if (tail.isEmpty() && front.hasSuffix())
status = model->importSnapshot(oms::ComRef(":" + front.suffix()), snapshot);
else
status = model->importSnapshot(tail, snapshot);

if (newCref)
*newCref = (char*)getModel(cref)->getCref().c_str();
*newCref = (char*)getModel(modelCref)->getCref().c_str();

return status;
}
86 changes: 86 additions & 0 deletions src/OMSimulatorLib/Snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ oms_status_enu_t oms::Snapshot::import(const char* snapshot)
return oms_status_ok;
}

bool oms::Snapshot::isPartialSnapshot()
{
pugi::xml_node oms_snapshot = doc.document_element();
return oms_snapshot.attribute("partial").as_bool();
}

oms_status_enu_t oms::Snapshot::importResourceFile(const filesystem::path& filename, const filesystem::path& root)
{
filesystem::path p = root / filename;
Expand Down Expand Up @@ -247,6 +253,86 @@ oms_status_enu_t oms::Snapshot::exportPartialSnapshot(const ComRef& cref, Snapsh
return oms_status_ok;
}

oms_status_enu_t oms::Snapshot::importPartialSnapshot(const ComRef& cref, const char* fullsnapshot)
{
ComRef subCref(cref);
std::string suffix = subCref.pop_suffix();

// copy the partial snapshot to new doc
pugi::xml_document copy;
copy.append_copy(doc.first_child());

// load the full snapshot
import(fullsnapshot);

pugi::xml_node partialsnapshot = copy.document_element(); // oms:snapshot
std::string partialSnapshotfilename = partialsnapshot.child(oms::ssp::Version1_0::oms_file).attribute("name").as_string();

// copy only single file
if (!suffix.empty() && subCref.isEmpty())
{
pugi::xml_node oms_snapshot = doc.document_element(); // oms:snapshot
for (pugi::xml_node node : oms_snapshot.children())
{
std::string filename = node.attribute("name").as_string();
//if ((filename == partialSnapshotfilename) && (filename == cref.c_str()))
if (filename == suffix && filename == partialSnapshotfilename)
{
oms_snapshot.remove_child(node);
// replace with partialsnapshot
oms_snapshot.append_copy(partialsnapshot.first_child());
}
}
}

// check cref if to filter component: subCref
if (!subCref.isEmpty() && !suffix.empty())
{
ComRef tail(subCref);
ComRef front = tail.pop_front();

// get SystemStructure.ssd
pugi::xml_node ssdNode = getResourceNode("SystemStructure.ssd");
pugi::xml_node systemNode = ssdNode.first_child();

if (tail.isEmpty())
{
// replace system
if (systemNode.attribute("name").as_string() == std::string(front))
{
ssdNode.remove_child(systemNode);
ssdNode.prepend_copy(partialsnapshot.first_child().first_child());
}
}
else
{
// replace subsystems or components
for (pugi::xml_node_iterator it = systemNode.begin(); it != systemNode.end(); ++it)
{
if (std::string(it->name()) == oms::ssp::Draft20180219::ssd::elements)
{
for (pugi::xml_node_iterator itElements = (*it).begin(); itElements != (*it).end(); ++itElements)
{
std::string name = itElements->name();
if (name == oms::ssp::Draft20180219::ssd::system || name == oms::ssp::Draft20180219::ssd::component)
{
if(itElements->attribute("name").as_string() == std::string(tail))
{
it->remove_child(*itElements);
it->append_copy(partialsnapshot.first_child().first_child());
}
}
}
}
}
}
}

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

return oms_status_ok;
}

oms_status_enu_t oms::Snapshot::writeDocument(char** contents)
{
class : public pugi::xml_writer
Expand Down
3 changes: 3 additions & 0 deletions src/OMSimulatorLib/Snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace oms
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);

bool isPartialSnapshot();

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

Expand All @@ -63,6 +65,7 @@ namespace oms
pugi::xml_node getTemplateResourceNodeSSD(const filesystem::path& filename);
pugi::xml_node getTemplateResourceNodeSSV(const filesystem::path& filename);
oms_status_enu_t exportPartialSnapshot(const ComRef& cref, Snapshot& partialSnapshot);
oms_status_enu_t importPartialSnapshot(const ComRef& cref, const char* fullsnapshot);

void debugPrintNode(const filesystem::path& filename) const;
void debugPrintAll() const;
Expand Down
1 change: 1 addition & 0 deletions testsuite/OMSimulator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import_hierarchical_ssv_sources.lua \
import_parameter_mapping_from_ssm.lua \
import_parameter_mapping_inline.lua \
importStartValues.lua \
importStartValues.lua \
multipleConnections.lua \
partialSnapshot.lua \
PI_Controller.lua \
Expand Down

0 comments on commit 038f1c2

Please sign in to comment.