© C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer...

32
© C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba

Transcript of © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer...

Page 1: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 1

COMP 4200: Expert SystemsCOMP 4200:

Expert Systems

Dr. Christel Kemke

Department of Computer Science

University of Manitoba

Page 2: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 2

CLIPS – Classes (COOL)CLIPS – Classes (COOL)

Intro to CLIPS – Classes (COOL) class definition

class specification slot specification, facets

instance creation message-handlers Classes and rules

Page 3: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 3

CLIPS – Example ClassesCLIPS – Example Classes(defclass person

(is-a USER) ;provides reference to system-defined ;super-class USER

(role concrete) ;this class can have instances

(slot gender (type STRING)) ;just slots with type(slot age (type NUMBER))) ;constraints

(defclass man(is-a person)(slot gender (type STRING)(default “male”)))

Try: In the Browse-Menu, for the Defclass-Manager and check the messagehandlers of these classes.

Page 4: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 4

CLIPS – Defining ClassesCLIPS – Defining Classes(defclass person

(is-a USER) ;provides reference to system-defined ;super-class USER

(role concrete) ;this class can have instances

(pattern-match reactive)* ;instances of this class can be

;used for pattern-matching in rules

(slot <slot-name> ;slot-specification

(create-accessor read-write) * ;read / write accessor ;created for this slot

(default <default-value>) ;default-value for slot

... <other facets>* ... ))

* Class and Slot descriptions often have defaults, e.g. pattern-match reactive, or create-accessor read-write. Refer to the CLIPS bpg.

Page 5: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 5

CLIPS – Defining InstancesCLIPS – Defining Instances(definstances people people is a list of instances

(mike of person of class person; instance

(gender “male”) mike is created.

(age 24)) )

(definstances men men is a list of instances of

(mark of man class man; here only one

(age 24)) ) instance named mark.

Try: (send [mark] get-gender) and (send [mike] get-gender)

Try: (send [mark] put-age 25)

Note: instance-names in messages have to be put in [ ... ]

Page 6: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 6

Message-HandlersMessage-Handlers

(defmessage-handler

<class-name>

<message-handler-name> (<parameters>*)

<body>

)

Some message-handlers like get-<slot-name> or put-<slot-name> are system pre-defined message-handlers.

Page 7: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 7

System-defined Message-HandlersSystem-defined Message-Handlers

retrieving and storing slot-values

retrieve (send [instance] get-slot) e.g. (send [Titanic] get-speed)

store (send [instance] put-slot value) e.g. (send [Titanic] put-speed 20)

Page 8: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 8

Message-Handler - ExampleMessage-Handler - Example

(defmessage-handler person birthday ()(send ?self put-age (+ (send ?self get-age) 1))

)

Message-Handler 'birthday' associated with class person (no parameter) sends a message to ?self to put-age to the current age + 1. It also works to use 'dynamic-put' which sends a put-message for the age to the current instance to which the birthday-message has been sent.

Try: (send [mark] birthday)

Try: substitute (send ?self ...) with dynamic-put or dynamic-get

Page 9: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 9

Parameters in Message-HandlersParameters in Message-Handlers

(defmessage-handler ship set-speed (?x)(dynamic-put speed ?x))

)

Message-Handler set-speed associated with class ship sets the speed of the current instance to the value of ?x.

e.g. (send [Titanic] set-speed 20)

sends a message to the instance [Titanic] of ship

which evaluates to (dynamic-put speed 20);

or (send ?self put-speed 20)

Page 10: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 10

if-then-else in Message-Handlersif-then-else in Message-HandlersControl Structures used in Message-Handlers.

instead of send to self: (dynamic-put speed 20))

instead of ?self:speed (dynamic-get speed))

Message-Handler 'slow-down' associated with class ship tests whether speed of addressed instance is > 30 and then sends a message to ?self to put-speed 20. It also works to use 'dynamic-put speed 20' which sends a put-message for speed to the current instance.

(defmessage-handler ship slow-down ()

(if (> ?self:speed 30)

then (send ?self put-speed 20))

)

Page 11: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 11

Classes and Meta-Classes in CLIPSClasses and Meta-Classes in CLIPS

OBJECT

USER

Liner

Ship

Titanicdomain instances

user definedclasses

systemdefined classes

INSTANCES

CLASSES

Page 12: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 12

Objects and RulesObjects and Rules

How to connect objects (instances) with rules?

1. read slot-values - use object (instance and slot-values) in condition part of rule

2. write slot-values - include put-messages in action-part of rule

Page 13: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 13

Objects in Condition Part of RuleObjects in Condition Part of Rule

Solution 1:

(object (name [<instance-name>]) (<slot-name> <slot-value>)* )

Example 1:

Comment: In the 'object'-pattern the name of the addressed instance is identified as [titanic] and the value for the slot speed (defined in the class ship) is determined as 20. This information is used for pattern matching.

(defrule if-titanic-goes-20-mph (object (name [titanic]) (speed 20))

=> ... )

Page 14: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 14

Objects in Condition Part of RuleObjects in Condition Part of Rule

Use variables instead of slot-values (corresponding to facts used in rules, based on templates)

Example 2:

Comment: In the 'object'-pattern the speed of the referenced object (instance [titanic]) is bound to variable ?speed which can be used later in the printout-function.

(defrule titanic-goes-at-what-speed (object (name [titanic]) (speed ?speed))

=> (printout "Titanic goes at " ?speed " mph."

crlf ))

Page 15: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 15

Objects in Condition Part of RuleObjects in Condition Part of Rule

Use variables instead of instance-names

Example 3:

Comment: In the 'object'-pattern the variable ?name is bound to the referenced object (e.g. the instance [titanic]) and thus can be used later in the action-part.

(defrule any-ship-which-goes-20mph

(object (name ?name) (speed 20mph))

=>

(printout ?name " goes at 20 mph." crlf )

)

Page 16: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 16

Objects in Action Part of RuleObjects in Action Part of Rule

In the action-part of a rule, a message to an instancecan be send, corresponding to messages used in message-handlers.

(send [<instance-name>] <message>)

Example 4:

(defrule titanic-has-to-slow-down(object (name [titanic]) (speed ?speed))(test (> ?speed 30))

=> (send [titanic] put-speed 20))

Page 17: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 17

Objects in Action Part of RuleObjects in Action Part of Rule

Use variables instead of instance-names

Example 5:

Comment: the variable ?name can be bound during pattern matching to an instance, e.g. [titanic], and a message (to put the value 20 into the speed-slot) is sent in the action-part to this instance.

(defrule ships-over-30mph-have-to-slow-down(object (name ?name) (speed ?speed))(test (> ?speed 30))

=> (send ?name put-speed 20))

Page 18: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 18

Objects in Action Part of RuleObjects in Action Part of Rule

Task:Define a rule which contains instead of a test-condition a field-constraint with predicate expression to constrainthe matching instances to those whose speed is over 30.

(defrule slow-down-ships-which-go-over-30mph

(object (name ?name) (speed ?speed))

(test (> ?speed 30))=> (send ?name put-speed 20))

Page 19: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 19

CLIPS - DefinitionsCLIPS - Definitions

Page 20: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 20

A defclass is a construct for specifying the properties (slots) of a class of objects.

A defclass consists of four elements: 1) a name, 2) a list of superclasses from which the new class inherits slots

and message-handlers,

3) a specifier saying whether or not the creation of direct instances of the new class is allowed and

4) a list of slots specific to the new class. All user-defined classes must inherit from at least one class; COOL

provides predefined system classes for use as a base in the derivation of new classes.

Any slots explicitly given in the defclass override those gotten from inheritance.

COOL applies rules to the list of superclasses to generate a class precedence list

for the new class. Facets further describe slots. Some examples of facets include:

default value, cardinality, and types of access allowed.

DEFCLASS DescriptionDEFCLASS Description

Page 21: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 21

(defclass <name> [<comment>](is-a <superclass-name>+) superclass(es)[<role>] can instances be created?[<pattern-match-role>] can instances be used in pattern matching?<slot>* slots!<handler-documentation>*) methods associated with this class

<handler-documentation> ::= (message-handler <name> [<handler-type>])

<handler-type> ::= primary | around | before | after

DEFCLASS - SyntaxDEFCLASS - Syntax

Page 22: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 22

<role> ::= (role concrete | abstract)

<pattern-match-role> ::= (pattern-match reactive | non-reactive)

<slot> ::= (slot <name> <facet>*) | (single-slot <name> <facet>*) | (multislot <name> <facet>*)

<facet> ::= <default-facet> | <storage-facet> | <access-facet> | <propagation-facet> | <source-facet> | <pattern-match-facet> | <visibility-facet> | <create-accessor-facet> <override-message-facet> |

<constraint-attributes>

Page 23: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 23

<default-facet>::= (default ?DERIVE | ?NONE | <expression>)

<pattern-match-facet> ::= (pattern-match reactive | non-reactive)

<visibility-facet> ::= (visibility private | public)

<create-accessor-facet> ::= (create-accessor ?NONE | read | write | read-write)

<override-message-facet> ::= (override-message ?DEFAULT | <message-name>)

Page 24: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 24

DEFINSTANCESDEFINSTANCES

Similar to deffacts, the definstances construct allows the specification of instances which will be created every time the reset command is executed.

On every reset all current instances receive a delete message, and the equivalent of a make-instance function call is made for every instance specified in the definstances constructs.

Page 25: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 25

DEFINSTANCES - SyntaxDEFINSTANCES - Syntax

(definstances <definstances-name> [<comment>] (<instance-definition>)*)

<instance-definition> ::= <instance-name-expression> of <class-name-expression> <slot-override>*

<slot-override> ::= (<slot-name-expression> <expression>*)

Page 26: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 26

DEFMESSAGE HANDLERDEFMESSAGE HANDLERObjects are manipulated by sending them messages via the function send. The result of a message is a useful return-value or side-effect.

A defmessage-handler is a construct for specifying the behavior of a class of objects in response to a particular message. The implementation of a message is made up of pieces of procedural code called message-handlers (or handlers for short).

Each class in the class precedence list of an object's class can have handlers for a message. In this way, the object's class and all its superclasses share the labor of handling the message. Each class's handlers handle the part of the message which is appropriate to that class.

Page 27: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 27

DEFMESSAGE HANDLERDEFMESSAGE HANDLERA defmessage-handler is comprised of: • a class name to which to attach the handler (the class must have been

previously defined), • a message name to which the handler will respond, • an optional type (the default is primary), • an optional comment, • a list of parameters that will be passed to the handler during execution, • an optional wildcard parameter, and• a series of expressions which are executed in order when the handler

is called.

The return-value of a message-handler is the evaluation of the last expression in the body.

Page 28: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 28

DEFMESSAGE HANDLERDEFMESSAGE HANDLER

(defmessage-handler <class-name> <message-name> [<handlertype>]

[<comment>](<parameter>* [<wildcard-parameter>])<action>*)

<handler-type> ::= around | before | primary | after

<parameter> ::= <single-field-variable>

<wildcard-parameter> ::= <multifield-variable>

Page 29: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 29

DEFMESSAGE HANDLERDEFMESSAGE HANDLER

Within a class, the handlers for a particular message can be further subdivided into four types or categories:

1. primary,

2. before,

3. after and

4. around.

Page 30: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 30

(defclass pistol(is-a USER)(role concrete)(pattern-match reactive)(slot safety (type SYMBOL) (create-accessor read-write))

(slot slide (type SYMBOL) (create-accessor read-write))

(slot hammer (type SYMBOL) (create-accessor read-write))

(slot chamber (type INTEGER) (create-accessor read-write))

(slot magazine (type SYMBOL) (create-accessor read-write))

(slot rounds (type INTEGER) (create-accessor read-write))

)

CLIPS Example – Pistol FiringCLIPS Example – Pistol Firing

Page 31: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 31

;; We associate message-handlers with ‘safety’ and ‘drop.’

(defmessage-handler pistol safety (?on-off) (dynamic-put safety ?on-off) (if (eq ?on-off on) then (dynamic-put hammer down)))

;; The ‘drop’ message-handler drops the magazine from the mag.

(defmessage-handler pistol drop () (dynamic-put magazine out))

Page 32: © C. Kemke CLIPS Classes 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.

© C. Kemke CLIPS Classes 32

;; We associate message-handlers with ‘safety’ and ‘drop.’

(defmessage-handler pistol safety (?on-off) (dynamic-put safety ?on-off) (if (eq ?on-off on) then (dynamic-put hammer down)))

;; The ‘drop’ message-handler drops the magazine from the mag well.

(defmessage-handler pistol drop () (dynamic-put magazine out))