Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LogicalExpressions.cc
Go to the documentation of this file.
2 #include "marlin/Exceptions.h"
3 
4 #include <iostream>
5 #include <sstream>
6 #include <algorithm>
7 
8 
9 namespace marlin{
10 
12 
13  if( e.Operation == Expression::AND ) s << " && " ;
14  else s << " || " ;
15  if( e.isNot ) s << " ! " ;
16  s << " [ " << e.Value << " ] " << std::endl ;
17  return s ;
18  }
19 
21  setValue("true",true);
22  setValue("True",true);
23  setValue("false",false);
24  setValue("False",false);
25  }
26 
27 
28  void LogicalExpressions::addCondition( const std::string& name, const std::string& expression ) {
29  _condMap[ name ] = expression ;
30 
31 // std::cout << " LogicalExpressions::addCondition( " << name << ", " << expression << " ) " << std::endl ;
32  }
33 
35 
36  for( ResultMap::iterator it = _resultMap.begin() ; it != _resultMap.end() ; it++){
37  it->second = false ;
38  }
39  setValue("true",true);
40  setValue("True",true);
41  setValue("false",false);
42  setValue("False",false);
43 // std::cout << " LogicalExpressions::clear() " << std::endl ;
44  }
45 
47 
48  return expressionIsTrue( _condMap[ name ] ) ;
49  }
50 
52 
54  Tokenizer t( tokens ) ;
55 
56  std::for_each( expression.begin(),expression.end(), t ) ;
57 
58  // atomic expression
59  if( tokens.size() == 1
60  && tokens[0].Value.find('&') == std::string::npos
61  && tokens[0].Value.find('|') == std::string::npos ) {
62 
63  if( tokens[0].isNot ) {
64 // std::cout << " evaluated !"<< tokens[0].Value << " to " << ! _resultMap[ tokens[0].Value ] << std::endl ;
65 // return ! _resultMap[ tokens[0].Value ] ;
66  return ! getValue( tokens[0].Value ) ;
67  }
68  else {
69 // if( _resultMap.find( tokens[0].Value ) == _resultMap.end() )
70 // std::cout << " error in result map : " << tokens[0].Value << " not found !" << std::endl ;
71 // std::cout << " evaluated "<< tokens[0].Value << " to " << _resultMap[ tokens[0].Value ] << std::endl ;
72 // return _resultMap[ tokens[0].Value ] ;
73  return getValue( tokens[0].Value ) ;
74  }
75  }
76 
77  bool returnVal = true ;
78 
79  for( std::vector<Expression>::iterator it = tokens.begin() ; it != tokens.end() ; it++ ){
80 
81  bool tokenValue = expressionIsTrue( it->Value ) ;
82 
83  if( it->isNot )
84  tokenValue = ! tokenValue ;
85 
86  if( it->Operation == Expression::AND )
87  returnVal &= tokenValue ;
88  else
89  returnVal |= tokenValue ;
90  }
91 
92 // std::cout << " expression given : " << expression << std::endl ;
93 // std::cout << " evaluated by parser:" << std::endl ;
94 // for( std::vector<Expression>::iterator it = tokens.begin() ; it != tokens.end() ; it++ ){
95 // std::cout << *it ;
96 // }
97 // std::cout << " Result " << returnVal << std::endl << std::endl ;
98 
99  return returnVal ;
100  }
101 
102  void LogicalExpressions::setValue( const std::string& key, bool val ) {
103 
104 // std::cout << " LogicalExpressions::setValue() " << key << " - " << val << std::endl ;
105 
106  _resultMap[ key ] = val ;
107  }
108 
109 // ConditionsMap _condMap ;
110 // ResultMap _resultMap ;
111 
112 
114  ResultMap::iterator it = _resultMap.find( key );
115  if (it == _resultMap.end() ) {
116  std::ostringstream error;
117  error << "LogicalExpressions::getValue(): key \"" << key << "\" not found. Bad processor condition?\n";
118 
119  //fg: debug:
120  for( it = _resultMap.begin() ; it != _resultMap.end() ; ++it ){
121 
122  streamlog_out( DEBUG ) << " key : " << it->first << " val: " << it->second << std::endl ;
123  }
124 
125  throw marlin::ParseException( error.str() );
126  }
127  return it->second;
128  }
129 
130 }
131 
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...
ParseException used for parse errors, e.g.
Definition: Exceptions.h:16
T endl(T...args)
Helper class for LogicalExpressions that splits the expression into subexpressions - needs to be apll...
T end(T...args)
Helper struct for LogicalExpression.
STL class.
bool expressionIsTrue(const std::string &expression)
True if the given expression is true with the current values.
T find(T...args)
T size(T...args)
STL class.
T begin(T...args)
bool conditionIsTrue(const std::string &name)
True if the named condition (stored with addCondition) is true with the current values.
T for_each(T...args)
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)