[Igarden-commit] Garden/source/SourceMaterialDB SourceQuery.h, 1.8.2.1, 1.8.2.2 SourceQuery.cpp, 1.
Status: Beta
Brought to you by:
jlbedell
|
From: Jeffrey B. <jlb...@us...> - 2008-05-13 03:22:24
|
Update of /cvsroot/igarden/Garden/source/SourceMaterialDB In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv26404/source/SourceMaterialDB Modified Files: Tag: Release_branch_v1 SourceQuery.h SourceQuery.cpp Log Message: Improve material search query Index: SourceQuery.h =================================================================== RCS file: /cvsroot/igarden/Garden/source/SourceMaterialDB/SourceQuery.h,v retrieving revision 1.8.2.1 retrieving revision 1.8.2.2 diff -C2 -d -r1.8.2.1 -r1.8.2.2 *** SourceQuery.h 4 Sep 2007 00:12:03 -0000 1.8.2.1 --- SourceQuery.h 13 May 2008 03:22:20 -0000 1.8.2.2 *************** *** 78,82 **** void BuildQuery( XGStreamHandle *stream ); void SecondarySearch(QueryResult *resultPtr); ! // attributes bool _enableRemoteSearch; --- 78,83 ---- void BuildQuery( XGStreamHandle *stream ); void SecondarySearch(QueryResult *resultPtr); ! long SuccessPercentage(); ! // attributes bool _enableRemoteSearch; Index: SourceQuery.cpp =================================================================== RCS file: /cvsroot/igarden/Garden/source/SourceMaterialDB/SourceQuery.cpp,v retrieving revision 1.19.2.4 retrieving revision 1.19.2.5 diff -C2 -d -r1.19.2.4 -r1.19.2.5 *** SourceQuery.cpp 21 Mar 2008 21:48:07 -0000 1.19.2.4 --- SourceQuery.cpp 13 May 2008 03:22:20 -0000 1.19.2.5 *************** *** 122,204 **** - // --------------------------------------------------------------------------- - /// SecondarySearch - // - /// Given a maximum number of responses, and using previously entered criteria, return a "top 10" type list of queryResult_t (an ObjAddr and a % match). */ - // - void SourceQuery::SecondarySearch(QueryResult *resultPtr) - { - InternetMaterialSearch *searchPlug; - String intName; - XGFileSpecifier pictSpec; - Int32 numPlugins, n; - bool isActive; - AppCallbacks *callback; - Plugin *plugin; - Material *intMaterial; - PluginManager *mgr = PluginManager::GetPluginManager(); - XGDirectory pictDir; - - MyAssert(this != NULL); - MyAssert(mgr != NULL); - MyAssert(resultPtr != NULL); - numPlugins = mgr->NumPluginsOfType(kSearchPlugin); - - callback = new AppCallbacks(); - - GetPictureRoot(pictSpec); - pictSpec.GetDirectory(pictDir); - - if(_enableRemoteSearch) - { - // Now that we have made primary matches, for each name matched, lookup - // the name on any scondary searches available. - MyAssert(resultPtr != NULL); - for(n = 0; n < numPlugins; n++) - { - isActive = mgr->IndexPluginsOfType(n, kSearchPlugin, &plugin); - if(isActive) - { - MyAssert(SUCCEEDED(plugin->QueryInterface( CFUUIDGetUUIDBytes( kMaterialSearchInterfaceID ), (LPVOID *)( &searchPlug ) ) ) ); - if(searchPlug->HandlesQueryTypes(kSecondarySearch)) - { - searchPlug->Setup(&pictDir, kPluginThermometer, callback); - resultPtr->Reset(); - intMaterial = resultPtr->NextMatch(); - while(intMaterial != NULL) - { - intName = intMaterial->GetCanonicalName(); - - XGFileHandle resultStream; - RGXMLWriter writer; - - writer.StartXMLWriter(&resultStream); - writer.WriteStartTag("Query"); - writer.WriteStartTagArg(kTagFieldCanonicalName, intName); - writer.WriteEndTag(); // Query - writer.EndXMLWriter(); - - String query(static_cast<char*>(*resultStream)); - CanonicalNameList_t result2; - String debug; - - InternetPlugin *myPlug; - MyAssert(SUCCEEDED(plugin->QueryInterface( CFUUIDGetUUIDBytes( kMaterialSearchInterfaceID ), (LPVOID *)( &myPlug ) ) ) ); - myPlug->GetName(debug); - - searchPlug->SetSourceQuery(kMaterialPropertiesSearch, query); - searchPlug->FindMatches(1, 20, result2); - delete intMaterial; - - intMaterial = resultPtr->NextMatch(); - } - } - } - } - } - - delete callback; - } - void SourceQuery::FindPictures(String &fullCanonicalName, pictureAmount_t amount, feedbackType_t feedback) { --- 122,125 ---- *************** *** 300,403 **** } - // --------------------------------------------------------------------------- - // void SourceQuery::BuildQuery( XGStreamHandle *stream ) - // --------------------------------------------------------------------------- - // - void SourceQuery::BuildQuery( XGStreamHandle *stream ) - { - XGXMLWriter writer; - String nameStr; - Int32 n, count; - materialType_t mtype; - char queryBuf[4096]; - ViewClass *vc; - attributeID_t propID; - const char *propName; - bool found; - long longVal; - - MyAssert(stream != NULL); - writer.StartXMLWriter(stream); - writer.WriteStartTag("Query"); - nameStr = GetCanonicalName(); - if(nameStr.Length() != 0) - { - writer.WriteStartTag(kTagFieldCanonicalName); - writer.WriteData(nameStr.c_str()); - writer.WriteEndTag(); // kTagFieldCanonicalName - } - else - { - nameStr = GetCommonName(); - if(nameStr.Length() != 0) - { - writer.WriteStartTag(kTagFieldCommonName); - writer.WriteData(nameStr.c_str()); - writer.WriteEndTag(); // kTagFieldCommonName - } - else - { - mtype = GetMaterialType(); - if(mtype != kMaterialTypeUnspecified) - { - writer.WriteStartTag(kTagFieldMaterialType); - sprintf(queryBuf, "%ld", static_cast<long>(mtype)); - writer.WriteData(queryBuf); - writer.WriteEndTag(); // CopyrightName - } - - vc = GetViewClass(); - - count = vc->NumProperties(); - for(n = 0; n < count; n++) - { - propID = static_cast<attributeID_t>(vc->GetNthPropertyID(n)); - if((propID != kFieldMaterialType) && AttributeDictionary::IsSearchAttr(propID) && AttributeDictionary::GetAttributeClass(propID) == kClassAttribute) - { - found = false; - - propName = vc->FieldName(propID); - switch(AttributeDictionary::TypeForID(propID)) - { - case kAttrMenu: - case kAttrBitfield: - if(GetInt32(propID, &longVal)) - { - if(longVal != 0) - { - sprintf(queryBuf, "%ld", longVal); - found = true; - } - } - break; - case kAttrSize: - case kAttrInteger: - if(GetInt32(propID, &longVal)) - { - sprintf(queryBuf, "%ld", longVal); - found = true; - } - break; - - default: - MyAssert(false); - break; - } - if(found) - { - writer.WriteStartTag("Arg"); - writer.WriteStartTagArg("Name", propName); - writer.WriteData((const char*)queryBuf); - writer.WriteEndTag(); // <Arg Name= > - } - } - } - } - } - writer.WriteEndTag(); // Query - writer.EndXMLWriter(); - stream->Write(1, ""); - stream->SetFilePos(0); - } // --------------------------------------------------------------------------- --- 221,224 ---- *************** *** 412,417 **** { QueryResult *localResult; ! Int16 percent; ! Int32 row, numRows, numVariants, v; Material *oneItem, *varItem; materialID_t id; --- 233,238 ---- { QueryResult *localResult; ! Int16 percent, successPercent; ! Int32 row, numRows, numVariants, v, keyMType, testMType; Material *oneItem, *varItem; materialID_t id; *************** *** 419,451 **** localResult = new QueryResult(_dbFile, SHRT_MAX); numRows = DBPersist::NumRows(_dbFile, kMaterialBodyCID); for(row = 0; row < numRows; row++) { oneItem = new Material(_dbFile, row); ! percent = oneItem->PercentageMatchInKey(this); ! if(percent != 0) { ! id = oneItem->GetMaterialID(); ! numVariants = oneItem->NumVariants(); ! if(numVariants != 0) { ! for(v = 0; v < numVariants; v++) { ! Variant *var; ! ! var = oneItem->GetIndexedVariant(v); ! var->GetVariantID(id.variant); ! varItem = new Material(_dbFile, id); ! percent = varItem->PercentageMatchInKey(this); ! if(percent != 0) { ! localResult->AppendItem(varItem->GetMaterialID(), percent, false); } - - delete varItem; } ! } ! else ! { localResult->AppendItem(oneItem->GetMaterialID(), percent, false); } --- 240,280 ---- localResult = new QueryResult(_dbFile, SHRT_MAX); + successPercent = SuccessPercentage(); numRows = DBPersist::NumRows(_dbFile, kMaterialBodyCID); + if(!GetInt32(kFieldMaterialType, &keyMType)) + keyMType = 0; + for(row = 0; row < numRows; row++) { oneItem = new Material(_dbFile, row); ! if(!oneItem->GetInt32(kFieldMaterialType, &testMType)) ! testMType = 0; ! ! if((keyMType == testMType) && (keyMType != 0)) { ! percent = oneItem->PercentageMatchInKey(this); ! if(percent > successPercent) { ! id = oneItem->GetMaterialID(); ! numVariants = oneItem->NumVariants(); ! if(numVariants != 0) { ! for(v = 0; v < numVariants; v++) { ! Variant *var; ! ! var = oneItem->GetIndexedVariant(v); ! var->GetVariantID(id.variant); ! varItem = new Material(_dbFile, id); ! percent = varItem->PercentageMatchInKey(this); ! if(percent > successPercent) ! { ! localResult->AppendItem(varItem->GetMaterialID(), percent, false); ! } ! ! delete varItem; } } ! localResult->AppendItem(oneItem->GetMaterialID(), percent, false); } *************** *** 756,757 **** --- 585,798 ---- return _statusEvent; } + + #pragma mark ***Private*** + + // --------------------------------------------------------------------------- + // void SourceQuery::BuildQuery( XGStreamHandle *stream ) + // --------------------------------------------------------------------------- + // + void SourceQuery::BuildQuery( XGStreamHandle *stream ) + { + XGXMLWriter writer; + String nameStr; + Int32 n, count; + materialType_t mtype; + char queryBuf[4096]; + ViewClass *vc; + attributeID_t propID; + const char *propName; + bool found; + long longVal; + + MyAssert(stream != NULL); + writer.StartXMLWriter(stream); + writer.WriteStartTag("Query"); + nameStr = GetCanonicalName(); + if(nameStr.Length() != 0) + { + writer.WriteStartTag(kTagFieldCanonicalName); + writer.WriteData(nameStr.c_str()); + writer.WriteEndTag(); // kTagFieldCanonicalName + } + else + { + nameStr = GetCommonName(); + if(nameStr.Length() != 0) + { + writer.WriteStartTag(kTagFieldCommonName); + writer.WriteData(nameStr.c_str()); + writer.WriteEndTag(); // kTagFieldCommonName + } + else + { + mtype = GetMaterialType(); + if(mtype != kMaterialTypeUnspecified) + { + writer.WriteStartTag(kTagFieldMaterialType); + sprintf(queryBuf, "%ld", static_cast<long>(mtype)); + writer.WriteData(queryBuf); + writer.WriteEndTag(); // CopyrightName + } + + vc = GetViewClass(); + + count = vc->NumProperties(); + for(n = 0; n < count; n++) + { + propID = static_cast<attributeID_t>(vc->GetNthPropertyID(n)); + if((propID != kFieldMaterialType) && AttributeDictionary::IsSearchAttr(propID) && AttributeDictionary::GetAttributeClass(propID) == kClassAttribute) + { + found = false; + + propName = vc->FieldName(propID); + switch(AttributeDictionary::TypeForID(propID)) + { + case kAttrMenu: + case kAttrBitfield: + if(GetInt32(propID, &longVal)) + { + if(longVal != 0) + { + sprintf(queryBuf, "%ld", longVal); + found = true; + } + } + break; + case kAttrSize: + case kAttrInteger: + if(GetInt32(propID, &longVal)) + { + sprintf(queryBuf, "%ld", longVal); + found = true; + } + break; + + default: + MyAssert(false); + break; + } + if(found) + { + writer.WriteStartTag("Arg"); + writer.WriteStartTagArg("Name", propName); + writer.WriteData((const char*)queryBuf); + writer.WriteEndTag(); // <Arg Name= > + } + } + } + } + } + writer.WriteEndTag(); // Query + writer.EndXMLWriter(); + stream->Write(1, ""); + stream->SetFilePos(0); + } + + // --------------------------------------------------------------------------- + /// SecondarySearch + // + /// Given a maximum number of responses, and using previously entered criteria, return a "top 10" type list of queryResult_t (an ObjAddr and a % match). */ + // + void SourceQuery::SecondarySearch(QueryResult *resultPtr) + { + InternetMaterialSearch *searchPlug; + String intName; + XGFileSpecifier pictSpec; + Int32 numPlugins, n; + bool isActive; + AppCallbacks *callback; + Plugin *plugin; + Material *intMaterial; + PluginManager *mgr = PluginManager::GetPluginManager(); + XGDirectory pictDir; + + MyAssert(this != NULL); + MyAssert(mgr != NULL); + MyAssert(resultPtr != NULL); + numPlugins = mgr->NumPluginsOfType(kSearchPlugin); + + callback = new AppCallbacks(); + + GetPictureRoot(pictSpec); + pictSpec.GetDirectory(pictDir); + + if(_enableRemoteSearch) + { + // Now that we have made primary matches, for each name matched, lookup + // the name on any scondary searches available. + MyAssert(resultPtr != NULL); + for(n = 0; n < numPlugins; n++) + { + isActive = mgr->IndexPluginsOfType(n, kSearchPlugin, &plugin); + if(isActive) + { + MyAssert(SUCCEEDED(plugin->QueryInterface( CFUUIDGetUUIDBytes( kMaterialSearchInterfaceID ), (LPVOID *)( &searchPlug ) ) ) ); + if(searchPlug->HandlesQueryTypes(kSecondarySearch)) + { + searchPlug->Setup(&pictDir, kPluginThermometer, callback); + resultPtr->Reset(); + intMaterial = resultPtr->NextMatch(); + while(intMaterial != NULL) + { + intName = intMaterial->GetCanonicalName(); + + XGFileHandle resultStream; + RGXMLWriter writer; + + writer.StartXMLWriter(&resultStream); + writer.WriteStartTag("Query"); + writer.WriteStartTagArg(kTagFieldCanonicalName, intName); + writer.WriteEndTag(); // Query + writer.EndXMLWriter(); + + String query(static_cast<char*>(*resultStream)); + CanonicalNameList_t result2; + String debug; + + InternetPlugin *myPlug; + MyAssert(SUCCEEDED(plugin->QueryInterface( CFUUIDGetUUIDBytes( kMaterialSearchInterfaceID ), (LPVOID *)( &myPlug ) ) ) ); + myPlug->GetName(debug); + + searchPlug->SetSourceQuery(kMaterialPropertiesSearch, query); + searchPlug->FindMatches(1, 20, result2); + delete intMaterial; + + intMaterial = resultPtr->NextMatch(); + } + } + } + } + } + + delete callback; + } + + + // --------------------------------------------------------------------------- + /// SecondarySearch + // + /// returns the percentage match required for success. + /// For now, 2 elements of the query must match + // + long SourceQuery::SuccessPercentage() + { + c4_View *view; + long count, iter, itemsInKey, result; + propertyID_t propertyID; + + view = GetView(); + count = GetViewClass()->NumProperties(); + + for(iter = 0, itemsInKey=0; iter < count; iter++) + { + propertyID = GetViewClass()->GetNthPropertyID(iter); + if(!isEmpty(propertyID)) // Must be something in pattern (mat) + itemsInKey++; + } + if(itemsInKey <= 2) + result = 99; // Not 100% to avoid rounding errors + else + result = static_cast<long>(2.0 / itemsInKey); + + return result; + } |