Diego Santana
Diego Santana

Reputation: 23

SPARQL query returns different results depending on the order of statements

I have a SPARQL query which returns the most specific common classes of two resources.

When I try to run it on https://dbpedia.org/sparql, sometimes it returns nothing, and other times it returns the classes I want.

I have noticed it is related to the order of the statements in the query.

This is not a desirable behavior because, when I execute the query, I expect it to have the same results regardless of the order in which I input the URIs of the resources.

Has anyone experienced this problem and found a solution for it?

Queries

The following query works properly, returning http://dbpedia.org/ontology/Film and http://dbpedia.org/ontology/Wikidata:Q11424 as results:

SELECT ?lcs
WHERE
{
  <http://dbpedia.org/resource/Ice_Age_(2002_film)> a ?class1 .
  ?class1 rdfs:subClassOf* ?lcs .
  <http://dbpedia.org/resource/Finding_Nemo> a ?class2 .
  ?class2 rdfs:subClassOf* ?lcs .

  FILTER NOT EXISTS {
    <http://dbpedia.org/resource/Finding_Nemo> a ?class3 .
    ?class3 rdfs:subClassOf* ?sublcs .
    <http://dbpedia.org/resource/Ice_Age_(2002_film)> a ?class4 .
    ?class4 rdfs:subClassOf* ?sublcs .
    ?sublcs  rdfs:subClassOf ?lcs
  }

  FILTER strstarts(str(?lcs), "http://dbpedia.org/ontology")
}

However, the following query returns nothing:

SELECT ?lcs
WHERE
{
  <http://dbpedia.org/resource/Finding_Nemo> a ?class1 .
  ?class1 rdfs:subClassOf* ?lcs .
  <http://dbpedia.org/resource/Ice_Age_(2002_film)> a ?class2 .
  ?class2 rdfs:subClassOf* ?lcs .

  FILTER NOT EXISTS {
    <http://dbpedia.org/resource/Finding_Nemo> a ?class3 .
    ?class3 rdfs:subClassOf* ?sublcs .
    <http://dbpedia.org/resource/Ice_Age_(2002_film)> a ?class4 .
    ?class4 rdfs:subClassOf* ?sublcs .
    ?sublcs  rdfs:subClassOf ?lcs
  }

  FILTER strstarts(str(?lcs), "http://dbpedia.org/ontology")
}

Upvotes: 2

Views: 483

Answers (1)

TallTed
TallTed

Reputation: 9434

  1. You're running your queries on a public endpoint, which has a variety of self-protection settings in place. This means that expensive queries may produce partial or no results, while inexpensive queries may produce full results. You can set up your own mirror endpoint (quick-and-easy on Amazon EC2) with no such protections, and you should see equivalent results from equivalent queries.

  2. @AKSW provided a tweak to your query that quickly produces full results --

    add FILTER strstarts(str(?sublcs), "http://dbpedia.org/ontology") to the NOT EXISTS part

    (see the query form and live results)

    SELECT ?lcs
    WHERE
    {
      <http://dbpedia.org/resource/Finding_Nemo>  a  ?class1  .
      ?class1  rdfs:subClassOf*  ?lcs  .
      <http://dbpedia.org/resource/Ice_Age_(2002_film)>  a  ?class2  .
      ?class2  rdfs:subClassOf*  ?lcs  .
    
      FILTER NOT EXISTS {
        <http://dbpedia.org/resource/Finding_Nemo>  a  ?class3  .
        ?class3  rdfs:subClassOf*  ?sublcs  .
        <http://dbpedia.org/resource/Ice_Age_(2002_film)>  a  ?class4  .
        ?class4  rdfs:subClassOf*  ?sublcs .
        ?sublcs  rdfs:subClassOf  ?lcs 
        FILTER strstarts( str(?sublcs), "http://dbpedia.org/ontology" )
      }
    
      FILTER strstarts( str(?lcs), "http://dbpedia.org/ontology" )
    }
    

(ObDisclaimer: OpenLink Software produces Virtuoso, which powers the DBpedia endpoint, and the AMI referenced above. They also employ me.)

Upvotes: 1

Related Questions