How to query all properties of owl:Thing of dbpedia using SPARQL? - sparql

When I use the following SPARQL query I get all properties of the DBpedia class Country:
select ?range ?domain ?prop ?label
Where{
?class rdfs:subClassOf{0,1} ?domain.
?prop rdfs:domain ?domain.
?prop rdfs:range ?range.
?prop rdfs:label ?label.
FILTER(lang(?label) = 'en')
FILTER(?class = <http://dbpedia.org/ontology/Country>)
}
When I try to do this with 'Thing' or 'OWL:Thing' or 'A Thing' or anything equivalent instead of Country, I get an empty result.
I want to adopt the ontology of DBpedia's owl:Thing, so I want to retrieve all properties of http://mappings.dbpedia.org/server/ontology/classes/owl%3AThing (including labela and range).
Does anyone know how I can achieve this?

There is not property in DBpedia with the domain owl:Thing:
select * {
?prop rdfs:domain owl:Thing
}
The reason for this is probably that if no explicit domain is given, owl:Thing is the trivial domain. You can check this also if you look at particular properties from your referred list, e.g. dbo:abbreviation
Workaround query:
SELECT ?range (owl:Thing as ?domain) ?prop ?label {
VALUES ?type {owl:DatatypeProperty owl:ObjectProperty}
?prop a ?type
OPTIONAL {?prop rdfs:range ?range }
?prop rdfs:label ?label.
FILTER(langmatches(lang(?label), 'en'))
FILTER NOT EXISTS {?prop rdfs:domain ?domain}
}

Related

Get range class of Datatype property

I have the following SPARQL query
SELECT DISTINCT ?p ?class ?type
WHERE {
?resource ?p ?target .
?p rdfs:range ?class .
?class rdf:type ?type .
}
I get results only for the object properties.
If ?class = xsd:float, then rdf:type = rdfs:Datatype, right?
Why I do not receive also results for the type of data properties?

How to add conditions to a query

I would like to add a condition to a query, so that, if a result is empty, it fills it with a result of another query.
For example : If a owl:Class has no "skos:definition", I would like to replace it by a "rdfs:comment"
Is this possible ?
Thank you !
COALESCE is can help, especially as you get more choices from skos:prefLabel, skos:altLabel and more.
OPTIONAL { ?property rdfs:label ?label }
OPTIONAL { ?property skos:definition ?definition .}
# ... other ways to choose ?comment ...
BIND ( COALESCE(?definition, ?label, "Unknown") AS ?comment )
COALESCE allows expressions (unlike BOUND)
https://www.w3.org/TR/sparql11-query/#func-coalesce
I found something with BIND (IF(BOUND(...) ..., ...) AS ...) statement.
SELECT DISTINCT ?type ?property ?comment ?propertyType ?domain
WHERE {
?property rens:hasAttributeType ?propertyType .
?property rens:hasForDomain ?domain .
?property rdf:type ?type .
?property rdfs:label ?label .
OPTIONAL {?property skos:definition ?definition .}
BIND(IF(BOUND(?definition), ?definition, ?label) AS ?comment) .
}
Is this the better way ?

SPARQL query for specific information

I am struggling a lot to create some SPARQL queries. I need 3 specific things, and this is what i have so far:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
select distinct ?title ?author ?country ?genre ?language
where {
?s rdf:type dbo:Book;
dbp:title ?title;
dbp:author ?author;
dbp:country ?country;
dbp:genre ?genre;
dbp:language ?language.
}
This query will bring me a list of all books. What i really need is the ability to add some filters to this code. There are 3 things i want to filter by:
specific title name (e.g., search for title with "harry potter")
specific author name (e.g., search for author with "J. K. Rowling")
specific genre (e.g., search for genre with "adventure")
I've been struggling with this for too long and i simply cannot define these 3 queries. I am trying to implement a function that will execute a SPARQL statement using parameters passed by an user form. I found a few examples here and in the web but i just cannot build these 3 specific queries.
As noted, not every book has every property, and some of your properties may not exist at all. For instance, I changed dbp:genre to dbo:literaryGenre, based on the description of Harry Potter and the Goblet of Fire. See query form, and results.
SELECT *
WHERE
{ ?s rdf:type dbo:Book .
?s rdfs:label ?bookLabel .
FILTER(LANGMATCHES(LANG(?bookLabel), 'en'))
?s dbo:author ?author .
?author rdfs:label ?authorLabel .
FILTER(LANGMATCHES(LANG(?authorLabel), 'en'))
?authorLabel bif:contains "Rowling"
OPTIONAL { ?s dbp:country ?country .
?country rdfs:label ?countryLabel .
FILTER(LANGMATCHES(LANG(?countryLabel), 'en')) }
OPTIONAL { ?s dbo:literaryGenre ?genre .
?genre rdfs:label ?genreLabel .
FILTER(LANGMATCHES(LANG(?genreLabel), 'en')) }
OPTIONAL { ?s dbp:language ?language .
?language rdfs:label ?languageLabel .
FILTER(LANGMATCHES(LANG(?languageLabel), 'en')) }
}

Aggregate properties

I'm developing my own Fuseki endpoint from some DBpedia data.
I'm in doubt on how to aggregate properties related to a single resource.
SELECT ?name ?website ?abstract ?genre ?image
WHERE{
VALUES ?s {<http://dbpedia.org/resource/Attack_Attack!>}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image } .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}
SPARQL endpoint: http://dbpedia.org/sparql/
This query returns 2 matching results. They are different just for the dbo:genre value. There is a way I can query the knowledge base and retrieving a single result with a list of genres?
#chrisis's query works well on the DBpedia SPARQL Endpoint, which is based on Virtuoso.
However, if you are using Jena Fuseki, you should use more conformant syntax:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT
?name
(SAMPLE(?website) AS ?sample_website)
(SAMPLE(?abstract) AS ?sample_abstract)
(SAMPLE(?image) AS ?sample_image)
(GROUP_CONCAT(?genre; separator=', ') AS ?genres)
WHERE {
VALUES (?s) {(<http://dbpedia.org/resource/Attack_Attack!>)}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
} GROUP BY ?name
The differences from the #chrisis's query are:
Since GROUP_CONCAT is an aggregation function, it might be used with GROUP BY only;
Since GROUP BY is used, all non-grouping variables should be aggregated (e.g. via SAMPLE);
GROUP_CONCAT syntax is slightly different.
In Fuseki, these AS in the projection are in fact superfluous: see this question and comments.
Yes, the GROUP_CONCAT() function is what you want.
SELECT ?name ?website ?abstract (GROUP_CONCAT(?genre,',') AS ?genres) ?image
WHERE{
<http://dbpedia.org/resource/Attack_Attack!> a dbo:Band ;
foaf:name ?name;
dbo:abstract ?abstract .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:genre ?genre } .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbp:website ?website} .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}

Getting super and sub classes of Dbpedia Ontology

I am trying to get the hierarchy of super and subclass of dbpedia ontolgy.
Sparql query:
SELECT DISTINCT ?superclass ?subclass
WHERE
{
?subclass a owl:Class .
?subclass rdfs:subClassOf ?superclass
}
ORDER BY ?superclass ?subclass
It gives me all classes. But when I am trying to get the counts of entity inside classes. Some classes have entity while some don't have.
Sparql query to get the count of entity inside classes.
Getting Entities:
SELECT DISTINCT ?label AS ?label
?name AS ?name
?link AS ?link
WHERE
{
?link rdf:type <http://www.wikidata.org/entity/Q12136> .
OPTIONAL { ?link foaf:name ?name }
OPTIONAL { ?link rdfs:label ?label }
FILTER( lang(?label) = "en" )
}
Not getting Entities:
SELECT DISTINCT ?label AS ?label
?name AS ?name
?link AS ?link
WHERE
{
?link rdf:type <http://www.wikidata.org/entity/Q18553493> .
OPTIONAL { ?link foaf:name ?name }
OPTIONAL { ?link rdfs:label ?label }
FILTER(lang(?label) = "en" )
}
Why some classes don't have entities? Or Am I doing something wrong? Any help please?
First of all, you are talking about classes and not ontologies. I don't know why you think that entities are instances of ontologies, especially when you're talking about individuals/instances/resources.
Second, why shouldn't there be classes that do not have instances yet?