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

Update the importSnapshot interface #969

Merged
merged 2 commits into from
Feb 26, 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
9 changes: 5 additions & 4 deletions doc/UsersGuide/source/api/importSnapshot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
importSnapshot
--------------

Loads a snapshot to restore a previous model state. The model must be in virgin model state, which means it must not be instantiated.
Loads a snapshot to restore a previous model state. The model must be
in virgin model state, which means it must not be instantiated.
#END#

#LUA#
.. code-block:: lua

status = oms_importSnapshot(cref, snapshot)
newCref, status = oms_importSnapshot(cref, snapshot)

#END#

#PYTHON#
.. code-block:: python

status = oms.importSnapshot(cref, snapshot)
newCref, status = oms.importSnapshot(cref, snapshot)

#END#

#CAPI#
.. code-block:: c

oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot);
oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot, char** newCref);

#END#

Expand Down
10 changes: 2 additions & 8 deletions src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,9 @@ oms_status_enu_t oms_loadSnapshot(const char* cref, const char* snapshot, char**
return oms::Scope::GetInstance().loadSnapshot(oms::ComRef(cref), snapshot, newCref);
}

oms_status_enu_t oms_importSnapshot(const char* cref_, const char* snapshot)
oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot, char** newCref)
{
oms::ComRef cref(cref_);

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

return model->importSnapshot(snapshot);
return oms::Scope::GetInstance().importSnapshot(oms::ComRef(cref), snapshot, newCref);
}

oms_status_enu_t oms_addSystem(const char* cref_, oms_system_enu_t type)
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/OMSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ OMSAPI oms_status_enu_t OMSCALL oms_getVariableStepSize(const char* cref, double
OMSAPI const char* OMSCALL oms_getVersion();
OMSAPI oms_status_enu_t OMSCALL oms_faultInjection(const char* signal, oms_fault_type_enu_t faultType, double faultValue);
OMSAPI oms_status_enu_t OMSCALL oms_importFile(const char* filename, char** cref);
OMSAPI oms_status_enu_t OMSCALL oms_importSnapshot(const char* cref, const char* snapshot);
OMSAPI oms_status_enu_t OMSCALL oms_importSnapshot(const char* cref, const char* snapshot, char** newCref);
OMSAPI oms_status_enu_t OMSCALL oms_initialize(const char* cref);
OMSAPI oms_status_enu_t OMSCALL oms_instantiate(const char* cref);
OMSAPI oms_status_enu_t OMSCALL oms_list(const char* cref, char** contents);
Expand Down
18 changes: 18 additions & 0 deletions src/OMSimulatorLib/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,21 @@ oms_status_enu_t oms::Scope::loadSnapshot(const oms::ComRef& cref, const char* s

return status;
}

// TODO: renaming not yet supported
oms_status_enu_t oms::Scope::importSnapshot(const oms::ComRef& cref, const char* snapshot, char** newCref)
{
if (newCref)
*newCref = NULL;

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

oms_status_enu_t status = model->importSnapshot(snapshot);

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

return status;
}
1 change: 1 addition & 0 deletions src/OMSimulatorLib/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace oms
Model* getModel(const ComRef& cref);

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

const std::string& getTempDirectory() const {return GetInstance().tempDir;}
std::string getWorkingDirectory();
Expand Down
11 changes: 7 additions & 4 deletions src/OMSimulatorLua/OMSimulatorLua.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ static int OMSimulatorLua_oms_listUnconnectedConnectors(lua_State *L)
return 2;
}

//oms_status_enu_t oms_loadSnapshot(const char* cref, const char* snapshot);
//oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot, char** newCref);
static int OMSimulatorLua_oms_loadSnapshot(lua_State *L)
{
if (lua_gettop(L) != 2)
Expand All @@ -349,7 +349,7 @@ static int OMSimulatorLua_oms_loadSnapshot(lua_State *L)
return 2;
}

//oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot);
//oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot, char** newCref);
static int OMSimulatorLua_oms_importSnapshot(lua_State *L)
{
if (lua_gettop(L) != 2)
Expand All @@ -359,10 +359,13 @@ static int OMSimulatorLua_oms_importSnapshot(lua_State *L)

const char* cref = lua_tostring(L, 1);
const char* snapshot = lua_tostring(L, 2);
oms_status_enu_t status = oms_importSnapshot(cref, snapshot);

char* newCref = NULL;
oms_status_enu_t status = oms_importSnapshot(cref, snapshot, &newCref);

lua_pushstring(L, newCref ? newCref : "");
lua_pushinteger(L, status);
return 1;
return 2;
}

//oms_status_enu_t oms_exportDependencyGraphs(const char* cref, const char* initialization, const char* event, const char* simulation);
Expand Down
19 changes: 10 additions & 9 deletions src/OMSimulatorPython/capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(self):
self.obj.oms_getVersion.restype = ctypes.c_char_p
self.obj.oms_importFile.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
self.obj.oms_importFile.restype = ctypes.c_int
self.obj.oms_importSnapshot.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
self.obj.oms_importSnapshot.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
self.obj.oms_importSnapshot.restype = ctypes.c_int
self.obj.oms_initialize.argtypes = [ctypes.c_char_p]
self.obj.oms_initialize.restype = ctypes.c_int
Expand Down Expand Up @@ -199,7 +199,7 @@ def exportDependencyGraphs(self, cref, initialization, event, simulation):
def exportSnapshot(self, ident):
contents = ctypes.c_char_p()
status = self.obj.oms_exportSnapshot(ident.encode(), ctypes.byref(contents))
return [contents.value.decode('utf-8'), status]
return [contents.value.decode('utf-8') if contents.value else None, status]
def exportSSMTemplate(self, ident, filename):
return self.obj.oms_exportSSMTemplate(ident.encode(), filename.encode())
def exportSSVTemplate(self, ident, filename):
Expand All @@ -226,7 +226,7 @@ def getResultFile(self, cref):
filename = ctypes.c_char_p()
bufferSize = ctypes.c_int()
status = self.obj.oms_getResultFile(cref.encode(), ctypes.byref(filename), ctypes.byref(bufferSize))
return [filename.value.decode('utf-8'), bufferSize.value, status]
return [filename.value.decode('utf-8') if filename.value else None, bufferSize.value, status]
def getSolver(self, cref):
value = ctypes.c_int()
status = self.obj.oms_getSolver(cref.encode(), ctypes.byref(value))
Expand Down Expand Up @@ -258,26 +258,27 @@ def getVersion(self):
def importFile(self, filename):
cref = ctypes.c_char_p()
status = self.obj.oms_importFile(filename.encode(), ctypes.byref(cref))
return [cref.value.decode("utf-8"), status]
return [cref.value.decode('utf-8') if cref.value else None, status]
def importSnapshot(self, ident, snapshot):
status = self.obj.oms_importSnapshot(ident.encode(), snapshot.encode())
return status
newCref = ctypes.c_char_p()
status = self.obj.oms_importSnapshot(ident.encode(), snapshot.encode(), ctypes.byref(newCref))
return [newCref.value.decode('utf-8') if newCref.value else None, status]
def initialize(self, cref):
return self.obj.oms_initialize(cref.encode())
def instantiate(self, cref):
return self.obj.oms_instantiate(cref.encode())
def list(self, ident):
contents = ctypes.c_char_p()
status = self.obj.oms_list(ident.encode(), ctypes.byref(contents))
return [contents.value.decode('utf-8'), status]
return [contents.value.decode('utf-8') if contents.value else None, status]
def listUnconnectedConnectors(self, ident):
contents = ctypes.c_char_p()
status = self.obj.oms_listUnconnectedConnectors(ident.encode(), ctypes.byref(contents))
return [contents.value.decode('utf-8'), status]
return [contents.value.decode('utf-8') if contents.value else None, status]
def loadSnapshot(self, ident, snapshot):
newCref = ctypes.c_char_p()
status = self.obj.oms_loadSnapshot(ident.encode(), snapshot.encode(), ctypes.byref(newCref))
return [newCref.value.decode("utf-8"), status]
return [newCref.value.decode('utf-8') if newCref.value else None, status]
def newModel(self, cref):
return self.obj.oms_newModel(cref.encode())
def removeSignalsFromResults(self, cref, regex):
Expand Down