Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query Classes with Object Property in Sparql

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

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

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

like image 729
user2334508 Avatar asked Aug 27 '13 06:08

user2334508


People also ask

What is a SPARQL query?

SPARQL queries contain triple patterns, much like the data itself, which utilise the relationships to quickly navigate any linked data. This language is common for all linked data so queries can traverse across multiple RDF databases at once. Query 7 in the next section is an example of this powerful characteristic.

How to use RDF with a SPARQL query?

So open the RDF version of your ontology and look the construct; you can then try to match it with your SPARQL query. It sounds much more likely that you have an object property hasPizza, the rdfs:domain and rdfs:range of which are Human and Pizzas, respectively. This is relatively easy to search for too, though, but it is a distinct task.

What is the easiest way to learn SPARQL?

The easiest way to start learning SPARQL is by example so lets start with a simple query and build from it. The following query returns every triple in the triplestore:

Does ontop support SPARQL queries under OWL 2 QL?

But, the validation of ontology produced has been manually. We present an extension of the ontology-based data access platform Ontop that supports answering SPARQL queries under the OWL 2 QL direct semantics entailment regime for data instances stored in relational databases.


1 Answers

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>

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 .

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
}

you will get results such as

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

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

and you'll get results like this:

$ arq --data pizzas.ttl --query query.sparql
-------------------
| domain | range  |
===================
| :Human | :Pizza |
-------------------
like image 74
Joshua Taylor Avatar answered Nov 05 '22 08:11

Joshua Taylor