2011-07-13 21 views
3

使用Allegrograph,Prolog仿函数非常棒,但有一个缺点。Allegrograph - 函数类似于RDF对象的属性?

比方说,你定义了连接两个实体仿函数,例如parentOf这等于“N:!motherOf OR N:!fatherOf”这是你的本体定义的两个RDF对象的属性(不仿函数)。

让我们定义三元组“A !n:fatherOf B”。由于“parentOf”是一个仿函数,而不是rdf对象的属性,所以如果您请求链接A和B的所有属性,则只会获得三元组“A !n:fatherOf B”(但不是“父元素B“)。

要知道A是B的父亲的唯一方法是直接询问布尔问题。

所以我的问题是:如何轻松获得“获得由函数生成的RDF三元组+由函数生成的所涉事实?”的结果?

回答

2

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")) 
+0

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

0

你想要这样的东西吗?

(<-- (parentOf ?a ?b) 
    (or 
     (q ?a !n:fatherOf ?b) 
     (q ?a !n:motherOf ?b))) 

(select (?a ?b) 
    (parentOf ?a ?b)) 

select语句将返回涉及n:fatherOf或n:motherOf的三元组。

相关问题