00001 #ifndef LogicalExpressions_h
00002 #define LogicalExpressions_h 1
00003
00004 #include <map>
00005 #include <string>
00006 #include <list>
00007 #include <vector>
00008 #include <ostream>
00009
00010 typedef std::map< const std::string, std::string > ConditionsMap ;
00011 typedef std::map< const std::string, bool > ResultMap ;
00012
00013
00014 namespace marlin{
00015
00016
00022 struct Expression{
00023
00024 Expression() : Operation( AND ), isNot( false ), Value("") {
00025 }
00026 enum Operator{ OR, AND } ;
00027
00028 Operator Operation ;
00029 bool isNot ;
00030 std::string Value ;
00031 };
00032
00033 std::ostream& operator<< ( std::ostream& s, Expression& e ) ;
00034
00039 class Tokenizer{
00040
00041 enum state{
00042 newtoken,
00043 parenthesis,
00044 operation
00045 } ;
00046
00047 std::vector< Expression >& _tokens ;
00048 char _last ;
00049 bool needToken ;
00050 int openPar ;
00051 int closedPar ;
00052 state _state ;
00053
00054 public:
00055
00056 Tokenizer( std::vector< Expression >& tokens ) : _tokens(tokens) , needToken(true)
00057 , openPar(0), closedPar(0) ,
00058 _state( newtoken ) {
00059 }
00060
00061
00063 void operator()(const char& c) {
00064
00065 if( c != ' ' && c != '\t' ) {
00066
00067 if( c == '(' ) ++openPar ;
00068
00069 if( c == ')' ) ++closedPar ;
00070
00071 switch( _state ){
00072
00073 case( newtoken ):
00074
00075 if( needToken ){
00076 _tokens.push_back( Expression() ) ;
00077 needToken = false ;
00078 }
00079 if( c == '!' )
00080 _tokens.back().isNot = true ;
00081
00082 else if( c == '(' )
00083 _state = parenthesis ;
00084
00085 else {
00086 _tokens.back().Value += c ;
00087 _state = operation ;
00088 }
00089 break ;
00090
00091 case( parenthesis ):
00092
00093 if( closedPar == openPar ) {
00094
00095 _state = operation ;
00096
00097 } else {
00098
00099 _tokens.back().Value += c ;
00100 }
00101 break ;
00102
00103 case( operation ):
00104
00105 if( c == '&' || c=='|' ){
00106
00107 if ( c == '&' && _last == '&' ) {
00108 _tokens.push_back( Expression() ) ;
00109 _tokens.back().Operation = Expression::AND ;
00110 _state = newtoken ;
00111 }
00112 if ( c == '|' && _last == '|' ) {
00113 _tokens.push_back( Expression() ) ;
00114 _tokens.back().Operation = Expression::OR ;
00115 _state = newtoken ;
00116 }
00117
00118 } else {
00119
00120 _tokens.back().Value += c ;
00121 }
00122
00123 break ;
00124 }
00125
00126 }
00127 _last = c ;
00128 }
00129
00130 ~Tokenizer(){
00131 }
00132
00133 std::vector<Expression> & result() {
00134
00135 return _tokens ;
00136
00137 }
00138 };
00139
00143 class LogicalExpressions {
00144
00145 public:
00146
00149 LogicalExpressions() ;
00150
00152 virtual ~LogicalExpressions() {}
00153
00157 void addCondition( const std::string& name, const std::string& expression ) ;
00158
00160 void clear() ;
00161
00163 bool conditionIsTrue( const std::string& name ) ;
00164
00166 bool expressionIsTrue( const std::string& expression ) ;
00167
00169 void setValue( const std::string& key, bool val ) ;
00170
00171
00172 protected:
00173
00174 ConditionsMap _condMap ;
00175 ResultMap _resultMap ;
00176
00177 } ;
00178 }
00179
00180 #endif
00181
00182
00183