casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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 21168 2012-01-04 08:11:03Z gervandiepen $
00027 
00028 #ifndef TABLES_EXPRFUNCNODE_H
00029 #define TABLES_EXPRFUNCNODE_H
00030 
00031 //# Includes
00032 #include <tables/Tables/ExprNodeRep.h>
00033 #include <casa/Quanta/MVAngle.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 class TableExprNodeSet;
00039 
00040 
00041 // <summary>
00042 // Class representing a function in table select expression
00043 // </summary>
00044 
00045 // <use visibility=local>
00046 
00047 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
00048 // </reviewed>
00049 // <prerequisite>
00050 //# Classes you should understand before using this one.
00051 //   <li> <linkto class=TableExprNodeMulti>TableExprNodeMulti</linkto>
00052 // </prerequisite>
00053 
00054 // <synopsis> 
00055 // This class represents a function in a table select tree.
00056 // The <src>rownumber</src> function is represented by class
00057 // <linkto class=TableExprNodeRownr>TableExprNodeRownr</linkto>.
00058 // The <src>rowid</src> function is represented by class
00059 // <linkto class=TableExprNodeRowid>TableExprNodeRowid</linkto>.
00060 // The <src>rand</src> function is represented by class
00061 // <linkto class=TableExprNodeRandom>TableExprNodeRandom</linkto>.
00062 // <p>
00063 // When one wants to add a function to the table selection grammar,
00064 // the following has to be done:
00065 // <ul>
00066 //  <li> Add the function to the enum below.
00067 //  <li> Implement the function in the get functions in ExprFuncNode(Array).cc.
00068 //  <li> Implement the function in the checkOperands in ExprFuncNode.cc.
00069 //  <li> Declare and define the function in ExprNode.h (for C++ binding).
00070 //  <li> Add the function to findFunc in TableParse.cc (for TaQL).
00071 // </ul>
00072 // </synopsis> 
00073 
00074 
00075 class TableExprFuncNode : public TableExprNodeMulti
00076 {
00077 public:
00078     //# Define the function types.
00079     enum FunctionType {
00080         piFUNC,           //# 0
00081         eFUNC,            //# 1
00082         cFUNC,            //# 2
00083             // for Int, or Double or Complex returning Bool
00084             // (2 is with default tolerance)
00085         near2FUNC,        //# 3
00086         near3FUNC,        //# 4
00087         nearabs2FUNC,     //# 5
00088         nearabs3FUNC,     //# 6
00089             // for Int, Double or DComplex returning Double or Complex
00090         sinFUNC,          //# 7
00091         sinhFUNC,         //# 8
00092         cosFUNC,          //# 9
00093         coshFUNC,         //# 10
00094         expFUNC,          //# 11
00095         logFUNC,          //# 12
00096         log10FUNC,        //# 13
00097         sqrtFUNC,         //# 14
00098         powFUNC,          //# 15
00099         conjFUNC,         //# 16
00100             // for Int, Double or DComplex returning Int, Double or Complex
00101         squareFUNC,       //# 17
00102         cubeFUNC,         //# 18
00103         minFUNC,          //# 19
00104         maxFUNC,          //# 20
00105             // for Int, Double or DComplex returning Int or Double
00106         normFUNC,         //# 21
00107         absFUNC,          //# 22
00108         argFUNC,          //# 23
00109             // for Int, Double or DComplex returning Double
00110         realFUNC,         //# 24
00111         imagFUNC,         //# 25
00112             // for Int or Double returning Int (using floor)
00113         intFUNC,          //# 26
00114             // for Int or Double returning Double
00115         asinFUNC,         //# 27
00116         acosFUNC,         //# 28
00117         atanFUNC,         //# 29
00118         atan2FUNC,        //# 30
00119         tanFUNC,          //# 31
00120         tanhFUNC,         //# 32
00121             // for Int or Double returning Int or Double
00122         signFUNC,         //# 33
00123         roundFUNC,        //# 34
00124         floorFUNC,        //# 35
00125         ceilFUNC,         //# 36
00126         fmodFUNC,         //# 37
00127             // for Int, Double or DComplex returning DComplex
00128         complexFUNC,      //# 38
00129             // for Int, Double or Complex array returning the same
00130         arrsumFUNC,       //# 39
00131         arrsumsFUNC,      //# 40
00132         arrproductFUNC,   //# 41
00133         arrproductsFUNC,  //# 42
00134         arrsumsqrFUNC,    //# 43
00135         arrsumsqrsFUNC,   //# 44
00136             // for Int or Double array returning Int or Double
00137         arrminFUNC,       //# 45
00138         arrminsFUNC,      //# 46
00139         runminFUNC,       //# 47
00140         boxminFUNC,       //# 48
00141         arrmaxFUNC,       //# 49
00142         arrmaxsFUNC,      //# 50
00143         runmaxFUNC,       //# 51
00144         boxmaxFUNC,       //# 52
00145             // for Int or Double array returning Double
00146         arrmeanFUNC,      //# 53
00147         arrmeansFUNC,     //# 54
00148         runmeanFUNC,      //# 55
00149         boxmeanFUNC,      //# 56
00150         arrvarianceFUNC,  //# 57
00151         arrvariancesFUNC, //# 58
00152         runvarianceFUNC,  //# 59
00153         boxvarianceFUNC,  //# 60
00154         arrstddevFUNC,    //# 61
00155         arrstddevsFUNC,   //# 62
00156         runstddevFUNC,    //# 63
00157         boxstddevFUNC,    //# 64
00158         arravdevFUNC,     //# 65
00159         arravdevsFUNC,    //# 66
00160         runavdevFUNC,     //# 67
00161         boxavdevFUNC,     //# 68
00162         arrrmsFUNC,       //# 69
00163         arrrmssFUNC,      //# 70
00164         runrmsFUNC,       //# 71
00165         boxrmsFUNC,       //# 72
00166         arrmedianFUNC,    //# 73
00167         arrmediansFUNC,   //# 74
00168         runmedianFUNC,    //# 75
00169         boxmedianFUNC,    //# 76
00170         arrfractileFUNC,  //# 77
00171         arrfractilesFUNC, //# 78
00172             // for Bool array returning Bool
00173         anyFUNC,          //# 79
00174         anysFUNC,         //# 80
00175         runanyFUNC,       //# 81
00176         boxanyFUNC,       //# 82
00177         allFUNC,          //# 83
00178         allsFUNC,         //# 84
00179         runallFUNC,       //# 85
00180         boxallFUNC,       //# 86
00181             // for Bool array returning Int scalar
00182         ntrueFUNC,        //# 87
00183         ntruesFUNC,       //# 88
00184         gnfalseFUNC,      //# 89
00185         nfalseFUNC,       //# 90
00186         nfalsesFUNC,      //# 91
00187             // for any type returning array of that type
00188         arrayFUNC,        //# 92
00189         transposeFUNC,    //# 93
00190             // for Int, Double or DComplex array returning Bool
00191         isnanFUNC,        //# 94
00192         isinfFUNC,        //# 95
00193         isfiniteFUNC,     //# 96
00194             // for any array returning Bool scalar
00195         isdefFUNC,        //# 97
00196             // for any array returning Int scalar
00197         ndimFUNC,         //# 98
00198         nelemFUNC,        //# 99
00199             // for any array returning Int array
00200         shapeFUNC,        //# 100
00201             // for String
00202         strlengthFUNC,    //# 101          returning Int
00203         upcaseFUNC,       //# 102          returning String
00204         downcaseFUNC,     //# 103          returning String
00205         trimFUNC,         //# 104          returning String
00206         ltrimFUNC,        //# 105          returning String
00207         rtrimFUNC,        //# 106          returning String
00208         substrFUNC,       //# 107          returning String
00209         replaceFUNC,      //# 108          returning String
00210         regexFUNC,        //# 109          returning TaqlRegex
00211         patternFUNC,      //# 110          returning TaqlRegex
00212         sqlpatternFUNC,   //# 111          returning TaqlRegex
00213             // for Date
00214         datetimeFUNC,     //# 112          returning Date
00215         mjdtodateFUNC,    //# 113          returning Date
00216         mjdFUNC,          //# 114          returning Double
00217         dateFUNC,         //# 115          returning Date
00218         timeFUNC,         //# 116          returning Double (in radians)
00219         yearFUNC,         //# 117          returning Int
00220         monthFUNC,        //# 118          returning Int
00221         dayFUNC,          //# 119          returning Int
00222         cmonthFUNC,       //# 120          returning String
00223         weekdayFUNC,      //# 121          returning Int
00224         cdowFUNC,         //# 122          returning String
00225         weekFUNC,         //# 123          returning Int
00226         ctodFUNC,         //# 124          returning String
00227         cdateFUNC,        //# 125          returning String
00228         ctimeFUNC,        //# 126          returning String
00229             // return angles as hms strings
00230         hmsFUNC,          //# 127
00231             // return angles as dms strings
00232         dmsFUNC,          //# 128
00233             // return angles as hms/dms strings
00234         hdmsFUNC,         //# 129
00235             // special function returning a random Double number
00236         randFUNC,         //# 130
00237             // special function returning Int row number
00238         rownrFUNC,        //# 131
00239             // special function returning Int row id (meant for GIVING)
00240         rowidFUNC,        //# 132
00241             // special function resembling if statement
00242         iifFUNC,          //# 133
00243             // angular distance returning radians
00244         angdistFUNC,      //# 134
00245         angdistxFUNC,     //# 135
00246             // other functions, implemented in derived class
00247         conesFUNC,        //# 136
00248         cones3FUNC,       //# 137
00249         anyconeFUNC,      //# 138
00250         anycone3FUNC,     //# 139
00251         findconeFUNC,     //# 140
00252         findcone3FUNC,    //# 141
00253         NRFUNC      //# should be last
00254         };
00255 
00256     // Constructor
00257     TableExprFuncNode (FunctionType, NodeDataType, ValueType,
00258                        const TableExprNodeSet& source);
00259 
00260     // Destructor
00261     ~TableExprFuncNode ();
00262 
00263     // Does the node result in a single value (for e.g. GROUPBY)?
00264     // This is the case for reduction functions and constant functions.
00265     virtual Bool isSingleValue() const;
00266 
00267     // 'get' Functions to get the desired result of a function
00268     // <group>
00269     Bool      getBool     (const TableExprId& id);
00270     Int64     getInt      (const TableExprId& id);
00271     Double    getDouble   (const TableExprId& id);
00272     DComplex  getDComplex (const TableExprId& id);
00273     String    getString   (const TableExprId& id);
00274     TaqlRegex getRegex    (const TableExprId& id);
00275     MVTime    getDate     (const TableExprId& id);
00276     // </group>
00277 
00278     // Check the data and value types of the operands.
00279     // It sets the exptected data and value types of the operands.
00280     // Set the value type of the function result and returns
00281     // the data type of the function result.
00282     static NodeDataType checkOperands (Block<Int>& dtypeOper,
00283                                        ValueType& resVT,
00284                                        Block<Int>& vtypeOper,
00285                                        FunctionType,
00286                                        PtrBlock<TableExprNodeRep*>&);
00287 
00288     // Fill the result unit in the node.
00289     // Adapt the children nodes if their units need to be converted.
00290     // It returns a possible scale factor in case result unit is SI (for sqrt).
00291     static Double fillUnits (TableExprNodeRep* node,
00292                              PtrBlock<TableExprNodeRep*>& nodes,
00293                              FunctionType func);
00294 
00295     // Link the children to the node and convert the children
00296     // to constants if possible. Also convert the node to
00297     // constant if possible.
00298     static TableExprNodeRep* fillNode (TableExprFuncNode* thisNode,
00299                                        PtrBlock<TableExprNodeRep*>& nodes,
00300                                        const Block<Int>& dtypeOper);
00301 
00302     // Link the children to the node and convert the children
00303     // to constants if possible.
00304     static void fillChildNodes (TableExprFuncNode* thisNode,
00305                                 PtrBlock<TableExprNodeRep*>& nodes,
00306                                 const Block<Int>& dtypeOper);
00307 
00308     // Set unit scale factor (needed for sqrt).
00309     void setScale (Double scale)
00310         { scale_p = scale; }
00311 
00312     // Get possible unit scale factor (needed for sqrt).
00313     Double getScale() const
00314         { return scale_p; }
00315 
00316     // Some functions to be used by TableExprNodeFuncArray.
00317     // <group>
00318     const PtrBlock<TableExprNodeRep*>& operands() const
00319         { return operands_p; }
00320     PtrBlock<TableExprNodeRep*>& rwOperands()
00321         { return operands_p; }
00322     FunctionType funcType() const
00323         { return funcType_p; }
00324     NodeDataType argDataType() const
00325         { return argDataType_p; }
00326     // </group>
00327 
00328     // Convert the date and/or time to a string.
00329     // <group>
00330     static String stringDT (const MVTime& dt, Int prec, MVTime::formatTypes);
00331     static String stringDateTime (const MVTime& dt, Int prec);
00332     static String stringDate (const MVTime& dt);
00333     static String stringTime (const MVTime& dt, Int prec);
00334     // </group>
00335 
00336     // Convert angle to a string (hms or dms).
00337     // <group>
00338     static String stringAngle (double val, Int prec,
00339                                MVAngle::formatTypes type);
00340     static String stringHMS (double val, Int prec);
00341     static String stringDMS (double val, Int prec);
00342     // </group>
00343 
00344     // Get the angular distance between two positions on a sphere.
00345     static double angdist (double ra1, double dec1, double ra2, double dec2)
00346       { return acos (sin(dec1)*sin(dec2) + cos(dec1)*cos(dec2)*cos(ra1-ra2)); }
00347 
00348 private:
00349     // Try if the function gives a constant result.
00350     // If so, set the expression type to Constant.
00351     void tryToConst();
00352 
00353     // Make the units of nodes from <src>starg</src> till <src>endarg</src>
00354     // equal. Return the unit found.
00355     static const Unit& makeEqualUnits (PtrBlock<TableExprNodeRep*>& nodes,
00356                                        uInt starg, uInt endarg);
00357 
00358 
00359     FunctionType funcType_p;        // which function
00360     NodeDataType argDataType_p;     // common argument data type
00361     Double       scale_p;           // possible scaling for unit conversion
00362                                     // (needed for sqrt)
00363 };
00364 
00365 
00366 } //# NAMESPACE CASA - END
00367 
00368 #endif