2

I want to write a query to fetch triples about entities related to a film. This is the query:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX : <http://dbpedia.org/resource/>

SELECT DISTINCT ?s1 WHERE {

SERVICE <http://dbpedia.org/sparql/> {

?film ?p ?o
FILTER (?film = :Braveheart) .

OPTIONAL {
{
{
{?o ?p2 ?o2 .}
OPTIONAL {
  {?o2 ?p21 ?o21}
  UNION
  {?s21 ?p22 ?o2}
}
}

UNION

{?s1 ?p3 ?o}
}
}
}
}

This query does not fetch results. However, commenting the section

OPTIONAL {
  {?o2 ?p21 ?o21}
  UNION
  {?s21 ?p22 ?o2}
}

generates the results. I want to understand why the OPTIONAL clause prevents the results from being generated and how I can resolve the issue.

kurious
  • 1,024
  • 10
  • 29

2 Answers2

2

Try (note: timeout in original query was due to Cartesian product arising from placement of OPTIONAL CLAUSE.) :

SELECT DISTINCT ?s1  WHERE 
  {
    dbr:Braveheart ?p ?o
    OPTIONAL 
      {
        {
          {?s1 ?p3 ?o}
          UNION
          {
            {?o ?p2 ?o2 }
            OPTIONAL 
              {
                {?o2 ?p21 ?o21}
                UNION
                {?s21 ?p22 ?o2}
              }
          }
        }
      } 
  }

Links:

1

I don't know where did you pose this query (I assume Sparql endpoint) and what does it mean does not fetch any results but I have written the query a little bit simpler as following and executed on the endpoint:

SELECT DISTINCT ?s1 WHERE {
dbr:Braveheart ?p ?o
    OPTIONAL {
    {
        {
            {?o ?p2 ?o2 .}
            OPTIONAL {
              {?o2 ?p21 ?o21}
              UNION
              {?s21 ?p22 ?o2}
            }
        }
       UNION
       {?s1 ?p3 ?o}
    }
   } 
  }

It does not return results because of the time out. Otherwise, there is no problem with the OPTIONAL clause. For example, if you put "dbo:narrator" instead of "?p" you will see the results.

Erwarth
  • 547
  • 6
  • 18