00001 #pragma ident "$Id: Expression.hpp 1329 2008-07-30 03:54:10Z ocibu $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00032 #ifndef EXPRESSION__HPP
00033 #define EXPRESSION__HPP
00034
00035 #include <iostream>
00036 #include <string>
00037 #include <list>
00038 #include <map>
00039
00040 #include "RinexObsHeader.hpp"
00041 #include "RinexObsData.hpp"
00042 #include "ObsEpochMap.hpp"
00043 #include "Exception.hpp"
00044
00045 namespace gpstk
00046 {
00049
00073 NEW_EXCEPTION_CLASS(ExpressionException, Exception);
00074
00075 class Expression
00076 {
00077 public:
00078
00082 Expression(void);
00083
00088 Expression(const std::string& str);
00089
00094 Expression(const Expression& rhs);
00095
00097 ~Expression(void);
00098
00100 Expression& operator=(const Expression& rhs);
00101
00102
00112 bool set(const std::string name, double value);
00113
00124 bool set(const char* name, double value)
00125 { return set (std::string(name),value); }
00126
00135 bool setGPSConstants(void);
00136
00144 bool setRinexObs(const RinexObsData::RinexObsTypeMap& rotm);
00145
00153 bool setSvObsEpoch(const SvObsEpoch& soe);
00154
00159 bool canEvaluate(void);
00160
00166 double evaluate(void) throw (ExpressionException)
00167 { return root->getValue(); }
00168
00172 void print(std::ostream& ostr) const {root->print(ostr);}
00173
00174 private:
00175
00176 class ExpNode {
00177 public:
00178
00179 virtual ~ExpNode() {}
00180
00181
00182 virtual double getValue()
00183 throw (ExpressionException) =0;
00184
00185
00186
00187 virtual std::ostream& print(std::ostream& ostr) =0;
00188
00189 };
00190
00191
00192
00193 class ConstNode : public ExpNode {
00194 public:
00195
00196 ConstNode( double theNum ): number(theNum) {}
00197
00198 double getValue() throw (ExpressionException)
00199 { return number; }
00200
00201 std::ostream& print(std::ostream& ostr) {
00202 ostr << number;
00203 return ostr;
00204 }
00205
00206 double number;
00207 };
00208
00209
00210 class VarNode : public ExpNode {
00211 public:
00212
00213
00214 VarNode(std::string theName ): name(theName), hasValue(false)
00215 {}
00216
00217 double getValue() throw (ExpressionException);
00218
00219 std::ostream& print(std::ostream& ostr) {
00220 ostr << name;
00221 return ostr;
00222 }
00223
00224 std::string name;
00225 bool hasValue;
00226
00227 void setValue(double newValue);
00228
00229 private:
00230 double value;
00231
00232
00233 };
00234
00235
00236 class BinOpNode : public ExpNode {
00237 public:
00238
00239
00240 BinOpNode( const std::string& theOp, ExpNode *theLeft, ExpNode *theRight ):
00241 op(theOp), left(theLeft), right(theRight){}
00242
00243 double getValue()
00244 throw (ExpressionException);
00245
00246 std::ostream& print(std::ostream& ostr);
00247
00248 std::string op;
00249 ExpNode *left;
00250 ExpNode *right;
00251
00252 };
00253
00254
00255 class FuncOpNode : public ExpNode {
00256 public:
00257
00258
00259 FuncOpNode( const std::string& theOp, ExpNode *theRight ):
00260 op(theOp), right(theRight){}
00261
00262 double getValue()
00263 throw (ExpressionException);
00264
00265 std::ostream& print(std::ostream& ostr);
00266
00267 std::string op;
00268 ExpNode *right;
00269
00270 };
00271
00272
00273
00274 class Token
00275 {
00276 public:
00277
00278 Token(std::string value, int relPriority,
00279 bool isOperator);
00280
00281 std::string getValue(void) {return value;}
00282
00283 int getPriority(void) {return priority;}
00284
00285 void setUsed(void) {used=true;}
00286 bool getUsed(void) {return used;}
00287
00288 ExpNode * getNode(void) {return expNode;}
00289 void setNode(ExpNode *newNode) {expNode = newNode; }
00290
00291 void setResolved(bool value) {resolved=value;}
00292 bool getResolved(void) {return resolved;}
00293
00294 bool getOperator(void) {return isOperator;}
00295 void setOperator(bool value) {isOperator = value;}
00296
00297 std::string getArgumentPattern(void) {return argumentPattern;}
00298 void setArgumentPattern(std::string value) {argumentPattern = value;}
00299
00300 void print(std::ostream& ostr);
00301
00302 private:
00303
00304 std::string value;
00305 bool isOperator;
00306 bool resolved;
00307
00308 int priority;
00309 ExpNode *expNode;
00310 bool used;
00311
00312 std::string argumentPattern;
00313 };
00314
00315 void setExpression(const std::string& newExpression);
00316 void dumpLists(void);
00317
00318 void defineOperators(void);
00319 void tokenize(const std::string& str);
00320 void buildExpressionTree(void);
00321
00322 int countResolvedTokens(void);
00323
00324 static std::map<std::string,int> operatorMap;
00325 static std::map<std::string,std::string> argumentPatternMap;
00326 static bool operatorsDefined;
00327
00328 std::list<Token> tList;
00329 std::list<ExpNode *> eList;
00330 ExpNode *root;
00331 };
00332
00333
00334 }
00335
00336 #endif // EXPRESSION_HPP