Include Paulo Moura's Logtalk OO LP system

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@53 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc
2001-06-06 19:40:57 +00:00
parent 38247e38fc
commit cc4531cd1e
344 changed files with 27125 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
To load all objects in this example consult the birds.loader utility
file.
You will also need to also load the library/hierarchies.loader file.
Note that the *.loader files are Prolog files.
This folder contains an example of a bird identification expert system
adopted with permission from the book "Adventure in Prolog" by Amzi! inc.
The book is available on-line in HTML format at the URL:
http://www.amzi.com
Please refer to the book for more information on the original example.
The bird identification hierarchy is organized as a prototype hierarchy
as follows:
<order>
<family>
<bird>
order
falconiforms
falcon
peregrine_falcon
sparrow_hawk
vulture
california_condor
turkey_vulture
passerformes
flycatcher
ash_throated_flycatcher
great_crested_flycatcher
swallow
barn_swallow
cliff_swallow
purple_martin
tubenose
fulmar
albatross
black_footed_albatross
laysan_albatross
waterfowl
duck
female_mallard
male_mallard
pintail
goose
canada_goose
snow_goose
swan
trumpeter_swan
whistling_swan

View File

@@ -0,0 +1,72 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.8.4
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
=================================================================
% ask the expert system for help in identifying a bird:
| ?- expert::identify.
Bird identification expert system
bill:sharp_hooked? (yes or no): yes.
eats:birds? (yes or no): yes.
feet:curved_talons? (yes or no): yes.
head:large? (yes or no): yes.
What is the value for tail?
1 : narrow_at_tip
2 : forked
3 : long_rusty
4 : square
5 : other
Enter the number of choice> 1.
wings:long_pointed? (yes or no): yes.
Possible identification : peregrine_falcon
No (more) candidates found.
(16379 ms) yes
% identify another bird:
| ?- expert::identify.
Bird identification expert system
bill:sharp_hooked? (yes or no): no.
bill:flat? (yes or no): no.
bill:short? (yes or no): no.
bill:hooked? (yes or no): yes.
What is the value for flight?
1 : ponderous
2 : powerful
3 : agile
4 : flap_glide
5 : other
Enter the number of choice> 2.
color:dark? (yes or no): yes.
live:at_sea? (yes or no): yes.
nostrils:external_tubular? (yes or no): yes.
What is the value for size?
1 : large
2 : plump
3 : medium
4 : small
Enter the number of choice> 1.
wings:long_narrow? (yes or no): yes.
Possible identification : black_footed_albatross
No (more) candidates found.
(34624 ms) yes
| ?-

View File

@@ -0,0 +1,14 @@
:- object(albatross,
imports(descriptors),
extends(tubenose)).
family(albatross).
size(large).
wings(long_narrow).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(ash_throated_flycatcher,
imports(descriptors),
extends(flycatcher)).
throat(white).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(barn_swallow,
imports(descriptors),
extends(swallow)).
tail(forked).
:- end_object.

View File

@@ -0,0 +1,41 @@
:- initialization(
logtalk_load([
descriptors,
expert,
order,
falconiforms, % orders
passerformes,
tubenose,
waterfowl,
albatross, % families
duck,
falcon,
flycatcher,
goose,
swallow,
swan,
vulture,
ash_throated_flycatcher, % birds
barn_swallow,
black_footed_albatross,
california_condor,
canada_goose,
cliff_swallow,
female_mallard,
fulmar,
great_crested_flycatcher,
laysan_albatross,
male_mallard,
peregrine_falcon,
pintail,
purple_martin,
snow_goose,
sparrow_hawk,
trumpeter_swan,
turkey_vulture,
whistling_swan])).

View File

@@ -0,0 +1,10 @@
:- object(black_footed_albatross,
imports(descriptors),
extends(albatross)).
color(dark).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(california_condor,
imports(descriptors),
extends(vulture)).
flight_profile(flat).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(canada_goose,
imports(descriptors),
extends(goose)).
head(black).
cheek(white).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(cliff_swallow,
imports(descriptors),
extends(swallow)).
tail(square).
:- end_object.

View File

@@ -0,0 +1,56 @@
:- category(descriptors).
:- info([
authors is 'Paulo Moura',
version is 1.0,
date is 2000/2/18,
comment is 'Bird descriptors predicates.',
source is 'Example adopted from an Amzi! Inc Prolog book.']).
:- public([
bill/1,
cheek/1,
color/1,
eats/1,
family/1,
feed/1,
feet/1,
flight/1,
flight_profile/1,
head/1,
live/1,
neck/1,
nostrils/1,
order/1,
size/1,
tail/1,
throat/1,
voice/1,
wings/1]).
:- public(descriptor/1).
descriptor(bill/1).
descriptor(cheek/1).
descriptor(color/1).
descriptor(eats/1).
descriptor(feed/1).
descriptor(feet/1).
descriptor(flight/1).
descriptor(flight_profile/1).
descriptor(head/1).
descriptor(live/1).
descriptor(neck/1).
descriptor(nostrils/1).
descriptor(size/1).
descriptor(tail/1).
descriptor(throat/1).
descriptor(voice/1).
descriptor(wings/1).
:- end_category.

View File

@@ -0,0 +1,14 @@
:- object(duck,
imports(descriptors),
extends(waterfowl)).
family(duck).
feed(on_water_surface).
flight(agile).
:- end_object.

View File

@@ -0,0 +1,166 @@
:- object(expert,
imports(protected::descriptors)).
:- info([
authors is 'Paulo Moura',
version is 1.0,
date is 2000/2/18,
comment is 'Expert system for bird identification.',
source is 'Example adopted from an Amzi! Inc Prolog book.']).
:- public(identify/0).
:- mode(identify, one).
:- info(identify/0,
[comment is 'Starts a bird identification session.']).
:- private(known_/3).
:- dynamic(known_/3).
:- mode(known_(?nonvar, ?nonvar, ?nonvar), zero_or_more).
:- info(known_/3, [
comment is 'Table of already known facts.',
argnames is ['Answer', 'Attribute', 'Value']]).
identify :-
::retractall(known_(_, _, _)),
write('Bird identification expert system'), nl, nl,
forall(
(order::leaf(Bird), check(Bird)),
(nl, write('Possible identification : '), write(Bird), nl)),
nl, write('No (more) candidates found.').
check(Bird) :-
forall(
(::descriptor(Functor/Arity),
functor(Predicate, Functor, Arity),
Bird::Predicate),
call(Predicate)).
bill(X):-
ask(bill, X).
cheek(X):-
ask(cheek, X).
color(X):-
ask(color, X).
eats(X):-
ask(eats, X).
feed(X):-
ask(feed,X).
feet(X):-
ask(feet, X).
flight(X):-
menuask(flight, X, [ponderous, powerful, agile, flap_glide, other]).
flight_profile(X):-
menuask(flight_profile, X, [flat, v_shaped, other]).
head(X):-
ask(head,X).
live(X) :-
ask(live, X).
neck(X):-
ask(neck, X).
nostrils(X):-
ask(nostrils, X).
size(X):-
menuask(size, X, [large, plump, medium, small]).
tail(X):-
menuask(tail, X, [narrow_at_tip, forked, long_rusty, square, other]).
throat(X):-
ask(throat, X).
voice(X):-
ask(voice,X).
wings(X):-
ask(wings, X).
ask(Attribute,Value):-
::known_(yes, Attribute, Value),
!.
ask(Attribute,Value):-
::known_(_, Attribute, Value),
!, fail.
ask(Attribute,_):-
::known_(yes, Attribute, _),
!, fail.
ask(Attribute, Value):-
write(Attribute:Value),
write('? (yes or no): '),
read(Answer),
::asserta(known_(Answer, Attribute, Value)),
Answer = yes.
menuask(Attribute,Value, _):-
::known_(yes, Attribute, Value),
!.
menuask(Attribute, _, _):-
::known_(yes, Attribute, _),
!, fail.
menuask(Attribute, AskValue, Menu):-
nl, write('What is the value for '), write(Attribute), write('?'), nl,
display_menu(Menu),
write('Enter the number of choice> '),
read(Num),nl,
pick_menu(Num, AnswerValue, Menu),
::asserta(known_(yes,Attribute,AnswerValue)),
AskValue = AnswerValue.
display_menu(Menu):-
display_menu(Menu, 1).
display_menu([], _).
display_menu([Item| Rest], N):-
write(N), write(' : '), write(Item), nl,
NN is N + 1,
display_menu(Rest, NN).
pick_menu(N, Val, Menu):-
integer(N),
pic_menu(1, N, Val, Menu), !.
pick_menu(Val, Val, _).
pic_menu(_, _, none_of_the_above, []).
pic_menu(N, N, Item, [Item| _]).
pic_menu(Ctr, N, Val, [_| Rest]):-
NextCtr is Ctr + 1,
pic_menu(NextCtr, N, Val, Rest).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(falcon,
imports(descriptors),
extends(falconiforms)).
family(falcon).
wings(long_pointed).
head(large).
tail(narrow_at_tip).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(falconiforms,
imports(descriptors),
extends(order)).
order(falconiforms).
eats(meat).
feet(curved_talons).
bill(sharp_hooked).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(female_mallard,
imports(descriptors),
extends(duck)).
voice(quack).
color(mottled_brown).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(flycatcher,
imports(descriptors),
extends(passerformes)).
family(flycatcher).
bill(flat).
eats(flying_insects).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(fulmar,
imports(descriptors),
extends(tubenose)).
size(medium).
flight(flap_glide).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(goose,
imports(descriptors),
extends(waterfowl)).
family(goose).
size(plump).
flight(powerful).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(great_crested_flycatcher,
imports(descriptors),
extends(flycatcher)).
tail(long_rusty).
:- end_object.

View File

@@ -0,0 +1,11 @@
:- object(laysan_albatross,
imports(descriptors),
extends(albatross)).
color(white).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(male_mallard,
imports(descriptors),
extends(duck)).
voice(quack).
head(green).
:- end_object.

View File

@@ -0,0 +1,6 @@
:- object(order,
imports(descriptors, proto_hierarchy)).
:- end_object.

View File

@@ -0,0 +1,12 @@
:- object(passerformes,
imports(descriptors),
extends(order)).
order(passerformes).
feet(one_long_backward_toe).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(peregrine_falcon,
imports(descriptors),
extends(falcon)).
eats(birds).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(pintail,
imports(descriptors),
extends(duck)).
voice(short_whistle).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(purple_martin,
imports(descriptors),
extends(swallow)).
color(dark).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(snow_goose,
imports(descriptors),
extends(goose)).
color(white).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(sparrow_hawk,
imports(descriptors),
extends(falcon)).
eats(insects).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(swallow,
imports(descriptors),
extends(passerformes)).
family(swallow).
wings(long_pointed).
tail(forked).
bill(short).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(swan,
imports(descriptors),
extends(waterfowl)).
family(swan).
neck(long).
color(white).
flight(ponderous).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(trumpeter_swan,
imports(descriptors),
extends(swan)).
voice(loud_trumpeting).
:- end_object.

View File

@@ -0,0 +1,16 @@
:- object(tubenose,
imports(descriptors),
extends(order)).
order(tubenose).
nostrils(external_tubular).
live(at_sea).
bill(hooked).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(turkey_vulture,
imports(descriptors),
extends(vulture)).
flight_profile(v_shaped).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(vulture,
imports(descriptors),
extends(falconiforms)).
family(vulture).
feed(scavange).
wings(broad).
:- end_object.

View File

@@ -0,0 +1,14 @@
:- object(waterfowl,
imports(descriptors),
extends(order)).
order(waterfowl).
feet(webbed).
bill(flat).
:- end_object.

View File

@@ -0,0 +1,10 @@
:- object(whistling_swan,
imports(descriptors),
extends(swan)).
voice(muffled_musical_whistle).
:- end_object.