Blame view

src/expressionParser/ExpressionPrinter.hh 8.28 KB
a7f2648e   Benjamin Renard   Parser: First imp...
1
2
3
4
5
#ifndef EXPRESSIONPRINTER_HH_
#define EXPRESSIONPRINTER_HH_

#include "ExpressionContainer.hh"

04d84599   Benjamin Renard   Parser: Add suppo...
6
7
#include <sstream>

c1f4db8e   Benjamin Renard   Add process to te...
8
9
//#define ADD_DEBUG_TAG

a7f2648e   Benjamin Renard   Parser: First imp...
10
11
12
13
namespace AMDA {
namespace parser {
namespace Expression {

47755d13   Benjamin Renard   Minor changes
14
class ExpressionPrinter : public boost::static_visitor<void>
a7f2648e   Benjamin Renard   Parser: First imp...
15
{
47755d13   Benjamin Renard   Minor changes
16
public:
a2df6c28   Benjamin Renard   Parser: Retrieve ...
17
18
19
20
	ExpressionPrinter(std::ostream& os) : _os(os), _pParamsList(NULL) {}

	ExpressionPrinter(std::ostream& os, std::vector<std::string>* pParamsList) : _os(os), _pParamsList(pParamsList) {}

c1f4db8e   Benjamin Renard   Add process to te...
21
	std::ostream& _os;
a7f2648e   Benjamin Renard   Parser: First imp...
22

a2df6c28   Benjamin Renard   Parser: Retrieve ...
23
24
	std::vector<std::string>* _pParamsList;

c1f4db8e   Benjamin Renard   Add process to te...
25
26
27
28
29
30
	void operator()(const var& v) const {
		#ifdef ADD_DEBUG_TAG
		_os << "@VAR@";
		#endif
		_os << v;
	}
a7f2648e   Benjamin Renard   Parser: First imp...
31

c1f4db8e   Benjamin Renard   Add process to te...
32
33
34
35
36
37
38
39
40
41
	void operator()(const BinaryOperation& b) const {
		#ifdef ADD_DEBUG_TAG
		_os << "@BINOP@";
		#endif
		_os << "(";
		boost::apply_visitor(*this, b.l);
		_os << b.op.kernelSymbol;
		boost::apply_visitor(*this, b.r);
		_os << ")";
	}
a7f2648e   Benjamin Renard   Parser: First imp...
42

c1f4db8e   Benjamin Renard   Add process to te...
43
44
45
46
47
48
	void operator()(const UnaryOperation& u) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@UNOP@";
		#endif
		_os << u.op.kernelSymbol;
		boost::apply_visitor(*this, u.e);
ef417f05   Benjamin Renard   Improve function ...
49
	}
c1f4db8e   Benjamin Renard   Add process to te...
50

02869c99   Benjamin Renard   Improve parse of ...
51
52
53
54
55
56
57
58
59
60
61
	void operator()(const PowerOperation& p) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@POWER@";
		#endif
		_os << "pow(";
		boost::apply_visitor(*this, p.l);
		_os << ",";
		boost::apply_visitor(*this, p.r);
		_os << ")";
	}

c1f4db8e   Benjamin Renard   Add process to te...
62
63
64
65
66
	void operator()(const FunctionOperation& f) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@FUNC@";
		#endif
		_os << f.func << "(";
e8489ddc   Benjamin Renard   Fix printer for a...
67
		bool isProcess = (!f.func.empty() && (f.func[0] == '#'));
c1f4db8e   Benjamin Renard   Add process to te...
68
69
		for (unsigned int i = 0; i < f.args.size(); ++i) {
			if (i != 0) {
e8489ddc   Benjamin Renard   Fix printer for a...
70
				_os << (isProcess ? ";" : ",");
c1f4db8e   Benjamin Renard   Add process to te...
71
72
73
74
			}
			boost::apply_visitor(*this, f.args[i]);
		}
		_os << ")";
02869c99   Benjamin Renard   Improve parse of ...
75
	}
e33239bf   Benjamin Renard   Parse parameters
76

02869c99   Benjamin Renard   Improve parse of ...
77
78
79
80
	void operator()(const ParameterDef& p) const{
		#ifdef ADD_DEBUG_TAG
		_os << "@PARAM@";
		#endif
e8489ddc   Benjamin Renard   Fix printer for a...
81

a2df6c28   Benjamin Renard   Parser: Retrieve ...
82
		if (_pParamsList != NULL) {
aae52524   Benjamin Renard   Parser: Write res...
83
84
85
			if (std::find(_pParamsList->begin(), _pParamsList->end(), p.param) == _pParamsList->end()) {
				_pParamsList->push_back(p.param);
			}
a2df6c28   Benjamin Renard   Parser: Retrieve ...
86
87
		}

04d84599   Benjamin Renard   Parser: Add suppo...
88
89
90
91
92
93
94
		if (
			p.components.empty() ||
			((p.components.size() == 1) && (p.components[0].type == ParameterDef::ComponentType::CT_ALL)) ||
			((p.components.size() == 2) && (p.components[0].type == ParameterDef::ComponentType::CT_ALL) && (p.components[1].type == ParameterDef::ComponentType::CT_ALL)))
		{
			// All components
			_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
95
		}
04d84599   Benjamin Renard   Parser: Add suppo...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
		else if ((p.components.size() == 1) ||
			((p.components.size() == 2) && (p.components[1].type == ParameterDef::ComponentType::CT_ALL)))
		{
			//only first component defined
			switch (p.components[0].type) {
			case ParameterDef::ComponentType::CT_INDEX:
				_os << printParam(p.param);
				_os << printComponentIndex(p.components[0].index);
				break;
			case ParameterDef::CT_RANGE_VALUES:
			case ParameterDef::CT_RANGE_INDEXES:
				{
					std::stringstream ss;
					ss << printParam(p.param);
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
110
					_os << printSumIntoTable(p.components[0].type, ss.str(), 0, p.components[0].min, p.components[0].max, p.components[0].processType);
04d84599   Benjamin Renard   Parser: Add suppo...
111
					break;
e8489ddc   Benjamin Renard   Fix printer for a...
112
				}
04d84599   Benjamin Renard   Parser: Add suppo...
113
114
115
			default:
				//Unknown case => return parameter
				_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
116
			}
04d84599   Benjamin Renard   Parser: Add suppo...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
		}
		else if ((p.components.size() == 2) && (p.components[0].type == ParameterDef::ComponentType::CT_ALL))
		{
			//only second component defined
			switch (p.components[1].type) {
			case ParameterDef::ComponentType::CT_INDEX:
				{
				_os << printParamColumn(p.param, p.components[1].index);
				break;
				}
			case ParameterDef::CT_RANGE_VALUES:
			case ParameterDef::CT_RANGE_INDEXES:
				{
					std::stringstream ss;
					ss << printParam(p.param);
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
132
					_os << printSumIntoTable(p.components[1].type, ss.str(), 1, p.components[1].min, p.components[1].max, p.components[1].processType);
04d84599   Benjamin Renard   Parser: Add suppo...
133
134
135
136
137
				}
				break;
			default:
				//Unknown case => return parameter
				_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
138
			}
04d84599   Benjamin Renard   Parser: Add suppo...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
		}
		else {
			//two components defined
			switch (p.components[0].type) {
			case ParameterDef::ComponentType::CT_INDEX:
				{
					switch (p.components[1].type) {
					case ParameterDef::ComponentType::CT_INDEX:
						_os << printParam(p.param);
						_os << printComponentIndex(p.components[0].index);
						_os << printComponentIndex(p.components[1].index);
						break;
					case ParameterDef::CT_RANGE_VALUES:
					case ParameterDef::CT_RANGE_INDEXES:
						{
							std::stringstream ss;
							ss << printParam(p.param);
							ss << printComponentIndex(p.components[0].index);
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
157
							_os << printSumIntoTable(p.components[1].type, ss.str(), 1, p.components[1].min, p.components[1].max, p.components[1].processType);
04d84599   Benjamin Renard   Parser: Add suppo...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
						}
						break;
					default:
						//Unknown case => return parameter
						_os << printParam(p.param);
					}
				}
				break;
			case ParameterDef::CT_RANGE_VALUES:
			case ParameterDef::CT_RANGE_INDEXES:
				{
					switch (p.components[1].type) {
					case ParameterDef::ComponentType::CT_INDEX:
						{
							std::stringstream ss;
							ss << printParamColumn(p.param,p.components[1].index);
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
174
							_os << printSumIntoTable(p.components[0].type, ss.str(), 0, p.components[0].min, p.components[0].max, p.components[0].processType);
04d84599   Benjamin Renard   Parser: Add suppo...
175
176
177
178
179
180
181
182
183
184
185
						}
						break;
					case ParameterDef::CT_RANGE_VALUES:
					case ParameterDef::CT_RANGE_INDEXES:
						if (p.components[0].type != p.components[1].type) {
							//Cannot mix range values and range indexes => return parameter
							_os << printParam(p.param);
						}
						else {
							std::stringstream ss;
							ss << printParam(p.param);
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
186
							_os << printSumIntoTable(p.components[0].type, ss.str(), 0, p.components[0].min, p.components[0].max, p.components[0].processType, p.components[1].min, p.components[1].max, p.components[1].processType);
04d84599   Benjamin Renard   Parser: Add suppo...
187
188
189
190
191
192
193
194
195
196
197
						}
						break;
					default:
						//Unknown case => return parameter
						_os << printParam(p.param);
					}
				}
				break;
			default:
				//Unknown case => return parameter
				_os << printParam(p.param);
e8489ddc   Benjamin Renard   Fix printer for a...
198
199
			}
		}
04d84599   Benjamin Renard   Parser: Add suppo...
200
	}
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
       ParameterDef::ProcessType strToProcessType(const std::string& type) const{
           if(type == "all") return ParameterDef::ProcessType::ALL;
           else if(type == "sum") return ParameterDef::ProcessType::SUM;
           else if(type == "all") return ParameterDef::ProcessType::AVERAGE;
           else return ParameterDef::ProcessType::NOT_KNOWN;
         }
        std::string pocessTypeToStr(ParameterDef::ProcessType type) const{
             switch(type){
                case ParameterDef::ProcessType::ALL: return "all";
                case ParameterDef::ProcessType::SUM: return "sum";
                case ParameterDef::ProcessType::AVERAGE: return "average";
                default: return "Unknown type";
             }
         }

e8489ddc   Benjamin Renard   Fix printer for a...
216

04d84599   Benjamin Renard   Parser: Add suppo...
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
private:
	std::string printParam(std::string name) const {
		std::stringstream ss;
		ss << "$";
		ss << name;
		return ss.str();
	}

	std::string printParamColumn(std::string name, int col) const {
		std::stringstream ss;
		ss << "(";
		ss << printParam(name);
		ss << ").getColumn(";
		ss << col;
		ss << ")";
		return ss.str();
	}

	std::string printComponentIndex(int index) const {
		std::stringstream ss;
		ss << "[";
		ss << index;
		ss << "]";
		return ss.str();
	}

	

93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
245
246
	std::string printSumIntoTable(ParameterDef::ComponentType type, std::string expression, int relatedDim1, float min, float max, ParameterDef::ProcessType process_type1,
 float min2 = NAN, float max2 = NAN, ParameterDef::ProcessType process_type2=ParameterDef::ProcessType::NOT_KNOWN) const {
04d84599   Benjamin Renard   Parser: Add suppo...
247
248
249
		std::stringstream ss;
		bool forceInt = false;
		if (type == ParameterDef::ComponentType::CT_RANGE_INDEXES) {
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
250
			ss << "#table_indexes";
04d84599   Benjamin Renard   Parser: Add suppo...
251
252
253
			forceInt = true;
		}
		else {
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
254
			ss << "#table_range";
04d84599   Benjamin Renard   Parser: Add suppo...
255
256
257
258
259
260
261
262
263
		}
		ss << "(";
		ss << expression;
		ss << ";";
		ss << relatedDim1;
		ss << ";";
		ss << (forceInt ? (int)min : min);
		ss << ";";
		ss << (forceInt ? (int)max : max);
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
264
                                        ss << pocessTypeToStr(process_type1);
04d84599   Benjamin Renard   Parser: Add suppo...
265
266
267
268
269
		if (!std::isnan(min2) && !std::isnan(max2)) {
			ss << ";";
			ss << (forceInt ? (int)min2 : min2);
			ss << ";";
			ss << (forceInt ? (int)max2 : max2);
93dc5c5d   Hacene SI HADJ MOHAND   expression parser ok
270
                                        ss << pocessTypeToStr(process_type2);
04d84599   Benjamin Renard   Parser: Add suppo...
271
272
273
		}
		ss << ")";
		return ss.str();
02869c99   Benjamin Renard   Improve parse of ...
274
	}
a7f2648e   Benjamin Renard   Parser: First imp...
275
276
};

47755d13   Benjamin Renard   Minor changes
277
std::ostream& operator<<(std::ostream& os, const ExpressionContainer& e)
a7f2648e   Benjamin Renard   Parser: First imp...
278
{
47755d13   Benjamin Renard   Minor changes
279
	boost::apply_visitor(ExpressionPrinter(os), e);
a7f2648e   Benjamin Renard   Parser: First imp...
280
281
	return os;
}
a2df6c28   Benjamin Renard   Parser: Retrieve ...
282
283
284
285
286
287

void getExpressionInfo(const ExpressionContainer& e, std::string& kernelExpression, std::vector<std::string>& paramsList) {
	std::stringstream ss;
        boost::apply_visitor(ExpressionPrinter(ss, &paramsList), e);
	kernelExpression = ss.str();
}
a7f2648e   Benjamin Renard   Parser: First imp...
288
289
290
291
292
293
294
    
} /* namespace Expression */
} /* namespace parser */
} /* namespace AMDA */


#endif