0% found this document useful (0 votes)
114 views

Dynamic Database

Two types of databases are used in Prolog: static and dynamic. Static databases are compiled with the program and do not change during execution, while dynamic databases can change at runtime. Dynamic databases are of two types: those created during each execution in working memory, and those stored in files and consulted using predicates like 'save' and 'consult'. Clauses can be dynamically added and removed from dynamic databases using predicates like 'asserta', 'retract', and 'retractall'. The example program demonstrates storing student records dynamically using these predicates to enter, delete, and search data in a dynamic database file.

Uploaded by

pdhruvil6969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Dynamic Database

Two types of databases are used in Prolog: static and dynamic. Static databases are compiled with the program and do not change during execution, while dynamic databases can change at runtime. Dynamic databases are of two types: those created during each execution in working memory, and those stored in files and consulted using predicates like 'save' and 'consult'. Clauses can be dynamically added and removed from dynamic databases using predicates like 'asserta', 'retract', and 'retractall'. The example program demonstrates storing student records dynamically using these predicates to enter, delete, and search data in a dynamic database file.

Uploaded by

pdhruvil6969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Database handling in Prolog

Two types of databases :- static and dynamic.


● Static database is a part of the program that is complied along with it. It does not change
during execution of the program.
● Dynamic database can change dynamically at execution time and are of two types.
Type1: created at each execution. It grows, shrinks and is deleted at the end of program.
- This type of database is no longer available after a program finishes its execution
and is called working memory.

Type2: Other type of dynamic databases are those which are stored in files and called
database files.
− These are consulted in any program whenever required.
− These types of databases are not part of any particular program and are available for
use in future by different programs using system defined predicates called save and consult.
− While executing a Prolog program one can load database file(s) using 'consult' predicate.
− These files can be updated dynamically at run time and saved using 'save' predicate.

● The format of predicates 'save' and 'consult' are as follows:


− save(filename) - succeeds after saving the contents of a file named 'filename'.
− consult(filename) - succeeds after loading or adding all the clauses from a file
stored in 'filename' in the current program being executed.

● Clauses can be added to a database at run time using following predicates.


- asserta(X) & assertz(X) - succeed by adding fact X in the beginning & at the end of
database of facts respectively.
For example, asserta(father(mike, john)) adds fact father(mike, john) in the
beginning of current database.

● Clauses can be constructed dynamically and asserted in a dynamic database as


follows:
start :-
writeln('Input name of mother: '),
readln(M),
writeln('Input name of child: '),
readln(C),
asserta(parent(M, C)),
asserta(female(M))
● Similarly obsolete clauses can be deleted by using system defined predicate called
retract from dynamic database at the run time.
For example,
retract(father(mike, X)) deletes the first fact father(mike, _) from working
memory.

● retractall(X) deletes all the clauses from a database whose head match with X.
Example:-
retractall(father(X, Y)) deletes all the facts father( _ , _ ) and
retractall( _ ) deletes all the clauses from the working memory.
Using dynamic database in prolog Code:
Aim of program:-
● Store facts of student(name, branch, semester , percentage) dynamically.
● Use asserta predicate to enter new data in a dynamic database.
● Use retract predicate to delete a given data from dynamic db.
● Create appropriate predicates to search and display some specified students
details.
● Create appropriate predicate to list all the students having percentage greater
than some specified value.

domains
name,branch = symbol
sem, percentage = integer
database
student(name,sem,branch, percentage)
predicates
start
rule(integer)
continue(string)
clauses
start:-
clearwindow(),
write("!!!!!....Dynamic Database....!!!!!"),nl,
write("1. Enter new Student Details."),nl,
write("2. Delete the Student Data."),nl,
write("3. Display specific Student Details."),nl,
write("4. List of Students having percentage greater than specified percentage."),nl,
write("5. Exit from the program."),nl,
write("Enter the Choice: "),readint(X),
rule(X),nl,
write("Do you want to continue? "),
readln(C),
continue(C).
rule(1):-
nl,write("Enter Name:-"),
readln(Name),
write("Enter Sem:-"),
readint(Sem),
write("Enter Branch:-"),
readln(Branch),
write("Enter percentage:-"),
readint(Per),
asserta(student(Name,Sem,Branch,Per)),nl,
save("Exp10.txt"),
nl,write("Saved Successfully....!!!!"),nl.
rule(1).
rule(2):-
nl,write("Enter Name:-"),
readln(Name),
retract(student(Name,_,_,_)),nl,
save("Lab10.txt"),
nl,write("Deleted Successfully....!!!!"),nl.
rule(2).
rule(3):-
nl,write("Enter the name:- "),
readln(N),
student(N,B,S,P),
write("Name:- ",N," ,Branch:- ",B," ,Sem:- ",S,"
,Percentage:- ",P ,"%"),nl.
rule(3).
rule(4):-
nl,write("\nEnter Minimum Percentage:- "),
readint(X),
student(N,B,S,P),
X<P,
write("Name:- ",N," ,Branch:- ",B," ,Sem:- ",S,"
,Percentage:- ",P ,"%"),nl,fail.
rule(4).
rule(5):-
nl,write("Exit():w"),nl,
continue(n).
continue(y):-start.
Output :
After Performing above goals.

You might also like