490 likes | 509 Views
Samad Paydar Ferdowsi University of Mashhad. A Brief Introduction to CLIPS C Language Integrated Production System. Inroduction. C Language Integrated Production System (CLIPS) A tool for building expert systems
E N D
SamadPaydar Ferdowsi University of Mashhad A Brief Introduction to CLIPSC Language Integrated Production System
Inroduction • C Language Integrated Production System (CLIPS) • A tool for building expert systems • An expert system is a program which is specifically intended to model human expertise or knowledge • Created in 1985 at NASA’s Johnson Space Center
Inroduction • Written in C for portability and speed • Has been installed on many different operating systems • Widely used in industry, academia and government
Inroduction • Can be integrated with languages such as C, Java, Visual Basic • Different extensions are available: • FuzzyCLIPS: an extension of CLIPS incorporating fuzzy logic • PHLIPS is a PHP extension that provides a basic interface to the CLIPS environment • CLIPSNet, a .Net wrapper for CLIPS
Knowledge Representation • Three ways to represent knowledge in CLIPS • Rules • For heuristic knowledge base on experience • Deffunctions and generic functions • For procedural knowledge • Object Oriented Programming • For better (i.e. Modular) representation
Facilities • CLIPS supports developing expert systems by providing some facilities • An integrated editor • Debugging tool • Shell • Portion of CLIPS which performs inferences or reasoning
Main Elements • The CLIPS shell provides the basic elements of an expert system: • Fact-list, and instance-list: Global memory for data • Knowledge-base: Contains all the rules, the rule-base • Inference engine: Controls overall execution of rules
Basic Points • CLIPS has a LISP-like syntax • Extensive use of parentheses • E.g. as delimiters • It is case-sensitive
Facts • assert command for adding facts to the fact-list • (assert (argument) ) • Some example • (assert (is-a-country “Iran” )) • (assert (is-a-city “Tehran”)) • (assert (has-population “Tehran” 14000000))
Facts • facts command for listing current facts • (facts) • Each fact has an identifier of the form • f-{fact-index} • Duplicate facts are not allowed by default • set-fact-duplication allows duplicate fact entry.
Facts • Removing facts from the fact-list is called retraction and is done with the retract command. • To retract a fact, you must specify the fact index as the argument of retract • (retract (facts indices)
Facts • Example • (retract 2) • It is possible to retract multiple facts simultaneously • (retract 3 4 5 6) • It is possible to retract all the facts • (retract *)
Facts • Retract does not change indices of remaining facts
Facts • reset command to clear all the facts • It insert a fact (initial-fact) as f-0 • This fact is often used for convenience to initially activate rules
Facts • clear command for removing all facts from memory • It actually does more than just removing facts • Also removes all the rules
Facts • A fact such as (Tuesday) or (“Bahman”) is said to consist of a single field. • A field is a placeholder (named or unnamed) that may have a value associated with it. • The (Tuesday) fact has a single, unnamed field • Multi-field fact • (Country Iran) • The order of unnamed fields is significant. • (Country Iran) vs. (Iran Country)
nil Symbol • Symbol nil, which means "nothing" may be used for an empty field as a placeholder. • (basket-content nil) • The basket yet contains nothing
Types • Different types of fields: • float • integer • symbol • string • …
Types • A symbol is one type of field that starts with a printable ASCII character and is followed optionally by zero or more printable characters.
Comments • The semicolon acts as the start of a comment in CLIPS
String • A string must begin and end with double quotes. • The double quotes are part of the field. • Zero or more characters of any kind can appear between the double quotes.
Numbers • All numbers in CLIPS are treated as long integers or double-precision floats • (assert (x 1.5)) • (assert (y -1))
Relations • It is good rule-based programming style to use the first field of a fact to describe the • relationship of the following fields. When used this way, the first field is called a relation. • (basket-contents apple orange banana)
Debug • CLIPS provides several commands to help you debug programs • One command allows you to continuously watch facts being asserted and retracted • This is more convenient than having to type in a (facts) command over and over again and trying to figure out what's changed in the fact-list.
Watch • To start watching facts, enter the command (watch facts) • The right double arrow, ==>, means that a fact is entering memory • The left double arrow, <==, indicates a fact is leaving memory
unwatch • To turn off watching facts enter • (unwatch facts)
Rules • A rule is similar to an IF THEN statement in traditional programming languages • Rules are defined by defrule construct
Rules • General syntax of a rule (defrulerule_name "optional_comment" (pattern_1) ;Left-Hand Side (LHS) (pattern_2) ;of the rule consisting of elements ;before the "=>" (pattern_N) => (action_1) ;Right-Hand Side (RHS) (action_2) ;of the rule consisting of elements ;after the "=>" (action_M)) ;the last ")" balances the opening ; "(" to the left of "defrule". Be ; sure all your parentheses balance ; or you will get error messages
Action • An action is actually a function which performs some useful action • Typically has no return value • such as an (assert) or (retract)
Rules (defrule capital-rule “simple rule about capitals" (is-a-country ?country) ;Pattern1 (has-capital ?country ?capital) ;Pattern2 => (assert (is-a-city ?capital)) ;Action1 (assert (has-city ?country ?capital)) ;Action2 ) ;end of defrule
Variable • The name of a variable, or variable identifier, is always written by a question mark followed by a symbol that is the name of the variable.
Rules • CLIPS attempts to match the patterns of rules against facts in the fact-list. • If all the patterns of a rule match facts, the rule is activated and put on the agenda. • The agenda is a collection of activations which are those rules which match pattern entities. • Zero or more activations may be on the agenda.
Rules • The last part of a rule is the list of zero or more actions that will be executed when the rule fires. • The term fires means that CLIPS has selected a certain rule for execution from the agenda
Rules • A program will cease execution when no activations are on the agenda
Rules • When multiple activations are on the agenda, CLIPS automatically determines which activation is appropriate to fire • CLIPS orders the activations on the agenda in terms of increasing priority or salience
Rules • CLIPS always executes the actions on the RHS of the highest priority rule on the agenda. • This rule is then removed from the agenda and the actions of the new highest salience rule is executed. • This process continues until there are no more activations or a command to stop is encountered.
Rules • To see a rule, use the ppdefrule command • pretty print rule • To see a rule, specify the rule name as an argument to ppdefrule • To list all the current rules, use the rules command
Agenda • You can check what's on the agenda with the agenda command
Run • To make a program run, just enter the run command
Creating output • Use printout function to print something in the output • (printout t “Hello World!“ crlf)) • letter "t” tells CLIPS to send the output to the standard output device of your computer, i.e. the terminal
Creating output • Besides asserting facts in the RHS of rules, you also can print out information using the printout function (defrule good_rule (today “Friday”) => (printout t “No Study! No Work!“ crlf)) )
Input • CLIPS can read the information from the keyboard using the read function (defrule simple-rule (initial-fact) => (printout t “Enter your name:" crlf) (assert (Person (read) )) )
Conflict Resolution • The Inference Engine sorts the activations according to their salience. • This sorting process is called conflict resolution because it eliminates the conflict of deciding which rule should fired next.
Conflict Resolution • CLIPS offers seven different modes of conflict resolution: depth, breadth, LEX, MEA, complexity, simplicity, and random. • It's difficult to say that one is clearly better than another without considering the specific application. • Even then, it may be difficult to judge which is "best."
Conflict Resolution • The depth strategy is the standard default strategy of CLIPS. • In this strategy, is ordered from highest to lowest salience
Templates • Can help in writing rules whose patterns have a well-defined structure. • Templates are defined by the deftemplate construct • Analogous to a record structure in Pascal, or Class in C++ • Defines a group of related fields in a pattern
Templates • A deftemplate is a list of named fields called slots • Allows access by name rather than by specifying the order of fields • Contributes to good style in expert systems programs and is a valuable tool • A slot is a named single-slot or multi-slot.
Templates (deftemplate person (slot name) (slot age) (slot eye-color) (slot hair-color) )
Templates (deftemplate Person “This is a sample template for the concept of person" ;optional comment (slot name (type STRING) (default “”)) (slot gender (type SYMBOL) (default male)) )