Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LogicalExpressions.h
Go to the documentation of this file.
1 #ifndef LogicalExpressions_h
2 #define LogicalExpressions_h 1
3 
4 #include <map>
5 #include <string>
6 #include <list>
7 #include <vector>
8 #include <ostream>
9 
12 
13 
14 namespace marlin{
15 
16 
22  struct Expression{
23 
24  Expression() : Operation( AND ), isNot( false ), Value("") {
25  }
26  enum Operator{ OR, AND } ;
27 
29  bool isNot ;
31  };
32 
34 
39  class Tokenizer{
40 
41  enum state{
45  } ;
46 
48  char _last ;
49  bool needToken ;
50  int openPar ;
51  int closedPar ;
53 
54  public:
55 
56  Tokenizer( std::vector< Expression >& tokens ) : _tokens(tokens) , _last(0), needToken(true)
57  , openPar(0), closedPar(0) ,
58  _state( newtoken ) {
59  }
60 
61 
63  void operator()(const char& c) {
64 
65  if( c != ' ' && c != '\t' ) { // ignore whitespaces and tabs
66 
67  if( c == '(' ) ++openPar ;
68 
69  if( c == ')' ) ++closedPar ;
70 
71  switch( _state ){
72 
73  case( newtoken ):
74 
75  if( needToken ){
76  _tokens.push_back( Expression() ) ; // create a new object
77  needToken = false ;
78  }
79  if( c == '!' )
80  _tokens.back().isNot = true ;
81 
82  else if( c == '(' )
84 
85  else {
86  _tokens.back().Value += c ;
87  _state = operation ;
88  }
89  break ;
90 
91  case( parenthesis ):
92 
93  if( closedPar == openPar ) {
94 
95  _state = operation ;
96 
97  } else {
98 
99  _tokens.back().Value += c ;
100  }
101  break ;
102 
103  case( operation ): // need to accumulate values until && or ||
104 
105  if( c == '&' || c=='|' ){
106 
107  if ( c == '&' && _last == '&' ) {
108  _tokens.push_back( Expression() ) ; // create a new object
109  _tokens.back().Operation = Expression::AND ;
110  _state = newtoken ;
111  }
112  if ( c == '|' && _last == '|' ) {
113  _tokens.push_back( Expression() ) ; // create a new object
114  _tokens.back().Operation = Expression::OR ;
115  _state = newtoken ;
116  }
117 
118  } else {
119 
120  _tokens.back().Value += c ;
121  }
122 
123  break ;
124  }
125 
126  }
127  _last = c ;
128  }
129 
131  }
132 
134 
135  return _tokens ;
136 
137  }
138  };
139 
144 
145  public:
146 
150 
152  virtual ~LogicalExpressions() {}
153 
157  void addCondition( const std::string& name, const std::string& expression ) ;
158 
160  void clear() ;
161 
163  bool conditionIsTrue( const std::string& name ) ;
164 
166  bool expressionIsTrue( const std::string& expression ) ;
167 
169  void setValue( const std::string& key, bool val ) ;
170 
171 
172  protected:
173 
175  bool getValue( const std::string& key );
176 
179 
180  } ;
181 } // end namespace
182 
183 #endif
184 
185 
186 
std::map< const std::string, bool > ResultMap
Tokenizer(std::vector< Expression > &tokens)
void clear()
Clear all boolean values.
void setValue(const std::string &key, bool val)
Set the the boolean value for the given key.
bool getValue(const std::string &key)
helper function for finding return values, that actually have been set by their corresponding process...
virtual ~LogicalExpressions()
Virtual d&#39;tor.
Helper class for LogicalExpressions that splits the expression into subexpressions - needs to be apll...
Helper struct for LogicalExpression.
STL class.
std::vector< Expression > & result()
Helper class that holds named boolean values and named conditions that are expressions of these value...
bool expressionIsTrue(const std::string &expression)
True if the given expression is true with the current values.
std::map< const std::string, std::string > ConditionsMap
bool conditionIsTrue(const std::string &name)
True if the named condition (stored with addCondition) is true with the current values.
std::vector< Expression > & _tokens
void operator()(const char &c)
Save the current character and change state if necessary.
STL class.
void addCondition(const std::string &name, const std::string &expression)
Add a new named logical expression formed out of [!,(,&amp;&amp;,||,),value], e.g.
std::ostream & operator<<(std::ostream &s, Expression &e)