Top Banner
___________________________________________ ___________________________________________ Knowledge-Based Systems Knowledge-Based Systems (KBSs) (KBSs) ___________________________________________ ___________________________________________ ة ي ف و ن م ل ا عة ام جFourth Year Fourth Year (First Semester) (First Semester) CS CS (CS471) (CS471) & & IT IT (College Ellective -2) (College Ellective -2) Dr. Hamdy M. Dr. Hamdy M. Mousa Mousa MENOUFIA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATION Lecture two
33
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture2

______________________________________________________________________________________

Knowledge-Based Knowledge-Based SystemsSystems (KBSs)(KBSs)______________________________________________________________________________________

جامعة المنوفية

Fourth YearFourth Year (First Semester) (First Semester) CS CS (CS471) (CS471) & IT & IT (College Ellective -2)(College Ellective -2)

Dr. Hamdy M. MousaDr. Hamdy M. Mousa

MENOUFIA UNIVERSITYFACULTY OF COMPUTERS AND INFORMATION

Lecture two

Page 2: Lecture2

INTRODUCTION TO CLIPSINTRODUCTION TO CLIPS

Page 3: Lecture2

• CLIPS is a decent example of an expert system shell– rule-based, forward-chaining system

• it illustrates many of the concepts and methods used in other ES shells

• it allows the representation of knowledge, and its use for solving suitable problems

Motivation

Page 4: Lecture2

• be familiar with the important concepts and methods used in rule-based ES shells – facts, rules, pattern matching, agenda, working

memory, forward chaining

• understand the fundamental workings of an ES shell – knowledge representation– reasoning

• apply rule-based techniques to simple examples

• evaluate the suitability of rule-based systems for specific tasks dealing with knowledge

Objectives

Page 5: Lecture2

Introduction• CLIPSC Language Implementation Production

System

• forward-chaining – starting from the facts, a solution is developed

• pattern-matching – Rete matching algorithm: find ``fitting'' rules and

facts

• knowledge-based system shell – empty tool, to be filled with knowledge

• multi-paradigm programming language – rule-based, object-oriented (Cool) and procedural

Page 6: Lecture2

The CLIPS Programming Tool

• history of CLIPS– implemented in C for efficiency and portability– developed by NASA, distributed & supported by

COSMIC– runs on PC, Mac, UNIX, VAX VMS

• CLIPS provides mechanisms for expert systems– a top-level interpreter– production rule interpreter– object oriented programming language– LISP-like procedural language

Page 7: Lecture2

Components of CLIPS

• rule-based language– can create a fact list– can create a rule set– an inference engine matches facts against

rules

• object-oriented language (COOL)– can define classes– can create different sets of instances– special forms allow you to interface rules and

objects

Page 8: Lecture2

Notation• symbols, characters, keywords

– entered exactly as shown: (example)

• square brackets [...] – contents are optional: (example [test])

• pointed brackets (less than / greater than signs) < ... >– replace contents by an instance of that type– (example <char>) >>>>>>> (example 1)

• star * – replace with zero or more instances of the type– <char>* >>>>>>> 1 or 1 2 or 1 2 3 or

• plus + – replace with one or more instances of the type– <char>+ (is equivalent to <char> <char>* )

• vertical bar | – choice among a set of items:– true | false

Page 9: Lecture2

Tokens and Fields

• Tokens – groups of characters with special meaning for

CLIPS,– e.g. ( ) \ separated by delimiters

• (space, tab, Carriage Return, ...)

• Fields – particularly important group of tokens– CLIPS primitive data types

• float, integer, symbol, string, external address, instance name, instance address

Page 10: Lecture2

CLIPS Primitive Data Types– float: decimal point (1.5) or exponential notation

(3.7e10)– integer: [sign] <digit>+– symbol: <printable ASCII character>+

• e.g. this-is-a-symbol, wrzlbrmft, !?@*+• NOTE: CLIPS is case-sensitive

– string: delimited by double quotes• e.g. "This is a string“

• There can be zero or more characters of any kind between the double quotes, including characters normally used by CLIPS as delimiters. i.e.

"CLIPS"

Page 11: Lecture2

Ex.: “”three-tokens“”

in clips

“”

three-tokens

“”

• Within a string, double quotes can be included by using the backslash operator, \.

Ex.: “ \”single-token\“”

CLIPS Primitive Data Types

Page 12: Lecture2

– external address• address of external data structure returned by user-

defined functions– instance name (used with Cool)

• delimited by square brackets– instance address (used with Cool)

• return values from functions

• multi-field value is A series of zero or more fields contained together. Multifield values are usually created by calling a function or when specifying initial values for facts.

For example, the zero length multifield:( )

the multifield containing the symbols this and that :(this that)

CLIPS Primitive Data Types

Page 13: Lecture2

Invoke / Exit CLIPSentering CLIPS

double-click on icon, or type program name(CLIPS)

system prompt appears:

CLIPS> • exiting CLIPS

at the system prompt

CLIPS>

type (exit)– Note: enclosing parentheses are important; they

indicate a command to be executed, not just a symbol

Page 14: Lecture2

A:\>CLIPSDOSCLIPS (V6.22 06/15/04) CLIPS> exitexit

CLIPS> (+ 3 4) 7

CLIPS> (exit) A:\>

Invoke / Exit CLIPS

Page 15: Lecture2

Facts• elementary information items (“chunks”)• Facts is A "chunk" of information • Facts consist of a relation name (a symbolic field) followed by

zero or more slots (also symbolic fields) and their associated values.

• relation name – symbolic field used to access the information– often serves as identifier for the fact

• slots (zero or more) – symbolic fields with associated values

• deftemplate construct – used to define the structure of a fact

• names and number of slots• deffacts

– used to define initial groups of facts

Page 16: Lecture2

Example of a fact

(person (name "John Q. Public")

(age 23)

(eye-color blue)

(hair-color black))

Page 17: Lecture2

deftemplate fact

• (deftemplate <relation-name> [<optional-comment>]

<slot-definition>*)

• deftemplate fact(deftemplate person "deftemplate example”

(slot name)

(slot age)

(slot eye-color)

(slot hair-color))

Page 18: Lecture2

ordered fact• deftemplate facts are Facts with a relation name that has a

corresponding deftemplate. • ordered facts are Facts with a relation name that does not have a

corresponding deftemplate

• ordered fact

(person-name Franz J. Kurfess)

Ex.:

(number-list 7 9 3 4 20)– equivalent to defining the following deftemplate:

(deftemplate number-list (multislot values))– then defining the fact as follows:

(number-list (values 7 9 3 4 20))

Page 19: Lecture2

Defining Facts• Facts representing information can be added and removed from the fact list.• New facts can be added to the fact list using the assert command. The

syntax of the assert command is(assert <fact>+)

• Facts can be asserted. CLIPS> (assert (parson (name "John Q. Public") (age 23)

(eye-color blue)(hair-color black)))

<Fact-0>CLIPS>

• Facts can be listedCLIPS> (facts) f-0 (person (name "John Q. Public") (age 23) (eye-color blue) (hair-color black)) For a total of 1 fact. CLIPS>

Page 20: Lecture2

retract command

• Removing facts from the fact list is called retraction and is done with the retract command.

(retract <fact-index>+)

• Facts can be retractedCLIPS> (retract 0)CLIPS> (facts)

• an instance of a fact is created by (assert (person (name "Franz J. Kurfess") (age 46) (eye-color brown) (hair-color brown)))

Page 21: Lecture2

MODIFYING AND DUPLICATING FACTS

• Slot values of deftemplate facts can be modified using the modify command.

(modify <fact-index> <slot-modifier>+)• where <slot-modifier> is

(<slot-name> <slot-value>)Ex.:

CLIPS> (modify 0 (age 24))<Fact-1>CLIPS> (Facts)f-1 (person (name "John Q. Public")

(age 24) (eye-color blue) (hair-color black))

For a total of 1 fact,CLIPS>

Page 22: Lecture2

The duplicate command

CLIPS> (duplicate 2 (name “Jack S. Public”)<Fact-3>CLIPS> (Facts)f-2 (person (name "John Q. Public") (age 24) (eye-color blue) (hair-color black))f-3 (person (name "Jack Q. Public") (age 24) (eye-color blue) (hair-color black))For a total of 2 fact,CLIPS>

Page 23: Lecture2

WATCH COMMAND (watch <watch-item>)

• where <watch-item> is one of the symbols facts, rules, activations, statistics, compilations, focus, deffunction, global, generic-function, methods, instances, slots, messages, message-handler or all.

CLIPS> (facts 3 3)f-3 (person (name "Jack S. Public")

(age 24) (eye-color blue)

(hair-color black)) For a total of 1 fact,CLIPS> (watch facts) CLIPS> (modify 3 (age 25))

<= = f-3 (person (name "Jack S. Public")(age 24)(eye-color blue) (hair-color black))

==> f-4 (person (name "Jack S. Public")(age 25)(eye-color blue)(hair-color black))

<Fact-4> CLIPS>

Page 24: Lecture2

Initial Facts(deffacts kurfesses "some members of the Kurfess family"

(person (name "Franz J. Kurfess") (age 46) (eye-color brown) (hair-color brown))

(person (name "Hubert Kurfess") (age 44) (eye-color blue) (hair-color blond))

(person (name "Bernhard Kurfess") (age 41) (eye-color blue) (hair-color blond))

(person (name "Heinrich Kurfess") (age 38) (eye-color brown) (hair-color blond))

(person (name "Irmgard Kurfess") (age 37) (eye-color green) (hair-color blond)))

Page 25: Lecture2

Usage of Facts

• adding facts – (assert <fact>+)

• deleting facts – (retract <fact-index>+)

• modifying facts – (modify <fact-index> (<slot-name> <slot-value>)+ )

• retracts the original fact and asserts a new, modified fact

• duplicating facts – (duplicate <fact-index> (<slot-name> <slot-value>)+ )

• adds a new, possibly modified fact

• inspection of facts – (facts)

• prints the list of facts– (watch facts)

• automatically displays changes to the fact list

Page 26: Lecture2

Rules

• general format(defrule <rule name> ["comment"]

<patterns>* ; left-hand side (LHS)

; or antecedent of the rule

=>

<actions>*) ; right-hand side (RHS)

; or consequent of the rule

Page 27: Lecture2

Rule Components

• rule header – defrule keyword, name of the rule, optional

comment string

• rule antecedent (LHS)– patterns to be matched against facts

• rule arrow – separates antecedent and consequent

• rule consequent (RHS)– actions to be performed when the rule fires

Page 28: Lecture2

Example• industrial plant monitoring expert system >>>> IF the emergency is a fire

THEN the response is to activate the sprinkler system

CLIPS Rules: (deftemplate emergency {slot type))(deftemplate response (slot action))

; Rule header(defrule fire-emergency "An example rule" ; Patterns

(emergency (type fire)); THEN arrow=>; Actions

(assert (response

(action activate-sprinkler-system))))

Page 29: Lecture2

Examples of Rules

• simple rule (defrule birthday-FJK (person (name "Franz J. Kurfess") (age 46) (eye-color brown) (hair-color brown)) (date-today April-13-02)=> (printout t "Happy birthday, Franz!") (modify 1 (age 47)))

Page 30: Lecture2

Properties of Simple Rules

• very limited:– LHS must match facts exactly– facts must be accessed through their index

number– changes must be stated explicitly

• can be enhanced through the use of variables

Page 31: Lecture2

THE AGENDA AND EXECUTION• A CLIPS program can be made to run with the run command.

(run [<limit>] )• where the optional argument <limit> is the maximum number of

rules to be fired. • The list of rules on the agenda can be displayed with the agenda

command. (agenda)

EX.:

CLIPS> (reset)CLIPS> (assert (emergency (type fire))) <Fact-1>CLIPS> (agenda)0 fire-emergency: f-1For a total of 1 activation.CLIPS>The 0 indicates the salience of the rule on the agenda.

Page 32: Lecture2

Program Execution

• agenda – if all patterns of a rule match with facts, it is put on

the agenda– (agenda) displays all activated rules

• salience – indicates priority of rules

• refraction – rules fire only once for a specific set of facts

• prevents infinite loops

– (refresh <rule-name>) • reactivates rules

Page 33: Lecture2

Execution of a Program

• (reset) prepares (re)start of a program: – all previous facts are deleted– initial facts are asserted– rules matching these facts are put on the agenda

• (run [<limit>]) starts the execution

• breakpoints– (set-break [<rule-name>])

• stops the execution before the rule fires,

• continue with (run)

– (remove-break [<rule-name>])

– (show-breaks)