Skip to content

Commit e6b6eea

Browse files
authored
Update the importSnapshot interface (#969)
1 parent b754feb commit e6b6eea

File tree

7 files changed

+44
-26
lines changed

7 files changed

+44
-26
lines changed

doc/UsersGuide/source/api/importSnapshot.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22
importSnapshot
33
--------------
44

5-
Loads a snapshot to restore a previous model state. The model must be in virgin model state, which means it must not be instantiated.
5+
Loads a snapshot to restore a previous model state. The model must be
6+
in virgin model state, which means it must not be instantiated.
67
#END#
78

89
#LUA#
910
.. code-block:: lua
1011
11-
status = oms_importSnapshot(cref, snapshot)
12+
newCref, status = oms_importSnapshot(cref, snapshot)
1213
1314
#END#
1415

1516
#PYTHON#
1617
.. code-block:: python
1718
18-
status = oms.importSnapshot(cref, snapshot)
19+
newCref, status = oms.importSnapshot(cref, snapshot)
1920
2021
#END#
2122

2223
#CAPI#
2324
.. code-block:: c
2425
25-
oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot);
26+
oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot, char** newCref);
2627
2728
#END#
2829

src/OMSimulatorLib/OMSimulator.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,9 @@ oms_status_enu_t oms_loadSnapshot(const char* cref, const char* snapshot, char**
268268
return oms::Scope::GetInstance().loadSnapshot(oms::ComRef(cref), snapshot, newCref);
269269
}
270270

271-
oms_status_enu_t oms_importSnapshot(const char* cref_, const char* snapshot)
271+
oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot, char** newCref)
272272
{
273-
oms::ComRef cref(cref_);
274-
275-
oms::Model* model = oms::Scope::GetInstance().getModel(cref);
276-
if (!model)
277-
return logError_ModelNotInScope(cref);
278-
279-
return model->importSnapshot(snapshot);
273+
return oms::Scope::GetInstance().importSnapshot(oms::ComRef(cref), snapshot, newCref);
280274
}
281275

282276
oms_status_enu_t oms_addSystem(const char* cref_, oms_system_enu_t type)

src/OMSimulatorLib/OMSimulator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ OMSAPI oms_status_enu_t OMSCALL oms_getVariableStepSize(const char* cref, double
116116
OMSAPI const char* OMSCALL oms_getVersion();
117117
OMSAPI oms_status_enu_t OMSCALL oms_faultInjection(const char* signal, oms_fault_type_enu_t faultType, double faultValue);
118118
OMSAPI oms_status_enu_t OMSCALL oms_importFile(const char* filename, char** cref);
119-
OMSAPI oms_status_enu_t OMSCALL oms_importSnapshot(const char* cref, const char* snapshot);
119+
OMSAPI oms_status_enu_t OMSCALL oms_importSnapshot(const char* cref, const char* snapshot, char** newCref);
120120
OMSAPI oms_status_enu_t OMSCALL oms_initialize(const char* cref);
121121
OMSAPI oms_status_enu_t OMSCALL oms_instantiate(const char* cref);
122122
OMSAPI oms_status_enu_t OMSCALL oms_list(const char* cref, char** contents);

src/OMSimulatorLib/Scope.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,21 @@ oms_status_enu_t oms::Scope::loadSnapshot(const oms::ComRef& cref, const char* s
428428

429429
return status;
430430
}
431+
432+
// TODO: renaming not yet supported
433+
oms_status_enu_t oms::Scope::importSnapshot(const oms::ComRef& cref, const char* snapshot, char** newCref)
434+
{
435+
if (newCref)
436+
*newCref = NULL;
437+
438+
oms::Model* model = oms::Scope::GetInstance().getModel(cref);
439+
if (!model)
440+
return logError_ModelNotInScope(cref);
441+
442+
oms_status_enu_t status = model->importSnapshot(snapshot);
443+
444+
if (newCref)
445+
*newCref = (char*)getModel(cref)->getCref().c_str();
446+
447+
return status;
448+
}

src/OMSimulatorLib/Scope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace oms
7171
Model* getModel(const ComRef& cref);
7272

7373
oms_status_enu_t loadSnapshot(const ComRef& cref, const char* snapshot, char** newCref);
74+
oms_status_enu_t importSnapshot(const ComRef& cref, const char* snapshot, char** newCref);
7475

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

src/OMSimulatorLua/OMSimulatorLua.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static int OMSimulatorLua_oms_listUnconnectedConnectors(lua_State *L)
330330
return 2;
331331
}
332332

333-
//oms_status_enu_t oms_loadSnapshot(const char* cref, const char* snapshot);
333+
//oms_status_enu_t oms_importSnapshot(const char* cref, const char* snapshot, char** newCref);
334334
static int OMSimulatorLua_oms_loadSnapshot(lua_State *L)
335335
{
336336
if (lua_gettop(L) != 2)
@@ -349,7 +349,7 @@ static int OMSimulatorLua_oms_loadSnapshot(lua_State *L)
349349
return 2;
350350
}
351351

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

360360
const char* cref = lua_tostring(L, 1);
361361
const char* snapshot = lua_tostring(L, 2);
362-
oms_status_enu_t status = oms_importSnapshot(cref, snapshot);
363362

363+
char* newCref = NULL;
364+
oms_status_enu_t status = oms_importSnapshot(cref, snapshot, &newCref);
365+
366+
lua_pushstring(L, newCref ? newCref : "");
364367
lua_pushinteger(L, status);
365-
return 1;
368+
return 2;
366369
}
367370

368371
//oms_status_enu_t oms_exportDependencyGraphs(const char* cref, const char* initialization, const char* event, const char* simulation);

src/OMSimulatorPython/capi.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(self):
8686
self.obj.oms_getVersion.restype = ctypes.c_char_p
8787
self.obj.oms_importFile.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
8888
self.obj.oms_importFile.restype = ctypes.c_int
89-
self.obj.oms_importSnapshot.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
89+
self.obj.oms_importSnapshot.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
9090
self.obj.oms_importSnapshot.restype = ctypes.c_int
9191
self.obj.oms_initialize.argtypes = [ctypes.c_char_p]
9292
self.obj.oms_initialize.restype = ctypes.c_int
@@ -199,7 +199,7 @@ def exportDependencyGraphs(self, cref, initialization, event, simulation):
199199
def exportSnapshot(self, ident):
200200
contents = ctypes.c_char_p()
201201
status = self.obj.oms_exportSnapshot(ident.encode(), ctypes.byref(contents))
202-
return [contents.value.decode('utf-8'), status]
202+
return [contents.value.decode('utf-8') if contents.value else None, status]
203203
def exportSSMTemplate(self, ident, filename):
204204
return self.obj.oms_exportSSMTemplate(ident.encode(), filename.encode())
205205
def exportSSVTemplate(self, ident, filename):
@@ -226,7 +226,7 @@ def getResultFile(self, cref):
226226
filename = ctypes.c_char_p()
227227
bufferSize = ctypes.c_int()
228228
status = self.obj.oms_getResultFile(cref.encode(), ctypes.byref(filename), ctypes.byref(bufferSize))
229-
return [filename.value.decode('utf-8'), bufferSize.value, status]
229+
return [filename.value.decode('utf-8') if filename.value else None, bufferSize.value, status]
230230
def getSolver(self, cref):
231231
value = ctypes.c_int()
232232
status = self.obj.oms_getSolver(cref.encode(), ctypes.byref(value))
@@ -258,26 +258,27 @@ def getVersion(self):
258258
def importFile(self, filename):
259259
cref = ctypes.c_char_p()
260260
status = self.obj.oms_importFile(filename.encode(), ctypes.byref(cref))
261-
return [cref.value.decode("utf-8"), status]
261+
return [cref.value.decode('utf-8') if cref.value else None, status]
262262
def importSnapshot(self, ident, snapshot):
263-
status = self.obj.oms_importSnapshot(ident.encode(), snapshot.encode())
264-
return status
263+
newCref = ctypes.c_char_p()
264+
status = self.obj.oms_importSnapshot(ident.encode(), snapshot.encode(), ctypes.byref(newCref))
265+
return [newCref.value.decode('utf-8') if newCref.value else None, status]
265266
def initialize(self, cref):
266267
return self.obj.oms_initialize(cref.encode())
267268
def instantiate(self, cref):
268269
return self.obj.oms_instantiate(cref.encode())
269270
def list(self, ident):
270271
contents = ctypes.c_char_p()
271272
status = self.obj.oms_list(ident.encode(), ctypes.byref(contents))
272-
return [contents.value.decode('utf-8'), status]
273+
return [contents.value.decode('utf-8') if contents.value else None, status]
273274
def listUnconnectedConnectors(self, ident):
274275
contents = ctypes.c_char_p()
275276
status = self.obj.oms_listUnconnectedConnectors(ident.encode(), ctypes.byref(contents))
276-
return [contents.value.decode('utf-8'), status]
277+
return [contents.value.decode('utf-8') if contents.value else None, status]
277278
def loadSnapshot(self, ident, snapshot):
278279
newCref = ctypes.c_char_p()
279280
status = self.obj.oms_loadSnapshot(ident.encode(), snapshot.encode(), ctypes.byref(newCref))
280-
return [newCref.value.decode("utf-8"), status]
281+
return [newCref.value.decode('utf-8') if newCref.value else None, status]
281282
def newModel(self, cref):
282283
return self.obj.oms_newModel(cref.encode())
283284
def removeSignalsFromResults(self, cref, regex):

0 commit comments

Comments
 (0)