Reputation: 3577
I'm currently creating an ontology for a project that deals with sensor data. I successfully created an RDF graph using RDFLib in Python. And then stored the graph using Jena TDB database. Now I want to query the database using SPARQL but I honestly don't know where to start.
For example if I m using already a knowing vocabulary lets say Friend of a Friend (FOAF
) then I will simply execute a query something like this and I will get what I need:
SELECT ?name
WHERE {
?person foaf:name ?name .
}
But since I'm creating my RDF using different ontologies (a sensor ontologies) my aim is to execute this query and get all the data related:
SELECT * {?dev ?lon ?lat}
where dev
is the device name , lon
is the longitude where the device is located and lat
is the latitude. All this data are provided within the graph but its not yet defined to be queried.
To summarize, I'm trying to figure out a prefixed name for classes and so on, for example the class device can be represented by (dev, device...)
Can assigning labels in RDFLib be the solution or do I have to develop a vocabulary for querying ?
Upvotes: 1
Views: 1081
Reputation: 4001
RDF triples are designed to represent data with a subject, object, predicate structure. For example, the following is an RDF triple:
<http://example.org/FalaG> foaf:name "FalaG"^^xsd:string
...where foaf:name
is a prefixed qname that expands to <http://xmlns.com/foaf/0.1/name>
.
SPARQL is basically the same as the above triple (this is Turtle text serialization), except that you can introduce variables prefixed by a ?
. SPARQL will then find matches in the data and bind the variables for each match.
The name of the variable has no inherent meaning other than as a variable name. So the following would find the above triple, amongst others that match:
SELECT ?p
WHERE {
?p foaf:name ?o
}
Note I've purposefully put in nonsense variable names to outline that there is no inherent meaning to the variable names.
In terms of finding the URIs, you can use SPARQL to discover what values are found in the repository. Building on the previous query, you can find whatever other information is in the repository for FalaG
with the following:
SELECT ?person ?p ?o
WHERE {
?person foaf:name "FalaG"^^xsd:string .
?person ?p ?o
}
To discover URIs for other properties, etc, use the following:
SELECT ?s ?p ?o
WHERE {
?s ?p ?o
} LIMIT
The LIMIT
is important here, because any triple pattern with three variables is requesting all triples, which will likely timeout or crash the server. But by setting the limit, you can find how the data is modeled, then substitute values in ?s
, '?pand
?o`, etc. to look in more detail at specific parts of the data.
And yea, there are lots of sources for learning more about RDF and SPARQL.
Upvotes: 1