sat: factor the creation of temporary files

* src/misc/satsolver.hh, src/misc/satsolver.cc: Present
the SAT solver as an object with a stream interface, to
prepare for a better implementation.
* src/tgbaalgos/dtbasat.cc, src/tgbaalgos/dtgbasat.cc:
Adjust to the new interface, removing all the handling
of temporary files.
* src/tgbatest/readsat.cc: Adjust.
This commit is contained in:
Alexandre Duret-Lutz 2013-12-06 18:42:19 +01:00
parent 1853bdd53b
commit 9c98975c19
5 changed files with 110 additions and 168 deletions

View file

@ -83,18 +83,10 @@ namespace spot
};
}
int satsolver(printable* input, printable* output)
{
// Make this static, so the SPOT_SATSOLVER lookup is done only on
// the first call to run_sat().
static satsolver_command cmd;
return cmd.run(input, output);
}
sat_solution
satsolver::solution
satsolver_get_solution(const char* filename)
{
sat_solution sol;
satsolver::solution sol;
std::istream* in;
if (filename[0] == '-' && filename[1] == 0)
in = &std::cin;
@ -130,4 +122,47 @@ namespace spot
return sol;
}
satsolver::satsolver()
: cnf_tmp_(0), cnf_stream_(0)
{
start();
}
void satsolver::start()
{
cnf_tmp_ = create_tmpfile("sat-", ".cnf");
cnf_stream_ = new std::fstream(cnf_tmp_->name(),
std::ios_base::trunc | std::ios_base::out);
cnf_stream_->exceptions(std::ifstream::failbit | std::ifstream::badbit);
}
satsolver::~satsolver()
{
delete cnf_tmp_;
delete cnf_stream_;
}
std::ostream& satsolver::operator()()
{
return *cnf_stream_;
}
satsolver::solution_pair
satsolver::get_solution()
{
delete cnf_stream_; // Close the file.
cnf_stream_ = 0;
temporary_file* output = create_tmpfile("sat-", ".out");
solution_pair p;
// Make this static, so the SPOT_SATSOLVER lookup is done only on
// the first call to run_sat().
static satsolver_command cmd;
p.first = cmd.run(cnf_tmp_, output);
p.second = satsolver_get_solution(output->name());
delete output;
return p;
}
}

View file

@ -21,8 +21,10 @@
#define SPOT_MISC_SATSOLVER_HH
#include "common.hh"
#include "tmpfile.hh"
#include <vector>
#include <stdexcept>
#include <iosfwd>
namespace spot
{
@ -65,29 +67,37 @@ namespace spot
}
};
/// \brief Run a SAT solver.
/// \brief Interface with a SAT solver.
///
/// Run a SAT solver using the input in file \a input,
/// and sending output in file \a output.
/// Call start() to create some temporary file, then send DIMACs
/// text to the stream returned by operator(), and finally call
/// get_solution().
///
/// These two arguments are instance of printable, as
/// they will be evaluated in a %-escaped string such as
/// "satsolver %I >%O"
/// This command can be overridden using the
/// <code>SPOT_SATSOLVER</code> environment variable.
///
/// Note that temporary_file instances implement the
/// printable interface.
SPOT_API int
satsolver(printable* input, printable* output);
/// The satsolver called can be configured via the
/// <code>SPOT_SATSOLVER</code> environment variable. It
/// defaults to
/// "satsolver -verb=0 %I >%O"
/// where %I and %O are replaced by input and output files.
class SPOT_API satsolver
{
public:
satsolver();
~satsolver();
void start();
std::ostream& operator()();
typedef std::vector<int> sat_solution;
typedef std::vector<int> solution;
typedef std::pair<int, solution> solution_pair;
solution_pair get_solution();
private:
temporary_file* cnf_tmp_;
std::ostream* cnf_stream_;
};
/// \brief Extract the solution of a SAT solver output.
SPOT_API sat_solution
SPOT_API satsolver::solution
satsolver_get_solution(const char* filename);
}
#endif // SPOT_MISC_SATSOLVER_HH