def test_parse(self): a, b, c = boolean.symbols("a", "b", "c") parse = boolean.parse self.assertTrue(parse("0") == parse("(0)") == boolean.FALSE) self.assertTrue(parse("1") == parse("(1)") == boolean.TRUE) self.assertTrue(parse("a") == parse("(a)") == a) self.assertTrue(parse("~a") == parse("~(a)") == parse("(~a)") == ~a) self.assertTrue(parse("~~a") == ~~a) self.assertTrue(parse("a*b") == a * b) self.assertTrue(parse("~a*b") == ~a * b) self.assertTrue(parse("a*~b") == a * ~b) self.assertTrue( parse("a*b*c") == parse("a*b*c", eval=False) == boolean.AND( a, b, c)) self.assertTrue( parse("~a*~b*~c") == parse("~a*~b*~c", eval=False) == boolean.AND( ~a, ~b, ~c)) self.assertTrue(parse("a+b") == a + b) self.assertTrue(parse("~a+b") == ~a + b) self.assertTrue(parse("a+~b") == a + ~b) self.assertTrue( parse("a+b+c") == parse("a+b+c", eval=False) == boolean.OR( a, b, c)) self.assertTrue(parse("~a+~b+~c") == boolean.OR(~a, ~b, ~c)) self.assertTrue(parse("(a+b)") == a + b) self.assertTrue(parse("a*(a+b)") == a * (a + b)) self.assertTrue(parse("a*(a+~b)") == a * (a + ~b)) self.assertTrue( parse("(a*b)+(b*((c+a)*(b+(c*a))))") == parse( "a*b + b*(c+a)*(b+c*a)") == (a * b) + (b * ((c + a) * (b + (c * a)))))
def test_distributive(self): a = self.a b = self.b c = self.c d = boolean.Symbol("d") e = boolean.Symbol("e") self.assertTrue((a * (b + c)).distributive() == (a * b) + (a * c)) t1 = boolean.AND(a, (b + c), (d + e)) t2 = boolean.OR(boolean.AND(a, b, d), boolean.AND(a, b, e), boolean.AND(a, c, d), boolean.AND(a, c, e)) self.assertTrue(t1.distributive() == t2)
def term_reduce_to_one_alternative(term, removal_terms): if type(term) == boolean.Symbol: if removal_terms.get(str(term)): # "Remove" term by returning None. return None else: # return symbol as is return term elif type(term) == boolean.NOT: # return Nonetype (do not handle negated terms) return None elif type(term) == boolean.OR: # Return None symbol if there is one, otherwise first alternative. # A bit counter-intuitive, ending at 0 omits the final 0 when counting # backwards. for term_pos in range(len(term.args) - 1, 0, -1): result = term_reduce_to_one_alternative(term.args[term_pos], removal_terms) if result is None: return result return term_reduce_to_one_alternative(term.args[0], removal_terms) elif type(term) == boolean.AND: newterm = () for subterm in term.args: newsubterm = term_reduce_to_one_alternative(subterm, removal_terms) if newsubterm != None: newterm += (newsubterm, ) if len(newterm) == 0: return None elif len(newterm) == 1: return newterm[0] else: return boolean.AND(*newterm) else: raise ValueError
def __init__(self, pos, hidden=False, anonymous_symbols=True): dual_expression = boolean.NOT( boolean.AND(boolean.Symbol(None), boolean.Symbol(None))) super().__init__(pos, dual_expression, hidden, anonymous_symbols)
def term_reduce_to_first_alternative(term): if type(term) == boolean.Symbol: # return symbol as is return term elif type(term) == boolean.NOT: # return Nonetype (do not handle negated terms) return None elif type(term) == boolean.OR: # return first alternative return term_reduce_to_first_alternative(term.args[0]) elif type(term) == boolean.AND: newterm = () for subterm in term.args: newsubterm = term_reduce_to_first_alternative(subterm) if newsubterm != None: newterm += (newsubterm, ) if len(newterm) == 0: return None elif len(newterm) == 1: return newterm[0] else: return boolean.AND(*newterm) else: raise ValueError
def test_normalize(self): parse = boolean.parse expr = parse("((s+a)*(s+b)*(s+c)*(s+d)*(e+c+d))+(a*e*d)") result = boolean.AND(*boolean.normalize(boolean.AND, expr)) sol = parse("(a+s)*(b+e+s)*(c+d+e)*(c+e+s)*(d+s)") self.assertTrue(result == sol)
def test_order(self): x, y, z = boolean.symbols(1, 2, 3) self.assertTrue(boolean.AND(x, y) < boolean.AND(x, y, z)) self.assertTrue(not boolean.AND(x, y) > boolean.AND(x, y, z)) self.assertTrue(boolean.AND(x, y) < boolean.AND(x, z)) self.assertTrue(not boolean.AND(x, y) > boolean.AND(x, z)) self.assertTrue(boolean.AND(x, y) < boolean.AND(y, z)) self.assertTrue(not boolean.AND(x, y) > boolean.AND(y, z)) self.assertTrue(not boolean.AND(x, y) < boolean.AND(x, y)) self.assertTrue(not boolean.AND(x, y) > boolean.AND(x, y))