MarlinMT  0.1.0
LogicalExpressions.cc
Go to the documentation of this file.
2 #include "marlinmt/Exceptions.h"
3 #include "marlinmt/Logging.h"
4 
5 #include <iostream>
6 #include <sstream>
7 #include <algorithm>
8 
9 
10 namespace marlinmt{
11 
12  std::ostream& operator<< ( std::ostream& s, Expression& e ) {
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 
33  for( ResultMap::iterator it = _resultMap.begin() ; it != _resultMap.end() ; it++){
34  it->second = false ;
35  }
36  setValue("true",true);
37  setValue("True",true);
38  setValue("false",false);
39  setValue("False",false);
40  }
41 
42  bool LogicalExpressions::conditionIsTrue( const std::string& name ) const {
43  auto iter = _condMap.find( name ) ;
44  // RE: This method is now const. The old logic of
45  // a condition not in the map was to return true.
46  // Keep this logic here
47  if( _condMap.end() == iter ) {
48  return true ;
49  }
50  return expressionIsTrue( iter->second ) ;
51  }
52 
53  bool LogicalExpressions::expressionIsTrue( const std::string& expression ) const {
54  std::vector<Expression> tokens ;
55  Tokenizer t( tokens ) ;
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  if( tokens[0].isNot ) {
63  return not getValue( tokens[0].Value ) ;
64  }
65  else {
66  return getValue( tokens[0].Value ) ;
67  }
68  }
69  bool returnVal = true ;
70  for( auto it = tokens.begin() ; it != tokens.end() ; it++ ) {
71  bool tokenValue = expressionIsTrue( it->Value ) ;
72  if( it->isNot ) {
73  tokenValue = ! tokenValue ;
74  }
75  if( it->Operation == Expression::AND ) {
76  returnVal &= tokenValue ;
77  }
78  else {
79  returnVal |= tokenValue ;
80  }
81  }
82  return returnVal ;
83  }
84 
85  void LogicalExpressions::setValue( const std::string& key, bool val ) {
86  _resultMap[ key ] = val ;
87  }
88 
89  bool LogicalExpressions::getValue( const std::string& key) const {
90  auto it = _resultMap.find( key ) ;
91  if (it == _resultMap.end() ) {
92  std::ostringstream error;
93  error << "LogicalExpressions::getValue(): key \"" << key << "\" not found. Bad processor condition?\n";
94  //fg: debug:
95  for( auto iter = _resultMap.begin() ; iter != _resultMap.end() ; ++iter ){
96  streamlog_out( DEBUG ) << " key : " << iter->first << " val: " << iter->second << std::endl ;
97  }
98  throw marlinmt::ParseException( error.str() );
99  }
100  return it->second;
101  }
102 
103 }
void clear()
Clear all boolean values.
std::ostream & operator<<(std::ostream &stream, const Configuration &cfg)
Stream operator.
bool getValue(const std::string &key) const
helper function for finding return values, that actually have been set by their corresponding process...
Helper struct for LogicalExpression.
void addCondition(const std::string &name, const std::string &expression)
Add a new named logical expression formed out of [!,(,&&,||,),value], e.g.
Helper class for LogicalExpressions that splits the expression into subexpressions - needs to be apll...
void setValue(const std::string &key, bool val)
Set the the boolean value for the given key.
bool expressionIsTrue(const std::string &expression) const
True if the given expression is true with the current values.
bool conditionIsTrue(const std::string &name) const
True if the named condition (stored with addCondition) is true with the current values.