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

export partial snapshot #972

Merged
merged 8 commits into from
Mar 2, 2021
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
15 changes: 6 additions & 9 deletions src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,6 @@ oms_status_enu_t oms::Model::list(const oms::ComRef& cref, char** contents)

oms_status_enu_t oms::Model::exportSnapshot(const oms::ComRef& cref, char** contents)
{
// only top level model is allowed
if (!cref.isEmpty())
{
//return logError("only top level model is allowed, unknown model: " + std::string(cref));
return logError("\"" + std::string(getCref()+std::string(cref)) + "\" is not a top level model");
}

Snapshot snapshot;

pugi::xml_node oms_ssd = snapshot.newResourceNode("SystemStructure.ssd");
Expand All @@ -378,9 +371,13 @@ oms_status_enu_t oms::Model::exportSnapshot(const oms::ComRef& cref, char** cont
// TODO ssm file
}

snapshot.writeDocument(contents);
if (cref.isEmpty())
return snapshot.writeDocument(contents);

return oms_status_ok;
// query for partial snapshot
Snapshot partialSnapshot(true);
snapshot.exportPartialSnapshot(cref, partialSnapshot);
return partialSnapshot.writeDocument(contents);
}

oms_status_enu_t oms::Model::exportSSVTemplate(const oms::ComRef& cref, const std::string& filename)
Expand Down
8 changes: 7 additions & 1 deletion src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,16 @@ oms_status_enu_t oms_exportSnapshot(const char* cref_, char** contents)
{
oms::ComRef tail(cref_);
oms::ComRef front = tail.pop_front();
oms::Model* model = oms::Scope::GetInstance().getModel(front);

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

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

if (tail.isEmpty() && front.hasSuffix())
return model->exportSnapshot(oms::ComRef(":" + front.suffix()), contents);
return model->exportSnapshot(tail, contents);
}

Expand Down
76 changes: 75 additions & 1 deletion src/OMSimulatorLib/Snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@

#include <iostream>

oms::Snapshot::Snapshot()
oms::Snapshot::Snapshot(bool partial)
{
// set the document with the root node <oms:snapshot>
doc.append_child(oms::ssp::Version1_0::snap_shot);
pugi::xml_node oms_snapshot = doc.document_element();
oms_snapshot.append_attribute("partial") = partial ? "true" : "false";
}

oms::Snapshot::~Snapshot()
Expand Down Expand Up @@ -102,6 +104,17 @@ oms_status_enu_t oms::Snapshot::importResourceNode(const filesystem::path& filen
return oms_status_ok;
}

oms_status_enu_t oms::Snapshot::importPartialResourceNode(const filesystem::path& filename, const filesystem::path& nodename, 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);
oms_file.append_attribute("name") = filename.generic_string().c_str();
oms_file.append_attribute("node") = nodename.generic_string().c_str();
oms_file.append_copy(node);

return oms_status_ok;
}

void oms::Snapshot::getResources(std::vector<std::string>& resources) const
{
pugi::xml_node oms_snapshot = doc.document_element();
Expand Down Expand Up @@ -173,6 +186,67 @@ pugi::xml_node oms::Snapshot::getTemplateResourceNodeSSV(const filesystem::path&
return node_parameters;
}

oms_status_enu_t oms::Snapshot::exportPartialSnapshot(const ComRef& cref, Snapshot& partialSnapshot)
{
ComRef subCref(cref);
std::string suffix = subCref.pop_suffix();

// copy only single file
if (!suffix.empty() && subCref.isEmpty())
{
pugi::xml_node node = getResourceNode(filesystem::path(suffix));
if (!node)
return logError("Failed to find node \"" + suffix + "\"");

partialSnapshot.importResourceNode(filesystem::path(suffix), node);
}

// 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();

std::string nodeName = (ComRef(ssdNode.attribute("name").as_string()) + subCref);

if (tail.isEmpty())
{
// return system
if (systemNode.attribute("name").as_string() == std::string(front))
{
partialSnapshot.importPartialResourceNode("SystemStructure.ssd", nodeName, systemNode);
}
}
else
{
// iterate System
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))
{
partialSnapshot.importPartialResourceNode("SystemStructure.ssd", nodeName, *itElements);
}
}
}
}
}
}
}

return oms_status_ok;
}

oms_status_enu_t oms::Snapshot::writeDocument(char** contents)
{
class : public pugi::xml_writer
Expand Down
9 changes: 7 additions & 2 deletions src/OMSimulatorLib/Snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
#ifndef _OMS_SNAPSHOT_H_
#define _OMS_SNAPSHOT_H_

#include "ComRef.h"
#include "OMSFileSystem.h"
#include "Types.h"
#include "ssd/Tags.h"
#include "Types.h"

#include <pugixml.hpp>
#include <string>
Expand All @@ -45,7 +46,7 @@ namespace oms
class Snapshot
{
public:
Snapshot();
Snapshot(bool partial=false);
~Snapshot();

oms_status_enu_t import(const char* snapshot);
Expand All @@ -61,12 +62,16 @@ 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);

void debugPrintNode(const filesystem::path& filename) const;
void debugPrintAll() const;

oms_status_enu_t writeDocument(char** contents);

private:
oms_status_enu_t importPartialResourceNode(const filesystem::path& filename, const filesystem::path& nodename, const pugi::xml_node& node);

private:
// stop the compiler generating methods copying the object
Snapshot(Snapshot const& copy); ///< not implemented
Expand Down
1 change: 1 addition & 0 deletions testsuite/OMSimulator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import_parameter_mapping_from_ssm.lua \
import_parameter_mapping_inline.lua \
importStartValues.lua \
multipleConnections.lua \
partialSnapshot.lua \
PI_Controller.lua \
QuarterCarModel.DisplacementDisplacement.lua \
rename.lua \
Expand Down
2 changes: 1 addition & 1 deletion testsuite/OMSimulator/importStartValues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ oms_delete("importStartValues")
-- warning: Wrong/deprecated content detected but successfully loaded. Please re-export the SSP file to avoid this message.
-- warning: Wrong/deprecated content detected but successfully loaded. Please re-export the SSP file to avoid this message.
-- <?xml version="1.0"?>
-- <oms:snapshot>
-- <oms:snapshot partial="false">
-- <oms:file name="SystemStructure.ssd">
-- <ssd:SystemStructureDescription xmlns:ssc="http://ssp-standard.org/SSP1/SystemStructureCommon" xmlns:ssd="http://ssp-standard.org/SSP1/SystemStructureDescription" xmlns:ssv="http://ssp-standard.org/SSP1/SystemStructureParameterValues" xmlns:ssm="http://ssp-standard.org/SSP1/SystemStructureParameterMapping" xmlns:ssb="http://ssp-standard.org/SSP1/SystemStructureSignalDictionary" xmlns:oms="https://raw.githubusercontent.com/OpenModelica/OMSimulator/master/schema/oms.xsd" name="importStartValues" version="1.0">
-- <ssd:System name="root">
Expand Down
77 changes: 3 additions & 74 deletions testsuite/OMSimulator/import_export_snapshot.lua
Original file line number Diff line number Diff line change
@@ -1,112 +1,44 @@
-- status: correct
-- teardown_command: rm -rf import_export_snapshot_lua/
-- teardown_command: rm -rf import_export_snapshot_lua/ import_export_snapshot.ssp
-- linux: yes
-- mingw: yes
-- win: no
-- mac: no


oms_setCommandLineOption("--suppressPath=true --exportParametersInline=false")
status = oms_setTempDirectory("./import_export_snapshot_lua/")

oms_newModel("import_export_snapshot")
oms_addSystem("import_export_snapshot.root", oms_system_wc)

oms_addConnector("import_export_snapshot.root.C1", oms_causality_input, oms_signal_type_real)
oms_setReal("import_export_snapshot.root.C1", -10)

oms_addSubModel("import_export_snapshot.root.add", "../resources/Modelica.Blocks.Math.Add.fmu")

oms_setReal("import_export_snapshot.root.add.u1", 10)
oms_setReal("import_export_snapshot.root.add.k1", 30)

-- src1 = oms_list("import_export_snapshot")
-- print(src1)

oms_export("import_export_snapshot", "import_export_snapshot.ssp");
oms_delete("import_export_snapshot")

oms_importFile("import_export_snapshot.ssp");

src1 = oms_list("import_export_snapshot")
print(src1)

src2 = oms_exportSnapshot("import_export_snapshot")
print(src2)

oms_importSnapshot("import_export_snapshot", src2)

-- check of error msg
oms_exportSnapshot("import_export_snapshot.root.add")

oms_setStopTime("import_export_snapshot", 2)

oms_instantiate("import_export_snapshot")

oms_initialize("import_export_snapshot")
oms_simulate("import_export_snapshot")
oms_terminate("import_export_snapshot")
oms_delete("import_export_snapshot")


-- Result:
-- <?xml version="1.0"?>
-- <ssd:SystemStructureDescription xmlns:ssc="http://ssp-standard.org/SSP1/SystemStructureCommon" xmlns:ssd="http://ssp-standard.org/SSP1/SystemStructureDescription" xmlns:ssv="http://ssp-standard.org/SSP1/SystemStructureParameterValues" xmlns:ssm="http://ssp-standard.org/SSP1/SystemStructureParameterMapping" xmlns:ssb="http://ssp-standard.org/SSP1/SystemStructureSignalDictionary" xmlns:oms="https://raw.githubusercontent.com/OpenModelica/OMSimulator/master/schema/oms.xsd" name="import_export_snapshot" version="1.0">
-- <ssd:System name="root">
-- <ssd:Connectors>
-- <ssd:Connector name="C1" kind="input">
-- <ssc:Real />
-- </ssd:Connector>
-- </ssd:Connectors>
-- <ssd:ParameterBindings>
-- <ssd:ParameterBinding source="resources/import_export_snapshot.ssv" />
-- </ssd:ParameterBindings>
-- <ssd:Elements>
-- <ssd:Component name="add" type="application/x-fmu-sharedlibrary" source="resources/0001_add.fmu">
-- <ssd:Connectors>
-- <ssd:Connector name="u1" kind="input">
-- <ssc:Real />
-- <ssd:ConnectorGeometry x="0.000000" y="0.333333" />
-- </ssd:Connector>
-- <ssd:Connector name="u2" kind="input">
-- <ssc:Real />
-- <ssd:ConnectorGeometry x="0.000000" y="0.666667" />
-- </ssd:Connector>
-- <ssd:Connector name="y" kind="output">
-- <ssc:Real />
-- <ssd:ConnectorGeometry x="1.000000" y="0.500000" />
-- </ssd:Connector>
-- <ssd:Connector name="k1" kind="parameter">
-- <ssc:Real />
-- </ssd:Connector>
-- <ssd:Connector name="k2" kind="parameter">
-- <ssc:Real />
-- </ssd:Connector>
-- </ssd:Connectors>
-- </ssd:Component>
-- </ssd:Elements>
-- <ssd:Annotations>
-- <ssc:Annotation type="org.openmodelica">
-- <oms:Annotations>
-- <oms:SimulationInformation>
-- <oms:FixedStepMaster description="oms-ma" stepSize="0.100000" absoluteTolerance="0.000100" relativeTolerance="0.000100" />
-- </oms:SimulationInformation>
-- </oms:Annotations>
-- </ssc:Annotation>
-- </ssd:Annotations>
-- </ssd:System>
-- <ssd:DefaultExperiment startTime="0.000000" stopTime="1.000000">
-- <ssd:Annotations>
-- <ssc:Annotation type="org.openmodelica">
-- <oms:Annotations>
-- <oms:SimulationInformation resultFile="import_export_snapshot_res.mat" loggingInterval="0.000000" bufferSize="10" signalFilter=".*" />
-- </oms:Annotations>
-- </ssc:Annotation>
-- </ssd:Annotations>
-- </ssd:DefaultExperiment>
-- </ssd:SystemStructureDescription>
--
-- <?xml version="1.0"?>
-- <oms:snapshot>
-- <oms:snapshot partial="false">
-- <oms:file name="SystemStructure.ssd">
-- <ssd:SystemStructureDescription xmlns:ssc="http://ssp-standard.org/SSP1/SystemStructureCommon" xmlns:ssd="http://ssp-standard.org/SSP1/SystemStructureDescription" xmlns:ssv="http://ssp-standard.org/SSP1/SystemStructureParameterValues" xmlns:ssm="http://ssp-standard.org/SSP1/SystemStructureParameterMapping" xmlns:ssb="http://ssp-standard.org/SSP1/SystemStructureSignalDictionary" xmlns:oms="https://raw.githubusercontent.com/OpenModelica/OMSimulator/master/schema/oms.xsd" name="import_export_snapshot" version="1.0">
-- <ssd:System name="root">
Expand Down Expand Up @@ -180,8 +112,5 @@ oms_delete("import_export_snapshot")
-- </oms:file>
-- </oms:snapshot>
--
-- error: [exportSnapshot] "import_export_snapshot.root.add" is not a top level model
-- info: Result file: import_export_snapshot_res.mat (bufferSize=10)
-- info: 0 warnings
-- info: 1 errors
-- endResult
Loading