Chapter 8: Advanced Pattern Matching Expert Systems: Principles and Programming, Fourth Edition.

Slides:



Advertisements
Similar presentations
Chapter 8 Pattern Matching
Advertisements

Chapter 7: Introduction to CLIPS
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
Chapter 4: Control Structures I (Selection)
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Introduction to Jess.
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
Basic Elements of C++ Chapter 2.
Fundamentals of Python: From First Programs Through Data Structures
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Chapter 2 How to Compile and Execute a Simple Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Chapter 9: Modular Design, Execution Control, and Rule Efficiency Expert Systems: Principles and Programming, Fourth Edition.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 7: Introduction to CLIPS Expert Systems: Principles and Programming, Fourth Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 10: Procedural Programming Expert Systems: Principles and Programming, Fourth Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Input, Output, and Processing
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
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>
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
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.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Artificial Intelligence Lecture No. 24 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.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
Artificial Intelligence Lecture No. 19 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Artificial Intelligence Lecture No. 23 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
The CLIPS Expert System Shell Dr Nicholas Gibbins
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
C Formatted Input/Output
Chapter 4: Control Structures I (Selection)
Topics Designing a Program Input, Processing, and Output
The Selection Structure
Chapter 7: Introduction to CLIPS
Chapter 4: Control Structures I (Selection)
Chapter 8: Advanced Pattern Matching
A First Book of ANSI C Fourth Edition
بسم الله الرحمن الرحیم آموزش نرم افزار CLIPS
Chapter 11: Classes, Instances, and Message-Handlers
Chapter 6 Control Statements: Part 2
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Presentation transcript:

Chapter 8: Advanced Pattern Matching Expert Systems: Principles and Programming, Fourth Edition

2 Objectives Learn about field constraints Learn how to use functions and expressions Learn how to perform summing values using rules Learn how to use the bind function Learn how to use I/O functions

Expert Systems: Principles and Programming, Fourth Edition3 Objectives Examine the two-player game called Sticks Learn how to use the test conditional element Learn how to use the predicate field constraint and the return value constraint Examine the or, and, not, exists, forall logical elements.

Expert Systems: Principles and Programming, Fourth Edition4 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.

Expert Systems: Principles and Programming, Fourth Edition5 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.

Expert Systems: Principles and Programming, Fourth Edition6 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.

Expert Systems: Principles and Programming, Fourth Edition7 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.

Expert Systems: Principles and Programming, Fourth Edition8 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.

Expert Systems: Principles and Programming, Fourth Edition9 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: (infix form)  (prefix form) Example #2: (infix) (y2 – y1) / (x2 – x1) > 0 (prefix) (> ( / ( - y2 y1 ) (- x2 x1 ) ) 0)

Expert Systems: Principles and Programming, Fourth Edition10 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.

Expert Systems: Principles and Programming, Fourth Edition11 Variable Numbers of Arguments Many CLIPS functions accept variable numbers of arguments. Example: CLIPS> ( )  returns = = -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.

Expert Systems: Principles and Programming, Fourth Edition12 Embedding Expressions Expressions may be freely embedded within other expressions:

Expert Systems: Principles and Programming, Fourth Edition13 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)

Expert Systems: Principles and Programming, Fourth Edition14 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))

Expert Systems: Principles and Programming, Fourth Edition15 Summing Values

Expert Systems: Principles and Programming, Fourth Edition16 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 )

Expert Systems: Principles and Programming, Fourth Edition17 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:

Expert Systems: Principles and Programming, Fourth Edition18 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.

Expert Systems: Principles and Programming, Fourth Edition19 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)

Expert Systems: Principles and Programming, Fourth Edition20 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

Expert Systems: Principles and Programming, Fourth Edition21 Table 8.2 File Access Modes

Expert Systems: Principles and Programming, Fourth Edition22 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 [ ]) (close data)  example

Expert Systems: Principles and Programming, Fourth Edition23 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).

Expert Systems: Principles and Programming, Fourth Edition24 Formatting Output sent to a terminal or file may need to be formatted – enhanced in appearance. To do this, we use the format function which provides a variety of formatting styles. General format: (format *)

Expert Systems: Principles and Programming, Fourth Edition25 Formatting Logical name – t – standard output device; or logical name associated with a file. Control string: –Must be delimited with quotes –Consists of format flags to indicate how parameters should be printed –1 – 1 correspondence between flags and number of parameters – constant values or expressions –Return value of the format function is the formatted string – nil can be used to suppress printing.

Expert Systems: Principles and Programming, Fourth Edition26 Formatting Example: (format nil “Name: %-15s Age: %3d” “Bob Green” 35)  Produces the results: “Name: Bob green Age: 35”

Expert Systems: Principles and Programming, Fourth Edition27 Specifications of Format Flag %-m.Nx The “-” means to left justify (right is the default) M – total field width – no truncation occurs N – number of digits of precision – default = 6 x – display format specification

Expert Systems: Principles and Programming, Fourth Edition28 Table 8.3 Display Format Specifications

Expert Systems: Principles and Programming, Fourth Edition29 Readline Function To read an entire line of input, the readline function can be used: (readline [ ]) Example: (defrule get-name => (printout t “What is your name? “ (bind ?response (readline)) (assert (user’s-name ?response)))

Expert Systems: Principles and Programming, Fourth Edition30 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.

Expert Systems: Principles and Programming, Fourth Edition31 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.

Expert Systems: Principles and Programming, Fourth Edition32 Test Condition A rule will be triggered only if all its test CEs are satisfied along with other patterns. (test ) Example: (test (> ?value 1))

Expert Systems: Principles and Programming, Fourth Edition33 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).

Expert Systems: Principles and Programming, Fourth Edition34 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.

Expert Systems: Principles and Programming, Fourth Edition35 The OR Conditional Element Consider the two rules:

Expert Systems: Principles and Programming, Fourth Edition36 OR Conditional Element These two rules can be combined into one rule – or CE requires only one CE be satisfied:

Expert Systems: Principles and Programming, Fourth Edition37 The And Conditional Element The and CE is opposite in concept to the or CE – requiring all the CEs be satisfied:

Expert Systems: Principles and Programming, Fourth Edition38 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:

Expert Systems: Principles and Programming, Fourth Edition39 Not Conditional We can implement this as follows:

Expert Systems: Principles and Programming, Fourth Edition40 The Exists Conditional Element The exists CE allows one to pattern match based on the existence of at least one fact that matches a pattern without regard to the total number of facts that actually match the pattern. This allows a single partial match or activation for a rule to be generated based on the existence of one fact out of a class of facts.

Expert Systems: Principles and Programming, Fourth Edition41 Exists Conditional

Expert Systems: Principles and Programming, Fourth Edition42 Exists When more than one emergency fact is asserted, the message to the operators is printed more than once. The following modification prevents this:

Expert Systems: Principles and Programming, Fourth Edition43 Exists This assumes there was already an alert – triggered by an operator-emergency rule. What if the operators were merely placed on alert to a drill:

Expert Systems: Principles and Programming, Fourth Edition44 Exists Now consider how the rule has been modified using the exists CE: (defrule operator-alert-for-emergency (exists (emergency)) (not (operator-alert)) => (printout t “Emergency: Operator Alert” crlf) (assert (operator-alert))))

Expert Systems: Principles and Programming, Fourth Edition45 Forall / Logical Conditional Elements The forall CE allows one to pattern match based on a set of CEs that are satisfied for every occurrence of another CE. The logical CE allows one to specify that the existence of a fact depends on the existence of another fact or group of facts.

Expert Systems: Principles and Programming, Fourth Edition46 Summary We have introduced the concept of field constraints – not, and, and or. Functions are entered into CLIPS top-level command loop or are used on the LHS or RHS of a rule. –Many functions have variable number of arguments –Function calls can be nested w/in other function calls

Expert Systems: Principles and Programming, Fourth Edition47 Summary CLIPS provides several I/O functions. –Open / close –Printout / read / readline –Format Various concepts for controlling the flow of execution are available Also included in the discussion were the following CEs: –Test, And, Or, Exists, Forall, and logical