【问题标题】:Allegrograph - Functors like RDF objects properties?Allegrograph - 像 RDF 对象属性这样的函子?
【发布时间】:2011-07-13 09:22:33
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: prolog functor inference allegrograph


    【解决方案1】:

    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 任务。这就像一个前向链接推理,但不是实时的。 :( 所以没有最好的主意?
    【解决方案2】:

    你想要这样的东西吗?

    (<-- (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 的三元组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      相关资源
      最近更新 更多