Querying subclasses through Fuseki - sparql

I use the following simple SPARQL query to obtain a list of classes of an ontology and their subclasses through Fuseki:
SELECT DISTINCT ?subject ?object
WHERE { ?subject rdfs:subClassOf ?object }
This way, I can see the complete URI of all the classes. Now, I would like to query the subclasses of a specific class, say abc
I look at the output of the query and I see the URI of the class in focus abc is this:
http://blahblahblah/file.owl#abc
So, I pose the following SPARQL query to get its subclasses:
SELECT DISTINCT ?subject
WHERE { ?subject rdfs:subClassOf http://blahblahblah/file.owl#abc }
But the output is empty. I also trying enclosing the URL within single quote and double quotes, to no avail.
SELECT DISTINCT ?subject
WHERE { ?subject rdfs:subClassOf 'http://blahblahblah/file.owl#abc' }
SELECT DISTINCT ?subject
WHERE { ?subject rdfs:subClassOf "http://blahblahblah/file.owl#abc" }
What am I doing wrong?
Thanks,

The syntax for IRIs in SPARQL encloses IRIs in angle brackets (< and >). Your query should be written as:
SELECT DISTINCT ?subject
WHERE { ?subject rdfs:subClassOf <http://blahblahblah/file.owl#abc> }

Related

How to construct a list in SPARQL

I have a ttl file that looks like this:
ex:Shape1
a sh:NodeShape ;
sh:property ex:Property-1
rdfs:label "Shape 1"
ex:Property-1
a sh:PropertyShape ;
sh:path ex:property1
sh:in (
"Option 1"
"Option 2"
) ;
sh:name "Property 1"
ex:property1
a owl:DatatypeProperty
After loading the above data into my triple store (which contains many shapes already), what query can I use to retrieve the same data back?
This query gets everything I need except for the list. For the list it only gives a blank node.
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX ex: <http://example.com/#>
CONSTRUCT {
?subject ?predicate ?object
}
WHERE {
{
bind(ex:Shape1 as ?subject)
ex:Shape1 ?predicate ?object
}
UNION
{
ex:Shape1 sh:property ?subject .
?subject ?predicate ?object
}
UNION
{
ex:Shape1 sh:property/sh:path ?subject .
?subject ?predicate ?object
}
}
First of all, why do you need to query to get the same data back? Do you mean you need to get it in the same syntax and formatting?
to get the same data in the form of triples, you can simply execute SELECT * from ?s ?p ?o and it will give you all the triples. It also depends on your triplestore.
The standard query to fetch the items in your list would be the following:
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX ex: <http://example.com/#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?item
WHERE {
ex:Property-1 sh:in/rdf:rest*/rdf:first ?item
}
You can read more about how the list items are stored internally. This link will be helpful.
You can bind the ex:Property-1 in the same way in UNION or WHERE clause in your construct query to get the desired result.
I hope it is helpful. Good luck.

Why `DESCRIBE ?subject ?object` and not just `DESCRIBE ?subject`

I have the misfortune of having to maintain some legacy code authored by another programmer; at a certain point the code generates the following SPARQL:
DESCRIBE ?subject ?object
{
SELECT DISTINCT ?subject ?object
WHERE {
[A where clause which generates unique ?subject ?object pairs]
}
ORDER BY ?subject
}
I have a limited understanding of SPARQL, but I can't work out why this would be any different from:
DESCRIBE ?subject
{
SELECT ?subject
WHERE {
[A where clause which generates unique ?subject ?object pairs]
}
ORDER BY ?subject
}
i.e.: Why SELECT DISTINCT on something which already returns unique pairs, and what difference would DESCRIBE ?subject ?object make compared to DESCRIBE ?subject.
Both queries return the same result on my data store.
Either I am missing something, or my predecessor made a bit of a mess of this query. Does anyone have any further insights?
Thanks!

SPARQL - select from skos:category - Virtuoso 37000

I have problem with SPARQL. I want to select something from category. For example subjects. I make query like this in http://dbpedia.org/snorql.
SELECT ?category ?subject WHERE
{
?category a skos:Concept .
?category skos:Concept: American_punk_rock_guitarists.
?category dct:subject ?subject .
} LIMIT 1000
I have error Virtuoso 37000. I don't understand why.
P.S. Is it good book for beginnier in SPARQL - Learning SPARQL, 2nd Edition
Querying and Updating with SPARQL 1.1 ?
You have at least one syntax error: the second colon (:) in the second triple.
Semantically... I don't really know the classes or predicates in dbpedia... but can skos:Concept be both a type and a predicate?
I wrote you a valid query that returns 10 members of the category "American_punk_rock_guitarists"
I put this together by going to dbpedia's faceted free text search and familiarizing myself with the concept of American punk rock guitarists, specifically Joey Ramone
prefix dbpcat: <http://dbpedia.org/resource/Category:>
SELECT ?subject ?category
WHERE
{ values ?category { dbpcat:American_punk_rock_guitarists } .
?subject dct:subject ?category }
LIMIT 10

How to get dct:subjects for all entities using SPARQL

I am using the following query
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?subject WHERE {
?concept rdfs:label 'Exoskeleton'#en ;
^dct:subject ?subject .
}
ORDER BY ?subject
This doesn't give me any results. However, the rdfs:label exists. (See http://dbpedia.org/page/Exoskeleton.)
On querying with a different label, it works :
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?subject WHERE {
?concept rdfs:label 'Machine learning'#en ;
^dct:subject ?subject .
}
ORDER BY ?subject
The above query works and gives me the results. (See http://dbpedia.org/page/Machine_learning.)
What do I change, such that the first query works too?
The dct:subject predicate is used between a page and a category it belongs to. So, your second query is giving you results that are in Category:Machine learning. But since there is no Category:Exoskeleton, your first query gives you no results. This also means the two pages you liked to are irrelevant to your queries.
I don't know how to change the first query so that it works, because I don't understand what would "working" entail.
Devil is in the details:
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?concept WHERE {
?concept rdfs:label 'Machine learning'#en.
}
ORDER BY ?concept
Returns two results:
http://dbpedia.org/resource/Category:Machine_learning
http://dbpedia.org/resource/Machine_learning
While Exoskeleton has no corresponding concept:
http://dbpedia.org/resource/Exoskeleton
Thus your inverse property path finds resources under a http://dbpedia.org/resource/Category:Machine_learning concept but not under a http://dbpedia.org/resource/Machine_learning or http://dbpedia.org/resource/Exoskeleton pages.
If you drop the inverse modifier,
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?subject WHERE {
?concept rdfs:label 'Exoskeleton'#en ;
dct:subject ?subject .
}
ORDER BY ?subject
Will return the categories (subject) for the concepts under a given label.

retrieving most specific classes of instances

Is it possible to have a definition of an resource (from DBpedia) with a SPARQL query? I want to have something like the TBox and ABox that are shown in (Conceptual) Clustering methods for
the Semantic Web: issues and applications (slides 10–11). For example, for DBpedia resource Stephen King, I would like to have:
Stephen_King : Person &sqcap; Writer &sqcap; Male &sqcap; … (most specific classes)
You can use a query like the following to ask for the classes of which Stephen King is an instance which have no subclasses of which Stephen King is also an instance. This seems to align well with the idea of “most specific classes.” However, since (as far as I know) there's no reasoner attached to the DBpedia SPARQL endpoint, there may be subclass relationships that could be inferred but which aren't explicitly present in the data.
select distinct ?type where {
dbr:Stephen_King a ?type .
filter not exists {
?subtype ^a dbr:Stephen_King ;
rdfs:subClassOf ?type .
}
}
SPARQL results
Actually, since every class is an rdfs:subClassOf itself, you might want to add another line to that query to exclude the case where ?subtype and ?type are the same:
select distinct ?type where {
dbr:Stephen_King a ?type .
filter not exists {
?subtype ^a dbr:Stephen_King ;
rdfs:subClassOf ?type .
filter ( ?subtype != ?type )
}
}
SPARQL results
If you actually want a result string like the one shown in those slides, you could use values to bind a variable to dbr:Stephen_King, and then use some grouping and string concatenation to get something nicer looking (sort of):
select
(concat( ?person, " =\n", group_concat(?type; separator=" AND\n")) as ?sentence)
where {
values ?person { dbr:Stephen_King }
?type ^a ?person .
filter not exists {
?subtype ^a ?person ;
rdfs:subClassOf ?type .
filter ( ?subtype != ?type )
}
}
group by ?person
SPARQL results
http://dbpedia.org/resource/Stephen_King =
http://dbpedia.org/class/yago/AuthorsOfBooksAboutWritingFiction AND
http://dbpedia.org/ontology/Writer AND
http://schema.org/Person AND
http://xmlns.com/foaf/0.1/Person AND
http://dbpedia.org/class/yago/AmericanSchoolteachers AND
http://dbpedia.org/class/yago/LivingPeople AND
http://dbpedia.org/class/yago/PeopleFromBangor,Maine AND
http://dbpedia.org/class/yago/PeopleFromPortland,Maine AND
http://dbpedia.org/class/yago/PeopleFromSarasota,Florida AND
http://dbpedia.org/class/yago/PeopleSelf-identifyingAsAlcoholics AND
http://umbel.org/umbel/rc/Artist AND
http://umbel.org/umbel/rc/Writer AND
http://dbpedia.org/class/yago/20th-centuryNovelists AND
http://dbpedia.org/class/yago/21st-centuryNovelists AND
http://dbpedia.org/class/yago/AmericanHorrorWriters AND
http://dbpedia.org/class/yago/AmericanNovelists AND
http://dbpedia.org/class/yago/AmericanShortStoryWriters AND
http://dbpedia.org/class/yago/CthulhuMythosWriters AND
http://dbpedia.org/class/yago/HorrorWriters AND
http://dbpedia.org/class/yago/WritersFromMaine AND
http://dbpedia.org/class/yago/PeopleFromDurham,Maine AND
http://dbpedia.org/class/yago/PeopleFromLisbon,Maine AND
http://dbpedia.org/class/yago/PostmodernWriters