From 004b9fc671e6305103ecd14257409675ce2f8773 Mon Sep 17 00:00:00 2001 From: rnv Date: Wed, 5 Nov 2008 13:16:40 +0000 Subject: [PATCH] 1. Dump notebook variables in the Study script. 2. Use a std::vector for storage notebook vasiables instead of a std::map. --- src/SALOMEDS/SALOMEDS_Study.cxx | 6 +- src/SALOMEDS/SALOMEDS_Study_i.cxx | 6 +- .../SALOMEDSImpl_GenericVariable.cxx | 26 +++- .../SALOMEDSImpl_GenericVariable.hxx | 6 +- .../SALOMEDSImpl_ScalarVariable.cxx | 38 ++++- .../SALOMEDSImpl_ScalarVariable.hxx | 4 +- src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx | 130 ++++++++++++++---- src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx | 15 +- .../SALOMEDSImpl_StudyManager.cxx | 14 +- 9 files changed, 190 insertions(+), 55 deletions(-) diff --git a/src/SALOMEDS/SALOMEDS_Study.cxx b/src/SALOMEDS/SALOMEDS_Study.cxx index 14797c541..ade49db95 100644 --- a/src/SALOMEDS/SALOMEDS_Study.cxx +++ b/src/SALOMEDS/SALOMEDS_Study.cxx @@ -722,7 +722,7 @@ double SALOMEDS_Study::GetReal(const string& theVarName) double aResult; if (_isLocal) { SALOMEDS::Locker lock; - aResult = _local_impl->GetVariable(theVarName); + aResult = _local_impl->GetVariableValue(theVarName); } else aResult = _corba_impl->GetReal((char*)theVarName.c_str()); @@ -734,7 +734,7 @@ int SALOMEDS_Study::GetInteger(const string& theVarName) int aResult; if (_isLocal) { SALOMEDS::Locker lock; - aResult = (int) _local_impl->GetVariable(theVarName); + aResult = (int) _local_impl->GetVariableValue(theVarName); } else aResult = _corba_impl->GetInteger((char*)theVarName.c_str()); @@ -746,7 +746,7 @@ bool SALOMEDS_Study::GetBoolean(const string& theVarName) bool aResult; if (_isLocal) { SALOMEDS::Locker lock; - aResult = (bool) _local_impl->GetVariable(theVarName); + aResult = (bool) _local_impl->GetVariableValue(theVarName); } else aResult = _corba_impl->GetBoolean((char*)theVarName.c_str()); diff --git a/src/SALOMEDS/SALOMEDS_Study_i.cxx b/src/SALOMEDS/SALOMEDS_Study_i.cxx index 3f161946c..6d8ef34e5 100644 --- a/src/SALOMEDS/SALOMEDS_Study_i.cxx +++ b/src/SALOMEDS/SALOMEDS_Study_i.cxx @@ -886,7 +886,7 @@ void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValu //============================================================================ CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName) { - return _impl->GetVariable(string(theVarName)); + return _impl->GetVariableValue(string(theVarName)); } //============================================================================ @@ -896,7 +896,7 @@ CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName) //============================================================================ CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName) { - return (int)_impl->GetVariable(string(theVarName)); + return (int)_impl->GetVariableValue(string(theVarName)); } //============================================================================ @@ -906,7 +906,7 @@ CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName) //============================================================================ CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName) { - return (bool)_impl->GetVariable(string(theVarName)); + return (bool)_impl->GetVariableValue(string(theVarName)); } //============================================================================ diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.cxx index 5c6840b72..529620ef0 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.cxx @@ -36,8 +36,10 @@ using namespace std; */ //============================================================================ SALOMEDSImpl_GenericVariable:: -SALOMEDSImpl_GenericVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType): - _type(theType) +SALOMEDSImpl_GenericVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType, + const string& theName): + _type(theType), + _name(theName) {} //============================================================================ @@ -58,6 +60,16 @@ SALOMEDSImpl_GenericVariable::VariableTypes SALOMEDSImpl_GenericVariable::Type() return _type; } +//============================================================================ +/*! Function : Name + * Purpose : + */ +//============================================================================ +string SALOMEDSImpl_GenericVariable::Name() const +{ + return _name; +} + //============================================================================ /*! Function : setType * Purpose : @@ -121,6 +133,16 @@ string SALOMEDSImpl_GenericVariable::Save() const return string(); } + +//============================================================================ +/*! Function : SaveToScript + * Purpose : + */ +//============================================================================ +string SALOMEDSImpl_GenericVariable::SaveToScript() const +{ + return string(); +} //============================================================================ /*! Function : SaveType * Purpose : diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx b/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx index d40d5cd53..5e4d6c669 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_GenericVariable.hxx @@ -37,11 +37,13 @@ class SALOMEDSIMPL_EXPORT SALOMEDSImpl_GenericVariable //Supported types of the nootebook variables enum VariableTypes{REAL_VAR, INTEGER_VAR, BOOLEAN_VAR}; - SALOMEDSImpl_GenericVariable(VariableTypes theType); + SALOMEDSImpl_GenericVariable(VariableTypes theType, const std::string& theName); ~SALOMEDSImpl_GenericVariable(); VariableTypes Type() const; + std::string Name() const; + static VariableTypes String2VariableType(const std::string& theStrType); void setType(const VariableTypes theType); @@ -51,12 +53,14 @@ class SALOMEDSIMPL_EXPORT SALOMEDSImpl_GenericVariable virtual void SetModifyFlag(); virtual std::string Save() const; + virtual std::string SaveToScript() const; virtual std::string SaveType() const; virtual void Load(const std::string& theStrValue); private: VariableTypes _type; + std::string _name; }; #endif //_GENERICIMPL_VARIABLE_HXX_ diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx index 036c5c0ae..683dbbf16 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.cxx @@ -32,8 +32,9 @@ using namespace std; */ //============================================================================ SALOMEDSImpl_ScalarVariable:: -SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes type): - SALOMEDSImpl_GenericVariable(type) +SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes type, + const string& theName): + SALOMEDSImpl_GenericVariable(type,theName) {} //============================================================================ @@ -88,12 +89,45 @@ string SALOMEDSImpl_ScalarVariable::Save() const{ case SALOMEDSImpl_GenericVariable::INTEGER_VAR: { sprintf(buffer, "%d", (int)myValue); + break; } default:break; } return string(buffer); } +//============================================================================ +/*! Function : SaveToScript() + * Purpose : + */ +//============================================================================ +string SALOMEDSImpl_ScalarVariable::SaveToScript() const +{ + char buffer[255]; + switch(Type()) + { + case SALOMEDSImpl_GenericVariable::REAL_VAR: + { + sprintf(buffer, "%f", myValue); + break; + } + case SALOMEDSImpl_GenericVariable::INTEGER_VAR: + { + sprintf(buffer, "%d", (int)myValue); + break; + } + case SALOMEDSImpl_GenericVariable::BOOLEAN_VAR: + { + if((bool)myValue) + sprintf(buffer, "%s", "True"); + else + sprintf(buffer, "%s", "False"); + break; + } + default:break; + } + return string(buffer); +} //============================================================================ /*! Function : SaveType() diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx index 94316a47c..b13ddeed2 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_ScalarVariable.hxx @@ -31,13 +31,15 @@ class SALOMEDSIMPL_EXPORT SALOMEDSImpl_ScalarVariable : public SALOMEDSImpl_GenericVariable { public: - SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType); + SALOMEDSImpl_ScalarVariable(SALOMEDSImpl_GenericVariable::VariableTypes theType, + const std::string& theName); ~SALOMEDSImpl_ScalarVariable(); void setValue(const double theValue); double getValue() const; virtual std::string Save() const; + virtual std::string SaveToScript() const; virtual std::string SaveType() const; virtual void Load(const std::string& theStrValue); diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx index 5b1b02411..c74298441 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx @@ -877,6 +877,60 @@ SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO, return RefSO; } +//============================================================================ +/*! Function : _GetNoteBookAccessor + * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR + */ +//============================================================================ +string SALOMEDSImpl_Study::_GetNoteBookAccessor(){ + return string("notebook"); +} + +//============================================================================ +/*! Function : _GetStudyVariablesScript + * Purpose : + */ +//============================================================================ +string SALOMEDSImpl_Study::_GetStudyVariablesScript() +{ + string dump(""); + + if(myNoteBookVars.empty()) + return dump; + + dump += "####################################################\n"; + dump += "## Begin of NoteBook variables section ##\n"; + dump += "####################################################\n"; + + string set_method = _GetNoteBookAccessor()+".set("; + string varName; + string varValue; + for(int i = 0 ; i < myNoteBookVars.size();i++ ) { + varName = myNoteBookVars[i]->Name(); + varValue = myNoteBookVars[i]->SaveToScript(); + dump+=set_method+"\""+varName+"\", "+varValue+")\n"; + } + + dump += "####################################################\n"; + dump += "## End of NoteBook variables section ##\n"; + dump += "####################################################\n"; + + return dump; +} + +//============================================================================ +/*! Function : _GetNoteBookAccess + * Purpose : + */ +//============================================================================ +string SALOMEDSImpl_Study::_GetNoteBookAccess() +{ + string accessor = _GetNoteBookAccessor(); + string notebook = "import salome_notebook\n"; + notebook += accessor+" = salome_notebook."+accessor + "\n"; + return notebook; +} + bool SALOMEDSImpl_Study::IsLocked() { _errorCode = ""; @@ -1156,8 +1210,12 @@ bool SALOMEDSImpl_Study::DumpStudy(const string& thePath, fp << aBatchModeScript << ".salome_init()" << endl << endl; + fp << _GetNoteBookAccess(); + fp << "sys.path.insert( 0, \'" << thePath << "\')" << endl << endl; + //Dump NoteBook Variables + fp << _GetStudyVariablesScript(); //Check if it's necessary to dump visual parameters bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this); @@ -1495,22 +1553,21 @@ void SALOMEDSImpl_Study::SetVariable(const string& theVarName, const double theValue, const SALOMEDSImpl_GenericVariable::VariableTypes theType) { - std::map::iterator it - = myNoteBookVars.find(theVarName); + + SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName); - SALOMEDSImpl_ScalarVariable* aVar = NULL; - if( it == myNoteBookVars.end() ) { + if( aGVar == NULL ) { - aVar = new SALOMEDSImpl_ScalarVariable(theType); + SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName); - aVar->setValue(theValue); - myNoteBookVars[theVarName] = aVar; + aSVar->setValue(theValue); + myNoteBookVars.push_back(aSVar); } else { - if(aVar = dynamic_cast((*it).second)) { - aVar->setValue(theValue); - if(aVar->Type() != theType) - aVar->setType(theType); + if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast(aGVar)) { + aSVar->setValue(theValue); + if(aSVar->Type() != theType) + aSVar->setType(theType); } } } @@ -1520,14 +1577,13 @@ void SALOMEDSImpl_Study::SetVariable(const string& theVarName, * Purpose : */ //============================================================================ -double SALOMEDSImpl_Study::GetVariable(const string& theVarName) +double SALOMEDSImpl_Study::GetVariableValue(const string& theVarName) { - std::map::const_iterator it = - myNoteBookVars.find(theVarName); + SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName); - if(it != myNoteBookVars.end()) - if(SALOMEDSImpl_ScalarVariable* aVar = dynamic_cast((*it).second)) - return aVar->getValue(); + if(aGVar != NULL ) + if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast(aGVar)) + return aSVar->getValue(); } //============================================================================ @@ -1539,11 +1595,10 @@ bool SALOMEDSImpl_Study::IsTypeOf(const string& theVarName, SALOMEDSImpl_GenericVariable:: VariableTypes theType) const { - std::map::const_iterator it = - myNoteBookVars.find(theVarName); + SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName); - if(it != myNoteBookVars.end()) - return (*it).second->Type() == theType; + if(aGVar != NULL ) + return aGVar->Type() == theType; return false; } @@ -1555,7 +1610,8 @@ bool SALOMEDSImpl_Study::IsTypeOf(const string& theVarName, //============================================================================ bool SALOMEDSImpl_Study::IsVariable(const string& theVarName) const { - return myNoteBookVars.find(theVarName) != myNoteBookVars.end(); + SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName); + return (aGVar != NULL); } //============================================================================ @@ -1566,12 +1622,10 @@ bool SALOMEDSImpl_Study::IsVariable(const string& theVarName) const vector SALOMEDSImpl_Study::GetVariableNames() const { vector aResult; - std::map::const_iterator it = - myNoteBookVars.begin(); - - for(; it != myNoteBookVars.end(); it++) - aResult.push_back((*it).first); + for(int i = 0; i < myNoteBookVars.size(); i++) + aResult.push_back(myNoteBookVars[i]->Name()); + return aResult; } @@ -1580,10 +1634,26 @@ vector SALOMEDSImpl_Study::GetVariableNames() const * Purpose : */ //============================================================================ -void SALOMEDSImpl_Study::AddVariable(const string& theVarName, - SALOMEDSImpl_GenericVariable* theVariable) +void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable) { - myNoteBookVars[theVarName] = theVariable; + myNoteBookVars.push_back(theVariable); +} + +//============================================================================ +/*! Function : AddVariable + * Purpose : + */ +//============================================================================ +SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const +{ + SALOMEDSImpl_GenericVariable* aResult = NULL; + for(int i = 0; i < myNoteBookVars.size();i++) { + if(theName.compare(myNoteBookVars[i]->Name()) == 0) { + aResult = myNoteBookVars[i]; + break; + } + } + return aResult; } //============================================================================ diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx b/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx index ed7982084..98bb6c52a 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_Study.hxx @@ -70,7 +70,7 @@ private: std::map _mapOfSO; std::map _mapOfSCO; std::map myIORLabels; - std::map myNoteBookVars; + std::vector myNoteBookVars; SALOMEDSImpl_SObject _FindObject(const SALOMEDSImpl_SObject& SO, const std::string& anObjectName, @@ -80,6 +80,10 @@ private: const std::string& anObjectIOR, bool& _find); + std::string _GetStudyVariablesScript(); + std::string _GetNoteBookAccessor(); + std::string _GetNoteBookAccess(); + public: static SALOMEDSImpl_Study* GetStudy(const DF_Label& theLabel); @@ -258,18 +262,19 @@ public: const double theValue, const SALOMEDSImpl_GenericVariable::VariableTypes); - double GetVariable(const std::string& theVarName); + double GetVariableValue(const std::string& theVarName); bool IsTypeOf(const std::string& theVarName, SALOMEDSImpl_GenericVariable::VariableTypes theType) const; bool IsVariable(const std::string& theVarName) const; + std::vector GetVariableNames() const; - void AddVariable(const std::string& theVarName, - SALOMEDSImpl_GenericVariable* theVariable); - + void AddVariable(SALOMEDSImpl_GenericVariable* theVariable); + + SALOMEDSImpl_GenericVariable* GetVariable(const std::string& theName) const; //Returns a callback SALOMEDSImpl_Callback* GetCallback() { return _cb; } diff --git a/src/SALOMEDSImpl/SALOMEDSImpl_StudyManager.cxx b/src/SALOMEDSImpl/SALOMEDSImpl_StudyManager.cxx index 9fb0cbefe..95d1de891 100644 --- a/src/SALOMEDSImpl/SALOMEDSImpl_StudyManager.cxx +++ b/src/SALOMEDSImpl/SALOMEDSImpl_StudyManager.cxx @@ -652,18 +652,16 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl, hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file); hdf_notebook_vars->CreateOnDisk(); - map ::const_iterator it = - aStudy->myNoteBookVars.begin(); string varValue; string varType; - for( ;it != aStudy->myNoteBookVars.end(); it++ ){ + for(int i=0 ;i < aStudy->myNoteBookVars.size(); i++ ){ // For each variable create HDF group - hdf_notebook_var = new HDFgroup((char*)(*it).first.c_str(),hdf_notebook_vars); + hdf_notebook_var = new HDFgroup((char*)aStudy->myNoteBookVars[i]->Name().c_str(),hdf_notebook_vars); hdf_notebook_var->CreateOnDisk(); // Save Variable type - varType = (*it).second->SaveType(); + varType = aStudy->myNoteBookVars[i]->SaveType(); name_len = (hdf_int32) varType.length(); size[0] = name_len +1 ; hdf_dataset = new HDFdataset("VARIABLE_TYPE",hdf_notebook_var,HDF_STRING,size,1); @@ -673,7 +671,7 @@ bool SALOMEDSImpl_StudyManager::Impl_SaveAs(const string& aUrl, hdf_dataset=0; //will be deleted by hdf_sco_group destructor // Save Variable value - varValue = (*it).second->Save(); + varValue = aStudy->myNoteBookVars[i]->Save(); name_len = (hdf_int32) varValue.length(); size[0] = name_len +1 ; hdf_dataset = new HDFdataset("VARIABLE_VALUE",hdf_notebook_var,HDF_STRING,size,1); @@ -1315,11 +1313,11 @@ void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup) //Create variable and add it in the study SALOMEDSImpl_GenericVariable* aVariable = - new SALOMEDSImpl_ScalarVariable(aVarType); + new SALOMEDSImpl_ScalarVariable(aVarType,string(aVarName)); aVariable->Load(string(currentVarValue)); delete currentVarValue; - theStudy->AddVariable(string(aVarName),aVariable); + theStudy->AddVariable(aVariable); } } theGroup->CloseOnDisk(); -- 2.39.2