BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/rdbModel/src/Tables/Assertion.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/rdbModel/src/Tables/Assertion.cxx,v 1.1.1.1 2005/10/17
2// 06:10:53 maqm Exp $
3#include "rdbModel/Tables/Assertion.h"
4#include "facilities/Timestamp.h"
5#include "facilities/Util.h"
6#include "rdbModel/Rdb.h"
7#include "rdbModel/RdbException.h"
8#include "rdbModel/Tables/Column.h"
9#include "rdbModel/Tables/Datatype.h"
10#include "rdbModel/Tables/Table.h"
11namespace rdbModel {
12
14 if ( !m_keepChildren )
15 {
16 while ( m_operands.size() )
17 {
18 Operator* op = m_operands.back();
19 m_operands.pop_back();
20 delete op;
21 }
22 }
23 }
24
25 // Substitute values from toBe row as needed.
26 Assertion::Assertion( const Assertion* orig, Row* toBe )
27 : m_op( 0 )
28 , m_myTable( orig->m_myTable )
29 , m_keepOp( false )
30 , m_name( "" )
31 , m_compiled( "" ) {
32 toBe->rowSort();
33 m_op = new Assertion::Operator( orig->m_op, toBe );
34 }
35
37 if ( !m_keepOp ) delete m_op;
38 }
39
40 // Constructor for comparisons
41 // It's also used for isNull and isEmpty, but in those cases rightArg
42 // and rightLiteral are ignored.
43 // Internally they get set to "" and FIELDTYPElit, resp.
44 Assertion::Operator::Operator( OPTYPE type, const std::string& leftArg,
45 const std::string& rightArg, FIELDTYPE leftLiteral,
46 FIELDTYPE rightLiteral )
47 : m_opType( type ), m_keepChildren( false ), m_toBe( false ), m_old( false ) {
48
49 m_tableName.clear();
50 m_operands.clear();
51 if ( !isCompareOp() )
52 {
53 m_opType = OPTYPEundefined;
54 return;
55 }
56
57 m_compareArgs[0] = leftArg;
58 m_compareArgs[1] = rightArg;
59 m_compareType[0] = leftLiteral;
60 m_compareType[1] = rightLiteral;
61 if ( ( type == OPTYPEisNull ) || ( type == OPTYPEisEmpty ) )
62 {
63 m_compareType[1] = FIELDTYPElit;
64 m_compareArgs[1] = "";
65 }
66 m_toBe = ( ( leftLiteral == FIELDTYPEtoBe ) || ( rightLiteral == FIELDTYPEtoBe ) );
67 m_old = ( ( leftLiteral == FIELDTYPEold ) || ( rightLiteral == FIELDTYPEold ) );
68 }
69
70 /// Constructor for EXISTS
71 Assertion::Operator::Operator( OPTYPE type, const std::string& tableName, Operator* child )
72 : m_opType( type ), m_tableName( tableName ), m_keepChildren( false ) {
73 if ( type != OPTYPEexists )
74 {
75 m_opType = OPTYPEundefined;
76 return;
77 }
78 m_operands.clear();
79 m_operands.push_back( child );
80 }
81
82 /// Constructor for OR, AND, NOT
83 Assertion::Operator::Operator( OPTYPE type, const std::vector<Operator*>& children,
84 bool keepChildren )
85 : m_opType( type ), m_keepChildren( keepChildren ) {
86
87 m_toBe = false;
88 m_old = false;
89 if ( ( type == OPTYPEor ) || ( type == OPTYPEand ) || ( type = OPTYPEnot ) )
90 {
91 m_tableName.clear();
92 unsigned int nChild = children.size();
93 if ( !nChild )
94 {
95 m_opType = OPTYPEundefined;
96 return;
97 }
98 for ( unsigned int iChild = 0; iChild < nChild; iChild++ )
99 {
100 m_toBe |= ( children[iChild]->m_toBe );
101 m_old |= ( children[iChild]->m_old );
102 m_operands.push_back( children[iChild] );
103 }
104 }
105 else m_opType = OPTYPEundefined;
106 }
107
108 /// Copy an operator, substituting from toBe row as appropriate
110 : m_opType( op->m_opType )
111 , m_tableName( op->m_tableName )
112 , m_keepChildren( false )
113 , m_toBe( false )
114 , m_old( op->m_old ) {
115 m_operands.clear();
116
117 switch ( m_opType )
118 {
119 // OPTYPEor, and, not and exists all have child operators
120 case OPTYPEor:
121 case OPTYPEand:
122 case OPTYPEnot:
123 case OPTYPEexists: {
124 unsigned nChild = op->m_operands.size();
125 for ( unsigned iChild = 0; iChild < nChild; iChild++ )
126 {
127 Operator* child = new Operator( ( op->m_operands )[iChild], toBe );
128 appendChild( child );
129 }
130 break;
131 }
132 case OPTYPEequal:
133 case OPTYPEnotEqual:
134 case OPTYPElessThan:
138 case OPTYPEisEmpty:
139 case OPTYPEisNull: {
140 for ( unsigned i = 0; i < 2; i++ )
141 {
142 if ( ( op->m_compareType[i] == FIELDTYPEtoBe ) ||
143 ( op->m_compareType[i] == FIELDTYPEtoBeDef ) )
144 {
145 // have to supply value from row
146 FieldVal* f = toBe->find( op->m_compareArgs[i] );
147 if ( !f )
148 { throw RdbException( "Assertion::Operator constructor can't resolve field" ); }
149 m_compareArgs[i] = f->m_val;
150 m_compareType[i] = FIELDTYPElit;
151 }
152 else
153 { // just copy what's there
154 m_compareArgs[i] = op->m_compareArgs[i];
155 m_compareType[i] = op->m_compareType[i];
156 }
157 }
158 break;
159 }
160 default: throw RdbException( "Assertion::Operator constructor - Unknown OP type" );
161 }
162 }
163
164 // This only makes sense for conjunction-style operators AND, OR
166 m_toBe |= child->m_toBe;
167 m_old |= child->m_old;
168 if ( ( m_opType == OPTYPEor ) || ( m_opType == OPTYPEand ) )
169 {
170 m_operands.push_back( child );
171 return true;
172 }
173 else if ( ( m_opType == OPTYPEnot ) && ( m_operands.size() == 0 ) )
174 {
175 m_operands.push_back( child );
176 return true;
177 }
178 throw RdbException( "Assertion::Operator::appendChild: wrong parent operator type" );
179 return false;
180 }
181
183 if ( m_compareType[0] != FIELDTYPElit )
184 {
185 Column* col0 = myTable->getColumnByName( m_compareArgs[0] );
186 if ( m_compareType[1] != FIELDTYPElit )
187 {
188 Column* col1 = myTable->getColumnByName( m_compareArgs[1] );
189 return col1->isCompatible( col0 );
190 }
191 else
192 { // one column, one literal
193 return col0->okValue( m_compareArgs[1], false );
194 }
195 }
196 else
197 { // 1st arg is a literal; second arg must be column
198 Column* col1 = myTable->getColumnByName( m_compareArgs[1] );
199 return col1->okValue( m_compareArgs[0], false );
200 }
201 }
202
203 /// Throw exception if Operator is not a comparison operator
204 const std::string* Assertion::Operator::getCompareArgs() const {
205 if ( !isCompareOp() )
206 throw RdbException( "Assertion::Operator::getCompareArgs: wrong type" );
207 return &m_compareArgs[0];
208 }
209
210 /// Throw exception if Operator is not a comparison operator
211 // const bool* Assertion::Operator::getLiteralness() const {
213 if ( !isCompareOp() )
214 throw RdbException( "Assertion::Operator::getLiteralness: wrong type" );
215 return &m_compareType[0];
216 }
217
218 /// Throw exception if Operator is not EXISTS
219 const std::string& Assertion::Operator::getTableName() const {
220 if ( m_opType != OPTYPEexists )
221 throw RdbException( "Assertion::Operator::getTableName: wrong type" );
222 return m_tableName;
223 }
224
225 bool Assertion::Operator::verify( Row& old, Row& toBe, Table* t ) const {
226 switch ( m_opType )
227 {
228 case OPTYPEor: {
229 unsigned nChild = m_operands.size();
230 for ( unsigned i = 0; i < nChild; i++ )
231 {
232 if ( m_operands[i]->verify( old, toBe, t ) ) return true;
233 }
234 return false;
235 }
236
237 case OPTYPEand: {
238 unsigned nChild = m_operands.size();
239 for ( unsigned i = 0; i < nChild; i++ )
240 {
241 if ( !( m_operands[i]->verify( old, toBe, t ) ) ) return false;
242 }
243 return true;
244 }
245 case OPTYPEnot: return ( !( m_operands[0]->verify( old, toBe, t ) ) );
246
247 case OPTYPEisNull:
248 case OPTYPEisEmpty:
249 // These two are almost the same
250 {
251 Row* r = 0;
252 if ( ( m_compareType[0] == FIELDTYPEtoBe ) ||
253 ( m_compareType[0] == FIELDTYPEtoBeDef ) )
254 r = &toBe;
255 else r = &old;
256
257 FieldVal* f = r->find( m_compareArgs[0] );
258 if ( f )
259 {
260 if ( m_opType == OPTYPEisNull ) return f->m_null;
261 return ( ( f->m_val ).size() == 0 );
262 }
263 else
264 { // improper input. Field should have been found
265 throw RdbException( "Assertion::Operator::verify missing isNull field" );
266 }
267 }
268 // handle all 2-argument compare operators together
269 case OPTYPEequal:
270 case OPTYPEnotEqual:
271 case OPTYPElessThan:
274 case OPTYPEgreaterOrEqual: return verifyCompare( old, toBe, t );
275
276 default: return false;
277 }
278 return false;
279 }
280
281 bool Assertion::Operator::verifyCompare( Row& old, Row& toBe, Table* t ) const {
282 // Assume we already know the comparison is sensible; that is, that
283 // the two args are of compatible types. This check should be
284 // done when the operator is constructed (by XercesBuilder)
285 std::string values[2];
286 std::string colname;
287 colname.clear(); // used to determine type (string, int,..) of compare
288
289 for ( unsigned i = 0; i < 2; i++ )
290 {
291 switch ( m_compareType[i] )
292 {
293 case FIELDTYPElit:
294 case FIELDTYPElitDef: values[i] = m_compareArgs[i]; break;
295 case FIELDTYPEold:
296 case FIELDTYPEoldDef: {
297 FieldVal* f = old.find( m_compareArgs[i] );
298 if ( !f )
299 {
300 std::string msg =
301 std::string( "Assertion::Operator::verifyCompare missing field " ) +
302 m_compareArgs[i];
303 throw RdbException( msg );
304 }
305 values[i] = f->m_val;
306 colname = f->m_colname;
307 break;
308 }
309 case FIELDTYPEtoBe:
310 case FIELDTYPEtoBeDef: {
311 FieldVal* f = toBe.find( m_compareArgs[i] );
312 if ( !f ) { throw RdbException( "Assertion::Operator::verifyCompare missing field" ); }
313 values[i] = f->m_val;
314 colname = f->m_colname;
315 break;
316 }
317 default: throw RdbException( "Assertion::Operator::verifyCompare bad arg type" );
318 }
319 }
320 if ( colname.size() > 0 )
321 { // determine type to convert to
322 Column* c = t->getColumnByName( colname );
323 Datatype* d = c->getDatatype();
324
325 switch ( d->getType() )
326 {
328 case Datatype::TYPEdatetime: return compareTs( &values[0], m_opType );
331 case Datatype::TYPEsmallint: return compareInt( &values[0], m_opType );
333 case Datatype::TYPEdouble: return compareFloat( &values[0], m_opType );
334 default: // do nothing
335 ;
336 }
337 }
338 // Otherwise they're strings.
339 return compareString( &values[0], m_opType );
340 }
341 /// Throw exception if Operator is a comparison operator
342 const std::vector<Assertion::Operator*>& Assertion::Operator::getChildren() const {
343 if ( isCompareOp() ) throw RdbException( "Assertion::Operator::getChildren: wrong type" );
344 return m_operands;
345 }
346
348 Visitor::VisitorState state = v->visitAssertion( this );
349 if ( state == Visitor::VBRANCHDONE ) return Visitor::VCONTINUE;
350 return state;
351 }
352
353 bool Assertion::verify( Row& old, Row& toBe ) const {
354
355 if ( getOld() )
356 { // will actually use old vector, so sort
357 old.rowSort();
358 }
359 if ( getToBe() )
360 { // will actually use toBe vector, so sort
361 toBe.rowSort();
362 }
363 return m_op->verify( old, toBe, m_myTable );
364 }
365
366 bool Assertion::Operator::compareTs( const std::string* vals, OPTYPE type ) const {
368 Timestamp left, right;
369 try
370 {
371 left = Timestamp( *vals );
372 right = Timestamp( *( vals + 1 ) );
373 } catch ( facilities::BadTimeInput ex )
374 { throw RdbException( "Assertion::Operator::CompareTs illegal input" ); }
375
376 switch ( type )
377 {
378 case OPTYPEequal: return left == right;
379 case OPTYPEnotEqual: return left != right;
380 case OPTYPElessThan: return left < right;
381 case OPTYPEgreaterThan: return left > right;
382 case OPTYPElessOrEqual: return left <= right;
383 case OPTYPEgreaterOrEqual: return right >= right;
384 default: throw RdbException( "Assertion::Operator::compareTs bad OPTYPE" );
385 }
386 return false;
387 }
388
389 bool Assertion::Operator::compareInt( const std::string* vals, OPTYPE type ) const {
390 using facilities::Util;
391
392 try
393 {
394 int i = Util::stringToInt( *vals );
395 i = Util::stringToInt( *( vals + 1 ) );
396 } catch ( facilities::WrongType ex )
397 { throw RdbException( "Assertion::Operator::compareInt illegal input" ); }
398 return compareFloat( vals, type );
399 }
400
401 /// Handling specific to floating point data
402 bool Assertion::Operator::compareFloat( const std::string* vals, OPTYPE type ) const {
403 using facilities::Util;
404 double left, right;
405 try
406 {
407 left = Util::stringToDouble( *vals );
408 right = Util::stringToDouble( *( vals + 1 ) );
409 } catch ( facilities::WrongType ex )
410 { throw RdbException( "Assertion::Operator::compareFloat illegal input" ); }
411 switch ( type )
412 {
413 case OPTYPEequal: return left == right;
414 case OPTYPEnotEqual: return left != right;
415 case OPTYPElessThan: return left < right;
416 case OPTYPEgreaterThan: return left > right;
417 case OPTYPElessOrEqual: return left <= right;
418 case OPTYPEgreaterOrEqual: return right >= right;
419 default: throw RdbException( "Assertion::Operator::compareFloat bad OPTYPE" );
420 }
421 return false;
422 }
423
424 /// Handling specific to string data. Only supported operators for
425 /// strings are == and !=
426 bool Assertion::Operator::compareString( const std::string* vals, OPTYPE type ) const {
427 switch ( type )
428 {
429 case OPTYPEequal: return ( ( *vals ).compare( *( vals + 1 ) ) == 0 );
430 case OPTYPEnotEqual: return ( ( *vals ).compare( *( vals + 1 ) ) != 0 );
431 default: throw RdbException( "Assertion::Operator::compareString Unsupported OPTYPE" );
432 }
433 return false;
434 }
435
436} // namespace rdbModel
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition KarLud.h:35
bool appendChild(Operator *child)
Add another child to a conjunction-style operator.
const std::vector< Operator * > & getChildren() const
Throw exception if Operator is a comparison operator.
const std::string & getTableName() const
Throw exception if Operaotr is not EXISTS.
const std::string * getCompareArgs() const
Throw exception if Operator is not a comparison operator.
const FIELDTYPE * getCompareArgTypes() const
Get types of comparison args.
bool verify(Row &old, Row &toBe, Table *t) const
Evaluate operator on argument Rows.
Visitor::VisitorState accept(Visitor *v)
Assertion(Operator *op=0, Table *myTable=0, bool keepOp=false)
bool isCompatible(const Column *otherCol) const
Return true if otherCol and this have compatible datatypes.
Definition Column.cxx:29
bool okValue(const std::string &val, bool set=true) const
Definition Column.cxx:18
FieldVal * find(std::string colname)
Definition Column.cxx:65
void rowSort()
Definition Column.cxx:57
Column * getColumnByName(const std::string &name) const
Definition Table.cxx:67
int t()
Definition t.c:1