def test_equal(self): from boolean.boolean import DualBase a, b, c = Symbol('a'), Symbol('b'), Symbol('c') t1 = DualBase(a, b) t1_2 = DualBase(b, a) t2 = DualBase(a, b, c) t2_2 = DualBase(b, c, a) # Test __eq__. self.assertTrue(t1 == t1) self.assertTrue(t1_2 == t1) self.assertTrue(t2_2 == t2) self.assertFalse(t1 == t2) self.assertFalse(t1 == 1) self.assertFalse(t1 is True) self.assertFalse(t1 is None) # Test __ne__. self.assertFalse(t1 != t1) self.assertFalse(t1_2 != t1) self.assertFalse(t2_2 != t2) self.assertTrue(t1 != t2) self.assertTrue(t1 != 1) self.assertTrue(t1 is not True) self.assertTrue(t1 is not None)
def test_isliteral(self): from boolean.boolean import DualBase a, b, c = Symbol('a'), Symbol('b'), Symbol('c') t1 = DualBase(a, b) t2 = DualBase(a, b, c) self.assertFalse(t1.isliteral) self.assertFalse(t2.isliteral)
def test_literals(self): l1 = Symbol(1) l2 = Symbol(1) self.assertTrue(l1 in l1.literals) self.assertTrue(l1 in l2.literals) self.assertTrue(l2 in l1.literals) self.assertTrue(l2 in l2.literals) self.assertRaises(AttributeError, setattr, l1, 'literals', 1)
def test_init(self): from boolean.boolean import DualBase a, b, c = Symbol('a'), Symbol('b'), Symbol('c') t1 = DualBase(a, b) t2 = DualBase(a, b, c) t3 = DualBase(a, a) t4 = DualBase(a, b, c) self.assertRaises(TypeError, DualBase) for term in (t1, t2, t3, t4): self.assertTrue(isinstance(term, DualBase))
def test_literals(self): from boolean.boolean import DualBase a, b, c = Symbol('a'), Symbol('b'), Symbol('c') t1 = DualBase(a, b) t2 = DualBase(a, b, c) t3 = DualBase(a, a) t4 = DualBase(a, b, c) for term in (t1, t2, t3, t4): self.assertTrue(a in term.literals) for term in (t1, t2, t4): self.assertTrue(b in term.literals) for term in (t2, t4): self.assertTrue(c in term.literals)
def new_variable_generator(): var_prefix = "__AUXILIARY_VAR__" cur_i = 0 while True: yield Symbol(f"{var_prefix}{cur_i}") cur_i += 1
def get_unique_var_name() -> Symbol: if not hasattr(get_unique_var_name, 'counter'): get_unique_var_name.counter = 1 # Here we just hope that user won't use so obscure name. s = Symbol(f'_system_{get_unique_var_name.counter}') get_unique_var_name.counter += 1 return s
def test_init(self): Symbol(1) Symbol('a') Symbol(None) Symbol(sum) Symbol((1, 2, 3)) Symbol([1, 2])
def test_simplify(self): s = Symbol(1) self.assertEqual(s.simplify(), s)
def test_literalize(self): s = Symbol(1) self.assertEqual(s.literalize(), s)
def test_printing(self): self.assertEqual('a', str(Symbol('a'))) self.assertEqual('1', str(Symbol(1))) self.assertEqual("Symbol('a')", repr(Symbol('a'))) self.assertEqual('Symbol(1)', repr(Symbol(1)))
def test_simplify_different_instances(self): s1 = Symbol(1) s2 = Symbol(1) self.assertEqual(s1.simplify(), s2.simplify())
def test_isliteral(self): self.assertTrue(Symbol(1).isliteral is True)
def solve(clause, clause2): if type(clause) == str: bl.parse(clause) if type(clause2) == str: bl.parse(clause2) # E.g: a and b if (type(clause) == boolean.Symbol and type(clause2) == boolean.Symbol) or (type(clause) == NOT and type(clause2) == NOT): return None # E.g: a or b and a or b elif clause == clause2: return None # E.g: not a and a elif type(clause) == NOT and type(clause2) == boolean.Symbol: if clause.args[0] == clause2: return True # E.g: a and not a elif type(clause) == boolean.Symbol and type(clause2) == NOT: if clause2.args[0] == clause: return True # E.g: a and (not a or b) elif type(clause) == boolean.Symbol and type(clause2) == OR: tmp = [arg for arg in clause2.args if not (type(arg) == NOT and arg.args[0] == clause)] try: return OR(*tmp) except TypeError: return tmp[0] # Eg: not a and (a or b) elif type(clause) == NOT and type(clause2) == OR: tmp = [arg for arg in clause2.args if arg != clause.args[0]] try: return OR(*tmp) except TypeError: return tmp[0] # E.g: (not a or b) and a elif type(clause) == OR and type(clause2) == boolean.Symbol: tmp = [arg for arg in clause.args if not (type(arg) == NOT and arg.args[0] == clause2)] try: return OR(*tmp) except TypeError: return tmp[0] # Eg: (a or b) and not a elif type(clause) == OR and type(clause2) == NOT: tmp = [arg for arg in clause.args if arg != clause2.args[0]] try: return OR(*tmp) except TypeError: return tmp[0] else: vetted = set() excluded = set() for arg in clause.args: # tmp = False for arg2 in clause2.args: # E.g: not a and a if type(arg) == NOT and type(arg2) == boolean.Symbol: if arg.args[0] == arg2: excluded.add(arg) excluded.add(arg2) vetted.discard(arg) vetted.discard(arg2) # E.g: a and not a elif type(arg) == boolean.Symbol and type(arg2) == NOT: if arg == arg2.args[0]: excluded.add(arg) excluded.add(arg2) vetted.discard(arg) vetted.discard(arg2) if arg2 not in excluded: vetted.add(arg2) if arg not in excluded: vetted.add(arg) if len(vetted) == 1: try: return Symbol(*vetted) except TypeError: quit() elif len(vetted) == 0: return None else: return OR(*vetted)