根据其他关系/比较集在SPARQL中查找个人 [英] Find individuals in SPARQL based on other relations / Compare sets

查看:77
本文介绍了根据其他关系/比较集在SPARQL中查找个人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出了分配了属性的对象:A,:B和:C,这些属性本身不是标量,而是具有键和值属性的对象.

Given are Objects :A, :B and :C which have properties assigned, whereas these properties are not scalar themselves but are also objects with key and value properties.

@prefix x: <http://example.com/example#> 

x:A x:hasProp x:Prop1 .
x:Prop1 x:Key "1" .
x:Prop1 x:Value "AA" .
x:B x:hasProp x:Prop2 .
x:Prop2 x:Key "1" .
x:Prop2 x:Value "AA" .
x:C x:hasProp x:Prop3 .
x:C x:hasProp x:Prop4 .
x:Prop3 x:Key "1" .
x:Prop3 x:Value "AA" .
x:Prop4 x:Key "2" .
x:Prop4 x:Value "BB" .

我如何断言:A和:B具有相同的属性,而:A和:C没有? 我是SPARQL的新手,我不知道... 我尝试过类似的东西:

How can I assert that :A and :B have the same properties whereas :A and :C not? I'm new to SPARQL and I have no idea... I tried something like :

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

select ?another ?k ?v
{x:A x:hasProp ?p .
?p ?k ?v .
?another x:hasProp ?p2 .   
?p2 ?k ?v .
}

但是我认为这是错误的方式.它还返回:C.

but I think it's a wrong way. It also returns :C.

如何轻松地在SPARQL中比较两组?

How can one easily compare two sets in SPARQL?

其他问题: 答案1的查询工作正常,但仅当直接使用:A时.使用变量代替:A也可以使:C符合条件.为什么?

Additional question: The query from the answer 1 works fine but only if :A used directly. Using a variable in place of :A makes :C also to qualify. Why?

我的意思是: 插入一些条件以找到:A

I mean: insert some condition to find :A

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

INSERT DATA {:A rdfs:label "A"}

,然后使用变量代替:A

and then use a variable in place of :A

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

select ?other ?k ?v {
  #-- Find ?other such that :A and ?other have
  #-- some property in common,
  ?a rdfs:label "A"
  ?a     :hasProp [ :Key ?k ; :Value ?v ] .
  ?other :hasProp [ :Key ?k ; :Value ?v ] .

  #-- but remove any ?other such that:
  filter not exists { 
#-- (i) :A has a property that ?other doesn't; 
  {
    ?a :hasProp [ :Key ?kk ; :Value ?vv ] .
    filter not exists { ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
     }
  }
  union
  #-- or (ii) ?other has a property that :A doesn't.
  {
    ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
    filter not exists {
         ?a :hasProp [ :Key ?kk ; :Value ?vv ] .
   }
 }
}  
}


更新:


Update:

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

INSERT DATA {
:A rdfs:label "A" .
:A :hasProp :Prop1 .
:Prop1 :Key "1" .
:Prop1 :Value "AA" .

:B :hasProp :Prop2 .
:Prop2 :Key "1" .
:Prop2 :Value "AA" .

:C :hasProp :Prop3 .
:C :hasProp :Prop4 .
:Prop3 :Key "1" .
:Prop3 :Value "AA" .

:Prop4 :Key "2" .
:Prop4 :Value "BB" .
}

使用:A

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

select ?other ?k ?v {
   :A    :hasProp [ :Key ?k ; :Value ?v ] .
   ?other :hasProp [ :Key ?k ; :Value ?v ] .
   filter not exists { 
     { :A :hasProp [ :Key ?kk ; :Value ?vv ] .
       filter not exists { ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
       }
     }
     union
     {
      ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
      filter not exists { :A :hasProp [ :Key ?kk ; :Value ?vv ] .
      }
   }
  }
 }

答案:

 -------------------  
 |other|  k  | v  
 |A    | "1" | "AA"  
 |B    | "1" | "AA"  
 -------------------  

使用变量?a进行查询:

Query using variable ?a:

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

select ?other ?k ?v {
   ?a rdfs:label "A" .
   ?a    :hasProp [ :Key ?k ; :Value ?v ] .
   ?other :hasProp [ :Key ?k ; :Value ?v ] .
   filter not exists { 
     { ?a :hasProp [ :Key ?kk ; :Value ?vv ] .
       filter not exists { ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
       }
     }
     union
     {
      ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
      filter not exists { ?a :hasProp [ :Key ?kk ; :Value ?vv ] .
      }
    }
   }
 }

返回

其他k v
A"1""AA"
B"1""AA"
C"1""AA"

other k v
A "1" "AA"
B "1" "AA"
C "1" "AA"

推荐答案

为什么您的查询未返回您想要的内容...

它总是有助于显示您实际得到的输出,因为这样您可以指出您不希望看到的部分.在这种情况下,您的查询返回:

Why your query doesn't return what you want…

It always helps to show the output that you're actually getting, because then you can point out which parts you're not expecting. In this case, your query returns:

---------------------------
| another | k      | v    |
===========================
| :C      | :Value | "AA" |
| :C      | :Key   | "1"  |
| :B      | :Value | "AA" |
| :B      | :Key   | "1"  |
| :A      | :Value | "AA" |
| :A      | :Key   | "1"  |
---------------------------

在您的情况下这很有意义,因为C确实具有您要查询的那种数据.这是您数据的这一部分:

This makes sense in your case because C does have the kind of data you're asking about. It's this portion of your data:

x:A x:hasProp x:Prop1 .
x:Prop1 x:Key "1" .
x:Prop1 x:Value "AA" .
…
x:C x:hasProp x:Prop3 .
…
x:Prop3 x:Key "1" .
x:Prop3 x:Value "AA" .

返回您想要的内容的查询

这是我编写查询以获取所需内容的方式.查找具有与:A相同的属性的东西很容易.从这些中,您需要过滤掉任何?other,以使:A具有?other不具有的属性,或者使?other具有:A不具有的属性,

A query that returns what you want

Here's how I'd write a query that gets you what you want. It's easy enough to find things that have some property in common with :A. From those, you need to filter out any ?other such that either :A has a property that ?other doesn't, or such that ?other has a property that :A doesn't,

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

select ?other ?k ?v {
  #-- Find ?other such that :A and ?other have
  #-- some property in common,
  :A     :hasProp [ :Key ?k ; :Value ?v ] .
  ?other :hasProp [ :Key ?k ; :Value ?v ] .

  #-- but remove any ?other such that:
  filter not exists { 
    #-- (i) :A has a property that ?other doesn't; 
    {
      :A :hasProp [ :Key ?kk ; :Value ?vv ] .
      filter not exists {
        ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
      }
    }
    union
    #-- or (ii) ?other has a property that :A doesn't.
    {
      ?other :hasProp [ :Key ?kk ; :Value ?vv ] .
      filter not exists {
        :A :hasProp [ :Key ?kk ; :Value ?vv ] .
      }
    }
  }
}

----------------------
| other | k   | v    |
======================
| :B    | "1" | "AA" |
| :A    | "1" | "AA" |
----------------------

枚举等效类

实际上,您可以使用对此的概括来列出数据中的不同等价类.

Enumerating the equivalence classes

In fact, you can use a generalization of this to list the different equivalence classes in the data.

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

select distinct ?x ?y {
  ?x :hasProp [] .
  ?y :hasProp [] .

  filter not exists { 
    {
      ?x :hasProp [ :Key ?kk ; :Value ?vv ] .
      filter not exists {
        ?y :hasProp [ :Key ?kk ; :Value ?vv ] .
      }
    }
    union
    {
      ?y :hasProp [ :Key ?kk ; :Value ?vv ] .
      filter not exists {
        ?x :hasProp [ :Key ?kk ; :Value ?vv ] .
      }
    }
  }
}
order by ?x ?y

-----------
| x  | y  |
===========
| :A | :A |
| :A | :B |
| :B | :A |
| :B | :B |
| :C | :C |
-----------

这篇关于根据其他关系/比较集在SPARQL中查找个人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆