From: asl Date: Tue, 24 Nov 2009 10:12:57 +0000 (+0000) Subject: implementation of substitutions X-Git-Tag: PHASE_17_Part1_V1~28 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=60e5ca250f60b54aa7109e326271556f54e48b72;p=modules%2Fkernel.git implementation of substitutions --- diff --git a/src/Notebook/SALOME_EvalExpr.cxx b/src/Notebook/SALOME_EvalExpr.cxx index 148144552..e52fd0857 100755 --- a/src/Notebook/SALOME_EvalExpr.cxx +++ b/src/Notebook/SALOME_EvalExpr.cxx @@ -165,6 +165,7 @@ bool SALOME_EvalExpr::autoDeleteOperationSets() const { return myParser->autoDeleteOperationSets(); } + //======================================================================= //function : setAutoDeleteOperationSets //purpose : @@ -173,3 +174,13 @@ void SALOME_EvalExpr::setAutoDeleteOperationSets(const bool on) { myParser->setAutoDeleteOperationSets( on ); } + +//======================================================================= +//function : substitute +//purpose : +//======================================================================= +void SALOME_EvalExpr::substitute( const SALOME_String& theParamName, const SALOME_EvalExpr& theExpr ) +{ + myParser->substitute( theParamName, theExpr.parser() ); + myExpr = myParser->rebuildExpression(); +} diff --git a/src/Notebook/SALOME_EvalExpr.hxx b/src/Notebook/SALOME_EvalExpr.hxx index 09254c66f..28c4c0d02 100755 --- a/src/Notebook/SALOME_EvalExpr.hxx +++ b/src/Notebook/SALOME_EvalExpr.hxx @@ -34,7 +34,7 @@ class SALOME_EvalExpr { public: - SALOME_EvalExpr(const SALOME_String& = SALOME_String()); + SALOME_EvalExpr( const SALOME_String& = SALOME_String() ); SALOME_EvalExpr( const bool, const SALOME_String& = SALOME_String() ); ~SALOME_EvalExpr(); @@ -54,6 +54,8 @@ public: void removeOperationSet( SALOME_EvalSet* ); void insertOperationSet( SALOME_EvalSet*, const int = -1 ); + void substitute( const SALOME_String& theParamName, const SALOME_EvalExpr& theExpr ); + private: void initialize( const bool, const SALOME_String& ); diff --git a/src/Notebook/SALOME_EvalParser.cxx b/src/Notebook/SALOME_EvalParser.cxx index 2b20e7e71..f5fb86b06 100755 --- a/src/Notebook/SALOME_EvalParser.cxx +++ b/src/Notebook/SALOME_EvalParser.cxx @@ -868,6 +868,128 @@ bool SALOME_EvalParser::isMonoParam() const return myError == EvalExpr_OK && myPostfix.size()==1 && myPostfix.begin()->myType == Param; } +//======================================================================= +//function : substitute +//purpose : +//======================================================================= +void SALOME_EvalParser::substitute( const SALOME_String& theParamName, + SALOME_EvalParser* theNewExpr ) +{ + Postfix::iterator it = myPostfix.begin(), last = myPostfix.end(); + for( ; it!=last; it++ ) + { + //printf( "ELEM: %s\n", it->myValue.toString().c_str() ); + if( it->myType == Param && it->myValue.toString() == theParamName ) + { + Postfix::iterator removed = it; it++; + myPostfix.erase( removed ); + //printf( "REMOVE:\n" ); + Postfix::iterator nit = theNewExpr->myPostfix.begin(), nlast = theNewExpr->myPostfix.end(); + for( ; nit!=nlast; nit++ ) + { + myPostfix.insert( it, *nit ); + //printf( "INSERT: %s\n", nit->myValue.toString().c_str() ); + } + it--; + } + } +} + + +//======================================================================= +//class : RebultExprItem +//purpose : Reconstruction of expression by postfix form +//======================================================================= +class RebultExprItem +{ +public: + RebultExprItem( const SALOME_EvalParser* theParser, const SALOME_String& theExpr = "" ) + : myExpr( theExpr ), myParser( theParser ) + { + } + + int priority() const + { + return myOp.length() > 0 ? myParser->priority( myOp, myOpBin ) : 100000; + } + + RebultExprItem concat( const SALOME_String& theOp, const RebultExprItem& theItem, bool theIsBin ) const + { + int pleft = priority(), pright = theItem.priority(), + pOp = myParser->priority( theOp, theIsBin ); + + //printf( "concat: '%s' (%i) and '%s' (%i) with op = %s (%i)\n", + // myExpr.c_str(), pleft, theItem.myExpr.c_str(), pright, + // theOp.c_str(), pOp ); + + SALOME_String aNewExpr = expression( pleft aStack; + + Postfix::const_iterator it = myPostfix.begin(), last = myPostfix.end(); + RebultExprItem anItem1( this ), anItem2( this ); + for( ; it!=last; it++ ) + { + SALOME_String aValue = it->myValue.toString(); + switch( it->myType ) + { + case Value: + case Param: + aStack.push( RebultExprItem( this, aValue ) ); + break; + + case Pre: + anItem1 = aStack.top(); aStack.pop(); + anItem2 = RebultExprItem( this ).concat( aValue, anItem1, false ); + aStack.push( anItem2 ); + break; + + case Post: + anItem1 = aStack.top(); aStack.pop(); + anItem2 = anItem1.concat( aValue, RebultExprItem( this ), false ); + aStack.push( anItem2 ); + break; + + case Binary: + anItem2 = aStack.top(); aStack.pop(); + anItem1 = aStack.top(); aStack.pop(); + anItem2 = anItem1.concat( aValue, anItem2, true ); + aStack.push( anItem2 ); + break; + } + } + + return aStack.top().expression( false ); +} + //======================================================================= //function : dump //purpose : diff --git a/src/Notebook/SALOME_EvalParser.hxx b/src/Notebook/SALOME_EvalParser.hxx index 9764ad02d..cbc8768bb 100755 --- a/src/Notebook/SALOME_EvalParser.hxx +++ b/src/Notebook/SALOME_EvalParser.hxx @@ -72,6 +72,11 @@ public: bool isMonoParam() const; + void substitute( const SALOME_String& theParamName, + SALOME_EvalParser* theNewExpr ); + + SALOME_String rebuildExpression() const; + protected: //! Types of postfix representation elements typedef enum @@ -127,6 +132,8 @@ private: static void append( Postfix& aL, const Postfix& aL1 ); private: + friend class RebultExprItem; + SALOME_ListOfEvalSet mySets; SALOME_EvalExprError myError; ParamMap myParams;