Strange Apache Jena OPTIONAL behavior

160 views Asked by At

I'm using Jena (TDB 0.10.1, CORE/ARQ 2.10.1) from the maven repository. I imported this file:

tdbloader --graph=http://linkedgeodata.org --loc=$bsdStore $lgdData"supermarkets.ttl"

I'm now trying to query this model like so:

PREFIX lgd: <http://linkedgeodata.org/ontology/> 
PREFIX wgs: <http://www.w3.org/2003/01/geo/wgs84_pos#>  
PREFIX geo: <http://geovocab.org/geometry#>  
PREFIX gis:     <http://www.opengis.net/ont/geosparql#>  
PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>  

SELECT DISTINCT ?supermarket ?label ?latitude ?longitude ?points  
FROM <http://linkedgeodata.org>  
WHERE {  
    ?supermarket a lgd:Supermarket .  
    ?supermarket ?p ?o .  
    OPTIONAL { 
      ?supermarket rdfs:label ?label .  
      ?supermarket geo:geometry ?geo .  
      ?geo gis:asWKT ?points .  
      ?supermarket wgs:lat ?latitude .  
      ?supermarket wgs:long ?longitude .  
    }
} 

Unfortunatley this returns am empty result set. If I now move the wgs patterns to a separate OPTIONAL I get the correct results.

SELECT DISTINCT ?supermarket ?label ?latitude ?longitude ?points  
FROM <http://linkedgeodata.org>  
WHERE {  
    ?supermarket a lgd:Supermarket .  
    ?supermarket ?p ?o .  
    OPTIONAL { 
         ?supermarket rdfs:label ?label .  
         ?supermarket geo:geometry ?geo .  
         ?geo gis:asWKT ?points .  
    }
    OPTIONAL {
         ?supermarket wgs:lat ?latitude .  
         ?supermarket wgs:long ?longitude .  
    }
}     

Is this a bug in Jena or is my query wrong? Cheers, Daniel

1

There are 1 answers

0
AndyS On BEST ANSWER

Th whole of the OPTIONAL block must match; it does not make each of the triple patterns independently optional. That would be:

WHERE {  
    ?supermarket a lgd:Supermarket .  
    ?supermarket ?p ?o .  
    OPTIONAL { ?supermarket rdfs:label ?label .  }
    OPTIONAL { ?supermarket geo:geometry ?geo .  }
    OPTIONAL {   ?geo gis:asWKT ?points .  }
    OPTIONAL {   ?supermarket wgs:lat ?latitude .  }
    OPTIONAL {   ?supermarket wgs:long ?longitude .  }
    }
}

so if the second one has some results, presumably for any one ?supermarket, one of the OPTIONALs matches and the other does not.