From 8b478322078380dd4de8b7fef1a0ab58c84055f4 Mon Sep 17 00:00:00 2001 From: asl Date: Thu, 26 Nov 2009 11:10:16 +0000 Subject: [PATCH] Correct parameters sorting on the base of IDs --- src/Notebook/SALOME_Notebook.cxx | 122 +++++++++++++++++++++--------- src/Notebook/SALOME_Notebook.hxx | 10 ++- src/Notebook/SALOME_Parameter.cxx | 22 +++++- src/Notebook/SALOME_Parameter.hxx | 6 +- 4 files changed, 116 insertions(+), 44 deletions(-) diff --git a/src/Notebook/SALOME_Notebook.cxx b/src/Notebook/SALOME_Notebook.cxx index 6cc8a8418..eb3e4465e 100644 --- a/src/Notebook/SALOME_Notebook.cxx +++ b/src/Notebook/SALOME_Notebook.cxx @@ -78,13 +78,9 @@ std::string SALOME_Notebook::KeyHelper::key() const bool SALOME_Notebook::KeyHelper::operator < ( const KeyHelper& theKH ) const { bool ok; - const std::list - &aList1 = myNotebook->myDependencies.find( myKey )->second, - &aList2 = myNotebook->myDependencies.find( theKH.myKey )->second; - - if( find( aList1.begin(), aList1.end(), theKH.myKey ) != aList1.end() ) + if( myNotebook->HasDependency( myKey, theKH.myKey ) ) ok = false; - else if( find( aList2.begin(), aList2.end(), myKey ) != aList2.end() ) + else if( myNotebook->HasDependency( theKH.myKey, myKey ) ) ok = true; else ok = myKey < theKH.myKey; @@ -104,7 +100,7 @@ bool SALOME_Notebook::KeyHelper::operator == ( const std::string& theKey ) const SALOME_Notebook::SALOME_Notebook( PortableServer::POA_ptr thePOA, SALOMEDS::Study_ptr theStudy ) -: SALOME::GenericObj_i( thePOA ), myUpdateOnlyParameters( false ) +: SALOME::GenericObj_i( thePOA ), myUpdateOnlyParameters( false ), myMaxId( -1 ) { myStudy = SALOMEDS::Study::_duplicate( theStudy ); } @@ -196,7 +192,7 @@ void SALOME_Notebook::Update( CORBA::Boolean theOnlyParameters ) { std::string aKey = (*it).key(); //printf( "key = %s\n", aKey.c_str() ); - SALOME::ParameterizedObject_ptr anObj = FindObject( aKey ); + SALOME::ParameterizedObject_var anObj = FindObject( aKey ); if( CanUpdate( anObj ) ) anObj->Update( _this() ); else @@ -232,7 +228,7 @@ bool SALOME_Notebook::Substitute( SubstitutionInfo& theSubstitution ) std::list::const_iterator it = theSubstitution.myObjects.begin(), last = theSubstitution.myObjects.end(); for( ; it!=last; it++ ) { - SALOME::ParameterizedObject_ptr anObj = FindObject( it->key() ); + SALOME::ParameterizedObject_var anObj = FindObject( it->key() ); if( !CanUpdate( anObj ) ) { aPostponedUpdate.push_back( *it ); @@ -423,39 +419,77 @@ SALOME::StringArray* SALOME_Notebook::GenerateList( const std::list return aRes._retn(); } -SALOME::StringArray* SALOME_Notebook::Parameters() +class ParameterRank { - //Utils_Locker lock( &myMutex ); +public: + ParameterRank( SALOME_Parameter* theParam ) + { + myName = theParam->GetEntry(); + myId = theParam->GetId(); + } - std::list aNames; + bool operator < ( const ParameterRank& theRank ) + { + return myId < theRank.myId; + } + + bool operator == ( const ParameterRank& theRank ) + { + return myId == theRank.myId && myName == theRank.myName; + } + + std::string name() const + { + return myName; + } + +private: + std::string myName; + int myId; +}; + +std::list SALOME_Notebook::GetParameters() const +{ + std::list aParams; std::map< std::string, SALOME_Parameter* >::const_iterator it = myParameters.begin(), last = myParameters.end(); for( ; it!=last; it++ ) { - std::string anEntry = it->second->GetEntry(); - if( !it->second->IsAnonymous() && find( aNames.begin(), aNames.end(), anEntry ) == aNames.end() ) - aNames.push_back( anEntry ); + ParameterRank aRank( it->second ); + if( !it->second->IsAnonymous() && find( aParams.begin(), aParams.end(), aRank ) == aParams.end() ) + aParams.push_back( aRank ); } + aParams.sort(); + + std::list aNames; + std::list::const_iterator pit = aParams.begin(), plast = aParams.end(); + for( ; pit!=plast; pit++ ) + aNames.push_back( pit->name() ); + + return aNames; +} + +SALOME::StringArray* SALOME_Notebook::Parameters() +{ + //Utils_Locker lock( &myMutex ); - aNames.sort(); - return GenerateList( aNames ); + return GenerateList( GetParameters() ); } -SALOME::StringArray* SALOME_Notebook::AbsentParameters() +SALOME::StringArray* SALOME_Notebook::AbsentParameters( const char* theExpr ) { //Utils_Locker lock( &myMutex ); std::list anAbsents; - std::map< std::string, std::list >::const_iterator it = myDependencies.begin(), last = myDependencies.end(); - for( ; it!=last; it++ ) + + SALOME_EvalExpr anExpr( theExpr ); + if( anExpr.parser()->error() == EvalExpr_OK ) { - std::list::const_iterator pit = it->second.begin(), plast = it->second.end(); - for( ; pit!=plast; pit++ ) - { - std::string anEntry, aComponent = GetComponent( *pit, anEntry ); - if( aComponent == PARAM_COMPONENT && !GetParameterPtr( anEntry.c_str() ) && - find( anAbsents.begin(), anAbsents.end(), anEntry ) == anAbsents.end() ) - anAbsents.push_back( anEntry ); - } + std::list aParams = anExpr.parser()->parameters(); + std::list::const_iterator it = aParams.begin(), last = aParams.end(); + for( ; it!=last; it++ ) + if( !GetParameterPtr( it->c_str() ) && + find( anAbsents.begin(), anAbsents.end(), *it ) == anAbsents.end() ) + anAbsents.push_back( *it ); } anAbsents.sort(); @@ -680,6 +714,9 @@ CORBA::Boolean SALOME_Notebook::Save( const char* theFileName ) fprintf( aFile, "\n" ); } + //4. Save substitutions list + std::list::const_iterator sit = mySubstitutions.begin(), slast = mySubstitutions.end(); + fclose( aFile ); return true; } @@ -892,16 +929,22 @@ char* SALOME_Notebook::Dump() //2. Parameters aStr += "Parameters:\n"; - std::map< std::string, SALOME_Parameter* >::const_iterator pit = myParameters.begin(), plast = myParameters.end(); + std::list aNames = GetParameters(); + std::list::const_iterator pit = aNames.begin(), plast = aNames.end(); for( ; pit!=plast; pit++ ) { - aStr += pit->first + " (" + pit->second->GetEntry() + "): "; - if( pit->second->IsAnonymous() ) + SALOME_Parameter* p = myParameters[*pit]; + aStr += *pit + " (" + p->GetEntry() + "): "; + + char buf[16]; sprintf( buf, "%i", p->GetId() ); + aStr += arg( "[%1] ", buf ); + + if( p->IsAnonymous() ) aStr += "[A] "; - if( pit->second->IsCalculable() ) - aStr += std::string( pit->second->GetExpression( false ) ) + " (val=" + pit->second->AsString() + ")"; + if( p->IsCalculable() ) + aStr += std::string( p->GetExpression( false ) ) + " (val=" + p->AsString() + ")"; else - aStr += pit->second->AsString(); + aStr += p->AsString(); aStr += "\n"; } aStr += "\n"; @@ -956,3 +999,14 @@ char* SALOME_Notebook::GetParameters( const char* theComponent, const char* theE return CORBA::string_dup( aRes.c_str() ); } + +int SALOME_Notebook::GetNewId() +{ + return ++myMaxId; +} + +bool SALOME_Notebook::HasDependency( const std::string& theObjKey, const std::string& theRefKey ) const +{ + std::map< std::string, std::list >::const_iterator it = myDependencies.find( theObjKey ); + return it==myDependencies.end() ? false : find( it->second.begin(), it->second.end(), theRefKey ) != it->second.end(); +} diff --git a/src/Notebook/SALOME_Notebook.hxx b/src/Notebook/SALOME_Notebook.hxx index 3023da03b..90e131020 100644 --- a/src/Notebook/SALOME_Notebook.hxx +++ b/src/Notebook/SALOME_Notebook.hxx @@ -63,7 +63,7 @@ public: virtual void Rename( const char* theOldName, const char* theNewName ); virtual SALOME::Parameter_ptr GetParameter( const char* theParamName ); virtual SALOME::StringArray* Parameters(); - virtual SALOME::StringArray* AbsentParameters(); + virtual SALOME::StringArray* AbsentParameters( const char* theExpr ); virtual CORBA::Boolean Save( const char* theFileName ); virtual CORBA::Boolean Load( const char* theFileName ); @@ -72,7 +72,9 @@ public: virtual char* GetParameters( const char* theComponent, const char* theEntry ); SALOME_Parameter* GetParameterPtr( const char* theParamName ) const; - void UpdateAnonymous( const std::string& theOldName, const std::string& theNewName ); + void UpdateAnonymous( const std::string& theOldName, const std::string& theNewName ); + int GetNewId(); + bool HasDependency( const std::string& theObjKey, const std::string& theRefKey ) const; static std::vector Split( const std::string& theData, const std::string& theSeparator, bool theIsKeepEmpty ); @@ -85,7 +87,6 @@ private: SALOME::StringArray* GenerateList( const std::list& theList ) const; std::string GetComponent( const std::string& theKey, std::string& theEntry ) const; void ParseDependencies( const std::string& theData ); - std::string GetKey( SALOME::ParameterizedObject_ptr theObj ) const; std::string GetKey( const std::string& theComponent, const std::string& theParamName ) const; std::string GetKey( const std::string& theParamName ) const; @@ -93,9 +94,9 @@ private: SALOME::ParameterizedObject_ptr FindObject( const std::string& theKey ) const; void ThrowError( const std::string& theErrorMsg ) const; bool CanUpdate( SALOME::ParameterizedObject_ptr theObj ) const; + std::list GetParameters() const; private: - friend class KeyHelper; class KeyHelper { public: @@ -130,6 +131,7 @@ private: SALOMEDS::Study_var myStudy; Utils_Mutex myMutex; bool myUpdateOnlyParameters; + int myMaxId; }; #endif diff --git a/src/Notebook/SALOME_Parameter.cxx b/src/Notebook/SALOME_Parameter.cxx index a2f9a2980..3065d50ca 100644 --- a/src/Notebook/SALOME_Parameter.cxx +++ b/src/Notebook/SALOME_Parameter.cxx @@ -33,21 +33,25 @@ SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theName, bool theValue ) : myNotebook( theNotebook ), myName( theName ), myResult( theValue ), myIsAnonymous( false ), myIsCalculable( false ) { + SetId(); } SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theName, int theValue ) : myNotebook( theNotebook ), myName( theName ), myResult( theValue ), myIsAnonymous( false ), myIsCalculable( false ) { + SetId(); } SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theName, double theValue ) : myNotebook( theNotebook ), myName( theName ), myResult( theValue ), myIsAnonymous( false ), myIsCalculable( false ) { + SetId(); } SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theName, const std::string& theData, bool isExpr ) : myNotebook( theNotebook ), myName( theName ), myIsAnonymous( false ), myIsCalculable( isExpr ) { + SetId(); if( isExpr ) { InternalSetExpression( theData ); @@ -60,6 +64,7 @@ SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::str SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::string& theExpr ) : myNotebook( theNotebook ), myName( theExpr ), myExpr( theExpr ), myIsAnonymous( true ), myIsCalculable( true ) { + SetId(); Update( SALOME::Notebook::_nil() ); } @@ -240,7 +245,7 @@ SALOME_StringList SALOME_Parameter::Dependencies() const std::string SALOME_Parameter::Save() const { char buf[256]; - sprintf( buf, "%s %i %i?", myName.c_str(), (int)myIsAnonymous, (int)myIsCalculable ); + sprintf( buf, "%s %i %i %i?", myName.c_str(), myId, (int)myIsAnonymous, (int)myIsCalculable ); std::string aRes = buf; if( myIsCalculable ) aRes += myExpr.expression(); @@ -259,11 +264,10 @@ SALOME_Parameter* SALOME_Parameter::Load( const std::string& theData ) return 0; char aName[256]; - int anAnonym, aCalc; - if( sscanf( aParts[0].c_str(), "%s %i %i", aName, &anAnonym, &aCalc ) < 3 ) + int anAnonym, aCalc, anId; + if( sscanf( aParts[0].c_str(), "%s %i %i %i", aName, &anId, &anAnonym, &aCalc ) < 4 ) return 0; - printf( "load param: %s\n", aName ); return 0; } @@ -407,3 +411,13 @@ void SALOME_Parameter::ThrowError( bool isCalc, const std::string& theMsg ) throw anError; } } + +void SALOME_Parameter::SetId() +{ + myId = myNotebook->GetNewId(); +} + +int SALOME_Parameter::GetId() const +{ + return myId; +} diff --git a/src/Notebook/SALOME_Parameter.hxx b/src/Notebook/SALOME_Parameter.hxx index fde123eac..90ded0a93 100644 --- a/src/Notebook/SALOME_Parameter.hxx +++ b/src/Notebook/SALOME_Parameter.hxx @@ -73,21 +73,22 @@ public: virtual char* GetExpression( CORBA::Boolean theForceConvert ); - SALOME_StringList Dependencies() const; - std::string Save() const; static SALOME_Parameter* Load( const std::string& theData ); bool IsAnonymous() const; bool IsCalculable() const; + SALOME_StringList Dependencies() const; void Substitute( const std::string& theName, const SALOME_EvalExpr& theExpr ); + int GetId() const; private: void ThrowTypeError( const std::string& theMsg ); void ThrowError( bool isCalc, const std::string& theMsg ); void InternalSetExpression( const std::string& theExpr ); void AnalyzeError(); + void SetId(); private: SALOME_Notebook* myNotebook; @@ -95,6 +96,7 @@ private: SALOME_EvalExpr myExpr; SALOME_EvalVariant myResult; bool myIsAnonymous, myIsCalculable; + int myId; }; #endif -- 2.39.2