sparql“diff”(减去一些非共享变量)

时间:2017-08-09 13:17:57

标签: sparql

这是我的问题Compare models for identity, but with variables? Construct with minus?的后续内容。要么我忘记了我学到的东西,要么就是我没有像我想的那样学到什么。

我有这样的三倍:

prefix : <http://example.com/>

:rose :color :red .
:violet :color :blue .
:rose a :flower .
:flower rdfs:subClassOf :plant .
:dogs :love :trucks .

我想发现我的三元店中的任何三元组至少不满足这些规则

  1. :rose作为主题
  2. :rose的父类作为主题
  3. 使用:rose作为主题
  4. 的任何三元组中使用的任何谓词
  5. 对象作为主题的任何三元组的对象 {/ 1}}。
  6. 因此,在这种情况下,异常发现查询(选择或构造)应该只返回

    :rose

    此查询显示在Triplestore中的内容:

    :dogs :love :trucks .
    

    PREFIX : <http://example.com/>
    construct where {
        :rose ?p ?o  .
        :rose a ?c .
        ?c rdfs:subClassOf ?super .
        ?s ?p ?x1 .
        ?x2 ?x3 ?o
    }
    

    有没有办法从三元组+---+---------+-----------------+---------+ | | subject | predicate | object | +---+---------+-----------------+---------+ | A | :flower | rdfs:subClassOf | :plant | | B | :rose | :color | :red | | C | :rose | rdf:type | :flower | | D | :violet | :color | :blue | +---+---------+-----------------+---------+ 中的所有内容中减去该模式,即使我使用{ ?s ?p ?o }?s和{{1}以外的变量名称在?p声明中?

    我见过this post有比较RDF的策略,但我想用标准的SPARQL来做。

    为了将其与我之前的帖子结合在一起,这个最终查询错误地暗示了几个所需的三元组违反了邮件顶部设置的规则

    ?o

    三E确实不受欢迎。但是A是理想的,因为它有construct作为主题规则2 ),并且需要三倍D,因为它的谓词也用于一些三元组:rose作为主题规则3 )。

       +---------+-----------------+---------+
     E | :dogs   | :love           | :trucks |
     A | :flower | rdfs:subClassOf | :plant  |
     D | :violet | :color          | :blue   |
       +---------+-----------------+---------+
    

1 个答案:

答案 0 :(得分:4)

如果我理解正确,您希望在数据中允许四种类型的三元组。如果您的数据中包含三元组(s,p,o),则它应至少满足以下条件之一:

  1. s = rose (关于玫瑰)
  2. p = rdfs:subClassOf,数据包含(rose,a,s)(关于某种类型)
  3. 数据还包含(rose,p,x)(共享谓词)
  4. 数据还包含(rose,q,o)(共享对象)
  5. 为每一个编写模式很容易。您只需找到每个三元组(s,p,o)并过滤掉那些不符合这些条件的三元组。我想你可以这样做:

    select ?s ?p ?o {
      ?s ?p ?o 
      filter not exists {
              { values ?s { :rose } }                       #-- (1)
        union { values ?p { rdfs:subClassOf } :rose a ?s }  #-- (2)
        union { :rose ?p ?x }                               #-- (3)
        union { :rose ?x ?o }                               #-- (4)
      }
    }
    
相关问题