sat: generalize the code for reading the solution

* src/misc/satsolver.cc, src/misc/satsolver.hh (satsolver_get_solution):
New function, that accepts a solution split on multiple 'v ' lines.
* src/tgbaalgos/dtbasat.cc, src/tgbaalgos/dtgbasat.cc (get_solution):
Remove, and adjust existing code to use satsolver_get_solution().
* src/tgbatest/readsat.cc, src/tgbatest/readsat.test: New files.
* src/tgbatest/Makefile.am: Add them.
* src/bin/man/spot-x.x: Mention the SAT competition rules for
the expected input/output format.
This commit is contained in:
Alexandre Duret-Lutz 2013-08-28 15:24:16 +02:00
parent 1ab46b0864
commit 90c106f8a8
8 changed files with 147 additions and 65 deletions

View file

@ -22,6 +22,9 @@
#include <cstdlib>
#include <sstream>
#include <stdexcept>
#include "satsolver.hh"
#include <fstream>
#include <limits>
namespace spot
{
@ -67,4 +70,44 @@ namespace spot
static satsolver_command cmd;
return cmd.run(input, output);
}
sat_solution
satsolver_get_solution(const char* filename)
{
sat_solution sol;
std::istream* in;
if (filename[0] == '-' && filename[1] == 0)
in = &std::cin;
else
in = new std::fstream(filename, std::ios_base::in);
int c;
while ((c = in->get()) != EOF)
{
// If a line does not start with 'v ', ignore it.
if (c != 'v' || in->get() != ' ')
{
in->ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
// Otherwise, read integers one by one.
int i;
while (*in >> i)
{
if (i == 0)
goto done;
sol.push_back(i);
}
if (!in->eof())
// If we haven't reached end-of-file, then we just attempted
// to extract something that wasn't an integer. Clear the
// fail bit so that will loop over.
in->clear();
}
done:
if (in != &std::cin)
delete in;
return sol;
}
}

View file

@ -21,6 +21,7 @@
#define SPOT_MISC_SATSOLVER_HH
#include "common.hh"
#include <vector>
namespace spot
{
@ -41,6 +42,14 @@ namespace spot
/// printable interface.
SPOT_API int
satsolver(printable* input, printable* output);
typedef std::vector<int> sat_solution;
/// \brief Extract the solution of a SAT solver output.
SPOT_API sat_solution
satsolver_get_solution(const char* filename);
}
#endif // SPOT_MISC_SATSOLVER_HH