From 4e51387c63adeeb6b085275a4c6e5601a91f7b92 Mon Sep 17 00:00:00 2001 From: Christian Muise Date: Thu, 27 May 2021 20:45:34 -0400 Subject: [PATCH] Allowing for iterable arguments on the And/Or features. --- bauhaus/core.py | 14 ++++++++++++-- tests/test_core.py | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bauhaus/core.py b/bauhaus/core.py index 1c3baee..a11ed5d 100644 --- a/bauhaus/core.py +++ b/bauhaus/core.py @@ -1,4 +1,5 @@ import weakref +from collections.abc import Iterable # add try import import nnf from functools import wraps @@ -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): diff --git a/tests/test_core.py b/tests/test_core.py index f2c9f67..0ed5903 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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()