You are on page 1of 8

FLP Finals Part B - TAN JUN WEI (17205304/1)

Question 1

An expert system to identify the possible diseases and provide some advice with suggestions
based on the symptoms provided by the user and the disease verified. The code is shown below.

go :-
hypothesis(Disease),
write('I believe that the patient have '),
write(Disease), nl,
write('TAKE CARE'),
undo.

/*Hypothesis that should be tested*/


hypothesis(cold) :- cold, !.
hypothesis(flu) :- flu, !.
hypothesis(typhoid) :- typhoid, !.
hypothesis(measles) :- measles, !.
hypothesis(malaria) :- malaria, !.
hypothesis(unknown). /* no diagnosis*/

/*Hypothesis Identification Rules*/


cold :-
verify(headache),
verify(runny_nose),
verify(sneezing),
verify(sore_throat),
write('Advices and Sugestions:'), nl,
write('1: Tylenol'), nl,
write('2: Panadol'), nl,
write('3: Nasal spray'), nl,
write('Please wear warm cloths because'), nl.

flu :-
verify(fever),
verify(headache),
verify(chills),
verify(body_ache),
write('Advices and Sugestions:'), nl,
write('1: Tamiflu'), nl,
write('2: Panadol'), nl,
write('3: Zanamivir'), nl,
write('Please take a warm bath and do salt gargling because'), nl.

typhoid :-
verify(headache),
verify(abdominal_pain),
verify(poor_appetite),
verify(fever),
write('Advices and Sugestions:'), nl,
write('1: Chloramphenicol'), nl,
write('2: Amoxicillin'), nl,
FLP Finals Part B - TAN JUN WEI (17205304/1)

write('3: Ciprofloxacin'), nl,


write('4: Azithromycin'), nl,
write('Please do complete bed rest and take soft diet because'), nl.

measles :-
verify(fever),
verify(runny_nose),
verify(rash),
verify(conjunctivitis),
write('Advices and Sugestions:'), nl,
write('1: Tylenol'), nl,
write('2: Aleve'), nl,
write('3: Advil'), nl,
write('4: Vitamin A'), nl,
write('Please get rest and use more liquid because'), nl.

malaria :-
verify(fever),
verify(sweating),
verify(headache),
verify(nausea),
verify(vomiting),
verify(diarrhea),
write('Advices and Sugestions:'), nl,
write('1: Aralen'), nl,
write('2: Qualaquin'), nl,
write('3: Plaquenil'), nl,
write('4: Mefloquine'), nl,
write('Please do not sleep in open air and cover your full skin
because'), nl.

/* how to ask questions */


ask(Question) :-
write('Does the patient have following symptom: '),
write(Question),
write('? '),
read(Response), nl,
( (Response == yes ; Response == y) -> assert(yes(Question)) ;
assert(no(Question)), fail).

:- dynamic yes/1,no/1.

/*How to verify something */


verify(S) :- (yes(S) -> true ; (no(S) -> fail ; ask(S))).

/* undo all yes/no assertions*/


undo :- retract(yes(_)), fail.
undo :- retract(no(_)), fail.
undo.
FLP Finals Part B - TAN JUN WEI (17205304/1)

First, the user will run go and the code below will be executed:

The program will first go through all the hypothesis available in the program:

An example is shown below:

The program will verify the user’s disease by using the verify functor and ask functor to ask the
user some questions:

The dynamic yes/1, no/1 code is included because a user-defined procedure is static by default
which mean we cannot alter the database as the clauses of a status procedure cannot be altered
unless a dynamic/1 directive precedes its definition then only the clauses of a dynamic procedure
can be altered when using assert, asserta or assertz (GNU Prolog, 2018).
FLP Finals Part B - TAN JUN WEI (17205304/1)

This block of codes above asks the user if the user has the symptoms. The user can choose yes or
y to indicate he or she has the symptom. The program will assert the symptoms in the database
for all the symptoms that the user is having and the symptoms that the user is not having, then it
has the ‘fail’ to force the backtracking and then continue asking the other questions. The example
is shown below:

Lastly, the program will retract all the asserted data in the database by using the code below:

References

1. https://www.youtube.com/watch?v=pFdagVD2a-Y
2. https://www.facebook.com/ch.usamanizam/posts/1950403498608080
3. https://www.gprolog.org/manual/html_node/gprolog031.html
FLP Finals Part B - TAN JUN WEI (17205304/1)

Question 2
:- dynamic room/2.

start :-
write('WELCOME TO ROOM BOOKING SYSTEM'), nl,
repeat,
write('Enter a number for option:'), nl,
write('1. Add new booking'), nl,
write('2. Delete booking'), nl,
write('3. End'), nl,
read(Choice), nl, run(Choice), Choice == 3, !.

run(Choice) :-
Choice == 1,
write('Enter room |:'), read(Room),
write('Enter date |:'), read(Date),
(room(Room, Date) -> room_exists; books_room(Room, Date)).

run(Choice) :-
Choice == 2,
lists_rooms,
write('Enter room |:'), read(Room),
write('Enter date |:'), read(Date),
retract(room(Room, Date)),
write('The booking record has been deleted.'), nl, nl, !.

run(Choice) :-
Choice == 3,
write('Thank you').

books_room(Room, Date) :-
assertz(room(Room, Date)),
write('The room has been successfully booked.'), nl, nl.

room_exists :-
write('The room is not available.'), nl, nl.

lists_rooms :-
forall(room(Room, Date),format('room(~w, ~w).~n', [Room,
Date])), nl.
FLP Finals Part B - TAN JUN WEI (17205304/1)

Question 3

This line of code is included because a user-defined procedure is static by default which mean we
cannot alter the database as the clauses of a status procedure cannot be altered unless a
dynamic/1 directive precedes its definition then only the clauses of a dynamic procedure can be
altered when using assert, asserta or assertz (GNU Prolog, 2018).

Then, we will run the prolog using this query above.

The program will print out the welcoming message and we can see we have the ‘repeat’ to repeat
all the below code unless the user selects ‘3’ which is to end the program. The program will then
read the user’s choice and run the program according to the choice made. There is ‘Choice == 3, !.’
on line 10 because we want avoid the program from backtracking as shown below:

So the ‘!’ cut is used to stop backtracking and end the program when the user chooses 3.
FLP Finals Part B - TAN JUN WEI (17205304/1)

Here we have the room booking function when the user chooses ‘1’, it will first check if the
choice made is ‘1’, then if it is, it will prompt the user to enter the room number and enter the
date for the booking. On line 16, we can see that the program will check if the room exists in our
database.

If it exists, it will run the below code:

It will inform the user that the room is not available.

Else if the room is available, it will then run the code below:

The program will assert the room detail to the end of the database using the ‘assertz’ and then
it will the user that the room is booked.

Next, if the user would like to delete the booking, the user will have to input ‘2’ when prompted:

The code above checks if the choice is ‘2’, then it will then run ‘lists_rooms’ to show all the list
of the booked rooms asserted in the database.

As mentioned above, it will find all the room booked in the database using ‘forall’ then using the
format to display out the booked rooms line by line. The first ~w is for the Room and the second
~w is for the Date while ~n is like ‘nl’, going to the next line.
FLP Finals Part B - TAN JUN WEI (17205304/1)

If the program can find the booked room in the database, it will remove the booked room detail
using ‘retract’ and inform the user that the detail of the booked room is removed from the
database and again we have a ‘!’, a cut to stop the backtracking.

Lastly, if the user chooses 3 to end the program. It will first check if the choice made is ‘3’, if it is
then it will display a ‘Thank you’ and end the program.

You might also like