]>
Commit | Line | Data |
---|---|---|
a5a091ce | 1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * | |
3 | * * | |
4 | * Author: The ALICE Off-line Project. * | |
5 | * Contributors are mentioned in the code where appropriate. * | |
6 | * * | |
7 | * Permission to use, copy, modify and distribute this software and its * | |
8 | * documentation strictly for non-commercial purposes is hereby granted * | |
9 | * without fee, provided that the above copyright notice appears in all * | |
10 | * copies and that both the copyright notice and this permission notice * | |
11 | * appear in the supporting documentation. The authors make no claims * | |
12 | * about the suitability of this software for any purpose. It is * | |
13 | * provided "as is" without express or implied warranty. * | |
14 | **************************************************************************/ | |
15 | ||
16 | /* $Id$ */ | |
17 | ||
18 | /////////////////////////////////////////////////////////////////////////////// | |
19 | // // | |
20 | // AliExpression Class // // | |
21 | // // | |
22 | // Helper class to evaluate the condition expressions in // | |
51f6d619 | 23 | // AliTrigger* classes // |
a5a091ce | 24 | // Implements a simple recursive-descent parser // |
25 | // // | |
26 | /////////////////////////////////////////////////////////////////////////////// | |
27 | ||
28 | //#include <Riostream.h> | |
29 | #include <TString.h> | |
30 | #include <TObjString.h> | |
31 | #include <TObjArray.h> | |
32 | ||
33 | #include "AliLog.h" | |
34 | #include "AliExpression.h" | |
35 | #include "AliTriggerInput.h" | |
36 | ||
37 | ClassImp( AliExpression ) | |
38 | ||
39 | //______________________________________________________________________________ | |
75e3794b | 40 | AliExpression::AliExpression( TString exp ) : |
41 | TObject(), | |
42 | fVname(""), | |
43 | fArg1(0x0), | |
44 | fArg2(0x0), | |
45 | fOperator(0) | |
a5a091ce | 46 | { |
47 | // Default constructor | |
48 | TObjArray* tokens = Tokenize( exp ); | |
49 | ||
50 | Int_t i = -1; | |
51 | AliExpression* e = Expression( *tokens, i ); | |
52 | // Copy !!! | |
53 | fArg1 = e->fArg1; e->fArg1 = 0; | |
54 | fArg2 = e->fArg2; e->fArg2 = 0; | |
55 | fOperator = e->fOperator; | |
92c1978f | 56 | fVname = e->fVname; |
a5a091ce | 57 | delete e; |
58 | delete tokens; | |
59 | } | |
60 | ||
61 | //______________________________________________________________________________ | |
62 | AliExpression::~AliExpression() | |
63 | { | |
64 | if( fArg1 ) delete fArg1; | |
65 | if( fArg2 ) delete fArg2; | |
66 | } | |
67 | ||
68 | //______________________________________________________________________________ | |
69 | AliExpression& AliExpression::operator=(const AliExpression& e) | |
70 | { | |
71 | // AliExpression assignment operator. | |
72 | ||
73 | if( this != &e ) { | |
74 | TObject::operator=(e); | |
75 | fArg1 = e.fArg1; | |
76 | fArg2 = e.fArg2; | |
77 | fOperator = e.fOperator; | |
92c1978f | 78 | fVname = e.fVname; |
a5a091ce | 79 | } |
80 | return *this; | |
81 | } | |
82 | ||
83 | //______________________________________________________________________________ | |
75e3794b | 84 | AliExpression::AliExpression( int op, AliExpression* a, AliExpression* b ) : |
85 | TObject(), | |
86 | fVname(""), | |
87 | fArg1(a), | |
88 | fArg2(b), | |
89 | fOperator(op) | |
a5a091ce | 90 | { |
91 | // Create a new expression | |
a5a091ce | 92 | } |
93 | ||
94 | //______________________________________________________________________________ | |
75e3794b | 95 | AliExpression::AliExpression( int op, AliExpression* a ) : |
96 | TObject(), | |
97 | fVname(""), | |
98 | fArg1(0), | |
99 | fArg2(a), | |
100 | fOperator(op) | |
a5a091ce | 101 | { |
102 | // Create a unary expression. | |
a5a091ce | 103 | } |
104 | ||
105 | //______________________________________________________________________________ | |
51f6d619 | 106 | Bool_t AliExpression::Value( const TObjArray &vars ) |
a5a091ce | 107 | { |
108 | // Evaluate the expression | |
92c1978f | 109 | if ( fArg2 == 0 && fVname.IsNull() ) { |
a5a091ce | 110 | AliError( "Expression undefined." ); |
111 | return kFALSE; | |
112 | } | |
113 | ||
114 | switch (fOperator) { | |
115 | ||
116 | case kOpOR : | |
117 | return fArg1->Value(vars) || fArg2->Value(vars); | |
118 | ||
119 | case kOpAND : | |
120 | return fArg1->Value(vars) && fArg2->Value(vars); | |
121 | ||
122 | case kOpNOT : | |
123 | return !(fArg2->Value(vars)); | |
124 | ||
92c1978f | 125 | case 0 : |
126 | { | |
127 | TObject* dd = vars.FindObject( fVname.Data() ); | |
128 | if( dd == NULL ) { | |
129 | AliError( fVname + " is undefined" ); | |
130 | return 0; | |
131 | } | |
132 | return ((AliTriggerInput*)dd)->GetValue(); | |
133 | } | |
134 | ||
a5a091ce | 135 | default: |
136 | AliError( "Illegal operator in expression!"); | |
137 | ||
138 | } | |
139 | return kFALSE; | |
140 | } | |
141 | ||
142 | ||
143 | //______________________________________________________________________________ | |
144 | TString AliExpression::Unparse() const | |
145 | { | |
146 | // Unparse the expression | |
147 | ||
92c1978f | 148 | TString opVals[4] = { "", "&", "|","!" }; |
149 | if ( fArg2 == 0 && fVname.IsNull() ) { | |
150 | AliError( "Expression undefined." ); | |
151 | return "Error"; | |
a5a091ce | 152 | } |
153 | ||
92c1978f | 154 | if( fArg2 == 0 && !fVname.IsNull() ) return fVname; |
155 | ||
a5a091ce | 156 | if (fArg1 == 0 && fArg2) { |
92c1978f | 157 | return opVals[fOperator]+fArg2->Unparse(); |
a5a091ce | 158 | } |
159 | return "("+fArg1->Unparse()+" "+opVals[fOperator]+" "+fArg2->Unparse()+")"; | |
160 | } | |
161 | ||
162 | //______________________________________________________________________________ | |
163 | TObjArray* AliExpression::Tokenize( TString str ) const | |
164 | { | |
165 | // tokenize the expression | |
166 | ||
167 | // Remove spaces | |
168 | TString str1; | |
169 | for( Int_t i=0; i<str.Length(); i++ ) { | |
170 | if( str[i] == ' ' ) continue; | |
171 | str1.Append( str[i] ); | |
172 | } | |
173 | // get variable tokens | |
174 | TObjArray* valtok = str1.Tokenize( "!&|()" ); | |
175 | // put all variables together | |
176 | Int_t nvt = valtok->GetEntriesFast(); | |
177 | TString sumval; | |
178 | for( Int_t i=0; i<nvt; i++ ) { | |
179 | TObjString* val = (TObjString*)valtok->At( i ); | |
180 | sumval.Append( val->String() ); | |
181 | } | |
182 | // get the operator tokens | |
183 | TObjArray* optok = str1.Tokenize( sumval.Data() ); | |
184 | // put all operator in one string | |
185 | TString operators; | |
186 | Int_t nopt = optok->GetEntriesFast(); | |
187 | for( Int_t i=0; i<nopt; i++ ) { | |
188 | TObjString* val1 = (TObjString*)optok->At( i ); | |
189 | operators.Append( val1->String() ); | |
190 | } | |
191 | // add more room to be safe | |
192 | TObjString* blank = new TObjString(" "); | |
193 | operators.Append( " " ); | |
194 | valtok->AddLast( blank ); | |
195 | // Now put var. and oper. together | |
196 | TObjArray* tokens = new TObjArray( valtok->GetEntriesFast() + operators.Length() ); | |
197 | int io = 0,iv = 0; | |
198 | int index = 0; | |
199 | while( 1 ) { | |
200 | TString so = operators[io]; | |
201 | int indexO = str1.Index( so, index ); | |
202 | TString val2 = ((TObjString*)valtok->At( iv ))->String(); | |
203 | int indexV = str1.Index( val2, index ); | |
204 | if( (indexO < indexV || indexV < 0) && indexO >=0 ) { | |
205 | tokens->AddLast( new TObjString( so ) ); | |
206 | index += so.Length(); | |
207 | io++; | |
208 | } | |
209 | if( (indexV < indexO || indexO < 0) && indexV >=0 ) { | |
210 | tokens->AddLast( new TObjString( val2 ) ); | |
211 | index += val2.Length(); | |
212 | iv++; | |
213 | } | |
214 | if( index >= str1.Length() ) break; | |
215 | } | |
216 | ||
217 | // Debug -> Print the tokens | |
218 | // Int_t nt = tokens->GetEntriesFast(); | |
219 | // for( Int_t i=0; i<nt; i++ ) { | |
220 | // TObjString* val3 = (TObjString*)tokens->At( i ); | |
221 | // cout << i << " " << val3->String() << endl; | |
222 | // } | |
223 | delete valtok; | |
224 | delete optok; | |
225 | ||
226 | return tokens; | |
227 | } | |
228 | ||
229 | ||
230 | //______________________________________________________________________________ | |
231 | AliExpression* AliExpression::Element( TObjArray &st, Int_t &i ) | |
232 | { | |
233 | // create an element | |
234 | ||
235 | AliExpression* result = 0; | |
236 | ||
237 | Int_t nt = st.GetEntriesFast(); | |
238 | TString token = "@"; | |
239 | TObjString* valt; | |
240 | // next token | |
241 | if( i < nt-1 ) { | |
242 | i++; | |
243 | valt = (TObjString*)st.At( i ); | |
244 | token = valt->String(); | |
245 | } | |
246 | // token type | |
247 | char ttok = ( token[0]!='|' && token[0]!='&' && | |
248 | token[0]!='!' && token[0]!='('&& token[0]!=')') ? 'w' : token[0]; | |
249 | switch( ttok ) { | |
250 | case 'w' : | |
251 | result = new AliVariableExpression( token ); | |
252 | break; | |
253 | case '(' : | |
254 | result = Expression(st, i); | |
255 | // next token | |
256 | if( i < nt-1 ) { | |
257 | i++; | |
258 | valt = (TObjString*)st.At( i ); | |
259 | token = valt->String(); | |
260 | } | |
261 | if( token[0] != ')' ) { | |
262 | // i--; // push back | |
263 | AliErrorGeneral( "AliExpression::Element", "Mismatched parenthesis." ); | |
264 | delete result; | |
265 | result = new AliExpression; | |
266 | } | |
267 | break; | |
268 | default: | |
269 | i--; // push back | |
270 | AliErrorGeneral( "AliExpression::Element", Form("Unexpected symbol on input. %s", token.Data()) ); | |
271 | if( result ) delete result; | |
272 | result = new AliExpression; | |
273 | } | |
274 | return result; | |
275 | } | |
276 | ||
277 | //______________________________________________________________________________ | |
278 | AliExpression* AliExpression::Primary( TObjArray &st, Int_t &i ) | |
279 | { | |
280 | // create a primary | |
281 | ||
282 | Int_t nt = st.GetEntriesFast(); | |
283 | TString token = "@"; | |
284 | TObjString* valt; | |
285 | // next token | |
286 | if( i < nt-1 ) { | |
287 | i++; | |
288 | valt = (TObjString*)st.At( i ); | |
289 | token = valt->String(); | |
290 | } | |
291 | ||
292 | switch (token[0]) { | |
293 | case '!' : | |
294 | return new AliExpression( kOpNOT, Primary( st, i ) ); | |
295 | default: | |
296 | i--; // push back | |
297 | return Element( st, i ); | |
298 | } | |
299 | } | |
300 | ||
301 | //______________________________________________________________________________ | |
302 | AliExpression* AliExpression::Expression( TObjArray &st,Int_t &i ) | |
303 | { | |
304 | // create an expression | |
305 | ||
306 | AliExpression* result = 0; | |
307 | Bool_t done = kFALSE; | |
308 | TString token; | |
309 | TObjString* valt; | |
310 | ||
311 | static int stack = 0; | |
312 | stack++; | |
313 | Int_t nt = st.GetEntriesFast(); | |
314 | ||
315 | result = Primary( st, i ); | |
316 | // cout <<"i "<<i<< "Primary " << result->Unparse() << endl; | |
317 | while (! done) { | |
318 | // next token | |
319 | if( i < nt-1 ) i++; | |
320 | else break; | |
321 | valt = (TObjString*)st.At( i ); | |
322 | token = valt->String(); | |
323 | switch (token[0]) { | |
324 | case '&' : | |
325 | result = new AliExpression( kOpAND, result, Primary( st, i ) ); | |
326 | // cout <<"i "<<i<< " Expression AND " << result->Unparse() << endl; | |
327 | break; | |
328 | case '|' : | |
329 | result = new AliExpression( kOpOR, result, Primary( st, i ) ); | |
330 | // cout <<"i "<<i<< " Expression OR " << result->Unparse() << endl; | |
331 | break; | |
332 | default: | |
333 | done = kTRUE; | |
334 | i--; // push back | |
335 | break; | |
336 | } | |
337 | } | |
338 | stack--; | |
339 | if( stack == 0 && !token.IsNull() && token[0] == ')' ) { | |
340 | AliErrorGeneral( "AliExpression::Expression", "To many closing parenthesis." ); | |
341 | delete result; | |
342 | result = new AliExpression; | |
343 | } else | |
344 | if( stack == 0 && i< nt-1 ) { | |
345 | AliErrorGeneral( "AliExpression::Expression", Form( "Unexpected symbol on input. %s", token.Data() ) ); | |
346 | delete result; | |
347 | result = new AliExpression; | |
348 | } | |
349 | return result; | |
350 | } | |
351 | ||
352 | //////////////////////////////////////////////////////////////////////////////// | |
353 | ||
354 | ClassImp( AliVariableExpression ) | |
355 | ||
356 | //______________________________________________________________________________ | |
51f6d619 | 357 | Bool_t AliVariableExpression::Value( const TObjArray& pgm ) |
a5a091ce | 358 | { |
359 | // return the value | |
360 | TObject* dd = pgm.FindObject( fVname.Data() ); | |
361 | if( dd == NULL ) { | |
362 | AliError( fVname + " is undefined" ); | |
363 | return 0; | |
364 | } | |
365 | return ((AliTriggerInput*)dd)->GetValue(); | |
366 | } | |
367 |