Ejemplo n.º 1
0
 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"))])
Ejemplo n.º 2
0
 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"))])
Ejemplo n.º 3
0
 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")]))
Ejemplo n.º 4
0
 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_)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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")
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 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"))])
Ejemplo n.º 10
0
 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_)}")
Ejemplo n.º 11
0
 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_)
Ejemplo n.º 12
0
 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_)
Ejemplo n.º 13
0
 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")])))
Ejemplo n.º 14
0
 def test_occurs_check(self):
     v = Variable("E")
     t = Function("cons", [Atom("a"), Atom("b"), Variable("E")])
     #assert
     (self.occurs_check(v, t))
Ejemplo n.º 15
0
 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")]))
Ejemplo n.º 16
0
 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_)
Ejemplo n.º 17
0
    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_)}")
Ejemplo n.º 18
0
 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")]))
Ejemplo n.º 19
0
 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_)}")
Ejemplo n.º 20
0
 def test_final_1_2():
     assert ((interpreter.variables_of_term(
         Function("p",
                  [Variable("X"), Variable("Y"),
                   Atom("a")]))) == set([Variable("X"),
                                         Variable("Y")]))
Ejemplo n.º 21
0
 def father_consts(x, y):
     return father(Atom(x), Atom(y))
Ejemplo n.º 22
0
 def atom(self, value):
     return Atom(value.value)
Ejemplo n.º 23
0
 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_)