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#ifndef TABLES_TABLEEXPRDATA_H
27#define TABLES_TABLEEXPRDATA_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Utilities/DataType.h>
32#include <casacore/casa/Arrays/ArrayFwd.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations
37class String;
38class IPosition;
39template <class T>
40class Block;
41
42// <summary>
43// Abstract base class for data object in a TaQL expression.
44// </summary>
45
46// <use visibility=export>
47
48// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
49// </reviewed>
50
51// <prerequisite>
52// <li> <linkto class="TableExprNode">TableExprNode</linkto>.
53// </prerequisite>
54
55// <synopsis>
56// The Table Query Language (TaQL) is implemented by means of the
57// <src>TableExprNode</src> classes. It is primarily meant to do
58// selection on tables. However, it is also possible to use it for
59// selection on any other set of data resembling tabular data.
60// <br>An example of such a data set is a set of
61// <linkto class=Record>Record</linkto> objects. TaQL can be used
62// to select some of those records based on the contents of one or more
63// fields in the records. Note that this example is already directly
64// supported by TaQL.
65// <br>Another example is when a user has several equally long vectors
66// with data. The vectors can be seen as fields and TaQL can be used
67// to select entries from the vectors. This example requires that
68// this class TableExprData is used.
69// <p>
70// The <linkto class=TableExprNodeRecordField>TableExprNodeRecordField</linkto>
71// and <linkto class=TableExprId>TableExprId</linkto> classes form
72// the means by which TaQL can deal with any set of data.
73// <br>First the TaQL expression has to be setup. This is done by
74// constructing a <src>TableExprNodeRecordField</src> object for each
75// 'field' to be used in the expression. <src>TableExprNodeRecordField</src>
76// uses a <linkto class=RecordInterface>RecordInterface</linkto> object
77// to make the data type of a field in the data set known and to
78// map a field name to a field index (the index is the sequence number
79// of the field in the record description).
80// <br>When evaluating the expression for each member in the data set,
81// a <src>TableExprData></src> needs to be passed (which is automatically
82// converted to <linkto class=TableExprId>TableExprId</linkto>).
83// So a class needs to be written to access the data in the data set.
84// It needs to be derived from the abstract base class <src>TableExprData</src>
85// defined in this file. An example is given below.
86// <p>
87// It is also possible that the data set contains records and that
88// the selection is based on fields in those records. In such a case
89// the record passed to <src>TableExprNodeRecordField</src> should contain
90// subrecords representing those records. The field index in the various
91// functions as passed as a <src>Block<Int></src> to denote the fields
92// in the subrecords (and possibly subsubrecords, etc..
93// However, normally records won't be used and <src>fieldNrs[0]</src>
94// gives the field index.
95// </synopsis>
96
97// <example>
98// This example shows how a data set consisting of two vectors
99// of scalars can be used.
100// <srcblock>
101// // Write a class derived from TableExprData to handle the vectors.
102// class MyTestClass : public TableExprData
103// {
104// public:
105// // Constructor checks if both vectors have equal length.
106// MyTestClass (const Vector<Int>& fld1, const Vector<String>& fld2)
107// : itsFld1(fld1), itsFld2(fld2), itsEntry(0)
108// { AlwaysAssert (fld1.nelements() == fld2.nelements(), AipsError); }
109// virtual ~MyTestClass()
110// {}
111// void next()
112// { itsEntry++; }
113// // Note that only the get functions for the possible types are needed.
114// // Also note that all numeric types are handled by TaQL as Double.
115// // The exception should never be thrown unless things are screwed up.
116// virtual Double getDouble (const Block<Int>& fieldNrs) const
117// { switch (fieldNrs[0]) {
118// case 0:
119// return itsFld1(itsEntry);
120// default:
121// throw AipsError();
122// }
123// }
124// virtual String getString (const Block<Int>& fieldNrs) const
125// { switch (fieldNrs[0]) {
126// case 1:
127// return itsFld2(itsEntry);
128// default:
129// throw AipsError();
130// }
131// }
132// virtual DataType dataType (const Block<Int>& fieldNrs) const
133// { switch (fieldNrs[0]) {
134// case 0:
135// return TpInt;
136// case 1:
137// return TpString;
138// default:
139// throw AipsError();
140// }
141// }
142// // Make a Record to give to vectors a name.
143// // The order in which the fields are defined determines the fieldnrs
144// // passed to the get functions.
145// static Record makeRecord()
146// { RecordDesc desc;
147// desc.addField ("fld1", TpInt);
148// desc.addField ("fld2", TpString);
149// return Record(desc);
150// }
151// private:
152// Vector<Int> itsFld1;
153// Vector<String> itsFld2;
154// uInt itsEntry;
155// };
156//
157// Vector<uInt> findMatches (const Vector<Int>& fld1,
158// const Vector<String>& fld2)
159// {
160// // Make some expression.
161// // First create a Record to make the names and types known.
162// Record rec(MyTestClass::makeRecord());
163// TableExprNode expr (makeRecordExpr(rec,"fld1") > 10 &&
164// makeRecordExpr(rec,"fld2") != pattern("*xxx*"));
165// // Now evaluate the expression for each entry in the vector.
166// // Make a MyTestClass object to handle the vectors and put it in
167// // a TableExprId object for the TaQL evaluator.
168// // Note that TableExprId holds a pointer to the original MyTestClass
169// // object, so the TaQL evaluator 'sees' the changes we make by
170// // using the its next() function.
171// MyTestClass subj(fld1, fld2);
172// TableExprId eid(subj);
173// // The matching entry numbers are stored in a vector.
174// Vector<uInt> result(fld1.nelements());
175// uInt nr=0;
176// Bool valb;
177// for (uInt i=0; i<fld1.nelements(); i++) {
178// expr.get (eid, valb);
179// if (valb) {
180// result(nr++) = i;
181// }
182// subj.next(); // Next time the next entry must be used
183// }
184// result.resize (nr, True);
185// return result;
186// }
187// </srcBlock>
188// </example>
189
190// <motivation>
191// This class makes it possible that TaQL can be used in a very versatile way.
192// </motivation>
193
194// # <todo asof="1996/03/12">
195// # </todo>
196
198 public:
199 // Construct it from a row number.
201
202 virtual ~TableExprData();
203
204 // Get the shape of the given field.
205 // Need only be implemented if there are arrays in the data.
206 // The default implementation returns an empty IPosition.
207 virtual IPosition shape(const Block<Int>& fieldNrs) const;
208
209 // Get the data type of the given field.
210 // Note that TpArray types have to be returned for arrays.
211 // If the field is unknown, TpOther should be returned.
212 // It is used for the isdefined function to check if the field
213 // is really defined.
214 virtual DataType dataType(const Block<Int>& fieldNrs) const = 0;
215
216 // Get a scalar in the given type.
217 // This might involve converting for Double and DComplex.
218 // Most default implementations throws an "not possible" exception.
219 // The default <src>getDouble</src> invokes <src>getInt</src>.
220 // The default <src>getDComplex</src> invokes <src>getDouble</src>.
221 // <group>
222 virtual Bool getBool(const Block<Int>& fieldNrs) const;
223 virtual Int64 getInt(const Block<Int>& fieldNrs) const;
224 virtual Double getDouble(const Block<Int>& fieldNrs) const;
225 virtual DComplex getDComplex(const Block<Int>& fieldNrs) const;
226 virtual String getString(const Block<Int>& fieldNrs) const;
227 // </group>
228
229 // Get an array in the given type.
230 // This might involve converting for Double and DComplex.
231 // Most default implementations throws an "not possible" exception.
232 // The default <src>getArrayDComplex</src> invokes
233 // <src>getArrayDouble</src>.
234 // <group>
235 virtual Array<Bool> getArrayBool(const Block<Int>& fieldNrs) const;
236 virtual Array<Int64> getArrayInt(const Block<Int>& fieldNrs) const;
237 virtual Array<Double> getArrayDouble(const Block<Int>& fieldNrs) const;
238 virtual Array<DComplex> getArrayDComplex(const Block<Int>& fieldNrs) const;
239 virtual Array<String> getArrayString(const Block<Int>& fieldNrs) const;
240 // </group>
241};
242
243} // namespace casacore
244
245#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
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.
For temporary backward namespace compatibility, use casa as alias for casacore.
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