def test_final_4_4(): print(f"\nProgram: {list2str(pstark)}") g = [ancestor(Atom("rickard"), Atom("ned"))] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(pstark, g) print(f"Solution: {list2str(g_)}") assert (g_ == [ancestor(Atom("rickard"), Atom("ned"))])
def test_final_4_6(): g = [ancestor(Variable("X"), Atom("robb"))] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(pstark, g) print(f"Solution: {list2str(g_)}") assert (g_ == [ancestor(Atom("ned"), Atom("robb"))] or g_ == [ancestor(Atom("rickard"), Atom("robb"))])
def test_variables_of_clause(self): c = Rule( Function("p", [Variable("X"), Variable("Y"), Atom("a")]), RuleBody([Function( "q", [Atom("a"), Atom("b"), Atom("a")])])) #assert (self.variables_of_clause(c) == set([Variable("X"), Variable("Y")]))
def test_final_2_3(): s = {Variable(("Y")): Number("0"), Variable("X"): Variable(("Y"))} r = Rule((Function( "p", [Variable("X"), Variable("Y"), Atom("a")])), RuleBody([])) r_ = Rule( (Function("p", [Variable("Y"), Number("0"), Atom("a")])), RuleBody([])) assert (interpreter.substitute_in_clause(s, r) == r_)
def test_unify(self): t1 = Function("f", [Variable("X"), Variable("Y"), Variable("Y")]) t2 = Function("f", [Variable("Y"), Variable("Z"), Atom("a")]) u = { Variable("X"): Atom("a"), Variable("Y"): Atom("a"), Variable("Z"): Atom("a") } #assert (self.unify(t1, t2) == u)
def test_challenge_2(): print(f"\nProgram:{list2str(pstark)}") # Tests backtracking g = [ancestor(Atom("rickard"), Atom("robb"))] print(f"Goal: {list2str(g)}") g_ = interpreter.det_query(pstark, g) assert (len(g_) == 1) g_ = g_[0] print(f"Solution: {list2str(g_)}") assert (g_ == g)
def test_challenge_1(): print("\n\n###################################################") print("###### Testing the deterministic interpreter ######") print("###################################################") print(f"Program: {list2str(psimple)}") # Tests query failure g = [Function("f", [Atom("a"), Atom("c")])] print(f"Goal: {list2str(g)}") assert (interpreter.det_query(psimple, g) == []) print("Solution: Empty solution")
def test_final_3_9(): t1 = Function("f", [Variable("X"), Variable("Y"), Variable("Y")]) t2 = Function("f", [Variable("Y"), Variable("Z"), Atom("a")]) u = { Variable("X"): Atom("a"), Variable("Y"): Atom("a"), Variable("Z"): Atom("a") } #print("9") assert (interpreter.unify(t1, t2) == u)
def test_challenge_3(): # Tests choice points g = [ancestor(Variable("X"), Atom("robb"))] print(f"Goal: {list2str(g)}") g_ = interpreter.det_query(pstark, g) assert (len(g_) == 2) g1, g2 = g_[0], g_[1] print(f"Solution: {list2str(g1)}") print(f"Solution: {list2str(g2)}") assert (g1 == [ancestor(Atom("ned"), Atom("robb"))]) assert (g2 == [ancestor(Atom("rickard"), Atom("robb"))])
def test_final_4_1(): print( "\n\n################################################################" ) print( "###### Testing the non-deterministic abstract interpreter ######") print( "################################################################") print(f"Program: {list2str(psimple)}") g = [Function("f", [Atom("a"), Atom("b")])] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(psimple, g) assert (g_ == [Function("f", [Atom("a"), Atom("b")])]) print(f"Solution: {list2str(g_)}")
def test_final_2_4(): s = {Variable(("Y")): Number("0"), Variable("X"): Variable(("Y"))} p = Function("p", [Variable("X"), Variable(("Y")), Atom(("a"))]) q = Function("q", [Atom(("a")), Atom(("b")), Atom(("a"))]) p_ = Function("p", [Variable(("Y")), Number("0"), Atom(("a"))]) q_ = Function("q", [Atom(("a")), Atom(("b")), Atom(("a"))]) r = Rule(p, RuleBody([q])) r_ = Rule(p_, RuleBody([q_])) assert (interpreter.substitute_in_clause(s, r) == r_)
def test_substitute_in_clause(self): s = {Variable("Y"): Number(0), Variable("X"): Variable("Y")} p = Function("p", [Variable("X"), Variable("Y"), Atom("a")]) q = Function("q", [Atom("a"), Atom("b"), Atom("a")]) p_ = Function("p", [Variable("Y"), Number(0), Atom("a")]) q_ = Function("q", [Atom("a"), Atom("b"), Atom("a")]) r = Rule(p, [q]) r_ = Rule(p_, [q_]) #assert (self.substitute_in_clause(s, r) == r_)
def test_final_1_3(): r = Rule((Function( "p", [Variable("X"), Variable("Y"), Atom("a")])), RuleBody([])) assert ((interpreter.variables_of_clause(r) == set( [Variable("X"), Variable("Y")])))
def test_occurs_check(self): v = Variable("E") t = Function("cons", [Atom("a"), Atom("b"), Variable("E")]) #assert (self.occurs_check(v, t))
def test_variables_of_term(self): t = Function("f", [Variable("X"), Variable("Y"), Atom("a")]) #assert (self.variables_of_term(t) == set([Variable("X"), Variable("Y")]))
def test_final_2_2(): s = {Variable(("Y")): Number("0"), Variable("X"): Variable(("Y"))} t = Function("p", [Variable("X"), Variable("Y"), Atom("a")]) t_ = Function("p", [Variable("Y"), Number("0"), Atom("a")]) assert (interpreter.substitute_in_term(s, t) == t_)
def test_final_3_9(): t1 = Function("f", [Variable("X"), Variable("Y"), Variable("Y")]) t2 = Function("f", [Variable("Y"), Variable("Z"), Atom("a")]) u = { Variable("X"): Atom("a"), Variable("Y"): Atom("a"), Variable("Z"): Atom("a") } #print("9") assert (interpreter.unify(t1, t2) == u) def list2str(l): return ('(' + (',' + ' ').join(list(map(str, l))) + ')') # Test on a simple program psimple = [Rule(Function("f", [Atom("a"), Atom("b")]), RuleBody([]))] def test_final_4_1(): print( "\n\n################################################################" ) print( "###### Testing the non-deterministic abstract interpreter ######") print( "################################################################") print(f"Program: {list2str(psimple)}") g = [Function("f", [Atom("a"), Atom("b")])] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(psimple, g) assert (g_ == [Function("f", [Atom("a"), Atom("b")])]) print(f"Solution: {list2str(g_)}")
def test_final_1_4(): h = Function("p", [Variable("X"), Variable("Y"), Atom("a")]) b = [Function("q", [Atom("a"), Atom("b"), Atom("a")])] r = Rule(h, RuleBody(b)) assert (interpreter.variables_of_clause(r) == set( [Variable("X"), Variable("Y")]))
def test_final_4_3(): g = [Function("f", [Variable("X"), Variable("Y")])] print(f"Goal: {list2str(g)}") g_ = interpreter.nondet_query(psimple, g) assert (g_ == [Function("f", [Atom("a"), Atom("b")])]) print(f"Solution: {list2str(g_)}")
def test_final_1_2(): assert ((interpreter.variables_of_term( Function("p", [Variable("X"), Variable("Y"), Atom("a")]))) == set([Variable("X"), Variable("Y")]))
def father_consts(x, y): return father(Atom(x), Atom(y))
def atom(self, value): return Atom(value.value)
def test_substitute_in_term(self): s = {Variable("Y"): Number(0), Variable("X"): Variable("Y")} t = Function("f", [Variable("X"), Variable("Y"), Atom("a")]) t_ = Function("f", [Variable("Y"), Number(0), Atom("a")]) #assert (self.substitute_in_term(s, t) == t_)