How to query Classes with Object Property in Sparql

后端 未结 1 1689
耶瑟儿~
耶瑟儿~ 2021-01-02 05:48

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
<         


        
1条回答
  •  渐次进展
    2021-01-02 06:11

    Given data like this (in RDF/XML):

    
      
      
      
      
        
        
      
      
        
        
          
            
          
        
      
    
    

    or the same, in the more readable Turtle:

    @prefix :         .
    @prefix rdfs:     .
    @prefix pizzas:   .
    @prefix owl:      .
    @prefix xsd:      .
    @prefix rdf:      .
    
    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 .
    
    
          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 :        
    
    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 :        
    prefix rdfs:    
    
    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 |
    -------------------
    

    0 讨论(0)
提交回复
热议问题