Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS?...

81
Introduction to CLIPS

Transcript of Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS?...

Page 1: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Introduction to CLIPS

Page 2: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 2

What is CLIPS?

• CLIPS is a multiparadigm programming language that provides support for:– Rule-based

– Object-oriented

– Procedural programming

• Syntactically, CLIPS resembles:– Eclipse

– CLIPS/R2

– JESS

Page 3: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 3

Other CLIPS Characteristics

• CLIPS supports only forward-chaining rules.• The OOP capabilities of CLIPS are referred to as

CLIPS Object-Oriented Language (COOL).• The procedural language capabilities of CLIPS

are similar to languages such as:– C

– Ada

– Pascal

– Lisp

Page 4: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 4

CLIPS Characteristics

• CLIPS is an acronym for C Language Integrated Production System.

• CLIPS was designed using the C language at the NASA/Johnson Space Center.

• CLIPS is portable – PC CRAY.

Page 5: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 5

CLIPS Notation

• Symbols other than those delimited by < >, [ ], or { } should be typed exactly as shown.

• [ ] mean the contents are optional and < > mean that a replacement is to be made.

• * following a description means that the description can be replaced by zero or more occurrences of the specified value.

Page 6: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 6

CLIPS Notation

• Descriptions followed by + mean that one or more values specified by description should be used in place of the syntax description.

• A vertical bar | indicates a choice among one or more of the items separated by the bars.

Page 7: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 7

Fields

• To build a knowledge base, CLIPS must read input from keyboard / files to execute commands and load programs.

• During the execution process, CLIPS groups symbols together into tokens – groups of characters that have the same meaning.

• A field is a special type of token of which there are 8 types.

Page 8: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 8

Numeric Fields

• The floats and integers make up the numeric fields – simply numbers.

• Integers have only a sign and digits.

• Floats have a decimal and possibly “e” for scientific notation.

Page 9: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 9

Symbol Fields

• Symbols begin with printable ASCII characters followed by zero or more characters, followed by a delimiter.

• CLIPS is case sensitive.

Page 10: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 10

String Fields

• Strings must begin and end with double quotation marks.

• Spaces w/in the string are significant.

• The actual delimiter symbols can be included in a string by preceding the character with a backslash.

Page 11: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 11

Address Fields

• External addresses represent the address of an external data structure returned by a user-defined function.

• Fact address fields are used to refer to a specific fact.

• Instance Name / Address field – instances are similar to facts addresses but refer to the instance rather than a fact.

Page 12: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 12

Entering / Exiting CLIPS

• The CLIPS prompt is: CLIPS>

• This is the type-level mode where commands can be entered.

• To exit CLIPS, one types: CLIPS> (exit)

• CLIPS will accept input from the user / evaluate it / return an appropriate response:

CLIPS> (+ 3 4) value 7 would be returned.

Page 13: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 13

Facts and CLIPS

• To solve a problem, CLIPS must have data or information with which to reason.

• Each chunk of information is called a fact.

• Facts consist of:

– Relation name (symbolic field)

– Zero or more slots w/associated values

Page 14: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 14

Example Fact in CLIPS

Page 15: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 15

Deftemplate

• Before facts can be constructed, CLIPS must be informed of the list of valid slots for a given relation name.

• A deftemplate is used to describe groups of facts sharing the same relation name and contain common information.

Page 16: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 16

Deftemplate General Format

Page 17: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 17

Deftemplate vs. Ordered Facts

• Facts with a relation name defined using deftemplate are called deftemplate facts.

• Facts with a relation name that does not have a corresponding deftemplate are called ordered facts – have a single implied multifield slot for storing all the values of the relation name.

Page 18: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 18

Adding Facts

• CLIPS store all facts known to it in a fact list.

• To add a fact to the list, we use the assert command.

Page 19: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 19

Displaying Facts

• CLIPS> (facts)

Page 20: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 20

Removing Facts

• Just as facts can be added, they can also be removed.

• Removing facts results in gaps in the fact identifier list.

• To remove a fact:

CLIPS> (retract 2)

Page 21: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 21

Modifying Facts

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

Page 22: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 22

Results of Modification

• A new fact index is generated because when a fact is modified:– The original fact is retracted

– The modified fact is asserted

• The duplicate command is similar to the modify command, except it does not retract the original fact.

Page 23: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 23

Watch Command

• The watch command is useful for debugging purposes.

• If facts are “watched”, CLIPS will automatically print a message indicating an update has been made to the fact list whenever either of the following has been made:– Assertion

– Retraction

Page 24: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 24

Deffacts Construct

• The deffacts construct can be used to assert a group of facts.

• Groups of facts representing knowledge can be defined as follows:

(deffacts <deffacts name> [<optional] comment]

<facts> * )• The reset command is used to assert the facts in a

deffacts statement.

Page 25: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 25

The Components of a Rule

• To accomplish work, an expert system must have rules as well as facts.

• Rules can be typed into CLIPS (or loaded from a file).

• Consider the pseudocode for a possible rule:

IF the emergency is a fire

THEN the response is to activate the sprinkler system

Page 26: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 26

Rule Components

• First, we need to create the deftemplate for the types of facts:

(deftemplate emergency (slot type))

-- type would be fire, flood, etc.

• Similarly, we must create the deftemplate for the types of responses:

(deftemplate response (slot action))

-- action would be “activate the sprinkler”

Page 27: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 27

Rule Components

• The rule would be shown as follows:

(defrule fire-emergency “An example rule”

(emergency (type fire))

=>

(assert (response

(action activate-sprinkler-system))))

Page 28: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 28

Analysis of the Rule

• The header of the rule consists of three parts:1. Keyword defrule

2. Name of the rule – fire-emergency

3. Optional comment string – “An example rule”

Page 29: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 29

Analysis of Rule

• If all the patterns of a rule match facts, the rule is activated and put on the agenda.

• The agenda is a collection of activated rules.

• The arrow => represents the beginning of the THEN part of the IF-THEN rule.

• The last part of the rule is the list of actions that will execute when the rule fires.

Page 30: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 30

The Agenda and Execution

• To run the CLIPS program, use the run command:

CLIPS> (run [<limit>])

-- the optional argument <limit> is the maximum number of rules to be fired – if omitted, rules will fire until the agenda is empty.

Page 31: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 31

Execution

• When the program runs, the rule with the highest salience on the agenda is fired.

• Rules become activated whenever all the patterns of the rule are matched by facts.

• The reset command is the key method for starting or restarting .

• Facts asserted by a reset satisfy the patterns of one or more rules and place activation of these rules on the agenda.

Page 32: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 32

What is on the Agenda?

• To display the rules on the agenda, use the agenda command:

CLIPS> (agenda)

• Refraction is the property that rules will not fire more than once for a specific set of facts.

• The refresh command can be used to make a rule fire again by placing all activations that have already fired for a rule back on the agenda.

Page 33: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 33

Command for Manipulating Constructs

• The list-defrules command is used to display the current list of rules maintained by CLIPS.

• The list-deftemplates displays the current list of deftemplates.

• The list-deffacts command displays the current list of deffacts.

• The ppdefrule, ppdeftemplate and ppdeffacts commands display the text representations of a defrule, deftemplate, and a deffact, respectively.

Page 34: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 34

Commands

• The undefrule, undeftemplate, and undeffacts commands are used to delete a defrule, a deftemplate, and a deffact, respectively.

• The clear command clears the CLIPS environment and adds the initialfact-defacts to the CLIPS environment.

• The printout command can also be used to print information.

Page 35: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 35

Other Commands

• Load – allows loading of rules from an external file.

• Save – opposite of load, allows saving of constructs to disk

Page 36: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 36

Commenting and Variables

• Comments – provide a good way to document programs to explain what constructs are doing.

• Variables – store values, syntax requires preceding with a question mark (i.e: ?example)

Page 37: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 37

Fact Addresses, Single-Field Wildcards, and Multifield Variables

• A variable can be bound to a fact address of a fact matching a particular pattern on the LHS of a rule by using the pattern binding operator “<-”.

• Single-field wildcards can be used in place of variables when the field to be matched against can be anything and its value is not needed later in the LHS or RHS of the rule (?).

• Multifield variables and wildcards allow matching against more than one field in a pattern ($?)

Page 38: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 38

Field Constraints

• In addition to pattern matching capabilities and variable bindings, CLIPS has more powerful pattern matching operators.

• Consider writing a rule for all people who do not have brown hair:– We could write a rule for every type of hair color that

is not brown.

– This involves testing the condition in a roundabout manner – tedious, but effective.

Page 39: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 39

Field Constraints

• The technique for writing a rule for all non-brown hair colors implies that we have the ability to supply all hair colors – virtually impossible.

• An alternative is to use a field constraint to restrict the values a field may have on the LHS – the THEN part of the rule.

Page 40: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 40

Connective Constraints

• Connective constraints are used to connect variables and other constraints.

• Not connective – the ~ acts on the one constraint or variable that immediately follows it.

• Or constraint – the symbol | is used to allow one or more possible values to match a field or a pattern.

• And constraint – the symbol & is useful with binding instances of variables and on conjunction with the not constraint.

Page 41: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 41

Combining Field Constraints

• Field constraints can be used together with variables and other literals to provide powerful pattern matching capabilities.

• Example #1: ?eyes1&blue|green– This constraint binds the person’s eye color to the

variable, ?eyes1 if the eye color of the fact being matched is either blue or green.

• Example #2: ?hair1&~black– This constraint binds the variable ?hair1 if the hair

color of the fact being matched is not black.

Page 42: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 42

Functions and Expressions

• CLIPS has the capability to perform calculations.

• The math functions in CLIPS are primarily used for modifying numbers that are used to make inferences by the application program.

Page 43: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 43

Numeric Expressions in CLIPS

• Numeric expressions are written in CLIPS in LISP-style – using prefix form – the operator symbol goes before the operands to which it pertains.

• Example #1:5 + 8 (infix form) + 5 8 (prefix form)

• Example #2:(infix) (y2 – y1) / (x2 – x1) > 0

(prefix) (> ( / ( - y2 y1 ) (- x2 x1 ) ) 0)

Page 44: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 44

Return Values

• Most functions (addition) have a return value that can be an integer, float, symbol, string, or multivalued value.

• Some functions (facts, agenda commands) have no return values – just side effects.

• Division results are usually rounded off.• Return values for +, -, and * will be integer if all

arguments are integer, but if at least one value is floating point, the value returned will be float.

Page 45: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 45

Variable Numbers of Arguments

• Many CLIPS functions accept variable numbers of arguments.

• Example:CLIPS> (- 3 5 7) returns 3 - 5 =-2 - 7 = -9

• There is no built-in arithmetic precedence in CLIPS – everything is evaluated from left to right.

• To compensate for this, precedence must be explicitly written.

Page 46: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 46

Embedding Expressions

• Expressions may be freely embedded within other expressions:

Page 47: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 47

Summing Values Using Rules

• Suppose you wanted to sum the areas of a group of rectangles.– The heights and widths of the rectangles can be

specified using the deftemplate:

(deftemplate rectangle (slot height) (slot width))

• The sum of the rectangle areas could be specified using an ordered fact such as:

(sum 20)

Page 48: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 48

Summing Values

• A deffacts containing sample information is:

(deffacts initial-information

(rectangle (height 10) (width 6))

(rectangle (height 7) (width 5)

(rectangle (height 6) (width 8))

(rectangle (height 2) (width 5))

(sum 0))

Page 49: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 49

Summing Values

Page 50: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 50

The Bind Function

• Sometimes it is advantageous to store a value in a temporary variable to avoid recalculation.

• The bind function can be used to bind the value of a variable to the value of an expression using the following syntax:

(bind <variable> <value>)

Page 51: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 51

I/O Functions

• When a CLIPS program requires input from the user of a program, a read function can be used to provide input from the keyboard:

Page 52: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 52

Read Function from Keyboard

• The read function can only input a single field at a time.

• Characters entered after the first field up to the are discarded.

• To input, say a first and last name, they must be delimited with quotes, “xxx xxx”.

• Data must be followed by a carriage return to be read.

Page 53: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 53

I/O from/to Files

• Input can also come from external files.• Output can be directed to external files.• Before a file can be accessed, it must be opened

using the open function:

Example:

(open “mydata.dat” data “r”)

• mydata.dat – is the name of the file (path can also be provided)

Page 54: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 54

I/O from/to Files

• data – is the logical name that CLIPS associates with the file

• “r” – represents the mode – how the file will be used – here read access

• The open function acts as a predicate function

– Returns true if the file was successfully opened

– Returns false otherwise

Page 55: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 55

Table 8.2 File Access Modes

Page 56: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 56

Close Function

• Once access to the file is no longer needed, it should be closed.

• Failure to close a file may result in loss of information.

• General format of the close function:

(close [<file-ID>])

(close data) example

Page 57: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 57

Reading / Writing to a File

• Which logical name used, depends on where information will be written – logical name t refers to the terminal (standard output device).

Page 58: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 58

Predicate Functions

• A predicate function is defined to be any function that returns:– TRUE

– FALSE

• Any value other than FALSE is considered TRUE.

• We say the predicate function returns a Boolean value.

Page 59: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 59

The Test Conditional Element

• Processing of information often requires a loop.• Sometimes a loop needs to terminate

automatically as the result of an arbitrary expression.

• The test condition provides a powerful way to evaluate expressions on the LHS of a rule.

• Rather than pattern matching against a fact in a fact list, the test CE evaluates an expression – outermost function must be a predicate function.

Page 60: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 60

Test Condition

• A rule will be triggered only if all its test CEs are satisfied along with other patterns.

(test <predicate-function>)

Example:

(test (> ?value 1))

Page 61: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 61

Predicate Field Constraint

• The predicate field constraint : allows for performing predicate tests directly within patterns.

• The predicate field constraint is more efficient than using the test CE.

• It can be used just like a literal field constraint – by itself or part of a complex field.

• The predicate field constraint is always followed by a function for evaluation (predicate function).

Page 62: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 62

Return Value Constraint

• The return value constraint = allows the return value of a function to be used for comparison inside a pattern.

• The return value constraint must be followed by a function (not necessarily a predicate function).

• The function must have a single-field return value.

Page 63: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 63

The OR Conditional Element

• Consider the two rules:

Page 64: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 64

OR Conditional Element

These two rules can be combined into one rule – or CE requires only one CE be satisfied:

Page 65: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 65

The And Conditional Element

The and CE is opposite in concept to the or CE – requiring all the CEs be satisfied:

Page 66: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 66

Not Conditional Element

When it is advantageous to activate a rule based on the absence of a particular fact in the list, CLIPS allows the specification of the absence of the fact in the LHS of a rule using the not conditional element:

Page 67: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 67

Not Conditional

We can implement this as follows:

Page 68: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 68

Deftemplate Attributes

• CLIPS provides slot attributes which can be specified when deftemplate slots are defined.

• Slot attributes provide strong typing and constraint checking.

• One can define the allowed types that can be stored in a slot, range of numeric values.

• Multislots can specify min / max numbers of fields they can contain.

• Default attributes can be provided for slots not specified in an assert command.

Page 69: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 69

Type Attribute

• Defines the data types can be placed in a slot

• Example:

(deftemplate person

(multislot name (type SYMBOL))

(SLOT AGE (TYPE integer)))

• Once defined, CLIPS will enforce these restrictions on the slot attributes

name – must store symbols

age – must store integers

Page 70: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 70

Allowed Value Attributes

• CLIPS allows one to specify a list of allowed values for a specific type – 8 are provided:

Symbols Strings

Lexemes Integers

Floats Numbers

Instance-names Values

Page 71: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 71

Range Attributes

• This attribute allows the specification of minimum and maximum numeric values.

• Example:(deftemplate person

(multislot name (type SYMBOL))

(slot age (type INTEGER)

(range 0 ?VARIABLE)))

Page 72: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 72

Salience

• CLIPS provides two explicit techniques for controlling the execution of rules:– Salience

– Modules

• Salience allows the priority of rules to be explicitly specified.

• The agenda acts like a stack (LIFO) – most recent activation placed on the agenda being first to fire.

Page 73: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 73

Salience

• Salience allows more important rules to stay at the top of the agenda, regardless of when they were added.

• Lower salience rules are pushed lower on the agenda; higher salience rules are higher.

• Salience is set using numeric values in the range -10,000 +10,000 – zero is intermediate priority.

• Salience can be used to force rules to fire in a sequential fashion.

Page 74: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 74

Salience

• Rules of equal salience, activated by different patterns are prioritized based on the stack order of facts.

• If 2+ rules with same salience are activated by the same fact, no guarantee about the order in which they will be place on the agenda.

Page 75: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 75

Phases and Control Facts

Page 76: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 76

Figure 9.2 Assignment of Salience for Different Phases

Page 77: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 77

Implementation of System

Approaches:1. Embed the control knowledge directly into the rules.

Example:

Detection rules would include rules indicating when the isolation phase should be entered. Each group of rules would be given a pattern indicating in which phase it would be applicable.

Page 78: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 78

Implementation

2. Use salience to organize the rules.

3. Separate the control knowledge from the domain knowledge. Each rule is given a control pattern that indicates its applicable phase. Control rules are then written to transfer control between the different phases.

Page 79: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 79

Salience Hierarchy

• Salience hierarchy is a description of the salience values used by an expert system.

• Each level corresponds to a specific set of rules whose members are all given the same salience.

• When rules for detection / isolation / recovery are zero, salience hierarchy is:

Page 80: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 80

Misuse of Salience

• Because salience is such a powerful tool, allowing explicit control over execution, there is potential for misuse.

• Well-designed rule-based programs should allow inference engine to control firings in an optimal manner.

• Salience should be used to determine the order when rules fire, not for selecting a single rule from a group of rules when patterns can control criteria for selection.

Page 81: Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.

Expert Systems: Principles and Programming, Fourth Edition 81

Rule of Thumb

• No more than seven salience values should ever be required for coding an expert system – bested limited to 3 – 4.

• For large expert systems, programmers should use modules to control the flow of execution – limited to 2 – 3 salience values.