Why does this SPARQL query generate an error - sparql

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.

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

SPARQL BIND(COUNT(...) AS ...) not working [duplicate]

This question already has an answer here:
Sparql BIND result of count WITH A VARIABLE
(1 answer)
Closed 6 years ago.
I'm trying to assign a count to a variable for later use in the query (SPARQL).
I can't even get the following to work:
SELECT ?resultsCount
WHERE{
?subject ?predicate ?object.
BIND(COUNT(?object) AS ?resultsCount)
}
There is something wrong with my syntax or semantics here, as I simply get an empty result repeated ?resultsCount many times, instead of simply e.g. 86 (number of results).
However, I get the number of results (e.g. 86) when I do the following:
SELECT (COUNT(?object) AS ?resultsCount)
WHERE{
?subject ?predicate ?object
}
Is there any way to get BIND to work with COUNT like in the first example? If not, is there a different correct way to get the same sort of functionality?
I'm using Blazegraph 2.1.2. Could it be a bug with that?
count is an aggregate function. To use count, you need to use group by in your query, and use count in the projection. Count will count the number of results within each group. You can also use distinct with count, so you could do something like the following to get the number of distinct objects for each subject:
select ?subject (count(?object) as ?numObjects) {
?subject ?predicate ?object
}
group by ?subject
If you're just trying to count the number of matches, then you can use count without a group by (which gives you a single, implicit, group):
select (count(*) as ?numResults) {
?subject ?predicate ?object
}

Querying subclasses through Fuseki

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