- Fixed some bugs in command processing, added the configure-cpu-policy command
git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@748 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
1a6805afc4
commit
6150af3d30
8 changed files with 196 additions and 77 deletions
|
@ -24,6 +24,7 @@
|
|||
#include "backend/policy_parameters.hh"
|
||||
#include "backend/history.hh"
|
||||
|
||||
|
||||
#include "text_simulation.hh"
|
||||
|
||||
#include <sstream>
|
||||
|
@ -99,17 +100,31 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
|
|||
{
|
||||
arguments_ignored(arguments);
|
||||
|
||||
PolicyParameters& parameters = get_policy()->get_parameters();
|
||||
// FIXME we should use the current policy to obtain parmaters, this code
|
||||
// is only for testing purposes
|
||||
PolicyParameters parameters;
|
||||
|
||||
p_stdout(_("\nPlease provide a value for each attribute"));
|
||||
p_stdout(_("\nMandatory arguments are marked with an asterisk (*)"));
|
||||
parameters.register_int("a", -50, 50, false, 10);
|
||||
parameters.register_int("b", -2, INT_MAX, true);
|
||||
parameters.register_int("c", INT_MIN, 2, true);
|
||||
|
||||
parameters.register_float("d", -FLT_MAX, FLT_MAX, true);
|
||||
parameters.register_float("e", -5.0f, 50.0f, false, 10);
|
||||
parameters.register_float("f", -FLT_MAX, 2.0f, true);
|
||||
|
||||
parameters.register_string("g", false);
|
||||
parameters.register_string("h", true);
|
||||
parameters.register_string("g", true);
|
||||
|
||||
p_stdout(_("\nPlease provide a value for each attribute:"));
|
||||
p_stdout(_("\nMandatory arguments are marked with an asterisk (*)\n"));
|
||||
|
||||
p_stdout(_("\nInteger arguments:\n"));
|
||||
|
||||
typedef map<ustring, PolicyParameters::Parameter<int> > IntParams;
|
||||
typedef map<ustring, PolicyParameters::Parameter<float> > FloatParams;
|
||||
typedef map<ustring, PolicyParameters::Parameter<ustring> > StringParams;
|
||||
|
||||
// TODO for int and double this code is easily replaced with a call to a
|
||||
// template function. for strings we'll need to specialize it...
|
||||
IntParams int_params = parameters.get_registered_int_parameters();
|
||||
|
||||
for(IntParams::iterator it = int_params.begin(); it != int_params.end();)
|
||||
|
@ -127,7 +142,6 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
|
|||
|
||||
p_stdout(buf.str());
|
||||
|
||||
bool arg_provided = false;
|
||||
bool correct = true;
|
||||
|
||||
ustring input = readline();
|
||||
|
@ -142,7 +156,13 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
|
|||
{
|
||||
try
|
||||
{
|
||||
value = string_to_int(input);
|
||||
value = string_to<int>(input);
|
||||
|
||||
if(value > p.get_upper_bound() || value < p.get_lower_bound())
|
||||
{
|
||||
p_stderr(_("\nERROR: Provided value is out of range"));
|
||||
correct = false;
|
||||
}
|
||||
}
|
||||
catch(domain_error e)
|
||||
{
|
||||
|
@ -150,14 +170,8 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
|
|||
correct = false;
|
||||
}
|
||||
|
||||
if(value > p.get_upper_bound() || value < p.get_lower_bound())
|
||||
{
|
||||
p_stderr(_("\nERROR: Provided value is out of range"));
|
||||
correct = false;
|
||||
}
|
||||
|
||||
if(correct)
|
||||
p.set_value(value);
|
||||
parameters.set_int(p.get_name(), value);
|
||||
}
|
||||
else if(p.is_required())
|
||||
{
|
||||
|
@ -170,41 +184,109 @@ TextSimulation::on_configure_cpu_policy(const Tokens& arguments)
|
|||
++it;
|
||||
}
|
||||
|
||||
//FIXME code for float arguments
|
||||
p_stdout(_("\nFloating-point arguments:\n"));
|
||||
|
||||
// NOTE this piece code is a verbatim copy of the one above.
|
||||
// I tried solving this issue by using templates, but to make
|
||||
// it work will require adding to PolicyParameters a member template
|
||||
// method with 2 specializations...
|
||||
|
||||
// StringParams string_params = parameters->get_registered_string_parameters();
|
||||
//
|
||||
// for(StringParams::iterator it = int_params.begin(); it != int_params.end();)
|
||||
// {
|
||||
// Parameter<ustring> &p = *it;
|
||||
// ustring buf;
|
||||
//
|
||||
// buf = "\n";
|
||||
//
|
||||
// if(p.is_required())
|
||||
// buf += "*";
|
||||
//
|
||||
// p_stdout(buf + p.get_name() + " = ");
|
||||
//
|
||||
// buf = read();
|
||||
//
|
||||
// // FIXME semi-hack, it's a bit overkill to tokenize the string
|
||||
// // to find if it's only composed of white spaces...
|
||||
// // Indedeed there's a pro: by using extensively tokenize() we are more sure
|
||||
// // it's correct ;-)
|
||||
// Tokens tokens = tokenize(input);
|
||||
//
|
||||
// if(tokens.size() == 0 && p.is_required())
|
||||
// p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!"));
|
||||
// else
|
||||
// {
|
||||
// p.set_value(tokens[0]);
|
||||
// ++it;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
FloatParams float_params = parameters.get_registered_float_parameters();
|
||||
|
||||
for(FloatParams::iterator it = float_params.begin(); it != float_params.end();)
|
||||
{
|
||||
PolicyParameters::Parameter<float> &p = it->second;
|
||||
ostringstream buf;
|
||||
|
||||
buf << "\n";
|
||||
|
||||
if(p.is_required())
|
||||
buf << "*";
|
||||
|
||||
buf << p.get_name() << " (range: [" << p.get_lower_bound() << ", " <<
|
||||
p.get_upper_bound() << "] default: " << p.get_default() << ") = ";
|
||||
|
||||
p_stdout(buf.str());
|
||||
|
||||
bool correct = true;
|
||||
|
||||
ustring input = readline();
|
||||
|
||||
float value;
|
||||
|
||||
// FIXME semi-hack, it's a bit overkill to tokenize the string
|
||||
// to find if it's only composed of white spaces...
|
||||
// Indedeed there's a pro: by using extensively tokenize() we are more sure
|
||||
// it's correct ;-)
|
||||
if(tokenize(input).size() > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
value = string_to<float>(input);
|
||||
|
||||
if(value > p.get_upper_bound() || value < p.get_lower_bound())
|
||||
{
|
||||
p_stderr(_("\nERROR: Provided value is out of range"));
|
||||
correct = false;
|
||||
}
|
||||
}
|
||||
catch(domain_error e)
|
||||
{
|
||||
p_stderr(_("\nERROR: Please provide a valid floating-point value"));
|
||||
correct = false;
|
||||
}
|
||||
|
||||
if(correct)
|
||||
parameters.set_float(p.get_name(), value);
|
||||
}
|
||||
else if(p.is_required())
|
||||
{
|
||||
p_stderr(_("\nERROR: This is a mandatory attribute; you MUST provide a valid value!"));
|
||||
|
||||
correct = false;
|
||||
}
|
||||
|
||||
if(correct)
|
||||
++it;
|
||||
}
|
||||
|
||||
p_stdout(_("\nString arguments:\n"));
|
||||
|
||||
StringParams string_params = parameters.get_registered_string_parameters();
|
||||
|
||||
for(StringParams::iterator it = string_params.begin(); it != string_params.end();)
|
||||
{
|
||||
PolicyParameters::Parameter<ustring> &p = it->second;
|
||||
ustring buf;
|
||||
|
||||
buf = "\n";
|
||||
|
||||
if(p.is_required())
|
||||
buf += "*";
|
||||
|
||||
p_stdout(buf + p.get_name() + " = ");
|
||||
|
||||
buf = readline();
|
||||
|
||||
// FIXME semi-hack, it's a bit overkill to tokenize the string
|
||||
// to find if it's only composed of white spaces...
|
||||
// Indedeed there's a pro: by using extensively tokenize() we are more sure
|
||||
// it's correct ;-)
|
||||
Tokens tokens = tokenize(buf);
|
||||
|
||||
if(tokens.size() == 0 && p.is_required())
|
||||
p_stderr(_("\nERROR: This is a mandatory atribute; you MUST provide a valid value!"));
|
||||
else
|
||||
{
|
||||
// FIXME should we insert the entire line here or just a token?
|
||||
parameters.set_string(p.get_name(), buf);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
TextSimulation::on_help(const Tokens& arguments)
|
||||
{
|
||||
|
@ -212,7 +294,7 @@ TextSimulation::on_help(const Tokens& arguments)
|
|||
//make a local copy which we'll probably modify
|
||||
Tokens args = arguments;
|
||||
|
||||
if(args.size() > 1)
|
||||
if(args.size() > 0)
|
||||
{
|
||||
command = args[0].uppercase();
|
||||
args.erase(args.begin());
|
||||
|
@ -222,9 +304,9 @@ TextSimulation::on_help(const Tokens& arguments)
|
|||
|
||||
if(command.size() == 0)
|
||||
p_stdout(_("\nAvaiable commands:\nRUN\nPAUSE\nSTOP\nQUIT\nHELP"
|
||||
"\nGET\nSET\nSHOW\nADD\nREMOVE"
|
||||
"\nGET\nSET\nSHOW\nADD\nREMOVE\nCONFIGURE-CPU-POLICY"
|
||||
"\n\nHELP followed by a command shows help about it."
|
||||
"\n ex. HELP RUN shows help about the command RUN"));
|
||||
"\n ex. HELP RUN shows help about the command RUN\n"));
|
||||
else if(command == "RUN")
|
||||
p_stdout(_("\n-- RUN COMMAND --\nStarts the simulation. It can be "
|
||||
"continuous or step-by-step depending on the mode configured with "
|
||||
|
@ -278,31 +360,31 @@ TextSimulation::on_quit(const Tokens& arguments)
|
|||
void
|
||||
TextSimulation::on_get(const Tokens& arguments)
|
||||
{
|
||||
p_stderr(_("\nFIXME: Not implemented"));
|
||||
p_stderr(_("\nFIXME: Not implemented\n"));
|
||||
}
|
||||
|
||||
void
|
||||
TextSimulation::on_set(const Tokens& arguments)
|
||||
{
|
||||
p_stderr(_("\nFIXME: Not implemented"));
|
||||
p_stderr(_("\nFIXME: Not implemented\n"));
|
||||
}
|
||||
|
||||
void
|
||||
TextSimulation::on_show(const Tokens& arguments)
|
||||
{
|
||||
p_stderr(_("\nFIXME: Not implemented"));
|
||||
p_stderr(_("\nFIXME: Not implemented\n"));
|
||||
}
|
||||
|
||||
void
|
||||
TextSimulation::on_add(const Tokens& arguments)
|
||||
{
|
||||
p_stderr(_("\nFIXME: Not implemented"));
|
||||
p_stderr(_("\nFIXME: Not implemented\n"));
|
||||
}
|
||||
|
||||
void
|
||||
TextSimulation::on_remove(const Tokens& arguments)
|
||||
{
|
||||
p_stderr(_("\nFIXME: Not implemented"));
|
||||
p_stderr(_("\nFIXME: Not implemented\n"));
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -326,7 +408,7 @@ TextSimulation::readline()
|
|||
}
|
||||
|
||||
void
|
||||
TextSimulation::parse_command(pair< pair<TextSimulation*, IOManager*>, const ustring > p)
|
||||
TextSimulation::parse_command(TextSimulation& sim, const ustring& str)
|
||||
{
|
||||
typedef void (TextSimulation::*f_ptr)(const Tokens&);
|
||||
map<ustring, f_ptr> command_handlers;
|
||||
|
@ -343,25 +425,22 @@ TextSimulation::parse_command(pair< pair<TextSimulation*, IOManager*>, const us
|
|||
command_handlers["REMOVE"] = &TextSimulation::on_remove;
|
||||
command_handlers["QUIT"] = &TextSimulation::on_quit;
|
||||
|
||||
ustring str = p.second;
|
||||
|
||||
Tokens arguments = tokenize(str);
|
||||
|
||||
if(arguments.size() == 0)
|
||||
return;
|
||||
|
||||
if(command_handlers.find(arguments[0]) == command_handlers.end())
|
||||
ustring key = arguments[0].uppercase();
|
||||
|
||||
if(command_handlers.find(key) == command_handlers.end())
|
||||
{
|
||||
p_stderr(_("\nERROR: command not supported"));
|
||||
p_stderr(_("\nERROR: command not supported\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
ustring key = arguments[0].uppercase();
|
||||
arguments.erase(arguments.begin());
|
||||
|
||||
TextSimulation* obj = p.first.first;
|
||||
|
||||
(obj->*(command_handlers[key]))(arguments);
|
||||
(sim.*(command_handlers[key]))(arguments);
|
||||
}
|
||||
|
||||
// ** Please do NOT delete this code, I still use it as a reference **
|
||||
|
@ -842,7 +921,10 @@ TextSimulation::_io_loop(pair<TextSimulation* , int > pun)
|
|||
//Thread::create( sigc::bind(&TextSimulation::parse_command, p), true);
|
||||
//else
|
||||
//no read is possible: only write
|
||||
pun.first->parse_command(p);
|
||||
|
||||
// FIXME what was the purpose of this (_io_loop()) method
|
||||
// need to comment out to make the code compile
|
||||
//pun.first->parse_command(p);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue