33``solve`` invokes the SAT solver directly on the given theory.
44"""
55
6- import io
76import os
87import subprocess
98import tempfile
109import typing as t
1110
12- from nnf import NNF , And , Or , Var , dimacs , Model
11+ from nnf import And , Or , Var , dimacs , Model
1312
1413__all__ = ('solve' )
1514
1615
17-
1816def solve (
1917 sentence : And [Or [Var ]],
2018 extra_args : t .Sequence [str ] = []
@@ -29,7 +27,7 @@ def solve(
2927 raise ValueError ("Sentence must be in CNF" )
3028
3129 SOLVER = os .path .join (os .path .dirname (os .path .abspath (__file__ )),
32- 'bin' , 'kissat' )
30+ 'bin' , 'kissat' )
3331 assert os .path .isfile (SOLVER ), "Cannot seem to find kissat solver."
3432
3533 args = [SOLVER ] + extra_args
@@ -66,10 +64,13 @@ def solve(
6664 if 's SATISFIABLE' not in log :
6765 raise RuntimeError ("Something went wrong. Log:\n \n {}" .format (log ))
6866
67+ log = [line .strip () for line in log .split ('\n ' )]
6968
70- model = log .split ('\n v ' )[1 ].split ('\n ' )[0 ] # Line that starts with 'v ...'
71- model = model [:- 2 ] # Strip off the final '0'
72- model = map (int , model .strip ().split (' ' )) # Individual numbered literals
73- model = {var_labels_inverse [abs (lit )]: lit > 0 for lit in model }
69+ model = [line [2 :] for line in log if line [:2 ] == 'v ' ] # Variable lines
70+ model = ' ' .join (model ).split (' ' ) # Combine lines and split out literals
71+ assert '0' == model [- 1 ], "Error: Last entry should be a 0\n %s" % str (model )
72+ model = model [:- 1 ]
73+ model = map (int , model ) # Individual numbered literals
74+ model = {var_labels [abs (lit )]: lit > 0 for lit in model }
7475
7576 return model
0 commit comments