[go: up one dir, main page]

Menu

[925ebb]: / OptFrame / Action.hpp  Maximize  Restore  History

Download this file

280 lines (200 with data), 5.9 kB

  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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// OptFrame - Optimization Framework
// Copyright (C) 2009-2015
// http://optframe.sourceforge.net/
//
// This file is part of the OptFrame optimization framework. This framework
// is free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License v3 as published by the
// Free Software Foundation.
// This framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License v3 for more details.
// You should have received a copy of the GNU Lesser General Public License v3
// along with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
#ifndef OPTFRAME_ACTION_HPP_
#define OPTFRAME_ACTION_HPP_
#include <cstdlib>
#include <iostream>
#include <vector>
#include "Scanner++/Scanner.h"
#include "Component.hpp"
#include "Solution.hpp"
#include "Evaluation.hpp"
//#include "Command.hpp"
using namespace std;
using namespace scannerpp;
template<class R, class ADS> class HeuristicFactory;
namespace optframe
{
template<class R, class ADS = OPTFRAME_DEFAULT_ADS>
class Action
{
public:
virtual ~Action()
{
}
virtual string usage() = 0;
virtual bool handleComponent(string type) = 0;
virtual bool handleComponent(Component& component) = 0;
virtual bool handleAction(string action) = 0;
virtual bool doAction(string content, HeuristicFactory<R, ADS>& hf, map<string, string>& dictionary, map<string, vector<string> >& ldictionary) = 0;
virtual bool doCast(string component, int id, string type, string variable, HeuristicFactory<R, ADS>& hf, map<string, string>& d) = 0;
static bool addAndRegister(Scanner& scanner, Component& comp, HeuristicFactory<R, ADS>& hf, map<string, string>& d)
{
int index = hf.addComponent(comp);
if (index == -1)
return false;
if (scanner.hasNext())
{
string varName = scanner.next();
stringstream sscomp;
sscomp << comp.id() << " " << index;
d[varName] = sscomp.str(); // TODO: fix!!
return true;
//return Command<R, ADS>::defineText(varName, sscomp.str(), d);
}
return true;
}
static bool registerText(Scanner& scanner, string value, map<string, string>& d)
{
if (scanner.hasNext())
{
string varName = scanner.next();
d[varName] = value; // TODO: fix!!
return true;
//return Command<R, ADS>::defineText(varName, sscomp.str(), d);
}
else
{
cout << "Action error: no variable to store value '" << value << "'" << endl;
return false;
}
}
static string formatDouble(double d)
{
stringstream ss;
ss << fixed;
ss << d;
return ss.str();
}
static string formatInt(int i)
{
stringstream ss;
ss << i;
return ss.str();
}
static string formatBool(bool b)
{
if(b)
return "true";
else
return "false";
}
};
template<class R, class ADS = OPTFRAME_DEFAULT_ADS>
class ComponentAction: public Action<R, ADS>
{
public:
virtual ~ComponentAction()
{
}
virtual string usage()
{
return "OptFrame: idx log output_variable\nOptFrame: idx print";
}
virtual bool handleComponent(string type)
{
return Component::compareBase(Component::idComponent(), type);
}
virtual bool handleComponent(Component& component)
{
return component.compatible(Component::idComponent());
}
virtual bool handleAction(string action)
{
return (action == "log") || (action == "print") || (action == "setVerboseLevel") || (action == "getVerboseLevel");
}
virtual bool doCast(string component, int id, string type, string variable, HeuristicFactory<R, ADS>& hf, map<string, string>& d)
{
if(!handleComponent(type))
{
cout << "ComponentAction::doCast error: can't handle component type '" << type << " " << id << "'" << endl;
return false;
}
Component* comp = hf.components[component].at(id);
if(!comp)
{
cout << "ComponentAction::doCast error: NULL component '" << component << " " << id << "'" << endl;
return false;
}
// cast object to upper base
if(!Component::compareBase(comp->id(), type))
{
cout << "ComponentAction::doCast error: component '" << comp->id() << " is not base of " << type << "'" << endl;
return false;
}
// cast object to lower type
Component* final = (Component*) comp;
// remove old component from factory
hf.components[component].at(id) = NULL;
// add new component
Scanner scanner(variable);
return ComponentAction<R, ADS>::addAndRegister(scanner, *final, hf, d);
}
virtual bool doAction(string content, HeuristicFactory<R, ADS>& hf, map<string, string>& dictionary, map<string, vector<string> >& ldictionary)
{
//cout << "Evaluation::doAction '" << content << "'" << endl;
Scanner scanner(content);
if (!scanner.hasNext())
return false;
Component* c;
hf.assign(c, scanner.nextInt(), scanner.next());
if (!c)
return false;
if (!scanner.hasNext())
return false;
string action = scanner.next();
if (!handleAction(action))
return false;
if (action == "log")
{
if (!scanner.hasNext())
return false;
string var = scanner.next();
stringstream ss;
ss << c->log();
dictionary[var] = ss.str();
return true;
}
if (action == "print")
{
c->print();
return true;
}
if (action == "setVerboseLevel")
{
if (!scanner.hasNext())
return false;
int verboseLevel = scanner.nextInt();
c->setMessageLevel(verboseLevel);
return true;
}
if (action == "getVerboseLevel")
{
if (!scanner.hasNext())
return false;
string var = scanner.next();
stringstream ss;
ss << c->getVerboseLevel();
dictionary[var] = ss.str();
return true;
}
// no action found!
return false;
}
};
}
#endif /* OPTFRAME_ACTION_HPP_ */