Expression.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Expression.hpp 1329 2008-07-30 03:54:10Z ocibu $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //  
00021 //  Copyright 2004, The University of Texas at Austin
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       // Represents a node of any type in an expression tree.
00176       class ExpNode {
00177          public:
00178 
00179          virtual ~ExpNode() {}
00180 
00181          // Compute and return the numerical value of this node
00182          virtual double getValue()
00183             throw (ExpressionException) =0;
00184          
00185   
00186          // Write out this node to a stream
00187         virtual std::ostream& print(std::ostream& ostr) =0; 
00188 
00189       }; // end class ExpNode
00190 
00191 
00192       // Represents a node that holds a number.  
00193       class ConstNode : public ExpNode {
00194          public:
00195             // Constructor.  Create a node to hold val.
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;  // The number in the node.
00207       }; // end class ConstNode
00208 
00209       // Represents a node that holds a variable  
00210       class VarNode : public ExpNode {
00211          public:
00212             // Constructor.  
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;  // The name of the varaible
00225             bool hasValue;
00226 
00227          void setValue(double newValue);
00228 
00229         private:
00230             double value;
00231           
00232          
00233       }; // end class VarNode
00234 
00235       // Represents a node that holds an operator.
00236       class BinOpNode : public ExpNode {
00237          public:
00238 
00239             // Constructor.  Create a node to hold the given data.
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;        // The operator.
00249             ExpNode *left;   // The left operand.
00250             ExpNode *right;  // The right operand.
00251 
00252       }; // end class BinOpNode
00253 
00254       // Represents a node that holds a function of a signle variable
00255       class FuncOpNode : public ExpNode {
00256          public:
00257 
00258             // Constructor.  Create a node to hold the given data.
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;        // The operator.
00268             ExpNode *right;  // The right operand.
00269 
00270       }; // end class FuncOpNode
00271 
00272          // This class is used internally, during construction of an Expression,
00273          // to generate ExpNodes. 
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; // has the node of this token been used (linked to?)
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    }; // End class expression
00332    
00333    
00334 } // End namespace gpstk
00335 
00336 #endif // EXPRESSION_HPP

Generated on Tue May 22 03:30:58 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1