Search code examples
sparqlowl

Why does this SPARQL query generate an error


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)

Solution

  • 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.