SPARQL query to retrieve all objects and properties
Asked Answered
C

1

9

Using the Wine ontology, I want to create SPARQL query so I can retrieve all wines and their properties like the table below - consider that I don't know the properties' names a priori.

vin                  | rdf:type     | vin:hasMaker      |  vin:hasSugar   | ...
==========================================================================  ...
GaryFarrellMerlot    |  vin:Merlot  | vin:Elyse         |  vin:Dry        | ...
--------------------------------------------------------------------------
ElyseZinfandel       |  vin:Elyse   | vin:GaryFarrell   |  vin:Dry        | ...
...

Can someone give me a hint?

-- EDIT

It is not possible to have the query result in the format I mentioned, but I can have it this way:

vin                |   property     |  value
=================================================
GaryFarrellMerlot  |   rdf:type     |  vin:Merlot
-------------------------------------------------
GaryFarrellMerlot  |   rdf:hasMaker |  vin:Elyse
-------------------------------------------------
...

With this select (thanks cygri):

SELECT DISTINCT ?wine ?property ?value
WHERE { 
       ?o1 a ?class .
       ?wine a ?o1 .
       ?wine ?property ?value .
}

This will take wines instead of type of wines (Merlot -> GaryFarrellMerlot). The only problem is it takes wine but also Winery, Regions, Flavours, etc and I wanted only wines and its properties. Besides, there's no such attribute vin:Merlot rdfs:subClassOf vin:Wine. Any hint?

Cristiano answered 20/4, 2012 at 18:46 Comment(0)
P
12

You can't. You need to know beforehand what columns you want in your query result.

Listing all properties of wines isn't very hard of course:

SELECT DISTINCT ?property
WHERE { 
    ?wine a vin:Wine .
    ?wine ?property ?value .
}

Then you need to write some code that creates the final SPARQL query from the list of results.

Palaeography answered 20/4, 2012 at 19:46 Comment(1)
Well, the wine ontology is not a simple RDF model but a complex OWL ontology. So to do more complex things, like inferring that Merlot is a subclass of Wine, you need an OWL reasoner (or probably a triple store that supports OWL).Palaeography

© 2022 - 2024 — McMap. All rights reserved.