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!
Related
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.
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.
My ontology IRI is "http://mycompany.com/ontologies/quality". I want to find "Find all subjects with a given object property (hasSolution)"
SELECT ?subject
WHERE { ?subject hasSolution <http://mycompany.com/ontologies/quality#Issue> } LIMIT 10
It caused this error message:
Caused by: org.openrdf.query.parser.sparql.ast.TokenMgrError: Lexical error at line 8, column 30. Encountered: " " (32), after : "hasSolution"
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilderTokenManager.getNextToken(SyntaxTreeBuilderTokenManager.java:3499)
at org.openrdf.query.parser.sparql.ast.SyntaxTreeBuilder.jj_ntk(SyntaxTreeBuilder.java:8861)
I suspect the issue here is how you've expressed your predicate - it should be in IRI form, either using angle-brackets like this:
SELECT ?subject
WHERE {
?subject <http://mycompany.com/ontologies/hasSolution> <http://mycompany.com/ontologies/quality#Issue>
}
LIMIT 10
Or, using the PREFIX statement to give you something like this:
PREFIX mycoont: <http://mycompany.com/ontologies/>
SELECT ?subject
WHERE
{
?subject mycoont:hasSolution <http://mycompany.com/ontologies/quality#Issue>
}
LIMIT 10
Or, more generally, if you don't know what the predicate IRI is exactly, you can make it part of a query:
SELECT ?subject ?predicate
WHERE {
?subject ?predicate <http://mycompany.com/ontologies/quality#Issue>
}
LIMIT 10
And explore the results - the returned ?predicate values should be expressed as being full IRIs, or, depending on your query engine, as some form of prefix:suffix arrangement.
On exploring the results, you should just be able to copy-paste the appropriate value into the ?predicate triple of your WHERE clause, and remove the ?predicate triple from your SELECT clause.
SELECT ?predicate (COUNT(*)AS ?frequency)
WHERE {?subject ?predicate ?object}
GROUP BY ?predicate
ORDER BY DESC(?frequency)
LIMIT 50
I want to find and order most frequent predicates. I am interested in the frequency of predicates defined by myself, but not all these builtins as below:
rdf:type
hashcode
sourceOntology
rdfs:subClassOf
rdf:first
rdf:next
...
None of my predicates appear. How to display user defined predicates?
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> }