Marlin  1.17.1
 All Classes Namespaces Functions Variables Enumerations Friends Pages
LogicalExpressions.h
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 
10 typedef std::map< const std::string, std::string > ConditionsMap ;
11 typedef std::map< const std::string, bool > ResultMap ;
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 
28  Operator Operation ;
29  bool isNot ;
30  std::string Value ;
31  };
32 
33  std::ostream& operator<< ( std::ostream& s, Expression& e ) ;
34 
39  class Tokenizer{
40 
41  enum state{
42  newtoken,
43  parenthesis,
44  operation
45  } ;
46 
47  std::vector< Expression >& _tokens ;
48  char _last ;
49  bool needToken ;
50  int openPar ;
51  int closedPar ;
52  state _state ;
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 == '(' )
83  _state = parenthesis ;
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 
130  ~Tokenizer(){
131  }
132 
133  std::vector<Expression> & result() {
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 
177  ConditionsMap _condMap{};
178  ResultMap _resultMap{};
179 
180  } ;
181 } // end namespace
182 
183 #endif
184 
185 
186 
void clear()
Clear all boolean values.
Definition: LogicalExpressions.cc:34
void setValue(const std::string &key, bool val)
Set the the boolean value for the given key.
Definition: LogicalExpressions.cc:102
bool getValue(const std::string &key)
helper function for finding return values, that actually have been set by their corresponding process...
Definition: LogicalExpressions.cc:113
virtual ~LogicalExpressions()
Virtual d&#39;tor.
Definition: LogicalExpressions.h:152
Helper class for LogicalExpressions that splits the expression into subexpressions - needs to be apll...
Definition: LogicalExpressions.h:39
Helper struct for LogicalExpression.
Definition: LogicalExpressions.h:22
Helper class that holds named boolean values and named conditions that are expressions of these value...
Definition: LogicalExpressions.h:143
bool expressionIsTrue(const std::string &expression)
True if the given expression is true with the current values.
Definition: LogicalExpressions.cc:51
LogicalExpressions()
C&#39;tor.
Definition: LogicalExpressions.cc:20
bool conditionIsTrue(const std::string &name)
True if the named condition (stored with addCondition) is true with the current values.
Definition: LogicalExpressions.cc:46
void operator()(const char &c)
Save the current character and change state if necessary.
Definition: LogicalExpressions.h:63
void addCondition(const std::string &name, const std::string &expression)
Add a new named logical expression formed out of [!,(,&amp;&amp;,||,),value], e.g.
Definition: LogicalExpressions.cc:28