casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TBTable.h
Go to the documentation of this file.
00001 //# Table.h: Primary interface for the rest of the browser to a table.
00002 //# Copyright (C) 2007-2008
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef TBTABLE_H_
00028 #define TBTABLE_H_
00029 
00030 #include <casaqt/QtBrowser/TBConstants.h>
00031 #include <casaqt/QtBrowser/TBPlotter.qo.h>
00032 
00033 #include <casa/BasicSL/String.h>
00034 
00035 #include <map>
00036 #include <vector>
00037 
00038 #include <casa/namespace.h>
00039 using namespace std;
00040 
00041 namespace casa {
00042 
00043 //# Forward Declarations
00044 class TBField;
00045 class TBKeyword;
00046 class TBArray;
00047 class ProgressHelper;
00048 class XMLtoken;
00049 class TBParser;
00050 class TBTableDriver;
00051 class TBData;
00052 class TBArrayData;
00053 class TBPlotData;
00054 class TBFilterRuleSequence;
00055 
00056 // <summary>
00057 // Parameters to indicate what driver should be used for the table backend.
00058 // </summary>
00059 //
00060 // <synopsis>
00061 // The DriverParams consist of two parameters: the driver type and (if
00062 // applicable) the parser type.  The parser type is only used for the XML
00063 // driver.
00064 // </synopsis>
00065 
00066 class DriverParams {
00067 public:
00068     // Default Constructor.
00069     DriverParams(Driver t = DIRECT, Parser p = XERCES_SAX) : type(t),
00070                                                              parser(p) { }
00071 
00072     // Copy Constructor.
00073     DriverParams(DriverParams* dp) : type(dp->type), parser(dp->parser) { }
00074 
00075     
00076     // Driver type.
00077     Driver type;
00078 
00079     // Parser type (for XML driver).
00080     Parser parser;
00081 };
00082 
00083 // <summary>
00084 // Parameters that define all table data and meta-deta.
00085 // </summary>
00086 //
00087 // <synopsis>
00088 // A TableParams contains references to all important table members.  Since
00089 // this information needs to be accessed and potentially changed by both the
00090 // TBTable and the TBTableDriver, the references are shared by passing a
00091 // TableParams object.
00092 // </synopsis>
00093 
00094 class TableParams {
00095 public:
00096     TableParams(String& l, bool& ir, bool& rr,
00097                 vector<vector<TBData*>*>& d,
00098                 vector<TBField*>& f, vector<TBKeyword*>& k,
00099                 vector<int>& sr, int& tr, int& lr, vector<bool>& w, bool& t,
00100                 DriverParams* dp): location(l), insertRow(ir), removeRow(rr),
00101                 data(d), fields(f), keywords(k), subtableRows(sr),
00102                 totalRows(tr), loadedRows(lr), writable(w), taql(t),
00103                 dParams(dp) { }
00104 
00105                 
00106     // See TBTable::location.
00107     String& location;
00108     
00109     // See TBTable::insertRow.
00110     bool& insertRow;
00111     
00112     // See TBTable::removeRow.
00113     bool& removeRow;
00114     
00115     // See TBTable::data.    
00116     vector<vector<TBData*>*>& data;
00117     
00118     // See TBTable::fields.
00119     vector<TBField*>& fields;
00120     
00121     // See TBTable::keywords.
00122     vector<TBKeyword*>& keywords;
00123     
00124     // See TBTable::subtableRows.
00125     vector<int>& subtableRows;
00126     
00127     // See TBTable::totalRows.
00128     int& totalRows;
00129     
00130     // See TBTable::loadedRows.
00131     int& loadedRows;
00132     
00133     // See TBTable::writable.
00134     vector<bool>& writable;
00135     
00136     // See TBTable::taql.
00137     bool& taql;
00138     
00139     // See TBTable::dParams.
00140     DriverParams* dParams;
00141 };
00142 
00143 // <summary>
00144 // Primary interface for the rest of the browser to a table.
00145 // </summary>
00146 // 
00147 // <synopsis>
00148 // Although TBTable is considered the table backend, it is actually more like
00149 // an interface over a TBTableDriver.  However, since there may be multiple
00150 // implementations of the driver, the table provides a unified interface over
00151 // the driver as well as other useful operations.  Generally speaking, the
00152 // driver handles dealing with the table on disk while the TBTable handles
00153 // dealing with the data represented in memory.
00154 // </synopsis>
00155 
00156 class TBTable {
00157 public:
00158     // Constructor that takes the filename and driver parameters.  If taql is
00159     // true, then this table is a table constructed from a TaQL command stored
00160     // in filename; otherwise filename holds the location on disk of the table.
00161     TBTable(String filename, DriverParams* dp, bool taql = false);
00162 
00163     ~TBTable();
00164     
00165 
00166     // Returns the filename where this table is located.
00167     String getFile();
00168 
00169     // Returns the name of this table (which the last the part of the
00170     // filename).  If this is a TaQL table, returns "TaQL Table".
00171     String getName();
00172 
00173     // Returns the driver parameters for this table.
00174     DriverParams* getDriverParams();
00175 
00176     // Returns the fields for this table.
00177     vector<TBField*>* getFields();
00178     
00179     // Returns the field at the given index, or NULL if there is none.
00180     TBField* field(int i);
00181     
00182     // Returns the field with the given name, or NULL if there is none.
00183     TBField* field(String name);
00184 
00185     // Returns the table keywords.
00186     vector<TBKeyword*>* getTableKeywords();
00187 
00188     // Returns the keyword at the given index, or NULL if there is none.
00189     TBKeyword* keyword(int i);
00190     
00191     // Returns the keyword with the given name, or NULL if there is none.
00192     TBKeyword* keyword(String name);
00193     
00194     // Indicates if the table is ready for access or not.
00195     bool isReady();
00196 
00197     // Returns true if the underlying table allows for insertion of rows,
00198     // false otherwise.
00199     bool canInsertRows();
00200 
00201     // Returns true if the underlying table allows for deletion of rows,
00202     // false otherwise.
00203     bool canDeleteRows();
00204 
00205     // Returns the total number of rows in the table.
00206     int getTotalRows();
00207 
00208     // Returns the number of rows loaded into the table.
00209     int getLoadedRows();
00210     
00211     // Returns the number of selected rows.  Note: this may be different than
00212     // the number of loaded rows.  For example, if you load rows [0 - 1000] but
00213     // the table has only 20 rows, getSelectedRows() would return 1000 while
00214     // getLoadedRows() would return 20.
00215     int getSelectedRows();
00216     
00217     // Returns the number of pages in the table.
00218     int getNumPages();
00219 
00220     // Return the currently loaded page.
00221     int getPage();
00222 
00223     // Returns the first loaded row in the table.
00224     int getRowIndex();
00225     
00226     // Returns the number of columns in the table.
00227     int getNumFields();
00228     
00229     // Sets whether the table should print debug information or not.
00230     void setPrintDebug(bool pdb);
00231     
00232     // Returns the data at the given indices, or NULL if they are invalid.
00233     TBData* dataAt(unsigned int row, unsigned int col);
00234 
00235     // Returns the shape of the array for the given field, or an empty list
00236     // if the field is not an array.
00237     vector<int> dataDimensionsAt(unsigned int col);
00238     
00239     // Returns whether all the columns in this table are editable or not.
00240     bool isEditable();
00241     
00242     // Returns whether any of the columns in this table are editable or not.
00243     bool isAnyEditable();
00244     
00245     // Returns whether the column at the given index is editable or not.
00246     bool isEditable(int index);
00247     
00248     // Returns whether this table is constructed from a TaQL query or not.
00249     bool isTaQL();
00250     
00251     // See TBTableDriver::canRead().
00252     bool canRead();
00253     
00254     // See TBTableDriver::canWrite().
00255     bool canWrite();
00256     
00257     // See TBTableDriver::tryWriteLock().
00258     bool tryWriteLock();
00259 
00260     // See TBTableDriver::releaseWriteLock().
00261     bool releaseWriteLock();
00262     
00263     
00264     // Loads the given rows into the table and returns a Result indicating the
00265     // success of the loading.  The rows loaded will be from start to start +
00266     // number (inclusive).  If full is true, array data will be included;
00267     // otherwise only the shapes of the array will be included.  If columns
00268     // is a non-NULL, non-empty list, then only those fields will be loaded
00269     // into the table.  If parsedata is true, the table data will be loaded;
00270     // otherwise only the meta-data will be loaded.  If a ProgressHelper is
00271     // provided, the label and progress meter will be periodically updated
00272     // as progress is made.
00273     Result loadRows(int start = 0,
00274                     int number = TBConstants::DEFAULT_SELECT_NUM,
00275                     bool full = false, vector<String>* columns = NULL,
00276                     bool parsedata = true,
00277                     ProgressHelper* progressPanel = NULL);
00278 
00279     // Returns the column headers (field names) for this table.
00280     vector<String> getColumnHeaders();
00281 
00282     // Returns the row headers (row numbers) for the loaded rows in this table.
00283     vector<String> getRowHeaders();
00284 
00285     // Loads and returns the array at the given indices.
00286     TBArrayData* loadArray(unsigned int row, unsigned int col);
00287     
00288     // Release the array at the given indices and returns whether the release
00289     // succeeded.
00290     bool releaseArray(unsigned int row, unsigned int col);
00291 
00292     // Edits the data at the given indices to have the new value and returns
00293     // whether the operation succeeded.
00294     Result editData(unsigned int row, unsigned int col, TBData* newVal);
00295     
00296     // Updates the table on disk with the new value at the given coordinates
00297     // in the array at the given row and column.  oneDim must be true for a
00298     // one-dimensional array, false otherwise.  Returns a result indicating
00299     // the success of the editing.    
00300     Result editArrayData(unsigned int row, unsigned int col,
00301                          vector<int> coords, TBData* newVal, bool oneDim);
00302 
00303     // Exports the entirety of this table to VOTable XML format to the given
00304     // file.  If a ProgressHelper is provided, progress information is updated
00305     // periodically.
00306     void exportVOTable(String file, ProgressHelper* progressPanel = NULL);
00307 
00308     // Returns data used for plotting, using the given plot parameters and row
00309     // information.  If a ProgressHelper is provided, progress information is
00310     // updated periodically.
00311     TBPlotData* plotRows(PlotParams& x, PlotParams& y, int rowFrom, int rowTo,
00312                        int rowInterval, TBFilterRuleSequence* rule = NULL,
00313                        ProgressHelper* ph = NULL);
00314 
00315     // Returns data used for plotting (an 1-D array and its indices),
00316     // using the given plot parameters, slice axis, and row number.
00317     // If a ProgressHelperis provided, progress information is updated 
00318     // periodically.
00319     TBPlotData* plotIndices(PlotParams& dp, int axis, bool x, int row,
00320                        TBFilterRuleSequence* rule = NULL,
00321                        ProgressHelper* ph = NULL);
00322 
00323     // Returns the total number of rows for the table at the given location, or
00324     // -1 for an invalid location or other problem.  TBTable caches the total
00325     // rows of all subtables (i.e., tables pointed to in the table keywords).
00326     int totalRowsOf(String location);
00327 
00328     // Returns a tooltip for the field at index i.
00329     String fieldToolTip(int i);
00330 
00331     // Returns a tooltip for this table.
00332     String tableToolTip();
00333 
00334     // Inserts the given number of rows at the end of the table.  Returns a
00335     // Result indicating the success of the operation.
00336     Result insertRows(int n);
00337 
00338     // Deletes the given rows from the table.  Each element in the vector
00339     // should be a row number.  Returns a Result indicating the success of the
00340     // operation.
00341     Result deleteRows(vector<int> r);
00342 
00343 private:
00344     // Driver parameters.
00345     DriverParams* dParams;
00346 
00347     // Location of the table.
00348     String location;
00349     
00350     // Indicates whether the table is ready for access or not.
00351     bool ready;
00352     
00353     // Indicates whether this table allows for the insertion of rows or not.
00354     bool insertRow;
00355     
00356     // Indicates whether this table allows for the deletion of rows or not.
00357     bool removeRow;
00358     
00359     // Holds the table data.    
00360     vector< vector<TBData*>* > data;
00361     
00362     // The total number of rows in the table.
00363     int totalRows;
00364     
00365     // The number of rows currently loaded in the table.
00366     int loadedRows;
00367     
00368     // The last number of rows selected for loading during the last load.
00369     int selectedRows;
00370     
00371     // The index of the first row loaded in this table.
00372     int rowIndex;
00373 
00374     // The table fields.
00375     vector<TBField*> fields;
00376     
00377     // The table keywords.
00378     vector<TBKeyword*> keywords;
00379     
00380     // Cache of total number of rows for subtables.
00381     vector<int> subtableRows;
00382     
00383     // Indicates whether this table is writable or not.
00384     vector<bool> writable;
00385     
00386     // Indates whether this table was constructed from a TaQL command or not.
00387     bool taql;
00388 
00389     // Table driver.
00390     TBTableDriver* driver;
00391 
00392     
00393     // Helper method for plotRows().
00394     double getDouble(TBData* d, vector<int>* slice, bool complex, bool amp);
00395     
00396     // Helper method for plotRows().
00397     void filter(TBPlotData* data, TBTable& table, TBFilterRuleSequence* rules,
00398                 int rowFrom, int rowTo, int rowInterval);
00399 };
00400 
00401 }
00402 
00403 #endif /* TBTABLE_H_ */