correção dos exercícios de engenharia do conhecimento em prolog
DESCRIPTION
Correção dos exercícios de engenharia do conhecimento em Prolog. Jacques Robin, DI-UFPE www.di.ufpe.br/~jr. Estudo de caso: a terrível novela Requisitos em Inglês. 1. A soap opera is a TV show whose characters include a husband, a wife and a mailman such that: - PowerPoint PPT PresentationTRANSCRIPT
Correção dos exercícios de Correção dos exercícios de engenharia do conhecimento em engenharia do conhecimento em
PrologProlog
Jacques Robin, DI-UFPEwww.di.ufpe.br/~jr
Estudo de caso: Estudo de caso: a terrível novelaa terrível novelaRequisitos em InglêsRequisitos em Inglês
1. A soap opera is a TV show whose characters include a husband, a wife and a mailman such that:
2. the wife and the mailman blackmail each other3. everybody is either alcoholic, drug addict or gay4. Dick is gay, Jane is alcoholic and Harry is a drug addict 5. the wife is always an alcoholic and the long-lost sister of her husband 6. the husband is always called Dick and the lover of the mailman7. the long-lost sister of any gay is called either Jane or Cleopatra8. Harry is the lover of every gay9. Jane blackmails every drug addicted lover of Dick10. soap operas are invariably terrible!0. Who are the characters of a terrible TV show?
Correção do exercício 1: Correção do exercício 1: A terrível novelaA terrível novela em L1 em L1
0%)),(),(),(
)()((,,,,
4%))()()(
))))),()),(),(((,,(
1%)),()),((,(
1%)),()),((,(
1%)),()),((,(()((,(
10%))))(
9%))),()),()(((,(
8%))),()((,(
7%)))())(),(((,,(
6%))),(
5%),()(
2%),(
1%),(),(),((,,(
3%))))()()((),((,(
)((
)((,((
MXcharacterWXcharacterHXcharacter
XterribleXtvShowMWHX
harrydrugAddictjanealcoholicdickgay
WHmarriedWXwifeHXhusbandWH
MXcharacterMXmailmanM
WXcharacterWXwifeW
HXcharacterHXhusbandHXtvShowX
Xterrible
AjaneblackmailAdickloversAdrugAddictA
harryLloversLgayL
cleopatraSjaneSGgayGSsterlongLostSiGS
MdickloversdickH
dickWsterlongLostSiWalcoholic
MWblackmail
MXmailmanWXwifedickXhusbandMW
CdrugAddictCalcoholicCgayCXcharacterC
XtvShow
XsoapOperaX
Correção do exercício 2:Correção do exercício 2:A terrível novelaA terrível novela em Prolog em Prolog
tvShow(Cast,Qual) :- soapOpera(Cast,Qual).
soapOpera([dick,W,M],terrible) :- soapCast([dick,W,M]), blackmail(W,M), alcoholic(W), longLostSister(W,dick), lover(M,dick).
soapCast([]).soapCast([H|T]) :- soapChar(H),
soapCast(T).soapChar(C) :- alcoholic(C).soapChar(C) :- drugAddict(C).soapChar(C) :- gay(C).
gay(dick).alcoholic(jane).drugAddict(harry).
lover(harry,G) :- gay(G).
longLostSister(jane,G) :- gay(G).longLostSister(cleopatra,G) :- gay(G).
blackmail(jane,M) :- lover(M,dick), drugAddict(M).
?-tvShow(Cast,terrible). Cast = [dick,jane,harry].
Estudo de caso: Estudo de caso: o BD acadêmicoo BD acadêmicoRequisitos em InglêsRequisitos em Inglês
1. Bob is 40 and the manager of the CS department.
2. His assistants are John and Sally. 3. Mary’s highest degree is an MS and she
works at the CS department. 4. She co-authored with her boss and her
friends, John and Sally, a paper published in the Journal of the ACM.
5. Phil is a faculty, who published a paper on F-Logic at a Conference of the ACM, jointly with Mary and Bob.
6. Every faculty is a midaged person who writes article, makes in the average $50,000 a year and owns a degree of some kind, typically a PhD.
7. One is considered midage if one is between 30 and 50 years old.
8. A faculty’s boss is both a faculty and a manager.
9. Friends and children of a person are also persons.
10. Every department has a manager who is an employee and assistants who are both employees and students
11. A boss is an employee who is the manager of another employee of the same department.
12. A joint work is a paper that is written by two faculties
13. There are three types of papers: technical reports, journal papers and conference papers
0a: Who are the midaged employees of the CS department and who are their boss?
0b: Who published jointly with Mary in the Journal of the ACM?
0c: Where did Mary published joint work with Phil?
Correção do exercício 3:Correção do exercício 3:O banco de dados acadêmico O banco de dados acadêmico em Prologem Prolog
1/ fatos ground1/ fatos ground
person(bob).age(bob,40).works(bob,cs,faculty).manager(cs,bob).dept(cs).
works(john,cs,assistant).study(john,cs).works(sally,cs,assistant).study(sally,cs).
hiDeg(mary,ms).works(mary,cs,faculty).friends(mary,bob).friends(mary,sally).
works(phil,cs,faculty).
degree(phd).degree(ms).
journal(jacm).conf(cacm).article(flogic,[john,sally,mary,bob],jacm).article(florid,[phil,mary,bob],cacm).
Correção do exercício 3:Correção do exercício 3:O banco de dados acadêmico O banco de dados acadêmico em Prologem Prolog
2/ regras de dedução2/ regras de dedução
hiDeg(F,phd) :- works(F,_,faculty), not hiDeg(F,ms).
salary(P,5000) :- works(F,_,faculty), not salary(F,_).
midaged(F) :- age(F,A), !, integer(A), A >= 30, A =< 50.
midaged(F) :- works(F,_,faculty).works(B,D,faculty) :- manager(D,B),
works(E,D,faculty), !.activity(F,paperWriting) :-
works(F,_,faculty).
person(P2) :- friends(P1,P2), person(P1).
person(C) :- parent(A,C), person(A).person(P) :- study(P,D), dept(D).person(P) :- works(P,_,D), dept(D).
works(S,D,assistant) :- study(S,D), dept(D), works(S,D,_), !.
works(M,D,_) :- manager(M,D).boss(B,E) :- manager(D,B), works(E,D,_).
jointWork(W,F1,F2,P) :- works(F1,_,faculty), works(F2,_,faculty), F1 \= F2, report(W,Fl,P), member(F1,Fl), member(F2,Fl).
member(H,[H|T]).member(X,[H|T]) :- member(X,T).
report(T,Al,J) :- article(T,Al,J), journal(J).report(T,Al,C) :- article(T,Al,C), conf(C).report(T,Al,D) :- techrep(T,Al,D), dept(D).
Correção do exercício 3:Correção do exercício 3:O banco de dados acadêmico O banco de dados acadêmico em Prologem Prolog
3/ consultas3/ consultas
midagedWorkerOf(E,D) :- works(E,D,_), midaged(E).
bossOfMidagedWorkerOf(B,D) :- midagedWorkerOf(E,D), boss(B,E).
? setof(E,midagedWorkerOf(E,cs),Le), setof(B,bossOfMidagedWorkerOf(B,cs),Lb). E = _20, Le = [bob,mary], B = _51, Lb = [bob]; no.
? setof(F,jointWork(_,F,mary,jacm),Lf). F = _20, Lf = [bob]; no.
? setof(P,jointWork(_,phil,mary,P),Lp).
P = _20, Lp = [cacm]; no
Correção do exercício 4:Correção do exercício 4:O banco de dados acadêmico O banco de dados acadêmico em L1em L1
1/ formulas ground1/ formulas ground
) conf(cacmacm) journal(j) degree(msd) degree(phdept(cs)
cacm) logic,bob, article(f) ,mary,cacmcle(floridcacm) artiorid,phil,article(fl
jacm) logic,bob, article(fjacm) ogic,mary,article(fl
y,jacm) logic,sall article(fjacm) ogic,john,article(fl
ary,sally) friends(mary,bob) friends(mty) y,cs,facul works(mar,ms) hiDeg(mary
ly,cs) study(saltant)y,cs,assisworks(sall
n,cs) study(johant) ,cs,assistworks(johny),cs,facultworks(phil
s,bob) manager(cy) ,cs,facult works(bob0) age(bob,4) person(bob
Correção do exercício 4:Correção do exercício 4:O banco de dados acadêmico O banco de dados acadêmico em L1em L1
2/ formulas quantificadas2/ formulas quantificadas
)))).,,())()(),,((()),,()))()((),,((((,,,(
11%))))),1,1(,2(
)),1,1(,1(
))),2,(),1,((,(()2,1,((2,1,(
10%))))),()2,,()1,,((2,1,(),((,,(
9%))))),2(),,2((,2(
)),1(),,1((,1()),,(),((,,(()(((,(
8%))))2,1,(,,2())1,(,(()),,1(,,((,1(7%))))()(()),(),()(((,,,(6%)))),,()),()),,(,(((,,(5%))))50000,(
)),,(,,()),()),(((
)((),,(((,,(
PAWtechrepPconfPjournalPAWreportPAWarticlePconfPjournalPAWreportPAW
facultyDFworksD
facultyDFworksD
PFWreportPFWreportPFFWjoinWorkFFW
BDmanagerPDBworksPDEworksPPDEBbossEB
DAstudyassistantDAworksA
DAstudyassistantDAworksAPDMworksMDmanagerPMDdeptD
EEWjoinWorkWEEBbossBFDEworksFDECpersonFpersonCPparentFPfriendsPpersonCFP
facultyDBworksFBbossfacultyDFworksDBFFavgSalary
PFRarticlePRphdFhiDegmsFhiDeg
FmidagedfacultyDFworksDF
Correção do exercício 4:Correção do exercício 4:O banco de dados acadêmico O banco de dados acadêmico em L1em L1
3/ consultas3/ consultas
bPmaryWarticlemaryphilWjoinWorkWPbjacmmaryWarticlemaryFWjoinWorkWFaEBbossPcsEworksPEmidagedBE
0%)),,(),,((,,0%)),,(),,((,,0%)),()),,(,()((,,
Estudo de caso: Estudo de caso: A curiosidade matou o gato? A curiosidade matou o gato?
Requisitos em inglês
1. Jack owns a dog.2. Every dog owner is an animal
lover.3. No animal lover kills an animal.4. Either Jack or curiosity killed
Tuna5. Tuna is a catA. Did curiosity kill the cat?B. Quem matou o gato?
Em L1 1. x Dog(x) Owns(Jack,x)2. x (y Dog(y) Owns(x,y))
AnimalLover(x)3. x,y (AnimalLover(x)
Animal(y)) Kills(x,y)4. (Kills(Jack, Tuna) Kills(Curiosity, Tuna))
Kills(Jack, Tuna) Kills(Curiosity, Tuna))5. Cat(Tuna)6. x Cat(x) Animal(x)
0. Kills(Curiosity,Tuna)B. X, Kills(X,Tuna)
Exercício 5: Exercício 5: A curiosidade matou o gatou? em PrologA curiosidade matou o gatou? em Prolog
owns(jack,dog1). /* 1 */dog(dog1). /* 1 */animalLover(H) :- owns(H,A), animal(A). /* 2 */notKills(X,A) :- animalLover(X), animal(A), !. /* 3 */ notKills(X,A) :- not kills(X,A). /* 3 */kills(curiosity,tuna) :- notKills(jack,tuna). /* 4 */kills(jack,tuna) :- notKills(curiosity,tuna). /* 4 */cat(tuna). /* 5 */animal(A) :- cat(A). /* 6 */animal(A) :- dog(A). /* 7 */ ?- kills(curiosity,tuna).yes ?- kills(curiosity,X).X = tunaMore (y/n)? yno ?
Exercício 6:Exercício 6:A curiosidade matou o gato? em LIFEA curiosidade matou o gato? em LIFE
Arquivo da curiosidade e tuna
load("glOnto.lf")?
animal := {cat; dog}. % 6-7dog1 <| dog. % 1 tuna <| cat. % 5 feeling <| abstObj. curiosity <| feeling.
owns(jack,dog1). % 1 love(H,animal) :- owns(H,dog). % 2 notKills(X,Y) :- love(X,Y), !. % 3 notKills(X,Y) :- not kills(X,Y). % 3
kills(curiosity,tuna) :- notKills(jack,tuna). % 4 kills(jack,tuna) :- notKills(curiosity,tuna). % 4
Arquivo da ontologia geralentities := {situation; object; quality;
quantity; place; time}. situation := {event; relation}. event := {action; happening}. object := {physObj; abstObj}. physObj := {liveBeing; artefact}. person <| liveBeing.checkList([],Sort) -> true.checkList([Sort|Tail],Sort) -> checkList(Tail).
Consultas:> kills(curiosity,tuna)?*** Yes--1> *** No> kills(X,Y)?*** Yes, X = curiosity, Y = tuna.
--1> *** No
Estudo de caso: Coloração de mapaEstudo de caso: Coloração de mapa
Colorir mapa tal que:• países adjacentes• de cores diferentes
A B
C
F
D
E
Instância de problema de resolução de restrições
A B
C
F
D
E
Exercício 7: Coloração de mapa em Exercício 7: Coloração de mapa em Prolog Prolog
Exercício 8: Coloração de mapa em LIFEExercício 8: Coloração de mapa em LIFE