Está en la página 1de 38

241-320 Design Architecture and Engineering for Intelligent System Suntorn Witosurapot

Contact Address: Phone: 074 287369 or Email: wsuntorn@coe.psu.ac.th

January 2010

Lecture 13: Expert System (CLIPS Tutorial)

Recall: CLIPS Expert System Shell


It is an XPS shell tool for building expert systems. rule-based, forward-chaining system multi-paradigm programming language (rule-based,
object-oriented and procedural like LISP)

We will use CLIPS to illustrate many of the concepts and methods Suggest to download the latest version of Application Installer on Windows Platform

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

In this lecture, you should


be familiar with the important concepts and methods used in rule-based XPS shells
facts, rules, pattern matching, agenda, working memory, forward chaining

understand the fundamental workings of an XPS shell


knowledge representation reasoning

apply rule-based techniques to simple examples evaluate the suitability of rule-based systems for specific tasks dealing with knowledge
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

Recall: CLIPS Expert System Shell


a tool for building expert systems.
Its name stands for C Language Integrated Production System

CLIPS is a multi-paradigm programming language that provides support for rule-based, object-oriented, and procedural programming. You can download it from:
http://clipsrules.sourceforge.net/index.html Download Installer version on Windows Platform

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

CLIPS in PC
Double click from the shortcuts

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

CLIPS Desktop Environment


You can clear the desktop area with a command from the menu bar.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

CLIPS Tutorial
We will take the tutorial given in the CLIPS main page to study in our class.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

CLIPS Tutorial 1: Facts and Rules


After the prompt CLIPS>, any valid commands can be use, e.g. the commands in below table.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

Facts and rules: General view

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

10

Facts and rules: General view of Facts

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

11

Example: Insert / list / remove facts


1

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

12

Example: Clear the system


We should now clear everything before moving to learn about the next topic.

Recheck to see whether or not it is really clear.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

13

Facts and rules: General view of Facts

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

14

Example: Rules
Rules: IF <antecedent> THEN <consequent> The rule name The IF part The THEN part Q: Can you explain the rule above?
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

15

Example: Rules (cont.)


Doing what?? This is a new fact!

Q: Explain why a new fact is occurred after running the duck rule? A: It is inferred by the rule.
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

16

Example: Rules (cont.)


This is the power of rule-based programming - the
ability to make inferences from data, particularly as the results of one rule can be used as the pattern for another.

Now add the rule

Clear all facts, but the rules will be untouched Q: Can you explain the rule above?
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

17

Example: Rules (cont.)


Since we just clear all facts, it is easy to see that no fact exists. Then, we insert 2 new facts. No fact is left!

Adding 2 rules

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

18

Example: Rules (cont.)


Q: Guess how many facts will be in the system, after running the rule? A: See the picture below

Two new facts

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

19

Example: Rules (cont.)


Rewrite the first rule, this time with the multiple actions Whenever you run the rules, you'll get a message on screen as well as the asserted quack fact.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

20

CLIPS Tutorial
We will take the tutorial given in the CLIPS main page to study in our class.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

21

Tips: Loading scripts from file


You can download scripts from a file (rather than typing them every time you use them).

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

22

CLIPS Tutorial 2: Patterns and actions


We can specify facts using the deffacts structure, but if you add the word startup it will loaded it will recreated every time a (reset) is executed.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

23

CLIPS Tutorial 2: Patterns & actions (cont.)


Here is showing that it has triggered four times, once for each fact matching the (animal ?) pattern. The ? symbol is a wildcard, which will match any symbol.

No fact at this moment But scripts is in action after this reset command

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

24

CLIPS Tutorial 2: Matching things

From the facts and rule above, this results is produced. Q: What animal are represented are on each line?
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

25

CLIPS Tutorial 2: Variables in patterns


Rule: (animal dog) (animal cat) (animal duck) (animal turtle) We can use variable e.g. ?name in the rule

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

26

CLIPS Tutorial 2: Getting data from the user


CLIPS gets information from the user by means of the (read) function.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

27

Tips: Naming the rules and facts


Please be noticed how rules or facts should be named in this example. This decision tree represents a small section of the diagnosis of a cars failure to start.
Each rounded box is a recommended remedy Each rectangular box is piece of evidence, which might be represented by a fact such as (lights-working no) or (petrol yes).
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

28

Tips: Save the script to file


If you want to save your working script for future use, you can use the save function which returns TRUE if there were no errors in the process of saving.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

29

CLIPS Tutorial
We will take the tutorial given in the CLIPS main page to study in our class.

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

30

CLIPS Tutorial 4: Templates & conditions


We have learned how facts can be created. In some case, we may need to link them together. Ex: an application which is to try to determine the general fitness of people (age Andrew 20) (weight Andrew 80) (height Andrew 188) (blood-pressure Andrew 130 80) (age brenda 23) (weight brenda 50) Q: What is the (height brenda 140) common among each set of facts? (blood-pressure brenda 120 60)
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

31

CLIPS Tutorial 4: Templates & conditions (cont.)


A kind of record data structure can be used; here it is called template
(deftemplate personal-data (slot name) (slot age) (slot weight) (slot height) (multislot blood-pressure) )

(assert (personal-data (name Andrew) (age 20) (weight 80) (height 188) (blood-pressure 130 80))) You don't have to specify all the information; (assert (personal-data (weight 150) (age 23) (name Brenda)))
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

32

CLIPS Tutorial 4: Templates & conditions (cont.)


Or use in a deffacts structure;
(deftemplate personal-data (slot name) (slot age) (slot weight) (slot height) (multislot blood-pressure) )

(deffacts people (personal-data (name Andrew) (age 20) (weight 80) (height 188) (blood-pressure 130 80)) (personal-data (name Cyril) (age 63) (weight 70) (height 1678) (blood-pressure 180 90)))

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

33

CLIPS Tutorial 4: Templates & conditions (cont.) (deftemplate personal-data


Any slot can be referred by name The function modify is used to change the value of one or more slots on a fact.
(slot name) (slot age) (slot weight) (slot height) (multislot blood-pressure) )

(defrule birthday ?birthday <- (birthday ?name) ?data-fact <- (personal-data (name ?name) (age ?age)) => (modify ?data-fact (age (+ ?age 1))) (retract ?birthday) )
Delete the birthday facts, since it is only once a year. Another reasons is that every time any part of Andrew's personal data (e.g. weight) was changed,Design Architecture & rule would Knowledge Representation and rapid ageing! the birthday be fired again, causing 241-320
Engineering for Intelligent System

Reasoning - part 3 (Rule-based)

34

CLIPS Tutorial 4: Templates & conditions (cont.)


Template-based facts can be used in rules just as ordinary facts.

(defrule birthday ?birthday <- (birthday ?name) ?data-fact <- (personal-data (name ?name) (age ?age)) => (modify ?data-fact (age (+ ?age 1))) (retract ?birthday) )
Delete the birthday facts, since it is only once a year. Another reasons is that every time any part of Andrew's personal data (e.g. weight) was changed,Design Architecture & rule would Knowledge Representation and rapid ageing! the birthday be fired again, causing 241-320
Engineering for Intelligent System

Reasoning - part 3 (Rule-based)

35

CLIPS Tutorial
You may proceed to the other tutorials on your own!

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

36

Last note & Exercise


You are now roughly understand how to program CLIPS for building the expert system. For better understanding, you are encouraged to learn more tutorials that will be uploaded to LMS server later. Although CLIPS can do more than what we learned, that are all enough for our learning in this course. Since it would be useless if we havent done anything on CLIPS, let exercise on a homework, which details are in next slide.
241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

37

Exercise:
You are about to develop an expert system that can have the diagnosis of a cars failure
The decision tree given here can be used as a guideline Interaction with user is preferred

241-320 Design Architecture & Engineering for Intelligent System

Knowledge Representation and Reasoning - part 3 (Rule-based)

38

También podría gustarte