Professional Documents
Culture Documents
Lab Manual
ISM 383
Knowledge-Based
Systems
Department of Information systems
College of Computer Science
King Khalid University
Course Coordinator
ISM 383
Course Name Course Code
Knowledge-Based Systems - LAB ISM 383
Lab Syllabus with Work Plan
Lab No. Topics to be covered
LAB 1 Overview and Introduction of CLIPS Software
How to define a variable?
How to check list of variable?
How to delete any specific variable?
How to reset CLIPS memory?
LAB 2 Defining the Rules
• How to define a rule and assign value to it?
LAB 3 Defining the Rules
• Defining the rules for matching things
LAB 4 Use of NOT operator in CLIPS by defining single condition
Use of OR operator by defining multiple conditions
Use of AND operator by defining multiple conditions
LAB 5 Use of AND / OR together by defining at least 3 or more conditions
LAB 6 Use of test operation in CLIPS
• How to find Less from 2 given numbers
• How to find Less from more than 2 numbers
• How to find Greater from 2 given numbers
• How to find Greater from more than 2 numbers
LAB 7 Representation of Mathematical expressions in CLIPS
• Use of mathematical operations on simple string
• Use of mathematical operations on complex string
LAB 8 Working with strings
• Functions for operations on strings
LAB 9 Combination of mathematical and logical operations
LAB 10 Assigning multiple values through multi valued wildcard
Extracting the multiple values on different conditions
LAB 11 Forward chain algorithm, Backward chain algorithm
LAB 12 Defining a template in CLIPS in shape of a form
Assigning multiple value using single command
Retrieving the assigned data from template
LAB 13&14 Expert System Development
LAB 15 Final LAB Exam
Software(s)/Tools Used
CLIPS SOFTWARE
References
Lecture notes
http://users.csc.calpoly.edu/~fkurfess/481/W02/Lab
CLIPS is an expert system tool originally developed by the Software Technology Branch
(STB), NASA/Lyndon B. Johnson Space Center. Since its first release in 1986, CLIPS has
undergone continual refinement and improvement. It is now used by thousands of people
around the world. CLIPS is designed to facilitate the development of software to model
human knowledge or expertise. There are three ways to represent knowledge in CLIPS:
• Rules, which are primarily intended for heuristic knowledge based on experience.
• Deffunctions and generic functions, which are primarily intended for procedural
knowledge. vi —
• Object-oriented programming, also primarily intended for procedural knowledge. The
five generally accepted features of object-oriented programming are supported: classes,
message-handlers, abstraction, encapsulation, inheritance, polymorphism. Rules may
pattern match on objects and facts.
CLIPS is a type of computer language designed for writing applications called expert
systems. An expert system is a program which is specifically intended to model human
expertise or knowledge. In contrast, common programs such as payroll programs, word
processors, spreadsheets, computer games, and so forth, are not intended to embody
human expertise or knowledge. (One definition of an expert is someone more than 50 miles
from home and carrying a briefcase.) CLIPS is called an expert system tool because it is a
complete environment for developing expert systems which includes features such as an
integrated editor and a debugging tool. The word shell is reserved for that portion of CLIPS
which performs
inferences or reasoning. The CLIPS shell provides the basic elements of an expert system: 1.
fact-list, and instance-list: Global memory for data 2. knowledge-base: Contains all the
rules, the rule-base 3. inference engine: Controls overall execution of rules A program
written in CLIPS may consist of rules, facts, and objects. The inference engine decides
which rules should be executed and when. A rule-based expert system written in CLIPS is a
data-driven program where the facts, and objects if desired, are the data that stimulate
execution via the inference engine.
LAB 1
Objective:
Understanding the CLIPS environment and learn to use basic commands and structure in
CLIPS
Activities:
Activity 1: Open CLIPS and run the basic commands
Activity 2: Insert a fact/variable in CLIPS
Activity 3:How to display variables in CLIPS?
Activity 4: How to delete a variable in CLIPS?
Learning outcome:
Get basic overview of CLIPS environment
CLIPS>
(exit)
(clear)
Removes all program and data from memory. Equivalent to shutting down and restarting
CLIPS.
(reset)
(run)
This command will display the saved variable in clips with respective positions.
E.g: (retract 3)
Exercise 1:
Enter at least 5 different names with the using the assert command. E.g: (assert
(name tariq))
Exercise2:
Retract the variable enter at position number 3 and 5, as entered in previous
exercise.
Note: CLIPS is case sensitive; so you should use the command in the same case as
explained.
LAB 2
Objective:
Learn to define rules in CLIPS and get basic understanding of rule execution.
Understanding the role of antecedents and consequences in rule.
Activities:
Activity 1: Assert some facts. Define a rule in CLIPS. The rule should check the condition
and if the condition is true, it should assert a fact in the memory.
Learning outcome:
Understanding the role of CLIPS rules in knowledge-based system.
You can check using (facts) command to make sure that variable entered correctly or not.
Now enter the following rule
(defrule duck
(animal duck)
=>
(assert (sound-is quack)))
Explanation:
The first part, (defrule duck, simply gives the rule a unique name.
The second part, (animal duck), is the pattern (condition part) of the rule
Last part, (assert (sound-is quack)), is the action (the action part).
This rule will check that if a fact (animal duck) is defined then it will assign the sound
quack to the animal duck.
If no fact is defined then sound will not be allocated and you will not see any changes.
Screenshot:
Exercise 1:
Assert a Course 383 and Define a rule to assign teacher to the course 383. Assign the
teacher name Raja by executing the same rule
Exercise 2:
Assert some courses including "Course 383" and assign the following using a rule:
LAB 3
Objective:
Understanding pattern matching in an expert system. pattern can be matched against a
pattern entity. A pattern entity is either a fact or an instance of a user-defined class.
Activities:
Activity 1: Assert some facts. Define a rule in CLIPS. The rule should mach the pattern of
existing fact and display <variable name> found like "animal found animal found animal
found".
Activity 2: Assert some facts. Define a rule in CLIPS. The rule should mach the pattern of
existing fact and display <value of variable> found like "dog found cat found camel found".
Learning outcome:
Learn pattern matching pattern matching in an expert system using CLIPS.
Activity:
(animal dog)
(animal cat)
(animal camel)
(animal tiger)
(animal duck)
(animal turtle)
(defrule name
(animal ?name)
=>
You will see that it will match the name of animal variable and display the result.
Exercise 1:
Assert some student names and faculty names in the working memory. Write two different
rules; one to display the faculty names and one to display the students name and execute
them.
Lab 4
Objective:
To implement and use AND operator and OR operator in CLIPS.
Activities:
Activity 1: Assert some car name and then assert some cars which have four wheel drive.
Define a rule in CLIPS. The rule should mach the pattern of existing fact and display the cars
which do not have four wheel drive .
Activity 2: Any student can register project if he has passed following two subjects:
1. Operating System
2. SAD2
Write a clips program that displays a message that Ahmed can register the project if he has
passed above two subjects.
Activity 3: any one can take the license if he completes the following three conditions. His
age must be 21 years old, gender should be male and have driving license.
Learning outcome:
understand the role of AND, OR operator and using them in CLIPS programming.
Q: From the following cars find out which one is not four-wheel drive?
1. Corolla
2. Land-cruiser
3. Yukon
4. Crown-victoria
5. Camry
Step 1: Define variables
(assert (car corolla))
(assert (car land-cruiser))
(assert (car Yukon))
Exercise 1:
Assert the below list of animals and assert the name of animals which give
egg. Define a rule to find the animals which do not give egg and display them
as mammal.
1. Monkey
2. Chicken
3. Duck
4. Camel
5. Sheep
Exercise 2:
From the following students find out the names and enter them in the
memory which is Fail?
1. Usman
2. Tariq
3. Mohammad (Pass)
4. Yahya
5. Fahad (Pass)
6. Ali (Pass)
Activity 2:
Any student can register project if he has passed following two subjects:
1. Operating System
2. SAD2
Write a clips program that displays a message that Ahmed can register the
project if he has passed above two subjects.
Activity 3:
any one can take the license if he completes the following three conditions. His
age must be 21 years old, gender should be male and have driving license.
Solution:
(assert (name ali))
Exerciese 3: Write the following program where you can see the
conditions are not true so the rule will not execute.
Second Try (First reset your memory and type the follwoing
(assert (name ali))
(assert (pass os))
(assert (fail sad2))
(defrule six
(name ?name)
(and (pass os)
(pass sad2))
=>
(printout t ?name "can register the subject" crlf))
Exercise 4:
(assert (name ali))
(assert (car-time two-months))
(assert (car-milage 2500))
RULE:
(defrule seven
(name ?name)
(or (car-time three-months)
(car-milage 2500))
=>
(printout t ?name " car is required service" crlf))
Lab 5
Objective:
To implement and use AND operator and OR operator together in CLIPS.
Activities:
Activity 1: Write a rule in CLIPS which choose people who have a bachelor degree or a
diploma with five years of experience.
Learning outcome:
Learn to combine multiple logical operators.
Activity 1:
You can get job if you are Bachelors in Computer Science (BCS) or you have
diploma with 5 years' experience.
Question:
Tariq has diploma and has 3 years’ experience. Fahad has bachelors in
computer science. Which of them will get the job?
Solution:
For Tariq:
(assert (name tariq))
(assert (have diploma))
(assert (exp 3yrs))
(defrule seven
(name ?name)
(or (have bachlors)
(and (have diploma)
(exp 5yrs)))
=>
Exercise:
Assert 10 students data with courses completed and credits left. Write rule to
print the list of students who can register for project. The condition for
registering project is completed the courses 362ISM and 326ISM or the
credits left is less than 30.
LAB 6
Objective:
Understand the test (comparison) operation in CLIPS.
Activities:
Activity 1: Find the larger of two numbers in Clips using test operation.
Activity 1: Find the smaller of two numbers in Clips using test operation.
Learning outcome:
the use of test (comparison) operation in various mathematical operations in CLIPS
Activity 1:
Find the larger of two numbers in Clips using test operation
(assert (number 76))
(assert (number 85))
Define the rule:
(defrule ten
(number ?x)
(number ?y)
(test (> ?x ?y))
=>
(printout t ?x "is greater than" ?y crlf))
Activity 2:
Find the smaller of two numbers in Clips using test operation
(assert (number 67))
(assert (number 58))
Define the rule:
(defrule less
(number ?x)
(number ?y)
(test (< ?x ?y))
=>
(printout t ?x "is greater than" ?y crlf))
Exercise:
1. Write a CLIPS program to find greater number from 3 numbers
LAB 7
Objective:
Understand the representation of mathematical expressions in CLIPS.
Activities:
Activity 1: Convert the given mathematical expressions and use them in CLIPS.
Learning outcome:
Learn to represent mathematical expressions in CLIPS
The Instructor must explain prefix, postfix, infix methods of expression and
the conversion method.
LAB 8
Objective:
Understand various functions that operates on strings in CLIPS.
Activities:
Activity 1: Practice all string related operations in CLIPS.
Learning outcome:
Learn string operations in CLIPS
Working on Strings
1 String Concatenation
The strcat function will concatenates its arguments into a single string.
Syntax
(str-cat <expression>*)
Each <expression> should be one of the following types: symbol, string, float, integer, or
instance- name.
Example
"foobar"
CLIPS>
2 Symbol Concatenation
The symcat function will concatenate its arguments into a single symbol. It is functionally
identical to the str- cat function with the exception that the returned value is a symbol and
not a string.
Syntax
(sym-cat <expression>*)
Each <expression> should be one of the following types: symbol, string, float, integer, or
instance- name.
The substring function will retrieve a portion of a string from another string.
Syntax
<string-expression>)
where the first argument, counting from one, must be a number marking the beginning
position in the string and the second argument must be a number marking the ending
position in the string. If the first argument is greater than or equal to the second argument,
a null string is returned.
Example
"cdefgh"
CLIPS>
4 Searching a String
The strindex function will return the position of a string inside another string.
Syntax
where the second argument is searched for the first occurrence of the first argument. The
str- index function returns the integer starting position, counting from one, of the first
argument in the second argument or returns the symbol FALSE if not found.
Example
FALSE
CLIPS>
The eval function evaluates the string as though it were entered at the command prompt.
Syntax
(eval <string-or-symbol-expression>)
where the only argument is the command, constant, or global variable to be executed.
NOTE: eval does not permit the use of local variables (except when the local variables are
defined as part of the command such as with an instance query function), nor will it
evaluate any of the construct definition forms (i.e., defrule, deffacts, etc.). The return value
is the result of the evaluation of the string (or FALSE if an error occurs).
The eval function is not available for binary-load only or run- time CLIPS configurations
(see the Advanced Programming Guide).
Example
(a b c)
CLIPS>
The build function evaluates the string as though it were entered at the command prompt.
Syntax
(build <string-or-symbol-expression>)
where the only argument is the construct to be added. The return value is TRUE if the
construct was added (or FALSE if an error occurs).
The build function is not available for binary-load only or run- time CLIPS configurations
(see the Advanced Programming Guide).
Example
CLIPS> (clear)
TRUE
CLIPS> (rules)
foo
CLIPS>
The uppercase function will return a string or symbol with uppercase alphabetic
characters.
Syntax
(upcase <string-or-symbol-expression>)
Example
A_WORD_TEST_FOR_UPCASE
CLIPS>
The lowcase function will return a string or symbol with lowercase alphabetic characters.
Syntax
(lowcase <string-or-symbol-expression>)
Example
a_word_test_for_lowcase
CLIPS>
The strcompare function will compare two strings to determine their logical relationship
(i.e., equal to, less than, greater than). The comparison is performed character- by-
character until the strings are exhausted (implying equal strings) or unequal characters are
found. The positions of the unequal characters within the ASCII character set are used to
determine the logical relationship of unequal strings.
Syntax
(str-compare <string-or-symbol-expression>
<string-or-symbol-expression>)
This function returns an integer representing the result of the comparison (0 if the strings
are equal, < 0 if the first argument < the second argument, and > 0 if the first argument >
the second argument).
Example
CLIPS>
Syntax
(str-length <string-or-symbol-expression>)
Example
CLIPS> (str-length "abcd")
4
CLIPS> (str-length xyz)
3
LAB 9
Objective:
Understand the implementation of the combination of logical and mathematical operations
in CLIPS.
Activities:
Activity 1: Toyota Company offers the first service on either of the two following
conditions:
3 months from the car purchase
OR
3000KM milage
Question: Ali has purchase a new car from Toyota and in two months he complete
4000KM? Does he require service?
Learning outcome:
Learn implementation of the combination of logical and mathematical operations in CLIPS.
Activity 1
Toyota Company offers the first service on either of the two following
conditions:
3 months from the car purchase
OR
3000KM milage
Question: Ali has purchase a new car from Toyota and in two months he
complete 4000KM? Does he require service?
Solution:
(assert (name ali))
(assert (months 2))
(assert (milage 4000))
Rule 2:
(defrule two
(name ?name)
(or (months 3)
(B))
=>
(printout t ?name "car required service" crlf))
Exercise:
Question-1:
King Khalid University has a job. To get this job you must have Masters in
computer science AND 5 years’ experience AND age should be more than 25.
Tariq has Masters in computer science with 3 years’ experience. His age is 27
Can he get this job?
Question-2:
To register the project either the students remaining credit should be less
than 35 OR he has passed the following two subjects.
Multimedia
Operating Systems
Mohammad is a student whose remaining credits are 30. Can he register the
project?
Question-3:
Albayan offer the guarantee of any car up to 100000 KM within 5 years from
date of purchase.
Ahmed purchases his car in 2008 and his car current mileage is 90000 KM.
Can he use his guarantee?
Solution:
(assert (name ahmed))
(assert (pur-year 2008))
(assert (milage 90000))
(assert (req-pur-year 2011))
(assert (req-milage 100000))
(defrule one
(pur-year ?x)
(req-pur-year ?y)
(test (> ?x ?y))
=>
(assert (A)))
(defrule two
(milage ?x)
(req-milage ?y)
(test (< ?x ?y))
=>
(assert (B)))
(defrule three
(name ?name)
(and (A) (B))
=>
(printout t ?name “can use guarantee” crlf))
LAB 10
Objective:
Understand the use of wildcards in CLIPS. Single- and Multi-field wild cards.
Activities:
Activity 1: Define some multi-field facts and use Single- and Multi-field wild cards to
display them in different way.
Learning outcome:
Learn use of wildcards in CLIPS. Single- and Multi-field wild cards.
Rule 2:
(defrule cls
(class ?name $? ?member $?)
=>
(printout t ?member " is a teacher of " ?name crlf))
CLIPS has two wildcard symbols that may be used to match fields in a pattern.
CLIPS interprets these wildcard symbols as standing in place of some part of a
pattern entity. The single-field wildcard, denoted by a question mark
character (?), matches any value stored in exactly one field in the pattern
entity. The multi-field wildcard, denoted by a dollar sign followed by a
question mark ($?), matches any value in zero or more fields in a pattern
entity. Single-field and multi-field wildcards may be combined in a single
pattern in any combination. It is illegal to use a multifield wildcard in a single
field slot of a deftemplate or object pattern. By default, an unspecified single-
field slot in a deftemplate/object pattern is matched against an implied single-
field wildcard. Similarly, an unspecified multifield slot in a deftemplate/object
pattern is matched against an implied multifield-wildcard.
LAB 11
Objective:
Understand the forward chaining mechanism used for inference in knowledge-Based
Systems.
Activities:
Activity 1: Define facts and rules for a small expert system. Watch the facts and variables to
understand forward chaining algorithm.
Learning outcome:
Understand the forward chaining algorithm.
Step 1: Consider the initial facts and store them in working memory of the
knowledge base
Step 3: If all the conditions are matched, fire the rule (execute the right side)
Step 5: If more than one rule is selected, use the conflict resolution strategy to
select the most appropriate rules and go to Step 4.
1. runny nose
2.temperature = 104 f
3.headache
4. cough
Solution:
Solution:
(assert (runny_nose))
(assert (temperature 104))
(assert (headache))
(assert (cough))
(defrule R1
(logical (nasal_congestion) (virosis))
=>
(assert (influenza))
)
(defrule R2
(runny_nose)
=>
(assert (nasal_congestion))
)
(defrule R3
(bodyache)
=>
(assert (itchiness))
)
(defrule R4
(temperature ?temperature)
(test (> ?temperature 100))
=>
(assert (fever))
)
(defrule R5
(headache)
=>
(assert (itchiness))
)
(defrule R6
(logical (fever) (itchiness) (cough))
=>
(assert (virosis))
)
Example
R5: IF F THEN G
Cycle through rules, looking for rules whose premise matches the working memory.
Working memory
A, B
LAB 12
Objective:
Understand the encoding of knowledge/facts positionally. Create a template which can be
used by non-ordered facts to access fields of the fact by name
Activities:
Activity 1: Define a template to store the details of a patient. The template should have
slots to store name, age, weight, height and blood pressure. Store some data in the template
and defile rules to fetch data from the templates.
Learning outcome:
Learn to create template, store data in the templates and fetch data from it.
(deftemplate personal-data
(slot name)
(slot age)
(slot weight)
(slot height)
(multislot blood-pressure)
)
Note: You need to write (reset) command to enter these data in facts.
Different rules on given data?
1. Display the age of different people
(defrule print-ages
(personal-data (name ?name) (age ?age))
=>
(printout t ?name " is " ?age " years old." crlf)
)
(defrule print-ages
(and
(personal-data (name ?name) (age ?age))
(personal-data (name ?name) (weight ?weight))
)
=>
(printout t ?name " weighs " ?weight " at " ?age " years old." crlf)
)
Exercise:
Question 1:
Define a template for the students with the following details:
Name:
ID:
Date of Birth
Level
Date of Graduation
a) Enter at least records of 7 students.
b) Define a rule that display all those student details who are in level 8 or
above
c) Define a rule that display all those students details who are going to
graduate in next 6 months
LAB 13 & 14
Objective:
To develop a small expert system that would suggest solution based on user input.
Activities:
Activity 1: Define required facts and rules required for the given expert system. Make
changes in the system as per the change in requirements
Learning outcome:
Understand the development and working of knowledge-based systems.
Activity:
Load the given expert system, run it and make changes as per the direction of the lab
instructor.
;;;======================================================
;;; Automotive Expert System
;;;
;;; This expert system diagnoses some simple
;;; problems with a car.
;;;
;;; CLIPS Version 6.0 Example
;;;
;;; To execute, merely load, reset and run.
;;;======================================================
;;****************
;;* DEFFUNCTIONS *
;;****************
;;;**********************
;;;* ENGINE STATE RULES *
;;;**********************
;;;***************
;;;* QUERY RULES *
;;;***************
;;;****************************
;;;* STARTUP AND REPAIR RULES *
;;;****************************
Exercise:
Develop a small expert system of similar kind.