___________________________________________ ___________________________________________ 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
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.
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
INTRODUCTION TO CLIPSINTRODUCTION TO CLIPS
• 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
• 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
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
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
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
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
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
• 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"
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
– 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
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
• deftemplate fact(deftemplate person "deftemplate example”
(slot name)
(slot age)
(slot eye-color)
(slot hair-color))
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))
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>
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)))
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
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>
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))
• 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
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.
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
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