RDF + OWL reasoning - sql

Let's suppose I have an RDF data about Socrates. The data is shown below
subject, predicate, object
man, being, mortal
Socrates, being, man
To check whether Socrates is mortal I have a request
SELECT *
FROM RDFData t1
JOIN RDFData t2
ON t1.subject = t2.object
Then I have a filter on "Socrates" and "mortal" and if result is not empty, then Socrates is mortal.
It works fine, but my teacher asks to add OWL information.
For example, if we have the next data
subject, predicate, object
man, being, mortal
Socrates, being, Greek
Greek, being, man
My approach does not work, because we have additional step in the chain.
I need to add an OWL static data here and implement a request for arbitrary number of steps in the chain.
What are my next steps?

If we turn your example data into actual RDF (using Turtle syntax), you'd get something like this:
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix : <http://example.org/> .
:socrates rdf:type :Greek .
:Man rdfs:subClassOf :Mortal .
:Greek rdfs:subClassOf :Man .
If you upload this data into an RDF database (such as RDF4J, Jena, GraphDB, Stardog, Blazegraph, or a host of other options), you can use the following SPARQL query:
ASK WHERE { :socrates rdf:type/rdfs:subClassOf* :Mortal }
This checks if :socrates is of a type that is either :Mortal, or a (direct or indirect) subclass of :Mortal. It returns true if Socrates is a mortal, false otherwise. You don't even need a reasoner for this, you can just use the expressivity of the query language.
If your RDF database supports basic RDFS reasoning, you can simplify your query even further:
ASK WHERE { :socrates rdf:type :Mortal }

Related

Unexpected behavior in Visual Graph, and when returning triples with SPARQL

How are you?
I 'm taking my first steps with GraphDb and I've found a couple of behaviors that I don't quite understand. Let me build the case.
I have the following Ontology, which I have loaded into GraphDb.
enter image description here
Let's only consider the tree under Persona.
When I go to Visual Graph and type :Persona in the search bar, this is what returns:
enter image description here
As you can see all the nodes corresponding to the ontology are there, but also some other nodes like Class (in different colors), and Thing and Nothing. (Ignore Paul which is an instance I added)
What are these other nodes? and how can I prevent them from appearing in this view?
Now, when I query with SPARQL, and I run
select * where {
?s ?p ?o .
}
If I have "Include inferred data in results" Off, I get this
enter image description here
Which is fine. It's what I would expect.
But ... when I turn inference On , which I believe is one of the true powers of working with rdf, I get around 800 records with all sort of triples with definitions for owl and rdf generic objects.
In order to get only my stuff and keep the inference capability on, I filtered as follows
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX pe: <http://www.semanticlab.com/ontologias/EmpresasYPersonas#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
select * where {
?s ?p ?o .
FILTER (REGEX(str(?s),"Persona*" )).
FILTER (!REGEX(str(?o),"owl*" )).
FILTER (!REGEX(str(?o),"rdf*" )).
}
order by ?s
Which returns
enter image description here
Here I see some inferencing, for example Paul classified as a Persona, which is not explicitly stated.
But on the other hand there are a bunch of new triples that I don't understand nor want, as they don't bring any value: for example the ones with owl:sameAs, owl:equivalentClass, or the ones stating that Persona is a subClassOf Persona (which in fact I believe is wrong).
Could you please explain why this is happening, and how to prevent this behavior?
I'm aware that I might be making some mistakes, so if you spot any, please let me know.

How does a SPARQL type expression work internally (e.g. ?a rdf:type ?b)?

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX my: <http://www.ex.com#>
SELECT distinct ?person ?nationality
WHERE{
?person rdf:type ?p.
?person my:Nationality ?nationality.
filter (?nationality = "Some nationality")
}
I have created an ontology in Protege which describes a music production company. I have trouble understanding the SPARQL queries and the way they work.
Some explanations
?person: variable in which I want to output people. They are individuals in Protege
?nationality a variable in which I want to output nationalities. They are
data properties in Protege
Nationality: data property which contains nationalities as strings and every person has one
my: prefix I created
How does this work ?person rdf:type ?p and how does it select the right type? Does it work automatically? I don't feel like I have set the ?person variable as a type Person variable (which is a class I have created and it describes a person as an entity), even though it outputs exactly the outcome I need.
How does this work "?person rdf:type ?p" and how does it select the right type? I don't feel like I have set the ?person variable as a type Person variable (which is a class I have created and it describes a person as an entity), even though it outputs exactly the outcome I need.
It doesn't select the right type. It selects a type, any type (and also any individual). Presumably the reason you are seeing the expect outcome is the second part of your query:
?person my:Nationality ?nationality.
In your data, only person-individuals have this property, so only they will match the full query (even if there are other individuals that have a different type).
How SPARQL works is essentially pattern matching. You specify a template for your RDF graph, the variables in the query are the "holes" in that template. Whichever parts of your graph fit the entire template get returned.
Put another way, your query asks the following: "give me all things that have both a type, and a nationality". There may be many things that have a type, but since only persons have a nationality, only persons are returned.
If you want, you can make it explicit that you are only interested in individuals of type Person, by replacing the variable ?p with the class identifier for persons, for example:
?person rdf:type my:Person.
?person my:Nationality ?nationality.

how to query sibling Classes in "DOID" ontology?

I am using ontobee to execute a query to get all siblings of "essential Hypertension" in human Disease Ontology "DOID", The query returns 5 triples.
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix owl: <http://www.w3.org/2002/07/owl#>
SELECT ?xChild Str(?xChildLa)
from <http://purl.obolibrary.org/obo/merged/DOID>
WHERE {
<http://purl.obolibrary.org/obo/DOID_10825> rdfs:subClassOf ?x.
?xChild rdfs:subClassOf|(owl:equivalentClass)* ?x ;
rdfs:label ?xChildLa.
}
but the page of "essential Hypertension" shows in Class Hierarchy that there is one more sibling not returned by query.
what is wrong with the above query?
why "renal hypertension" not returned?
If you click on the description of renal hypertension, you will see that is
renal hypertension EquivalentTo hypertension and (located in some kidney)
which means it's using an OWL class equivalence axiom (owl:equivalentClass). This is syntactic sugar for the rdfs:subClassOf relation in both directions, and indeed your SPARQL query doesn't handle this axiom neither syntactically nor semantically.
Not sure whether they use an OWL reasoner to get all subclasses for visualization.
Doing it via SPARQL can be found in the great answer from Joshua Taylor.

Fundamental understanding of SPARQL

I try to understand SPARQL and mess around with the SPARQL Tool that is provided by dbpedia. I've read the w3 documentation and now I want to create my very own query. I would like to find the names of all books in dbpedia written by J. J. R. Tolkien.
Therefore I "designed" this query:
SELECT ?name WHERE { ?name ?author "J._R._R._Tolkien".
?name ?mediaType "Print"}
The result is empty, but I would at least expect this book popping up:
http://dbpedia.org/page/The_Lord_of_the_Rings
Can someone tell me, what my conceptual mistake is?
I good approach would be to review his DBpedia page and then choose the desired properties. In your case two good candidates are notableWork and author. Here's a query with both of them, effectively using the latter.
PREFIX : <http://dbpedia.org/resource/>
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
SELECT DISTINCT ?is_author_of #?has_notable_work
FROM <http://dbpedia.org/>
WHERE {
:J._R._R._Tolkien rdf:type o:Writer ;
#o:notableWork ?has_notable_work ;
^p:author ?is_author_of .
}
I have used rdf:type o:Writer to reduce possible ambiguity (none, in case of using URI of an individual resource in dbpedia), and ^ to get the right direction. My preference for ?is_author_of and ?has_notable_workinstead of e.g. ?book and ?popular_book was because I'm not sure what kind of work is he author of.

PROTÉGÉ SPARQL QUERY TAB: cannot query for ontology-specific classes

I am using SPARQL Query tab in Protege 5 to query an OWL ontology I have been constructing. I succeded in many kinds of queries, but when I use some specific class of my ontology inside the very same queries (that are apparently well formed) they return no results. Following, two of the problematic queries - assuming "Event" as one of the concepts of the ontology (http://www.semanticweb.org/ontologies/2014/5/MyOnto#Event):
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX onto: <http://www.semanticweb.org/ontologies/2014/5/MyOnto#>
SELECT ?a WHERE { ?a rdfs:subClassOf onto:Event }
and, with the same prefixes
SELECT ?a WHERE { ?a rdfs:range onto:Event }
Both return no results. However, if I substitute "onto:Event" for, let's say, ?b, both return a long list of results - inclunding Event as a match for ?b.
Is it something I'm misusing or forgetting (although I've seen this pattern in several links on internet with people claiming to have got results) or is it a limitation of SPARQL or some issue of the Protege tab?
The problem is that, in fact, although the URI of the ontology is:
<http://www.semanticweb.org/ontologies/2014/5/MyOnto#>
in the OWL document, the prefix used before class names is the IRI:
<http://www.semanticweb.org/ontologies/2014/2/untitled-ontology-662#>
Thus, replacing the old onto: by
PREFIX onto: <http://www.semanticweb.org/ontologies/2014/2/untitled-ontology-662#>
solves the issue.
(Thanks to #Csongor from Protégé Project mail list, who found the answer.)
P.S.: It's also worthy to note that it can't be taken for granted that all the terms in the ontology will be <current_ontology_URI#term> - e.g. if one includes some terms in the ontology and then changes ontology URI, these terms will be identified as <previous_ontology_URI#term> and the new ones as <current_ontology_URI#term> (which was exactly the cause of the problem above).
I had the same problem, whenever using an ontology specific class there were no results although there should have been. My ontology (pizza.owl) was loaded from a local file.
I found that it is required to add the file name in the PREFIX.
PREFIX : <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
After that I got the information that I expected.