Beispiel #1
0
 def test_prolog_strings(self):
     """
     See: https://github.com/yuce/pyswip/issues/9
     """
     p = pl.Prolog()
     p.assertz('some_string_fact("abc")')
     self.assertEqual([{"S": b"abc"}], list(p.query("some_string_fact(S)")))
Beispiel #2
0
 def test_prolog_functor_in_list(self):
     p = pl.Prolog()
     p.assertz('f([g(a,b),h(a,b,c)])')
     self.assertEqual([{
         "L": [u"g(a, b)", u"h(a, b, c)"]
     }], list(p.query("f(L)")))
     p.retract("f([g(a,b),h(a,b,c)])")
Beispiel #3
0
    def test_nested_queries(self):
        """
        SWI-Prolog cannot have nested queries called by the foreign function
        interface, that is, if we open a query and are getting results from it,
        we cannot open another query before closing that one.

        Since this is a user error, we just ensure that a appropriate error
        message is thrown.
        """

        p = pl.Prolog()

        # Add something to the base
        p.assertz("father(john,mich)")
        p.assertz("father(john,gina)")
        p.assertz("mother(jane,mich)")

        somequery = "father(john, Y)"
        otherquery = "mother(jane, X)"

        # This should not throw an exception
        for _ in p.query(somequery):
            pass
        for _ in p.query(otherquery):
            pass

        with self.assertRaises(pl.NestedQueryError):
            for q in p.query(somequery):
                for j in p.query(otherquery):
                    # This should throw an error, because I opened the second
                    # query
                    pass
Beispiel #4
0
 def test_prolog_read_file(self):
     """
     See: https://github.com/yuce/pyswip/issues/10
     """
     prolog = pl.Prolog()
     prolog.consult("tests/test_read.pl")
     list(prolog.query('read_file("tests/test_read.pl", S)'))
Beispiel #5
0
 def test_prolog_functor_in_functor(self):
     p = pl.Prolog()
     p.assertz("f([g([h(a,1), h(b,1)])])")
     result = list(p.query('f(G)'))
     self.assertEqual('[g([h(a, 1), h(b, 1)])]', str(result[0]['G']))
     p.assertz("a([b(c(x), d([y, z, w]))])")
     self.assertEqual("[b(c(x), d([y, z, w]))]",
                      str(next(p.query('a(B)'))['B']))
     p.retract("f([g([h(a,1), h(b,1)])])")
     p.retract("a([b(c(x), d([y, z, w]))])")
Beispiel #6
0
    def test_quoted_strings(self):
        """
        See: https://github.com/yuce/pyswip/issues/90
        """
        p = pl.Prolog()
        self.assertEqual([{"X": b"a"}], list(p.query('X = "a"')))

        p.assertz('test_quoted_strings("hello","world")')
        self.assertEqual([{
            "A": b"hello",
            "B": b"world"
        }], list(p.query('test_quoted_strings(A,B)')))
Beispiel #7
0
 def test_prolog_functor_in_functor(self):
     p = pl.Prolog()
     p.assertz("f([g([h(a,1), h(b,1)])])")
     self.assertEqual([{
         'G': [u"g([u'h(a, 1)', u'h(b, 1)'])"]
     }], list(p.query('f(G)')))
     p.assertz("a([b(c(x), d([y, z, w]))])")
     self.assertEqual([{
         'B': [u"b(c(x), d(['y', 'z', 'w']))"]
     }], list(p.query('a(B)')))
     p.retract("f([g([h(a,1), h(b,1)])])")
     p.retract("a([b(c(x), d([y, z, w]))])")
Beispiel #8
0
    def test_functor_return(self):
        """
        pyswip should generate string representations of query results
        that are at least meaningful, preferably equal to what
        SWI-Prolog would generate. This test checks if this is true for
        `Functor` instance results.

        Not a formal issue, but see forum topic:
        https://groups.google.com/forum/#!topic/pyswip/Mpnfq4DH-mI
        """

        import pyswip.prolog as pl

        p = pl.Prolog()

        # Add a simple grammar to the base
        p.consult("test_functor_return.pl")

        query = "sentence(Parse_tree, [the,bat,eats,a,cat], [])"
        expectedTree = "s(np(d(the), n(bat)), vp(v(eats), np(d(a), n(cat))))"

        # This should not throw an exception
        results = list(p.query(query))
        self.assertEqual(len(results), 1,
                         "Query should return exactly one result")

        ptree = results[0]["Parse_tree"]
        self.assertEqual(ptree, expectedTree)

        # A second test, based on what was posted in the forum
        p.assertz("friend(john,son(miki))")
        p.assertz("friend(john,son(kiwi))")
        p.assertz("friend(john,son(wiki))")
        p.assertz("friend(john,son(tiwi))")
        p.assertz("father(son(miki),kur)")
        p.assertz("father(son(kiwi),kur)")
        p.assertz("father(son(wiki),kur)")

        soln = [
            s["Y"]
            for s in p.query("friend(john,Y), father(Y,kur)", maxresult=1)
        ]
        self.assertEqual(soln[0], "son(miki)")
Beispiel #9
0
 def test_prolog_functor_in_list(self):
     p = pl.Prolog()
     p.assertz('f([g(a,b),h(a,b,c)])')
     result = list(p.query("f(L)"))
     self.assertEqual('[g(a, b), h(a, b, c)]', str(result[0]['L']))
     p.retract("f([g(a,b),h(a,b,c)])")