2

I'm trying to create a Prolog query for a database I translated from mySql to Prolog facts.

The Prolog facts regard many tables, but the relevant ones for this query are just two:

actor(ID,firstName,lastName)
film_actor(actor_id,film_id)
film(ID, title,_,_,_,_,_,_,_)

I'm trying to create a query that states the following:

List the title of the films that have exactly 5 actors.

So far I managed to create this query that lists me all the films and the ID of each actor in that film:

all((T,AID),(film(FID,C,T,_,_,_,_,_,_),film_actor(AID,FID)),RS).

What I need is to count this AID and state that is must be equal to 5 occurrences for each film_id But I do not know how to do that from the yap documentation I read so far.

Any tips?

Thanks in advance

0xfabiof
  • 41
  • 2

1 Answers1

1

You can just use findall, followed by length to constraint the list to 5 elements, to get one film ID.

one_film_with_5_actors(FilmID,ActorIDs) :-
  findall(AID, (film(FilmID,_,_,_,_,_,_,_,_),film_actor(AID,FilmID)), ActorIDs),
  length(ActorIDs, 5).

Prolog will return on backtracking every pair (FilmID,ActorIDs), then apply elementary accessors to this 'grouped by' DB view to get the info you need.

CapelliC
  • 59,646
  • 5
  • 47
  • 90