How to query for specific person on Wikidata using SparQL? - 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)

Related

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.
?
}

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

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 ] }

How to traverse RDF tree in SPARQL and find connections outside of the tree?

Considering a tree with the root node "A" and "hasChild" relationships (e.g. product structure) as following:
Target is to find out:
What nodes have parents outside of the tree?
In this case the answer should be ‘B’ and ‘Q’ as they have parents outside of the tree.
The query should go to every node and check its parents rather than creating a list of children and the check every one of them I guess.
How can I efficiently (should work for millions of nodes) traverse this tree an SPARQL and answer this?
This is what i tried but gave 0 results:
PREFIX xxx: <http://example.org/xxx#>
select * where {
xxx:A xxx:hasChild* ?child .
?child ^xxx:hasChild ?foreignParent .
?child ^xxx:hasChild ?parent .
FILTER (?parent =! ?foreignParent) .
}
Attached the respective sample data:
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xxx="http://example.org/xxx#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xml:base="http://example.org/xxx">
<owl:Ontology rdf:about="">
<owl:versionInfo>Created with TopBraid Composer</owl:versionInfo>
</owl:Ontology>
<owl:Class rdf:ID="Other">
<rdfs:label>Other</rdfs:label>
<rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</owl:Class>
<owl:Class rdf:ID="Item">
<rdfs:label>Item</rdfs:label>
<rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</owl:Class>
<rdf:Property rdf:ID="hasChild">
<rdfs:range rdf:resource="#Item"/>
<rdfs:range rdf:resource="#Other"/>
<rdfs:domain rdf:resource="#Item"/>
<rdfs:label>has child</rdfs:label>
</rdf:Property>
<xxx:Other rdf:ID="Fake_1">
<xxx:hasChild>
<xxx:Item rdf:ID="B">
<xxx:hasChild>
<xxx:Item rdf:ID="D">
<xxx:hasChild>
<xxx:Item rdf:ID="F"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="E"/>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="C"/>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
<rdfs:label>Fake 1</rdfs:label>
</xxx:Other>
<xxx:Other rdf:ID="Fake_2">
<xxx:hasChild>
<xxx:Item rdf:ID="Q"/>
</xxx:hasChild>
<rdfs:label>Fake 2</rdfs:label>
</xxx:Other>
<xxx:Item rdf:ID="A">
<xxx:hasChild>
<xxx:Item rdf:ID="G">
<xxx:hasChild>
<xxx:Item rdf:ID="X">
<xxx:hasChild>
<xxx:Item rdf:ID="Z"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="Y"/>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="R">
<xxx:hasChild>
<xxx:Item rdf:ID="W"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="S">
<xxx:hasChild>
<xxx:Item rdf:ID="V"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="U"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="T"/>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="M">
<xxx:hasChild rdf:resource="#Q"/>
<xxx:hasChild>
<xxx:Item rdf:ID="P"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="O"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="N"/>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="H">
<xxx:hasChild>
<xxx:Item rdf:ID="L"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="K"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="J"/>
</xxx:hasChild>
<xxx:hasChild>
<xxx:Item rdf:ID="I"/>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
</xxx:Item>
</xxx:hasChild>
<xxx:hasChild rdf:resource="#B"/>
</xxx:Item>
</rdf:RDF>
The trick is to make sure there is no path from your tree root to your foreign parent node. You can do that by means of a FILTER NOT EXISTS construction, like so:
PREFIX xxx: <http://example.org/xxx#>
SELECT ?child ?foreignParent
WHERE {
xxx:A xxx:hasChild+ ?child.
?child ^xxx:hasChild ?foreignParent.
FILTER NOT EXISTS { xxx:A xxx:hasChild* ?foreignParent }
}
Whether this scales to "millions of nodes" will depend on a) the depth of the tree and b) the triplestore you use. I ran the query using RDF4J on my laptop with the test data you provided, and got this:
Evaluating SPARQL query...
+-------------------------------------+-------------------------------------+
| child | foreignParent |
+-------------------------------------+-------------------------------------+
| <http://example.org/xxx#B> | <http://example.org/xxx#Fake_1> |
| <http://example.org/xxx#Q> | <http://example.org/xxx#Fake_2> |
+-------------------------------------+-------------------------------------+
2 result(s) (19 ms)

Query relations between subclasses of two given classes

I would like to get all relations between a Person and a Movie, where anything that is subclass of Person is also a Person (an actor, for example)
This is my attempt:
SELECT ?property ?film WHERE
{
{
?property rdf:type owl:ObjectProperty;
rdfs:range ?person;
rdfs:domain ?film.
?person rdfs:subclassOf* dbo:Person.
?film rdfs:subclassOf* dbo:Work.
} UNION
{
?property rdf:type owl:ObjectProperty;
rdfs:domain ?person;
rdfs:range ?film.
?person rdfs:subclassOf* dbo:Person.
?film rdfs:subclassOf* dbo:Work.
}
}
With results:
SPARQL results:
property film person
dbpedia:ontology/author [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/chiefEditor [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/composer [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/coverArtist [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/mainCharacter [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/narrator [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/translator [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/writer [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/created [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/debutWork [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
dbpedia:ontology/notableWork [http] dbpedia:ontology/Work [http] dbpedia:ontology/Person [http]
However all results have as range/domain Person/Film and no subclasses. I would expect http://dbpedia.org/ontology/starring to be a result, since its domain is Actor and range Work
Why is my query failing? Can someone point my mistake?
Thanks in advance

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.