示例#1
0
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
示例#2
0
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"
示例#3
0
def test_parser_hardcore():
    p = Parser()
    comparator = TermComparator()
    for i in range(0, 100):
        term = RandomTermGenerator.generate_random_term(6, 10)
        term_cnf = p.convert_to_cnf(term)

        assert comparator.terms_are_equivalent(term, term_cnf)
示例#4
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"
示例#5
0
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"
示例#6
0
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
示例#7
0
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
示例#8
0
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)
示例#9
0
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 _call(self, city):
     response = requests.get(f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}")
     if not response.ok:
         return "not_found"
     parsed = Parser().parse(response.json())#class exctracting needed params and returns a dict
     obj = ApiRequests(city=parsed['city'], sky=parsed['sky'],
                 temperature=parsed['temperature'], pressure=parsed['pressure'],
                 humidity=parsed['humidity'], windspeed=parsed['windspeed'],
                 updated=datetime.strptime(parsed['updated'], '%Y-%m-%d %H:%M:%S'))
     db.session.add(obj)
     db.session.commit()
     self.weather = {"city" : parsed['city'], 'sky': parsed['sky'],
                 'temperature': parsed['temperature'], 'pressure': parsed['pressure'],
                 'windspeed': parsed['windspeed'],
                 'humidity': parsed['humidity']}
     return parsed
示例#11
0
def test_get_all_models():
    p = Parser()
    y = "a"
    p.get_all_models(y)
    file = open("allmodels", "r")
    content = file.read()
    assert "1 0" == content
    file.close()
    file = open("allmodels", "w+")
    file.close()
    y = "And(a,b)"
    p.get_all_models(y)
    file = open("allmodels", "r")
    content = file.read()
    assert "1 ß 0" == content
    file.close()
    file = open("allmodels", "w+")
    file.close()
示例#12
0
import subprocess
from Core.Parser import Parser

#y = "Or(Or(a,b),Or(d,c))"
y = "Or(Not(a),b)"
#y = "a"
#y = "Impl(Or(a,b),c)"
#x = Parser.convert_formula_to_dmacs(y)
z = Parser()
f = "Impl(BOT,a)"
f = "And(BOT,a)"
f = "And(b,Impl(BOT,a))"
f = "And(b,BOT)"
f = "And(And(Impl(a,b),Impl(b,a)),Impl(TOP,a))"

#f = Parser.create_term(f)

f = Parser.convert_formula_to_dmacs(f, True)
x = 1
#if content == prob:
#        print("yes")
#else:
#print("no")f
#print(content2)
#print(prob)
#y= Parser.convert_formula_to_dmacs(y)
#print(y)
示例#13
0
def buildcmpl(program):
    program = Parser.create_term(program)
    headtobodies = {}

    return program
示例#14
0
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))
示例#15
0
def Aufgabe3b():
    input_s = 'And(Impl(Not(a),b),Impl(Not(b),a))'
    Parser.get_all_models(input_s, True)
示例#16
0
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"
示例#17
0
def test_get_all_models():
    input_s = 'And(Impl(Not(a),b),Impl(Not(b),a))'
    Parser.get_all_models(input_s, True)
示例#18
0
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"
示例#19
0
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"
示例#20
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)
示例#21
0
def test_cc_2():
    input1 = und(impl('a', 'b'), impl('b', 'a'))
    output1 = Parser.create_clarks_completion(input1)
    input1 = impl(nicht('b'), 'a')
    output1 = Parser.create_clarks_completion(input1)
    x = 0