Laborator 2

Laborator 2

Problema 1

Cod:

father(root, child1).

father(root, child2).

father(root, child3).

father(child1, child11).

father(child1, child12).

father(child2, child21).

father(child2, child22).

father(child3, child31).

child(X, Y) :- father(Y, X).

brother(X, Y) :- father(Z, X), father(Z, Y), dif(X, Y).

nephew(X, Y) :- father(Z, X), brother(Z, Y).

grandchild(X, Y) :- father(Y,Z), father(Z, X).

predecessor(X, Y) :- father(X, Y);(father(X, Z), predecessor(Z, Y)).

Apel:

child(X, root).

brother(X, child1).

nephew(X, child1).

grandchild(X, root).

predecessor(root, X).

Rezultatul obtinut:

X = child1 ;

X = child2 ;

X = child3.

X = child2 ;

X = child3 ;

X = child21 ;

X = child22 ;

X = child31.

X = child11 ;

X = child12 ;

X = child21 ;

X = child22 ;

X = child31.

X = child1 ;

X = child2 ;

X = child3 ;

X = child11 ;

X = child12 ;

X = child21 ;

X = child22 ;

X = child31 ;

Se apasa ";" pentru obtinerea urmatoarelor rezultate. Initial ne este dat doar primul rezultat.

Explicatii:

father- X este copilul lui Y daca Y este tatal lui X

brother - X este fratele lui Y daca exista un Z care sa fie tatal lui X si tatal lui Y. Trebuie sa tinem cont ca X sa fie diferit de Y pentru ca nu se poate ca X sa fie fratele lui insusi

nephew - X este nepotul lui Y daca tatal lui X este fratele lui Y

grandchild - X este nepotul(de pe bunic) lui Y daca Y(bunicul) este tatal tatalui lui X.

predecesor - cautam fiul lui X. Si apoi fiul fiului lui X Mergam pe acest principiu, daca ajungem la Y, inseamna ca X a fost predecesorul lui Y.

Problema 2

Cod:

class('Vehicle').

class('EnginePoweredVehicle').

class('HumanPoweredVehicle').

class('Car').

class('Bus').

class('Bicycle').

inherits('EnginePoweredVehicle','Vehicle').

inherits('HumanPoweredVehicle','Vehicle').

inherits('Car','EnginePoweredVehicle').

inherits('Bus','EnginePoweredVehicle').

inherits('Bicycle','HumanPoweredVehicle').

memberVariable('numberOfSeats',protected,int,'Vehicle').

memberVariable('engineCapacity',public,int,'EnginePoweredVehicle').

memberVariable('fuelConsumption',protected,float,'EnginePoweredVehicle').

memberVariable('nameOfOwner',private,'java.lang.String','Car').

memberVariable('nameOfOwnerCompany',private,'java.lang.String','Bus').

memberVariable('numberOfGears',public,int,'Bicycle').

classBrother(X, Y) :- inherits(X, Z), inherits(Y, Z), dif(X, Y).

ancestor(X, Y) :- inherits(X, Y);(inherits(X, Z), ancestor(Z, Y)).

descendant(X, Y) :- inherits(Y, X);(inherits(Z, X), descendant(Z, Y)).

containsPublicMemberVariables(X) :- memberVariable(_, 'public', _, X).

Apel:

classBrother('EnginePoweredVehicle', X).

ancestor('Bicycle', Z).

descendant('Vehicle', W).

containsPublicMemberVariables(X).

containsPublicMemberVariables('Bicycle')

Rezultatul obtinut:

X = 'HumanPoweredVehicle'

Z = 'HumanPoweredVehicle' ;

Z = 'Vehicle' ;

W = 'EnginePoweredVehicle' ;

W = 'HumanPoweredVehicle' ;

W = 'Car' ;

W = 'Bus' ;

W = 'Bicycle'

X = 'EnginePoweredVehicle' ;

X = 'Bicycle'.

true.

Explicatii:

classBrother(X, Y) :- inherits(X, Z), inherits(Y, Z), dif(X, Y).

ancestor(X, Y) :- inherits(X, Y);(inherits(X, Z), ancestor(Z, Y)).

descendant(X, Y) :- inherits(Y, X);(inherits(Z, X), descendant(Z, Y)).

containsPublicMemberVariables(X) :- memberVariable(_, 'public', _, X).

classBrother - clasa X este "frate" cu clasa "Y" daca in acelasi timp "X" extinde pe "Z" si "Y" extinde pe "Z"...bineinteles..X si Y sa fie diferiti...doar sa fim noi siguri

ancestor - vrem sa ajungem la clasa de baza(in contextul cu familia, cel mai batran, stramosul comun)

daca X il extinde pe Y inseamna ca X e "copilul" lui Y si am ajuns la clasa de baza

cautam acea clasa pe care X o extinde, adica cautam parintele lui X..acesta este Z..acum suntem cu un nivel mai sus si incercam sa vedem daca vom ajunge de la Z la Y

descendant - incercam sa ajungem de la clasa de baza pana la clasele frunze(cele mai de baza). Daca Y il mosteneste pe X, inseamna ca X este clasa parinte pentru Y si il afisam.

cautam o clasa care sa o extinda pe clasa de baza X, aceasta este Z. Parcurgem recursiv cu un nivel mai jos in ierarhia de clase si incercam acum sa ajungem de la clasa Z la Y

containsPublicMemberVariables - singurul lucru pe care ne intereseaza este cautarea campului 'public'.

Problema 3

Consider the following Java file containing a hierarchy of classes:

Consider the following facts in a Prolog movies database.

% movie(M,Y) <- movie M came out in year Y movie(the_usual_suspects, 1995).

movie(american_beauty, 1999).

movie(down_from_the_mountain, 2000).

movie(girl_with_a_pearl_earring, 2003).

movie(barton_fink, 1991).

movie(crimewave, 1985).

movie(lick_the_star, 1998).

movie(the_horse_whisperer, 1998).

movie(blade_runner, 1997).

% director(M,D) <- movie M was directed by director D director(american_beauty, sam_mendes).

director(girl_with_a_pearl_earring, peter_webber).

director(barton_fink, joel_coen).

director(crimewave, sam_raimi).

director(lick_the_star, sofia_coppola).

director(blade_runner, joseph_d_kucan).

% actor(M,A,R) <- actor A played role R in movie M actor(american_beauty, kevin_spacey, lester_burnham).

actor(american_beauty, wes_bentley, ricky_fitts).

actor(american_beauty, chris_cooper, col_frank_fitts_usmc).

actor(the_usual_suspects, kevin_spacey, roger_verbal_kint).

actor(crimewave, joel_coen, reporter_at_execution).

actor(the_horse_whisperer, chris_cooper, frank_booker).

actor(blade_runner, joseph_d_kucan, crazylegs_larry).

% actress(M,A,R) <- actress A played role R in movie M actress(american_beauty, annette_bening, carolyn_burnham).

actress(american_beauty, thora_birch, jane_burnham).

actress(american_beauty, mena_suvari, angela_hayes).

actress(girl_with_a_pearl_earring, scarlett_johansson, griet).

actress(anna, sofia_coppola, noodle).

Write queries to answer the following questions (NB. Press ; after each answer to see if there are any more answers in the database):

% In which year was the movie American Beauty released?

% Find a movie that was released in the year 2000?

% Find a movie that was released before 2000?

% Find the name and year of a movie?

% Find an actor who has appeared in more than one movie?

% Find a director who has directed a movie in which the actress Scarlett Johansson appeared?

% Find an actor who has also directed a movie?

% Find an actor or actress who has also directed a movie?

% (Hint: to do this in a single query you will need to use disjunction % (semicolon) as well as conjunction (comma).

Write a definition in Prolog for each of the following predicates:

% released_since(M,Y) <- movie M was released after year X

% released_between(M,Y1,Y2) <- movie M was released between year X and year Y inclusive

% same_year_as(M1,M2) <- movie M1 was released in the same year as movie M2

% newer(M1,M2) <- movie M1 was released after movie M2

% cast_member(A,M) <- person A was an actor or actress in movie M % (Give as a two predicates)

% cast_member2(A,M) <- person A was an actor or actress in movie M % (Give as a single predicate using the ; disjunction predicate)

% directed_by(X,Y) <- person X has been in a movie directed by Y % (Hint: re-use your cast_member/2 predicate)

Cod:

released_since(M, Y) :- movie(M, Z), Z =< Y.

same_year_as(M1, M2) :- movie(M1,Y1), movie(M2, Y2), Y1=:=Y2, dif(M1, M2).

newer(M1, M2) :- movie(M1, Y1), movie(M2, Y2), Y1 > Y2.

cast_member(A, M) :- actor(M, A,_); actress(M, A, _).

directed_by(X, Y) :- director(M, Y), cast_member(X, M).

Apel și Rezultat:

movie(american_beauty, X).

X = 1999.

movie(X, 2000).

X = down_from_the_mountain

movie(X, Y), Y<2000.

X = american_beauty,

Y = 1999 ;

X = barton_fink,

Y = 1991 ;

X = crimewave,

Y = 1985 ;

X = lick_the_star,

Y = 1998 ;

X = the_horse_whisperer,

Y = 1998 ;

X = blade_runner,

Y = 1997.

movie(X, Y).

X = american_beauty,

Y = 1999 ;

X = down_from_the_mountain,

Y = 2000 ;

X = girl_with_a_pearl_earring,

Y = 2003 ;

X = barton_fink,

Y = 1991 ;

X = crimewave,

Y = 1985 ;

X = lick_the_star,

Y = 1998 ;

X = the_horse_whisperer,

Y = 1998 ;

X = blade_runner,

Y = 1997.

actor(M, A, _), actor(M2, A, _), dif(M, M2).

M = american_beauty,

A = chris_cooper,

M2 = the_horse_whisperer ;

M = the_horse_whisperer,

A = chris_cooper,

M2 = american_beauty

director(M, D), actress(M, scarlett_johansson,_).

M = girl_with_a_pearl_earring,

D = peter_webber ;

director(M, D), actor(_, D, _). // Aflam un film in care D este director, si aflam si pe D care este actor

M = barton_fink,

D = joel_coen ;

M = blade_runner,

D = joseph_d_kucan

director(M, D), actor(M, D, _). // Aflam acel actor care a fost si director

M = blade_runner,

D = joseph_d_kucan.

director(M, D),( actor(M, D, _); actress(M, D, _)).

M = blade_runner,

D = joseph_d_kucan ;

released_since('blade_runner', 1997). // Eu inteleg cerinta ca fiind: "Era filmul deja lansat in anul X?"

true

released_since('blade_runner', 56).

false.

same_year_as(M1, M2).

M1 = lick_the_star,

M2 = the_horse_whisperer ;

M1 = the_horse_whisperer,

M2 = lick_the_star ;

newer(M1, M2).

M1 = american_beauty,

M2 = barton_fink ;

M1 = american_beauty,

M2 = crimewave ;

M1 = american_beauty,

M2 = lick_the_star ;

M1 = american_beauty,

M2 = the_horse_whisperer ;

M1 = american_beauty,

M2 = blade_runner ;

M1 = down_from_the_mountain,

M2 = american_beauty ;

M1 = down_from_the_mountain,

M2 = barton_fink ;

M1 = down_from_the_mountain,

M2 = crimewave ;

M1 = down_from_the_mountain,

M2 = lick_the_star ;

M1 = down_from_the_mountain,

M2 = the_horse_whisperer ;

M1 = down_from_the_mountain,

M2 = blade_runner ;

M1 = girl_with_a_pearl_earring,

M2 = american_beauty ;

M1 = girl_with_a_pearl_earring,

M2 = down_from_the_mountain ;

M1 = girl_with_a_pearl_earring,

M2 = barton_fink ;

M1 = girl_with_a_pearl_earring,

M2 = crimewave ;

M1 = girl_with_a_pearl_earring,

M2 = lick_the_star ;

M1 = girl_with_a_pearl_earring,

M2 = the_horse_whisperer ;

M1 = girl_with_a_pearl_earring,

M2 = blade_runner ;

M1 = barton_fink,

M2 = crimewave ;

M1 = lick_the_star,

M2 = barton_fink ;

M1 = lick_the_star,

M2 = crimewave ;

M1 = lick_the_star,

M2 = blade_runner ;

M1 = the_horse_whisperer,

M2 = barton_fink ;

M1 = the_horse_whisperer,

M2 = crimewave ;

M1 = the_horse_whisperer,

M2 = blade_runner ;

M1 = blade_runner,

M2 = barton_fink ;

M1 = blade_runner,

M2 = crimewave ;

cast_member('chris_cooper', M).

M = american_beauty ;

M = the_horse_whisperer ;

directed_by(X, 'peter_webber').

X = scarlett_johansson.

Explicatii:

Rezolvarea este simpla si usor de inteles. Nu este nevoie de explicatii suplimentare.