def test_init(self): Symbol(1) Symbol('a') Symbol(None) Symbol(sum) Symbol((1, 2, 3)) Symbol([1, 2])
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_simplify(self): s = Symbol(1) self.assertEqual(s.simplify(), s)
def test_literalize(self): s = Symbol(1) self.assertEqual(s.literalize(), s)
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)