Skip to content

Commit ae2ffe1

Browse files
committed
Adding a context manager for kissat, and adding tests.
1 parent 2e2d6fc commit ae2ffe1

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

nnf/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
__all__ = ('NNF', 'Internal', 'And', 'Or', 'Var', 'Aux', 'Builder',
3434
'all_models', 'complete_models', 'decision', 'true', 'false',
35-
'dsharp', 'dimacs', 'amc', 'kissat', 'tseitin', 'operators')
35+
'dsharp', 'dimacs', 'amc', 'kissat', 'using_kissat', 'tseitin',
36+
'operators')
3637

3738

3839
def all_models(names: 't.Iterable[Name]') -> t.Iterator[Model]:
@@ -63,6 +64,22 @@ def all_models(names: 't.Iterable[Name]') -> t.Iterator[Model]:
6364
# Valid values: native and kissat
6465
SAT_BACKEND = 'native'
6566

67+
class using_kissat():
68+
"""Context manager to use the kissat solver in a block of code."""
69+
70+
def __init__(self) -> None:
71+
self.setting = SAT_BACKEND
72+
73+
def __enter__(self):
74+
global SAT_BACKEND
75+
SAT_BACKEND = 'kissat'
76+
return self
77+
78+
def __exit__(self, exc_type, exc_value, exc_traceback):
79+
global SAT_BACKEND
80+
SAT_BACKEND = self.setting
81+
82+
6683
if t.TYPE_CHECKING:
6784
def memoize(func: T) -> T:
6885
...

test_nnf.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import pickle
3+
import platform
34
import shutil
45
import os
56

@@ -13,7 +14,7 @@
1314
import nnf
1415

1516
from nnf import (Var, And, Or, amc, dimacs, dsharp, operators,
16-
tseitin, complete_models)
17+
tseitin, complete_models, using_kissat)
1718

1819
settings.register_profile('patient', deadline=2000,
1920
suppress_health_check=(HealthCheck.too_slow,))
@@ -821,3 +822,21 @@ def test_complete_models(model: nnf.And[nnf.Var]):
821822
assert len(multi) == 8
822823
assert len({frozenset(x.items()) for x in multi}) == 8 # all unique
823824
assert all(x.keys() == m.keys() | {"test1", "test2"} for x in multi)
825+
826+
if (platform.uname().system, platform.uname().machine) == ('Linux', 'x86_64'):
827+
828+
def test_kissat_uf20():
829+
for sentence in uf20_cnf:
830+
with using_kissat():
831+
assert sentence.satisfiable()
832+
833+
@given(CNF())
834+
def test_kissat_cnf(sentence: And[Or[Var]]):
835+
assume(all(len(clause) > 0 for clause in sentence))
836+
with using_kissat():
837+
assert sentence.satisfiable() == sentence._cnf_satisfiable_native()
838+
839+
@given(NNF())
840+
def test_kissat_nnf(sentence: And[Or[Var]]):
841+
with using_kissat():
842+
assert sentence.satisfiable() == tseitin.to_CNF(sentence)._cnf_satisfiable_native()

0 commit comments

Comments
 (0)