ExprFuncNode.h

Go to the documentation of this file.
00001 //# ExprFuncNode.h: Class representing a function in table select expression
00002 //# Copyright (C) 1994,1995,1996,1997,1998,2000,2001,2003
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: ExprFuncNode.h 21277 2012-10-31 16:07:31Z gervandiepen $
00027 
00028 #ifndef TABLES_EXPRFUNCNODE_H
00029 #define TABLES_EXPRFUNCNODE_H
00030 
00031 //# Includes
00032 #include <casacore/casa/aips.h>
00033 #include <casacore/tables/TaQL/ExprNodeRep.h>
00034 #include <casacore/casa/Quanta/MVAngle.h>
00035 
00036 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00037 
00038 //# Forward Declarations
00039 class TableExprNodeSet;
00040 
00041 
00042 // <summary>
00043 // Class representing a function in table select expression
00044 // </summary>
00045 
00046 // <use visibility=local>
00047 
00048 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00049 // </reviewed>
00050 // <prerequisite>
00051 //# Classes you should understand before using this one.
00052 //   <li> <linkto class=TableExprNodeMulti>TableExprNodeMulti</linkto>
00053 // </prerequisite>
00054 
00055 // <synopsis> 
00056 // This class represents a function in a table select tree.
00057 // The <src>rownumber</src> function is represented by class
00058 // <linkto class=TableExprNodeRownr>TableExprNodeRownr</linkto>.
00059 // The <src>rowid</src> function is represented by class
00060 // <linkto class=TableExprNodeRowid>TableExprNodeRowid</linkto>.
00061 // The <src>rand</src> function is represented by class
00062 // <linkto class=TableExprNodeRandom>TableExprNodeRandom</linkto>.
00063 // <p>
00064 // When one wants to add a function to the table selection grammar,
00065 // the following has to be done:
00066 // <ul>
00067 //  <li> Add the function to the enum below.
00068 //  <li> Implement the function in the get functions in ExprFuncNode(Array).cc.
00069 //  <li> Implement the function in the checkOperands in ExprFuncNode.cc.
00070 //  <li> Declare and define the function in ExprNode.h (for C++ binding).
00071 //  <li> Add the function to findFunc in TableParse.cc (for TaQL).
00072 // </ul>
00073 // </synopsis> 
00074 
00075 
00076 class TableExprFuncNode : public TableExprNodeMulti
00077 {
00078 public:
00079     //# Define the function types.
00080     enum FunctionType {
00081         piFUNC,           //# 0
00082         eFUNC,            //# 1
00083         cFUNC,            //# 2
00084             // for Int, or Double or Complex returning Bool
00085             // (2 is with default tolerance)
00086         near2FUNC,        //# 3
00087         near3FUNC,        //# 4
00088         nearabs2FUNC,     //# 5
00089         nearabs3FUNC,     //# 6
00090             // for Int, Double or DComplex returning Double or Complex
00091         sinFUNC,          //# 7
00092         sinhFUNC,         //# 8
00093         cosFUNC,          //# 9
00094         coshFUNC,         //# 10
00095         expFUNC,          //# 11
00096         logFUNC,          //# 12
00097         log10FUNC,        //# 13
00098         sqrtFUNC,         //# 14
00099         powFUNC,          //# 15
00100         conjFUNC,         //# 16
00101             // for Int, Double or DComplex returning Int, Double or Complex
00102         squareFUNC,       //# 17
00103         cubeFUNC,         //# 18
00104         minFUNC,          //# 19
00105         maxFUNC,          //# 20
00106             // for Int, Double or DComplex returning Int or Double
00107         normFUNC,         //# 21
00108         absFUNC,          //# 22
00109         argFUNC,          //# 23
00110             // for Int, Double, DComplex, Bool or String returning Double
00111         realFUNC,         //# 24
00112         imagFUNC,         //# 25
00113             // for Int, Double, Bool or String returning Int (using floor)
00114         intFUNC,          //# 26
00115             // for Int or Double returning Double
00116         asinFUNC,         //# 27
00117         acosFUNC,         //# 28
00118         atanFUNC,         //# 29
00119         atan2FUNC,        //# 30
00120         tanFUNC,          //# 31
00121         tanhFUNC,         //# 32
00122             // for Int or Double returning Int or Double
00123         signFUNC,         //# 33
00124         roundFUNC,        //# 34
00125         floorFUNC,        //# 35
00126         ceilFUNC,         //# 36
00127         fmodFUNC,         //# 37
00128             // for Int, Double or DComplex returning DComplex
00129         complexFUNC,      //# 38
00130             // for Int, Double or Complex array returning the same
00131         arrsumFUNC,       //# 39
00132         arrsumsFUNC,      //# 40
00133         arrproductFUNC,   //# 41
00134         arrproductsFUNC,  //# 42
00135         arrsumsqrFUNC,    //# 43
00136         arrsumsqrsFUNC,   //# 44
00137             // for Int or Double array returning Int or Double
00138         arrminFUNC,       //# 45
00139         arrminsFUNC,      //# 46
00140         runminFUNC,       //# 47
00141         boxminFUNC,       //# 48
00142         arrmaxFUNC,       //# 49
00143         arrmaxsFUNC,      //# 50
00144         runmaxFUNC,       //# 51
00145         boxmaxFUNC,       //# 52
00146             // for Int or Double array returning Double
00147         arrmeanFUNC,      //# 53
00148         arrmeansFUNC,     //# 54
00149         runmeanFUNC,      //# 55
00150         boxmeanFUNC,      //# 56
00151         arrvarianceFUNC,  //# 57
00152         arrvariancesFUNC, //# 58
00153         runvarianceFUNC,  //# 59
00154         boxvarianceFUNC,  //# 60
00155         arrstddevFUNC,    //# 61
00156         arrstddevsFUNC,   //# 62
00157         runstddevFUNC,    //# 63
00158         boxstddevFUNC,    //# 64
00159         arravdevFUNC,     //# 65
00160         arravdevsFUNC,    //# 66
00161         runavdevFUNC,     //# 67
00162         boxavdevFUNC,     //# 68
00163         arrrmsFUNC,       //# 69
00164         arrrmssFUNC,      //# 70
00165         runrmsFUNC,       //# 71
00166         boxrmsFUNC,       //# 72
00167         arrmedianFUNC,    //# 73
00168         arrmediansFUNC,   //# 74
00169         runmedianFUNC,    //# 75
00170         boxmedianFUNC,    //# 76
00171         arrfractileFUNC,  //# 77
00172         arrfractilesFUNC, //# 78
00173             // for Bool array returning Bool
00174         anyFUNC,          //# 79
00175         anysFUNC,         //# 80
00176         runanyFUNC,       //# 81
00177         boxanyFUNC,       //# 82
00178         allFUNC,          //# 83
00179         allsFUNC,         //# 84
00180         runallFUNC,       //# 85
00181         boxallFUNC,       //# 86
00182             // for Bool array returning Int scalar
00183         ntrueFUNC,        //# 87
00184         ntruesFUNC,       //# 88
00185         nfalseFUNC,       //# 89
00186         nfalsesFUNC,      //# 90
00187             // for any type returning array of that type
00188         arrayFUNC,        //# 91
00189         transposeFUNC,    //# 92
00190         resizeFUNC,       //# 93
00191         diagonalFUNC,     //# 94
00192             // for Int, Double or DComplex array returning Bool
00193         isnanFUNC,        //# 95
00194         isinfFUNC,        //# 96
00195         isfiniteFUNC,     //# 97
00196             // for any array returning Bool scalar
00197         isdefFUNC,        //# 98
00198         isnullFUNC,       //# 99
00199         iscolFUNC,        //# 100
00200         iskeyFUNC,        //# 101
00201             // for any array returning Int scalar
00202         ndimFUNC,         //# 102
00203         nelemFUNC,        //# 103
00204             // for any array returning Int array
00205         shapeFUNC,        //# 104
00206             // for String
00207         strlengthFUNC,    //# 105          returning Int
00208         upcaseFUNC,       //# 106          returning String
00209         downcaseFUNC,     //# 107          returning String
00210         capitalizeFUNC,   //# 108          returning String
00211         trimFUNC,         //# 109          returning String
00212         ltrimFUNC,        //# 110          returning String
00213         rtrimFUNC,        //# 111          returning String
00214         substrFUNC,       //# 112          returning String
00215         replaceFUNC,      //# 113          returning String
00216         regexFUNC,        //# 114          returning TaqlRegex
00217         patternFUNC,      //# 115          returning TaqlRegex
00218         sqlpatternFUNC,   //# 116          returning TaqlRegex
00219             // for Date
00220         datetimeFUNC,     //# 117          returning Date
00221         mjdtodateFUNC,    //# 118          returning Date
00222         mjdFUNC,          //# 119          returning Double
00223         dateFUNC,         //# 120          returning Date
00224         timeFUNC,         //# 121          returning Double (in radians)
00225         yearFUNC,         //# 122          returning Int
00226         monthFUNC,        //# 123          returning Int
00227         dayFUNC,          //# 124          returning Int
00228         cmonthFUNC,       //# 125          returning String
00229         weekdayFUNC,      //# 126          returning Int
00230         cdowFUNC,         //# 127          returning String
00231         weekFUNC,         //# 128          returning Int
00232         ctodFUNC,         //# 129          returning String
00233         cdateFUNC,        //# 130          returning String
00234         ctimeFUNC,        //# 131          returning String
00235             // return values as strings
00236         stringFUNC,       //# 132
00237             // return angles as hms strings
00238         hmsFUNC,          //# 133
00239             // return angles as dms strings
00240         dmsFUNC,          //# 134
00241             // return angles as hms/dms strings
00242         hdmsFUNC,         //# 135
00243             // special function returning a random Double number
00244         randFUNC,         //# 136
00245             // special function returning Int row number
00246         rownrFUNC,        //# 137
00247             // special function returning Int row id (meant for GIVING)
00248         rowidFUNC,        //# 138
00249             // special function resembling if statement
00250         iifFUNC,          //# 139
00251             // angular distance returning radians
00252         angdistFUNC,      //# 140
00253         angdistxFUNC,     //# 141
00254             // cone search functions, implemented in derived class
00255         conesFUNC,        //# 142
00256         cones3FUNC,       //# 143
00257         anyconeFUNC,      //# 144
00258         anycone3FUNC,     //# 145
00259         findconeFUNC,     //# 146
00260         findcone3FUNC,    //# 147
00261             // for Int, Double, Complex or String returning Bool
00262         boolFUNC,         //# 148
00263             // masked array functions
00264         nullarrayFUNC,    //# 149
00265         marrayFUNC,       //# 150
00266         arrdataFUNC,      //# 151
00267         arrmaskFUNC,      //# 152
00268         negatemaskFUNC,   //# 153
00269         replmaskedFUNC,   //# 154
00270         replunmaskedFUNC, //# 155
00271         arrflatFUNC,      //# 156
00272         //# AGGREGATE functions must be the last ones.
00273         FirstAggrFunc,    //# 157
00274         countallFUNC = FirstAggrFunc,
00275         gcountFUNC,
00276         gfirstFUNC,
00277         glastFUNC,
00278         //# Grouping doing aggregation on the fly; reducing to a scalar per group
00279         gminFUNC,         //# 161
00280         gmaxFUNC,
00281         gsumFUNC,
00282         gproductFUNC,
00283         gsumsqrFUNC,
00284         gmeanFUNC,
00285         gvarianceFUNC,
00286         gstddevFUNC,
00287         grmsFUNC,
00288         ganyFUNC,
00289         gallFUNC,
00290         gntrueFUNC,
00291         gnfalseFUNC,
00292         //# Grouping doing aggregation on the fly; reducing to an array per group
00293         FirstAggrArrayFunc,//# 174
00294         gminsFUNC = FirstAggrArrayFunc,
00295         gmaxsFUNC,
00296         gsumsFUNC,
00297         gproductsFUNC,
00298         gsumsqrsFUNC,
00299         gmeansFUNC,
00300         gvariancesFUNC,
00301         gstddevsFUNC,
00302         grmssFUNC,
00303         ganysFUNC,
00304         gallsFUNC,
00305         gntruesFUNC,
00306         gnfalsesFUNC,
00307         LastAggrArrayFunc,//# 187
00308         ghistFUNC = LastAggrArrayFunc,
00309         //# Grouping requiring aggregation of rows when getting result
00310         gaggrFUNC,        //# 188
00311         growidFUNC,
00312         gmedianFUNC,
00313         gfractileFUNC,
00314         gexpridFUNC,      //# special function (can be inserted by TableParse)
00315         NRFUNC            //# 193  should be last
00316         };
00317 
00318     // Constructor
00319     TableExprFuncNode (FunctionType, NodeDataType, ValueType,
00320                        const TableExprNodeSet& source,
00321                        const Table& = Table());
00322 
00323     // Destructor
00324     ~TableExprFuncNode ();
00325 
00326     // 'get' Functions to get the desired result of a function
00327     // <group>
00328     Bool      getBool     (const TableExprId& id);
00329     Int64     getInt      (const TableExprId& id);
00330     Double    getDouble   (const TableExprId& id);
00331     DComplex  getDComplex (const TableExprId& id);
00332     String    getString   (const TableExprId& id);
00333     TaqlRegex getRegex    (const TableExprId& id);
00334     MVTime    getDate     (const TableExprId& id);
00335     // </group>
00336 
00337     // Check the data and value types of the operands.
00338     // It sets the exptected data and value types of the operands.
00339     // Set the value type of the function result and returns
00340     // the data type of the function result.
00341     static NodeDataType checkOperands (Block<Int>& dtypeOper,
00342                                        ValueType& resVT,
00343                                        Block<Int>& vtypeOper,
00344                                        FunctionType,
00345                                        PtrBlock<TableExprNodeRep*>&);
00346 
00347     // Fill the result unit in the node.
00348     // Adapt the children nodes if their units need to be converted.
00349     // It returns a possible scale factor in case result unit is SI (for sqrt).
00350     static Double fillUnits (TableExprNodeRep* node,
00351                              PtrBlock<TableExprNodeRep*>& nodes,
00352                              FunctionType func);
00353 
00354     // Link the children to the node and convert the children
00355     // to constants if possible. Also convert the node to
00356     // constant if possible.
00357     static TableExprNodeRep* fillNode (TableExprFuncNode* thisNode,
00358                                        PtrBlock<TableExprNodeRep*>& nodes,
00359                                        const Block<Int>& dtypeOper);
00360 
00361     // Link the children to the node and convert the children
00362     // to constants if possible.
00363     static void fillChildNodes (TableExprFuncNode* thisNode,
00364                                 PtrBlock<TableExprNodeRep*>& nodes,
00365                                 const Block<Int>& dtypeOper);
00366 
00367     // Set unit scale factor (needed for sqrt).
00368     void setScale (Double scale)
00369         { scale_p = scale; }
00370 
00371     // Get possible unit scale factor (needed for sqrt).
00372     Double getScale() const
00373         { return scale_p; }
00374 
00375     // Some functions to be used by TableExprNodeFuncArray.
00376     // <group>
00377     const PtrBlock<TableExprNodeRep*>& operands() const
00378         { return operands_p; }
00379     PtrBlock<TableExprNodeRep*>& rwOperands()
00380         { return operands_p; }
00381     FunctionType funcType() const
00382         { return funcType_p; }
00383     NodeDataType argDataType() const
00384         { return argDataType_p; }
00385     // </group>
00386 
00387     // Get the possible print format, width, and/or precision.
00388     static void getPrintFormat (String& fmt, Int& width, Int& prec,
00389                                 const PtrBlock<TableExprNodeRep*>& operands,
00390                                 const TableExprId& id);
00391 
00392     // Convert the date and/or time to a string.
00393     // <group>
00394     static String stringDT (const MVTime& dt, Int prec, MVTime::formatTypes);
00395     static String stringDateTime (const MVTime& dt, Int prec);
00396     static String stringDate (const MVTime& dt);
00397     static String stringTime (const MVTime& dt, Int prec);
00398     // </group>
00399 
00400     // Convert a value to a string.
00401     // If <src>fmt</src> is empty, ostringstream is used.
00402     // Otherwise the printf-like format is used.
00403     // If possible, a double value is converted to radians if formatted as angle.
00404     // <group>
00405     static String stringValue (Bool val, const String& fmt, Int width);
00406     static String stringValue (Int64 val, const String& fmt, Int width);
00407     static String stringValue (Double val, const String& fmt,
00408                                Int width, Int prec,
00409                                const std::pair<int,int>& mvFormat,
00410                                const Unit& unit);
00411     static String stringValue (const DComplex& val, const String& fmt,
00412                                Int width, Int prec);
00413     static String stringValue (const String& val, const String& fmt,
00414                                Int width);
00415     static String stringValue (const MVTime& val, const String& fmt,
00416                                Int width,
00417                                const std::pair<int,int>& mvFormat);
00418 
00419     // Convert angle to a string (hms or dms).
00420     // <group>
00421     static String stringAngle (double val, Int prec,
00422                                MVAngle::formatTypes type);
00423     static String stringHMS (double val, Int prec);
00424     static String stringDMS (double val, Int prec);
00425     // </group>
00426 
00427     // Get the MVTime/Angle format and optional precision.
00428     // 0,0 is returned if empty or unknown format.
00429     static std::pair<int,int> getMVFormat (const String& fmt);
00430 
00431     // Get the angular distance between two positions on a sphere.
00432     static double angdist (double ra1, double dec1, double ra2, double dec2)
00433       { return acos (sin(dec1)*sin(dec2) + cos(dec1)*cos(dec2)*cos(ra1-ra2)); }
00434 
00435     // Read a string as an integer, double, complex or bool.
00436     static Int64 string2Int (const String&);
00437     static Double string2Real (const String&);
00438     static DComplex string2Complex (const String&);
00439     static Bool string2Bool (const String&);
00440 
00441 private:
00442     // Try if the function gives a constant result.
00443     // If so, set the expression type to Constant.
00444     void tryToConst();
00445 
00446     // Make the units of nodes from <src>starg</src> till <src>endarg</src>
00447     // equal. Return the unit found.
00448     static const Unit& makeEqualUnits (PtrBlock<TableExprNodeRep*>& nodes,
00449                                        uInt starg, uInt endarg);
00450 
00451     //# Data members.
00452     FunctionType funcType_p;        // which function
00453     NodeDataType argDataType_p;     // common argument data type
00454     Double       scale_p;           // possible scaling for unit conversion
00455                                     // (needed for sqrt)
00456     Table        table_p;           // table (for iscolumn and iskeyword)
00457 };
00458 
00459 
00460 } //# NAMESPACE CASACORE - END
00461 
00462 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1