Chapter 8 Pattern Matching

Slides:



Advertisements
Similar presentations
Expert systems CLIPS Seyed Hashem Davarpanah
Advertisements

The CLIPS Programming Tool History of CLIPS –Influenced by OPS5 and ART –Implemented in C for efficiency and portability –Developed by NASA, distributed.
CLIPS 1 Expert Systems Dr. Samy Abu Nasser. CLIPS 2 Course Overview u Introduction u CLIPS Overview u Concepts, Notation, Usage u Knowledge Representation.
Expert System Shells - Examples
Samad Paydar Ferdowsi University of Mashhad.  C Language Integrated Production System (CLIPS)  A tool for building expert systems  An expert system.
1 01/12/2011Knowledge-Based Systems, Paula Matuszek Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011.
Over view on Clips By: Mohsen Faghihi. Clips view.
November 2, 2004AI: CLIPS Language Tutorial1 Artificial Intelligence CLIPS Language Tutorial Michael Scherger Department of Computer Science Kent State.
© C. Kemke CLIPS 1 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
Introduction to CLIPS (Lecture Note #17)
Chapter 7: Introduction to CLIPS
CLIPS C Language Integrated Production System Note: Some slides and/or pictures are adapted from Lecture slides / Books of Dr Zafar Alvi.
Chapter 12: Expert Systems Design Examples
Stacks.
Introduction to CLIPS (Chapter 7) Fact List (contains data) Knowledge Base (contains rules) Inference Engine (controls execution)
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Rule-Based Reasoning Introduction UBI 517 Expert Systems.
© C. Kemke CLIPS 1 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
© C. Kemke Control 1 COMP 4200: Expert Systems Dr. Christel Kemke Department of Computer Science University of Manitoba.
Chapter 8: Advanced Pattern Matching Expert Systems: Principles and Programming, Fourth Edition.
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
Introduction to Jess.
Jess Presentation by Chun Ping Wang. What is Jess? Jess is an expert system shell made for java. Rete pattern algorithm. Purpose. –Jess is best use for.
Chapter 1 Program Design
CS 561, Session 25 1 Introduction to CLIPS Overview of CLIPS Facts Rules Rule firing Control techniques Example.
Chapter 2: Algorithm Discovery and Design
1 01/12/2011Knowledge-Based Systems, Paula Matuszek More Intro to CLIPS Paula Matuszek CSC 9010, Spring, 2011.
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
Review Topics Test 1. Background Topics Definitions of Artificial Intelligence & Turing Test Physical symbol system hypothesis vs connectionist approaches.
Expert System Topic 2.
1 Programming a Knowledge Based Application. 2 Overview.
Chapter 7: Introduction to CLIPS Expert Systems: Principles and Programming, Fourth Edition.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 10: Procedural Programming Expert Systems: Principles and Programming, Fourth Edition.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Chapter 7: Introduction to CLIPS Presented By: Farnaz Ronaghi.
IMSS013 CLIPS. 2 Background CLIPS is an expert system tool developed by the Software Technology Branch (STB), NASA/Lyndon B. Johnson Space Center. First.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Artificial Intelligence as Representation and Search.
Expert Systems Chapter 7 Introduction to CLIPS Entering and Exiting CLIPS A> CLIPS  CLIPS (V6.5 09/01/97) CLIPS> exit exit CLIPS> (+ 3 4)  7 CLIPS>
Advanced Pattern Matching. Field constraints Used to restrict the values of a field on LHS of a rule Used to restrict the values of a field on LHS of.
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Introduction to CLIPS. Expert Systems: Principles and Programming, Fourth Edition2 What is CLIPS? CLIPS is a multiparadigm programming language that provides.
1 Knowledge Based Systems (CM0377) Lecture 10 (Last modified 19th March 2001)
Artificial Intelligence Lecture No. 19 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Artificial Intelligence Lecture No. 23 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Unit 6 Repetition Processing Instructor: Brent Presley.
Mostly adopted from Jason Morris notes (Morris Technical Solutions)
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
Artificial Intelligence Lecture No. 22 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
The CLIPS Expert System Shell Dr Nicholas Gibbins
Summary for final exam Agent System..
Python Programming Module 4 Sensors and Loops Python Programming, 2/e1.
Introduction to CLIPS 2 Session 13 Course: T0273 – EXPERT SYSTEMS Year: 2014.
Jörg Kewisch, June 10, 2013, LILUG Meeting CLIPS C Language Integrated Production System Developed at the Software Development Branch, NASA Lyndon B. Johnson.
Programming with Recursion
Intelligent Systems JESS constructs.
Chapter 7: Introduction to CLIPS
Programming with Recursion
While Loops BIS1523 – Lecture 12.
File Handling Programming Guides.
Chapter 8: Advanced Pattern Matching
بسم الله الرحمن الرحیم آموزش نرم افزار CLIPS
Chapter 11: Classes, Instances, and Message-Handlers
JESS (Java Expert System Shall)
Jess Knowledge, Influence, Behavior
CPE/CSC 481: Knowledge-Based Systems
Computer Based Tutoring
Presentation transcript:

Chapter 8 Pattern Matching

8.2 Variables Variables in CLIPS are always written in the syntax of a question mark followed by a symbolic field name. Examples: ?speed ?sensor ?value ?noun ?color

Bound (Bind) Variables are used on the LHS of a rule to contain slot values that can later be compared to other values on the LHS of a rule or accessed on the RHS of a rule. The term bound and bind are used to describe the assignment of a value to a variable.

Use of variables One common use of variables is to bind a variable on the LHS of a rule and then use that value on the RHS of the rule. Example: CLIPS> (clear)  CLIPS> (deftemplate person (slot name) (slot eyes) (slot hair)) 

CLIPS> (defrule find-blue-eyes (person (name CLIPS> (defrule find-blue-eyes (person (name ?name) (eyes blue)) => (printout t ?name “has blue eyes.” crlf))  CLIPS> (deffacts people (person (name Jane) (eyes blue) (hair red)) (person (name Joe) (eyes green) (hair brown)) (person (name Jack) (eyes blue) (hair black)) (person (name Jeff) (eyes green) (hair brown)))  CLIPS> (reset) CLIPS> (run)  Jack has blue eyes. Jane has blue eyes. CLIPS> Example1

Multiple Use of Variables 8.3 Multiple Use of Variables The first time a variable is bound to a value, it retains that value within the rule. Other variables with the same name that are bound to a value must be bound to the first variable’s value. CLIPS> (deftemplate find (slot eyes)) CLIPS> (defrule find-eyes (find (eyes ?eyes)) (person (name ?name) (eyes ?eyes)) => (printout t ?name “ has” ?eyes “ eyes.” crlf)) Example2

CLIPS> (reset) CLIPS> (assert (find (eyes blue))) <Fact-5> CLIPS> (run) Jack has blue eyes. Jane has blue eyes. CLIPS> (assert (find (eyes green))) <Fact-6> CLIPS> (run) Jeff has green eyes. Joe has green eyes. CLIPS> (assert (find (eyes purple))) <Fact-7> CLIPS> (run) CLIPS>

8.4 Fact Addresses Retraction, modification, and duplication of facts are extremely common operations and are usually done on the RHS of a rule rather than at the top level. Before a fact can be manipulated from the RHS of a rule, however, there must be some way to specify the fact that matched a particular pattern.

Pattern Binding A variable can be bound to the fact address of the fact matching a pattern on the LHS of a rule by using the pattern binding operation, “<-”. Once the variable is bound it can be used with the retract, modify, or duplication commands in place of a fact index.

Without the retract command, CLIPS> (clear) CLIPS> (deftemplate person (slot name) (slot address)) CLIPS> (deftemplate moved (slot name) (slot address)) CLIPS> (defrule process-moved-information ?f1 <- (moved (name ?name) (address ?address)) ?f2 <- (person (name ?name)) => (retract ?f1) (modify ?f2 (address ?address))) CLIPS> (deffacts example (person (name “John Hill”) (address “25 Mulberry Lane”)) (moved (name “John Hill”) (address “37 Cherry Lane”))) CLIPS> (reset) CLIPS> (run) Without the retract command, the program will go into an infinite loop. Example3

Single-field Wildcard 8.5 Single-field Wildcard It is useful to test for the existence of a field within a slot without actually assigning a value to a variable. Particularly useful for multifield slots. Example: Suppose we want to print the social security number of every person with a specified last name……

(deftemplate person. (multislot name) (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number 483-98-9083)) (person (name Jack R. Public) (social-security-number 483-98-9084)) (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name ?first-name ?middle-name ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) Not referred to by actions on the RHS or other pattern or slots on the LHS of the rule. NO USE! Instead of using a variable, a single-field wildcard can be used when a field is required, but the value is not important.

(deftemplate person. (multislot name) (deftemplate person (multislot name) (slot social-security-number)) (deffacts some-people (person (name John Q. Public) (social-security-number 483-98-9083)) (person (name Jack R. Public) (social-security-number 483-98-9084)) (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name ? ? ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) Instead of using a variable, a single-field wildcard can be used when a field is required, but the value is not important. Example4

Blocks World A good example of planning, might be applied to automated manufacturing, where a robot arm manipulates parts. To begin solving, it will be useful to set up a configuration of blocks that can be used for testing the program. What steps must be taken to move block C on top of E? A D B E C F

Simple Solution Rule: directly move block C on top of block E. However, this rule could only be applied if both block C and block E had no blocks on top of them. Rule move-directly IF The goal is to move block ?upper on top of block ?lower and block ?upper is the top block in its stack and block ?lower is the top block in its stack THEN Move block ?upper on top of block ?lower F E C C F E

Simple Solution – cont. The simple blocks world does not require that the blocks be restacked, just move them to the floor. RULE Clear-upper-block IF The goal is to move block ?x and block ?x is not the top block in its stack and block ?above is on top of block ?x THEN The goal is to move block ?above to the floor RULE Clear-lower-block IF The goal is to move another block on top of block ?x and block ?x is not the top block in its stack and block ?above is on top of block ?x THEN The goal is to move block ?above to the floor X above X above X above X above

Blocks World – cont. Since the floor is really not a block, it may be necessary to treat it differently. RULE Move-to-floor IF The goal is to move block ?upper on top of the floor and block ?upper is the top block in its stack THEN Move block ?upper on top of the floor upper upper

Blocks World – cont. Several types of facts will be needed: (deftemplate on-top-of (slot upper) (slot lower)) Facts: (on-top-of (upper A) (lower B)) (on-top-of (upper B) (lower C)) (on-top-of (upper D) (lower E)) (on-top-of (upper E) (lower F)) (on-top-of (upper nothing) (lower A)) (on-top-of (upper C) (lower floor)) (on-top-of (upper nothing) (lower D)) (on-top-of (upper F) (lower floor)) C B A F E D

Floor and Nothing The words floor and nothing might be mistaken as the names of blocks. Use implied deftemplate block to identify the blocks. (block A) (block B) (block C) (block D) (block E) (block F)

Goals Goal described: (deftemplate goal (slot move) (slot on-top-of) Initial goal: (goal (move C) (on-top-of E))

Initial Configuration (deffacts initial-state (block A) (block B) (block C) (block D) (block E) (block F) (on-top-of (upper A) (lower B)) (on-top-of (upper B) (lower C)) (on-top-of (upper D) (lower E)) (on-top-of (upper E) (lower F)) (on-top-of (upper nothing) (lower A)) (on-top-of (upper C) (lower floor)) (on-top-of (upper nothing) (lower D)) (on-top-of (upper F) (lower floor)) (goal (move C) (on-top-of E)))

Update the stack information. Move-directly Rule Determine that there is a goal to move a block on top of another block. (defrule move-directly ?goal <- (goal (move ?block1) (on-top-of ?block2)) (block ?block1) (block ?block2) (on-top-of (upper nothing) (lower ?block1)) ?stack-1 <- (on-top-of (upper ?block1) (lower ?block3)) ?stack-2 <- (on-top-of (upper nothing) (lower ?block2)) => (retract ?goal ?stack-1 ?stack-2) (assert (on-top-of (upper ?block1) (lower ?block2)) (on-top-of (upper nothing) (lower ?block3))) (printout t ?block1 “ moved on top of “ ?block2 “.” crlf)) Ensure that a goal to move a block onto the floor will not be processed by this rule. Match against information necessary to update the stacks that the moving block is being taken from and moved to. Update the stack information. 3 2 1 3 1 2

Move-to-floor Rule (defrule move-to-floor ?goal <- (goal (move ?block1) (on-top-of floor)) (block ?block1) (on-top-of (upper nothing) (lower ?block1)) ?stack <- (on-top-of (upper ?block1) (lower ?block2)) => (retract ?goal ?stack) (assert (on-top-of (upper ?block1) (lower floor)) (on-top-of (upper nothing) (lower ?block2))) (printout t ?block1 “ moved on top of floor.” crlf)) 2 1 2 1

Clear-upper-block Rule (defrule clear-upper-block (goal (move ?block1)) (block ?block1) (on-top-of (upper ?block2) (lower ?block1)) (block ?block2) => (assert (goal (move ?block2) (on-top-of floor))) 1 2 1 2

Clear-lower-block Rule 1 2 (defrule clear-lower-block (goal (on-top-of ?block1)) (block ?block1) (on-top-of (upper ?block2) (lower ?block1)) (block ?block2) => (assert (goal (move ?block2) (on-top-of floor)))) Execution CLIPS> (unwatch all) CLIPS> (reset) CLIPS> (run) A moved on top of floor. B moved on top of floor. D moved on top of floor. C moved on top of E. CLIPS> 1 2 B A F E D C C B A F E D C B A F E D C B A F E D C B A F E D Example5

Step-by-step Method First: pseudorules were written using English-like text. Second: the pseudorules were used to determine the types of facts that would be required. Deftemplates describing the facts were designed, and the initial knowledge for the program was coded using these deftemplates. Finally, the pseudorules were translated to CLIPS rules using the deftemplates as a guide for translation.

Best Representation? It is not always possible to determine the best method for representing facts or the types of rules that will be needed to build an expert system. Following a consistent methodology, however, can aid in the development of an expert system even when a great deal of prototyping and iteration need to be performed.

8.7 Multifield Wildcards Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Example: (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name ? ? ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) will not match a person fact (person (name Joe Public) (social-security-number 345-89-3039))

8.7 Multifield Wildcards Multifield wildcards ($?) can be used to match against zero or more fields of a pattern. Example: (defrule print-social-security-numbers (print-ss-numbers-for ?last-name) (person (name $? ?last-name) (social-security-number ?ss-number)) => (printout t ?ss-number crlf)) will not match a person fact (person (name Joe Public) (social-security-number 345-89-3039)) Replace the two single-field wildcards With a single multifield wildcard.

Multifield Variables Multifield variables are preceeded by a “$?”. CLIPS> (reset) CLIPS> (assert (print-children John Q. Public)) <Fact-3> CLIPS> (run) (John Q. Public) has children (Jane Paul Mary) CLIPS> Multifield variables are preceeded by a “$?”. Example: construct showing how to print names of all the children of a specified person. (deftemplate person (multifield name) (multifield children)) (deffacts some-people (person (name John Q. Public) (children Jane Paul Mary)) (person (name Jack R. Public) (children Rick))) (defrule print-children (print-children $?name) (person (name $?name) (children $?children)) => (printout t ?name “has children “ ?children crlf)) The $ is only used on the LHS to indicate that zero or more fields can be bound to the variable. Example6

More than one multifield variable can be used in a single slot CLIPS> (reset) CLIPS> (assert (find-child Paul)) <Fact-3> CLIPS> (run) (John Q. Public) has child Paul Other children are (Jane) (Mary) CLIPS> (assert (find-child Rick)) <Fact-4> (Jack R. Public) has child Rick Other children are () () CLIPS> (assert (find-child Bill)) <Fact-5> CLIPS> Example: find all the people who have a child with a specific name. (defrule find-child (find-child ?child) (person (name $?name) (children $?before ?child $?after)) => (printout t ?name “has child” ?child crlf) (printout t “Other children are” ?before “ “ ?after crlf)) Example6

Match pattern in more than one way CLIPS> (reset) CLIPS> (assert (person (name Joe Fiveman) (children Joe Joe Joe))) <Fact-3> CLIPS> (assert (find-child Joe)) <Fact-4> CLIPS> (agenda) 0 find-child: f-4, f-3 0 find-child: f-4, f-3 0 find-child: f-4, f-3 For a total of 3 activations. CLIPS> (run) (Joe Fiveman) Has child Joe Other children are () (Joe Joe) (Joe Fiveman) Has child Joe Other children are (Joe) (Joe) (Joe Fiveman) Has child Joe Other children are (Joe Joe) () There are three different ways in which the variables ?child ?before, and ?after can be bound with the fact f-3. Example6

Implementing a Stack A stack is an ordered data structure to which items can be added and removed. Items are added and removed to one “end” of the stack. A new item can be pushed (added) to the stack or the last item added can be popped (removed) from the stack. In a stack the first value added is the last item to be removed and the last item added is the first item to be removed.

Push (stack X Y Z) (push-value A) (stack A X Y Z) (defrule push-value ?push-value <- (push-value ?value) ?stack <- (stack $?rest) => (retract ?push-value ?stack) (assert (stack ?value $?rest)) (printout t “Pushing value “ ?value crlf)) (stack X Y Z) (push-value A) (stack A X Y Z)

Pop (stack A X Y Z) (pop-value) (stack X Y Z) (defrule pop-value-valid ?pop-value <- (pop-value) ?stack <- (stack ?value $?rest) => (retract ?pop-value ?stack) (assert (stack $?rest)) (printout t “Popping value “ ?value crlf)) (defrule pop-value-invalid ?pop-value <- (pop-value) (stack) => (retract ?pop-value) (printout t “Popping from empty stack” crlf)) (stack X Y Z) Example7

Blocks World Revisited (deffacts initial-state (stack A B C) (stack D E F) (goal (move C) (on-top-of E) (stack)) (defrule move-directly ?goal <- (goal (move ?block1) (on-top-of ?block2)) ?stack-1 <- (stack ?block1 $?rest1) ?stack-2 <- (stack ?block2 $?rest2) => (retract ?goal ?stack-1 ?stack-2) (assert (stack $?rest1)) (assert (stack ?block1 ?block2 $?rest2)) (printout t ?block1 “ moved on top of “ ?block2 “.” crlf))

Blocks World Revisited – cont. (defrule move-to-floor ?goal <- (goal (move ?block1) (on-top-of floor)) ?stack-1 <- (stack ?block1 $?rest) => (retract ?goal ?stack-1) (assert (stack ?block1)) (assert (stack $?rest)) (printout t ?block1 “ moved on top of floor.” crlf )) (defrule clear-upper-block (goal (move ?block1)) (stack ?top $? ?block1 $?) => (assert (goal (move ?top) (on-top-of floor)))) (defrule clear-lower-block (goal (on-top-of ?block1)) (stack ?top $? ?block1 $?) => (assert (goal (move ?top) (on-top-of floor)))) Example8