簡體   English   中英

SPARQL查詢具有相同屬性的個人

[英]SPARQL query for individuals with same properties

我想找出所有與其他人具有相同屬性的人。 想象一下,如果有不同的購物清單,上面有不同的物品,則可以購買或租用(對象屬性)。 為了使事情變得更復雜,我還想支付一定的停車費並行駛一定距離(數據屬性)。 同時,存在不同的商店提供不同的商品。 作為一個懶惰的人,我想為每個列表標識商店,其中提供了列表中的所有商品。

我相信這是對這個問題的概括,但是我不知何故無法解決這個問題

我創建了一些樣本個人:

@prefix : <http://www.shopping.org/model#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.shopping.org/model> .

<http://www.shopping.org/model> rdf:type owl:Ontology .

#    Object Properties
:buy rdf:type owl:ObjectProperty .
:rent rdf:type owl:ObjectProperty .

#    Data properties
:distance rdf:type owl:DatatypeProperty .
:parking rdf:type owl:DatatypeProperty .

#    Classes
:Product rdf:type owl:Class .
:ShoppingList rdf:type owl:Class .
:Store rdf:type owl:Class .

#    Individuals
:Apples rdf:type owl:NamedIndividual ,
                 :Product .
:Cereal rdf:type owl:NamedIndividual ,
                 :Product .
:List1 rdf:type owl:NamedIndividual ,
                :ShoppingList ;
       :buy :Apples ,
            :Milk ;
       :distance "9.0"^^xsd:float ;
       :parking "10.0"^^xsd:float .
:List2 rdf:type owl:NamedIndividual ,
                :ShoppingList ;
       :buy :Cereal ,
            :Milk ;
       :rent :TV ;
       :distance "5.0"^^xsd:float ;
       :parking "10.0"^^xsd:float .
:Milk rdf:type owl:NamedIndividual ,
               :Product .
:Store1 rdf:type owl:NamedIndividual ,
                 :Store ;
        :buy :Apples ,
             :Cereal ,
             :Milk ,
             :TV ;
        :distance "9.0"^^xsd:float ;
        :parking "10.0"^^xsd:float .
:Store2 rdf:type owl:NamedIndividual ,
                 :Store ;
        :buy :Cereal ,
             :Milk ;
        :rent :TV ;
        :distance "5.0"^^xsd:float ;
        :parking "10.0"^^xsd:float .
:TV rdf:type owl:NamedIndividual ,
             :Product .

#    General axioms

[ rdf:type owl:AllDisjointClasses ;
  owl:members ( :Product
                :ShoppingList
                :Store
              )
] .

並嘗試了一些首次查詢。 這是我最好的:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX : <http://www.shopping.org/model#>
SELECT DISTINCT ?list ?store ?prop 
WHERE {
    ?list a :ShoppingList . 
    ?store a :Store . 
    ?list ?prop [] . 

    FILTER NOT EXISTS { 
        ?list ?prop ?value . 
        FILTER NOT EXISTS { 
            ?store ?prop ?value . 
        } 
    } 
}
ORDER BY ?list ?store 

但是,此查詢返回商店和列表的所有組合,因為每個商店的停車收費為10.0f。 如何只過濾那些滿足列表所有要求的商店? 另一個假設是,可能存在除購買租賃之外的其他業務模型,以及其他感興趣的標准,即數據屬性。 這就是為什么我不想指定這些屬性,而是想使用變量的原因。

這個查詢做了我打算做的事情:

PREFIXES [as above]
PREFIX : <http://www.shopping.org/model#>
SELECT DISTINCT ?list ?store 
    WHERE {
        ?list a :ShoppingList . 
        ?store a :Store . 

        FILTER NOT EXISTS { 
            ?compat a owl:DatatypeProperty 
            FILTER NOT EXISTS {
                ?list ?compat ?value . 
                ?store ?compat ?value . 
            }
        } 

        FILTER NOT EXISTS {
            ?subset a owl:ObjectProperty . 
            ?list ?subset ?value . 
            FILTER NOT EXISTS {
                ?store ?subset ?value . 
            }
        }
    }
    ORDER BY ?list ?store

我的錯誤實際上只是我在過濾器之外定義了?prop 這導致它們成為解決方案的一部分,即我無法通過過濾器刪除整個商店。

如果您願意具體說明事物需要共有的特定屬性,則可以執行以下操作。 我已經清理了您的數據,因為它實際上沒有必要的前綴聲明。 如果您希望人們能夠使用您所提供的內容, 提供完整的數據。 我沒有添加正確的前綴,但足以使事情正常進行。

樣本數據

@prefix :      <urn:ex:> .
@prefix owl:   <file:///home/taylorj/tmp/data.ttl> .
@prefix xsd:   <file:///home/taylorj/tmp/data.ttl> .

:Store1  a         owl:NamedIndividual , :Store ;
        :buy       :Apples , :Cereal , :Milk , :TV ;
        :distance  "9.0"^^owl:float ;
        :parking   "10.0"^^owl:float .

:List2  a          owl:NamedIndividual , :ShoppingList ;
        :buy       :Cereal , :Milk ;
        :distance  "5.0"^^owl:float ;
        :parking   "10.0"^^owl:float ;
        :rent      :TV .

:List1  a          owl:NamedIndividual , :ShoppingList ;
        :buy       :Apples , :Milk ;
        :distance  "9.0"^^owl:float ;
        :parking   "10.0"^^owl:float .

:Store2  a         owl:NamedIndividual , :Store ;
        :buy       :Cereal , :Milk ;
        :distance  "5.0"^^owl:float ;
        :parking   "10.0"^^owl:float ;
        :rent      :TV .

具體解決方案

首先,我們可以解決以下特定情況:我們需要一個人擁有一個屬性的另一個屬性值的子集buy ),並擁有其他屬性的相交值( distanceparking )。

詢問

prefix : <urn:ex:>

select ?list ?store {

  #-- For each list and store
  ?list a :ShoppingList .
  ?store a :Store .

  #-- Check that they have the same distance and parking.
  ?distance ^:distance ?list, ?store .
  ?parking  ^:parking  ?list, ?store .

  #-- Check that every item to buy (or rent) on the list is also
  #-- in the store to buy (or rent).
  filter not exists {
    values ?get { :buy :rent }
    ?list ?get ?item .
    filter not exists {
      ?store ?get ?item
    }
  }
}

結果

--------------------
| list   | store   |
====================
| :List1 | :Store1 |
| :List2 | :Store2 |
--------------------

一般的做法

通常,考慮消除不需要的結果會很有幫助。 在這種情況下,您具有兩種屬性:(i)個體必須具有兼容值的屬性,以及(ii)一個個體必須具有另一個體的值的超集的屬性。 檢查這些條件是否不成立並不難:

prefix : <urn:ex:>

select ?list ?store {

  #-- For each list and store
  ?list a :ShoppingList .
  ?store a :Store .

  #-- Check that for each "compatible property" ?compat,
  #-- there must be some value that the ?list and the
  #-- ?store have in common.
  filter not exists {
    values ?compat { :distance :parking }
    filter not exists {
      ?list ?compat ?value .
      ?store ?compat ?value .
    }
  }

  #-- Check that for each "subset property" ?subset,
  #-- there is no value that the ?list contains that
  #-- the store does not.
  filter not exists {
    values ?subset { :buy :rent }
    ?list ?subset ?value
    filter not exists {
      ?store ?subset ?value
    }
  }
}

結果(相同)

--------------------
| list   | store   |
====================
| :List1 | :Store1 |
| :List2 | :Store2 |
--------------------

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM