casacore
Loading...
Searching...
No Matches
TableExprData.h
Go to the documentation of this file.
1//# TableExprData.h: Abstract base class for data object in a TaQL expression
2//# Copyright (C) 2000,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26
27#ifndef TABLES_TABLEEXPRDATA_H
28#define TABLES_TABLEEXPRDATA_H
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Utilities/DataType.h>
33#include <casacore/casa/Arrays/ArrayFwd.h>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37//# Forward Declarations
38class String;
39class IPosition;
40template<class T> class Block;
41
42
43// <summary>
44// Abstract base class for data object in a TaQL expression.
45// </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
50// </reviewed>
51
52// <prerequisite>
53// <li> <linkto class="TableExprNode">TableExprNode</linkto>.
54// </prerequisite>
55
56// <synopsis>
57// The Table Query Language (TaQL) is implemented by means of the
58// <src>TableExprNode</src> classes. It is primarily meant to do
59// selection on tables. However, it is also possible to use it for
60// selection on any other set of data resembling tabular data.
61// <br>An example of such a data set is a set of
62// <linkto class=Record>Record</linkto> objects. TaQL can be used
63// to select some of those records based on the contents of one or more
64// fields in the records. Note that this example is already directly
65// supported by TaQL.
66// <br>Another example is when a user has several equally long vectors
67// with data. The vectors can be seen as fields and TaQL can be used
68// to select entries from the vectors. This example requires that
69// this class TableExprData is used.
70// <p>
71// The <linkto class=TableExprNodeRecordField>TableExprNodeRecordField</linkto>
72// and <linkto class=TableExprId>TableExprId</linkto> classes form
73// the means by which TaQL can deal with any set of data.
74// <br>First the TaQL expression has to be setup. This is done by
75// constructing a <src>TableExprNodeRecordField</src> object for each
76// 'field' to be used in the expression. <src>TableExprNodeRecordField</src>
77// uses a <linkto class=RecordInterface>RecordInterface</linkto> object
78// to make the data type of a field in the data set known and to
79// map a field name to a field index (the index is the sequence number
80// of the field in the record description).
81// <br>When evaluating the expression for each member in the data set,
82// a <src>TableExprData></src> needs to be passed (which is automatically
83// converted to <linkto class=TableExprId>TableExprId</linkto>).
84// So a class needs to be written to access the data in the data set.
85// It needs to be derived from the abstract base class <src>TableExprData</src>
86// defined in this file. An example is given below.
87// <p>
88// It is also possible that the data set contains records and that
89// the selection is based on fields in those records. In such a case
90// the record passed to <src>TableExprNodeRecordField</src> should contain
91// subrecords representing those records. The field index in the various
92// functions as passed as a <src>Block<Int></src> to denote the fields
93// in the subrecords (and possibly subsubrecords, etc..
94// However, normally records won't be used and <src>fieldNrs[0]</src>
95// gives the field index.
96// </synopsis>
97
98// <example>
99// This example shows how a data set consisting of two vectors
100// of scalars can be used.
101// <srcblock>
102// // Write a class derived from TableExprData to handle the vectors.
103// class MyTestClass : public TableExprData
104// {
105// public:
106// // Constructor checks if both vectors have equal length.
107// MyTestClass (const Vector<Int>& fld1, const Vector<String>& fld2)
108// : itsFld1(fld1), itsFld2(fld2), itsEntry(0)
109// { AlwaysAssert (fld1.nelements() == fld2.nelements(), AipsError); }
110// virtual ~MyTestClass()
111// {}
112// void next()
113// { itsEntry++; }
114// // Note that only the get functions for the possible types are needed.
115// // Also note that all numeric types are handled by TaQL as Double.
116// // The exception should never be thrown unless things are screwed up.
117// virtual Double getDouble (const Block<Int>& fieldNrs) const
118// { switch (fieldNrs[0]) {
119// case 0:
120// return itsFld1(itsEntry);
121// default:
122// throw AipsError();
123// }
124// }
125// virtual String getString (const Block<Int>& fieldNrs) const
126// { switch (fieldNrs[0]) {
127// case 1:
128// return itsFld2(itsEntry);
129// default:
130// throw AipsError();
131// }
132// }
133// virtual DataType dataType (const Block<Int>& fieldNrs) const
134// { switch (fieldNrs[0]) {
135// case 0:
136// return TpInt;
137// case 1:
138// return TpString;
139// default:
140// throw AipsError();
141// }
142// }
143// // Make a Record to give to vectors a name.
144// // The order in which the fields are defined determines the fieldnrs
145// // passed to the get functions.
146// static Record makeRecord()
147// { RecordDesc desc;
148// desc.addField ("fld1", TpInt);
149// desc.addField ("fld2", TpString);
150// return Record(desc);
151// }
152// private:
153// Vector<Int> itsFld1;
154// Vector<String> itsFld2;
155// uInt itsEntry;
156// };
157//
158// Vector<uInt> findMatches (const Vector<Int>& fld1,
159// const Vector<String>& fld2)
160// {
161// // Make some expression.
162// // First create a Record to make the names and types known.
163// Record rec(MyTestClass::makeRecord());
164// TableExprNode expr (makeRecordExpr(rec,"fld1") > 10 &&
165// makeRecordExpr(rec,"fld2") != pattern("*xxx*"));
166// // Now evaluate the expression for each entry in the vector.
167// // Make a MyTestClass object to handle the vectors and put it in
168// // a TableExprId object for the TaQL evaluator.
169// // Note that TableExprId holds a pointer to the original MyTestClass
170// // object, so the TaQL evaluator 'sees' the changes we make by
171// // using the its next() function.
172// MyTestClass subj(fld1, fld2);
173// TableExprId eid(subj);
174// // The matching entry numbers are stored in a vector.
175// Vector<uInt> result(fld1.nelements());
176// uInt nr=0;
177// Bool valb;
178// for (uInt i=0; i<fld1.nelements(); i++) {
179// expr.get (eid, valb);
180// if (valb) {
181// result(nr++) = i;
182// }
183// subj.next(); // Next time the next entry must be used
184// }
185// result.resize (nr, True);
186// return result;
187// }
188// </srcBlock>
189// </example>
190
191// <motivation>
192// This class makes it possible that TaQL can be used in a very versatile way.
193// </motivation>
194
195//# <todo asof="1996/03/12">
196//# </todo>
197
198
200{
201public:
202 // Construct it from a row number.
204 {;}
205
206 virtual ~TableExprData();
207
208 // Get the shape of the given field.
209 // Need only be implemented if there are arrays in the data.
210 // The default implementation returns an empty IPosition.
211 virtual IPosition shape (const Block<Int>& fieldNrs) const;
212
213 // Get the data type of the given field.
214 // Note that TpArray types have to be returned for arrays.
215 // If the field is unknown, TpOther should be returned.
216 // It is used for the isdefined function to check if the field
217 // is really defined.
218 virtual DataType dataType (const Block<Int>& fieldNrs) const = 0;
219
220 // Get a scalar in the given type.
221 // This might involve converting for Double and DComplex.
222 // Most default implementations throws an "not possible" exception.
223 // The default <src>getDouble</src> invokes <src>getInt</src>.
224 // The default <src>getDComplex</src> invokes <src>getDouble</src>.
225 // <group>
226 virtual Bool getBool (const Block<Int>& fieldNrs) const;
227 virtual Int64 getInt (const Block<Int>& fieldNrs) const;
228 virtual Double getDouble (const Block<Int>& fieldNrs) const;
229 virtual DComplex getDComplex (const Block<Int>& fieldNrs) const;
230 virtual String getString (const Block<Int>& fieldNrs) const;
231 // </group>
232
233 // Get an array in the given type.
234 // This might involve converting for Double and DComplex.
235 // Most default implementations throws an "not possible" exception.
236 // The default <src>getArrayDComplex</src> invokes
237 // <src>getArrayDouble</src>.
238 // <group>
239 virtual Array<Bool> getArrayBool (const Block<Int>& fieldNrs) const;
240 virtual Array<Int64> getArrayInt (const Block<Int>& fieldNrs) const;
241 virtual Array<Double> getArrayDouble (const Block<Int>& fieldNrs) const;
242 virtual Array<DComplex> getArrayDComplex (const Block<Int>& fieldNrs) const;
243 virtual Array<String> getArrayString (const Block<Int>& fieldNrs) const;
244 // </group>
245};
246
247
248} //# NAMESPACE CASACORE - END
249
250#endif
simple 1-D array
Definition Block.h:198
String: the storage and methods of handling collections of characters.
Definition String.h:223
virtual Int64 getInt(const Block< Int > &fieldNrs) const
virtual Array< Int64 > getArrayInt(const Block< Int > &fieldNrs) const
virtual Bool getBool(const Block< Int > &fieldNrs) const
Get a scalar in the given type.
virtual Array< Bool > getArrayBool(const Block< Int > &fieldNrs) const
Get an array in the given type.
virtual Array< Double > getArrayDouble(const Block< Int > &fieldNrs) const
virtual DComplex getDComplex(const Block< Int > &fieldNrs) const
TableExprData()
Construct it from a row number.
virtual Array< String > getArrayString(const Block< Int > &fieldNrs) const
virtual Array< DComplex > getArrayDComplex(const Block< Int > &fieldNrs) const
virtual IPosition shape(const Block< Int > &fieldNrs) const
Get the shape of the given field.
virtual Double getDouble(const Block< Int > &fieldNrs) const
virtual String getString(const Block< Int > &fieldNrs) const
virtual DataType dataType(const Block< Int > &fieldNrs) const =0
Get the data type of the given field.
this file contains all the compiler specific defines
Definition mainpage.dox:28
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53