Skip to content

Commit

Permalink
Allowing for iterable arguments on the And/Or features.
Browse files Browse the repository at this point in the history
  • Loading branch information
haz committed May 28, 2021
1 parent 921832b commit 4e51387
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions bauhaus/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import weakref
from collections.abc import Iterable
# add try import
import nnf
from functools import wraps
Expand Down Expand Up @@ -269,11 +270,20 @@ def compile(self):
return self.args[0].compile().negate() | self.args[1].compile()


def _flatten_and_build_andor(args, andor):
all_args = []
for arg in args:
if isinstance(arg, Iterable):
all_args.extend(list(arg))
else:
all_args.append(arg)
return CustomNNF(andor, all_args)

def And(*args):
return CustomNNF('and', args)
return _flatten_and_build_andor(args, 'and')

def Or(*args):
return CustomNNF('or', args)
return _flatten_and_build_andor(args, 'or')


def proposition(encoding: Encoding):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def test_andor():
c1, c2, d1 = C(), C(), D()
c.add_constraint(And(~c1, (c2 & d1)))
c.add_constraint(Or(c1 >> c2, d1))
c.add_constraint(And(Or([c1, c2]), d1))
c.add_constraint(Or(And({c1, c2}), d1))
T = c.compile()
assert T.satisfiable()

Expand Down

0 comments on commit 4e51387

Please sign in to comment.