def test_de_Morgan(): x = ("Not(Or(a,b))") y = Parser() x = y.create_term(x) x = y.de_morgan(x) assert x.operator == "And" assert x.parameters[0].operator == "Not" assert x.parameters[1].operator == "Not" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[1].parameters[0].operator == "b" x = "Not(Not(a))" x = y.create_term(x) x = y.de_morgan(x) assert x.operator == "a" x = ("Not(And(a,b))") x = y.create_term(x) x = y.de_morgan(x) assert x.operator == "Or" assert x.parameters[0].operator == "Not" assert x.parameters[1].operator == "Not" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[1].parameters[0].operator == "b" x = "Not(Impl(a,b))" x = y.create_term(x) assert x.operator == "Not" x = y.de_morgan(x) assert x.operator == "Not" assert x.parameters[0].operator == "Impl" x = "Not(Not(a))" x = y.create_term(x) x = y.de_morgan(x) assert x.operator == "a"
def test_applyDistributiveLaw(): y = Parser() x = "Or(And(a,b),c)" x = y.create_term(x) x = y.apply_distributive_law(x) assert x.operator == "And" assert x.parameters[0].operator == "Or" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[0].parameters[1].operator == "c" x = "Or(c,And(a,b))" x = y.create_term(x) x = y.apply_distributive_law(x) assert x.operator == "And" assert x.parameters[0].operator == "Or" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[0].parameters[1].operator == "c" x = "Or(And(a,b),And(Impl(a,b),c))" x = y.create_term(x) x = y.apply_distributive_law(x) assert x.operator == "And" assert x.parameters[0].operator == "And" assert x.parameters[0].parameters[0].operator == "Or" assert x.parameters[0].parameters[1].operator == "Or" assert x.parameters[1].parameters[0].operator == "Or" assert x.parameters[1].parameters[1].operator == "Or" assert x.parameters[0].parameters[0].parameters[0].operator == "a" assert x.parameters[0].parameters[0].parameters[1].operator == "Impl" assert x.parameters[0].parameters[1].parameters[0].operator == "a" assert x.parameters[0].parameters[1].parameters[1].operator == "c" assert x.parameters[1].parameters[0].parameters[0].operator == "b" assert x.parameters[1].parameters[0].parameters[1].operator == "Impl" assert x.parameters[1].parameters[1].parameters[0].operator == "b" assert x.parameters[1].parameters[1].parameters[1].operator == "c"
def test_buildString(): x = "Or(And(a,b),And(x,y))" y = Parser() x = y.create_term(x) x = y.convert_to_cnf(x) x = y.build_string(x) assert x[0] == "a"
def test_r_minus(): input_string = 'And(Impl(a,b),Impl(b,a))' input_term = Parser.create_term(input_string) loops = Parser.create_loops(input_term) r_minus = [] Parser.create_r_minus(loops[0], input_term, r_minus) assert len(r_minus) == 0
def test_replace_implication(): x = "Impl(a,b)" y = Parser() x = y.create_term(x) x = y.replace_implication(x) assert x.operator == "Or" assert x.parameters[0].operator == "Not" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[1].operator == "b"
def test_isClause(): x = "Not(Or(a,b))" y = Parser() x = y.create_term(x) b = y.is_clause(x) assert False == b x = "And(Or(a,b))" x = y.create_term(x) b = y.is_clause(x) assert False == b x = "Or(Or(a,b),v)" x = y.create_term(x) b = y.is_clause(x) assert True == b x = "Or(Or(And(a,b)),v)" x = y.create_term(x) b = y.is_clause(x) assert False == b
def test_create_loop(): input_string = 'And(Impl(a,b),Impl(b,a))' input_term = Parser.create_term(input_string) loops = Parser.create_loops(input_term) assert len(loops) == 1 assert loops[0][0].content == 'a' assert loops[0][1].content == 'b' assert len(loops[0][0].successors) == 1 assert len(loops[0][1].successors) == 1 assert loops[0][0].successors[0].content == 'b' assert loops[0][1].successors[0].content == 'a' #input_string = 'And(Impl(Not(a),b),Impl(b,Not(a)))' #input_term = Parser.create_term(input_string) #loops = Parser.create_loops(input_term) #assert len(loops) == 1 #assert loops[0][0].content == '-a' #assert loops[0][1].content == 'b' #assert len(loops[0][0].successors) == 1 #assert len(loops[0][1].successors) == 1 #assert loops[0][0].successors[0].content == 'b' #assert loops[0][1].successors[0].content == '-a' input_string = 'And(Impl(Not(a),b),Impl(b,a))' input_term = Parser.create_term(input_string) loops = Parser.create_loops(input_term) assert len(loops) == 0 #input_string = 'And(And(Impl(Not(a),b),Impl(b,Not(a))),Impl(c,Not(a)))' #input_term = Parser.create_term(input_string) #loops = Parser.create_loops(input_term) #assert len(loops) == 1 #assert loops[0][0].content == '-a' #assert loops[0][1].content == 'b' #assert len(loops[0][0].successors) == 2 #assert len(loops[0][1].successors) == 1 #assert loops[0][0].successors[0].content == 'b' #assert loops[0][0].successors[1].content == 'c' #assert loops[0][1].successors[0].content == '-a' x = 0
def test_clarks_completion(): comparator = TermComparator() input1 = Parser.create_term('Impl(Not(a),b)') output1 = Parser.create_clarks_completion(input1) expected_output1 = Term('And', [ Term('BiImpl', [Term('Not', [Term('a')]), Term('b')]), Term('BiImpl', [Term('a', []), Term('BOT')]) ]) assert comparator.terms_are_equal(output1, expected_output1) input1 = Parser.create_term('And(Impl(And(a,b),p),Impl(c,p))') expected_output_string = 'And(And(And(BiImpl(Or(And(a,b),c),p),BiImpl(a,BOT)),BiImpl(b,BOT)),BiImpl(c,BOT))' expected_output1 = Parser.create_term(expected_output_string) output1 = Parser.create_clarks_completion(input1) assert comparator.terms_are_equal(output1, expected_output1) input1 = Parser.create_term( 'And(And(Impl(And(a,b),p),Impl(c,p)),And(Impl(And(b,c),x),Impl(a,x)))') expected_output_string = 'And(And(And(And(BiImpl(Or(And(a,b),c),p),BiImpl(Or(And(b,c),a),x)),BiImpl(a,BOT)),BiImpl(b,BOT)),BiImpl(c,BOT))' expected_output1 = Parser.create_term(expected_output_string) output1 = Parser.create_clarks_completion(input1) assert comparator.terms_are_equal(output1, expected_output1) # Not in header-atom -> not allowed # input1 = Parser.create_term('And(And(Impl(And(a,b),Not(p)),Impl(c,Not(p))),And(Impl(And(b,c),x),Impl(a,x)))') # expected_output_string = 'And(And(And(And(BiImpl(Or(And(a,b),c),Not(p)),BiImpl(Or(And(b,c),a),x)),BiImpl(a,BOT)),BiImpl(b,BOT)),BiImpl(c,BOT))' # expected_output1 = Parser.create_term(expected_output_string) # output1 = Parser.create_clarks_completion(input1) # assert comparator.terms_are_equal(output1, expected_output1) input1 = Parser.create_term( 'And(And(Impl(And(a,Not(b)),p),Impl(c,p)),And(Impl(And(Not(b),c),x),Impl(a,x)))' ) expected_output_string = 'And(And(And(And(BiImpl(Or(And(a,Not(b)),c),p),BiImpl(Or(And(Not(b),c),a),x)),BiImpl(a,BOT)),BiImpl(b,BOT)),BiImpl(c,BOT))' expected_output1 = Parser.create_term(expected_output_string) output1 = Parser.create_clarks_completion(input1) assert comparator.terms_are_equal(output1, expected_output1) input1 = Parser.create_term('And(Impl(TOP,p),Impl(And(a,b),BOT))') expected_output_string = "And(And(And(BiImpl(TOP,p),BiImpl(And(a,b),BOT)),BiImpl(a,BOT)),BiImpl(b,BOT))" expected_output1 = Parser.create_term(expected_output_string) output1 = Parser.create_clarks_completion(input1) assert comparator.terms_are_equal(output1, expected_output1)
def test_stabilisation(): comparator = TermComparator() input_string = 'And(And(Impl(Not(a),b),Impl(b,Not(a))),Impl(c,Not(a)))' input_term = Parser.create_term(input_string) result = Parser.stabilize_formula(input_term) expected_output_string = "And(And(And(BiImpl(Not(a),b),BiImpl(Or(b,c),Not(a))),BiImpl(c,BOT)),Impl(Not(c),Not(Not(a))))" expected_output = Parser.create_term(expected_output_string) assert comparator.terms_are_equal(result, expected_output) input_string = 'And(And(And(And(Impl(Not(a),b),Impl(b,Not(a))),Impl(c,Not(a))),Impl(d,Not(a))),Impl(e,b))' input_term = Parser.create_term(input_string) result = Parser.stabilize_formula(input_term) clark_heads = und(biimpl(oder(nicht('a'), 'e'), 'b'), biimpl(oder(oder('b', 'c'), 'd'), nicht('a'))) clark_bodies = und(und(biimpl('c', 'BOT'), biimpl('d', 'BOT')), biimpl('e', 'BOT')) loops = impl(nicht(oder(oder('c', 'd'), 'e')), und(nicht(nicht('a')), nicht('b'))) expected_output = und(und(clark_heads, clark_bodies), loops) #assert comparator.terms_are_equivalent(clark_heads, result.parameters[0].parameters[0]) #assert comparator.terms_are_equivalent(clark_bodies, result.parameters[0].parameters[1]) assert comparator.terms_are_equal(loops, result.parameters[1]) #assert comparator.terms_are_equivalent(result, expected_output) a=1
def test_loop_formulas(): input_string = 'And(And(And(And(Impl(Not(a),b),Impl(b,Not(a))),Impl(c,Not(a))),Impl(d,e)),Impl(e,d))' input_term = Parser.create_term(input_string) loops = Parser.create_loops(input_term) r_minus = [] r_minus_2 = [] Parser.create_r_minus(loops[0], input_term, r_minus) Parser.create_r_minus(loops[1], input_term, r_minus_2) assert len(r_minus) == 1 loop_formula_1 = Parser.create_loop_formula(r_minus) expected_loop_formula_1_string = 'Impl(Not(c),Not(Not(a)))' expected_loop_formula_1 = Parser.create_term( expected_loop_formula_1_string) assert TermComparator.get_instance().terms_are_equal( loop_formula_1, expected_loop_formula_1) assert len(r_minus_2) == 0 loop_formula_2 = Parser.create_loop_formula(r_minus_2) expected_loop_formula_2_string = 'BOT' expected_loop_formula_2 = Parser.create_term( expected_loop_formula_2_string) assert TermComparator.get_instance().terms_are_equal( loop_formula_2, expected_loop_formula_2)
def test_convertToCNF(): y = Parser() x = "a" x = y.create_term(x) x = y.convert_to_cnf(x) assert x.operator == "a" x = "Or(And(a,b),Impl(c,d))" x = y.create_term(x) x = y.convert_to_cnf(x) assert x.operator == "And" assert x.parameters[0].operator == "Or" assert x.parameters[1].operator == "Or" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[0].parameters[1].operator == "Or" x = "Not(And(a,b))" x = y.create_term(x) x = y.convert_to_cnf(x) assert x.operator == "Or" assert x.parameters[0].operator == "Not" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[1].operator == "Not" assert x.parameters[1].parameters[0].operator == "b" x = "Not(Impl(a,b))" x = y.create_term(x) x = y.convert_to_cnf(x) assert x.operator == "And" assert x.parameters[0].operator == "a" assert x.parameters[1].operator == "Not" assert x.parameters[1].parameters[0].operator == "b" x = "Not(Not(Impl(a,b)))" x = y.create_term(x) x = y.convert_to_cnf(x) assert x.operator == "Or" assert x.parameters[0].operator == "Not" assert x.parameters[0].parameters[0].operator == "a" assert x.parameters[1].operator == "b"
def buildcmpl(program): program = Parser.create_term(program) headtobodies = {} return program
from Core.Parser import Parser, Term print(Parser.convert_formula_to_dmacs("And(x,y)")) #parser = Parser() megaformel1 = "Or(Impl(Not(a),And(b,Not(a))),And(d,Or(e,f)))" megaformel1AlsTerm = Parser.create_term(megaformel1) megaformel1InCnf = Parser.convert_to_cnf(megaformel1AlsTerm) print( Parser.convert_formula_to_dmacs( "Or(Impl(Not(a),And(b,Not(a))),And(d,Or(e,f)))")) megaformel2a = "Or(Impl(Not(a),And(b,Not(g))),And(d,Or(e,f)))" megaformel2 = "Impl(And(g,a)," + megaformel2a + ")" #megaforme21AlsTerm = parser.create_term(megaformel2) #megaformel1InCnf = parser.convertToCNF(megaforme21AlsTerm) print(Parser.convert_formula_to_dmacs(megaformel2))
def test_create_term(): p = Parser() x = "Or(a,b)" x = p.create_term(x) assert x.operator == "Or" assert len(x.parameters) == 2 assert x.parameters[0].operator == "a" assert x.parameters[1].operator == "b" n = "Not(f)" n = p.create_term(n) assert n.operator == "Not" assert len(n.parameters) == 1 assert n.parameters[0].operator == "f" phi = "And(Or(x,b),y)" phi = p.create_term(phi) assert phi.operator == "And" assert len(phi.parameters) == 2 assert phi.parameters[0].operator == "Or" assert phi.parameters[1].operator == "y" psi = "BiImpl(Or(x,b),y)" psi = p.create_term(psi) assert psi.operator == "BiImpl" assert len(psi.parameters) == 2 assert psi.parameters[0].operator == "Or" assert psi.parameters[1].operator == "y" x = "Impl(Or(a,b),n)" x = p.create_term(x) assert x.operator == "Impl" assert len(x.parameters) == 2 assert x.parameters[0].operator == "Or" assert x.parameters[1].operator == "n" x = "Impl(And(a,b),n)" x = p.create_term(x) assert len(x.parameters) == 2 assert x.parameters[0].operator == "And" assert x.parameters[1].operator == "n" x = "Impl(Not(a),n)" x = p.create_term(x) assert len(x.parameters) == 2 assert x.parameters[0].operator == "Not" assert x.parameters[1].operator == "n" assert len(x.parameters[0].parameters) == 1 x = "Not(Or(a,n))" x = p.create_term(x) assert x.parameters[0].operator == "Or" assert x.operator == "Not" x = "Not(And(a,n))" x = p.create_term(x) assert x.parameters[0].operator == "And" assert x.operator == "Not" x = "Not(Impl(a,n))" x = p.create_term(x) assert x.parameters[0].operator == "Impl" assert x.operator == "Not" x = "Or(And(a,b),And(x,y))" x = p.create_term(x) assert x.operator == "Or" assert x.parameters[0].operator == "And" assert x.parameters[1].operator == "And" x = "Or(And(Not(a),b),And(x,y))" x = p.create_term(x) assert x.operator == "Or" assert x.parameters[0].operator == "And" assert x.parameters[1].operator == "And"