Apply Expert System Using Prolog 2

Posted in LITERATURE on December 15, 2009 by booleanaljabar

-we make the rule first

-then we can try the rule of symptoms. there are sixteen symptoms that make a dissease appear

-CONT’D

-thus, we can decide what the dissease ….

Apply Expert System Using Prolog

Posted in LITERATURE on December 15, 2009 by booleanaljabar

first we must make the method or the rule :

-open prolog program, then choose file

-then, choose consult for compiling the prolog file that we made it before

-the file is filled by method of this program or rule

-after that, you begin to use this program by enter the method identify the symptoms

Posted in LITERATURE on December 8, 2009 by booleanaljabar

LOOPS (summary)

Most conventional programming languages have a looping facility that enables a

set of instructions to be executed repeatedly either a fixed number of times or until

a given condition is met. Prolog has no looping facilities, similar effects can be obtained that enable a sequence of goals to be evaluated repeatedly. This can be done in a variety of

ways, using backtracking, recursion, built-in predicates, or a combination of these.

LOOPING A FIXED NUMBER OF TIMES

In SWI-Prolog a similar effect can be obtained using recursion, as shown in the

example programs below.

Example 1

The following program outputs integers from a specified value down to 1.

loop(0).

loop(N):-N>0,write(‘The value is: ‘),write(N),nl,

M is N-1,loop(M).

And ther result is

?- loop(6).

The value is: 6

The value is: 5

The value is: 4

The value is: 3

The value is: 2

The value is: 1

Yes

Example 2

The next program outputs integers from First to Last inclusive.

/* output integers from First to Last inclusive */

output_values(Last,Last):- write(Last),nl,

write(‘end of example’),nl.

output_values(First,Last):-First=\=Last,write(First),

nl,N is First+1,output_values(N,Last).

?- output_values(5,12).

56789

10

11

12

end of example

yes

Looping Until a Condition Is Satisfied

In SWI- Prolog a similar effect can be obtained in several ways.

Recursion

The first example below shows the use of recursion to read terms entered by the

user from the keyboard and output them to the screen, until end is encountered.

go:-loop(start). /* start is a dummy value used to get

the looping process started.*/

loop(end).

loop(X):-X\=end,write(‘Type end to end’),read(Word),

write(‘Input was ‘),write(Word),nl,loop(Word).

?- go.

Type end to end: university.

Input was university

Type end to end: of.

Input was of

Type end to end: portsmouth.

Input was portsmouth

Type end to end: end.

Input was end

Yes

Using the disjunction operator ;/2 which was defined in Section 4.4 the above

program can be rewritten as a single clause.

loop:-write(‘Type end to end’),read(Word),

write(‘Input was ‘),write(Word),nl,

(Word=end;loop).

The ‘disjunctive goal’ (Word=end;loop) succeeds if variable Word is bound to

the atom end. If not, the system attempts to satisfy the goal loop recursively.

?- loop.

Type end to end: university.

Input was university

Type end to end: of.

Input was of

Type end to end: portsmouth.

Input was portsmouth

Type end to end: end.

Input was end

Yes

This recursive program repeatedly prompts the user to enter a term until either

yes or no is entered.

get_answer(Ans):-write(‘Enter answer to question’),

nl,get_answer2(Ans).

get_answer2(Ans):-

write(‘answer yes or no’),

read(A),

((valid(A),Ans=A,write(‘Answer is ‘),

write(A),nl);get_answer2(Ans)).

valid(yes). valid(no).

?- get_answer(Myanswer).

Enter answer to question

answer yes or no: maybe.

answer yes or no: possibly.

answer yes or no: yes.

Answer is yes

Myanswer = yes

6.2.2 Used ‘repeat’ Predicate


Although it can often be used to great effect, recursion is not always the easiest

way to provide the types of looping required in Prolog programs. Another method

that is often used is based on the built-in predicate repeat.

The name of this predicate is really a misnomer. The goal repeat does not

repeat anything; it merely succeeds whenever it is called. The great value of repeat

is that it also succeeds (as many times as necessary) on backtracking. The effect of

this, as for any other goal succeeding, is to change the order of evaluating goals

from ‘right to left’ (i.e. backtracking) back to ‘left-to-right’. This can be used to

create a looping effect, as shown in the examples below.

This program repeatedly prompts the user to enter a term until either yes or no

is entered. It is an alternative to the recursive program shown at the end of the

previous section. In this case it is debatable whether using repeat is an

improvement on using recursion, but the example is included for purposes of

illustration.

get_answer(Ans):-

write(‘Enter answer to question’),nl,

repeat,write(‘answer yes or no’),read(Ans),

valid(Ans),write(‘Answer is ‘),write(Ans),nl.

valid(yes). valid(no).

The first five goals in the body of get_answer will always succeed. Evaluating

the fifth goal: read(Ans) will prompt the user to enter a term. If the term input is

anything but yes or no, say unsure, the following goal valid(Ans) will fail. Prolog

will then backtrack over read(Ans) and write(‘answer yes or no’), both of which

are unresatisfiable, i.e. will always fail on backtracking.

Backtracking will then reach the predicate repeat and succeed, causing

evaluation to proceed forward (left-to-right) again, with write(‘answer yes or no’)

and read(Ans) both succeeding, followed by a further evaluation of valid(Ans).

Depending on the value of Ans, i.e. the user’s input, the valid(Ans) goal will

either fail, in which case Prolog will backtrack as far as repeat, as before, or it will

succeed in which case the final three goals write(‘Answer is’), write(Ans) and nl

will all succeed. The overall effect is that the two goals write(‘answer yes or no’)

and read(Ans) are called repeatedly until the terminating condition valid(Ans) is

satisfied, effectively creating a loop between repeat and valid(Ans).

?- get_answer(X).

Enter answer to question

answer yes or no: unsure.

answer yes or no: possibly.

answer yes or no: no.

answer is no

X = no

The next program to read the terms of the order of a particular file and output
them into the output stream until the end of the term is now common.

readterms(Infile):-

seeing(S),see(Infile),

repeat,read(X),write(X),nl,X=end,

seen,see(user).

In a similar way to the previous program, this effectively defines a loop

between the goals repeat and X=end.

If file myfile.txt contains the lines

‘first term’. ‘second term’.

‘third term’. ‘fourth term’.

‘fifth term’. ‘sixth term’.

‘seventh term’.

‘eighth term’.

end.

calling readterms will produce the following output

?- readterms(‘myfile.txt’).

first term

second term

third term

fourth term

fifth term

sixth term

seventh term

eighth term

end

yes

This program shows how to implement a menu structure that loops back
repeatedly to ask for more input. Entering the left at the prompt causes Prolog to output
menu from which users can choose one activity at a time until the option d is
selected. Note that all inputs are terms and must be followed by a point
characters.

go:- write(‘This shows how a repeated menu works’),

menu.

menu:-nl,write(‘MENU’),nl,

write(‘a. Activity A’),nl,write(‘b. Activity B’),nl,

write(‘c. Activity C’),nl,write(‘d. End’),nl,

read(Choice),nl,choice(Choice).

choice(a):-write(‘Activity A chosen’),menu.

choice(b):-write(‘Activity B chosen’),menu.

choice(c):-write(‘Activity C chosen’),menu.

choice(d):-write(‘Goodbye!’),nl.

choice(_):-write(‘Please try again!’),menu.

?- go.

This shows how a repeated menu works

MENU

a. Activity A

b. Activity B

c. Activity C

d. End

: b.

Activity B chosen

MENU

a. Activity A

b. Activity B

c. Activity C

d. End

: xxx.

Please try again!

MENU

a. Activity A

b. Activity B

c. Activity C

d. End

: d.

Goodbye!

Yes

6.3 Backtracking with Failure

As the name suggests, the predicate fail always fails, whether the ‘standard’
evaluation of left-to-right or on the decline. Benefits can be drawn from this,
combined with automatic backtracking Prolog, to search through the database for
find all clauses with a particular property.

6.3.1 Searching the Prolog Database

Supposing the database contains clauses such as

dog(fido).

dog(fred).

dog(jonathan).

Each dog clause can be processed in turn using the alldogs predicate defined

below.

alldogs:-dog(X),write(X),write(‘ is a dog’),nl,fail.

alldogs.

Calling alldogs will cause dog(X) to be matched with the dog clauses in the

database. Initially X will be bound to fido and ‘fido is a dog’ will be output. The

final goal in the first clause of the alldogs predicate will then cause evaluation to

fail. Prolog will then backtrack over nl and the two write goals (all of which are

unresatisfiable) until it reaches dog(X). This goal will succeed for a second time

causing X to be bound to fred.

This process will continue until fido, fred and jonathan have all been output,

when evaluation will again fail. This time the call to dog(X) will also fail as there

are no further dog clauses in the database. This will cause the first clause for

alldogs to fail and Prolog to examine the second clause of alldogs. This will

succeed and evaluation will stop.

The effect is to loop through the database finding all possible values of X that

satisfy the goal dog(X).

?- alldogs.

fido is a dog

fred is a dog

jonathan is a dog

yes

Note the importance of the second clause of predicate alldogs. This is to
ensure that, after the database has been searched, objectives successfully. With only
first row, each call to alldogs will eventually fail.

alldogs:-dog(X),write(X),write(‘ is a dog’),nl,fail.

?- alldogs.

fido is a dog

fred is a dog

jonathan is a dog

no

The next program is designed to search a database that contains the clause
represents the name, age, residence and occupation number
people.
If the database contains five clauses

person(john,smith,45,london,doctor).

person(martin,williams,33,birmingham,teacher).

person(henry,smith,26,manchester,plumber).

person(jane,wilson,62,london,teacher).

person(mary,smith,29,glasgow,surveyor).

The names of all the teachers can be found using the allteachers predicate.

allteachers:-person(Forename,Surname,_,_,teacher),

write(Forename),write(‘ ‘),write(Surname),nl,

fail.

allteachers.

The effect of using backtracking with failure in this case is to find all the

teachers in the database.

?- allteachers.

martin williams

jane wilson

yes

If the second clause removed allteachers, teachers will still be found but allteachers evaluation will end in failure. This is a bit atautidak important when goals are included in the system prompt, but if allteachers itudigunakan as a destination in the body’s rules would certainly be desirable to ensure successful bahwaselalu.

Please note that not always need to use ‘backtracking dengankegagalan’ to search the database. For example, the predicate somepeople / 0 definedbelow will find all the people in the database given earlier, down to williams, using only standard backtracking.

somepeople:-person(Forename,Surname,_,_,_),

write(Forename),write(‘ ‘),write(Surname),nl,

Surname=williams.

somepeople.

The goal Surname=williams succeeds if the variable Surname is bound to

williams. If not, it fails. The effect is to search the database down to and including

the person clause with second argument williams.

?- somepeople.

john smith

martin williams

yes

6.3.2 Finding Multiple Solutions

Backtracking with failure can also be used to find all the ways of satisfying a goal.

Suppose that a predicate findroute(Town1,Town2,Route) finds a route Route

between two towns Town1 and Town2. The details of this predicate are irrelevant

here. It may be assumed that Town1 and Town2 are atoms and that Route is a list.

Backtracking with failure can then be used to find all possible routes between

Town1 and Town2 and write out each one on a separate line, as follows:

find_all_routes(Town1,Town2):-

findroute(Town1,Town2,Route),

write(‘Possible route: ‘),write(Route),nl,fail.

find_all_routes(_,_).

LOOPING (Part 3) – Profesion Greater 40

Posted in TUTORIAL with tags , on December 8, 2009 by booleanaljabar

The third program of looping is to define the profesion of person who older than 40 years. Here the rules :

      person(john,smith,45,london,doctor).
person(martin,williams,33,birmingham,teacher).
person(henry,smith,26,manchester,teacher).
person(mary,smith,29,london,teacher).
person(jane,wilson,62,glassgow,surveyor).

profesion:-person(Forename,Surname,Old,_,As),Old>40,
write (Forename),write(‘ ‘), write(Surname), (‘is a ’),write(As),nl,fail.
profesion.

After you write profesion on Prolog, the program will be end by displaying the result as below :

1. ?-profesion.
john smith is a doctor
jane Wilson is a surveyor

NB : Will be coming soon our Final Project about Expert System realization and all source code of our PROLOG

LOOPING (Part 2) – Start and Stop Character

Posted in TUTORIAL with tags , , on December 8, 2009 by booleanaljabar

For the second problem of looping using PROLOG is define and test predicate to read character of user input and stop its process before the first new line or character ?.

The rules of this problem is :

  •  repeat is predicate that loop the process inside the program.
  • get0(Word) is predicate to read the word that input by user and convert it to ASCII numerical.
  • stop(Word) is predicate to stop the process.
  • stop(13) is indicated that the program will be end when user entering the blank character that also mention as firs new line.
  • stop(63) is indicated that the program will be end when user entering the ? character that has 63 of ASCII.
  • put(Word) is used to display again the character that entered by user after converted into ASCII.
  • Fail is predicate that make the program after reach certain condition.

    Test the rules to your SWI Prolog. Write go after you consult the rules file.

LOOPING (Part 1) – Find squares of integer using PROLOG

Posted in TUTORIAL with tags , on December 8, 2009 by booleanaljabar

I think you have familiar with looping using Java Programming, but now, it will be wonderful when you know the other programming language using pure logic, namely PROLOG. It can be applied to loop operation as in Java.

For the first application of looping using PROLOG, we can find the square of first number to the last number inclusive we enter.

Write the rule like below and save as firstLast.pl

  • output_values(First, Last) :- First>Last used to continue the program when First number is greater than Last number.
  • When the first number reach the same number as Last number we’ve input, the process will be end by displaying the result of last square number. It is show on the second rules above.
  • But if the square is less than last number we’ve input, so the process will be continue. The process is product first number with first number and put the result on N variable. Then, the first number before, will be add by 1 to find the next square number until the first number (that put on M variable) same as last number. (Show on the last rule).

    Now, open your prolog and consult firstLast.pl
    Test the program use 6 as First number, and 12 as Last number. It will find the square number of 6 until 12 (the square of 6,7,8,9,10,11,12).

INPUT and OUTPUT (combine)

Posted in TUTORIAL on November 27, 2009 by booleanaljabar

This study aims to study the program that serves to combine the data with each other

Firstly, we make first file shape .txt or notepad after the contents of the file like the example below.

secondly, set back the same data but with the name and contents of different files like the example below

The third step, open the SWI-Prolog program , then select the menu “file” on the toolbar -> click “new”
The fourth step below write a program like this

combine(In1,In2,Out):-
seeing(S),telling(T),
tell(Out),see(In1),copyfile,seen,
see(In2),copyfile,seen,see(S),
write(end),nl,told,telling(T).
copyfile:-read(N),process(N).
process(end).
process(N):-write(N),nl,copyfile.

and then save it

The fifth step, select “file” on the toolbar and then select “consult” the program was
if successful will appear like this syntac

then type the following program like this

?- combine(‘in1.txt’,’in2.txt’,’out.txt’).

and if all is successful, then the output will be formed as below

that’s all for this time and thank you for your attention

INPUT and OUTPUT (readfile)

Posted in TUTORIAL on November 24, 2009 by booleanaljabar

This program will display the contents of tesla.txt into the conversion of character to ASCII numerics.

First, create tesla.txt

And then, create new rules on prolog :

readfile(F):-
seeing(S),see(F),
readchar,readchar,readchar,
readchar,readchar,readchar,
readchar,readchar,readchar,
readchar,readchar,readchar,
readchar,readchar,readchar,
seen,see(S).
readchar:-get0(X),write(X),nl.

 

Save it.

Third, counsult the code above…

Write readfile(‘tesla.txt’).

The result is :

INPUT and OUTPUT (copyterms)

Posted in TUTORIAL on November 24, 2009 by booleanaljabar

This tutorial will show you how to copy any term from infile to outfilefile and then display the contents.

1. Create infile.txt

2. Save infile.txt, and open your prolog and create new rule as belows:

This program will call and read infile.txt and copy the contents into outfile.txt (you have to create it first too) and display the contents. So the result will be shown if you write copyterms(‘infile.txt’,’outfile.txt’). on you rSWI  Prolog.

INPUT and OUTPUT (makelower)

Posted in TUTORIAL with tags , on November 24, 2009 by booleanaljabar

Today, we will learn about how to make lower by convert upper case to lower case using prolog.

1. First, absolutely you have to open your SWI Prolog and create new rules as below :

makelower:-get0(Big),process(Big).
process(13).
case(Big,Small):-Big>64,Big<91,Small is Big+32.
case(Big,Small):-Small is Big+0.
process(Big):-Big=\=13,case(Big,Small),put(Small),makelower.

Notes :

get0(Big) : used to convert input character to ASCII numeric.

case(Small,Big) : used to check whether Big is between 63 – 95 (in ASCII is alphabet A-Z). If yes, it will be convert uppercase to lowercase by adding ASCII numeric of the character with 32 (in ASCII is alphabet a-z).

process(13) is used to include blank character in the process.

2. Secondly, consult it on your SWI Prolog.

After that, you can test the program by write makelower

Finally, the result will be shown as follows: