Retrieve objects from RDF triples

Viewed 160

With the following snippet from the pizza ontology:

<owl:Class rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#Pizza">
   <rdfs:subClassOf rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#Food"/>
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#hasBase"/>
                <owl:someValuesFrom rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#PizzaBase"/>
            </owl:Restriction>
        </rdfs:subClassOf>
        <rdfs:label xml:lang="en">Pizza</rdfs:label>
        <rdfs:seeAlso rdf:resource="https://en.wikipedia.org/wiki/Pizza"/>
        <skos:prefLabel xml:lang="en">Pizza</skos:prefLabel>
    </owl:Class>

I see from this that the following triple exists: <Pizza, hasBase, PizzaBase> representing <subject, predicate, object>

How do I write SPARQL to extract the the PizzaBase object or any object from a triplet when the entity and relation are known?

Note: I am equating subject as entity and predicate as relation

Update: Let me simplify my question. Based on the given RDF above and the following RDF graph:

enter image description here

What would be the SPARQL to extract the PizzaBase entity given the Pizza entity and the hasBase relation?

1 Answers

Enclose known subjects in <> tags. It's also possible to use SPARQL BIND to bind the URI to a variable. For predicates just be sure to prefix them, shown below.

Assuming that you know the identifier of the Pizza node, it should be...

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT ?pizzaBase WHERE { 
   <pizza> pizza:hasBase ?pizzaBase .
}

To get all pizza bases from pizzas, you can do...

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT ?pizza ?pizzaBase WHERE { 
   ?pizza pizza:hasBase ?pizzaBase .
}
Related