Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

omcsnet.cpp

Go to the documentation of this file.
00001 /*  This file is part of OMCSNetCPP
00002 
00003     OMCSNetCPP is free software; you can redistribute it and/or modify
00004     it under the terms of the GNU Lesser General Public License as published by
00005     the Free Software Foundation; either version 2.1 of the License, or
00006     (at your option) any later version.
00007 
00008     OMCSNetCPP is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011     GNU Lesser General Public License for more details.
00012 
00013     You should have received a copy of the GNU Lesser General Public License
00014     along with OMCSNetCPP; if not, write to the Free Software
00015     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016  */
00017 
00020 #include "omcsnet.h"
00021 #include "pred_fileio.h"
00022 #include "pred_decoder.h"
00023 #include "find_cfg.h"
00024 #include "findc_cfg.h"
00025 #include "finda_cfg.h"
00026 #include "debug.h"
00027 
00028 
00029 bool COMCSNet::LoadPredicateFile(const std::string &a_path)
00030 {
00031         CPredicateFileIO l_io;
00032 
00033         if (true == l_io.OpenInputFile(a_path))
00034         {
00035                 std::string l_predicateStr;
00036                 u_int       l_count        = 0;
00037 
00038                 do
00039                 {
00040                         l_predicateStr = l_io.GetPredicate(NULL);
00041 
00042                         if (l_predicateStr.size() > 0)
00043                         {
00044                                 CPredicateDecoder l_decoder;
00045 
00046                                 l_decoder.DecodePredicateText(l_predicateStr,
00047                                                               m_textIDMap,
00048                                                               m_idFactory,
00049                                                               m_conceptMap,
00050                                                               m_linkMap);
00051 
00052                                 l_count++;
00053 
00054                                 DPRINT("Loaded %d Predicates.\n", l_count);
00055 
00056                                 DPRINT("%d Concepts.\n",m_conceptMap.GetSize());
00057                                 DPRINT("%d Links.\n", m_linkMap.GetSize());
00058                         }
00059                 } while (l_predicateStr.size() > 0);
00060 
00061                 l_io.CloseInputFile();
00062         }
00063 
00064         DPRINT("[%d] Concepts Loaded.\n", m_conceptMap.GetSize());
00065         DPRINT("%d Links.\n", m_linkMap.GetSize());
00066 
00067         return true;
00068 }
00069 
00070 
00071 
00072 bool COMCSNet::FindAnalogous(CFindAnalogousConfig &a_config)
00073 {
00074         CSemanticConcept *l_concept = NULL;
00075 
00076         l_concept = LookupConcept(a_config.GetNodeName());
00077 
00078         if (!l_concept)
00079         {
00080                 return false;
00081         }
00082 
00083         DPRINT("gathering candidate nodes.\n");
00084 
00085         for (u_int i = 0;i < MAX_RELATIONS;++i)
00086         {
00087                 DPRINT("processing %d\n", i);
00088 
00089                 CLinkMap *l_linkMap = l_concept->GetLinks(i);
00090 
00091                 p_AnalogousProcessLinkMap(l_linkMap, a_config);
00092 
00093                 if (false == a_config.AllowVisit())
00094                 {
00095                         break;
00096                 }
00097         }
00098 
00099         CAnalogousWeightNodeMap_i l_iter;
00100 
00101         l_iter = a_config.GetWeightedIterator();
00102 
00103         CAnalogousWeightNode *l_weightedNode = NULL;
00104 
00105         DPRINT("processing weighted nodes.\n");
00106 
00107         do
00108         {
00109                 l_weightedNode = a_config.GetWeightedNode(l_iter);
00110 
00111                 if (l_weightedNode)
00112                 {
00113                         p_AnalogousProcessWeightedNode(l_weightedNode);
00114                 }
00115         } while (l_weightedNode);
00116 
00117         typedef std::multimap<u_int, CAnalogousWeightNode *>   CWeightSortMap;
00118         typedef CWeightSortMap::reverse_iterator            CWeightSortMap_ri;
00119 
00120         CWeightSortMap    l_weightSortTable;
00121         CWeightSortMap_ri l_weightSortIter;
00122 
00123         DPRINT("sorting results.\n");
00124 
00125         // copy results to sort map
00126         l_iter = a_config.GetWeightedIterator();
00127 
00128         do
00129         {
00130                 l_weightedNode = a_config.GetWeightedNode(l_iter);
00131 
00132                 if (l_weightedNode)
00133                 {
00134                         l_weightSortTable.insert(
00135                                 std::make_pair(l_weightedNode->GetWeightValue(),
00136                                             l_weightedNode));
00137                 }
00138         } while (l_weightedNode);
00139 
00140         DPRINT("copying results to output.\n");
00141 
00142         // take top X results
00143         for (l_weightSortIter = l_weightSortTable.rbegin();
00144              l_weightSortIter != l_weightSortTable.rend();++l_weightSortIter)
00145         {
00146                 DPRINT("adding to result.\n");
00147 
00148                 float l_result = (*l_weightSortIter).first / 200.0;
00149 
00150                 if (l_result > 0.90)
00151                 {
00152                         l_result = 0.90;
00153                 }
00154 
00155                 if (false == a_config.AddNodeToResults(
00156                                   (*l_weightSortIter).second, l_result))
00157                 {
00158                         break;
00159                 }
00160         }
00161 
00162         DPRINT("done.\n");
00163 
00164         if (a_config.GetNumResults() < 1)
00165         {
00166                 return false;
00167         }
00168 
00169         DPRINT("success!\n");
00170 
00171         return true;
00172 }
00173 
00174 
00175 void COMCSNet::p_AnalogousProcessWeightedNode(CAnalogousWeightNode *a_node)
00176 {
00177         if (!a_node)
00178         {
00179                 return;
00180         }
00181 
00182         CSemanticLinkMap_i i;
00183 
00184         i = a_node->GetLinkIterator();
00185 
00186         CSemanticLink *l_link = NULL;
00187 
00188         typedef std::map<RelationType, RelationType> CRelationTypeMap;
00189         typedef CRelationTypeMap::iterator           CRelationTypeMap_i;
00190 
00191         CRelationTypeMap   l_relationMap;
00192         CRelationTypeMap_i l_relationIter;
00193 
00194         do
00195         {
00196                 l_link = a_node->GetLink(i);
00197 
00198                 if (l_link)
00199                 {
00200                         l_relationIter = l_relationMap.find(l_link->GetType());
00201 
00202                         if ((l_relationIter      == l_relationMap.end()) &&
00203                             (RELATION_OFTEN_NEAR != l_link->GetType()))
00204                         {
00205                                 l_relationMap[l_link->GetType()] =
00206                                                         l_link->GetType();
00207                         }
00208 
00209                         if (RELATION_OFTEN_NEAR == l_link->GetType())
00210                         {
00211                                 a_node->AddWeightValue(1);
00212                         }
00213                         else if (RELATION_LOCATION_OF == l_link->GetType())
00214                         {
00215                                 a_node->AddWeightValue(5);
00216                         }
00217                         else if ((RELATION_USED_FOR    == l_link->GetType()) ||
00218                                  (RELATION_PROPERTY_OF == l_link->GetType()))
00219                         {
00220                                 a_node->AddWeightValue(12);
00221                         }
00222                         else
00223                         {
00224                                 a_node->AddWeightValue(8);
00225                         }
00226                 }
00227         } while (l_link);
00228 
00229         a_node->AddWeightValue(25 * l_relationMap.size());
00230 }
00231 
00232 bool COMCSNet::FindContext(const std::string          &a_conceptList,
00233                                  CFindContextConfig &a_config)
00234 {
00235         QStringList l_conceptNames;
00236 
00237         l_conceptNames = QStringList::split(',', a_conceptList);
00238 
00239         CSemanticConcept *l_concept = NULL;
00240 
00241         // get initial list of origin score
00242         for (QStringList::iterator i = l_conceptNames.begin();
00243              i != l_conceptNames.end();++i)
00244         {
00245                 l_concept = LookupConcept((*i));
00246 
00247                 if (l_concept)
00248                 {
00249                         a_config.QueueScore(l_concept, 1.0);
00250                 }
00251         }
00252 
00253         DPRINT("%d source concepts.\n", l_conceptNames.size());
00254 
00255         float l_score = 0.0;
00256 
00257         CResultsList_i l_iter = a_config.GetScoreIterator();
00258 
00259         // build list of initial scores based on connecting nodes
00260         do
00261         {
00262                 l_concept = a_config.GetScoreQueue(l_iter, &l_score);
00263 
00264                 DPRINT("building old score %f\n", l_score);
00265 
00266                 if (l_concept)
00267                 {
00268                         for (u_int i = 0;i < MAX_RELATIONS;++i)
00269                         {
00270                                 CLinkMap *l_linkMap = l_concept->GetLinks(i);
00271 
00272                                 p_FindContextProcessLinkMap(
00273                                                 l_linkMap, a_config, l_score);
00274 
00275                                 if (false == a_config.AllowVisit())
00276                                 {
00277                                         break;
00278                                 }
00279                         }
00280                 }
00281         } while ((l_concept) && (true == a_config.AllowVisit()));
00282 
00283         typedef std::map<SemanticID, float> CScoreMap;
00284         typedef CScoreMap::iterator         CScoreMap_i;
00285 
00286         CScoreMap   l_scoreLookupTable;
00287         CScoreMap_i l_scoreIter;
00288 
00289         l_iter = a_config.GetScoreIterator();
00290 
00291         float l_lookupTableScore = 0.0;
00292         float l_minScore         = 0.0;
00293         float l_maxScore         = 0.0;
00294         
00295         DPRINT("calculating score list.\n");
00296 
00297         // calculate score list
00298         do
00299         {
00300                 l_concept = a_config.GetScoreQueue(l_iter, &l_score);
00301 
00302                 if (l_concept)
00303                 {
00304                         l_scoreIter = l_scoreLookupTable.find(
00305                                                         l_concept->GetID());
00306 
00307                         if (l_scoreLookupTable.end() != l_scoreIter)
00308                         {
00309                                 l_lookupTableScore = (*l_scoreIter).second;
00310                         }
00311                         else
00312                         {
00313                                 l_lookupTableScore = 0.0;
00314                         }
00315 
00316                         if (l_lookupTableScore > l_score)
00317                         {
00318                                 l_maxScore = l_lookupTableScore;
00319 
00320                                 l_minScore = l_score;
00321                         }
00322                         else
00323                         {
00324                                 l_minScore = l_lookupTableScore;
00325 
00326                                 l_maxScore = l_score;
00327                         }
00328 
00329                         l_score = l_maxScore +
00330                                   ((1.0 - l_maxScore) * l_minScore);
00331 
00332                         l_scoreLookupTable[l_concept->GetID()] = l_score;
00333                 }
00334         } while (l_concept);
00335         
00336         DPRINT("score lookup table size = %d\n", l_scoreLookupTable.size());
00337 
00338         DPRINT("delete origin nodes from score list.\n");
00339 
00340         // delete origin nodes from score list
00341         for (QStringList::iterator i = l_conceptNames.begin();
00342              i != l_conceptNames.end();++i)
00343         {
00344                 l_concept = LookupConcept((*i));
00345 
00346                 if (l_concept)
00347                 {
00348                         l_scoreIter = l_scoreLookupTable.find(
00349                                                         l_concept->GetID());
00350 
00351                         if (l_scoreLookupTable.end() != l_scoreIter)
00352                         {
00353                                 DPRINT("deleting item.\n");
00354 
00355                                 l_scoreLookupTable.erase(l_scoreIter);
00356                         }
00357                 }
00358         }
00359 
00360         typedef std::multimap<float, SemanticID>         CScoreSortMap;
00361         typedef CScoreSortMap::reverse_iterator     CScoreSortMap_ri;
00362 
00363         CScoreSortMap    l_scoreSortTable;
00364         CScoreSortMap_ri l_scoreSortIter;
00365 
00366         DPRINT("sorting results.\n");
00367 
00368         // copy results to sort map
00369         for (l_scoreIter = l_scoreLookupTable.begin();
00370              l_scoreIter != l_scoreLookupTable.end();++l_scoreIter)
00371         {
00372                 DPRINT("adding to sort table.\n");
00373 
00374                 //l_scoreSortTable[(*l_scoreIter).second] = (*l_scoreIter).first;
00375 
00376                 l_scoreSortTable.insert(std::make_pair((*l_scoreIter).second,
00377                                                   (*l_scoreIter).first));
00378         }
00379 
00380         DPRINT("copying results to output.\n");
00381 
00382         // take top X results
00383         for (l_scoreSortIter = l_scoreSortTable.rbegin();
00384              l_scoreSortIter != l_scoreSortTable.rend();++l_scoreSortIter)
00385         {
00386                 l_concept = m_conceptMap.Lookup((*l_scoreSortIter).second);
00387 
00388                 if (l_concept)
00389                 {
00390                         DPRINT("adding to result.\n");
00391 
00392                         if (false == a_config.AddNodeToResults(l_concept,
00393                                                   (*l_scoreSortIter).first))
00394                         {
00395                                 break;
00396                         }
00397                 }
00398         }
00399 
00400         DPRINT("done.\n");
00401 
00402         if (a_config.GetNumResults() < 1)
00403         {
00404                 return false;
00405         }
00406 
00407         DPRINT("success!\n");
00408 
00409         return true;
00410 }
00411 
00412 
00413 bool COMCSNet::p_AnalogousProcessLinkMap(CLinkMap             *a_linkMap,
00414                                          CFindAnalogousConfig &a_config)
00415 {
00416         DPRINT("processing linkmap.\n");
00417 
00418         if (!a_linkMap)
00419         {
00420                 return false;
00421         }
00422 
00423         CSemanticLinkMap_i l_linkIter;
00424 
00425         l_linkIter = a_linkMap->GetIterator();
00426 
00427         CSemanticLink *l_link = NULL;
00428 
00429         do
00430         {
00431                 l_link = a_linkMap->GetNext(l_linkIter);
00432 
00433                 if ((l_link) && (a_config.AllowVisit()))
00434                 {
00435                         p_AnalogousProcessLink(l_link, a_config);
00436                 }
00437         } while ((l_link) && (a_config.AllowVisit()));
00438 
00439         return true;
00440 }
00441 
00442 
00443 bool COMCSNet::p_AnalogousProcessBackwardLinkMap(CLinkMap         *a_linkMap,
00444                                              CFindAnalogousConfig &a_config,
00445                                              const RelationType    a_relation)
00446 {
00447         DPRINT("processing backward linkmap.\n");
00448 
00449         if (!a_linkMap)
00450         {
00451                 return false;
00452         }
00453 
00454         CSemanticLinkMap_i l_linkIter;
00455 
00456         l_linkIter = a_linkMap->GetIterator();
00457 
00458         CSemanticLink *l_link = NULL;
00459 
00460         do
00461         {
00462                 l_link = a_linkMap->GetNext(l_linkIter);
00463 
00464                 if ((l_link) && (a_config.AllowVisit()))
00465                 {
00466                         p_AnalogousProcessBackwardLink(l_link, a_config,
00467                                                        a_relation);
00468                 }
00469         } while ((l_link) && (a_config.AllowVisit()));
00470 
00471         return true;
00472 }
00473 
00474 
00475 void COMCSNet::p_AnalogousProcessLink(CSemanticLink        *a_link,
00476                                       CFindAnalogousConfig &a_config)
00477 {
00478         CSemanticConcept *l_concept;
00479         RelationType      l_relation;
00480 
00481         l_relation = a_link->GetType();
00482         l_concept  = a_link->GetDest();
00483 
00484         if (l_concept)
00485         {
00486                 a_config.VisitNode();
00487 
00488                 for (u_int i = 0;i < MAX_RELATIONS;++i)
00489                 {
00490                         CLinkMap *l_linkMap = l_concept->GetBackwardLinks(i);
00491                 
00492                         p_AnalogousProcessBackwardLinkMap(l_linkMap, a_config,
00493                                                           l_relation);
00494 
00495                         if (false == a_config.AllowVisit())
00496                         {
00497                                 break;
00498                         }
00499                 }
00500         }
00501 }
00502 
00503 
00504 void COMCSNet::p_AnalogousProcessBackwardLink(CSemanticLink        *a_link,
00505                                               CFindAnalogousConfig &a_config,
00506                                               const RelationType    a_relation)
00507 {
00508         CSemanticConcept *l_concept;
00509 
00510         DPRINT("processing backward link.\n");
00511 
00512         l_concept  = a_link->GetSource();
00513 
00514         if (l_concept)
00515         {
00516                 if ((a_config.GetNodeName() != l_concept->GetName()) &&
00517                     (a_relation             == a_link->GetType()))
00518                 {
00519                         a_config.VisitNode();
00520 
00521                         DPRINT("storing weighted link.\n");
00522 
00523                         a_config.StoreWeightedLink(a_link);
00524                 }
00525         }
00526 }
00527 
00528 bool COMCSNet::p_FindContextProcessLinkMap(CLinkMap             *a_linkMap,
00529                                            CFindContextConfig &a_config,
00530                                            const float           a_oldScore)
00531 {
00532         DPRINT("OLD %f\n", a_oldScore);
00533 
00534         if (!a_linkMap)
00535         {
00536                 return false;
00537         }
00538 
00539         CSemanticLinkMap_i l_linkIter;
00540 
00541         l_linkIter = a_linkMap->GetIterator();
00542 
00543         CSemanticLink *l_link = NULL;
00544 
00545         do
00546         {
00547                 l_link = a_linkMap->GetNext(l_linkIter);
00548 
00549                 if ((l_link) && (a_config.AllowVisit()))
00550                 {
00551                         CSemanticConcept *l_concept;
00552 
00553                         l_concept = l_link->GetDest();
00554 
00555                         if (l_concept)
00556                         {
00557                                 a_config.VisitNode();
00558 
00559                                 float l_score = (a_oldScore *
00560                                                 a_config.GetDiscountFactor());
00561 
00562                                 a_config.QueueScore(l_concept, l_score);
00563                         }
00564                 }
00565         } while ((l_link) && (a_config.AllowVisit()));
00566 
00567         return true;
00568 }
00569 
00570 
00571 bool COMCSNet::FindPathFromA_To_B(const std::string     &a_src,
00572                                   const std::string     &a_dst,
00573                                         CFindPathConfig &a_config)
00574 {
00575         CSemanticConcept *l_srcConcept;
00576         CSemanticConcept *l_dstConcept;
00577 
00578         l_srcConcept = LookupConcept(a_src);
00579         l_dstConcept = LookupConcept(a_dst);
00580 
00581         if ((l_srcConcept) && (l_dstConcept))
00582         {
00583                 SemanticID l_dstID = l_dstConcept->GetID();
00584         
00585                 CSemanticConcept *l_concept = l_srcConcept;
00586 
00587                 CSemanticLinkList *l_list;
00588 
00589                 while ((l_concept) && (true == a_config.AllowVisit()))
00590                 {
00591                         DPRINT("findpathto for concept.\n");
00592 
00593                         l_concept->FindPathTo(l_dstID, a_config);
00594 
00595                         l_concept = NULL;
00596 
00597                         l_list = a_config.GetQueuePath();
00598 
00599                         if (l_list)
00600                         {
00601                                 DPRINT("got queued path.\n");
00602 
00603                                 CLinkList_ri tail_i;
00604 
00605                                 tail_i = l_list->GetReverseIterator();
00606 
00607                                 CSemanticLink *l_link = l_list->GetNext(tail_i);
00608 
00609                                 if (l_link)
00610                                 {
00611                                         DPRINT("got tail link\n");
00612 
00613                                         l_concept = l_link->GetDest();
00614 
00615                                         if (false ==
00616                                            a_config.CheckCurrentPath(l_link))
00617                                         {
00618                                                 a_config.SetCurrentPath(l_list);
00619                                         }
00620                                 }       
00621                         }               
00622                 }
00623 
00624                 if (a_config.GetNumResults() > 0)
00625                 {
00626                         DPRINT("%d nodes visited.\n", a_config.GetNumNodeVisits());
00627 
00628                         DPRINT("%d result paths found.\n", a_config.GetNumResults());
00629 
00630                         return true;
00631                 }
00632         }
00633 
00634         return false;
00635 }
00636 
00637 
00638 std::string COMCSNet::LookupString(const SemanticID a_id)
00639 {
00640         if (SEMANTIC_ID_NONE != a_id)
00641         {
00642                 CSemanticConcept *l_concept;
00643 
00644                 l_concept = m_conceptMap.Lookup(a_id);
00645 
00646                 if (l_concept)
00647                 {
00648                         return l_concept->GetName();
00649                 }
00650         }
00651 
00652         return "";
00653 }
00654 
00655 

Generated on Tue Sep 16 09:43:50 2003 for OMCSNetCPP by doxygen 1.3.3