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"))
感谢您的回答丰富。是的,我已经想过创建一个运行所有函子的cron任务,并将结果作为三元组插入到数据库中。这就像前向链接推理,但不是实时的。 :(所以没有最好的主意? – Aymeric