Using the SPARQL Query tab in Protoge to query elements within my own ontology - sparql

I have an ontology that I am trying to qrite SPARQL queries for.
I'm using the Ontology IRI (http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology) displayed on the Active ontology tab to define the PREFIX value in the query below:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX chris: <http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology#>
SELECT ?class ?activity
WHERE { ?class chris:hasActivity ?activity }
When I run this nothing is returned, yet when I output the ontology into RDF format I can see instances if what I want to be returned:
<owl:Class rdf:about="http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology#SportsHallBooking">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology#BookableTimetable"/>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology#hasActivity"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology#Badminton"/>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology#hasActivity"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/chris/ontologies/2020/2/dis-coursework-ontology#Football"/>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
So I would expect the results to include:
class | activity SportsHallBooking | Badminton
SportsHallBooking | Football
Yet I get nothing back.

Credit to Stanislav for the answer.
select * { ?class rdfs:subClassOf [ owl:onProperty chris:hasActivity; owl:someValuesFrom ?activity ] }

Related

How to query for specific person on Wikidata using SparQL?

I am making a query which I think would make sense, but it is just hanging, no error:
PREFIX schema: <http://schema.org/>
SELECT ?item ?itemLabel ?person ?personLabel ?pic ?givenName ?givenNameLabel ?familyName ?familyNameLabel ?placeOfBirth ?placeOfBirthLabel
WHERE {
?person wdt:P31 wd:Q5 .
?person wdt:P18 ?pic .
?person wdt:P735 ?givenName .
?person wdt:P734 ?familyName .
?person wdt:P19 ?placeOfBirth .
?person wdt:P569 ?birthDate .
?item ?label "Einstein"#en .
OPTIONAL {
?article schema:about ?cid .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/> .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 1
How do I query for a person using their Wikipedia page name, and get this sort of information?
Unhelpfully, it finally timed out with:
java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask.get(FutureTask.java:205)
at com.bigdata.rdf.sail.webapp.BigdataServlet.submitApiTask(BigdataServlet.java:292)
at com.bigdata.rdf.sail.webapp.QueryServlet.doSparqlQuery(QueryServlet.java:678)
at com.bigdata.rdf.sail.webapp.QueryServlet.doGet(QueryServlet.java:290)
at com.bigdata.rdf.sail.webapp.RESTServlet.doGet(RESTServlet.java:240)
at com.bigdata.rdf.sail.webapp.MultiTenancyServlet.doGet(MultiTenancyServlet.java:273)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
at org.wikidata.query.rdf.blazegraph.throttling.ThrottlingFilter.doFilter(ThrottlingFilter.java:320)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.wikidata.query.rdf.blazegraph.throttling.SystemOverloadFilter.doFilter(SystemOverloadFilter.java:82)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at ch.qos.logback.classic.helpers.MDCInsertingServletFilter.doFilter(MDCInsertingServletFilter.java:49)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.wikidata.query.rdf.blazegraph.filters.QueryEventSenderFilter.doFilter(QueryEventSenderFilter.java:116)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.wikidata.query.rdf.blazegraph.filters.ClientIPFilter.doFilter(ClientIPFilter.java:43)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.wikidata.query.rdf.blazegraph.filters.JWTIdentityFilter.doFilter(JWTIdentityFilter.java:66)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.wikidata.query.rdf.blazegraph.filters.RealAgentFilter.doFilter(RealAgentFilter.java:33)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1642)
at org.wikidata.query.rdf.blazegraph.filters.RequestConcurrencyFilter.doFilter(RequestConcurrencyFilter.java:50)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1634)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1340)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1242)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:220)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:503)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
at java.lang.Thread.run(Thread.java:750)

Select single ontology class and all of its axioms and annotations as a subset

Is there a SPARQL or other method to extract a single class and all of its associated axioms and annotations from an ontology? For example, assume one had a list of classes from one ontology that they wanted to add to another ontology.
For example, consider the following class from IDO: http://purl.obolibrary.org/obo/IDO_0000406 (see below). In this example, it might be easier to simply parse the contents of the ontology's OWL file using a regex pattern such as /(<owl:Class .+?\s\s\s\s\s\n<\/owl:Class>/ to capture all of the details of every class after which the classes of interest could simply be filtered out and appended to a new OWL file.
<owl:Class rdf:about="http://purl.obolibrary.org/obo/IDO_...">
...
</owl:Class>
...
<owl:Class rdf:about="http://purl.obolibrary.org/obo/IDO_0000406">
<rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/IDO_0000452"/>
<rdfs:subClassOf>
<owl:Class>
<owl:intersectionOf rdf:parseType="Collection">
<owl:Restriction>
<owl:onProperty rdf:resource="http://purl.obolibrary.org/obo/RO_0000052"/>
<owl:someValuesFrom rdf:resource="http://purl.obolibrary.org/obo/OBI_0100026"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="http://purl.obolibrary.org/obo/BFO_0000054"/>
<owl:allValuesFrom>
<owl:Class>
<owl:intersectionOf rdf:parseType="Collection">
<rdf:Description rdf:about="http://purl.obolibrary.org/obo/BFO_0000015"/>
<owl:Restriction>
<owl:onProperty rdf:resource="http://purl.obolibrary.org/obo/BFO_0000051"/>
<owl:someValuesFrom rdf:resource="http://purl.obolibrary.org/obo/IDO_0000625"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="http://purl.obolibrary.org/obo/BFO_0000051"/>
<owl:someValuesFrom rdf:resource="http://purl.obolibrary.org/obo/TRANS_0000000"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="http://purl.obolibrary.org/obo/BFO_0000051"/>
<owl:someValuesFrom>
<owl:Class>
<owl:intersectionOf rdf:parseType="Collection">
<rdf:Description rdf:about="http://purl.obolibrary.org/obo/IDO_0000626"/>
<owl:Class>
<owl:complementOf>
<owl:Restriction>
<owl:onProperty rdf:resource="http://purl.obolibrary.org/obo/BFO_0000066"/>
<owl:someValuesFrom rdf:resource="http://purl.obolibrary.org/obo/IDO_0000457"/>
</owl:Restriction>
</owl:complementOf>
</owl:Class>
</owl:intersectionOf>
</owl:Class>
</owl:someValuesFrom>
</owl:Restriction>
</owl:intersectionOf>
</owl:Class>
</owl:allValuesFrom>
</owl:Restriction>
</owl:intersectionOf>
</owl:Class>
</rdfs:subClassOf>
<obo:IAO_0000115 xml:lang="en">An infectious disposition to become part of a disorder only in organisms whose defenses are compromised.</obo:IAO_0000115>
<obo:IAO_0000117>Albert Goldfain</obo:IAO_0000117>
<obo:IAO_0000117>Alexander Diehl</obo:IAO_0000117>
<obo:IAO_0000117>Lindsay Cowell</obo:IAO_0000117>
<obo:IAO_0000118 xml:lang="en">opportunitistic pathogenic disposition</obo:IAO_0000118>
<rdfs:comment>The disposition is realized in a process by which the bearer becomes part of a disorder in an immunocompromised host.</rdfs:comment>
<rdfs:comment xml:lang="en">This includes individuals who are immunocompromised or who have damaged barriers that normally protect against infection (e.g. skin).</rdfs:comment>
<rdfs:label xml:lang="en">opportunistic infectious disposition</rdfs:label>
</owl:Class>
EDIT: I tried using rdflib to achieve this by loading the source ontology as a Graph(). Using a for loop to iterate through statements in the loaded graph, it's easy enough to find the triples which are directly connected to a class, say, ido:IDO_0000406 -> obo:IAO_0000117 -> Alexander Diehl. These can then be added to the target ontology, which is also loaded as a graph. However, using the IDO_0000406 class example, it is clear that triples not on the first sub-level (i.e. anything within the clause <rdfs:subClassOf><owl:Class>...) will not come through as expected. For example:
g_tgt = Graph()
classes = ['http://purl.obolibrary.org/obo/IDO_0000406'
'http://purl.obolibrary.org/obo/IDO_0000407',
'http://purl.obolibrary.org/obo/IDO_0000408',
'http://purl.obolibrary.org/obo/IDO_0000409']
g_src = Graph()
g_src.parse('ido.owl')
for stmt in g_src: # stmt: (subject, predicate, object)
if str(stmt[0]) in classes:
# adds all first-level triples to target graph
g_tgt.add((stmt[0], stmt[1], stmt[2]))
My thought is to approach non-first-level nodes in a recursive fashion and will update if this is successful.
EDIT 2: It should be possible to extract the classes using ROBOT (http://robot.obolibrary.org/extract). For example:
robot extract --method STAR \
--input filtered.owl \
--term-file uberon_module.txt \
--output results/uberon_module.owl
where uberon_module.txt would include the list of classes to be extracted.

How to query data from a object URI in sparql

<?xml version="1.0"?>
<rdf:RDF xmlns="http://localhost/mydata#"
xml:base="http://localhost/mydata"
xmlns:g="http://localhost/mydata/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://localhost/mydata">
<owl:imports rdf:resource="http://www.owl-ontologies.com/travel.owl"/>
<owl:imports rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
</owl:Ontology>
</rdf:RDF>
I have my RDF in the above format. I wish to query the data in the resource http://www.owl-ontologies.com/travel.owl as well. How can I do it? I get the URI value using the below query but shall be done next?
prefix ab:<http://www.owl-ontologies.com/travel.owl#>
prefix rdfs:<https://www.w3.org/2000/01/rdf-schema#>
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix owl:<http://www.w3.org/2002/07/owl#>
SELECT ?y
WHERE { ?x owl:imports ?y.
?
}

How can I create an owl:intersectionOf two owl:Classes?

For a school exercise, I have an RDF file and an OWL file.
There is an owl:Class Lecturer and an owl:Class Researcher. The intersection of both should be a Professor. I have put my RDF and OWL file below.
Problem is: when I do my query, no resource is of type Professor, while in the RDF file we can see that Laura should be a Professor.
Reduced version of rdf file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rdf:RDF [
<!ENTITY humans "http://www.inria.fr/2007/09/11/humans.rdfs">
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#"> ]>
<rdf:RDF
xmlns:rdf ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd ="&xsd;"
xmlns ="&humans;#"
xml:base ="&humans;-instances" >
<Person rdf:ID="Laura">
<name>Laura</name>
</Person>
<Lecturer rdf:about="#Laura"/>
<Researcher rdf:about="#Laura">
<name>Laura</name>
</Researcher>
</rdf:RDF>
Reduced version of owl file:
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns="http://www.w3.org/2000/01/rdf-schema#"
xml:base="http://www.inria.fr/2007/09/11/humans.rdfs"
xmlns:owl="http://www.w3.org/2002/07/owl#">
<owl:Class rdf:ID="Person">
</owl:Class>
<owl:Class rdf:ID="Lecturer">
<subClassOf rdf:resource="#Person"/>
</owl:Class>
<owl:Class rdf:ID="Researcher">
<subClassOf rdf:resource="#Person"/>
</owl:Class>
<owl:Class rdf:id="Professor">
<owl:intersectionOf rdf:parseType="Collection">
<owl:Class rdf:about="#Lecturer"/>
<owl:Class rdf:about="#Researcher"/>
</owl:intersectionOf>
</owl:Class>
</rdf:RDF>
The query I used was the defautl query:
select * where {
?x ?p ?y
}
But what I actually would expect to do is the following:
select * where {
?x a <http://www.inria.fr/2007/09/11/humans.rdfs#Professor>
}
I did look at this answer: Why do we need to use rdf:parseType="Collection" with owl:intersectionOf? but I don't understand in which way it should be used for my specific problem.
I hope somebody can help. By the way, it's my first post here, so let me know if something's missing.
Paraphrased from comments by #stanislav-kralin:
Use correct capitalization of rdf:ID (not rdf:id), and enable "OWL-Max" reasoning when loading your RDF into GraphDB.

Sparql query on restriction list (Equivalent To) in protégé

My ontology is about Cocktail. This is a cocktail named "AfterGlow"
<owl:Class rdf:about="&cocktails;AfterGlow">
<owl:equivalentClass>
<owl:Class>
<owl:intersectionOf rdf:parseType="Collection">
<owl:Restriction>
<owl:onProperty rdf:resource="&cocktails;aPourIngredient"/>
<owl:someValuesFrom rdf:resource="&cocktails;JusAnanas"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="&cocktails;aPourIngredient"/>
<owl:someValuesFrom rdf:resource="&cocktails;JusOrange"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="&cocktails;aPourIngredient"/>
<owl:someValuesFrom rdf:resource="&cocktails;SiropGrenadine"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="&cocktails;aPourDescription"/>
<owl:hasValue>Descriptoion AfterGlow</owl:hasValue>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="&cocktails;aPourTitre"/>
<owl:hasValue>AfterGlow</owl:hasValue>
</owl:Restriction>
</owl:intersectionOf>
</owl:Class>
</owl:equivalentClass>
<rdfs:subClassOf rdf:resource="&cocktails;Cocktail"/>
</owl:Class>
JusOrange means Orange juice
JusAnanas means Pineapple juice
aPourIngredient is a property which means "has(contain) the ingredient"
This is the request which list all my cocktails with theirs rectrictions
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX : <http://www.semanticweb.org/cocktails#>
SELECT *
WHERE {
?Cocktail rdfs:subClassOf :Cocktail.
?Cocktail owl:equivalentClass ?restriction .
}
How can I request for example "select all cocktail which contains JusAnanas AND JusOrange"
You can find my ontology here :
https://s3-eu-west-1.amazonaws.com/ontologycero/cocktailsOnthology.owl
I've already found an ugly request, but it's not usable because we have to know the ontology for use this kind of request.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX : <http://www.semanticweb.org/cocktails#>
SELECT *
WHERE {
?Cocktail rdfs:subClassOf :Cocktail.
?Cocktail owl:equivalentClass ?restriction .
?restriction owl:intersectionOf ?intersection.
?intersection rdf:first ?ingredient1.
?intersection rdf:rest ?rest1.
?rest1 rdf:first ?ingredient2.
?rest1 rdf:rest ?rest2.
?rest2 rdf:first ?ingredient3.
?ingredient1 owl:someValuesFrom :JusAnanas.
?ingredient2 owl:someValuesFrom :JusOrange.
}
I am not sure what you are looking for, but as far as I understand you want to bypass the structure of the ontology. Since you don't know what comes after restriction, you can mention to the query to check wall the possible combinations.
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 : <http://www.semanticweb.org/cocktails#>
SELECT *
WHERE {
?Cocktail rdfs:subClassOf :Cocktail.
?Cocktail owl:equivalentClass ?restriction .
?restriction (rdfs:subClassOf|(owl:intersectionOf/rdf:rest*/rdf:first))* ?ingredient1.
?restriction (rdfs:subClassOf|(owl:intersectionOf/rdf:rest*/rdf:first))* ?ingredient2.
?ingredient1 owl:someValuesFrom :JusAnanas.
?ingredient2 owl:someValuesFrom :JusOrange.
}
SPARQL isn't meant to understand the formal OWL model you've set up. It's a graph pattern matching query language for RDF triples. So instead of trying to query through the model, query the data using the semantics of the model you've designed:
SELECT *
WHERE {
?coctails rdfs:subClassOf* :Cocktail .
?aoCocktail a ?coctails .
?aoCocktail :aPourIngredient :JusAnanas .
?aoCocktail :aPourIngredient :JusOrange .
}
The first two triple patterns find all member of :Cocktail and its subclasses. The last two triple patterns find all members that have both :JusOrange and :JusAnanas as values for the property :aPourIngredient.