如何在Sparql中查询具有Object属性的类

use*_*508 10 rdf owl ontology rdfs sparql

有没有人知道如何在Sparql中查询具有对象属性的类?假设我们有一个包含以下内容的OWL文件

Human ----(hasPizza)---> Pizzas
Run Code Online (Sandbox Code Playgroud)

人类和比萨饼是课程(或概念).在SPARQL中,此查询不返回任何内容:

select ?x ?y where {
  ?x hasPizza ?y
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我在这些概念下添加两个人(或实体),比如

Human:Jim ----(hasPizza)---> Pizzas:cheesePizza
Run Code Online (Sandbox Code Playgroud)

该查询将返回?x=Jim,?y=cheesePizza 如何获取?x=Human?y=Pizza使用SPARQL?

Jos*_*lor 18

给定这样的数据(在RDF/XML中):

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:pizzas="http://example.org/pizzas#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Ontology rdf:about="http://example.org/pizzas"/>
  <owl:Class rdf:about="http://example.org/pizzas#Pizza"/>
  <owl:Class rdf:about="http://example.org/pizzas#Human"/>
  <owl:ObjectProperty rdf:about="http://example.org/pizzas#hasPizza">
    <rdfs:domain rdf:resource="http://example.org/pizzas#Human"/>
    <rdfs:range rdf:resource="http://example.org/pizzas#Pizza"/>
  </owl:ObjectProperty>
  <owl:NamedIndividual rdf:about="http://example.org/pizzas#Jim">
    <rdf:type rdf:resource="http://example.org/pizzas#Human"/>
    <pizzas:hasPizza>
      <owl:NamedIndividual rdf:about="http://example.org/pizzas#CheesePizza">
        <rdf:type rdf:resource="http://example.org/pizzas#Pizza"/>
      </owl:NamedIndividual>
    </pizzas:hasPizza>
  </owl:NamedIndividual>
</rdf:RDF>
Run Code Online (Sandbox Code Playgroud)

或者相同,在更易阅读的海龟中:

@prefix :        <http://example.org/pizzas#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix pizzas:  <http://example.org/pizzas#> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

pizzas:Jim
      a       pizzas:Human , owl:NamedIndividual ;
      pizzas:hasPizza pizzas:CheesePizza .

pizzas:hasPizza
      a       owl:ObjectProperty ;
      rdfs:domain pizzas:Human ;
      rdfs:range pizzas:Pizza .

pizzas:Human
      a       owl:Class .

pizzas:Pizza
      a       owl:Class .

<http://example.org/pizzas>
      a       owl:Ontology .

pizzas:CheesePizza
      a       pizzas:Pizza , owl:NamedIndividual .
Run Code Online (Sandbox Code Playgroud)

注意断言Jim hasPizza CheesePizza是图中的一个三元组.hasPizza对象属性的域和范围公理是两个三元组:hasPizza rdfs:domain HumanhasPizza rdfs:range Pizza.SPARQL查询将查询模式与图中的三元组进行匹配.因此,从以下查询:

prefix :        <http://example.org/pizzas#>

select ?x ?y where { 
  ?x :hasPizza ?y
}
Run Code Online (Sandbox Code Playgroud)

你会得到如下的结果

$ arq --data pizzas.ttl --query query.sparql
-----------------------
| x    | y            |
=======================
| :Jim | :CheesePizza |
-----------------------
Run Code Online (Sandbox Code Playgroud)

因为图中有一个三元组,其谓词是:hasPizza,并且该三元组有一个:Jim主题和:CheesePizza对象.听起来你实际上要求的:hasPizza属性的域和范围,这也很容易检索.使用这样的查询:

prefix :        <http://example.org/pizzas#>
prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>

select ?domain ?range where { 
  :hasPizza rdfs:domain ?domain ;
            rdfs:range ?range .
}
Run Code Online (Sandbox Code Playgroud)

你会得到这样的结果:

$ arq --data pizzas.ttl --query query.sparql
-------------------
| domain | range  |
===================
| :Human | :Pizza |
-------------------
Run Code Online (Sandbox Code Playgroud)