mercin
mercin

Reputation: 15

SPARQL query with filter for a certain date

I'm new to SPARQL and I'm trying to do the following thing for my assignment:

I need to pull out actors but only those that were born on a defined date. My problem is that every time I try to filter by a certain integer value, I get "Error making the query, see cause for details" (btw, I have no idea what "cause" is in this matter, there is no error log, or anything clickable for me to actually check what went wrong).

Here is my query:

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?actor ?dbpediaLink ?name ?date month(?date) WHERE {
        ?actor a movie:actor .
        ?actor owl:sameAs ?dbpediaLink .
    SERVICE <http://dbpedia.org/sparql> {
        ?dbpediaLink dbpprop:name ?name .
        ?dbpediaLink dbpprop:dateOfBirth ?date
    }
    FILTER(month(?date) = 2)
}
LIMIT 10

So, what I'm trying to do here is get all actors born in February, and with this I get the error mentioned above.

The following combinations of FILTER return the mentioned error:

FILTER(month(?date) = 2)
FILTER(month(?date) > 1 && month(?date) < 3)
FILTER(month(?date) >= 2 && month(?date) <=2)

Basically all the variations for getting back only people born in February return the error. I even tried casting the integers explicitly as "2"^^xsd:integer (because the month() function returns an integer) and it still failed.

I also tried (for some other problem) to get actors that have exactly 10 letters in their name but I also ran into the same problem.

It seems that any FILTER I try to make that uses = or tries to FILTER by multiple conditions (using &&) fails.

I'm stuck on this for the last 2 days...Am I missing something?

I should probably mention that I'm using locally deployed Fuseki server with LMDB dump, and I'm running the queries at http://localhost:3030

Upvotes: 0

Views: 2097

Answers (1)

RobV
RobV

Reputation: 28646

To start with your query has invalid syntax in it, you can't put month(?date) directly in your SELECT clause as you must enclose in it parentheses - ( and ) - and alias it using AS, e.g. --

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?actor ?dbpediaLink ?name ?date (month(?date) AS ?month) WHERE {
        ?actor a movie:actor .
        ?actor owl:sameAs ?dbpediaLink .
    SERVICE <http://dbpedia.org/sparql> {
        ?dbpediaLink dbpprop:name ?name .
        ?dbpediaLink dbpprop:dateOfBirth ?date
    }
    FILTER(month(?date) = 2)
}
LIMIT 10

The "cause" referred to is the exception that led to the exception shown to you. If you are using code to submit the query, catch the exception, and check the getCause() method to see if there is another exception object.

Note that if you are using a standard Fuseki setup there may be more error information logged to the terminal window in which you started the Fuseki server (or wherever you redirected that server processes output to).

The problem is unlikely to be the filter, since a FILTER is defined by the SPARQL specification to treat any errors in expression evaluation as a false.

If the problem is not just your bad syntax, then the most likely culprit is the SERVICE clause in your query which is asking Fuseki to go and query some information from http://dbpedia.org/sparql. SERVICE is defined such that if contacting the remote service fails, then the whole query fails. So it is likely you are not able to access that service from your machine, or there is an error on the DBPedia end causing the problem (which is not unheard of).

Can you browse to that URL in your browser? If you can, are you able to successfully run the portion of your query that appears in the SERVICE clause using the web interface there?

Note that you can add the SILENT keyword after the SERVICE keyword which will cause the query engine to continue even if the remote service request fails, e.g. --

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?actor ?dbpediaLink ?name ?date month(?date) WHERE {
        ?actor a movie:actor .
        ?actor owl:sameAs ?dbpediaLink .
    SERVICE SILENT <http://dbpedia.org/sparql> {
        ?dbpediaLink dbpprop:name ?name .
        ?dbpediaLink dbpprop:dateOfBirth ?date
    }
    FILTER(month(?date) = 2)
}
LIMIT 10

Upvotes: 2

Related Questions