Prolog 函子是 Prolog 程序的一部分。当您使用 Prolog 在 AllegroGraph 存储上编写查询时,您实际上是在编写一个 Prolog 程序,该程序为程序中表达的约束导出一组答案。
parentOf不是商店的一部分,它是程序的一部分。
您要做的是将 Prolog 程序所隐含的知识具体化,以便它以与基本三元组相同的形式可用。
为此,您需要编写一个... Prolog 程序,该程序导出推断的知识并将其添加到存储中。
这是一些应该有帮助的代码。这是 Lisp 中的一些设置和示例,但 Prolog 函子应该是显而易见的。
;; Assume the prefix is set up. These are for the Lisp environment.
(register-namespace "ex" "http://example.com/")
(enable-!-reader)
;; Define functors for basic relationships.
(<-- (parent ?x ?y)
(father ?x ?y))
(<- (parent ?x ?y)
(mother ?x ?y))
(<-- (male ?x)
(q- ?x !ex:sex !ex:male))
(<-- (female ?x)
(q- ?x !ex:sex !ex:female))
(<-- (father ?x ?y)
(male ?x)
(q- ?x !ex:has-child ?y))
(<-- (mother ?x ?y)
(female ?x)
(q- ?x !ex:has-child ?y))
;; Functors for adding triples.
(<-- (a- ?s ?p ?o)
;; Fails unless all parts ground.
(lisp (add-triple ?s ?p ?o)))
(<-- (a- ?s ?p ?o ?g)
;; Fails unless all parts ground.
(lisp (add-triple ?s ?p ?o ?g)))
(<-- (a-- ?s ?p ?o)
;; Fails unless all parts ground.
(lispp (not (get-triple :s ?s :p ?p :o ?o)))
(lisp (add-triple ?s ?p ?o)))
(<-- (a-- ?s ?p ?o ?g)
;; Fails unless all parts ground.
(lispp (not (get-triple :s ?s :p ?p :o ?o :g ?g)))
(lisp (add-triple ?s ?p ?o ?g)))
;; Add some sample data.
(create-triple-store "/tmp/foo")
(add-triple !ex:john !ex:sex !ex:male)
(add-triple !ex:dave !ex:sex !ex:male)
(add-triple !ex:alice !ex:sex !ex:female)
(add-triple !ex:alice !ex:has-child !ex:dave)
(add-triple !ex:john !ex:has-child !ex:dave)
;; Now who is a parent of whom?
(select (?parent ?child)
(parent ?parent ?child))
;; Returns:
;; (("http://example.com/john" "http://example.com/dave")
;; ("http://example.com/alice" "http://example.com/dave"))
;; Add the triples.
(select (?parent ?child) ; never succeeds
(parent ?parent ?child)
(a-- ?parent !ex:parentOf ?child)
(fail))
;; Now see what's in the store using the materialized triples.
(select (?parent ?child)
(q- ?parent !ex:parentOf ?child))
;; Returns:
;; (("http://example.com/john" "http://example.com/dave")
;; ("http://example.com/alice" "http://example.com/dave"))