OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Expression.h
Go to the documentation of this file.
1 #ifndef __EXPRESSION_H__
2 #define __EXPRESSION_H__
3 
4 #include <map>
5 #include <set>
6 #include <string>
7 
8 #include "Util/Types.h"
10 
16 
17 #include <boost/function.hpp>
18 #include <boost/lexical_cast.hpp>
19 #include <boost/variant/get.hpp>
20 #include <boost/variant/variant.hpp>
21 #include "boost/smart_ptr.hpp"
22 #include "boost/tuple/tuple.hpp"
23 #include "boost/algorithm/string.hpp"
24 
25 
26 typedef std::map<std::string, double> variableDictionary_t;
27 typedef std::map<std::string, client::function::type> functionDictionary_t;
28 
29 
30 class Expression;
31 namespace Expressions {
32 
34  typedef Expression Expr_t;
35 
36  // result of an evaluated expression
37  typedef boost::tuple<double, bool> Result_t;
41  };
42 
43  //FIXME: this actually should be a map of type (name, fusion::vector<...>)
45  typedef std::map<std::string, Expressions::Expr_t*> Named_t;
46 
47  //XXX name of one single expression
48  typedef std::pair<std::string, Expressions::Expr_t*> SingleNamed_t;
49 
53  EQ, // ==
54  NOT_EQ, // !=
55  INEQ_LHS, // <
56  INEQ_LHS_EQ, // <=
57  INEQ_RHS, // >
59  };
60 
61 }
62 
64 
75 class Expression {
76 
77 public:
78 
80  {}
81 
82  Expression(std::string expr)
83  : expr_(expr)
84  {
86  functionDictionary_t global_funcs = GlobalFunctions::get();
87  known_expr_funcs_.insert(global_funcs.begin(), global_funcs.end());
88  parse();
89  }
90 
91  Expression(std::string expr, functionDictionary_t known_expr_funcs)
92  : expr_(expr)
93  , known_expr_funcs_(known_expr_funcs)
94  {
96  functionDictionary_t global_funcs = GlobalFunctions::get();
97  known_expr_funcs_.insert(global_funcs.begin(), global_funcs.end());
98  parse();
99  }
100 
101  virtual ~Expression()
102  {}
103 
104 
105  std::set<std::string> getReqVars() const { return vars_; }
106  std::set<std::string> getReqFuncs() const { return funcs_; }
107  std::string toString() const { return expr_; }
109 
112 
115 
116  iterator_type iter = expr_.begin();
117  iterator_type end = expr_.end();
118  client::error_handler<iterator_type> error_handler(iter, end);
119  client::code_gen::StackEvaluator evaluator(error_handler);
120  evaluator.registerVariables(vars);
122 
123  double result = 0.0;
124  bool valid = false;
125  if (evaluator(ast_)) {
126  result = evaluator.result();
127  valid = true;
128  }
129 
130  return boost::make_tuple(result, valid);
131  }
132 
133 
134 private:
135 
136  typedef std::string::const_iterator iterator_type;
138 
139  std::set<std::string> vars_;
140  std::set<std::string> funcs_;
141 
142  std::string expr_;
144 
146 
148 
149  std::string op = expr_;
150 
151  if(boost::find_first(op, "=="))
153  else if(boost::find_first(op, "!="))
155  else if(boost::find_first(op, "<="))
157  else if(boost::find_first(op, "<"))
159  else if(boost::find_first(op, ">="))
161  else if(boost::find_first(op,">"))
163  else
165  }
166 
167  void parse() {
168 
169  iterator_type iter = expr_.begin();
170  iterator_type end = expr_.end();
171 
172  client::error_handler<iterator_type> error_handler(iter, end);
173  client::parser::expression<iterator_type> expression(error_handler);
175  client::code_gen::requirements requirements(error_handler);
176 
177  bool success = phrase_parse(iter, end, expression, skipper, ast_);
178 
179  if (!success || iter != end) {
180  std::cout << "Parsing failed!" << std::endl;
181  std::string here = (iter != end ? std::string(iter, end): expr_);
182  throw new OptPilotException("Expression::parse()",
183  "Parsing failed here: " + here + "!");
184  }
185 
186  // store the functions and variables required to evaluate this
187  // expression
188  if (requirements(ast_)) {
189  vars_ = requirements.variables();
190  funcs_ = requirements.functions();
191  }
192  }
193 };
194 
195 #endif
std::set< std::string > getReqFuncs() const
Definition: Expression.h:106
std::pair< std::string, Expressions::Expr_t * > SingleNamed_t
Definition: Expression.h:48
std::string::const_iterator iterator_type
Definition: Expression.h:136
std::string toString() const
Definition: Expression.h:107
Expression(std::string expr)
Definition: Expression.h:82
std::set< std::string > functions()
Expression to be evaluated in the framework.
Definition: Expression.h:75
virtual ~Expression()
Definition: Expression.h:101
std::set< std::string > vars_
Definition: Expression.h:139
void determineConstrOperator()
Definition: Expression.h:147
functionDictionary_t known_expr_funcs_
Definition: Expression.h:143
std::string expr_
Definition: Expression.h:142
boost::tuple< double, bool > Result_t
Definition: Expression.h:37
void registerFunctions(std::map< std::string, client::function::type > functions)
Definition: evaluator.hpp:56
std::set< std::string > getReqVars() const
Definition: Expression.h:105
Expression Expr_t
type of an expression
Definition: Expression.h:34
Expressions::OperatorType_t type_
Definition: Expression.h:145
Expressions::OperatorType_t getOpType() const
get operator type present (if expression is constraint)
Definition: Expression.h:111
void parse()
Definition: Expression.h:167
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition: Expression.h:45
OperatorType_t
distinguish different constraints
Definition: Expression.h:51
Expressions::Result_t evaluate(variableDictionary_t vars)
evaluate an expression given a value dictionary of free variables
Definition: Expression.h:114
Expression(std::string expr, functionDictionary_t known_expr_funcs)
Definition: Expression.h:91
std::set< std::string > variables()
client::ast::expression ast_
Definition: Expression.h:137
functionDictionary_t getRegFuncs() const
Definition: Expression.h:108
std::set< std::string > funcs_
Definition: Expression.h:140
void registerVariables(std::map< std::string, double > variableDictionary)
Definition: evaluator.hpp:61
std::map< std::string, double > variableDictionary_t
Definition: Expression.h:26
std::map< std::string, client::function::type > functionDictionary_t
Definition: Expression.h:27
Inform & endl(Inform &inf)
Definition: Inform.cpp:42