def solve(self, cnf): s = Solution() infile = NamedTemporaryFile(mode='w') outfile = NamedTemporaryFile(mode='r') io = DimacsCnf() infile.write(io.tostring(cnf)) infile.flush() ret = call(self.command % (infile.name, outfile.name), shell=True) infile.close() if ret != 10: return s s.success = True lines = outfile.readlines()[1:] for line in lines: varz = line.split(" ")[:-1] for v in varz: v = v.strip() value = v[0] != '-' v = v.lstrip('-') vo = io.varobj(v) s.varmap[vo] = value # Close deletes the tmp files outfile.close() return s
def solve(self, cnf): s = Solution() # NamedTemporaryFile doesn't work on Windows, see https://stackoverflow.com/questions/15169101/how-to-create-a-temporary-file-that-can-be-read-by-a-subprocess infile, infilename = tempfile.mkstemp(suffix="cnf") try: io = DimacsCnf() os.write(infile, io.tostring(cnf)) os.close(infile) try: cmd = self.command % (infilename.replace("\\", "/")) check_output(cmd, stderr=STDOUT, shell=True) # Lingeling and most SAT-solvers use a non-zero return code, which in most POSIX command indicates an error. # That's why python raises an exception, but here it's expected except CalledProcessError as call: if call.returncode != 10 and call.returncode != 20: s.error = call.output return s finally: os.remove(infilename) if call.returncode != 10: return s s.success = True for line in call.output.split("\n"): # Solution line example: v 1 -2 3 -4 5 6 0 if len(line) > 0 and line[0] == 'v': varz = line.split(" ")[1:-1] for v in varz: v = v.strip() value = v[0] != '-' v = v.lstrip('-') vo = io.varobj(v) s.varmap[vo] = value return s
def solve(self, cnf): s = Solution() # NamedTemporaryFile doesn't work on Windows, see https://stackoverflow.com/questions/15169101/how-to-create-a-temporary-file-that-can-be-read-by-a-subprocess infile, infilename = tempfile.mkstemp(suffix="cnf") try: io = DimacsCnf() os.write(infile, io.tostring(cnf)) os.close(infile) try: cmd = self.command % (infilename.replace("\\","/")) check_output(cmd, stderr=STDOUT, shell=True) # Lingeling and most SAT-solvers use a non-zero return code, which in most POSIX command indicates an error. # That's why python raises an exception, but here it's expected except CalledProcessError as call: if call.returncode != 10 and call.returncode != 20: s.error = call.output return s finally: os.remove(infilename) if call.returncode != 10: return s s.success = True for line in call.output.split("\n"): # Solution line example: v 1 -2 3 -4 5 6 0 if len(line) > 0 and line[0] == 'v': varz = line.split(" ")[1:-1] for v in varz: v = v.strip() value = v[0] != '-' v = v.lstrip('-') vo = io.varobj(v) s.varmap[vo] = value return s
def solve(self, cnf): s = Solution() with NamedTemporaryFile(mode='w') as infile, \ NamedTemporaryFile(mode='r') as outfile: io = DimacsCnf() infile.write(io.tostring(cnf)) infile.flush() try: output = check_output( [self.command, infile.name, outfile.name], universal_newlines=True, timeout=self.timeout) except CalledProcessError as ex: s.success = (ex.returncode == 10) output = ex.output s.stats = parse_stats(output) if not s.success: return s lines = outfile.readlines()[1:] for line in lines: varz = line.split(" ")[:-1] for v in varz: v = v.strip() value = v[0] != '-' v = v.lstrip('-') vo = io.varobj(v) s.varmap[vo] = value return s