- Added the LOAD command to TextSimulation, and the classic question made to user on replacing an unsaved simulation

- Changed the way syntactically incorrect python policies are handled, we no more exit abruptly

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@829 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-08-08 00:20:56 +00:00
parent d72ce96508
commit 0138387a7f
8 changed files with 189 additions and 14 deletions

View file

@ -91,7 +91,8 @@ namespace sgpem
}
TextSimulation::TextSimulation()
TextSimulation::TextSimulation() :
_saved(true)
{
}
@ -116,6 +117,35 @@ TextSimulation::check_arguments_num(const Tokens& arguments, unsigned int num)
return true;
}
bool
TextSimulation::unsaved_ask_confirm() const
{
if(!_saved)
{
p_stderr(_("WARNING: Simulation was not recently saved. "
"If you continue some changes to the simulation might be lost.\n"));
for(bool bad_arg = true; bad_arg;)
{
p_stdout(_("Continue? [y/n] "));
ustring buf = readline();
Tokens tokens = tokenize(buf);
if(tokens.size() == 1 && tokens[0].size() == 1)
{
if(tokens[0].lowercase() == _("n"))
return false;
else if(tokens[0].lowercase() == _("y"))
bad_arg = false;
}
}
}
return true;
}
template <typename Container>
void
TextSimulation::show(const Container& entities)
@ -459,7 +489,7 @@ TextSimulation::on_help(const Tokens& arguments)
if(command.size() == 0)
p_stdout(_("Avaiable commands:\nRUN\nSTOP\nPAUSE\n"
"CONFIGURE-CPU-POLICY\nHELP\nGET\nSET\nSHOW\nADD\n"
"REMOVE\nQUIT\n\n"
"REMOVE\nSAVE\nLOAD\nQUIT\n\n"
"HELP followed by a command name shows help about it.\n"
"ex. `HELP RUN` shows help about the command RUN\n"));
else if(command == "RUN")
@ -520,6 +550,12 @@ TextSimulation::on_help(const Tokens& arguments)
"numeric ids follow the same logic of the previous commands\n"
"`REMOVE subrequest <process_id> <thread_id> <request_id> <subrequest_id>` where the "
"numeric ids follow the same logic of the previous commands\n"));
else if(command == "SAVE")
p_stderr(_("-- SAVE COMMAND --\nSaves the simulation.\n\n"
"Syntax: SAVE <filename>\n"));
else if(command == "LOAD")
p_stderr(_("-- LOAD COMMAND --\nLoads the simulation.\n\n"
"Syntax: LOAD <filename>\n"));
else if(command == "QUIT")
p_stderr(_("-- QUIT COMMAND --\nGently closes the program.\n"));
else
@ -531,6 +567,9 @@ TextSimulation::on_quit(const Tokens& arguments)
{
check_arguments_num(arguments, 0);
if(!unsaved_ask_confirm())
return;
p_stdout(_("\n\n*** Thank you for using SGPEM by Sirius Cybernetics Corporation ***\n\n"));
// Is this ok? Really? Oh, sure, if it we always did it in this way, it is surely a Good Thing!
@ -1303,11 +1342,53 @@ TextSimulation::on_save(const Tokens& arguments)
vector<Serializer*> serializers =
SerializersGatekeeper::get_instance().get_registered();
// FIXME using the first serializer available, this
// will need to be changed when multiple serializers will
// be made available
Serializer& serializer = *serializers.at(0);
const History& history = Simulation::get_instance().get_history();
serializer.save_snapshot(filename, history);
_saved = true;
}
catch(out_of_range e)
{
p_stderr(_("ERROR: No registered serializer available\n"));
}
catch(SerializerError e)
{
string msg = _("ERROR: ");
p_stderr(msg + e.what() + "\n");
}
}
void
TextSimulation::on_load(const Tokens& arguments)
{
if(!check_arguments_num(arguments, 1))
return;
ustring filename = arguments[0];
if(!unsaved_ask_confirm())
return;
try
{
vector<Serializer*> serializers =
SerializersGatekeeper::get_instance().get_registered();
// FIXME using the first serializer available, this
// will need to be changed when multiple serializers will
// be made available
Serializer& serializer = *serializers.at(0);
History& history = Simulation::get_instance().get_history();
serializer.restore_snapshot(filename, history);
}
catch(out_of_range e)
{
@ -1361,8 +1442,9 @@ TextSimulation::parse_command(TextSimulation& sim, const ustring& str)
command_handlers["ADD"] = &TextSimulation::on_add;
command_handlers["REMOVE"] = &TextSimulation::on_remove;
command_handlers["SAVE"] = &TextSimulation::on_save;
command_handlers["LOAD"] = &TextSimulation::on_load;
command_handlers["QUIT"] = &TextSimulation::on_quit;
Tokens arguments = tokenize(str);
if(arguments.size() == 0)
@ -1379,6 +1461,9 @@ TextSimulation::parse_command(TextSimulation& sim, const ustring& str)
arguments.erase(arguments.begin());
(sim.*(command_handlers[key]))(arguments);
if(key == "ADD" || key == "REMOVE")
sim._saved = false;
}
static ostream&