0% found this document useful (0 votes)
86 views9 pages

Dynamic Database Management in Prolog

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views9 pages

Dynamic Database Management in Prolog

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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("[Link]"),
nl,write("Saved Successfully....!!!!"),nl.
rule(1).
rule(2):-
nl,write("Enter Name:-"),
readln(Name),
retract(student(Name,_,_,_)),nl,
save("[Link]"),
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.

Common questions

Powered by AI

Dynamic databases in Prolog significantly enhance a student information management system by allowing real-time updates and maintenance of student records. As students enroll or update their information, predicates such as 'asserta', 'assertz', and 'retract' can be used to modify records without restarting the program. File-based persistence with 'save' and 'consult' ensures continuity and data sharing across sessions, facilitating extensive analyses and management tasks. This dynamic nature supports responsive and adaptive user interfaces, making it possible to conduct various operations like searching, filtering, and displaying up-to-date student information efficiently .

Potential errors when using 'retract' in Prolog could include accidental deletion of unintended records due to ambiguous patterns, especially if wildcards or incomplete record specifications are used. These can lead to incomplete data or loss of important information. Mitigation strategies include using complete and specific patterns in 'retract' to ensure precise deletion, validating input to confirm correct entries, and possibly implementing confirmation dialogs or logs to track changes before committing deletions in the dynamic database. Such precautions help maintain data integrity during database operations .

Static databases in Prolog are compiled as part of the program and remain unchanged during execution, whereas dynamic databases can be altered at runtime. Dynamic databases can either be temporary, existing only during the program's execution, known as working memory, or be file-based and persistent. File-based dynamic databases are managed using system-defined predicates such as 'save' and 'consult', allowing data to be stored and retrieved across different program runs. Additionally, predicates like 'asserta', 'assertz', and 'retract' are used to add or remove facts dynamically from these databases during execution .

The use of input/output predicates in managing a dynamic database of student records in Prolog significantly enhances user experience by providing a clear and interactive interface for data manipulation. Input predicates like 'readln' and 'readint' facilitate data entry by guiding users through required fields, while output predicates like 'write' and 'nl' offer immediate feedback and formatted outputs, ensuring clarity and easy interpretation of data. This direct interaction encourages user engagement and reliance on the program for managing complicated datasets efficiently, enhancing overall system usability .

In Prolog, creating custom predicates for displaying specific student details and filtering by percentage helps in organizing and presenting data in a meaningful way. Predicates like the one used to display specific student details accept parameters to retrieve data matching given criteria, such as name, and output formatted information about the student. Similarly, predicates for filtering by percentage compare student records against a specified threshold, outputting results that meet the condition, thus enabling targeted data retrieval and reporting .

The 'asserta' predicate in Prolog is used to add a new clause at the beginning of a dynamic database of facts. This operation is crucial for managing dynamic data, as it allows for the insertion of new information that can be acted upon immediately during program execution. For example, using 'asserta(student(Name, Sem, Branch, Per))' adds a student record to the start of the student database, ensuring it is considered first in subsequent queries .

The 'retract' predicate in Prolog is used to remove clauses from a dynamic database. It matches clauses whose head is specified, allowing for precise control over which data is deleted. For example, 'retract(student(Name, _, _, _))' will remove the first occurrence of any student data with the specified name, effectively managing the dynamic aspects of data during execution. This ability to dynamically manage data ensures the database remains accurate and relevant throughout the life of the program .

The Prolog program employs a menu-based strategy for user interaction and command execution. It presents users with options, such as entering new student details or displaying specific records, and reads user choices with 'readint' and 'readln' predicates. Based on user input, it calls corresponding predicates using a 'rule' clause to execute specific actions, with each option set to handle tasks like data insertion and display. This structured approach simplifies program flow and enhances usability by dynamically responding to user commands .

The 'save' and 'consult' predicates in Prolog play a crucial role in managing file-based dynamic databases. 'Save' allows the current state of a database to be stored in a file, ensuring data persistence across different program runs. Meanwhile, 'consult' retrieves and includes clauses from specified files into the current execution context, making it easier to reuse data and share across programs. These operations facilitate the use of dynamic databases beyond the scope of a single program execution, promoting data consistency and continuity .

Using 'retractall' in Prolog has profound implications for data management as it deletes all instances of database clauses matching a pattern. This operation provides a sweeping method to clear redundant or outdated data, ensuring the database state reflects current realities. For instance, 'retractall(father(_, _))' will remove every 'father' entry, which is useful for completely resetting relationships in genealogical programs. Similarly, 'retractall(student(_, _, _, _))' can clear the student database if needed, such as when initializing for a new academic term, reflecting the database’s adaptive utility .

You might also like