Пример #1
0
    def manual_constructs(self):
        p1 = c_const("p1")
        p2 = c_const("p2")
        p3 = c_const("p3")

        parent = c_pred("parent", 2)
        grandparent = c_pred("grandparent", 2)

        f1 = parent(p1, p2)
        f2 = parent(p2, p3)

        v1 = c_var("X")
        v2 = c_var("Y")
        v3 = c_var("Z")

        cl = grandparent(v1, v3) <= parent(v1, v2) & parent(v2, v3)

        assert isinstance(p1, Constant)
        assert isinstance(p2, Constant)
        assert isinstance(p3, Constant)

        assert isinstance(parent, Predicate)
        assert isinstance(grandparent, Predicate)

        assert isinstance(v1, Variable)
        assert isinstance(v2, Variable)
        assert isinstance(v2, Variable)

        assert isinstance(cl, Clause)

        assert isinstance(f1, Atom)
        assert isinstance(f2, Atom)
Пример #2
0
def gnu_test1(pl):
    #pl = GNUProlog()

    p = c_pred("p", 2)
    f = c_functor("t", 3)
    f1 = p("a", "b")

    pl.assertz(f1)

    X = c_var("X")
    Y = c_var("Y")

    query = p(X, Y)

    r = pl.has_solution(query)
    print("has solution", r)

    rv = pl.query(query)
    print("all solutions", rv)

    f2 = p("a", "c")
    pl.assertz(f2)

    rv = pl.query(query)
    print("all solutions after adding f2", rv)

    func1 = f(1, 2, 3)
    f3 = p(func1, "b")
    pl.assertz(f3)

    rv = pl.query(query)
    print("all solutions after adding structure", rv)

    l = List([1, 2, 3, 4, 5])

    member = c_pred("member", 2)

    query2 = member(X, l)

    rv = pl.query(query2)
    print("all solutions to list membership ", rv)

    r = c_pred("r", 2)
    f4 = r("a", l)
    f5 = r("a", "b")

    pl.asserta(f4)
    pl.asserta(f5)

    query3 = r(X, Y)

    rv = pl.query(query3)
    print("all solutions after adding list ", rv)

    del pl
Пример #3
0
    def graph_connectivity(self):
        v1 = c_const("v1")
        v2 = c_const("v2")
        v3 = c_const("v3")
        v4 = c_const("v4")

        edge = c_pred("edge", 2)
        path = c_pred("path", 2)

        f1 = edge(v1, v2)
        f2 = edge(v1, v3)
        f3 = edge(v2, v4)

        X = c_var("X")
        Y = c_var("Y")
        Z = c_var("Z")

        cl1 = path(X, Y) <= edge(X, Y)
        cl2 = path(X, Y) <= path(X, Z) & edge(Z, Y)

        solver = MuZ()

        solver.assert_fact(f1)
        solver.assert_fact(f2)
        solver.assert_fact(f3)

        solver.assert_rule(cl1)
        solver.assert_rule(cl2)

        # assert solver.has_solution(path(v1, v2))
        # assert solver.has_solution(path(v1, v4))
        # assert not solver.has_solution(path(v3, v4))
        #
        # assert len(solver.one_solution(path(v1, X))) == 1
        # assert len(solver.one_solution(path(X, v4))) == 1
        # assert len(solver.one_solution(path(X, Y))) == 2
        #
        # assert len(solver.all_solutions(path(v1, X))) == 3
        # assert len(solver.all_solutions(path(X, Y))) == 4

        assert solver.has_solution(path(v1, v2))
        assert solver.has_solution(path(v1, v4))
        assert not solver.has_solution(path(v3, v4))

        assert len(solver.query(path(v1, X), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, v4), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, Y), max_solutions=1)[0]) == 2

        assert len(solver.query(path(v1, X))) == 3
        assert len(solver.query(path(X, Y))) == 4
Пример #4
0
    def register_foreign(self, pyfunction, arity):
        cwrap = self._callback(arity)
        fwrap = self._foreign_wrapper(pyfunction)
        fwrap2 = cwrap(fwrap)
        self._wrap_refs_to_keep.append(fwrap2)

        res = swipy.swipy_register_foreign(pyfunction.__name__, arity, fwrap2, 0)

        return c_pred(pyfunction.__name__, arity)
Пример #5
0
    def shorthand_constructs(self):
        parent = c_pred("parent", 2)
        grandparent = c_pred("grandparent", 2)

        f1 = parent("p1", "p2")
        f2 = parent("p2", "p3")
        f3 = grandparent("p1", "X")

        assert isinstance(parent, Predicate)
        assert isinstance(grandparent, Predicate)

        assert isinstance(f1, Atom)
        assert isinstance(f2, Atom)
        assert isinstance(f3, Atom)

        assert isinstance(f1.arguments[0], Constant)
        assert isinstance(f1.arguments[1], Constant)
        assert isinstance(f3.arguments[1], Variable)
Пример #6
0
    def test5():
        solver = SWIProlog()

        edge = c_pred("edge", 2)
        path = c_pred("path", 2)

        f1 = edge("v1", "v2")
        f2 = edge("v1", "v3")
        f3 = edge("v2", "v4")

        X = c_var("X")
        Y = c_var("Y")
        Z = c_var("Z")

        cl1 = path(X, Y) <= edge(X, Y)
        cl2 = path(X, Y) <= edge(X, Z) & path(Z, Y)

        solver.assertz(f1)
        solver.assertz(f2)
        solver.assertz(f3)

        solver.assertz(cl1)
        solver.assertz(cl2)

        assert solver.has_solution(path("v1", "v2"))
        assert solver.has_solution(path("v1", "v4"))
        assert not solver.has_solution(path("v3", "v4"))

        assert len(solver.query(path("v1", X), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, "v4"), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, Y), max_solutions=1)[0]) == 2

        assert len(solver.query(path("v1", X))) == 3
        assert len(solver.query(path(X, Y))) == 4

        solver.assertz(edge("v4", "v5"))
        assert len(solver.query(path(X, Y))) == 7

        print(solver.query(edge(X, Y), edge(Y, Z), edge(Z,"W")))
        del solver
Пример #7
0
    def test2():
        pl = SWIProlog()

        bongard = c_pred('bongard', 2)
        circle = c_pred('circle', 2)
        inp = c_pred('in', 3)
        config = c_pred('pconfig', 3)
        triangle = c_pred('triangle', 2)
        square = c_pred('square', 2)

        pl.assertz(bongard(2, "la"))
        pl.assertz(circle(2, "o3"))
        pl.assertz(config(2, "o1", "up"))
        pl.assertz(config(2, "o2", "up"))
        pl.assertz(config(2, "o5", "up"))
        pl.assertz(triangle(2, "o1"))
        pl.assertz(triangle(2, "o2"))
        pl.assertz(triangle(2, "o5"))
        # pl.assertz(square(2, "o4"))
        pl.assertz(inp(2, "o4", "o5"))
        pl.assertz(inp(2, "o2", "o3"))

        A = c_var("A")
        B = c_var("B")
        C = c_var("C")
        D = c_var("D")

        #pl.assertz((bongard(A,"la") <= triangle(A,C) & inp(A, C, D)))

        res = pl.query(bongard(A, "la"), triangle(A,C), inp(A, C, D))

        print(res)

        del pl
Пример #8
0
    def test10():

        pl = SWIProlog()

        p = c_pred("p", 2)
        a = c_pred("a", 2)

        f1 = p("a", "b")

        pl.assertz(f1)

        #cl1 = (a("X", "Y") <= p("X", "Y"))
        cl2 = (a("X", "Y") <= a("X", "Z") & p("Z", "Y"))

        #pl.assertz(cl1)
        pl.assertz(cl2)

        q = a("X", "Y")

        print(pl.query(q, time_limit=1))

        del pl
Пример #9
0
    def test5():
        solver = XSBProlog("/Users/seb/Documents/programs/XSB")

        edge = c_pred("edge", 2)
        path = c_pred("path", 2)

        f1 = edge("v1", "v2")
        f2 = edge("v1", "v3")
        f3 = edge("v2", "v4")

        X = c_var("X")
        Y = c_var("Y")
        Z = c_var("Z")

        cl1 = (path("X", "Y") <= edge("X", "Y"))
        cl2 = (path("X", "Y") <= edge("X", "Z") & path("Z", "Y"))

        as1 = solver.assertz(f1)
        as2 = solver.assertz(f2)
        as3 = solver.assertz(f3)

        as4 = solver.assertz(cl1)
        solver.assertz(cl2)

        assert solver.has_solution(edge("X", "v2"))
        assert solver.has_solution(path("v1", "v4"))
        assert len(solver.query(path("v1", "X"), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, "v4"), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, Y), max_solutions=1)[0]) == 2

        assert len(solver.query(path("v1", X))) == 3
        assert len(solver.query(path(X, Y))) == 4

        solver.assertz(edge("v4", "v5"))
        assert len(solver.query(path(X, Y))) == 7

        print(solver.query(edge(X, Y), edge(Y, Z), edge(Z, "W")))

        del solver
Пример #10
0
    def test4():
        pl = SWIProlog()

        parent = c_pred("parent", 2)
        grandparent = c_pred("grandparent", 2)

        f1 = parent("p1", "p2")
        f2 = parent("p2", "p3")

        v1 = c_var("X")
        v2 = c_var("Y")
        v3 = c_var("Z")

        cl = (grandparent(v1, v3) <= parent(v1, v2) & parent(v2, v3))

        pl.assertz(f1)
        pl.assertz(f2)
        pl.assertz(cl)

        assert pl.has_solution(parent(v1, v2))
        assert not pl.has_solution(parent(v1, v1))
        assert len(pl.query(parent(v1, v2))) == 2
        assert len(pl.query(parent("p1", v1))) == 1
        assert pl.has_solution(parent("p1", "p2"))
        assert not pl.has_solution(parent("p2", "p1"))
        assert len(pl.query(parent("p1", v1), max_solutions=1)) == 1

        assert pl.has_solution(grandparent(v1, v2))
        assert pl.has_solution(grandparent("p1", v1))
        assert len(pl.query(grandparent("p1", v1), max_solutions=1)) == 1

        print(pl.query(grandparent(v1, v2)))

        pl.assertz(parent("p2", "p4"))
        pl.assertz(parent("p1", "p5"))
        print(pl.query(grandparent(v1, v2)))

        del pl
Пример #11
0
    def test6():
        solver = SWIProlog()

        head = c_pred("head", 2)
        tail = c_pred("tail", 2)
        take_second = c_pred("take_second", 2)
        H = c_var("Head")
        T = c_var("Tail")
        X = c_var("X")
        Y = c_var("Y")

        hatm1 = head(Pair(H, T), H)
        tatm1 = tail(Pair(H, T), T)
        cl = (take_second(X,Y) <= tail(X, T) & head(T, Y))

        solver.assertz(hatm1)
        solver.assertz(tatm1)
        solver.assertz(cl)

        l = List([1,2,3,4,5])
        print(solver.query(take_second(l, X)))

        del solver
Пример #12
0
    def test2():
        pl = XSBProlog("/Users/seb/Documents/programs/XSB")

        person = c_pred("person", 1)
        friends = c_pred("friends", 2)
        stress = c_pred("stress", 1)
        influences = c_pred("influences", 2)
        smokes = c_pred("smokes", 1)
        asthma = c_pred("asthma", 1)

        pl.assertz(person("a"))
        pl.assertz(person("b"))
        pl.assertz(person("c"))
        pl.assertz(friends("a", "b"))
        pl.assertz(friends("a", "c"))

        pl.assertz(stress("X") <= person("X"))
        pl.assertz(influences("X", "Y") <= person("X") & person("Y"))
        pl.assertz(smokes("X") <= stress("X"))
        pl.assertz(
            smokes("X") <= friends("X", "Y") & influences("Y", "X")
            & smokes("Y"))
        pl.assertz(asthma("X") <= smokes("X"))

        query_p = person("X")
        tv = pl.query(query_p)
        print("all persons: ", tv)

        query_f = friends("X", "Y")
        tv = pl.query(query_f)
        print("all friends: ", tv)

        query_st = stress("Y")
        tv = pl.query(query_st)
        print("all stressed people: ", tv)

        tv = pl.query(influences("X", "Y"))
        print("all influences: ", tv)

        tv = pl.query(smokes("X"))
        print("all smokers: ", tv)

        tv = pl.query(asthma("X"))
        print("all asthma: ", tv)

        del pl
Пример #13
0
    def test1():
        pl = SWIProlog()

        p = c_pred("p", 2)
        f = c_functor("t", 3)
        f1 = p("a", "b")

        pl.assertz(f1)

        X = c_var("X")
        Y = c_var("Y")

        query = p(X, Y)

        r = pl.has_solution(query)
        print("has solution", r)

        rv = pl.query(query)
        print("all solutions", rv)

        f2 = p("a", "c")
        pl.assertz(f2)

        rv = pl.query(query)
        print("all solutions after adding f2", rv)

        func1 = f(1, 2, 3)
        f3 = p(func1, "b")
        pl.assertz(f3)

        rv = pl.query(query)
        print("all solutions after adding structure", rv)

        l = List([1, 2, 3, 4, 5])

        member = c_pred("member", 2)

        query2 = member(X, l)

        rv = pl.query(query2)
        print("all solutions to list membership ", rv)

        r = c_pred("r", 2)
        f4 = r("a", l)
        f5 = r("a", "b")

        pl.asserta(f4)
        pl.asserta(f5)

        query3 = r(X, Y)

        rv = pl.query(query3)
        print("all solutions after adding list ", rv)

        # Foreign predicates

        def hello(t):
            print("Foreign: Hello", t)

        hello_pred = pl.register_foreign(hello, 1)
        # print(hello_pred)

        f_query = hello_pred("a")

        pl.has_solution(f_query)

        del pl
Пример #14
0
    def simple_grandparent(self):
        p1 = c_const("p1")
        p2 = c_const("p2")
        p3 = c_const("p3")

        parent = c_pred("parent", 2)
        grandparent = c_pred("grandparent", 2)

        f1 = parent(p1, p2)
        f2 = parent(p2, p3)

        v1 = c_var("X")
        v2 = c_var("Y")
        v3 = c_var("Z")

        cl = (grandparent(v1, v3) <= parent(v1, v2) & parent(v2, v3))

        solver = MuZ()

        solver.assert_fact(f1)
        solver.assert_fact(f2)
        solver.assert_rule(cl)

        # assert solver.has_solution(parent(v1, v2))
        # assert not solver.has_solution(parent(v1, v1))
        # assert len(solver.all_solutions(parent(v1, v2))) == 2
        # assert len(solver.all_solutions(parent(p1, v1))) == 1
        # assert solver.has_solution(parent(p1, p2))
        # assert not solver.has_solution(parent(p2, p1))
        # assert len(solver.one_solution(parent(p1, v1))) == 1
        #
        # assert solver.has_solution(grandparent(v1, v2))
        # assert solver.has_solution(grandparent(p1, v1))
        # assert len(solver.one_solution(grandparent(p1, v1))) == 1
        # assert solver.has_solution(grandparent(p1, p3))
        # assert not solver.has_solution(grandparent(p2, v1))
        # assert len(solver.one_solution(grandparent(p1, v1))) == 1
        # ans = solver.one_solution(grandparent(p1, v1))
        # assert ans[v1] == p3
        # ans = solver.one_solution(grandparent(v1, v2))
        # assert ans[v1] == p1 and ans[v2] == p3
        #
        # assert solver.has_solution(cl)
        # ans = solver.one_solution(cl)
        # assert ans[v1] == p1 and ans[v3] == p3
        # assert len(solver.all_solutions(cl)) == 1

        assert solver.has_solution(parent(v1, v2))
        assert not solver.has_solution(parent(v1, v1))
        assert len(solver.query(parent(v1, v2))) == 2
        assert len(solver.query(parent(p1, v1))) == 1
        assert solver.has_solution(parent(p1, p2))
        assert not solver.has_solution(parent(p2, p1))
        assert len(solver.query(parent(p1, v1), max_solutions=1)) == 1

        assert solver.has_solution(grandparent(v1, v2))
        assert solver.has_solution(grandparent(p1, v1))
        assert len(solver.query(grandparent(p1, v1), max_solutions=1)) == 1
        assert solver.has_solution(grandparent(p1, p3))
        assert not solver.has_solution(grandparent(p2, v1))
        assert len(solver.query(grandparent(p1, v1), max_solutions=1)) == 1
        ans = solver.query(grandparent(p1, v1), max_solutions=1)[0]
        assert ans[v1] == p3
        ans = solver.query(grandparent(v1, v2), max_solutions=1)[0]
        assert ans[v1] == p1 and ans[v2] == p3

        assert solver.has_solution(*cl.get_literals())
        ans = solver.query(*cl.get_literals(), max_solutions=1)[0]
        assert ans[v1] == p1 and ans[v3] == p3
        assert len(solver.query(*cl.get_literals())) == 1
Пример #15
0
    def test7(limit=10):
        pl = XSBProlog("/Users/seb/Documents/programs/XSB")

        p = c_pred("p", 2)
        f = c_functor("t", 3)
        f1 = p("a", "b")

        pl.assertz(f1)

        X = c_var("X")
        Y = c_var("Y")

        query = p(X, Y)

        r = pl.has_solution(query)
        print("has solution", r)

        rv = pl.query(query, time_limit=limit)
        print("all solutions", rv)

        f2 = p("a", "c")
        pl.assertz(f2)

        rv = pl.query(query, time_limit=limit)
        print("all solutions after adding f2", rv)

        func1 = f(1, 2, 3)
        f3 = p(func1, "b")
        pl.assertz(f3)

        rv = pl.query(query, time_limit=limit)
        print("all solutions after adding structure", rv)

        l = List([1, 2, 3, 4, 5])

        member = c_pred("member", 2)
        pl.use_module("lists", predicates=[member])

        query2 = member(X, l)

        rv = pl.query(query2, time_limit=limit)
        print("all solutions to list membership ", rv)

        r = c_pred("r", 2)
        f4 = r("a", l)
        f5 = r("a", "b")

        pl.asserta(f4)
        pl.asserta(f5)

        query3 = r(X, Y)

        rv = pl.query(query3, time_limit=limit)
        print("all solutions after adding list ", rv)

        q = c_pred("q", 2)
        cl = (q("X", "Y") <= r("X", "Y") & r("X", "Z"))

        pl.assertz(cl)
        query4 = q("X", "Y")
        rv = pl.query(query4, time_limit=limit)
        print("all solutions to q: ", rv)

        del pl