1 #include "inc_solver.h"
3 #define SATSOLVER "sat_solver"
5 IncrementalSolver::IncrementalSolver() :
6 buffer((int *)malloc(sizeof(int)*BUFFERSIZE)),
14 IncrementalSolver::~IncrementalSolver() {
19 void IncrementalSolver::reset() {
25 void IncrementalSolver::addClauseLiteral(int literal) {
26 buffer[offset++]=literal;
27 if (offset==BUFFERSIZE) {
32 void IncrementalSolver::finishedClauses() {
36 void IncrementalSolver::freeze(int variable) {
37 addClauseLiteral(IS_FREEZE);
38 addClauseLiteral(variable);
41 int IncrementalSolver::solve() {
43 addClauseLiteral(IS_RUNSOLVER);
45 int result=readIntSolver();
46 if (result == IS_SAT) {
47 int numVars=readIntSolver();
48 if (numVars > solutionsize) {
51 solution = (int *) malloc(numVars*sizeof(int));
53 readSolver(solution, numVars * sizeof(int));
58 int IncrementalSolver::readIntSolver() {
60 readSolver(&value, 4);
64 void IncrementalSolver::readSolver(void * tmp, ssize_t size) {
65 char *result = (char *) tmp;
66 ssize_t bytestoread=size;
69 ssize_t n=read(from_solver_fd, &((char *)result)[bytesread], bytestoread);
71 fprintf(stderr, "Read failure\n");
76 } while(bytestoread != 0);
79 bool IncrementalSolver::getValue(int variable) {
80 return solution[variable];
83 void IncrementalSolver::createSolver() {
86 if (pipe(to_pipe) || pipe(from_pipe)) {
87 fprintf(stderr, "Error creating pipe.\n");
90 if ((solver_pid = fork()) == -1) {
91 fprintf(stderr, "Error forking.\n");
94 if (solver_pid == 0) {
98 if ((dup2(0, to_pipe[1]) == -1) ||
99 (dup2(1, from_pipe[0]) == -1)) {
100 fprintf(stderr, "Error duplicating pipes\n");
103 execlp(SATSOLVER, SATSOLVER, NULL);
104 fprintf(stderr, "execlp Failed\n");
107 to_solver_fd = to_pipe[0];
108 from_solver_fd = from_pipe[1];
114 void IncrementalSolver::killSolver() {
116 close(from_solver_fd);
119 killpg(solver_pid, SIGKILL);
122 void IncrementalSolver::flushBuffer() {
123 ssize_t bytestowrite=sizeof(int)*offset;
124 ssize_t byteswritten=0;
126 ssize_t n=write(to_solver_fd, &((char *)buffer)[byteswritten], bytestowrite);
128 fprintf(stderr, "Write failure\n");
133 } while(bytestowrite != 0);