From 47b15283eddc44681ec0d0439a952c85557fe514 Mon Sep 17 00:00:00 2001 From: Lennart Ochel Date: Fri, 26 Feb 2021 10:48:12 +0100 Subject: [PATCH 1/2] Update the importSnapshot interface --- doc/UsersGuide/source/api/importSnapshot.rst | 9 +++++---- src/OMSimulatorLib/OMSimulator.cpp | 10 ++-------- src/OMSimulatorLib/OMSimulator.h | 2 +- src/OMSimulatorLib/Scope.cpp | 18 +++++++++++++++++ src/OMSimulatorLib/Scope.h | 1 + src/OMSimulatorLua/OMSimulatorLua.c | 11 ++++++---- src/OMSimulatorPython/capi.py | 21 ++++++++++---------- 7 files changed, 45 insertions(+), 27 deletions(-) diff --git a/doc/UsersGuide/source/api/importSnapshot.rst b/doc/UsersGuide/source/api/importSnapshot.rst index f0b3dfa0a..93e259445 100644 --- a/doc/UsersGuide/source/api/importSnapshot.rst +++ b/doc/UsersGuide/source/api/importSnapshot.rst @@ -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# diff --git a/src/OMSimulatorLib/OMSimulator.cpp b/src/OMSimulatorLib/OMSimulator.cpp index 589e7b618..6206d207b 100644 --- a/src/OMSimulatorLib/OMSimulator.cpp +++ b/src/OMSimulatorLib/OMSimulator.cpp @@ -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) diff --git a/src/OMSimulatorLib/OMSimulator.h b/src/OMSimulatorLib/OMSimulator.h index 962ddd0ff..69fc7dea9 100644 --- a/src/OMSimulatorLib/OMSimulator.h +++ b/src/OMSimulatorLib/OMSimulator.h @@ -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); diff --git a/src/OMSimulatorLib/Scope.cpp b/src/OMSimulatorLib/Scope.cpp index 427c5d860..29c3d34f4 100644 --- a/src/OMSimulatorLib/Scope.cpp +++ b/src/OMSimulatorLib/Scope.cpp @@ -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; +} diff --git a/src/OMSimulatorLib/Scope.h b/src/OMSimulatorLib/Scope.h index e421a6d8a..a71287a96 100644 --- a/src/OMSimulatorLib/Scope.h +++ b/src/OMSimulatorLib/Scope.h @@ -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(); diff --git a/src/OMSimulatorLua/OMSimulatorLua.c b/src/OMSimulatorLua/OMSimulatorLua.c index 56352eb1b..f78963462 100644 --- a/src/OMSimulatorLua/OMSimulatorLua.c +++ b/src/OMSimulatorLua/OMSimulatorLua.c @@ -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) @@ -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) @@ -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); diff --git a/src/OMSimulatorPython/capi.py b/src/OMSimulatorPython/capi.py index 5682d5b7b..32238125a 100644 --- a/src/OMSimulatorPython/capi.py +++ b/src/OMSimulatorPython/capi.py @@ -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 @@ -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): @@ -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)) @@ -254,14 +254,15 @@ def getVariableStepSize(self, cref): status = self.obj.oms_getVariableStepSize(cref.encode(), ctypes.byref(initialStepSize), ctypes.byref(minimumStepSize), ctypes.byref(maximumStepSize)) return [initialStepSize.value, minimumStepSize.value, maximumStepSize.value, status] def getVersion(self): - return self.obj.oms_getVersion().decode('utf-8') + return self.obj.oms_getVersion().decode('utf- if oms_getVersion() else None8') 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): @@ -269,15 +270,15 @@ def instantiate(self, cref): 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): From 84cd307f98a7e98c439780a106df0b54208cf14d Mon Sep 17 00:00:00 2001 From: Lennart Ochel Date: Fri, 26 Feb 2021 11:52:12 +0100 Subject: [PATCH 2/2] Fix getVersion --- src/OMSimulatorPython/capi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OMSimulatorPython/capi.py b/src/OMSimulatorPython/capi.py index 32238125a..61fc0c0ac 100644 --- a/src/OMSimulatorPython/capi.py +++ b/src/OMSimulatorPython/capi.py @@ -254,7 +254,7 @@ def getVariableStepSize(self, cref): status = self.obj.oms_getVariableStepSize(cref.encode(), ctypes.byref(initialStepSize), ctypes.byref(minimumStepSize), ctypes.byref(maximumStepSize)) return [initialStepSize.value, minimumStepSize.value, maximumStepSize.value, status] def getVersion(self): - return self.obj.oms_getVersion().decode('utf- if oms_getVersion() else None8') + return self.obj.oms_getVersion().decode('utf-8') def importFile(self, filename): cref = ctypes.c_char_p() status = self.obj.oms_importFile(filename.encode(), ctypes.byref(cref))