summaryrefslogtreecommitdiff
path: root/impl/Expression.hpp
blob: dcf72012420fb6e97a98390daad5bc21c3e703ce (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#ifndef EXPRESSION_HPP
#define EXPRESSION_HPP

#include <string>
#include <vector>
#include <sstream>
#include <set>
#include "IdMap.hpp"
#include "Operator.hpp"

template<typename Domain>
struct VariableAssignment;

template<typename Domain>
struct MaxStrategy;

template<typename Domain>
struct Variable;

template<typename Domain>
struct MaxExpression;

template<typename Domain>
struct Expression {
  virtual ~Expression() { }

  virtual const MaxExpression<Domain>* toMaxExpression() const {
    return NULL;
  }

  virtual MaxExpression<Domain>* toMaxExpression() {
    return NULL;
  }

  virtual Domain eval(VariableAssignment<Domain>&) const = 0;
  virtual Domain eval(VariableAssignment<Domain>& rho,
                      const MaxStrategy<Domain>&) const {
    return eval(rho);
  }
  virtual Domain eval(VariableAssignment<Domain>& rho,
                      MaxStrategy<Domain>&) const {
    return eval(rho);
  }

  virtual void mapTo(Variable<Domain>&, IdMap<MaxExpression<Domain>, Variable<Domain>*>&) const {
  }

  virtual void subMaxExprs(std::set<const MaxExpression<Domain>*>&) const {
  }

  virtual void print(std::ostream&) const = 0;
};

template<typename Domain>
struct Constant : public Expression<Domain> {
  Constant(const Domain& value)
    : _value(value) { }

  virtual Domain eval(VariableAssignment<Domain>&) const {
    return _value;
  }

  void print(std::ostream& cout) const {
    cout << _value;
  }

  private:
  Domain _value;
};

template<typename Domain>
struct Variable : public Expression<Domain> {
  Variable(const unsigned int& id, const std::string& name)
    : _id(id), _name(name) { }

  unsigned int id() const {
    return _id;
  }
  std::string name() const {
    return _name;
  }

  virtual Domain eval(VariableAssignment<Domain>& rho) const {
    return rho[*this];
  }

  void print(std::ostream& cout) const {
    cout << _name;
  }

  private:
  const unsigned int _id;
  const std::string _name;
};

template<typename Domain>
struct OperatorExpression : public Expression<Domain> {
  OperatorExpression(const Operator<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
    : _operator(op), _arguments(arguments) { }

  virtual Domain eval(VariableAssignment<Domain>& rho) const {
    std::vector<Domain> argumentValues;
    for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin();
         it != _arguments.end();
         ++it) {
      argumentValues.push_back((*it)->eval(rho));
    }
    return _operator.eval(argumentValues);
  }

  virtual Domain eval(VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const {
    std::vector<Domain> argumentValues;
    for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin();
         it != _arguments.end();
         ++it) {
      argumentValues.push_back((*it)->eval(rho, strat));
    }
    return _operator.eval(argumentValues);
  }

  virtual Domain eval(VariableAssignment<Domain>& rho, MaxStrategy<Domain>& strat) const {
    std::vector<Domain> argumentValues;
    for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin();
         it != _arguments.end();
         ++it) {
      argumentValues.push_back((*it)->eval(rho, strat));
    }
    return _operator.eval(argumentValues);
  }

  std::vector<Expression<Domain>*>& arguments() {
    return _arguments;
  }

  const std::vector<Expression<Domain>*>& arguments() const {
    return _arguments;
  }

  const Operator<Domain>& op() const {
    return _operator;
  }

  virtual void mapTo(Variable<Domain>& v, IdMap<MaxExpression<Domain>, Variable<Domain>*>& m) const {
    for (unsigned int i = 0, length = _arguments.size();
         i < length;
         ++i) {
      _arguments[i]->mapTo(v, m);
    }
  }

  virtual void subMaxExprs(std::set<const MaxExpression<Domain>*>& exprs) const {
    for (unsigned int i = 0, length = _arguments.size();
         i < length;
         ++i) {
      _arguments[i]->subMaxExprs(exprs);
    }
  }

  void print(std::ostream& cout) const {
    cout << _operator << "(";
    for (unsigned int i = 0, length = _arguments.size();
         i < length;
         ++i) {
      if (i > 0)
        cout << ", ";
      cout << *_arguments[i];
    }
    cout << ")";
  }

  private:
  const Operator<Domain>& _operator;
  protected:
  std::vector<Expression<Domain>*> _arguments;
};

template<typename Domain>
struct MaxExpression : public OperatorExpression<Domain> {
  MaxExpression(const unsigned int& id, const Maximum<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
    : OperatorExpression<Domain>(op, arguments), _id(id) { }

  MaxExpression* toMaxExpression() {
    return this;
  }

  const MaxExpression* toMaxExpression() const {
    return this;
  }

  virtual Domain eval(VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const {
    log::fixpoint << "const MaxExpression eval" << std::endl;
    return this->_arguments[strat.get(*this)]->eval(rho, strat);
  }

  virtual Domain eval(VariableAssignment<Domain>& rho, MaxStrategy<Domain>& strat) const {
    log::strategy << "const MaxExpression eval" << std::endl;
    return this->_arguments[strat.get(*this)]->eval(rho, strat);
  }

  unsigned int bestStrategy(VariableAssignment<Domain>& rho, MaxStrategy<Domain>& strat) const {
    unsigned int bestIndex = const_cast<const MaxStrategy<Domain>&>(strat).get(*this);
    Domain bestValue = this->_arguments[bestIndex]->eval(rho, strat);

    for (unsigned int i = 0, length = this->_arguments.size();
         i < length;
         ++i) {
      const Domain value = this->_arguments[i]->eval(rho, strat);
      if (bestValue < value) {
        bestIndex = i;
        bestValue = value;
      }
    }
    return bestIndex;
  }

  virtual void mapTo(Variable<Domain>& v, IdMap<MaxExpression<Domain>,Variable<Domain>*>& m) const {
    m[*this] = &v;
    for (unsigned int i = 0, length = this->_arguments.size();
         i < length;
         ++i) {
      this->_arguments[i]->mapTo(v, m);
    }
  }

  virtual void subMaxExprs(std::set<const MaxExpression<Domain>*>& exprs) const {
    exprs.insert(this);
    for (unsigned int i = 0, length = this->_arguments.size();
         i < length;
         ++i) {
      this->_arguments[i]->subMaxExprs(exprs);
    }
  }

  unsigned int id() const {
    return _id;
  }

  private:
  const unsigned int _id;
};

template<typename T>
std::ostream& operator<<(std::ostream& cout, const Expression<T>& exp) {
  exp.print(cout);
  return cout;
}

#include "VariableAssignment.hpp"

#endif