1.1.10. Bazы dannыx i znaniy na prologe. Faktы, opisannыye v razdele clauses, mojno rassmatrivat, kak staticheskuyu bazu dannыx (BD). Eti faktы yavlyayutsya chastyu koda programmы i ne mogut bыt operativno izmenenы. Dlya sozdaniya dinamicheskoy bazы dannыx v PROLOGe predusmotren spesialnыy razdel database.
Predikatы v etom razdele mogut imet takuyu je formu predstavleniya, chto i v staticheskoy chasti PROLOG-programmы, no doljnы imet drugoye imya.
Rassmotrim prostoy primer.
Programma 20
DOMAINS
name = symbol
rost, ves = integer
DATABASE
dplayer(name, rost, ves)
PREDICATES
player(name, rost, ves)
CLAUSES
player(“Mixaylov”, 180, 87)
player(“Petrov”, 187, 93)
player(“Xarlamov”, 177, 80)
Dopustim, chto neobxodimo posle zapuska programmы peremestit dannыye iz staticheskoy BD v dinamicheskuyu. Dlya etogo mojno opisat sleduyuщyeye pravilo:
assert_database:-player(N, R, V), assertz(dplayer(N, R, V)),fail.
V etom pravile ispolzovan vstroyennыy predikat assertz, kotorыy pomeщayet utverjdeniye v bazu dannыx posle vsex utverjdeniy, kotorыye tam uje imeyutsya. Yest takje vstroyennыye predikatы dlya udaleniya utverjdeniy (retract), schitыvaniya s diska (consult), zapisi BD na disk (save) i sbora dannыx iz BD v spisok (findall).
Glavnoye dostoinstvo BD na PROLOGe, kak i lyuboy drugoy BD, zaklyuchayetsya v vozmojnosti bыstrogo vыborochnogo dostupa k informasii.
Naprimer, v privedennom vыshe primere eto mojet bыt zapros:
GOAL: player(N, R, V),R>180
Yesli sravnivat osnovnыye ponyatiya PROLOGa i relyasionnыx BD, to poluchitsya sleduyuщaya tabl.4.
Tablisa 4
PROLOG
|
Relyasionnыye BD
|
Predikat
|
Otnosheniye (tablisa)
|
Obyekt
|
Atribut otnosheniya
|
Otdelnoye utverjdeniye
|
Elementы otnosheniya (zapis)
|
Kolichestvo utverjdeniy
|
Moщnost otnosheniya
|
Takim obrazom, mojno skazat, chto lyubaya PROLOG-programma mojet bыt nazvana bazoy dannыx.
Kakovo je glavnoye otlichiye PROLOG-programm ot relyasionnыx BD? V BD mojno tolko izvlekat suщyestvuyuщiye svedeniya ili izmenyat dannыye po zadannomu zakonu. V PROLOG-programme za schet ispolzovaniya pravil i logicheskogo vыvoda mojno poluchat novыye znaniya. Poetomu PROLOG-programma mojet rassmatrivatsya v kachestve bazы znaniy (ekspertnoy sistemы).
Rassmotrim primer prostoy ekspertnoy sistemы, kotoraya reshayet zadachu opredeleniya vida ekzemplyara poymannoy rыbы.
Programma 21
DATABASE
xpositive(symbol, symbol)
xnegative(symbol, symbol)
PREDICATES
expertiza
vopros(symbol, symbol)
fish_is(symbol)
positive(symbol, symbol)
negative(symbol, symbol)
remember(symbol, symbol, symbol)
clear_facts
GOAL
expertiza.
CLAUSES
expertiza:-fish_is(X),!,nl,write(“vasha rыba eto “,X,” “),nl, clear_facts.
expertiza:-nl, write(“eto neizvestnaya rыba!”),clear_facts.
vopros(X, Y):-write(“vopros – “,X, “ “,Y, “? (da/net)”),readln(R),
remember(X,Y,R).
positive(X,Y):- xpositive(X,Y),!.
positive(X,Y):-not(negative(X,Y)),!,vopros(X,Y).
negative(X,Y):-xnegative(X,Y),!.
remember(X,Y,”da”):-assertz(xpositive(X,Y)).
remember(X,Y,”net”):-assertz(xnegative(X,Y)),fail.
clear_facts:-retract(xpositive(_,_)),fail.
clear_facts:-retract(xnegative(_,_)),fail.
fish_is(“som”):- positive(“u rыbы”,”ves >40 kg”).
fish_is(“som”):- positive(“u rыbы”,”ves <40 kg”),
positive(“u rыbы”,”yest usы”).
fish_is(“щuka”):- positive(“u rыbы”,“ves <20 kg”),
positive(“u rыbы”,”dlinnoye uzkoye telo”).
fish_is(“okun”):- positive(“u rыbы”,”ves <20 kg”),
positive(“u rыbы”,”shirokoye telo”),
positive(“u rыbы”,”temnыye polosы”).
fish_is(“plotva”):- positive(“u rыbы”,“ves <20 kg”),
positive(“u rыbы”,”shirokoye telo”),
positive(“u rыbы”,”serebristaya cheshuya”).
Netrudno videt, chto Programma 21 realizuyet zadannoye derevo poiska resheniya. Otvetы na zadannыye voprosы pozvolyayut prodvigatsya po vetvyam etogo dereva k odnomu iz variantov resheniya.
20>20>20>40>
Do'stlaringiz bilan baham: |