GME  13
SearchAlg.cpp
Go to the documentation of this file.
00001 // SearchAlg.cpp: implementation of the CSearch class.
00002 //
00004 
00005 
00006 #include "stdafx.h"
00007 #include "ComHelp.h"
00008 #include "Input.h"
00009 #include "SearchAlg.h"
00010 
00011 #ifdef _DEBUG
00012 #undef THIS_FILE
00013 static char THIS_FILE[]=__FILE__;
00014 #define new DEBUG_NEW
00015 #endif
00016 
00018 // Construction/Destruction
00020 
00021 CSearch::CSearch(CInput inp)
00022 {
00023     filter = inp;
00024 }
00025 
00026 
00027 CSearch::~CSearch()
00028 {
00029 }
00030 
00031 // the main entry point for a search
00032 void CSearch::Search(IMgaFolder *root, IMgaObjects* scopeColl, IMgaFCO *selected, CComPtr<IMgaFCOs> disp, CProgressCtrl *Progress)
00033 {
00034     results = disp;
00035     m_pgsSearch = Progress;
00036     CComPtr<IMgaFolder> pRoot = root;
00037 
00038     if(filter.GetReferences() && filter.GetSplSearch() && (selected != NULL))
00039         {
00040                 SearchReferences(selected);
00041         }
00042 
00043     else if( filter.GetSearchScope()==1 && scopeColl) //1 means it shud search within current scope
00044     {
00045         CComPtr<IMgaFCOs> non_container_coll;                  // will store the Atoms, Refs and Sets
00046         non_container_coll.CoCreateInstance( L"Mga.MgaFCOs");  // which are not Fs and Ms
00047         long nExtra = 0;                                       // how many are there in the above collection
00048 
00049         MGACOLL_ITERATE(IMgaObject, scopeColl)
00050         {
00051             CComPtr<IMgaObject> iter;
00052 
00053             objtype_enum type;
00054             COMTHROW( MGACOLL_ITER->get_ObjType( &type));
00055 
00056             switch( type) {
00057                 case OBJTYPE_FOLDER:   SearchFolderHierarchy( CComQIPtr<IMgaFolder>( MGACOLL_ITER));break;
00058                 case OBJTYPE_MODEL:    SearchModelHierarchy( CComQIPtr<IMgaModel>( MGACOLL_ITER));break;
00059                 case OBJTYPE_SET:
00060                 case OBJTYPE_ATOM:
00061                 case OBJTYPE_REFERENCE: 
00062                 case OBJTYPE_CONNECTION: //added
00063                     CComQIPtr<IMgaFCO> an_fco( MGACOLL_ITER);
00064                     if( an_fco && non_container_coll) 
00065                     {
00066                         COMTHROW(non_container_coll->Append( an_fco));
00067                         ++nExtra;
00068                     }
00069             } // switch
00070         } MGACOLL_ITERATE_END;
00071 
00072         if( nExtra && non_container_coll) // if coll is non-zero and has elements inside
00073         {               
00074             CheckAllReferences( non_container_coll);  // deals with refs only
00075             CheckAllAtoms( non_container_coll);       // ... atoms only
00076             CheckAllSets( non_container_coll);        // ... sets only
00077             CheckAllConnections(non_container_coll); //added
00078         }
00079         non_container_coll.Release();
00080     }
00081     else if(filter.GetSearchScope()==0)
00082     {
00083         SearchFolderHierarchy(pRoot);   
00084     }
00085 
00086     //return results;
00087 }
00088 
00089 // get all the atoms that have a matching role name, calling CheckAtom on each
00090 void CSearch::CheckAllAtoms(IMgaFCOs *allObjects)
00091 {
00092 
00093     MGACOLL_ITERATE(IMgaFCO, allObjects)
00094     {
00095         objtype_enum rret;
00096         COMTHROW( MGACOLL_ITER->get_ObjType( &rret) );
00097 
00098         if(rret == OBJTYPE_ATOM)  
00099         {
00100             if (CheckAtom((IMgaAtom*)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaAtom*)(MGACOLL_ITER.p)));
00101         }
00102 
00103         m_pgsSearch->StepIt();  
00104     } MGACOLL_ITERATE_END;
00105 
00106 }
00107 
00108 // get all the connections that have a matching role name, calling CheckAtom on each
00109 void CSearch::CheckAllConnections(IMgaFCOs *allObjects)
00110 {
00111 
00112     MGACOLL_ITERATE(IMgaFCO, allObjects)
00113     {
00114         objtype_enum rret;
00115         COMTHROW( MGACOLL_ITER->get_ObjType( &rret) );
00116 
00117         if(rret == OBJTYPE_CONNECTION)  
00118         {
00119             if (CheckConnection((IMgaConnection*)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaConnection*)(MGACOLL_ITER.p)));
00120         }
00121 
00122         m_pgsSearch->StepIt();  
00123     } MGACOLL_ITERATE_END;
00124 
00125 }
00126 
00127 
00128 // get all the references that have a matching role name, calling CheckReference on each
00129 void CSearch::CheckAllReferences(IMgaFCOs *allObjects)
00130 {
00131 
00132     MGACOLL_ITERATE(IMgaFCO, allObjects)
00133     {
00134         objtype_enum rret;
00135         COMTHROW( MGACOLL_ITER->get_ObjType( &rret) );
00136 
00137         if(rret == OBJTYPE_REFERENCE )
00138         {
00139             if (CheckReference((IMgaReference*)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaReference*)(MGACOLL_ITER.p)));
00140         }
00141 
00142         m_pgsSearch->StepIt();
00143     } MGACOLL_ITERATE_END;
00144 
00145 }
00146 
00147 // get all the sets that have a matching role name, calling CheckSet on each
00148 void CSearch::CheckAllSets(IMgaFCOs *allObjects)
00149 {
00150 
00151     MGACOLL_ITERATE(IMgaFCO, allObjects)
00152     {
00153         objtype_enum rret;
00154         COMTHROW( MGACOLL_ITER->get_ObjType( &rret) );
00155 
00156         if(rret == OBJTYPE_SET)
00157         {
00158             if(CheckSet((IMgaSet*)(MGACOLL_ITER.p)))COMTHROW(results->Append((IMgaSet*)(MGACOLL_ITER.p)));
00159         }
00160 
00161         m_pgsSearch->StepIt();
00162     } MGACOLL_ITERATE_END;      
00163 }
00164 
00165 // get all the models that have a matching role name, calling CheckModel on each
00166 void CSearch::CheckAllModels(IMgaFCOs *allObjects)
00167 {
00168     MGACOLL_ITERATE(IMgaFCO, allObjects)
00169     {
00170         objtype_enum rret;
00171         COMTHROW( MGACOLL_ITER->get_ObjType( &rret) );
00172 
00173         if(rret == OBJTYPE_MODEL) 
00174         {
00175             if(CheckModel((IMgaModel *)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaModel*)(MGACOLL_ITER.p)));;
00176         }
00177 
00178         m_pgsSearch->StepIt();
00179 
00180     } MGACOLL_ITERATE_END;
00181 }
00182 
00183 //searches the folder hierarchy, first searches all the child models and modelhierarchies of the child models
00184 //then does the same with each child folder, then searches the folderhiearchy of each child folder's child folders
00185 //for a Breadth First Search (not quite perfectly BFS)
00186 void CSearch::SearchFolderHierarchy(IMgaFolder *root)
00187 {
00188     // Search in Root Models & Below
00189         std::unique_ptr<CComPtrList<IMgaModel>> rootmodlist_cleanup(new CComPtrList<IMgaModel>);
00190     CComPtrList<IMgaModel> *rootmodlist = rootmodlist_cleanup.get(); //needed to use new or the addtail below would fail        
00191     CComPtr<IMgaFCOs> children;
00192     COMTHROW( root->get_ChildFCOs(&children));
00193     MGACOLL_ITERATE(IMgaFCO, children) {
00194         CComPtr<IMgaModel> mm;
00195         MGACOLL_ITER.QueryInterface(&mm);
00196         if(mm != NULL) 
00197         {
00198             rootmodlist->AddTail( mm );
00199         }
00200     } MGACOLL_ITERATE_END;
00201 
00202     if (filter.GetModels()) 
00203     {
00204         CheckAllModels(children);
00205     }
00206 
00207     if (filter.GetAtoms())
00208     {
00209         CheckAllAtoms(children);
00210     }
00211 
00212     if (filter.GetReferences())
00213     {
00214         CheckAllReferences(children);
00215     }
00216 
00217     if (filter.GetSets())
00218     {
00219         CheckAllSets(children);
00220     } 
00221 
00222     if(filter.GetConnections())
00223     {
00224         CheckAllConnections(children);
00225     }
00226 
00227     POSITION rmpos = rootmodlist->GetHeadPosition();
00228     while(rmpos)
00229     {
00230         CComPtr<IMgaModel> rootmodel = rootmodlist->GetNext(rmpos);
00231         SearchModelHierarchy(rootmodel);
00232     }
00233 
00234     // Search in Folders & Below
00235     CComPtr<IMgaFolders> flist;
00236     COMTHROW( root->get_ChildFolders(&flist));
00237 
00238     MGACOLL_ITERATE(IMgaFolder, flist)
00239     {
00240         // Search in Folder-Models & Below
00241 
00242 
00243         //uncomment next line, and comment out rest of function for more dfs approach
00244         //              SearchFolderHierarchy(MGACOLL_ITER);
00245 
00246                 std::unique_ptr<CComPtrList<IMgaModel>> mlist_cleanup(new CComPtrList<IMgaModel>);
00247         CComPtrList<IMgaModel> *mlist = mlist_cleanup.get();    //needed to use new or the addtail below would fail
00248         CComPtr<IMgaFCOs> subchildren;
00249         COMTHROW( MGACOLL_ITER->get_ChildFCOs(&subchildren));
00250         MGACOLL_ITERATE(IMgaFCO, subchildren) {
00251             CComPtr<IMgaModel> mod;
00252             MGACOLL_ITER.QueryInterface(&mod);
00253             if(mod != NULL) 
00254             {
00255                 mlist->AddTail( mod );
00256             }
00257         } MGACOLL_ITERATE_END;
00258 
00259         if (filter.GetModels()) 
00260         {
00261             CheckAllModels(subchildren);
00262         }
00263 
00264         if (filter.GetAtoms())
00265         {
00266             CheckAllAtoms(subchildren);
00267         }
00268 
00269         if (filter.GetReferences())
00270         {
00271             CheckAllReferences(subchildren);
00272         }
00273 
00274         if (filter.GetSets())
00275         {
00276             CheckAllSets(subchildren);
00277         } 
00278 
00279         if(filter.GetConnections())
00280         {
00281             CheckAllConnections(subchildren);
00282         }
00283 
00284         POSITION mpos = mlist->GetHeadPosition();
00285         while(mpos)
00286         {
00287             CComPtr<IMgaModel> submodel = mlist->GetNext(mpos);
00288             SearchModelHierarchy(submodel);
00289         }
00290 
00291         // Search in SubFolders & Below
00292         CComPtr<IMgaFolders> sflist;
00293         COMTHROW( MGACOLL_ITER->get_ChildFolders(&sflist));
00294         MGACOLL_ITERATE(IMgaFolder, sflist)
00295         {
00296             SearchFolderHierarchy(MGACOLL_ITER);
00297         } MGACOLL_ITERATE_END; 
00298 
00299     } MGACOLL_ITERATE_END;
00300 }
00301 
00302 
00303 //checks all the children of the input model, then searches the model hierarchy of all the child models
00304 void CSearch::SearchModelHierarchy(IMgaModel *root)
00305 {
00306     if(root != NULL)
00307     {
00308         CComPtr<IMgaFCOs> modelChildren;
00309         COMTHROW(root->get_ChildFCOs(&modelChildren));
00310 
00311         if (filter.GetAtoms())
00312         {
00313             CheckAllAtoms(modelChildren);
00314         }
00315 
00316         if (filter.GetReferences())
00317         {
00318             CheckAllReferences(modelChildren);
00319         }
00320 
00321         if (filter.GetSets())
00322         {
00323             CheckAllSets(modelChildren);
00324         } 
00325 
00326         if (filter.GetModels())
00327         {
00328             CheckAllModels(modelChildren);
00329         }
00330 
00331         if(filter.GetConnections())
00332         {
00333             CheckAllConnections(modelChildren);
00334         }
00335 
00336         CComPtrList<IMgaModel> submodellist;
00337         CComPtr<IMgaFCOs> psa;
00338         COMTHROW( root->get_ChildFCOs(&psa));
00339         MGACOLL_ITERATE(IMgaFCO, psa) {
00340             CComPtr<IMgaModel> mm;
00341             MGACOLL_ITER.QueryInterface(&mm);
00342             if(mm != NULL) 
00343             {
00344                 submodellist.AddTail( mm );
00345             }
00346         } MGACOLL_ITERATE_END;
00347 
00348         POSITION smpos = submodellist.GetHeadPosition();
00349 
00350         while(smpos)
00351         {
00352             CComPtr<IMgaModel> submodel = submodellist.GetNext(smpos);
00353 
00354             SearchModelHierarchy(submodel);
00355         }
00356     }
00357 }
00358 
00359 //checks the name, kindname and attributes, adding all matches to the results
00360 bool CSearch::CheckAtom(IMgaFCO *Atom)
00361 {
00362 
00363     //check if the first search criteria matches
00364     int x=Matches(Atom,true);
00365 
00366     //check if the atom matches second search criteria
00367     int y=Matches(Atom,false);
00368 
00369     //perform logical operation to determine if bot the result match overall criteria   
00370     return PerformLogical(x,y);
00371 
00372 }
00373 
00374 //checks the name, kindname and attributes, adding all matches to the results
00375 bool CSearch::CheckConnection(IMgaFCO *Connection)
00376 {
00377 
00378     //check if the first search criteria matches
00379     int x=Matches(Connection,true);
00380 
00381     //check if the atom matches second search criteria
00382     int y=Matches(Connection,false);
00383 
00384     //perform logical operation to determine if bot the result match overall criteria   
00385     return PerformLogical(x,y);
00386 
00387 }
00388 
00389 //checks the name, kindname and attributes, adding all matches to the results
00390 bool CSearch::CheckReference(IMgaFCO *Reference)
00391 {
00392 
00393     //check if the References satisfies first search criteria
00394     int x=Matches(Reference,true);
00395 
00396     //check if the reference matches second search criteria
00397     int y=Matches(Reference,false);
00398 
00399     bool found=PerformLogical(x,y);
00400 
00401     
00402    
00403     //this is only for the Null reference search, searching for specific refererences handled below
00404     //if both criteria is empty then it will find all NULL References
00405     //if criteria is there then it will find NULL reference satisfying given criteria
00406     if(filter.GetSplSearch() && (found || (x==-1 && y==-1)))
00407     {
00408         found=true;
00409         CComPtr<IMgaFCO> referred;
00410         COMTHROW(((IMgaReference*)Reference)->get_Referred(&referred));
00411         if(referred != NULL)
00412             found = false;
00413     }
00414 
00415   return found;
00416 }
00417 
00418 //checks the name, kindname and attributes, adding all matches to the results
00419 bool CSearch::CheckSet(IMgaFCO *Set)
00420 {
00421 
00422     //check if the first criteria is satisfied
00423     int x=Matches(Set,true);
00424 
00425     //check if second criteria is satisified
00426     int y=Matches(Set,false);
00427 
00428     //check if logical combination is satisfied
00429     return PerformLogical(x,y);
00430 }
00431 
00432 //checks the name, kindname and attributes, adding all matches to the results
00433 bool CSearch::CheckModel(IMgaFCO *Model) //BOOL first added to check if it matches first or second
00434 {
00435      //check if the first criteria is satisfied
00436     int x=Matches(Model,true);
00437 
00438      //check if second criteria is satisified
00439     int y=Matches(Model,false);
00440 
00441     //check if logical combination is satisfied
00442     return PerformLogical(x,y);
00443 }
00444 
00445 //special search for references to specific objects
00446 void CSearch::SearchReferences(IMgaFCO *referenced)
00447 {
00448     CComPtr<IMgaFCOs> objRefByList;
00449     COMTHROW(referenced->get_ReferencedBy(&objRefByList));
00450 
00451     MGACOLL_ITERATE(IMgaFCO, objRefByList)
00452     {
00453         CComPtr<IMgaFCO> refobj = MGACOLL_ITER;
00454         COMTHROW(results->Append(refobj));
00455     } MGACOLL_ITERATE_END;
00456 }
00457 
00458 struct AttributePair {
00459         CComPtr<IMgaMetaAttribute> metaAttribute;
00460         Attribute& searchAttribute;
00461         int expressionStackIndex;
00462 };
00463 
00464 bool CSearch::CheckAttributes(IMgaFCO *obj,bool first)
00465 {       
00466     bool found = false;
00467     CComPtr<IMgaFCO> cObj = obj;
00468 
00469     //Get appropriate expression stack, either first or second 
00470     std::vector<Attribute> expressionStack = first ? filter.GetFirstAttributeStack() : filter.GetSecondAttributeStack();
00471 
00472         CString& attributeString = first ? filter.GetFirstAttribute() : filter.GetSecondAttribute();
00473         if (attributeString.Find(L"_abspath=") == 0)
00474         {
00475                 BOOL eval;
00476                 _bstr_t id;
00477                 if (FAILED(cObj->get_AbsPath(id.GetAddress())))
00478                 {
00479                         eval = FALSE;
00480                 }
00481                 else
00482                         eval = wcscmp(static_cast<const wchar_t*>(id), static_cast<const wchar_t*>(attributeString) + wcslen(L"_abspath=")) == 0;
00483                 return eval;
00484                 for(std::vector<Attribute>::iterator it=expressionStack.begin();it!=expressionStack.end();++it)
00485             {
00486                         it->eval = eval;
00487                 }
00488                 return EvaluateResult(expressionStack);
00489         }
00490 
00491     CComPtr<IMgaMetaFCO> cmeta;
00492     CComPtr<IMgaMetaAttributes> mattrs;
00493     COMTHROW(cObj->get_Meta(&cmeta));
00494     COMTHROW(cmeta->get_Attributes(&mattrs));
00495 
00496         std::vector<AttributePair> attributePairs;
00497 
00498     //iterate thru attributes and make a list if it matches search criteria
00499     //also store its type and the index in original expression stack
00500         // TODO: profile this and see if it is worth it to memoize
00501     MGACOLL_ITERATE(IMgaMetaAttribute, mattrs) {
00502                 CComBSTR strDisplayedName;
00503         COMTHROW(MGACOLL_ITER->get_DisplayedName(&strDisplayedName) );
00504         for(std::vector<Attribute>::iterator it=expressionStack.begin();it!=expressionStack.end();++it)
00505         {
00506             Attribute& attr = *it;
00507             CString name=(CString) strDisplayedName;
00508             if (filter.IsCaseIgnored()) name.MakeLower();
00509             if(std::tr1::regex_search((LPCTSTR)name,attr.GetRegExp(attr.name,filter.MatchWholeWord())))
00510             {
00511                                 AttributePair p = { MGACOLL_ITER, attr };
00512                                 attributePairs.emplace_back(std::move(p));
00513             }
00514         }
00515     } MGACOLL_ITERATE_END;
00516 
00517         for(std::vector<Attribute>::iterator it=expressionStack.begin();it!=expressionStack.end();++it)
00518     {
00519         Attribute& attr = *it;
00520                 _bstr_t id;
00521                 if (_wcsicmp(attr.name, L"_id") == 0)
00522                 {
00523                         if (FAILED(cObj->get_ID(id.GetAddress())))
00524                         {
00525                                 attr.eval = FALSE;
00526                                 continue;
00527                         }
00528                         attr.eval = attr.CheckString(std::tr1::regex_search(static_cast<const wchar_t*>(id), attr.GetRegExp(attr.value,filter.MatchWholeWord())));
00529                         continue;
00530                 }
00531                 else if (_wcsicmp(attr.name, L"_guid") == 0)
00532                 {
00533                         if (FAILED(cObj->GetGuidDisp(id.GetAddress())))
00534                         {
00535                                 attr.eval = FALSE;
00536                                 continue;
00537                         }
00538                         if (attr.value.GetLength() == 38 && attr.value.GetAt(0) == L'{' && attr.value.GetAt(37) == L'}') // special case for _guid={00000000-0000-0000-0000-00000000000}
00539                                 attr.eval = wcscmp(static_cast<const wchar_t*>(id), attr.value) == 0;
00540                         else
00541                                 attr.eval = attr.CheckString(std::tr1::regex_search(static_cast<const wchar_t*>(id), attr.GetRegExp(attr.value,filter.MatchWholeWord())));
00542                 }
00543     }
00544 
00545     //now check the attributes one by one
00546     for (auto attributePairIt = attributePairs.begin(); attributePairIt != attributePairs.end(); ++attributePairIt)
00547     {
00548                 CComPtr<IMgaMetaAttribute>& metaAttribute = attributePairIt->metaAttribute;
00549                 CComBSTR strName;
00550                 COMTHROW(metaAttribute->get_Name(&strName));
00551         CString strAttribute = strName;
00552 
00553                 Attribute& attribute = attributePairIt->searchAttribute;
00554 
00555         CString objVal = attribute.value;
00556         if(!objVal.IsEmpty())
00557         {
00558             //place holders for actual attribute values
00559             int intActualValue;
00560             double dblActualValue;
00561             bool bActualValue;
00562             CString strActualValue;
00563 
00564             //User supplied values
00565             int intSearchValue;
00566             double dblSearchValue;
00567             bool bSearchValue = false;
00568 
00569             CBstr bstrS;
00570             long value;
00571             VARIANT_BOOL vb;
00572 
00573                 attval_enum type;
00574                         COMTHROW(metaAttribute->get_ValueType(&type));
00575             switch(type)
00576             {
00577             case ATTVAL_STRING:
00578                         case ATTVAL_ENUM:
00579                 if(! SUCCEEDED( cObj->get_StrAttrByName((CBstrIn)strAttribute, bstrS) ) ) attribute.eval=FALSE;
00580 
00581                 strActualValue = bstrS;
00582                 if( filter.IsCaseIgnored()) // case ignored, make attrib value lowercase
00583                 {
00584                     strActualValue.MakeLower();
00585                 }
00586 
00587                 if(attribute.CheckString(std::tr1::regex_search((LPCTSTR)strActualValue,attribute.GetRegExp(objVal,filter.MatchWholeWord()))))
00588                     attribute.eval=TRUE;
00589                 break;
00590 
00591             case ATTVAL_INTEGER: {
00592 
00593                 if (!SUCCEEDED(cObj->get_IntAttrByName((CBstrIn)strAttribute, &value))) attribute.eval = FALSE;
00594 
00595                 intActualValue = value;
00596 
00597                 const wchar_t* attributeValue = static_cast<const wchar_t*>(attribute.value);
00598                 wchar_t* endPtr;
00599                 wcstol(attributeValue, &endPtr, 10);
00600                 if (endPtr == attributeValue + wcslen(attributeValue) && attribute.CheckInteger(intActualValue, intSearchValue))
00601                     attribute.eval = TRUE;
00602             } break;
00603 
00604             case ATTVAL_DOUBLE:         
00605 
00606                 if( !SUCCEEDED( cObj->get_FloatAttrByName((CBstrIn)strAttribute, &dblActualValue) ) ) attribute.eval=FALSE;
00607 
00608                 dblSearchValue = _ttof(attribute.value);
00609                 if(attribute.CheckDouble(dblActualValue,dblSearchValue))
00610                     attribute.eval=TRUE ;
00611                 break;
00612 
00613             case ATTVAL_BOOLEAN:        
00614 
00615                 if( !SUCCEEDED( cObj->get_BoolAttrByName((CBstrIn)strAttribute, &vb) ) ) attribute.eval=FALSE;
00616 
00617                 bActualValue = (vb != 0);                       
00618                 intSearchValue = _ttoi(attribute.value);
00619                 if(attribute.value==_T("false") || attribute.value==_T("False") || attribute.value==_T("FALSE"))
00620                     bSearchValue = false;
00621                 if(attribute.value==_T("true") || attribute.value==_T("True") || attribute.value==_T("TRUE"))
00622                     bSearchValue = true;
00623 
00624                 if(attribute.CheckBool(bActualValue,bSearchValue))
00625                     attribute.eval=TRUE;
00626                 break;
00627 
00628             case ATTVAL_REFERENCE:      break;
00629 
00630             default:                            
00631                 attribute.eval = TRUE; break;
00632             }
00633         }
00634         else
00635             attribute.eval = TRUE;
00636     }
00637 
00638         return EvaluateResult(expressionStack); 
00639 }
00640 
00641 //Evaluate the logical combination result
00642 
00643 bool CSearch::EvaluateResult(std::vector<Attribute>& vectorAttributes)
00644 {
00645     if(vectorAttributes.size()==0)
00646         return true;
00647     else if (vectorAttributes.size()==1)
00648         return (vectorAttributes[0].eval==TRUE);
00649 
00650     while(vectorAttributes.size()!=1)
00651     {
00652         //pop the first attribute
00653         Attribute attribute1=vectorAttributes.back();
00654         vectorAttributes.pop_back();
00655 
00656         //pop the operation
00657         Attribute operation=vectorAttributes.back();
00658         vectorAttributes.pop_back();
00659 
00660         //pop the second attribute
00661         Attribute attribute2=vectorAttributes.back();
00662         vectorAttributes.pop_back();
00663 
00664         //binary evaluation of 2 attributes using the operation
00665         attribute1.eval=operation.LogicalCompare(attribute1.eval,operation,attribute2.eval);
00666 
00667         //put the result back
00668         vectorAttributes.push_back(attribute1);
00669     }
00670     return (vectorAttributes[0].eval==TRUE);
00671 }
00672 
00673 
00674 void CSearch::SearchResults(CComPtr<IMgaFCOs> old_results,CComPtr<IMgaFCOs> disp, CProgressCtrl *Progress)
00675 {
00676     //CComPtr<IMgaFCO> fco;
00677     results=disp;
00678     m_pgsSearch=Progress;
00679 
00680 
00681     MGACOLL_ITERATE(IMgaFCO, old_results)
00682     {
00683         objtype_enum rret;
00684         COMTHROW( MGACOLL_ITER->get_ObjType( &rret) );
00685         if (rret == OBJTYPE_ATOM  && filter.GetAtoms())
00686         {
00687             if (CheckAtom((IMgaAtom*)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaAtom*)(MGACOLL_ITER.p)));
00688         }
00689         else if (rret == OBJTYPE_MODEL && filter.GetModels())
00690         {
00691             if(CheckModel((IMgaModel *)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaModel*)(MGACOLL_ITER.p)));;
00692         }
00693         else if (rret == OBJTYPE_REFERENCE && filter.GetReferences())
00694         {
00695             if (CheckReference((IMgaReference*)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaReference*)(MGACOLL_ITER.p)));
00696         }
00697         else if (rret == OBJTYPE_SET && filter.GetSets())
00698         {
00699             if(CheckSet((IMgaSet*)(MGACOLL_ITER.p)))COMTHROW(results->Append((IMgaSet*)(MGACOLL_ITER.p)));
00700         }
00701         else if(rret == OBJTYPE_CONNECTION && filter.GetConnections())  
00702         {
00703             if (CheckConnection((IMgaConnection*)(MGACOLL_ITER.p))) COMTHROW(results->Append((IMgaConnection*)(MGACOLL_ITER.p)));
00704         }
00705     } MGACOLL_ITERATE_END;
00706 }
00707 
00708 
00709 //Perform logical operations ANDing and ORing
00710 //to evaluate whether the final result is true
00711 
00712 bool CSearch::PerformLogical(int x,int y)
00713 {
00714     //-1 undefined, 0 -false, 1 - true
00715     //-1 is used to indicate that no search term is supplied in the 
00716     //corresponding field
00717     switch (filter.GetLogicalOperator())
00718     {
00719      //and
00720     case 0:
00721         return (x != 0) && (y != 0);
00722      //or
00723     case 1:
00724         return x==1 || y==1 || (x == -1 && y == -1);
00725     //xor
00726     case 2:
00727         return (x==-1 && y==1)||(x==1 && y==-1)||(x==1 && y==0)||(x==0 && y==1);
00728     }
00729 
00730     return false;
00731 }
00732 
00733 //check if the search criteria matches against the fco
00734 int CSearch::Matches(IMgaFCO* fco,bool first)
00735 {
00736     CString partName;
00737     CBstr bstr;
00738     CComPtr<IMgaMetaRole> rmeta;
00739 
00740     //get role
00741     COMTHROW( fco->get_MetaRole(&rmeta) );
00742     if(rmeta) 
00743     {
00744         COMTHROW( rmeta->get_Name(bstr) );
00745         partName = bstr;
00746     } 
00747     else 
00748         partName = _T("");
00749 
00750     //get the name
00751     CBstr bstrName;
00752     COMTHROW(fco->get_Name(bstrName));
00753     CString strName = bstrName;
00754 
00755     //get the KindName
00756     CBstr bstrKindName;
00757     CComPtr<IMgaMetaFCO> cmeta;
00758     COMTHROW( fco->get_Meta(&cmeta) );
00759     COMTHROW( cmeta->get_Name(bstrKindName) );
00760     CString kindName = bstrKindName;
00761 
00762     if( filter.IsCaseIgnored()) // case ignored, make values lowercase
00763     {
00764         strName.MakeLower();
00765         kindName.MakeLower();
00766         partName.MakeLower();
00767     }
00768     bool found = true;
00769     int result=-1;
00770     if(first)
00771     {
00772         if(!filter.GetFirstRole().IsEmpty())
00773             result=std::tr1::regex_search((LPCTSTR)partName,filter.GetFirstRoleRegExp())?1:0;
00774 
00775         if(!filter.GetFirstKind().IsEmpty() && result!=0)
00776             result=std::tr1::regex_search((LPCTSTR)kindName,filter.GetFirstKindRegExp())?1:0;
00777 
00778         if(!filter.GetFirstName().IsEmpty() &&result!=0)
00779             result=std::tr1::regex_search((LPCTSTR)strName,filter.GetFirstNameRegExp())?1:0;
00780 
00781         if(!filter.GetFirstAttribute().IsEmpty() &&result!=0)
00782             result=CheckAttributes(fco,first)?1:0;
00783     }
00784     else
00785     {
00786         if(!filter.GetSecondRole().IsEmpty())
00787             result=std::tr1::regex_search((LPCTSTR)partName,filter.GetSecondRoleRegExp())?1:0;
00788 
00789         if(!filter.GetSecondKind().IsEmpty()&& result!=0)
00790             result=std::tr1::regex_search((LPCTSTR)kindName,filter.GetSecondKindRegExp())?1:0;
00791 
00792         if(!filter.GetSecondName().IsEmpty() &&result!=0)
00793             result=std::tr1::regex_search((LPCTSTR)strName,filter.GetSecondNameRegExp())?1:0;
00794 
00795         if(!filter.GetSecondAttribute().IsEmpty() && result!=0)
00796             result=CheckAttributes(fco,first)?1:0;
00797     }
00798 
00799     return result;
00800 }