如何在Sparql中查询具有对象属性的类 [英] How to query Classes with Object Property in Sparql

查看:144
本文介绍了如何在Sparql中查询具有对象属性的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does any one know how to query Classes with Object Property in Sparql? Assume we have an OWL file which contains follows

Human ----(hasPizza)---> Pizzas

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

Human and Pizzas are classes (or concepts). In SPARQL, this query returns nothing:

select ?x ?y where {
  ?x hasPizza ?y
}

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

But if I add two individuals (or entities) under those concepts like

Human:Jim ----(hasPizza)---> Pizzas:cheesePizza

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

that query will return ?x=Jim and ?y=cheesePizza How can I get ?x=Human and ?y=Pizza using SPARQL?

推荐答案

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

Given data like this (in 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>

或相同,在更具可读性的Turtle中:

or the same, in the more readable Turtle:

@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 .

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

notice that the assertion Jim hasPizza CheesePizza is one triple in the graph. The domain and range axioms for the hasPizza object property are two triples: hasPizza rdfs:domain Human and hasPizza rdfs:range Pizza. SPARQL queries match query patterns against the triples in the graph. Thus, from a query like:

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

select ?x ?y where { 
  ?x :hasPizza ?y
}

您将获得诸如

$ arq --data pizzas.ttl --query query.sparql
-----------------------
| x    | y            |
=======================
| :Jim | :CheesePizza |
-----------------------

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

because there is one triple in the graph whose predicate is :hasPizza, and that triple has a :Jim as the subject, and :CheesePizza as the object. It sounds like you're actually asking for the domain and range of the :hasPizza property, which are also easy to retrieve. Use a query like this:

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 .
}

您将获得如下结果:

$ arq --data pizzas.ttl --query query.sparql
-------------------
| domain | range  |
===================
| :Human | :Pizza |
-------------------

这篇关于如何在Sparql中查询具有对象属性的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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