Commit 907b72fb authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz

ltsmin: implement a two-step loading

* spot/ltsmin/ltsmin.cc, spot/ltsmin/ltsmin.hh: Split load_ltsmin() into
ltsmin_model::load() and ltsmin_model::kripke().  Report errors using
exceptions instead of on std::cerr.
* python/spot/ltsmin.i: Deal with exceptions.
* tests/ltsmin/modelcheck.cc, tests/python/ltsmin.ipynb: Adjust.
parent 84031d2a
...@@ -5,6 +5,16 @@ New in spot 1.99.7a (not yet released) ...@@ -5,6 +5,16 @@ New in spot 1.99.7a (not yet released)
* Building products with different dictionaries now raise an * Building products with different dictionaries now raise an
exception instead of using an assertion that could be disabled. exception instead of using an assertion that could be disabled.
* The load_ltsmin() function has been split in two. Now you should
first call ltsmin_model::load(filename) to create an ltlmin_model,
and then call the ltsmin_model::kripke(...) method to create an
automaton that can be iterated on the fly.
Python:
* The ltsmin interface has been binded in Python. See
https://spot.lrde.epita.fr/ipynb/ltsmin.html for a short example.
Documentation: Documentation:
* There is a new page giving informal illustrations (and extra * There is a new page giving informal illustrations (and extra
......
...@@ -47,20 +47,36 @@ using namespace spot; ...@@ -47,20 +47,36 @@ using namespace spot;
%import(module="spot.impl") <spot/kripke/fairkripke.hh> %import(module="spot.impl") <spot/kripke/fairkripke.hh>
%import(module="spot.impl") <spot/kripke/kripke.hh> %import(module="spot.impl") <spot/kripke/kripke.hh>
%exception {
try {
$action
}
catch (const std::runtime_error& e)
{
SWIG_exception(SWIG_RuntimeError, e.what());
}
}
namespace std { namespace std {
%template(atomic_prop_set) set<spot::formula>; %template(atomic_prop_set) set<spot::formula>;
} }
%rename(model) spot::ltsmin_model;
%rename(kripke_raw) spot::ltsmin_model::kripke;
%include <spot/ltsmin/ltsmin.hh> %include <spot/ltsmin/ltsmin.hh>
%pythoncode %{ %pythoncode %{
import spot import spot
def load(filename, ap_set, dict=spot._bdd_dict, def load(filename):
dead=spot.formula_ap('dead'), return model.load(filename)
compress=2, debug=False):
def _kripke(self, ap_set, dict=spot._bdd_dict,
dead=spot.formula_ap('dead'),
compress=2):
s = spot.atomic_prop_set() s = spot.atomic_prop_set()
for ap in ap_set: for ap in ap_set:
s.insert(spot.formula_ap(ap)) s.insert(spot.formula_ap(ap))
return load_ltsmin(filename, dict, s, dead, compress, debug) return self.kripke_raw(s, dict, dead, compress)
model.kripke = _kripke
%} %}
// -*- coding: utf-8 -*- // -*- coding: utf-8 -*-
// Copyright (C) 2011, 2012, 2014, 2015 Laboratoire de Recherche et // Copyright (C) 2011, 2012, 2014, 2015, 2016 Laboratoire de Recherche et
// Développement de l'Epita (LRDE) // Développement de l'Epita (LRDE)
// //
// This file is part of Spot, a model checking library. // This file is part of Spot, a model checking library.
...@@ -53,21 +53,33 @@ namespace spot ...@@ -53,21 +53,33 @@ namespace spot
typedef void (*TransitionCB)(void *ctx, typedef void (*TransitionCB)(void *ctx,
transition_info_t *transition_info, transition_info_t *transition_info,
int *dst); int *dst);
}
struct spins_interface struct spins_interface
{
lt_dlhandle handle; // handle to the dynamic library
void (*get_initial_state)(void *to);
int (*have_property)();
int (*get_successors)(void* m, int *in, TransitionCB, void *arg);
int (*get_state_size)();
const char* (*get_state_variable_name)(int var);
int (*get_state_variable_type)(int var);
int (*get_type_count)();
const char* (*get_type_name)(int type);
int (*get_type_value_count)(int type);
const char* (*get_type_value_name)(int type, int value);
~spins_interface()
{ {
lt_dlhandle handle; // handle to the dynamic library if (handle)
void (*get_initial_state)(void *to); lt_dlclose(handle);
int (*have_property)(); lt_dlexit();
int (*get_successors)(void* m, int *in, TransitionCB, void *arg); }
int (*get_state_size)(); };
const char* (*get_state_variable_name)(int var);
int (*get_state_variable_type)(int var); namespace
int (*get_type_count)(); {
const char* (*get_type_name)(int type); typedef std::shared_ptr<const spins_interface> spins_interface_ptr;
int (*get_type_value_count)(int type);
const char* (*get_type_value_name)(int type, int value);
};
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// STATE // STATE
...@@ -322,14 +334,15 @@ namespace spot ...@@ -322,14 +334,15 @@ namespace spot
}; };
int void
convert_aps(const atomic_prop_set* aps, convert_aps(const atomic_prop_set* aps,
const spins_interface* d, spins_interface_ptr d,
bdd_dict_ptr dict, bdd_dict_ptr dict,
formula dead, formula dead,
prop_set& out) prop_set& out)
{ {
int errors = 0; int errors = 0;
std::ostringstream err;
int state_size = d->get_state_size(); int state_size = d->get_state_size();
typedef std::map<std::string, var_info> val_map_t; typedef std::map<std::string, var_info> val_map_t;
...@@ -367,8 +380,7 @@ namespace spot ...@@ -367,8 +380,7 @@ namespace spot
++s; ++s;
if (!*s) if (!*s)
{ {
std::cerr << "Proposition `" << str err << "Proposition `" << str << "' cannot be parsed.\n";
<< "' cannot be parsed." << std::endl;
++errors; ++errors;
continue; continue;
} }
...@@ -393,8 +405,7 @@ namespace spot ...@@ -393,8 +405,7 @@ namespace spot
if (name == name_p) if (name == name_p)
{ {
std::cerr << "Proposition `" << str err << "Proposition `" << str << "' cannot be parsed.\n";
<< "' cannot be parsed." << std::endl;
free(name); free(name);
++errors; ++errors;
continue; continue;
...@@ -415,9 +426,9 @@ namespace spot ...@@ -415,9 +426,9 @@ namespace spot
if (ni == val_map.end()) if (ni == val_map.end())
{ {
std::cerr << "No variable `" << name err << "No variable `" << name
<< "' found in model (for proposition `" << "' found in model (for proposition `"
<< str << "')." << std::endl; << str << "').\n";
free(name); free(name);
++errors; ++errors;
continue; continue;
...@@ -429,14 +440,12 @@ namespace spot ...@@ -429,14 +440,12 @@ namespace spot
enum_map_t::const_iterator ei = enum_map[type_num].find(lastdot); enum_map_t::const_iterator ei = enum_map[type_num].find(lastdot);
if (ei == enum_map[type_num].end()) if (ei == enum_map[type_num].end())
{ {
std::cerr << "No state `" << lastdot err << "No state `" << lastdot << "' known for variable `"
<< "' known for variable `" << name << "'.\n";
<< name << "'." << std::endl; err << "Possible states are:";
std::cerr << "Possible states are:"; for (auto& ej: enum_map[type_num])
for (ei = enum_map[type_num].begin(); err << ' ' << ej.first;
ei != enum_map[type_num].end(); ++ei) err << '\n';
std::cerr << ' ' << ei->first;
std::cerr << '\n';
free(name); free(name);
++errors; ++errors;
...@@ -446,16 +455,16 @@ namespace spot ...@@ -446,16 +455,16 @@ namespace spot
// At this point, *s should be 0. // At this point, *s should be 0.
if (*s) if (*s)
{ {
std::cerr << "Trailing garbage `" << s err << "Trailing garbage `" << s
<< "' at end of proposition `" << "' at end of proposition `"
<< str << "'." << std::endl; << str << "'.\n";
free(name); free(name);
++errors; ++errors;
continue; continue;
} }
// Record that X.Y must be equal to Z. // Record that X.Y must be equal to Z.
int v = dict->register_proposition(*ap, d); int v = dict->register_proposition(*ap, d.get());
one_prop p = { ni->second.num, OP_EQ, ei->second, v }; one_prop p = { ni->second.num, OP_EQ, ei->second, v };
out.push_back(p); out.push_back(p);
free(name); free(name);
...@@ -515,9 +524,9 @@ namespace spot ...@@ -515,9 +524,9 @@ namespace spot
break; break;
default: default:
report_error: report_error:
std::cerr << "Unexpected `" << s err << "Unexpected `" << s
<< "' while parsing atomic proposition `" << str << "' while parsing atomic proposition `" << str
<< "'." << std::endl; << "'.\n";
++errors; ++errors;
free(name); free(name);
continue; continue;
...@@ -534,8 +543,7 @@ namespace spot ...@@ -534,8 +543,7 @@ namespace spot
val = strtol(s, &s_end, 10); val = strtol(s, &s_end, 10);
if (s == s_end) if (s == s_end)
{ {
std::cerr << "Failed to parse `" << s err << "Failed to parse `" << s << "' as an integer.\n";
<< "' as an integer." << std::endl;
++errors; ++errors;
free(name); free(name);
continue; continue;
...@@ -555,14 +563,13 @@ namespace spot ...@@ -555,14 +563,13 @@ namespace spot
enum_map_t::const_iterator ei = enum_map[type_num].find(st); enum_map_t::const_iterator ei = enum_map[type_num].find(st);
if (ei == enum_map[type_num].end()) if (ei == enum_map[type_num].end())
{ {
std::cerr << "No state `" << st err << "No state `" << st << "' known for variable `"
<< "' known for variable `" << name << "'.\n";
<< name << "'." << std::endl; err << "Possible states are:";
std::cerr << "Possible states are:";
for (ei = enum_map[type_num].begin(); for (ei = enum_map[type_num].begin();
ei != enum_map[type_num].end(); ++ei) ei != enum_map[type_num].end(); ++ei)
std::cerr << ' ' << ei->first; err << ' ' << ei->first;
std::cerr << '\n'; err << '\n';
free(name); free(name);
++errors; ++errors;
...@@ -578,11 +585,11 @@ namespace spot ...@@ -578,11 +585,11 @@ namespace spot
++s; ++s;
if (*s) if (*s)
{ {
std::cerr << "Unexpected `" << s err << "Unexpected `" << s
<< "' while parsing atomic proposition `" << str << "' while parsing atomic proposition `" << str
<< "'." << std::endl; << "'.\n";
++errors; ++errors;
continue; continue;
} }
...@@ -591,7 +598,8 @@ namespace spot ...@@ -591,7 +598,8 @@ namespace spot
out.push_back(p); out.push_back(p);
} }
return errors; if (errors)
throw std::runtime_error(err.str());
} }
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
...@@ -601,7 +609,7 @@ namespace spot ...@@ -601,7 +609,7 @@ namespace spot
{ {
public: public:
spins_kripke(const spins_interface* d, const bdd_dict_ptr& dict, spins_kripke(spins_interface_ptr d, const bdd_dict_ptr& dict,
const spot::prop_set* ps, formula dead, const spot::prop_set* ps, formula dead,
int compress) int compress)
: kripke(dict), : kripke(dict),
...@@ -679,13 +687,9 @@ namespace spot ...@@ -679,13 +687,9 @@ namespace spot
delete[] uncompressed_; delete[] uncompressed_;
delete[] compressed_; delete[] compressed_;
} }
lt_dlclose(d_->handle); dict_->unregister_all_my_variables(d_.get());
dict_->unregister_all_my_variables(d_);
delete d_;
delete ps_; delete ps_;
lt_dlexit();
if (state_condition_last_state_) if (state_condition_last_state_)
state_condition_last_state_->destroy(); state_condition_last_state_->destroy();
...@@ -918,7 +922,7 @@ namespace spot ...@@ -918,7 +922,7 @@ namespace spot
} }
private: private:
const spins_interface* d_; spins_interface_ptr d_;
int state_size_; int state_size_;
bdd_dict_ptr dict_; bdd_dict_ptr dict_;
const char** vname_; const char** vname_;
...@@ -950,8 +954,8 @@ namespace spot ...@@ -950,8 +954,8 @@ namespace spot
// Call spins to compile "foo.prom" as "foo.prom.spins" if the latter // Call spins to compile "foo.prom" as "foo.prom.spins" if the latter
// does not exist already or is older. // does not exist already or is older.
static bool static void
compile_model(std::string& filename, std::string& ext, bool verbose) compile_model(std::string& filename, std::string& ext)
{ {
std::string command; std::string command;
std::string compiled_ext; std::string compiled_ext;
...@@ -968,22 +972,14 @@ namespace spot ...@@ -968,22 +972,14 @@ namespace spot
} }
else else
{ {
if (verbose) throw std::runtime_error(std::string("Unknown extension '")
std::cerr << "Unknown extension `" << ext + ext + "'. Use '.prom', '.pm', '.pml', "
<< ("'. Use `.prom', `.pm', `.pml', `.dve', `.dve2C' or" "'.dve', '.dve2C', or '.prom.spins'.");
"`.prom.spins'.") << std::endl;
return false;
} }
struct stat s; struct stat s;
if (stat(filename.c_str(), &s) != 0) if (stat(filename.c_str(), &s) != 0)
{ throw std::runtime_error(std::string("Cannot open ") + filename);
if (verbose)
{
std::cerr << "Cannot open " << filename << std::endl;
return true;
}
}
std::string old = filename; std::string old = filename;
filename += compiled_ext; filename += compiled_ext;
...@@ -998,26 +994,19 @@ namespace spot ...@@ -998,26 +994,19 @@ namespace spot
if (stat(filename.c_str(), &d) == 0) if (stat(filename.c_str(), &d) == 0)
if (s.st_mtime < d.st_mtime) if (s.st_mtime < d.st_mtime)
// The .spins or .dve2C is up-to-date, no need to recompile it. // The .spins or .dve2C is up-to-date, no need to recompile it.
return false; return;
int res = system(command.c_str()); int res = system(command.c_str());
if (res) if (res)
{ throw std::runtime_error(std::string("Execution of '")
if (verbose) + command.c_str() + "' returned exit code "
std::cerr << "Execution of `" << command.c_str() + std::to_string(WEXITSTATUS(res)));
<< "' returned exit code " << WEXITSTATUS(res)
<< ".\n";
return true;
}
return false;
} }
} }
kripke_ptr ltsmin_model
load_ltsmin(const std::string& file_arg, const bdd_dict_ptr& dict, ltsmin_model::load(const std::string& file_arg)
const atomic_prop_set* to_observe,
const formula dead, int compress, bool verbose)
{ {
std::string file; std::string file;
if (file_arg.find_first_of("/\\") != std::string::npos) if (file_arg.find_first_of("/\\") != std::string::npos)
...@@ -1027,33 +1016,20 @@ namespace spot ...@@ -1027,33 +1016,20 @@ namespace spot
std::string ext = file.substr(file.find_last_of(".")); std::string ext = file.substr(file.find_last_of("."));
if (ext != ".spins" && ext != ".dve2C") if (ext != ".spins" && ext != ".dve2C")
{ compile_model(file, ext);
if (compile_model(file, ext, verbose))
{
if (verbose)
std::cerr << "Failed to compile `" << file_arg
<< "'." << std::endl;
return nullptr;
}
}
if (lt_dlinit()) if (lt_dlinit())
{ throw std::runtime_error("Failed to initialize libltldl.");
if (verbose)
std::cerr << "Failed to initialize libltdl." << std::endl;
return nullptr;
}
lt_dlhandle h = lt_dlopen(file.c_str()); lt_dlhandle h = lt_dlopen(file.c_str());
if (!h) if (!h)
{ {
if (verbose)
std::cerr << "Failed to load `" << file << "'." << std::endl;
lt_dlexit(); lt_dlexit();
return nullptr; throw std::runtime_error(std::string("Failed to load '")
+ file + "'.");
} }
spins_interface* d = new spins_interface; auto d = std::make_shared<spins_interface>();
d->handle = h; d->handle = h;
// SpinS interface. // SpinS interface.
...@@ -1112,36 +1088,38 @@ namespace spot ...@@ -1112,36 +1088,38 @@ namespace spot
&& d->get_type_name && d->get_type_name
&& d->get_type_value_count && d->get_type_value_count
&& d->get_type_value_name)) && d->get_type_value_name))
{ throw std::runtime_error(std::string("Failed resolve some symbol"
if (verbose) "while loading '") + file + "'.");
std::cerr << "Failed to resolve some symbol while loading `"
<< file << "'\n";
delete d;
lt_dlexit();
return nullptr;
}
if (d->have_property && d->have_property()) if (d->have_property && d->have_property())
{ throw std::runtime_error("Models with embedded properties "
if (verbose) "are not supported.");
std::cerr << "Model with an embedded property are not supported."
<< std::endl;
delete d;
lt_dlexit();
return nullptr;
}
return { d };
}
kripke_ptr
ltsmin_model::kripke(const atomic_prop_set* to_observe,
bdd_dict_ptr dict,
const formula dead, int compress) const
{
spot::prop_set* ps = new spot::prop_set; spot::prop_set* ps = new spot::prop_set;
int errors = convert_aps(to_observe, d, dict, dead, *ps); try
if (errors) {
convert_aps(to_observe, iface, dict, dead, *ps);
}
catch (std::runtime_error)
{ {
delete ps; delete ps;
dict->unregister_all_my_variables(d); dict->unregister_all_my_variables(iface.get());
delete d; throw;
lt_dlexit();
return nullptr;
} }
return std::make_shared<spins_kripke>(d, dict, ps, dead, compress); return std::make_shared<spins_kripke>(iface, dict, ps, dead, compress);
}
ltsmin_model::~ltsmin_model()
{
} }
} }
// -*- coding: utf-8 -*- // -*- coding: utf-8 -*-
// Copyright (C) 2011, 2013, 2014, 2015 Laboratoire de Recherche et // Copyright (C) 2011, 2013, 2014, 2015, 2016 Laboratoire de Recherche et
// Developpement de l'Epita (LRDE) // Developpement de l'Epita (LRDE)
// //
// This file is part of Spot, a model checking library. // This file is part of Spot, a model checking library.
...@@ -24,40 +24,56 @@ ...@@ -24,40 +24,56 @@
namespace spot namespace spot