GME  13
OCLCommonEx.cpp
Go to the documentation of this file.
00001 //###############################################################################################################################################
00002 //
00003 //      Object Constraint Language Generic Manager
00004 //      OCLCommon_Extensions.cpp
00005 //
00006 //###############################################################################################################################################
00007 #include "Solve4786.h"
00008 #include "OCLCommonEx.h"
00009 
00010 #include "TokenEx.h"
00011 #include <algorithm>
00012 #include <string>
00013 #include <cctype>
00014 
00015 namespace OclCommonEx {
00016 
00017 //##############################################################################################################################################
00018 //
00019 //      F U N C T I O N S
00020 //
00021 //##############################################################################################################################################
00022 
00023         std::string Convert(const CStringW& strIn)
00024         {
00025                 std::string strOut;
00026 
00027                 int len = GetCharLength(strIn, strIn.GetLength(), CP_UTF8);
00028                 if (len == 0)
00029                         return strOut;
00030                 char* out;
00031                 char out_stack[1024];
00032                 std::unique_ptr<char[]> out_heap;
00033                 if (len > sizeof(out_stack) / sizeof(out_stack[0]))
00034                 {
00035                         out_heap = std::unique_ptr<char[]>(new char[len]);
00036                         out = out_heap.get();
00037                 }
00038                 else
00039                 {
00040                         out = out_stack;
00041                 }
00042                 len = WideCharToMultiByte(CP_UTF8, 0, strIn, strIn.GetLength(), out, len, NULL, NULL);
00043                 ASSERT(len);
00044                 strOut = std::string(out, out + len);
00045                 return strOut;
00046         }
00047 
00048         CStringW Convert(const std::string& strIn)
00049         {
00050                 int len = MultiByteToWideChar(CP_UTF8, 0, strIn.c_str(), strIn.length(), nullptr, 0);
00051 
00052                 if (len == 0)
00053                         return CString();
00054                 CString ret;
00055                 len = MultiByteToWideChar(CP_UTF8, 0, strIn.c_str(), strIn.length(), ret.GetBuffer(len), len);
00056                 ASSERT(len);
00057                 ret.ReleaseBuffer(len);
00058                 return ret;
00059         }
00060 
00061         bool ParseCardinality( const CString& strCardinalityIn )
00062         {
00063                 CString strCardinality( strCardinalityIn );
00064                 strCardinality.Remove( _T( '\t' ) );
00065                 strCardinality.Remove( _T( '\n' ) );
00066                 strCardinality.Remove( _T( ' ' ) );
00067 
00068                 bool bError = false;
00069                 bool bMultiple = false;
00070                 CStringArray arrElements;
00071                 CTokenEx        tokenizer;
00072                 tokenizer.Split( strCardinality, _T(","), arrElements );
00073 
00074                 for ( int i = 0 ; i < arrElements.GetSize() ; i++ ) {
00075                         CStringArray arrBounds;
00076                         tokenizer.Split( arrElements[i], _T(".."), arrBounds);
00077 
00078                         if ( arrBounds.GetSize() == 0 ) {
00079                                 bError = true;
00080                                 break;
00081                         }
00082                         else if ( arrBounds.GetSize() == 1 ) {
00083                                 int iMultiplicity = 0;
00084                                 if ( _stscanf_s( (LPCTSTR) arrBounds[ 0 ], _T("%ld"), &iMultiplicity ) != 1 ) {
00085                                         bError = true;
00086                                         break;
00087                                 }
00088                                 else {
00089                                         if ( iMultiplicity < 0 ) {
00090                                                 bError = true;
00091                                                 break;
00092                                         }
00093                                         if ( iMultiplicity > 1 )
00094                                                 bMultiple = true;
00095                                 }
00096                         }
00097                         else if ( arrBounds.GetSize() == 2 ) {
00098                                 int iMultiplicityMin = 0;
00099                                 int iMultiplicityMax = 0;
00100 
00101                                 if( _stscanf_s( (LPCTSTR) arrBounds[ 0 ], _T("%ld"), &iMultiplicityMin ) != 1 ) {
00102                                         bError = true;
00103                                         break;
00104                                 }
00105                                 else {
00106                                         if ( iMultiplicityMin < 0 ) {
00107                                                 bError = true;
00108                                                 break;
00109                                         }
00110                                         if ( iMultiplicityMin > 1 )
00111                                                 bMultiple = true;
00112                                 }
00113 
00114                                 if( _stscanf_s( (LPCTSTR) arrBounds[ 1 ], _T("%ld"), &iMultiplicityMax ) != 1 ) {
00115                                         if ( arrBounds[ 1 ] != _T("*") ) {
00116                                                 bError = true;
00117                                                 break;
00118                                         }
00119                                         bMultiple = true;
00120                                 }
00121                                 else {
00122                                         if ( iMultiplicityMax < 0 ) {
00123                                                 bError = true;
00124                                                 break;
00125                                         }
00126                                         if ( iMultiplicityMax < iMultiplicityMax ) {
00127                                                 bError = true;
00128                                                 break;
00129                                         }
00130                                         if ( iMultiplicityMax > 1 )
00131                                                 bMultiple = true;
00132                                 }
00133                         }
00134                 }
00135 
00136                 return bError || bMultiple;
00137         }
00138 
00139         std::string GetConnPointRole( CComPtr<IMgaConnPoint> spCP )
00140         {
00141                 CString strRole;
00142                 COMTHROW( spCP->get_ConnRole( PutOut( strRole ) ) );
00143                 return Convert( strRole );
00144         }
00145 
00146         std::string GetObjectName( CComPtr<IMgaObject> spObject )
00147         {
00148                 CString strName;
00149                 COMTHROW( spObject->get_Name( PutOut( strName ) ) );
00150                 return Convert( strName );
00151         }
00152 
00153         std::string GetObjectName( CComPtr<IMgaMetaBase> spObject )
00154         {
00155                 CString strName;
00156                 COMTHROW( spObject->get_Name( PutOut( strName ) ) );
00157                 return Convert( strName );
00158         }
00159 
00160         std::string GetObjectKind( CComPtr<IMgaObject> spObject )
00161         {
00162                 CString strKind;
00163                 CComPtr<IMgaMetaBase> spMeta;
00164                 COMTHROW( spObject->get_MetaBase( &spMeta ) );
00165                 CComQIPtr<IMgaMetaRole> spRole = spMeta;
00166                 if ( spRole.p ) {
00167                         CComPtr<IMgaMetaFCO> spKind;
00168                         COMTHROW( spRole->get_Kind( &spKind ) );
00169                         return GetObjectName( spKind.p );
00170                 }
00171                 return GetObjectName( spMeta );
00172         }
00173 
00174         std::string GetObjectDisplayedName(CComPtr<IMgaObject>& spObject)
00175         {
00176                 CComPtr<IMgaMetaBase> spMeta;
00177                 COMTHROW( spObject->get_MetaBase( &spMeta ) );
00178                 _bstr_t name;
00179                 COMTHROW(spMeta->get_Name(name.GetAddress()));
00180                 return static_cast<const char*>(name);
00181         }
00182 
00183         std::string GetFCORole( CComPtr<IMgaFCO> spFCO )
00184         {
00185                 CComPtr<IMgaMetaRole> spMetaRole;
00186                 COMTHROW( spFCO->get_MetaRole( &spMetaRole ) );
00187                 return ( spMetaRole.p ) ? GetObjectName( spMetaRole.p ) : "";
00188         }
00189 
00190         objtype_enum GetObjectType( CComPtr<IMgaObject> spObject )
00191         {
00192                 objtype_enum eType;
00193                 COMTHROW( spObject->get_ObjType( &eType ) );
00194                 return eType;
00195         }
00196 
00197         objtype_enum GetObjectType( CComPtr<IMgaMetaBase> spObject )
00198         {
00199                 objtype_enum eType;
00200                 COMTHROW( spObject->get_ObjType( &eType ) );
00201                 return eType;
00202         }
00203 
00204         bool IsInLibrary( CComPtr<IMgaObject> spObject )
00205         {
00206                 VARIANT_BOOL bInLib;
00207                 COMTHROW( spObject->get_IsLibObject( &bInLib ) );
00208                 return bInLib == VARIANT_TRUE;
00209         }
00210 
00211         std::string ParseLibraryName( const std::string& strFullName )
00212         {
00213                 std::string strName;
00214                 size_t iPos = strFullName.rfind( "\\" );
00215                 if ( iPos != std::string::npos )
00216                         strName = strFullName.substr( iPos + 1 );
00217                 iPos = strName.rfind( "." );
00218                 if ( iPos != std::string::npos )
00219                         strName = strName.substr( 0, iPos );
00220                 Trim( strName );
00221                 return strName;
00222         }
00223 
00224         CComPtr<IMgaFolder> GetFirstLibrary( CComPtr<IMgaObject> spObject )
00225         {
00226                 // If It is NULL
00227 
00228                 if ( ! spObject.p )
00229                         return NULL;
00230 
00231                 // If this is the library, and it is not included in other library
00232 
00233                 CComQIPtr<IMgaFolder> spFolder = spObject;
00234                 if ( spFolder.p ) {
00235                         CString strLibName;
00236                         COMTHROW( spFolder->get_LibraryName( PutOut( strLibName ) ) );
00237                         if ( ! strLibName.IsEmpty() )
00238                                 return spFolder.p;
00239                 }
00240 
00241                 // If it is included in other library
00242 
00243                 if ( ! IsInLibrary( spObject ) )
00244                         return NULL;
00245                 if ( ! spFolder.p ) {
00246                         CComQIPtr<IMgaFCO> spFCO = spObject;
00247                         CComPtr<IMgaFCO> spRootFCO;
00248                         COMTHROW( spFCO->get_RootFCO( &spRootFCO ) );
00249                         spObject = spRootFCO;
00250                 }
00251                 CComPtr<IMgaObject> spParent;
00252                 COMTHROW( spObject->GetParent( &spParent ) );
00253                 return GetFirstLibrary( spParent );
00254         }
00255 
00256         StringVector GetLibraryPath( CComPtr<IMgaObject> spObject )
00257         {
00258                 StringVector vecPath;
00259 
00260                 CComPtr<IMgaFolder> spFolder = GetFirstLibrary( spObject );
00261                 while ( spFolder.p ) {
00262                         CString strFullName;
00263                         COMTHROW( spFolder->get_LibraryName( PutOut( strFullName ) ) );
00264                         vecPath.push_back( ParseLibraryName( Convert( strFullName ) ) );
00265                         CComPtr<IMgaObject> spParent;
00266                         COMTHROW( spFolder->GetParent( &spParent ) );
00267                         spFolder = GetFirstLibrary( spParent );
00268                 }
00269                 std::reverse( vecPath.begin(), vecPath.end() );
00270                 return vecPath;
00271         }
00272 
00273         std::string ObjectTypeToString( objtype_enum eType )
00274         {
00275                 switch ( eType ) {
00276                         case OBJTYPE_ATOM : return "Atom";
00277                         case OBJTYPE_MODEL : return "Model";
00278                         case OBJTYPE_CONNECTION : return "Connection";
00279                         case OBJTYPE_REFERENCE : return "Reference";
00280                         case OBJTYPE_SET : return "Set";
00281                         case OBJTYPE_FOLDER : return "Folder";
00282                         default : return "";
00283                 }
00284         }
00285 
00286         objtype_enum StringToObjectType( const std::string& strType )
00287         {
00288                 if ( strType == "Atom" )
00289                         return OBJTYPE_ATOM;
00290                 if ( strType == "Model" )
00291                         return OBJTYPE_MODEL;
00292                 if ( strType == "Connection" )
00293                         return OBJTYPE_CONNECTION;
00294                 if ( strType == "Reference" )
00295                         return OBJTYPE_REFERENCE;
00296                 if ( strType == "Set" )
00297                         return OBJTYPE_SET;
00298                 if ( strType == "Folder" )
00299                         return OBJTYPE_FOLDER;
00300                 return OBJTYPE_NULL;
00301         }
00302 
00303         std::string CreateType( const std::string& strKind, objtype_enum eType )
00304         {
00305                 if ( eType == OBJTYPE_NULL )
00306                         return "meta::" + strKind;
00307                 else
00308                         return "meta::" + LowerFirst( ObjectTypeToString( eType ) ) + "::" + strKind;
00309         }
00310 
00311         bool ResolveType( const std::string& strFullName, std::string& strKind, objtype_enum& eType )
00312         {
00313                 eType = OBJTYPE_NULL;
00314                 strKind = "";
00315                 size_t iPos = strFullName.find( "::" );
00316                 if ( iPos == std::string::npos ) {
00317                         strKind = strFullName;
00318                         return true;
00319                 }
00320                 if ( strFullName.substr( 0, iPos ) == "meta" ) {
00321                         std::string strTemp = strFullName.substr( iPos + 2 );
00322                         iPos = strTemp.find( "::" );
00323                         if ( iPos == std::string::npos ) {
00324                                 strKind = strTemp;
00325                                 return true;
00326                         }
00327                         eType = StringToObjectType( UpperFirst( strTemp.substr( 0, iPos ) ) );
00328                         if ( eType != OBJTYPE_NULL ) {
00329                                 strKind = strTemp.substr( iPos + 2 );
00330                                 return true;
00331                         }
00332                 }
00333                 return false;
00334         }
00335 
00336         std::string LowerFirst( const std::string& strValue )
00337         {
00338                 if ( strValue.empty() )
00339                         return strValue;
00340                 std::string strVal = strValue;
00341                 std::transform(strVal.begin(), strVal.begin() + 1, strVal.begin(), std::tolower);
00342                 return strVal;
00343         }
00344 
00345         std::string UpperFirst( const std::string& strValue )
00346         {
00347                 if ( strValue.empty() )
00348                         return strValue;
00349                 std::string strVal = strValue;
00350                 std::transform(strVal.begin(), strVal.begin() + 1, strVal.begin(), std::toupper);
00351                 return strVal;
00352         }
00353 
00354         CString LowerFirst( const CString& strValue )
00355         {
00356                 if ( strValue.IsEmpty() )
00357                         return strValue;
00358                 CString strTemp = strValue.Left( 1 );
00359                 strTemp.MakeLower();
00360                 return strTemp + strValue.Mid( 1 );
00361         }
00362 
00363         CString UpperFirst( const CString& strValue )
00364         {
00365                 if ( strValue.IsEmpty() )
00366                         return strValue;
00367                 CString strTemp = strValue.Left( 1 );
00368                 strTemp.MakeUpper();
00369                 return strTemp + strValue.Mid( 1 );
00370         }
00371 
00372         void AddAssociation( OclMeta::TypeManager* pManager, OclMeta::Association* pAssociation, OclMeta::AssociationVector& vecAssociations, bool bCantBeSet )
00373         {
00374                 OclMeta::Association* pAssociationFound = NULL;
00375                 unsigned int i;
00376                 for ( i = 0 ; i < vecAssociations.size() ; i++ )
00377                         if ( pAssociation->IsIdentical( *vecAssociations[ i ] ) ) {
00378                                 pAssociationFound = vecAssociations[ i ];
00379                                 break;
00380                         }
00381                 if ( pAssociationFound ) {
00382                         TypeSeq vecType = pAssociation->GetReturnTypeSeq();
00383                         TypeSeq vecTypeFound = pAssociationFound->GetReturnTypeSeq();
00384                         TypeSeq vecTypeNew;
00385                         if ( ! bCantBeSet )
00386                                 vecTypeNew.push_back( "ocl::Set" );
00387                         if ( vecType.size() == 1 )
00388                                 if ( vecTypeFound.size() == 1 )
00389                                         vecTypeNew.push_back( pManager->GetTypeBase( vecType[ 0 ], vecTypeFound[ 0 ] ) );
00390                                 else
00391                                         vecTypeNew.push_back( pManager->GetTypeBase( vecType[ 0 ], vecTypeFound[ 1 ] ) );
00392                         else
00393                                 if ( vecTypeFound.size() == 1 )
00394                                         vecTypeNew.push_back( pManager->GetTypeBase( vecType[ 1 ], vecTypeFound[ 0 ] ) );
00395                                 else
00396                                         vecTypeNew.push_back( pManager->GetTypeBase( vecType[ 1 ], vecTypeFound[ 1 ] ) );
00397                         vecAssociations[ i ]->SetReturnTypeSeq( vecTypeNew );
00398                         delete pAssociation;
00399                 }
00400                 else
00401                         vecAssociations.push_back( pAssociation );
00402         }
00403 
00404         void AddAssociations( OclMeta::AssociationVector& vecAssociationsToAdd, OclMeta::AssociationVector& vecAssociations )
00405         {
00406                 for ( unsigned int i = 0 ; i < vecAssociationsToAdd.size() ; i++ )
00407                         vecAssociations.push_back( vecAssociationsToAdd[ i ] );
00408                 vecAssociationsToAdd.clear();
00409         }
00410 
00411 //##############################################################################################################################################
00412 //
00413 //      F U N C T I O N S
00414 //
00415 //##############################################################################################################################################
00416 
00417         bool AddObject( CComPtr<IMgaFCO> spFCO, FCOVector& vecFCOs )
00418         {
00419                 if ( ! spFCO.p )
00420                         return false;
00421                 if ( ContainsObject( spFCO, vecFCOs ) )
00422                         return false;
00423                 vecFCOs.push_back( spFCO.p );
00424                 return true;
00425         }
00426 
00427         bool AddObject( CComPtr<IMgaObject> spObject, ObjectVector& vecObjects )
00428         {
00429                 if ( ! spObject.p )
00430                         return false;
00431                 if ( ContainsObject( spObject, vecObjects ) )
00432                         return false;
00433                 vecObjects.push_back( spObject.p );
00434                 return true;
00435         }
00436 
00437         bool ContainsObject( CComPtr<IMgaFCO> spFCO, FCOVector& vecFCOs )
00438         {
00439                 for ( unsigned int i = 0 ; i < vecFCOs.size() ; i++ )
00440                         if ( vecFCOs[ i ].p == spFCO.p )
00441                                 return true;
00442                 return false;
00443         }
00444 
00445         bool ContainsObject( CComPtr<IMgaObject> spObject, ObjectVector& vecObjects )
00446         {
00447                 for ( unsigned int i = 0 ; i < vecObjects.size() ; i++ )
00448                         if ( vecObjects[ i ].p == spObject.p )
00449                                 return true;
00450                 return false;
00451         }
00452 
00453         bool IsAbstract( CComPtr<IMgaFCO> spFCO )
00454         {
00455                 std::string strKind = GetObjectKind( spFCO.p );
00456                 if ( strKind == "Folder" )
00457                         return false;
00458                 if ( strKind == "FCO" )
00459                         return true;
00460                 short sAbstract;
00461                 if ( ! SUCCEEDED( spFCO->get_BoolAttrByName( CComBSTR( L"IsAbstract" ), &sAbstract ) ) )
00462                         return false;
00463                 return ( sAbstract == 0 ) ? false : true;
00464         }
00465 
00466         bool GetAllObjects( CComPtr<IMgaFCO>& spFCO, CComPtr<IMgaFCOs>& spRefs )
00467         {
00468                 spRefs = NULL;
00469                 CComQIPtr<IMgaReference> spRef = spFCO;
00470                 if ( spRef.p ) {
00471                         spFCO = NULL;
00472                         COMTHROW( spRef->get_Referred( &spFCO ) );
00473                         if ( ! spFCO.p )
00474                                 return false;
00475                 }
00476                 COMTHROW( spFCO->get_ReferencedBy( &spRefs ) );
00477                 return true;
00478         }
00479 
00480         void GetAssociationEnds( CComPtr<IMgaFCO> spFCO, const std::string& strRole, const std::string& strKind, FCOVector& vecFCOs )
00481         {
00482                 std::string strCPRole, strCKind;
00483                 CComPtr<IMgaConnPoints> spStartCPs;
00484                 COMTHROW( spFCO->get_PartOfConns( &spStartCPs ) );
00485                 MGACOLL_ITERATE( IMgaConnPoint , spStartCPs ) {
00486                         CComPtr<IMgaConnPoint> spStartCP = MGACOLL_ITER;
00487                         strCPRole = GetConnPointRole( spStartCP );
00488                         if ( strRole.empty() || strRole == GetConnPointRole( spStartCP ) ) {
00489                                 CComPtr<IMgaConnection> spConnection;
00490                                 COMTHROW( spStartCP->get_Owner( &spConnection ) );
00491                                 strCKind = GetObjectKind( spConnection.p );
00492                                 if ( strKind.empty() || strKind == GetObjectKind( spConnection.p ) ) {
00493                                         CComPtr<IMgaConnPoints> spEndCPs;
00494                                         COMTHROW( spConnection->get_ConnPoints( &spEndCPs ) );
00495                                         MGACOLL_ITERATE( IMgaConnPoint, spEndCPs ) {
00496                                                 CComPtr<IMgaConnPoint> spEndCP = MGACOLL_ITER;
00497                                                 if ( spEndCP != spStartCP ) {
00498                                                         CComPtr<IMgaFCO> spTarget;
00499                                                         COMTHROW( spEndCP->get_Target( &spTarget ) );
00500                                                         vecFCOs.push_back( spTarget.p );
00501                                                 }
00502                                         } MGACOLL_ITERATE_END;
00503                                 }
00504                         }
00505                 } MGACOLL_ITERATE_END;
00506         }
00507 
00508         void GetAssociationEnds( CComPtr<IMgaFCO> spFCO, const std::string& strRole, const std::string& strKind, FCOVector& vecFCOs, FCOVector& vecConnections )
00509         {
00510                 CComPtr<IMgaConnPoints> spStartCPs;
00511                 COMTHROW( spFCO->get_PartOfConns( &spStartCPs ) );
00512                 MGACOLL_ITERATE( IMgaConnPoint , spStartCPs ) {
00513                         CComPtr<IMgaConnPoint> spStartCP = MGACOLL_ITER;
00514                         if ( strRole.empty() || strRole == GetConnPointRole( spStartCP ) ) {
00515                                 CComPtr<IMgaConnection> spConnection;
00516                                 COMTHROW( spStartCP->get_Owner( &spConnection ) );
00517                                 if ( strKind.empty() || strKind == GetObjectKind( spConnection.p ) ) {
00518                                         CComPtr<IMgaConnPoints> spEndCPs;
00519                                         COMTHROW( spConnection->get_ConnPoints( &spEndCPs ) );
00520                                         MGACOLL_ITERATE( IMgaConnPoint, spEndCPs ) {
00521                                                 CComPtr<IMgaConnPoint> spEndCP = MGACOLL_ITER;
00522                                                 if ( spEndCP != spStartCP ) {
00523                                                         CComPtr<IMgaFCO> spTarget;
00524                                                         COMTHROW( spEndCP->get_Target( &spTarget ) );
00525                                                         vecFCOs.push_back( spTarget.p );
00526                                                         vecConnections.push_back( spConnection.p );
00527                                                 }
00528                                         } MGACOLL_ITERATE_END;
00529                                 }
00530                         }
00531                 } MGACOLL_ITERATE_END;
00532         }
00533 
00534         void GetInheritances( CComPtr<IMgaFCO> spFCO, const std::string& strInheritanceType, bool bIsParent, FCOVector& vecFCOs )
00535         {
00536                 AddObject( spFCO, vecFCOs );
00537                 std::string strKind1;
00538                 std::string strKind2;
00539 
00540                 if ( strInheritanceType == "Interface" ) {
00541                         strKind1 = ( bIsParent ) ? "DerivedIntInheritance" : "BaseIntInheritance";
00542                         strKind2 = ( ! bIsParent ) ? "DerivedIntInheritance" : "BaseIntInheritance";
00543                 }
00544                 else if ( strInheritanceType == "Implementation" ) {
00545                         strKind1 = ( bIsParent ) ? "DerivedImpInheritance" : "BaseImpInheritance";
00546                         strKind2 = ( ! bIsParent ) ? "DerivedImpInheritance" : "BaseImpInheritance";
00547                 }
00548                 else {
00549                         strKind1 = ( bIsParent ) ? "DerivedInheritance" : "BaseInheritance";
00550                         strKind2 = ( ! bIsParent ) ? "DerivedInheritance" : "BaseInheritance";
00551                 }
00552 
00553                 FCOVector vecInheritances;
00554                 GetAssociationEnds( spFCO, "", strKind1, vecInheritances );
00555                 for ( unsigned int i = 0 ; i < vecInheritances.size() ; i++ )
00556                         GetAssociationEnds( vecInheritances[ i ].p, "", strKind2, vecFCOs );
00557         }
00558 
00559         void GetEquivalences( CComPtr<IMgaFCO> spFCO, const std::string& strEquivalencesType, bool bToLeft, FCOVector& vecFCOs )
00560         {
00561                 AddObject( spFCO, vecFCOs );
00562                 std::string strKind1;
00563                 std::string strKind2;
00564 
00565                 if ( strEquivalencesType == "Aspect" ) {
00566                         strKind1 = ( bToLeft ) ? "SameAspectLeft" : "SameAspectRight";
00567                         strKind2 = ( ! bToLeft ) ? "SameAspectRight" : "SameAspectLeft";
00568                 }
00569                 else if ( strEquivalencesType == "Folder" ) {
00570                         strKind1 = ( bToLeft ) ? "SameFolderLeft" : "SameFolderRight";
00571                         strKind2 = ( ! bToLeft ) ? "SameFolderRight" : "SameFolderLeft";
00572                 }
00573                 else {
00574                         strKind1 = ( bToLeft ) ? "EquivLeft" : "EquivRight";
00575                         strKind2 = ( ! bToLeft ) ? "EquivRight" : "EquivLeft";
00576                 }
00577 
00578                 FCOVector vecEquivalences;
00579                 GetAssociationEnds( spFCO, "", strKind1, vecEquivalences );
00580                 for ( unsigned int i = 0 ; i < vecEquivalences.size() ; i++ )
00581                         GetAssociationEnds( vecEquivalences[ i ].p, "", strKind2, vecFCOs );
00582         }
00583 
00584         void GetMetaObjectsR( CComPtr<IMgaMetaBase> spMeta, const std::string& strName, objtype_enum eType, MetaBaseVector& vecMetaBases )
00585         {
00586                 if ( ( eType == OBJTYPE_NULL || eType == OclCommonEx::GetObjectType( spMeta ) ) && ( strName.empty() || strName == GetObjectName( spMeta ) ) )
00587                         vecMetaBases.push_back( spMeta.p );
00588 
00589                 CComPtr<IMgaMetaFCOs> spFCOs;
00590                 CComQIPtr<IMgaMetaModel> spModel = spMeta;
00591                 if ( spModel.p )
00592                         COMTHROW( spModel->get_DefinedFCOs( &spFCOs ) );
00593                 else {
00594                         CComQIPtr<IMgaMetaFolder> spFolder = spMeta;
00595                         if ( spFolder.p ) {
00596                                 CComPtr<IMgaMetaFolders> spFolders;
00597                                 COMTHROW( spFolder->get_DefinedFolders( &spFolders ) );
00598                                 MGACOLL_ITERATE( IMgaMetaFolder, spFolders ) {
00599                                         GetMetaObjectsR( MGACOLL_ITER.p, strName, eType, vecMetaBases );
00600                                 } MGACOLL_ITERATE_END;
00601                                 COMTHROW( spFolder->get_DefinedFCOs( &spFCOs ) );
00602                         }
00603                 }
00604                 if ( spFCOs.p ) {
00605                         MGACOLL_ITERATE( IMgaMetaFCO, spFCOs ) {
00606                                 GetMetaObjectsR( MGACOLL_ITER.p, strName, eType, vecMetaBases );
00607                         } MGACOLL_ITERATE_END;
00608                 }
00609         }
00610 
00611         void GetMetaObjects( CComPtr<IMgaMetaProject> spProject, const std::string& strName, objtype_enum eType, MetaBaseVector& vecMetaBases )
00612         {
00613                 CComPtr<IMgaMetaFolder> spRootFolder;
00614                 COMTHROW( spProject->get_RootFolder( &spRootFolder ) );
00615                 GetMetaObjectsR( spRootFolder.p, strName, eType, vecMetaBases );
00616         };
00617 
00618         std::string GetPointerRole( CComPtr<IMgaMetaPointerSpec> spPointer )
00619         {
00620                 CString strName;
00621                 COMTHROW( spPointer->get_Name( PutOut( strName ) ) );
00622                 return Convert( strName );
00623         }
00624 
00625         ModelMap GetModels( CComPtr<IMgaMetaProject> spProject )
00626         {
00627                 MetaBaseVector vecModels;
00628                 GetMetaObjects( spProject, "", OBJTYPE_MODEL, vecModels );
00629                 ModelMap mapModels;
00630                 for ( unsigned int i = 0 ; i < vecModels.size() ; i++ ) {
00631                         CComQIPtr<IMgaMetaModel> spModel = vecModels[ i ].p;
00632                         mapModels.insert( ModelMap::value_type( GetObjectName( vecModels[ i ].p ), spModel.p ) );
00633                 }
00634                 return mapModels;
00635         }
00636 
00637         StringVector GetPointerDescriptions( CComPtr<IMgaMetaPointerSpec> spPointer )
00638         {
00639                 StringVector vecDescriptions;
00640                 CComPtr<IMgaMetaPointerItems> spItems;
00641                 COMTHROW( spPointer->get_Items( &spItems ) );
00642                 MGACOLL_ITERATE( IMgaMetaPointerItem, spItems ) {
00643                         CString strDesc;
00644                         COMTHROW( MGACOLL_ITER->get_Desc( PutOut( strDesc ) ) );
00645                         strDesc.TrimLeft();
00646                         strDesc.TrimRight();
00647                         vecDescriptions.push_back( Convert( strDesc ) );
00648                 } MGACOLL_ITERATE_END;
00649                 return vecDescriptions;
00650         }
00651 
00652         StringVector TranslateDescriptionsToKinds( ModelMap& mapModels, const StringVector& vecDescriptions )
00653         {
00654                 StringVector vecKinds;
00655                 for ( unsigned int i = 0 ; i < vecDescriptions.size() ; i++ ) {
00656                         size_t iPos = vecDescriptions[ i ].find( ' ' );
00657                         if ( iPos == std::string::npos ) {
00658                                 if ( std::find( vecKinds.begin(), vecKinds.end(), vecDescriptions[ i ] ) == vecKinds.end() )
00659                                         vecKinds.push_back( vecDescriptions[ i ] );
00660                         }
00661                         else {
00662                                 std::string strModel = vecDescriptions[ i ].substr( 0, iPos );
00663                                 std::string strRole = vecDescriptions[ i ].substr( iPos + 1 );
00664                                 ModelMap::iterator iModel = mapModels.find( strModel );
00665                                 CComPtr<IMgaMetaRole> spRole;
00666                                 COMTHROW( (*iModel).second->get_RoleByName( CComBSTR( Convert( strRole ) ), &spRole ) );
00667                                 CComPtr<IMgaMetaFCO> spFCO;
00668                                 COMTHROW( spRole->get_Kind( &spFCO ) );
00669                                 std::string strKind = GetObjectName( spFCO.p );
00670                                 if ( std::find( vecKinds.begin(), vecKinds.end(), strKind ) == vecKinds.end() )
00671                                         vecKinds.push_back( strKind );
00672                         }
00673                 }
00674                 return vecKinds;
00675         }
00676 
00677         void GetDerivedObjectsR( CComPtr<IMgaFCO> spFCO, ObjectVector& vecObjects )
00678         {
00679                 vecObjects.push_back( spFCO.p );
00680                 CComPtr<IMgaFCOs> spFCOs;
00681                 COMTHROW( spFCO->get_DerivedObjects( &spFCOs ) );
00682                 if ( spFCOs.p ) {
00683                         MGACOLL_ITERATE( IMgaFCO, spFCOs ) {
00684                                 GetDerivedObjectsR( MGACOLL_ITER, vecObjects );
00685                         } MGACOLL_ITERATE_END;
00686                 }
00687         }
00688 
00689         void GetDerivedObjects( CComPtr<IMgaProject> spProject, const std::string& strType, ObjectVector& vecObjects )
00690         {
00691                 CComPtr<IMgaFCOs> spFCOs;
00692                 CComPtr<IMgaFilter> spFilter;
00693                 COMTHROW( spProject->CreateFilter( &spFilter ) );
00694                 COMTHROW( spFilter->put_Name( CComBSTR( OclCommonEx::Convert( strType ) ) ) );
00695                 COMTHROW( spProject->AllFCOs( spFilter, &spFCOs ) );
00696                 MGACOLL_ITERATE( IMgaFCO, spFCOs ) {
00697                         GetDerivedObjectsR( MGACOLL_ITER, vecObjects );
00698                 } MGACOLL_ITERATE_END;
00699         }
00700 
00701         void GetKindFolders( CComPtr<IMgaFolder> spFolder, const std::string& strKind, bool bKind, ObjectVector& vecObjects )
00702         {
00703                 if ( strKind.empty() || ( ( bKind ) ? GetObjectKind( spFolder.p ) : GetObjectName( spFolder.p ) ) == strKind )
00704                         AddObject( spFolder.p, vecObjects );
00705                 CComPtr<IMgaFolders> spFolders;
00706                 COMTHROW( spFolder->get_ChildFolders( &spFolders ) );
00707                 MGACOLL_ITERATE( IMgaFolder, spFolders ) {
00708                         GetKindFolders( MGACOLL_ITER, strKind, bKind, vecObjects );
00709                 } MGACOLL_ITERATE_END;
00710         }
00711 
00712         void GetKindObjects( CComPtr<IMgaProject> spProject, const std::string& strKind, ObjectVector& vecObjects )
00713         {
00714                 CComPtr<IMgaMetaProject> spMetaProject;
00715                 COMTHROW( spProject->get_RootMeta( &spMetaProject ) );
00716                 MetaBaseVector vecMetas;
00717                 GetMetaObjects( spMetaProject, strKind, OBJTYPE_NULL, vecMetas );
00718 
00719                 for ( unsigned int i = 0 ; i < vecMetas.size() ; i++ ) {
00720                         CComQIPtr<IMgaMetaFCO> spFCO = vecMetas[ i ].p;
00721 
00722                         if ( spFCO.p ) {
00723 
00724                                 // Collect Roles and Create Metaref List
00725 
00726                                 CString strMetaRefs;
00727                                 metaref_type ref;
00728                                 COMTHROW( vecMetas[ i ]->get_MetaRef( &ref ) );
00729                                 strMetaRefs.Format( _T("#%d , "), ref );
00730                                 CComPtr<IMgaMetaRoles> spRoles;
00731                                 COMTHROW( spFCO->get_UsedInRoles( &spRoles ) );
00732                                 MGACOLL_ITERATE( IMgaMetaRole, spRoles ) {
00733                                         COMTHROW( MGACOLL_ITER->get_MetaRef( &ref ) );
00734                                         CString strTemp;
00735                                         strTemp.Format( _T("#%d , "), ref );
00736                                         strMetaRefs += strTemp;
00737                                 } MGACOLL_ITERATE_END;
00738 
00739                                 // Create a Filter and get All FCOs / Call Inheritance Chain Root with Types
00740 
00741                                 CComPtr<IMgaFilter> spFilter;
00742                                 COMTHROW( spProject->CreateFilter( &spFilter ) );
00743                                 COMTHROW( spFilter->put_Kind( CComBSTR( strMetaRefs ) ) );
00744                                 CComPtr<IMgaFCOs> spFCOs;
00745                                 COMTHROW( spProject->AllFCOs( spFilter, &spFCOs ) );
00746                                 MGACOLL_ITERATE( IMgaFCO, spFCOs ) {
00747                                         AddObject( MGACOLL_ITER.p, vecObjects );
00748                                 } MGACOLL_ITERATE_END;
00749                         }
00750 
00751                         CComPtr<IMgaFolder> spRootFolder;
00752                         COMTHROW( spProject->get_RootFolder( &spRootFolder ) );
00753                         GetKindFolders( spRootFolder, strKind, true, vecObjects );
00754                 }
00755         }
00756 
00757         void GetObjectPathR( CComPtr<IMgaObject> spObject, std::string& strPath )
00758         {
00759                 CComPtr<IMgaObject> spParent;
00760                 COMTHROW( spObject->GetParent( &spParent ) );
00761                 if ( spParent.p ) {
00762                         strPath = "/" + GetObjectName( spParent ) + strPath;
00763                         GetObjectPathR( spParent, strPath );
00764                 }
00765         }
00766 
00767         std::string GetObjectPath( CComPtr<IMgaObject> spObject )
00768         {
00769                 std::string strPath;
00770                 GetObjectPathR( spObject, strPath );
00771                 return ( strPath.empty() ) ? "/" : strPath;
00772         }
00773 
00774         std::string GetObjectDescription( CComPtr<IMgaObject> spObject )
00775         {
00776                 if ( ! spObject.p )
00777                         return "null";
00778 
00779                 std::string strName = GetObjectName( spObject );
00780                 std::string strKind = GetObjectKind( spObject );
00781                 objtype_enum eType = GetObjectType( spObject );
00782                 std::string strMetaKind = "gme::" + ObjectTypeToString( eType );
00783 
00784                 std::string strContainmentRole;
00785                 CComQIPtr<IMgaFCO> spFCO = spObject;
00786                 if ( spFCO.p ) {
00787                         strContainmentRole = GetFCORole( spFCO.p );
00788                         if ( strContainmentRole == strKind )
00789                                 strContainmentRole = "";
00790                         if ( eType == OBJTYPE_SET )
00791                                 strContainmentRole = "";
00792                 }
00793 
00794                 std::string strPath = GetObjectPath( spObject );
00795 
00796                 std::string strOut = strName + " { ";
00797 
00798                 switch ( eType ) {
00799                         case OBJTYPE_FOLDER :
00800                         case OBJTYPE_ATOM :
00801                         case OBJTYPE_MODEL :
00802                         case OBJTYPE_SET :
00803                         {
00804                                 if ( ! strContainmentRole.empty() )
00805                                         strOut += "role: " + strContainmentRole + "; ";
00806                                 strOut += "kind: meta::" + strKind + "; ";
00807                                 return strOut + "path: " + strPath + "; }";
00808                         }
00809                         case OBJTYPE_REFERENCE :
00810                         {
00811                                 strOut += "kind: meta::" + strKind + "; ";
00812                                 CComQIPtr<IMgaReference> spReference = spFCO;
00813                                 CComPtr<IMgaFCO> spTarget;
00814                                 COMTHROW( spReference->get_Referred( &spTarget ) );
00815                                 if ( spTarget.p )
00816                                         strOut += "referred: " + GetObjectName( spTarget.p ) + "; ";
00817                                 if ( ! strContainmentRole.empty() )
00818                                         strOut += "role: " + strContainmentRole + "; ";
00819                                 return strOut + "path: " + strPath + "; }";
00820                         }
00821                         case OBJTYPE_CONNECTION :
00822                         {
00823                                 strOut += "kind: meta::" + strKind + "; ";
00824                                 CComQIPtr<IMgaConnection> spConnection = spFCO;
00825                                 CComPtr<IMgaConnPoints> spCPs;
00826                                 COMTHROW( spConnection->get_ConnPoints( &spCPs ) );
00827                                 MGACOLL_ITERATE( IMgaConnPoint, spCPs ) {
00828                                         strOut += GetConnPointRole( MGACOLL_ITER ) + ": ";
00829                                         CComPtr<IMgaFCO> spTarget;
00830                                         COMTHROW( MGACOLL_ITER->get_Target( &spTarget ) );
00831                                         strOut += GetObjectName( spTarget.p ) + "; ";
00832                                 } MGACOLL_ITERATE_END;
00833                                 if ( ! strContainmentRole.empty() )
00834                                         strOut += "role: " + strContainmentRole + "; ";
00835                                 return strOut + "path: " + strPath + "; }";
00836                         }
00837                         default :
00838                                 return strOut + "}";
00839                 }
00840         }
00841 
00842         std::string GetConnectionPointDescription( CComPtr<IMgaConnPoint> spCP )
00843         {
00844                 if ( ! spCP.p )
00845                         return "null";
00846 
00847                 std::string strOut = "gme::ConnectionPoint { ";
00848 
00849                 std::string strRole = GetConnPointRole( spCP );
00850                 if ( ! strRole.empty() )
00851                         strOut += "role: " + strRole + "; ";
00852 
00853                 CComPtr<IMgaFCO> spTarget;
00854                 COMTHROW( spCP->get_Target( &spTarget ) );
00855                 strOut += "target: " + GetObjectName( spTarget.p ) + "; ";
00856 
00857                 CComPtr<IMgaConnection> spConnection;
00858                 COMTHROW( spCP->get_Owner( &spConnection ) );
00859                 strOut += "owner: " + GetObjectName( spConnection.p ) + "; ";
00860 
00861                 return strOut + "path: " + GetObjectPath( spConnection.p ) + "; }";
00862         }
00863 
00864 }; // namespace OclCommonEx