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