1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // AliExpression Class // //
22 // Helper class to evaluate the condition expressions in //
23 // AliTriggerCondition //
24 // Implements a simple recursive-descent parser //
26 ///////////////////////////////////////////////////////////////////////////////
28 //#include <Riostream.h>
30 #include <TObjString.h>
31 #include <TObjArray.h>
34 #include "AliExpression.h"
35 #include "AliTriggerInput.h"
37 ClassImp( AliExpression )
39 //______________________________________________________________________________
40 AliExpression::AliExpression( TString exp )
42 // Default constructor
43 TObjArray* tokens = Tokenize( exp );
46 AliExpression* e = Expression( *tokens, i );
48 fArg1 = e->fArg1; e->fArg1 = 0;
49 fArg2 = e->fArg2; e->fArg2 = 0;
50 fOperator = e->fOperator;
56 //______________________________________________________________________________
57 AliExpression::~AliExpression()
59 if( fArg1 ) delete fArg1;
60 if( fArg2 ) delete fArg2;
63 //______________________________________________________________________________
64 AliExpression& AliExpression::operator=(const AliExpression& e)
66 // AliExpression assignment operator.
69 TObject::operator=(e);
72 fOperator = e.fOperator;
78 //______________________________________________________________________________
79 AliExpression::AliExpression( int op, AliExpression* a, AliExpression* b )
81 // Create a new expression
87 //______________________________________________________________________________
88 AliExpression::AliExpression( int op, AliExpression* a )
90 // Create a unary expression.
96 //______________________________________________________________________________
97 Bool_t AliExpression::Value( TObjArray &vars )
99 // Evaluate the expression
100 if ( fArg2 == 0 && fVname.IsNull() ) {
101 AliError( "Expression undefined." );
108 return fArg1->Value(vars) || fArg2->Value(vars);
111 return fArg1->Value(vars) && fArg2->Value(vars);
114 return !(fArg2->Value(vars));
118 TObject* dd = vars.FindObject( fVname.Data() );
120 AliError( fVname + " is undefined" );
123 return ((AliTriggerInput*)dd)->GetValue();
127 AliError( "Illegal operator in expression!");
134 //______________________________________________________________________________
135 TString AliExpression::Unparse() const
137 // Unparse the expression
139 TString opVals[4] = { "", "&", "|","!" };
140 if ( fArg2 == 0 && fVname.IsNull() ) {
141 AliError( "Expression undefined." );
145 if( fArg2 == 0 && !fVname.IsNull() ) return fVname;
147 if (fArg1 == 0 && fArg2) {
148 return opVals[fOperator]+fArg2->Unparse();
150 return "("+fArg1->Unparse()+" "+opVals[fOperator]+" "+fArg2->Unparse()+")";
153 //______________________________________________________________________________
154 TObjArray* AliExpression::Tokenize( TString str ) const
156 // tokenize the expression
160 for( Int_t i=0; i<str.Length(); i++ ) {
161 if( str[i] == ' ' ) continue;
162 str1.Append( str[i] );
164 // get variable tokens
165 TObjArray* valtok = str1.Tokenize( "!&|()" );
166 // put all variables together
167 Int_t nvt = valtok->GetEntriesFast();
169 for( Int_t i=0; i<nvt; i++ ) {
170 TObjString* val = (TObjString*)valtok->At( i );
171 sumval.Append( val->String() );
173 // get the operator tokens
174 TObjArray* optok = str1.Tokenize( sumval.Data() );
175 // put all operator in one string
177 Int_t nopt = optok->GetEntriesFast();
178 for( Int_t i=0; i<nopt; i++ ) {
179 TObjString* val1 = (TObjString*)optok->At( i );
180 operators.Append( val1->String() );
182 // add more room to be safe
183 TObjString* blank = new TObjString(" ");
184 operators.Append( " " );
185 valtok->AddLast( blank );
186 // Now put var. and oper. together
187 TObjArray* tokens = new TObjArray( valtok->GetEntriesFast() + operators.Length() );
191 TString so = operators[io];
192 int indexO = str1.Index( so, index );
193 TString val2 = ((TObjString*)valtok->At( iv ))->String();
194 int indexV = str1.Index( val2, index );
195 if( (indexO < indexV || indexV < 0) && indexO >=0 ) {
196 tokens->AddLast( new TObjString( so ) );
197 index += so.Length();
200 if( (indexV < indexO || indexO < 0) && indexV >=0 ) {
201 tokens->AddLast( new TObjString( val2 ) );
202 index += val2.Length();
205 if( index >= str1.Length() ) break;
208 // Debug -> Print the tokens
209 // Int_t nt = tokens->GetEntriesFast();
210 // for( Int_t i=0; i<nt; i++ ) {
211 // TObjString* val3 = (TObjString*)tokens->At( i );
212 // cout << i << " " << val3->String() << endl;
221 //______________________________________________________________________________
222 AliExpression* AliExpression::Element( TObjArray &st, Int_t &i )
226 AliExpression* result = 0;
228 Int_t nt = st.GetEntriesFast();
234 valt = (TObjString*)st.At( i );
235 token = valt->String();
238 char ttok = ( token[0]!='|' && token[0]!='&' &&
239 token[0]!='!' && token[0]!='('&& token[0]!=')') ? 'w' : token[0];
242 result = new AliVariableExpression( token );
245 result = Expression(st, i);
249 valt = (TObjString*)st.At( i );
250 token = valt->String();
252 if( token[0] != ')' ) {
254 AliErrorGeneral( "AliExpression::Element", "Mismatched parenthesis." );
256 result = new AliExpression;
261 AliErrorGeneral( "AliExpression::Element", Form("Unexpected symbol on input. %s", token.Data()) );
262 if( result ) delete result;
263 result = new AliExpression;
268 //______________________________________________________________________________
269 AliExpression* AliExpression::Primary( TObjArray &st, Int_t &i )
273 Int_t nt = st.GetEntriesFast();
279 valt = (TObjString*)st.At( i );
280 token = valt->String();
285 return new AliExpression( kOpNOT, Primary( st, i ) );
288 return Element( st, i );
292 //______________________________________________________________________________
293 AliExpression* AliExpression::Expression( TObjArray &st,Int_t &i )
295 // create an expression
297 AliExpression* result = 0;
298 Bool_t done = kFALSE;
302 static int stack = 0;
304 Int_t nt = st.GetEntriesFast();
306 result = Primary( st, i );
307 // cout <<"i "<<i<< "Primary " << result->Unparse() << endl;
312 valt = (TObjString*)st.At( i );
313 token = valt->String();
316 result = new AliExpression( kOpAND, result, Primary( st, i ) );
317 // cout <<"i "<<i<< " Expression AND " << result->Unparse() << endl;
320 result = new AliExpression( kOpOR, result, Primary( st, i ) );
321 // cout <<"i "<<i<< " Expression OR " << result->Unparse() << endl;
330 if( stack == 0 && !token.IsNull() && token[0] == ')' ) {
331 AliErrorGeneral( "AliExpression::Expression", "To many closing parenthesis." );
333 result = new AliExpression;
335 if( stack == 0 && i< nt-1 ) {
336 AliErrorGeneral( "AliExpression::Expression", Form( "Unexpected symbol on input. %s", token.Data() ) );
338 result = new AliExpression;
343 ////////////////////////////////////////////////////////////////////////////////
345 ClassImp( AliVariableExpression )
347 //______________________________________________________________________________
348 Bool_t AliVariableExpression::Value( TObjArray& pgm )
351 TObject* dd = pgm.FindObject( fVname.Data() );
353 AliError( fVname + " is undefined" );
356 return ((AliTriggerInput*)dd)->GetValue();