Re: Not understanding UNION well, or something

To summarize from the blog post, the goal is to find all actors that 
appear in both a John Waters and a Steven Spielberg film. But now you 
also want to find all the movies (by those directors) that the actor was 
in. If my understanding is right, this is how I would go about it 
(untested):

PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dc:    <http://purl.org/dc/terms/>
SELECT ?actorName ?dirName ?movieName WHERE {
   # bind the two directors
   ?jw movie:director_name "John Waters" .
   ?ss movie:director_name "Steven Spielberg" .

   # we want to find two movies, one by each director
   # but we use two variables so that we can only
   # pull out the name of one of them
   {
     ?movie1 movie:director ?jw .
     ?movie2 movie:director ?ss .
   } UNION {
     ?movie2 movie:director ?jw .
     ?movie1 movie:director ?ss .
   }

   ?movie1 dc:title ?movieName .

   # the actor needs to be in both movies
   ?movie1 movie:actor ?actor .
   ?movie2 movie:actor ?actor .

   ?actor movie:actor_name ?actorName .

   # we need to repeat the director information
   # to be able to bind a variable to the director's
   # name - this may give extra results if a movie has
   # multiple directors
   ?movie1 movie:director [ movie:director_name ?dirName ] .
}

There may yet be a more elegant way to do this, and I'm not positive 
I've got this right. I think that ARQ at least has a LET assignment 
operator that would avoid the need for the extra director binding there 
at the end.

Another way to approach this that is quite similar but might be 
considered easier would be to use FILTERs:

PREFIX movie: <http://data.linkedmdb.org/resource/movie/>
PREFIX dc:    <http://purl.org/dc/terms/>
SELECT DISTINCT ?actorName ?dirName1 ?movieName WHERE {

   # this is the info we want to pull out
   ?movie1 dc:title ?movieName .
   ?actor movie:actor_name ?actorName .

   # two movies with two directors
   ?movie1 movie:director [ movie:director_name ?dirName1 ].
   ?movie2 movie:director [ movie:director_name ?dirName2 ]

   # the same actor needs to be in both movies
   ?movie1 movie:actor ?actor .
   ?movie2 movie:actor ?actor .

   # use the filter to check the director names
   FILTER(
     (?dirName1 = 'John Waters' && ?dirName2 = 'Steven Spielberg') ||
     (?dirName2 = 'John Waters' && ?dirName1 = 'Steven Spielberg')
   ) .
}

This second one avoids some of the silliness since it relies on the fact 
that each actor for which this works will match the pattern two ways 
(one way with JW bound to ?dirName1 and one with SS  bound to it). We 
want to get *both* these results.

In practice, I'd usually do something like this with multiple queries - 
first find the relevant actors, then find their movies by JW and by SS.

hope this is helpful,
Lee

Bob DuCharme wrote:
> 
> I'm trying to expand the query shown at 
> http://www.snee.com/bobdc.blog/2008/11/sparql-at-the-movies.html#id203668 
> to include the director and movie names in the result of the query sent 
> to http://data.linkedmdb.org/sparql. I guess my main problem is trying 
> to understand how I can set it up so that ?actor is bound to the same 
> value throughout the query but ?movie and ?movieName can be bound to 
> different values in the two patterns. I know that one actor was in a 
> single movie by each of the two directors named below, and while the 
> following doesn't give me an error when submitted it gives an unrelated 
> set of data. I may be going about it completely wrong.
> 
> Any suggestions?
> 
> thanks,
> 
> Bob
> 
> ####################
> 
> SELECT ?actorName ?dirName ?movieName WHERE {
> 
>  ?dir1 <http://data.linkedmdb.org/resource/movie/director_name> "John 
> Waters".
>  ?dir2 <http://data.linkedmdb.org/resource/movie/director_name> "Steven 
> Spielberg".
> 
>  ?actor  <http://data.linkedmdb.org/resource/movie/actor_name> ?actorName.
> 
>   {
>     ?movie <http://data.linkedmdb.org/resource/movie/director> ?dir1;
>            <http://data.linkedmdb.org/resource/movie/actor> ?actor;
>            <http://purl.org/dc/terms/title> ?movieName.
>     ?dir1 <http://data.linkedmdb.org/resource/movie/director_name> 
> ?dirName.
>   }
> 
>   UNION
> 
>   {
>     ?movie <http://data.linkedmdb.org/resource/movie/director> ?dir2;
>            <http://data.linkedmdb.org/resource/movie/actor> ?actor;
>            <http://purl.org/dc/terms/title> ?movieName.
>     ?dir2 <http://data.linkedmdb.org/resource/movie/director_name> 
> ?dirName.
>   }
> }
> 
> 

Received on Thursday, 13 November 2008 01:09:00 UTC