prolog - Allegrograph - 像 RDF 对象属性这样的仿函数?

标签 prolog functor inference allegrograph

使用 Allegrograph,Prolog 仿函数非常棒,但有一个缺点。

假设您定义了一个链接两个实体的仿函数,例如 parentOf 等于“!n:motherOf OR !n:fatherOf”,它们都是在你的本体中定义的 rdf 对象属性(不是仿函数)。

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

要知道 A 是否是 B 的父级的唯一方法是直接问 bool 问题。

所以我的问题是:如何轻松获得“获取由 FACTS + INFERRED FACTS 生成的 RDF 三元组组成的结果?”

最佳答案

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

关于prolog - Allegrograph - 像 RDF 对象属性这样的仿函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676740/

相关文章:

prolog - 如何在Prolog中返回推荐列表?

c++ - 将共享指针存储在 lambda 中以使其保持事件状态

c++ - 贝叶斯网络中的推理

list - prolog 中的 '|' 字符如何追加而不是拆分列表?

prolog - 纯prolog小程序源码

Prolog 将字符串列表转换为数字列表

c++ - 语法帮助。模板函数对象中的模板运算符()

c++ - 仿函数和对象继承

typescript - 使 TypeScript 推断高阶函数的模板参数

flutter - 我可以向下转换一个类,使它丢失原始的构造函数吗?