Page 1
CS 6033
Logic Programming
Budditha HettigeDepartment of Computer Science
Faculty of ComputingGeneral Sir John Kotelawala Defence University
SOURCE : Building Expert Systems in Prolog, Dennis MerrittURL: http://www.inf.fu-berlin.de/lehre/SS09/KI/folien/merritt.pdf
Build up your own Expert System using PROLOG
Page 2
Overview
• What is an Expert System?
• What are the Expert system features?
• Steps to Expert System development
– Knowledgebase
– User Interface
– Shell
• Prolog Java Interface (JPL)
• Prolog Web Interface
• Examples
27/11/2015 Budditha Hettige ([email protected] )
Page 3
Expert System
• Computer applications which embody some non-
algorithmic expertise for solving certain types of
problems
7/11/2015 Budditha Hettige ([email protected] ) 3
{Dennis Merritt}
Page 4
Expert System Features
• Goal driven reasoning or backward chaining
• Coping with uncertainty
• Data driven reasoning or forward chaining
• Data representation
• User interface
• Explanations
7/11/2015 Budditha Hettige ([email protected] ) 4
Page 5
Steps to
Expert System Development
• Identify the problem
• Build up Knowledge base
– Fats
– Rules
– Expirations
• Build up Inference Engine
– Way to sole problem
• Design a user Interface
7/11/2015 Budditha Hettige ([email protected] ) 5
Page 6
Identify the problem
• Input
– What are the Questions?
– What is the order of the Questions?
• Output
– What is the final result(s)?
• Facts
– What are the fats requires to collect the answers?
• Explanations
– In case, what are the explanations?
7/11/2015 Budditha Hettige ([email protected] ) 6
Page 7
Build up Knowledgebase
• What are the facts?
• What are the rules?
• How they represents?
• Example
family(duck):-
order(waterfowl), feed(on_water_surface), flight(agile).
bird(mallard):- family(duck), voice(quack), head(green).
bird(mallard):- family(duck), voice(quack), color(mottled_brown).
bird(pintail):- family(duck), voice(short_whistle).
7/11/2015 Budditha Hettige ([email protected] ) 7
Page 8
Build up Inference Engine
• Prolog has a built-in backward chaining inference engine that can be used to partially implement some expert systems
• The rules for expert systems are usually written in the form:
– bird(pintail):- family(duck), voice(short_whistle).
7/11/2015 Budditha Hettige ([email protected] ) 8
Page 9
User Interface
• The system can be dramatically improved by
providing a user interface
• Identify Attribute and values for the each case
7/11/2015 Budditha Hettige ([email protected] ) 9
Page 10
Ask Questions
7/11/2015 Budditha Hettige ([email protected] ) 10
Page 11
Menu for a user
• Example:
7/11/2015 Budditha Hettige ([email protected] ) 11
Page 12
Remembering the answer
• Example:
7/11/2015 Budditha Hettige ([email protected] ) 12
Page 13
Expert System Shell
• Architecture of the Native shell.
7/11/2015 Budditha Hettige ([email protected] ) 13
Page 14
The Shell
• The shell will seek to solve a generic predicate called
top_goal.
top_goal(X) :- bird(X).
• The shell has a predicate called solve, which does some
housekeeping and then solves for the top_goal.
• Example
7/11/2015 Budditha Hettige ([email protected] ) 14
Page 15
Explanation
• Expert systems have ability to explain theirbehavior.
• This means the user can ask the system for justification of conclusions or questions at any point in a consultation with an expert system
• Types of Explanation
– A rule trace, which reports on the progress of a consultation;
– Explanation of how the system reached a given conclusion;
– Explanation of why the system is asking a question;
– Explanation of why not a given conclusion.
7/11/2015 Budditha Hettige ([email protected] ) 15
Page 16
Explanation (Questions)
The why questions occur at the bottom of an inference
chain, and the how questions occur at the top
7/11/2015 Budditha Hettige ([email protected] ) 16
Page 17
Java + Prolog Interface
7/11/2015 Budditha Hettige ([email protected] ) 17
Page 18
Java Prolog Interface
• SET PATH for the swipl.exe
– C:\Program Files (x86)\swipl\bin
• Add jpl.jar to your project
• Create suitable prolog source file
• Add prolog source file into your java project folder
• Import jpl and complete your development
7/11/2015 Budditha Hettige ([email protected] ) 18
Page 19
Java Codes for Prolog connection
• Import JPL libraryimport org.jpl7.JPL;
import org.jpl7.Query;
import org.jpl7.Term;
• Collect JPL information
7/11/2015 Budditha Hettige ([email protected] ) 19
Page 20
JPL Code
• Consult prolog sourceString t1 = "consult('family.pl')";System.out.println(t1 + " " + (Query.hasSolution(t1) ? "succ" : "failed"));
• Run Prolog predicate String t2 = "child_of(joe, ralf)";
System.out.println(t2 + " is " + (Query.hasSolution(t2) ? "pro" : "not"));
• Get one solutionString t4 = "descendent_of(X, ralf)";
System.out.println("first solution of " + t4 + ": X = " +
Query.oneSolution(t4).get("X"));
7/11/2015 Budditha Hettige ([email protected] ) 20
Page 21
JPL Code (2)
String t4 = "descendent_of(X, ralf)";
Map<String,Term>[] ss4 =
Query.allSolutions(t4);
System.out.println("all solutions of " + t4);
for (int i = 0; i < ss4.length; i++)
{
System.out.println("X = " + ss4[i].get("X"));
}
7/11/2015 Budditha Hettige ([email protected] ) 21
Page 22
JPL Code (3)
String t5 = "descendent_of(X,Y)";
Query q5 = new Query(t5);
System.out.println("each solution of " + t5);
while (q5.hasMoreSolutions())
{
Map<String, Term> s5 = q5.nextSolution();
System.out.println("X = " + s5.get("X") + ",
Y = " + s5.get("Y"));
}
7/11/2015 Budditha Hettige ([email protected] ) 22
Page 23
Example (relation.pl)
male(somapala).
male(gunapala).
male(rathnapala).
female(seela).
female(susila).
female(neetha).
parent(seela,kamal).
parent(seela,nayana).
parent(seela,saman).
parent(somapala,kamal).
parent(somapala,nayana).
parent(somapala,saman).
parent(kamal,sanjaya).
parent(ruvani,sanjaya).
parent(saman,susi).
parent(saman,yasith).
parent(saman,oshan).
parent(kumari,susi).
parent(kumari,yasith).
parent(kumari,oshan).
print:-write('how are you').
print(X):- write('Anwer is '),write(X),nl.
print(X,Y):- write('Anwer is '),write(X), write(Y),nl.
mother(X,Y):-female(X),parent(X,Y).
father(X,Y):-male(X),parent(X,Y).
son(X,Y):- male(X),parent(Y,X).
whois(X, Y) :- mother(X,Y), write(X), write(' is mother of'), write(Y).
whois(X, Y) :- father(X,Y), write(X), write(' is father of'), write(Y).
whois(X, Y) :-
son(X,Y), write(X), write(' is son of'), write(Y).
7/11/2015 Budditha Hettige ([email protected] ) 23
Page 24
Example:
7/11/2015 Budditha Hettige ([email protected] ) 24