Get range class of Datatype property - properties

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?

Related

How to return subgraph from rdf graph

I have an RDF graph G with several classes assuming for simplicity (Person and Parrot).
The class Person is connected to the class Parrot by the property hasAnimal, e.g.:
#PREFIX : <http://example.org/>
:Hugo rdf:type :Person .
:Hugo rdfs:label "Hugo" .
:Hugo :hasAnimal :Birdy.
:Birdy rdf:type :Parrot .
:Birdy rdfs:label :"Birdy" .
:LonleyBrido rdf:type :Parrot .
What is wanted is a subgraph of G that contains all the triples from Person and Parrot that are directly connected with each other, starting from Person. The initial Person does not matter to me, the important part is that only connected triples are extracted i.e. that only persons that do have a parrot or don't get outputted. What I have already tried is the following:
construct {
?person ?p ?o .
?parrot ?p2 ?o2 .
} where {
?person rdf:type :Person .
?person ?p ?o .
?person :hasAnimal ?parrot .
?parrot rdf:type :Parrot .
?parrot ?p2 ?o2 .
}
So the expected output would be:
:Hugo rdf:type :Person .
:Hugo rdfs:label "Hugo" .
:Hugo :hasAnimal :Birdy.
:Birdy rdf:type :Parrot .
:Birdy rdfs:label :"Birdy" .
I am executing this query on a rdflib graph.
Does anyone have a solution to this problem?
The solution is already described above:
import rdflib
from rdflib.namespace import RDF, RDFS
query = """
construct {
?person ?p ?o .
?parrot ?p2 ?o2 .
} where {
?person rdf:type :Person .
?person ?p ?o .
?person :hasAnimal ?parrot .
?parrot rdf:type :Parrot .
?parrot ?p2 ?o2 .
}
"""
g = rdflib.Graph()
g.parse("example.ttl", format="ttl")
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)
EX= rdflib.Namespace("http://example.org/")
g.bind("example", EX)
result = g.query(query)

The mechanism of "FILTER NOT EXISTS" in SPARQL

Assuming the triples are following:
#prefix : <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .
and then we perform 3 queries based on SPARQL 1.1:
Q1:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?s foaf:name ?y }
}
Q2:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:name ?y }
}
Q3:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:mailbox ?y }
}
These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)
Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.

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")
}

How to use Sparql Contains to match similar String?

I'm trying to grab some definition in dbpedia inside my thesaurus.
Although can find country that have a label that match my country, i don't get all of them. So i try to match similar label with contains but it does not work.
Any idea why.
SELECT distinct ?idbcountry ?label ?labelDb ?def
WHERE {
?idbcountry a skos:Concept .
?idbcountry rdfs:label ?label .
?idbcountry skos:inScheme iadb:IdBCountries .
FILTER(lang(?label) = "en")
Service <http://dbpedia.org/sparql> {
?s a <http://dbpedia.org/ontology/Country> .
?s rdfs:label ?labelDb .
FILTER(CONTAINS (?labelDb, ?label)).
?s rdfs:comment ?def .
FILTER(lang(?def) = "en") .
FILTER(lang(?labelDb) = "en") .
}}
The exact matching query that works is as follows:
SELECT distinct ?idbcountry ?label ?def
WHERE {
?idbcountry a skos:Concept .
?idbcountry rdfs:label ?label .
?idbcountry skos:inScheme iadb:IdBCountries .
FILTER(lang(?label) = "en")
Service <http://dbpedia.org/sparql> {
?s a <http://dbpedia.org/ontology/Country> .
?s rdfs:label ?label .
?s rdfs:comment ?def
FILTER(lang(?def) = "en")
}
}
EDIT1
Data Samples:
<http://thesaurus.iadb.org/publicthesauri/10157002136735779158437>
rdf:type skos:Concept ;
dct:created "2015-03-27T16:43:48.052-04:00"^^xsd:dateTime ;
rdfs:label "BO"#en ;
rdfs:label "Bolivia"#en ;
rdfs:label "Bolivia"#es ;
rdfs:label "Bolivie"#fr ;
rdfs:label "Bolívia"#pt ;
skos:altLabel "BO"#en ;
skos:definition "Bolivia (/bəˈlɪviə/, Spanish: [boˈliβja], Quechua: Buliwya, Aymara: Wuliwya), officially known as the Plurinational State of Bolivia (Spanish: Estado Plurinacional de Bolivia locally: [esˈtaðo pluɾinasjoˈnal de βoˈliβja]), is a landlocked country located in western-central South America."#en ;
skos:inScheme :IdBCountries ;
skos:prefLabel "Bolivia"#en ;
skos:prefLabel "Bolivia"#es ;
skos:prefLabel "Bolivie"#fr ;
skos:prefLabel "Bolívia"#pt ;
skos:topConceptOf :IdBCountries ;
<http://xmlns.com/foaf/0.1/focus> <http://dbpedia.org/resource/Bolivia> ;
Without seeing your data, we can't know why your query isn't working. However, using contains is pretty straightforward. It's just a matter of contains(string,substring). As Jeen said, we can't reproduce your problem without knowing what your data looks like, but here's an example of contains in action:
select distinct ?country ?label {
?country a dbpedia-owl:Country ; #-- select countries
rdfs:label ?label . #-- and get labels
filter langMatches(lang(?label),"en") #-- but only English labels
filter contains(?label,"land") #-- containing "land"
}
SPARQL results

SPARQL in Protege 4.3

I need to return individuals for my query:
SELECT ?subject ?class
WHERE { ?subject rdfs:subClassOf ?class.
?class rdfs:comment "linear"#en}
But it works only with subclasses. Should I replace rdfs:subClassOf on different operator?
Your query specifically asks for ?subjects that are subclasses of ?class (where ?class has the rdfs:comment "linear"#en). To retrieve instances of type ?class, you'd use
?subject rdf:type ?class
or, since SPARQL allows abbreviating rdf:type by a,
?subject a ?class
If you can't share details about the body of data, you are querying, you might want to get an idea yourself by checking
SELECT ?s ?p ?subject ?class
WHERE
{ ?s ?p ?class .
?subject rdfs:subClassOf ?class .
?class rdfs:comment "linear"#en .
} ORDER BY ?s ?p ?subject ?class
and/or
SELECT ?subject ?class ?p ?o
WHERE
{ ?subject ?p ?o .
?subject rdfs:subClassOf ?class .
?class rdfs:comment "linear"#en .
} ORDER BY ?subject ?class ?p ?o
from where you can expand in the same manner until you get a handle.