Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

AnnotationTableSet.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Table_AnnotationTableSet_h
00002 #define Impala_Core_Table_AnnotationTableSet_h
00003 
00004 #include <vector>
00005 #include "Core/Table/KeywordList.h"
00006 #include "Core/Table/AnnotationTable.h"
00007 #include "Core/Table/Copy.h"
00008 #include "Core/Table/Append.h"
00009 #include "Core/Table/Select.h"
00010 
00011 namespace Impala
00012 {
00013 namespace Core
00014 {
00015 namespace Table
00016 {
00017 
00018 
00019 // basically a wrapper around std::vector of AnnotationTables.
00020 class AnnotationTableSet
00021 {
00022 public:
00023 
00024     AnnotationTableSet()
00025     {
00026     }
00027 
00028     AnnotationTableSet(KeywordList labels, int tableSize)
00029     {
00030         for (size_t i=0 ; i<labels.size() ; i++)
00031         {
00032             AnnotationTable* tab = new AnnotationTable(labels[i], 0);
00033             Add(tab);
00034         }
00035     }
00036 
00037     ~AnnotationTableSet()
00038     {
00039         // no destruction of elements, as std::vector doesn't do it either
00040         //for (int i=0 ; i<mTables.size() ; i++)
00041         //    delete mTables[i];
00042     }
00043 
00044 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00045     static AnnotationTableSet*
00046     MakeFromFile(Database::RawDataSet* dataSet, CString labelSet,
00047                  bool readTables, int quidClass)
00048     {
00049         String fName = dataSet->GetFilePathAnnotation(labelSet, false, false);
00050         if (fName.empty())
00051         {
00052             ILOG_ERROR("Unable to read labels");
00053             return 0;
00054         }
00055         Util::Database* db = dataSet->GetDatabase();
00056         std::vector<String> labels;
00057         Util::DatabaseReadStrings(labels, fName, db);
00058 
00059         AnnotationTableSet* res = new AnnotationTableSet();
00060         int nrRead = 0;
00061         for (int i=0 ; i<labels.size() ; i++)
00062         {
00063             AnnotationTable* table = 0;
00064             if (readTables)
00065             {
00066                 fName = dataSet->GetFilePathAnnotation(quidClass, labelSet,
00067                                                        labels[i] + ".tab",
00068                                                        false, true);
00069                 if (! fName.empty())
00070                 {
00071                     table = AnnotationTable::MakeFromFile(labels[i], fName, db);
00072                     nrRead++;
00073                 }
00074             }
00075             if (table == 0)
00076             {
00077                 table = new AnnotationTable(labels[i], 0);
00078             }
00079             res->Add(table);
00080         }
00081         if (readTables && (nrRead == 0))
00082             ILOG_ERROR("No tables found");
00083         return res;
00084     }
00085 #endif // REPOSITORY_USED
00086 
00087     void
00088     Delete() // requires "explicit call to destructor"
00089     {
00090         for (int i=0 ; i<mTables.size() ; i++)
00091             delete mTables[i];
00092         mTables.clear();
00093     }
00094 
00095     int
00096     Size()
00097     {
00098         return mTables.size();
00099     }
00100 
00101     void
00102     Add(AnnotationTable* table)
00103     {
00104         mTables.push_back(table);
00105     }
00106 
00107     KeywordList
00108     GetLabels() const
00109     {
00110         KeywordList res;
00111         for (int i=0 ; i<mTables.size() ; i++)
00112             res.push_back(GetLabel(i));
00113         return res;
00114     }
00115 
00116     String
00117     GetLabel(int i) const
00118     {
00119         return mTables[i]->GetLabel();
00120     }
00121 
00122     AnnotationTable*
00123     GetTable(int i) const
00124     {
00125         return mTables[i];
00126     }
00127 
00128     // return table with given label
00129     AnnotationTable*
00130     GetTable(CString label)
00131     {
00132         for (int i=0 ; i<mTables.size() ; i++)
00133             if (GetLabel(i) == label)
00134                 return mTables[i];
00135         return 0;
00136     }
00137 
00138     // Merge sets by appending to existing tables
00139     void
00140     Merge(AnnotationTableSet* arg)
00141     {
00142         for (int i=0 ; i<arg->Size() ; i++)
00143         {
00144             AnnotationTable* argTab = arg->GetTable(i);
00145             String label = arg->GetLabel(i);
00146             AnnotationTable* thisTab = GetTable(label);
00147             if (thisTab == 0)
00148             {
00149                 thisTab = new AnnotationTable(argTab->GetLabel(), 0);
00150                 Copy(thisTab, argTab);
00151                 Add(thisTab);
00152             }
00153             else
00154             {
00155                 Append(thisTab, argTab);
00156             }
00157         }
00158     }
00159 
00160     void
00161     Correct(AnnotationTableSet* corrections)
00162     {
00163         for (int i=0 ; i<corrections->Size() ; i++)
00164         {
00165             AnnotationTable* corTab = corrections->GetTable(i);
00166             String label = corrections->GetLabel(i);
00167             AnnotationTable* thisTab = GetTable(label);
00168             if (thisTab == 0)
00169             {
00170                 thisTab = new AnnotationTable(corTab->GetLabel(), 0);
00171                 Copy(thisTab, corTab);
00172                 Add(thisTab);
00173             }
00174             else
00175             {
00176                 thisTab->Correct(corTab);
00177             }
00178         }
00179     }
00180 
00181     int
00182     Diff(AnnotationTableSet* arg)
00183     {
00184         if (Size() != arg->Size())
00185         {
00186             ILOG_ERROR("Diff: Size differs: " << Size() << " vs " <<
00187                        arg->Size());
00188             return 1;
00189         }
00190         int nDiff = 0;
00191         for (int i=0 ; i<Size() ; i++)
00192         {
00193             if (GetLabel(i) != arg->GetLabel(i))
00194             {
00195                 ILOG_DEBUG("Label " << i << " differs " << GetLabel(i) <<
00196                            " vs " << arg->GetLabel(i));
00197                 nDiff++;
00198             }
00199             else if (GetTable(i)->Diff(arg->GetTable(i)) > 0)
00200             {
00201                 ILOG_DEBUG("Table " << i << " differs ");
00202                 nDiff++;
00203             }
00204         }
00205         if (nDiff > 0)
00206             ILOG_ERROR("Found " << nDiff << " differences");
00207         return nDiff;
00208     }
00209 
00210     QuidTable*
00211     GetUniqueQuids()
00212     {
00213         AnnotationTable* allAnno = new AnnotationTable();
00214         for (int i=0 ; i<Size() ; i++)
00215         {
00216             SelectUniqueOnCol(allAnno, GetTable(i), 1, false);
00217         }
00218         QuidTable* quids = new QuidTable();
00219         Copy(quids, allAnno);
00220         delete allAnno;
00221         return quids;
00222     }
00223 
00224 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00225     bool
00226     SaveTables(Database::RawDataSet* dataSet, String conceptSet, bool report)
00227     {
00228         bool ok = true;
00229         for (int i=0 ; i<Size() ; i++)
00230         {
00231             AnnotationTable* annoTable = GetTable(i);
00232             if (annoTable->Size() == 0)
00233                 continue;
00234             int qClass = annoTable->GetQuidClass();
00235             String fName = dataSet->GetFilePathAnnotation(qClass, conceptSet,
00236                                                           GetLabel(i) + ".tab",
00237                                                           true, false);
00238             if (fName.empty())
00239             {
00240                 ok = false;
00241                 continue;
00242             }
00243             Write(annoTable, fName, dataSet->GetDatabase(), true);
00244             if (report)
00245                 annoTable->DumpSummary();
00246         }
00247         return ok;
00248     }
00249 #endif // REPOSITORY_USED
00250 
00251     void
00252     Dump(bool doTable)
00253     {
00254         for (int i=0 ; i<mTables.size() ; i++)
00255         {
00256             std::cout << "AnnotationTable " << i << ", label = " << GetLabel(i)
00257                       << std::endl;
00258             if (doTable)
00259                 GetTable(i)->Dump();
00260         }
00261     }
00262 
00263 private:
00264 
00265     std::vector<AnnotationTable*> mTables;
00266 
00267     ILOG_VAR_DEC;
00268 };
00269 
00270 ILOG_VAR_INIT(AnnotationTableSet, Impala.Core.Table);
00271 
00272 } // namespace Table
00273 } // namespace Core
00274 } // namespace Impala
00275 
00276 #endif

Generated on Thu Jan 13 09:04:37 2011 for ImpalaSrc by  doxygen 1.5.1