From: dmv Date: Thu, 8 Oct 2009 14:03:18 +0000 (+0000) Subject: 0020523: String notebook support X-Git-Tag: V5_1_3rc1~10 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=fbd8223f5fc5435c273607f3c8890be08aa1304a;p=modules%2Fkernel.git 0020523: String notebook support --- diff --git a/idl/SALOMEDS.idl b/idl/SALOMEDS.idl index 8e44c7438..4325a3875 100644 --- a/idl/SALOMEDS.idl +++ b/idl/SALOMEDS.idl @@ -427,6 +427,18 @@ during each working session. */ void SetBoolean( in string theVarName, in boolean theValue ); +/*! \brief Create string variable with Name theVarName and value theValue + + (or set if variable value into theValue already exists) + \param theVarName is a name of the variable + \param theVarName is a value of the variable. +*/ + void SetString( in string theVarName, in string theValue ); + +/*! \brief Set current value as double for string variable +*/ + void SetStringAsDouble( in string theVarName, in double theValue ); + /*! \brief Get value of a real variable \param theVarName is a name of the variable. @@ -444,6 +456,12 @@ during each working session. \param theVarName is a name of the variable. */ boolean GetBoolean( in string theVarName ); + +/*! \brief Get value of a string variable + + \param theVarName is a name of the variable. +*/ + string GetString( in string theVarName ); /*! \brief Indicate if a variable is real @@ -467,6 +485,13 @@ during each working session. */ boolean IsBoolean( in string theVarName ); +/*! \brief Indicate if a variable is string + + Return true if variable is string otherwise return false. + \param theVarName is a name of the variable. +*/ + boolean IsString( in string theVarName ); + /*! \brief Indicate if a variable exists in the study Return true if variable exists in the study, diff --git a/src/KERNEL_PY/salome_notebook.py b/src/KERNEL_PY/salome_notebook.py index cdc5a5ad1..48404bff9 100644 --- a/src/KERNEL_PY/salome_notebook.py +++ b/src/KERNEL_PY/salome_notebook.py @@ -29,6 +29,44 @@ Module salome_notebook gives access to Salome Notebook. import salome +class PseudoStudyForNoteBook(object): + + def __init__(self, **kwargs): + self.kwargs = kwargs + pass + + def GetVariableNames(self): + return self.kwargs.keys() + + def IsVariable(self, variableName): + return variableName in self.kwargs + + def IsReal(self, variableName): + val = self.kwargs[variableName] + try: + float(val) + return True + except: + pass + return False + + IsInteger = IsReal + IsBoolean = IsReal + + def IsString(self, variableName): + return not self.IsReal(variableName) + + def GetString(self, variableName): + return self.kwargs[variableName] + + def GetReal(self, variableName): + return float(self.kwargs[variableName]) + + GetInteger = GetReal + GetBoolean = GetReal + + pass + class NoteBook: def __init__(self, Study): @@ -48,6 +86,9 @@ class NoteBook: elif type(variable) == bool: self.myStudy.SetBoolean(variableName, variable) + elif type(variable) == str: + self.myStudy.SetString(variableName, variable) + def get(self, variableName): """ Return value of the variable with name "variableName". @@ -63,6 +104,33 @@ class NoteBook: elif self.myStudy.IsBoolean(variableName): aResult = self.myStudy.GetBoolean(variableName) + + elif self.myStudy.IsString(variableName): + aResult = self.myStudy.GetString(variableName) + aResult_orig = aResult + l = self.myStudy.GetVariableNames() + l.remove(variableName) + for name in l: + val = self.get(name) + import re + while 1: + m = re.search(r"\b(%s)\b"%name, aResult) + if not m: break + aResult = aResult[:m.start()] + "%s"%(val) + aResult[m.end():] + pass + pass + try: + aResult = eval(aResult) + except Exception, e: + msg = str(e) + msg += "\n" + msg += "A problem occurs while parsing " + msg += "the variable %s "%(variableName.__repr__()) + msg += "with value %s ..."%(aResult_orig.__repr__()) + msg += "\n" + msg += "Please, check your notebook !" + raise Exception(msg) + pass return aResult @@ -72,5 +140,37 @@ class NoteBook: exists in the study, otherwise return false. """ return self.myStudy.IsVariable(variableName) - -notebook = NoteBook(salome.myStudy) \ No newline at end of file + + def setAs(self, variableName, typ): + value = self.get(variableName) + value = float(typ(value)) + self.myStudy.SetStringAsDouble(variableName, value) + return + + def setAsReal(self, variableName): + self.setAs(variableName, float) + return + + def setAsInteger(self, variableName): + self.setAs(variableName, int) + return + + def setAsBool(self, variableName): + self.setAs(variableName, bool) + return + + def check(self): + for variableName in self.myStudy.GetVariableNames(): + self.get(variableName) + pass + return + + pass + +def checkThisNoteBook(**kwargs): + study = PseudoStudyForNoteBook(**kwargs) + note_book = NoteBook(study) + note_book.check() + return + +notebook = NoteBook(salome.myStudy) diff --git a/src/SALOMEDS/SALOMEDS_Study.cxx b/src/SALOMEDS/SALOMEDS_Study.cxx index bbd079251..d25086e27 100644 --- a/src/SALOMEDS/SALOMEDS_Study.cxx +++ b/src/SALOMEDS/SALOMEDS_Study.cxx @@ -717,6 +717,30 @@ void SALOMEDS_Study::SetBoolean(const string& theVarName, const bool theValue) _corba_impl->SetBoolean((char*)theVarName.c_str(),theValue); } +void SALOMEDS_Study::SetString(const string& theVarName, const string& theValue) +{ + if (_isLocal) { + SALOMEDS::Locker lock; + _local_impl->SetStringVariable(theVarName, + theValue, + SALOMEDSImpl_GenericVariable::STRING_VAR); + } + else + _corba_impl->SetString((char*)theVarName.c_str(),(char*)theValue.c_str()); +} + +void SALOMEDS_Study::SetStringAsDouble(const string& theVarName, const double theValue) +{ + if (_isLocal) { + SALOMEDS::Locker lock; + _local_impl->SetStringVariableAsDouble(theVarName, + theValue, + SALOMEDSImpl_GenericVariable::STRING_VAR); + } + else + _corba_impl->SetStringAsDouble((char*)theVarName.c_str(),theValue); +} + double SALOMEDS_Study::GetReal(const string& theVarName) { double aResult; @@ -753,6 +777,18 @@ bool SALOMEDS_Study::GetBoolean(const string& theVarName) return aResult; } +std::string SALOMEDS_Study::GetString(const string& theVarName) +{ + std::string aResult; + if (_isLocal) { + SALOMEDS::Locker lock; + aResult = _local_impl->GetStringVariableValue(theVarName); + } + else + aResult = _corba_impl->GetString((char*)theVarName.c_str()); + return aResult; +} + bool SALOMEDS_Study::IsReal(const string& theVarName) { bool aResult; @@ -792,6 +828,19 @@ bool SALOMEDS_Study::IsBoolean(const string& theVarName) return aResult; } +bool SALOMEDS_Study::IsString(const string& theVarName) +{ + bool aResult; + if (_isLocal) { + SALOMEDS::Locker lock; + aResult = _local_impl->IsTypeOf(theVarName, + SALOMEDSImpl_GenericVariable::STRING_VAR); + } + else + aResult = _corba_impl->IsString((char*)theVarName.c_str()); + return aResult; +} + bool SALOMEDS_Study::IsVariable(const string& theVarName) { bool aResult; diff --git a/src/SALOMEDS/SALOMEDS_Study.hxx b/src/SALOMEDS/SALOMEDS_Study.hxx index d6784b0ec..91ae9d64c 100644 --- a/src/SALOMEDS/SALOMEDS_Study.hxx +++ b/src/SALOMEDS/SALOMEDS_Study.hxx @@ -101,14 +101,18 @@ public: virtual void SetReal(const std::string& theVarName, const double theValue); virtual void SetInteger(const std::string& theVarName, const int theValue); virtual void SetBoolean(const std::string& theVarName, const bool theValue); + virtual void SetString(const std::string& theVarName, const std::string& theValue); + virtual void SetStringAsDouble(const std::string& theVarName, const double theValue); virtual double GetReal(const std::string& theVarName); virtual int GetInteger(const std::string& theVarName); virtual bool GetBoolean(const std::string& theVarName); + virtual std::string GetString(const std::string& theVarName); virtual bool IsReal(const std::string& theVarName); virtual bool IsInteger(const std::string& theVarName); virtual bool IsBoolean(const std::string& theVarName); + virtual bool IsString(const std::string& theVarName); virtual bool IsVariable(const std::string& theVarName); virtual std::vector GetVariableNames(); diff --git a/src/SALOMEDS/SALOMEDS_Study_i.cxx b/src/SALOMEDS/SALOMEDS_Study_i.cxx index 6b90ace27..bbb4855a6 100644 --- a/src/SALOMEDS/SALOMEDS_Study_i.cxx +++ b/src/SALOMEDS/SALOMEDS_Study_i.cxx @@ -917,6 +917,30 @@ void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValu SALOMEDSImpl_GenericVariable::BOOLEAN_VAR); } +//============================================================================ +/*! Function : SetString + * Purpose : + */ +//============================================================================ +void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue) +{ + _impl->SetStringVariable(string(theVarName), + theValue, + SALOMEDSImpl_GenericVariable::STRING_VAR); +} + +//============================================================================ +/*! Function : SetStringAsDouble + * Purpose : + */ +//============================================================================ +void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue) +{ + _impl->SetStringVariableAsDouble(string(theVarName), + theValue, + SALOMEDSImpl_GenericVariable::STRING_VAR); +} + //============================================================================ /*! Function : GetReal * Purpose : @@ -947,6 +971,16 @@ CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName) return (bool)_impl->GetVariableValue(string(theVarName)); } +//============================================================================ +/*! Function : GetString + * Purpose : + */ +//============================================================================ +char* SALOMEDS_Study_i::GetString(const char* theVarName) +{ + return CORBA::string_dup(_impl->GetStringVariableValue(string(theVarName)).c_str()); +} + //============================================================================ /*! Function : IsReal * Purpose : @@ -980,6 +1014,17 @@ CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName) SALOMEDSImpl_GenericVariable::BOOLEAN_VAR); } +//============================================================================ +/*! Function : IsString + * Purpose : + */ +//============================================================================ +CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName) +{ + return _impl->IsTypeOf(string(theVarName), + SALOMEDSImpl_GenericVariable::STRING_VAR); +} + //============================================================================ /*! Function : IsVariable * Purpose : diff --git a/src/SALOMEDS/SALOMEDS_Study_i.hxx b/src/SALOMEDS/SALOMEDS_Study_i.hxx index d53e660d6..bd8fd2b5a 100644 --- a/src/SALOMEDS/SALOMEDS_Study_i.hxx +++ b/src/SALOMEDS/SALOMEDS_Study_i.hxx @@ -304,18 +304,26 @@ public: virtual void SetBoolean(const char* theVarName, CORBA::Boolean theValue); + virtual void SetString(const char* theVarName, const char* theValue); + + virtual void SetStringAsDouble(const char* theVarName, CORBA::Double theValue); + virtual CORBA::Double GetReal(const char* theVarName); virtual CORBA::Long GetInteger(const char* theVarName); virtual CORBA::Boolean GetBoolean(const char* theVarName); + virtual char* GetString(const char* theVarName); + virtual CORBA::Boolean IsReal(const char* theVarName); virtual CORBA::Boolean IsInteger(const char* theVarName); virtual CORBA::Boolean IsBoolean(const char* theVarName); + virtual CORBA::Boolean IsString(const char* theVarName); + virtual CORBA::Boolean IsVariable(const char* theVarName); virtual SALOMEDS::ListOfStrings* GetVariableNames(); diff --git a/src/SALOMEDSClient/SALOMEDSClient_Study.hxx b/src/SALOMEDSClient/SALOMEDSClient_Study.hxx index 3177e9ba9..c63e258c9 100644 --- a/src/SALOMEDSClient/SALOMEDSClient_Study.hxx +++ b/src/SALOMEDSClient/SALOMEDSClient_Study.hxx @@ -97,14 +97,18 @@ public: virtual void SetReal(const std::string& theVarName, const double theValue) = 0; virtual void SetInteger(const std::string& theVarName, const int theValue) = 0; virtual void SetBoolean(const std::string& theVarName, const bool theValue) = 0; + virtual void SetString(const std::string& theVarName, const std::string& theValue) = 0; + virtual void SetStringAsDouble(const std::string& theVarName, const double theValue) = 0; virtual double GetReal(const std::string& theVarName) = 0; virtual int GetInteger(const std::string& theVarName) = 0; virtual bool GetBoolean(const std::string& theVarName) = 0; + virtual std::string GetString(const std::string& theVarName) = 0; virtual bool IsReal(const std::string& theVarName) = 0; virtual bool IsInteger(const std::string& theVarName) = 0; virtual bool IsBoolean(const std::string& theVarName) = 0; + virtual bool IsString(const std::string& theVarName) = 0; virtual bool IsVariable(const std::string& theVarName) = 0; virtual std::vector GetVariableNames() = 0; diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx b/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx index 5dc385cc0..3e74897a3 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx @@ -35,7 +35,7 @@ class SALOMEDSIMPL_EXPORT SALOMEDSImpl_GenericVariable public: //Supported types of the nootebook variables - enum VariableTypes{REAL_VAR, INTEGER_VAR, BOOLEAN_VAR}; + enum VariableTypes{REAL_VAR, INTEGER_VAR, BOOLEAN_VAR, STRING_VAR}; SALOMEDSImpl_GenericVariable(VariableTypes theType, const std::string& theName); ~SALOMEDSImpl_GenericVariable(); diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx index 619e22ccc..d40db227c 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx @@ -61,6 +61,21 @@ bool SALOMEDSImpl_ScalarVariable::setValue(const double theValue) return true; } +//============================================================================ +/*! Function : + * Purpose : + */ +//============================================================================ +bool SALOMEDSImpl_ScalarVariable::setStringValue(const string& theValue) +{ + + if(myStrValue == theValue) + return false; + + myStrValue = theValue; + return true; +} + //============================================================================ /*! Function : getValue() * Purpose : @@ -71,6 +86,16 @@ double SALOMEDSImpl_ScalarVariable::getValue() const return myValue; } +//============================================================================ +/*! Function : getStringValue() + * Purpose : + */ +//============================================================================ +string SALOMEDSImpl_ScalarVariable::getStringValue() const +{ + return myStrValue; +} + //============================================================================ /*! Function : Save() * Purpose : @@ -91,6 +116,11 @@ string SALOMEDSImpl_ScalarVariable::Save() const{ sprintf(buffer, "%d", (int)myValue); break; } + case SALOMEDSImpl_GenericVariable::STRING_VAR: + { + sprintf(buffer, "\"%s\"", myStrValue.c_str()); + break; + } default:break; } return string(buffer); @@ -124,6 +154,11 @@ string SALOMEDSImpl_ScalarVariable::SaveToScript() const sprintf(buffer, "%s", "False"); break; } + case SALOMEDSImpl_GenericVariable::STRING_VAR: + { + sprintf(buffer, "\"%s\"", myStrValue.c_str()); + break; + } default:break; } return string(buffer); diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx index 9684f6e06..acb897156 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx @@ -38,6 +38,9 @@ public: bool setValue(const double theValue); double getValue() const; + bool setStringValue(const std::string& theValue); + std::string getStringValue() const; + virtual std::string Save() const; virtual std::string SaveToScript() const; virtual std::string SaveType() const; @@ -46,6 +49,7 @@ public: private: double myValue; + std::string myStrValue; }; diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx index 7832d50c7..f756675c0 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx @@ -1590,6 +1590,50 @@ void SALOMEDSImpl_Study::SetVariable(const string& theVarName, Modify(); } +//============================================================================ +/*! Function : SetStringVariable + * Purpose : + */ +//============================================================================ +void SALOMEDSImpl_Study::SetStringVariable(const string& theVarName, + const string& theValue, + const SALOMEDSImpl_GenericVariable::VariableTypes theType) +{ + bool modified = false; + SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName); + + if( aGVar == NULL ) { + + SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName); + + aSVar->setStringValue(theValue); + myNoteBookVars.push_back(aSVar); + modified = true; + } + else { + if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast(aGVar)) { + modified = aSVar->setStringValue(theValue) || modified; + modified = aSVar->setType(theType) || modified; + } + } + if(modified) + Modify(); +} + +//============================================================================ +/*! Function : SetStringVariableAsDouble + * Purpose : + */ +//============================================================================ +void SALOMEDSImpl_Study::SetStringVariableAsDouble(const string& theVarName, + const double theValue, + const SALOMEDSImpl_GenericVariable::VariableTypes theType) +{ + SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName); + if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast(aGVar)) + aSVar->setValue(theValue); +} + //============================================================================ /*! Function : GetReal * Purpose : @@ -1606,6 +1650,22 @@ double SALOMEDSImpl_Study::GetVariableValue(const string& theVarName) return 0; } +//============================================================================ +/*! Function : GetString + * Purpose : + */ +//============================================================================ +string SALOMEDSImpl_Study::GetStringVariableValue(const string& theVarName) +{ + SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName); + + if(aGVar != NULL ) + if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast(aGVar)) + return aSVar->getStringValue(); + + return 0; +} + //============================================================================ /*! Function : IsTypeOf * Purpose : diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx b/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx index 55a2bca77..f9833cb23 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx @@ -263,8 +263,18 @@ public: const double theValue, const SALOMEDSImpl_GenericVariable::VariableTypes); + void SetStringVariable(const std::string& theVarName, + const std::string& theValue, + const SALOMEDSImpl_GenericVariable::VariableTypes); + + void SetStringVariableAsDouble(const std::string& theVarName, + const double theValue, + const SALOMEDSImpl_GenericVariable::VariableTypes); + double GetVariableValue(const std::string& theVarName); + std::string GetStringVariableValue(const std::string& theVarName); + bool IsTypeOf(const std::string& theVarName, SALOMEDSImpl_GenericVariable::VariableTypes theType) const;