My Basket
You have nothing in your shopping cart yet.
Title: Clips (Artificial Intelligence)
Description: In subject of (Artificial Intelligence) in computer science Defination of CLIPS
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
Reference Manual
Volume I
Basic Programming Guide
Quicksilver Beta
December 31st 2007
CLIPS Reference Manual
CLIPS Basic Programming Guide
Quicksilver Beta December 31st 2007
CONTENTS
License Information
...
iii
Acknowledgements
...
1
Section 2 - CLIPS Overview
...
1 Interacting with CLIPS
...
1
...
3
2
...
2 Automated Command Entry and Loading
...
1
...
5
2
...
5
2
...
6
2
...
1 Data Types
...
3
...
9
2
...
3 Constructs
...
4 Data Abstraction
...
4
...
11
2
...
1
...
11
2
...
1
...
12
2
...
1
...
13
2
...
2 Objects
...
4
...
1 Initial Objects
...
4
...
14
2
...
15
2
...
1 Heuristic Knowledge – Rules
...
5
...
16
2
...
2
...
16
2
...
2
...
16
2
...
2
...
17
2
...
2
...
17
2
...
17
2
...
1 COOL Deviations from a Pure OOP Paradigm
...
6
...
18
2
...
3 Instance-set Queries and Distributed Actions
...
21
CLIPS Basic Programming Guide
i
CLIPS Reference Manual
3
...
22
3
...
22
3
...
23
3
...
23
Section 4 - Deffacts Construct
...
27
5
...
27
5
...
28
5
...
29
5
...
1 Depth Strategy
...
3
...
29
5
...
3 Simplicity Strategy
...
3
...
30
5
...
5 LEX Strategy
...
3
...
31
5
...
7 Random Strategy
...
4 LHS Syntax
...
4
...
33
5
...
1
...
34
5
...
1
...
36
5
...
1
...
38
5
...
1
...
40
5
...
1
...
43
5
...
1
...
45
5
...
1
...
46
5
...
1
...
49
5
...
2 Test Conditional Element
...
4
...
51
5
...
4 And Conditional Element
...
4
...
53
5
...
6 Exists Conditional Element
...
4
...
56
5
...
8 Logical Conditional Element
...
4
...
61
5
...
9
...
61
5
...
9
...
61
5
...
9
...
62
5
...
9
...
63
5
...
9
...
63
5
...
10 Declaring Rule Properties
...
4
...
1 The Salience Rule Property
...
4
...
2 The Auto-Focus Rule Property
...
67
Section 7 - Deffunction Construct
...
75
8
...
75
8
...
76
8
...
76
8
...
76
8
...
1 Generic Function Headers
...
4
...
77
8
...
3 Method Parameter Restrictions
...
4
...
79
8
...
81
8
...
1 Applicability of Methods Summary
...
5
...
82
8
...
3 Shadowed Methods
...
5
...
85
8
...
5 Generic Function Return Value
...
87
9
...
87
9
...
87
9
...
88
9
...
1 Multiple Inheritance
...
3
...
1 Multiple Inheritance Rules
...
3
...
92
9
...
2
...
92
9
...
2
...
92
9
...
3 Slots
...
3
...
1 Slot Field Type
...
3
...
2 Default Value Facet
...
3
...
3 Storage Facet
...
3
...
4 Access Facet
...
3
...
5 Inheritance Propagation Facet
...
3
...
6 Source Facet
...
3
...
7 Pattern-Match Reactivity Facet
...
3
...
8 Visibility Facet
...
3
...
9 Create-Accessor Facet
...
3
...
10 Override-Message Facet
...
3
...
11 Constraint Facets
...
3
...
102
CLIPS Basic Programming Guide
iii
CLIPS Reference Manual
9
...
103
9
...
1 Message-handler Parameters
...
4
...
1 Active Instance Parameter
...
4
...
106
9
...
3 Daemons
...
4
...
108
9
...
4
...
108
9
...
4
...
109
9
...
4
...
110
9
...
4
...
110
9
...
4
...
111
9
...
4
...
111
9
...
4
...
112
9
...
4
...
112
9
...
112
9
...
1 Applicability of Message-handlers
...
5
...
113
9
...
3 Shadowed Message-handlers
...
5
...
114
9
...
5 Message Return Value
...
6 Manipulating Instances
...
6
...
115
9
...
1
...
117
9
...
2 Reinitializing Existing Instances
...
6
...
120
9
...
4 Setting Slots
...
6
...
121
9
...
6 Delayed Pattern-Matching When Manipulating Instances
...
6
...
122
9
...
7
...
122
9
...
7
...
123
9
...
7
...
123
9
...
7
...
124
9
...
8 Duplicating Instances
...
6
...
1 Directly Duplicating an Instance with Delayed Pattern-Matching
...
6
...
2 Directly Duplicating an Instance with Immediate Pattern-Matching
...
6
...
3 Duplicating an Instance using Messages with Delayed Pattern-Matching
...
6
...
4 Duplicating an Instance using Messages with Immediate Pattern-Matching 126
9
...
127
9
...
1 Instance-set Definition
...
7
...
129
9
...
3 Query Definition
...
7
...
131
iv
Table of Contents
CLIPS Reference Manual
9
...
5 Scope in Instance-set Query Functions
...
7
...
132
9
...
7 Halting and Returning Values from Query Functions
...
7
...
133
9
...
8
...
133
9
...
8
...
133
9
...
8
...
134
9
...
8
...
134
9
...
8
...
135
9
...
8
...
135
Section 10 - Defmodule Construct
...
1 Defining Modules
...
2 Specifying a Construct’s Module
...
3 Specifying Modules
...
4 Importing and Exporting Constructs
...
4
...
140
10
...
2 Importing Constructs
...
5 Importing and Exporting Facts and Instances
...
5
...
142
10
...
142
Section 11 - Constraint Attributes
...
1 Type Attribute
...
2 Allowed Constant Attributes
...
3 Range Attribute
...
4 Cardinality Attribute
...
5 Deriving a Default Value From Constraints
...
6 Constraint Violation Examples
...
151
12
...
151
12
...
1 Testing For Numbers
...
1
...
151
12
...
3 Testing For Integers
...
1
...
152
12
...
5 Testing For Strings
...
1
...
152
12
...
7 Testing For Even Numbers
...
1
...
152
12
...
9 Testing For Multifield Values
...
1
...
153
12
...
11 Comparing for Equality
...
1
...
153
12
...
13 Comparing Numbers for Equality
...
1
...
154
12
...
15 Greater Than Comparison
...
1
...
155
12
...
17 Less Than Comparison
...
1
...
156
12
...
19 Boolean And
...
1
...
157
12
...
21 Boolean Not
...
2 Multifield Functions
...
2
...
157
12
...
2 Specifying an Element
...
2
...
158
12
...
4 Comparing Multifield Values
...
2
...
159
12
...
6 Creating Multifield Values from Strings
...
2
...
160
12
...
8 Extracting a Sub-sequence from a Multifield Value
...
2
...
161
12
...
10 Inserting Fields within a Multifield Value
...
2
...
162
12
...
12 Getting All but the First Field from a Multifield Value
...
2
...
162
12
...
14 Deleting Specific Values within a Multifield Value
...
2
...
163
12
...
163
12
...
1 String Concatenation
...
3
...
164
12
...
3 Taking a String Apart
...
3
...
164
12
...
5 Evaluating a Function within a String
...
3
...
165
12
...
7 Converting a String to Uppercase
...
3
...
166
12
...
9 Comparing Two Strings
...
3
...
167
12
...
11 Checking the Syntax of a Construct or Function Call within a String
...
3
...
168
12
...
168
12
...
1 Logical Names
...
4
...
169
12
...
2
...
169
vi
Table of Contents
CLIPS Reference Manual
12
...
2
...
170
12
...
2
...
171
12
...
2
...
171
12
...
2
...
172
12
...
2
...
173
12
...
2
...
175
12
...
2
...
175
12
...
2
...
176
12
...
2
...
177
12
...
2
...
177
12
...
178
12
...
1 Standard Math Functions
...
5
...
1 Addition
...
5
...
2 Subtraction
...
5
...
3 Multiplication
...
5
...
4 Division
...
5
...
5 Integer Division
...
5
...
6 Maximum Numeric Value
...
5
...
7 Minimum Numeric Value
...
5
...
8 Absolute Value
...
5
...
9 Convert To Float
...
5
...
10 Convert To Integer
...
5
...
182
12
...
2
...
183
12
...
2
...
184
12
...
2
...
184
12
...
2
...
184
12
...
2
...
185
12
...
2
...
185
12
...
2
...
185
12
...
2
...
185
12
...
2
...
186
12
...
2
...
186
12
...
2
...
186
12
...
2
...
187
12
...
2
...
187
12
...
187
12
...
1 Binding Variables
...
6
...
then
...
189
12
...
3 While
...
6
...
191
12
...
5 Progn
...
6
...
192
CLIPS Basic Programming Guide
vii
CLIPS Reference Manual
12
...
7 Return
...
6
...
193
12
...
9 Switch
...
7 Miscellaneous Functions
...
7
...
194
12
...
2 Gensym*
...
7
...
195
12
...
4 Random
...
7
...
196
12
...
6 Time
...
7
...
197
12
...
8 Determining the Restrictions for a Function
...
7
...
197
12
...
10 Calling a Function
...
7
...
198
12
...
199
12
...
1 Determining the Module in which a Deftemplate is Defined
...
8
...
199
12
...
3 Getting the Cardinality for a Deftemplate Slot
...
8
...
200
12
...
5 Getting the Default Value for a Deftemplate Slot
...
8
...
201
12
...
7 Testing whether a Deftemplate Slot is a Multifield Slot
...
8
...
202
12
...
9 Getting the Numeric Range for a Deftemplate Slot
...
8
...
202
12
...
11 Getting the Primitive Types for a Deftemplate Slot
...
8
...
203
12
...
203
12
...
1 Creating New Facts
...
9
...
204
12
...
3 Modifying Template Facts
...
9
...
206
12
...
5 Asserting a String
...
9
...
207
12
...
7 Determining If a Fact Exists
...
9
...
208
12
...
9 Determining the Slot Names Associated with a Fact
...
9
...
209
12
...
11 Retrieving the Fact-List
...
9
...
210
12
...
12
...
211
12
...
12
...
212
viii
Table of Contents
CLIPS Reference Manual
12
...
12
...
213
12
...
12
...
213
12
...
12
...
214
12
...
12
...
214
12
...
12
...
215
12
...
12
...
215
12
...
218
12
...
1 Getting the List of Deffacts
...
10
...
219
12
...
219
12
...
1 Getting the List of Defrules
...
11
...
219
12
...
219
12
...
1 Getting the Current Focus
...
12
...
220
12
...
3 Removing the Current Focus from the Focus Stack
...
13 Defglobal Functions
...
13
...
221
12
...
2 Determining the Module in which a Defglobal is Defined
...
14 Deffunction Functions
...
14
...
222
12
...
2 Determining the Module in which a Deffunction is Defined
...
15 Generic Function Functions
...
15
...
222
12
...
2 Determining the Module in which a Generic Function is Defined
...
15
...
223
12
...
4 Type Determination
...
15
...
224
12
...
6 Calling Shadowed Methods
...
15
...
225
12
...
8 Calling a Specific Method
...
15
...
226
12
...
227
12
...
1 Class Functions
...
16
...
1 Getting the List of Defclasses
...
16
...
2 Determining the Module in which a Defclass is Defined
...
16
...
3 Determining if a Class Exists
...
16
...
4 Superclass Determination
...
16
...
5 Subclass Determination
...
16
...
6 Slot Existence
...
16
...
7 Testing whether a Slot is Writable
...
16
...
8 Testing whether a Slot is Initializable
...
16
...
9 Testing whether a Slot is Public
...
16
...
10 Testing whether a Slot can be Accessed Directly
...
16
...
11 Message-handler Existence
...
16
...
12 Determining if a Class can have Direct Instances
...
16
...
13 Determining if a Class can Satisfy Object Patterns
...
16
...
14 Getting the List of Superclasses for a Class
...
16
...
15 Getting the List of Subclasses for a Class
...
16
...
16 Getting the List of Slots for a Class
...
16
...
17 Getting the List of Message-Handlers for a Class
...
16
...
18 Getting the List of Facets for a Slot
...
16
...
19 Getting the List of Source Classes for a Slot
...
16
...
20 Getting the Primitive Types for a Slot
...
16
...
21 Getting the Cardinality for a Slot
...
16
...
22 Getting the Allowed Values for a Slot
...
16
...
23 Getting the Numeric Range for a Slot
...
16
...
24 Getting the Default Value for a Slot
...
16
...
25 Setting the Defaults Mode for Classes
...
16
...
26 Getting the Defaults Mode for Classes
...
16
...
27 Getting the Allowed Values for a Slot
...
16
...
236
12
...
2
...
236
12
...
2
...
237
12
...
2
...
237
12
...
3 Definstances Functions
...
16
...
1 Getting the List of Definstances
...
16
...
2 Determining the Module in which a Definstances is Defined
...
16
...
239
12
...
4
...
239
12
...
4
...
239
12
...
4
...
239
12
...
4
...
240
12
...
4
...
240
12
...
4
...
240
12
...
4
...
240
12
...
4
...
241
12
...
4
...
241
12
...
4
...
242
12
...
4
...
242
12
...
4
...
242
12
...
244
12
...
1 Getting the List of Defmodules
...
17
...
245
12
...
3 Getting the Current Module
...
18 Sequence Expansion
...
18
...
246
12
...
2 Multifield Expansion Function
...
18
...
247
12
...
4 Getting The Sequence Operator Recognition Behavior
...
18
...
248
Section 13 - Commands
...
1 Environment Commands
...
1
...
249
13
...
2 Loading Constructs From A File without Progress Information
...
1
...
249
13
...
4 Loading a Binary Image
...
1
...
250
13
...
6 Clearing CLIPS
...
1
...
251
13
...
8 Resetting CLIPS
...
1
...
252
13
...
10 Executing Commands From a File Without Replacing Standard Input
...
1
...
252
13
...
12 Calling the Operating System
...
1
...
253
13
...
14 Getting The Auto-Float Dividend Behavior
...
1
...
254
13
...
16 Getting the Dynamic Constraint Checking Behavior
...
1
...
254
13
...
18 Getting the Static Constraint Checking Behavior
...
1
...
254
13
...
255
13
...
1 Generating Trace Files
...
2
...
255
13
...
3 Enabling Watch Items
...
2
...
257
13
...
5 Viewing the Current State of Watch Items
...
3 Deftemplate Commands
...
3
...
258
13
...
2 Displaying the List of Deftemplates
...
3
...
259
13
...
259
13
...
1 Displaying the Fact-List
...
4
...
259
13
...
3 Saving The Fact-List To A File
...
4
...
260
13
...
5 Getting the Duplication Behavior of Facts
...
4
...
261
13
...
262
13
...
1 Displaying the Text of a Deffacts
...
5
...
262
13
...
3 Deleting a Deffacts
...
6 Defrule Commands
...
6
...
263
13
...
2 Displaying the List of Rules
...
6
...
263
13
...
4 Displaying Matches for a Rule
...
6
...
265
13
...
6 Removing a Breakpoint for a Rule
...
6
...
266
13
...
8 Refreshing a Rule
...
6
...
266
13
...
10 Getting the Incremental Reset Behavior
...
6
...
267
13
...
12 Determining the Logical Dependents of a Pattern Entity
...
7 Agenda Commands
...
7
...
268
13
...
2 Running CLIPS
...
7
...
268
13
...
4 Stopping Rule Execution
...
7
...
269
13
...
6 Getting The Current Conflict Resolution Strategy
...
7
...
269
13
...
8 Removing all Module Names from the Focus Stack
...
7
...
270
13
...
10 Getting the Salience Evaluation Behavior
...
7
...
270
13
...
271
13
...
1 Displaying the Text of a Defglobal
...
8
...
271
13
...
3 Deleting a Defglobal
...
8
...
271
13
...
5 Setting the Reset Behavior of Global Variables
...
8
...
272
13
...
272
13
...
1 Displaying the Text of a Deffunction
...
9
...
272
13
...
3 Deleting a Deffunction
...
10 Generic Function Commands
...
10
...
273
xii
Table of Contents
CLIPS Reference Manual
13
...
2 Displaying the Text of a Generic Function Method
...
10
...
273
13
...
4 Displaying the List of Methods for a Generic Function
...
10
...
274
13
...
6 Deleting a Generic Function Method
...
10
...
275
13
...
275
13
...
1 Class Commands
...
11
...
1 Displaying the Text of a Defclass
...
11
...
2 Displaying the List of Defclasses
...
11
...
3 Deleting a Defclass
...
11
...
4 Examining a Class
...
11
...
5 Examining the Class Hierarchy
...
11
...
279
13
...
2
...
279
13
...
2
...
279
13
...
2
...
280
13
...
2
...
280
13
...
3 Definstances Commands
...
11
...
1 Displaying the Text of a Definstances
...
11
...
2 Displaying the List of Definstances
...
11
...
3 Deleting a Definstances
...
11
...
282
13
...
4
...
282
13
...
4
...
283
13
...
4
...
283
13
...
4
...
284
13
...
4
...
284
13
...
4
...
284
13
...
4
...
284
13
...
285
13
...
1 Displaying the Text of a Defmodule
...
12
...
285
13
...
285
13
...
1 Determining the Amount of Memory Used by CLIPS
...
13
...
285
13
...
3 Releasing Memory Used by CLIPS
...
13
...
286
13
...
286
13
...
1 Using the CLIPS Help Facility
...
14
...
287
13
...
288
13
...
1 External Text File Format
...
15
...
290
13
...
2
...
290
13
...
2
...
290
13
...
2
...
292
13
...
2
...
292
13
...
292
13
...
1 Setting the Profiling Report Threshold
...
16
...
293
13
...
3 Resetting Profiling Information
...
16
...
293
13
...
5 Profiling Constructs and User Functions
...
297
A
...
297
A
...
297
A
...
297
Appendix B - Update Release Notes
...
1 Version 6
...
299
B
...
24
...
3 Version 6
...
302
B
...
22
...
5 Version 6
...
303
B
...
2
...
7 Version 6
...
306
B
...
05
...
9 Version 6
...
310
B
...
03
...
11 Version 6
...
312
B
...
01
...
315
Appendix D - Integrated Editor
...
1 Special characters
...
2 Control Commands
...
3 Extended (Control-X) Commands
...
4 Meta Commands (Activated by
or )
...
327 E
...
327 E
...
328 E
...
329 E
...
329 xiv Table of Contents CLIPS Reference Manual E
...
329 Appendix F - CLIPS Warning Messages
...
333 Appendix H - CLIPS BNF
...
379 Appendix J - Bibliography of CLIPS Publications
...
393 CLIPS Basic Programming Guide xv CLIPS Reference Manual License Information Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so
...
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE
...
However, if you derive commercial or monetary benefit from use of the software or just want to show support, please consider making a voluntary payment based on the worth of the software to you as compensation for the time and effort required to develop and maintain CLIPS
...
kagi
...
CLIPS Basic Programming Guide i CLIPS Reference Manual Preface The History of CLIPS The origins of the C Language Integrated Production System (CLIPS) date back to 1984 at NASA’s Johnson Space Center
...
However, despite extensive demonstrations of the potential of expert systems, few of these applications were put into regular use
...
In particular, three problems hindered the use of LISP based expert system tools within NASA: the low availability of LISP on a wide variety of conventional computers, the high cost of state-of-the-art LISP tools and hardware, and the poor integration of LISP with other languages (making embedded applications difficult)
...
Although a number of tool vendors started converting their tools to run in C, the cost of each tool was still very high, most were restricted to a small variety of computers, and the projected availability times were discouraging
...
The prototype version of CLIPS was developed in the spring of 1985 in a little over two months
...
Thus, the syntax of CLIPS was made to very closely resemble the syntax of a subset of the ART expert system tool developed by Inference Corporation
...
The original intent for CLIPS was to gain useful insight and knowledge about the construction of expert system tools and to lay the groundwork for the construction of a replacement tool for the commercial tools currently being used
...
0 demonstrated the feasibility of the project concept
...
Another year of development and internal use went into CLIPS improving its portability, performance, functionality, and supporting documentation
...
0 of CLIPS was made available to groups outside of NASA in the summer of 1986
...
Versions 4
...
1 of CLIPS, released CLIPS Basic Programming Guide iii CLIPS Reference Manual respectively in the summer and fall of 1987, featured greatly improved performance, external language integration, and delivery capabilities
...
2 of CLIPS, released in the summer of 1988, was a complete rewrite of CLIPS for code modularity
...
Version 4
...
Originally, the primary representation methodology in CLIPS was a forward chaining rule language based on the Rete algorithm (hence the Production System part of the CLIPS acronym)
...
0 of CLIPS, released in the spring of 1991, introduced two new programming paradigms: procedural programming (as found in languages such as C and Ada) and object-oriented programming (as found in languages such as the Common Lisp Object System and Smalltalk)
...
Version 5
...
Version 6
...
Version 6
...
Commands to profile the time spent in constructs and user-defined functions were also added
...
2 of CLIPS, released in 2002, added support for multiple environments into which programs can be loaded and improved Windows XP and MacOS development interfaces
...
The development of CLIPS has helped to improve the ability to deliver expert system technology throughout the public and private sectors for a wide range of applications and diverse computing environments
...
CLIPS is now maintained as public domain software by the main program authors who no longer work for NASA
...
CLIPS Version 6
...
3 of CLIPS contains two major enhancements
...
Second, support for integration with languages such as Java and C++ has been improved
...
x releases of CLIPS, refer to appendix B of the Basic Programming Guide and appendix C of the Advanced Programming Guide
...
• The CLIPS Reference Manual which is split into the following parts: • • Volume II - The Advanced Programming Guide, which provides detailed discussions of the more sophisticated features in CLIPS and is intended for people with extensive programming experience who are using CLIPS for advanced applications
...
Volume III - The Interfaces Guide, which provides information on machine-specific interfaces
...
CLIPS Basic Programming Guide v CLIPS Reference Manual Acknowledgements As with any large project, CLIPS is the result of the efforts of numerous people
...
Joseph Giarratano of the University of Houston-Clear Lake, who wrote the CLIPS User’s Guide; and Frank Lopez, who designed and developed CLIPS version 1
...
0 User’s Guide
...
J
...
CLIPS Basic Programming Guide vii CLIPS Reference Manual Section 1 - Introduction This manual is the Basic Programming Guide for CLIPS
...
No previous expert system background is required, although a general understanding of computer languages is assumed
...
Sections 3 through 11 provide additional details regarding the CLIPS programming language on topics such as rules and the CLIPS Object Oriented Programming Language (COOL)
...
Finally, commands typically used from the CLIPS interactive interface are described in section 13
...
More advanced capabilities, such as user-defined functions, embedded applications, etc
...
The Advanced Programming Guide is intended for users who have a complete knowledge of the CLIPS syntax and a programming background
...
CLIPS can be learned and simple expert systems can be built with the information provided in this manual
...
2
...
Embedded applications are discussed in the Advanced Programming Guide
...
The generic CLIPS interface is a simple, interactive, text-oriented, command prompt interface for high portability
...
The interface provides commands for viewing the current state of the system, tracing execution, adding or removing information, and clearing CLIPS
...
1, and X Window environments
...
These interfaces are described in more detail in the Interfaces Guide
...
1
...
When the “CLIPS>” prompt is printed, a command may be entered for evaluation
...
If a function call is entered (see section 2
...
2), that function is evaluated and its return value is printed
...
Entering a construct definition (see section 2
...
3) at the CLIPS prompt creates a new construct of the appropriate type
...
4
...
Local variables can be set at the command prompt using the bind function and retain their value until a reset or clear command is issued
...
Entering a constant (see section 2
...
1) at the top level causes the constant to be printed (which is not very useful)
...
3 10/25/07) CLIPS> (+ 3 4) 7 CLIPS Basic Programming Guide 3 CLIPS Reference Manual CLIPS> (defglobal ?*x* = 3) CLIPS> ?*x* 3 CLIPS> red red CLIPS> (bind ?a 5) 5 CLIPS> (+ ?a 3) 8 CLIPS> (reset) CLIPS> ?a [EVALUATN1] Variable a is unbound FALSE CLIPS> The previous example first called the addition function adding the numbers 3 and 4 to yield the result 7
...
The variable ?*x* was then entered at the prompt and its value of 3 was returned
...
2
...
2 Automated Command Entry and Loading Some operating systems allow additional arguments to be specified to a program when it begins execution
...
The command-line syntax for starting CLIPS and automatically reading commands or loading constructs from a file is as follows: Syntax clips * ::= -f | -f2 | -l For the -f option, is a file that contains CLIPS commands
...
If an exit command is not in the file, CLIPS will enter in its interactive state after executing the commands in the file
...
e
...
The -f command line option is equivalent to interactively entering a batch command as the first command to the CLIPS prompt
...
The commands stored in are immediately executed, but the commands and their return values are not displayed as they would be for a batch command
...
This file will be loaded into the environment
...
2
...
3 Integration with Other Languages When using an expert system, two kinds of integration are important: embedding CLIPS in other systems, and calling external functions from CLIPS
...
Using CLIPS as an embedded application allows the easy integration of CLIPS with existing systems
...
In these situations, CLIPS can be called as a subroutine and information may be passed to and from CLIPS
...
It also may be useful to call external functions while executing a CLIPS construct or from the top-level of the interactive interface
...
The easy addition of external functions allows CLIPS to be extended or customized in almost any way
...
2
...
Plain words or characters, particularly parentheses, are to be typed exactly as they appear
...
Sequences of words enclosed in single-angle brackets (called terms or non-terminal symbols), such as , represent a single entity of the named class of items to be supplied by the user
...
A non-terminal symbol followed by a +, represents one or more entities of the named class of items which must be supplied by the user
...
Vertical and horizontal ellipsis (three dots arranged respectively vertically and horizontally) are also used between non-terminal symbols to indicate the occurrence of one or more entities
...
e
...
Vertical bars indicate a choice between multiple terms
...
The ::= symbol is used to indicate how a non-terminal symbol can be replaced
...
CLIPS Basic Programming Guide 5 CLIPS Reference Manual ::= | A complete BNF listing for CLIPS constructs along with some commonly used replacements for non-terminal symbols are listed in appendix I
...
3 BASIC PROGRAMMING ELEMENTS CLIPS provides three basic elements for writing programs: primitive data types, functions for manipulating data, and constructs for adding to a knowledge base
...
3
...
These types are float, integer, symbol, string, external-address, fact-address, instance-name and instance-address
...
Symbolic information can be represented using symbols and strings
...
), a sign (+ or -), and, optionally, an (e) for exponential notation with its corresponding sign
...
Any number consisting of an optional sign followed by only digits is stored as an integer (represented internally by CLIPS as a C long integer)
...
The number of significant digits will depend on the machine implementation
...
As with any computer language, care should be taken when comparing floating-point values to each other or comparing integers to floating-point values
...
0 -32
...
09 Specifically, integers use the following format: ::= [+ | -] + ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Floating point numbers use the following format: ::= |
...
[exponent]
...
A symbol in CLIPS is any sequence of characters that starts with any printable ASCII character and is followed by zero or more printable ASCII characters
...
The following characters act as delimiters: any non-printable ASCII character (including spaces, tabs, carriage returns, and line feeds), a double quote, opening and closing parentheses “(” and “)”, an ampersand “&”, a vertical bar “|”, a less than “<”, and a tilde “~”
...
3
...
Delimiters may not be included in symbols with the exception of the “<“ character which may be the first character in a symbol
...
These characters are reserved for variables (which are discussed later in this section)
...
e
...
Note that numbers are a special case of symbols (i
...
they satisfy the definition of a symbol, but they are treated as a different data type)
...
A string ends with double quotes
...
A backslash may be embedded by placing two consecutive backslash characters in the string
...
They both contain the same characters, but are of different types
...
An external-address is the address of an external data structure returned by a function (written in a language such as C or Ada) that has been integrated with CLIPS
...
e
...
In the basic version of CLIPS (which has no user defined external functions), it is not possible to create this data type
...
Within CLIPS, the printed representation of an external-address is where XXXXXX is the external-address
...
Facts are referred to by index or address; section 2
...
1 gives more details
...
An instance is an object that is an instantiation or specific example of a class
...
A user-defined class is created using the defclass construct
...
Within the scope of a module (see section 10
...
1), an instance can also be uniquely referred to by name
...
4
...
5
...
3, 2
...
An instance-name is formed by enclosing a symbol within left and right brackets
...
If the CLIPS Object Oriented Language (COOL) is not included in a particular CLIPS configuration, then brackets may be wrapped around symbols
...
An instance-address can only be obtained by binding the return value of a function called instance-address or by binding a variable to an instance matching an object pattern on the LHS of a rule (i
...
, it is not possible to specify an instance-address by typing the value)
...
Within CLIPS, the printed representation of an instance-address is where XXX is the name of the instance
...
The primitive data types are referred to as single-field values
...
A multifield value is a sequence of zero or more single field values
...
Collectively, single and multifield values are referred to as values
...
0 "red" 567) Note that the multifield value (a) is not the same as the single field value a
...
In CLIPS, a variable is a symbolic location that is used to store values
...
2
...
2 Functions A function in CLIPS is a piece of executable code identified by a specific name which returns a useful value or performs a useful side effect (such as displaying information)
...
There are several types of functions
...
System defined functions are those functions that have been defined internally by the CLIPS environment
...
A complete list of system defined functions can be found in appendix J
...
Functions defined in this manner appear and act like other functions, however, instead of being directly executed (as code written in an external language would be) they are interpreted by the CLIPS environment
...
5
...
1 in the context of procedural knowledge representation
...
Generic functions allow different pieces of code to be executed depending upon the arguments passed to the generic function
...
Generic functions are also discussed in section 2
...
2
...
CLIPS Basic Programming Guide 9 CLIPS Reference Manual Function calls in CLIPS use a prefix notation – the arguments to a function always appear after the function name
...
Arguments to a function can be primitive data types, variables, or another function call
...
Some examples of function calls using the addition (+) and multiplication (*) functions are shown following
...
0 2) (* 8 9) 4) (+ 3 (* 2 3 4) 9) (* 3 4)) While a function refers to a piece of executable code identified by a specific name, an expression refers to a function which has its arguments specified (which may or may not be functions calls as well)
...
2
...
3 Constructs Several defining constructs appear in CLIPS: defmodule, defrule, deffacts, deftemplate, defglobal, deffunction, defclass, definstances, defmessage-handler, defgeneric, and defmethod
...
The construct opens with a left parenthesis and closes with a right parenthesis
...
Typically a function call leaves the CLIPS environment unchanged (with some notable exceptions such as resetting or clearing the environment or opening a file)
...
Unlike function calls, constructs never have a return value
...
All constructs (with the exception of defglobal) allow a comment directly following the construct name
...
Everything from the semicolon until the next return character will be ignored by CLIPS
...
Examples of commented code will be provided throughout the reference manual
...
2
...
10 Section 2 - CLIPS Overview CLIPS Reference Manual 2
...
1 Facts Facts are one of the basic high-level forms for representing information in a CLIPS system
...
Facts are the fundamental unit of data used by rules (see section 2
...
1)
...
The number of facts in the fact-list and the amount of information that can be stored in a fact is limited only by the amount of memory in the computer
...
4
...
4
...
Some commands, such as the retract, modify, and duplicate commands, require a fact to be specified
...
Whenever a fact is added (or modified) it is given a unique integer index called a fact-index
...
Whenever a reset or clear command is given, the fact-indices restart at zero
...
A fact-address can be obtained by capturing the return value of commands which return fact addresses (such as assert, modify, and duplicate) or by binding a variable to the fact address of a fact which matches a pattern on the LHS of a rule (see section 5
...
1
...
A fact identifier is a shorthand notation for displaying a fact
...
For example, f-10 refers to the fact with fact-index 10
...
2
...
1
...
The first field of an ordered fact specifies a “relation” that applied to the remaining fields in the ordered fact
...
Some examples of ordered facts are shown following
...
The following symbols are reserved and should not be used as the first field in any fact (ordered or non-ordered): test, and, or, not, declare, logical, object, exists, and forall
...
These symbols may be used as slot names, however, this is not recommended
...
4
...
2 Non-ordered Facts Ordered facts encode information positionally
...
Non-ordered (or deftemplate) facts provide the user with the ability to abstract the structure of a fact by assigning names to each field in the fact
...
The deftemplate construct is analogous to a record or structure definition in programming languages such as Pascal and C
...
Unlike ordered facts, the slots of a deftemplate fact may be constrained by type, value, and numeric range
...
A slot consists of an opening parenthesis followed by the name of the slot, zero or more fields, and a closing parenthesis
...
Deftemplate facts are distinguished from ordered facts by the first field within the fact
...
The first field of a deftemplate fact is followed by a list of zero or more slots
...
Some examples of deftemplate facts are shown following
...
For example the following facts are all identical: (class (teacher "Martha Jones") (#-students 30) (Room "37A")) (class (#-students 30) (teacher "Martha Jones") (Room "37A")) (class (Room "37A") (#-students 30) (teacher "Martha Jones")) In contrast, note that the following ordered fact are not identical
...
In addition to being asserted and retracted, deftemplate facts can also be modified and duplicated (using the modify and duplicate commands)
...
Duplicating a fact creates a new fact identical to the original fact and then changes a set of specified slots within the new fact
...
2
...
1
...
When the CLIPS environment is reset (using the reset command) every fact specified within a deffacts construct in the CLIPS knowledge base is added to the fact-list
...
4
...
Section 2
...
1 explains how to reference instances of user-defined classes
...
A class is a template for common properties and behavior of objects which are instances of that class
...
0 8 (8
...
These two types of objects differ in the way they are referenced, created and deleted as well as how their properties are specified
...
Primitive type objects have no names or slots, and their classes are predefined by CLIPS
...
It is anticipated that primitive types will not be used often in an object-oriented programming (OOP) context; the main reason classes are provided for them is for use in generic functions
...
3
...
5
...
2 and 8 give more detail
...
The properties of an instance of a user-defined class are expressed by a set of slots, which the object obtains from its class
...
For example, the object Rolls-Royce is an instance of the class CAR
...
00
...
Message-handlers and manipulation of objects are described in Section 2
...
2
...
All instances of a user-defined class have the same set of slots, but each instance may have different values for those slots
...
The primary difference between object slots and template (or non-ordered) facts is the notion of inheritance
...
COOL supports multiple inheritance: a class may directly inherit slots and message-handlers from more than one class
...
This is because these classes cannot have slots and usually do not have message-handlers
...
6, and a comprehensive description of the CLIPS Object-Oriented Language (COOL) can be found in Section 9
...
4
...
1 Initial Objects The definstances construct allows a set of a priori or initial knowledge to be specified as a collection of instances of user-defined classes
...
2
...
3 Global Variables The defglobal construct allows variables to be defined which are global in scope throughout the CLIPS environment
...
In contrast, some constructs (such as defrule and deffunction) allow local variables to be defined within the definition of the 14 Section 2 - CLIPS Overview CLIPS Reference Manual construct
...
A CLIPS global variable is similar to global variables found in procedural programming languages such as LISP, C and Ada
...
2
...
These two paradigms are discussed in this section
...
6
...
5
...
Rules are used to represent heuristics, or “rules of thumb”, which specify a set of actions to be performed for a given situation
...
A rule is composed of an antecedent and a consequent
...
The consequent of a rule is also referred to as the then portion or the right-hand side (RHS) of the rule
...
In CLIPS, the conditions of a rule are satisfied based on the existence or non-existence of specified facts in the fact-list or specified instances of user-defined classes in the instance-list
...
Patterns consist of a set of restrictions which are used to determine which facts or objects satisfy the condition specified by the pattern
...
CLIPS provides a mechanism, called the inference engine, which automatically matches patterns against the current state of the fact-list and instance-list and determines which rules are applicable
...
The actions of applicable rules are executed when the CLIPS inference engine is instructed to begin execution of applicable rules
...
The actions of the selected rule are executed (which may affect the list of applicable rules) and then the inference engine selects another rule and executes its actions
...
In many ways, rules can be thought of as IF-THEN statements found in procedural programming languages such as C and Ada
...
In contrast, rules act like WHENEVER-THEN statements
...
In this sense, rules are similar to exception handlers found in languages such as Ada
...
5
...
Deffunctions and generic functions allow the user to define new executable elements to CLIPS that perform a useful side-effect or return a useful value
...
Message-handlers allow the user to define the behavior of objects by specifying their response to messages
...
Defmodules allow a knowledge base to be partitioned
...
5
...
1 Deffunctions Deffunctions allow you to define new functions in CLIPS directly
...
The body of a deffunction is a series of expressions similar to the RHS of a rule that are executed in order by CLIPS when the deffunction is called
...
Calling a deffunction is identical to calling any other function in CLIPS
...
2
...
2
...
However, generic functions are much more powerful because they can be overloaded
...
Generic functions are comprised of multiple components called methods, where each method handles different cases of arguments for the generic function
...
However, the “+” operator will still perform arithmetic addition when passed numbers
...
The return value of a generic function is the evaluation of the last expression in the method executed
...
16 Section 2 - CLIPS Overview CLIPS Reference Manual 2
...
2
...
Object properties are specified in terms of slots obtained from the object’s class; slots are discussed in more detail in Section 2
...
2
...
Objects are manipulated via message-passing
...
How the Rolls-Royce responds to this message will be dictated by the execution of the message-handlers for “start-engine” attached to the CAR class and any of its superclasses
...
Further discussion on message-handlers can be found in Section 2
...
2
...
2
...
Every construct defined must be placed in a module
...
The visibility of facts and instances between modules can be controlled in a similar manner
...
Defmodules are covered comprehensively in Section 10
...
6 CLIPS OBJECT-ORIENTED LANGUAGE This section gives a brief overview of the programming elements of the CLIPS Object-Oriented Language (COOL)
...
This section gives an overview of COOL as a whole, incorporating the elements of both concepts
...
3
...
4
...
5
...
3
...
2
...
1 COOL Deviations from a Pure OOP Paradigm In a pure OOP language, all programming elements are objects which can only be manipulated via messages
...
All objects may be manipulated with messages, except instances of user-defined classes, which must be
...
In CLIPS, you may simply call the “+” function with the two CLIPS Basic Programming Guide 17 CLIPS Reference Manual numbers as arguments, or you can define message-handlers for the NUMBER class which allow you to do it in the purely OOP fashion
...
For example, to print a rule, you call the function ppdefrule; you do not send a message “print” to a rule, since it is not an object
...
6
...
An abstraction is a higher level, more intuitive representation for a complex concept
...
Classes may be described in terms of other classes by use of inheritance
...
Dynamic binding is the ability to defer the selection of which specific message-handlers will be called for a message until run-time
...
The slots and message-handlers of these classes describe the properties and behavior of a new group of objects
...
An instance cannot respond to a message for which it does not have a defined message-handler
...
This process is called multiple inheritance
...
Objects which are instances of this new class can inherit properties (slots) and behavior (message-handlers) from each of the classes in the class precedence list
...
One COOL object can respond to a message in a completely different way than another object; this is polymorphism
...
Dynamic binding is supported in that an object reference (see section 2
...
1) in a send function call is not bound until run-time
...
18 Section 2 - CLIPS Overview CLIPS Reference Manual 2
...
3 Instance-set Queries and Distributed Actions In addition to the ability of rules to directly pattern-match on objects, COOL provides a useful query system for determining, grouping and performing actions on sets of instances of user-defined classes that meet user-defined criteria
...
You can simply use the query system to determine if a particular association set exists, you can save the set for future reference, or you can iterate an action over the set
...
CLIPS Basic Programming Guide 19 CLIPS Reference Manual Section 3 - Deftemplate Construct Ordered facts encode information positionally
...
Non-ordered (or deftemplate) facts provide the user with the ability to abstract the structure of a fact by assigning names to each field found within the fact
...
The deftemplate construct is analogous to a record or structure definition in programming languages such as Pascal and C
...
A deftemplate can not be redefined while it is being used (for example, by a fact or pattern in a rule)
...
CLIPS always enforces the single and multifield definitions of the deftemplate
...
Example (deftemplate object (slot name) (slot location) (slot on-top-of) (slot weight) (multislot contents)) CLIPS Basic Programming Guide 21 CLIPS Reference Manual 3
...
One of two types of default selections can be chosen: default or dynamic-default
...
The specified expressions are evaluated once when the deftemplate is defined and the result is stored with the deftemplate
...
If the keyword ?DERIVE is used for the default value, then a default value is derived from the constraints for the slot (see section 11
...
By default, the default attribute for a slot is (default ?DERIVE)
...
It is an error to assert a template fact without specifying the values for the (default ?NONE) slots
...
The specified expressions are evaluated every time a template fact is asserted, and the result is assigned to the appropriate slot
...
Any number of values may be specified as the default for a multifield slot (as long as the number of values satisfies the cardinality attribute for the slot)
...
CLIPS> (assert (foo (w 3))) CLIPS> (assert (foo (w 4))) CLIPS> (facts) f-0 (foo (w 3) (x nil) (y gen1) (z gen2)) f-1 (foo (w 4) (x nil) (y gen1) (z gen3)) For a total of 2 facts
...
2 SLOT DEFAULT CONSTRAINTS FOR PATTERN-MATCHING Single-field slots that are not specified in a pattern on the LHS of a rule are defaulted to single-field wildcards (?) and multifield slots are defaulted to multifield wildcards ($?)
...
3 SLOT VALUE CONSTRAINT ATTRIBUTES The syntax and functionality of single and multifield constraint attributes are described in detail in Section 11
...
Static checking is performed when constructs or commands using deftemplates slots are being parsed (and the specific deftemplate associated with the construct or command can be immediately determined)
...
Errors for inappropriate values are immediately signaled
...
Static checking is enabled by default
...
Dynamic checking is also supported
...
This dynamic checking is disabled by default
...
If a violation occurs when dynamic checking is being performed, then execution will be halted
...
4 IMPLIED DEFTEMPLATES Asserting or referring to an ordered fact (such as in a LHS pattern) creates an “implied” deftemplate with a single implied multifield slot
...
The implied deftemplate can be manipulated and examined identically to any user defined deftemplate (although it has no pretty print form)
...
CLIPS> (facts) f-0 (foo 1 2 3) For a total of 1 fact
...
Facts asserted through deffacts may be retracted or pattern-matched like any other fact
...
Syntax (deffacts []*) Redefining a currently existing deffacts causes the previous deffacts with the same name to be removed even if the new definition has errors in it
...
Dynamic expressions may be included in a fact by embedding the expression directly within the fact
...
Example (deffacts startup "Refrigerator Status" (refrigerator light on) (refrigerator door open) (refrigerator temp (get-temp))) Upon startup and after a clear command, CLIPS automatically constructs the following deftemplate and deffacts
...
Important Note Once created, the initial-fact deffacts and deftemplate are treated just as any other deffacts or deftemplate defined by the user would be—All of the functions and commands appropriate for these constructs can be applied to them
...
Similarly, the initial-fact fact asserted by the initial-fact deffacts when a reset command is issued, should never be retracted by a program
...
A rule is a collection of conditions and the actions to be taken if the conditions are met
...
Rules execute (or fire) based on the existence or non-existence of facts or instances of user-defined classes
...
Throughout this section, the term pattern entity will be used to refer to either a fact or an instance of a user-defined class
...
1 DEFINING RULES Rules are defined using the defrule construct
...
The LHS is made up of a series of conditional elements (CEs) which typically consist of pattern conditional elements (or just simply patterns) to be matched against pattern entities
...
The RHS contains a list of actions to be performed when the LHS of the rule is satisfied
...
4
...
The arrow (=>) separates the LHS from the RHS
...
Actions are performed sequentially if, and only if, all conditional elements on the LHS are satisfied
...
If no actions are on the RHS, the rule can be activated and fired but nothing will happen
...
This means that CEs in newly defined rules can be satisfied by pattern entities at the time the rule is defined, in addition to pattern entities created after the rule is defined (see sections 13
...
8, 13
...
9, and 13
...
10 for more details)
...
2 BASIC CYCLE OF RULE EXECUTION Once a knowledge base (in the form of rules) is built and the fact-list and instance-list is prepared, CLIPS is ready to execute rules
...
With CLIPS, the program flow does not need to be defined quite so explicitly
...
The basic execution cycle is as follows: a) If the rule firing limit has been reached or there is no current focus, then execution is halted
...
If there are no rules on that agenda, then the current focus is removed from the focus stack and the current focus becomes the next module on the focus stack
...
See sections 5
...
10
...
6, 12
...
7 for information on the focus stack and the current focus
...
The use of the return function on the RHS of a rule may remove the current focus from the focus stack (see sections 10
...
6
...
The number of rules fired is incremented for use with the rule firing limit
...
Activated rules (those rules whose conditions are currently satisfied) are placed on the agenda of the module in which they are defined
...
3, 5
...
10, 13
...
5, and 13
...
6)
...
If the activations item is being watched (see section 13
...
d) If dynamic salience is being used, the salience values for all rules on the agenda are reevaluated (see sections 5
...
10, 13
...
9, and 13
...
10)
...
28 Section 5 - Defrule Construct CLIPS Reference Manual 5
...
Each module has its own agenda
...
When a rule is newly activated, its placement on the agenda is based (in order) on the following factors: a) Newly activated rules are placed above all rules of lower salience and below all rules of higher salience
...
c) If a rule is activated (along with several other rules) by the same assertion or retraction of a fact, and steps a and b are unable to specify an ordering, then the rule is arbitrarily (not randomly) ordered in relation to the other rules with which it was activated
...
Do not depend upon this arbitrary ordering for the proper execution of your rules
...
The default strategy is depth
...
5
...
1 Depth Strategy Newly activated rules are placed above all rules of the same salience
...
However, the position of rule-1 relative to rule-2 and rule-3 relative to rule-4 will be arbitrary
...
3
...
For example, given that fact-a activates rule-1 and rule-2 and fact-b activates rule-3 and rule-4, then if fact-a is asserted before fact-b, rule-1 and rule-2 will be above rule-3 and rule-4 on the agenda
...
5
...
3 Simplicity Strategy Among rules of the same salience, newly activated rules are placed above all activations of rules with equal or higher specificity
...
Each comparison to a constant or previously bound variable adds one to the specificity
...
The boolean functions and, or, and not do not add to the specificity of a rule, but their arguments do
...
For example, the following rule (defrule example (item ?x ?y ?x) (test (and (numberp ?x) (> ?x (+ 10 ?y)) (< ?x 100))) =>) has a specificity of 5
...
The calls to the and and + functions do not add to the specificity of the rule
...
3
...
5
...
5 LEX Strategy Among rules of the same salience, newly activated rules are placed using the OPS5 strategy of the same name
...
Every fact and instance is marked internally with a “time tag” to indicate its relative recency with respect to every other fact and instance in the system
...
An activation with a more recent pattern entities is placed before activations with less recent pattern entities
...
The comparison should continue until one activation’s time tag is greater than the other activation’s corresponding time tag
...
If one activation has more pattern entities than the other activation and the compared time tags are all identical, then the activation with more time tags is placed before the other activation on the agenda
...
Unlike OPS5, the not conditional elements in CLIPS have pseudo time tags which are used by the LEX conflict resolution strategy
...
30 Section 5 - Defrule Construct CLIPS Reference Manual As an example, the following six activations have been listed in their LEX ordering (where the comma at the end of the activation indicates the presence of a not CE)
...
For this example, assume that the time tags and indices are the same
...
rule-6: rule-5: rule-1: rule-2: rule-4: rule-3: f-4,f-1 f-3,f-2,f-1, f-3,f-2,f-1 f-3,f-1 f-2,f-1, f-2,f-1 5
...
6 MEA Strategy Among rules of the same salience, newly activated rules are placed using the OPS5 strategy of the same name
...
An activation thats first pattern’s time tag is greater than another activations first pattern’s time tag is placed before the other activation on the agenda
...
Again, as with the CLIPS LEX strategy, negated patterns have pseudo time tags
...
rule-2: rule-3: rule-6: rule-5: rule-1: rule-4: f-3,f-1 f-2,f-1 f-1,f-4 f-1,f-2,f-3, f-1,f-2,f-3 f-1,f-2, 5
...
7 Random Strategy Each activation is assigned a random number which is used to determine its placement among activations of equal salience
...
Usage Note A conflict resolution strategy is an implicit mechanism for specifying the order in which rules of equal salience should be executed
...
Because the mechanism is implicit, it’s not possible to determine the programmer’s original intent simply by looking at the code
...
] Rather than explicitly indicating that rule A should be executed before rule B, the order of execution is implicitly determined by the order in which facts are asserted and the complexity of the rules
...
Because they require explicit declarations, the preferred mechanisms in CLIPS for ordering the execution of rules are salience and modules
...
Modules allow one to explicitly specify that all of the rules in a particular group (module) should be executed before all of the rules in a different group
...
For example, the following two rules need correction because they can be activated at the same time, but the order in which they execute matters: (defrule rule-1 (factoid a) => (assert (factoid b))) (defrule rule-2 ?f <- (factoid a) (factoid d) => (retract ?f) (assert (factoid c))) Programmers should also be careful to avoid overusing salience
...
It’s rarely necessary to use more than five to ten salience values in a well-designed program
...
The breadth, simplicity, and complexity strategies are provided largely for academic reasons (i
...
the study of 32 Section 5 - Defrule Construct CLIPS Reference Manual conflict resolution strategies)
...
The random strategy is useful for testing
...
Before running a program with the random strategy, first seed the random number generator using the seed function
...
5
...
The LHS of a CLIPS rule is made up of a series of conditional elements (CEs) that must be satisfied for the rule to be placed on the agenda
...
The pattern CE is the most basic and commonly used conditional element
...
The test CE is used to evaluate expressions as part of the pattern-matching process
...
The or CE is used to specify that only one of a group of CEs must be satisfied
...
The exists CE is used to test for the occurence of at least one partial match for a set of CEs
...
Finally, the logical CE allows assertions of facts and the creation of instances on the RHS of a rule to be logically dependent upon pattern entities matching patterns on the LHS of a rule (truth maintenance)
...
4
...
A pattern CE is satisfied by each and every pattern entity that satisfies its constraints
...
A field constraint may consist of only a single literal constraint, however, it may also consist of CLIPS Basic Programming Guide 33 CLIPS Reference Manual several constraints connected together
...
Wildcards are used within pattern CEs to indicate that a single field or group of fields can be matched by anything
...
The first field of any pattern must be a symbol and can not use any other constraints
...
The symbol object is reserved to indicate an object pattern
...
Slot names must also be symbols and cannot contain any other constraints
...
A multifield slot can contain any number of field constraints
...
Section 5
...
1
...
The following constructs are used by the examples
...
0 blue "red") (data 1 blue) (data 1 blue red) (data 1 blue RED) (data 1 blue red 6
...
4
...
1 Literal Constraints The most basic constraint which can be used in a pattern CE is one which precisely defines the exact value that will match a field
...
A literal pattern CE consists entirely of constants such as floats, integers, symbols, strings, and instance names
...
All constraints in a literal pattern must be matched exactly by all fields of a pattern entity
...
) A deftemplate pattern conditional element containing only literals has the following basic syntax: ( ( ) • • • ( )) Example 1 This example utilizes the data-facts deffacts shown in section 5
...
1
...
CLIPS> (facts) f-0 (initial-fact) f-1 (data 1
...
9) For a total of 6 facts
...
4
...
CLIPS> (clear) CLIPS> (defrule Find-Bob (person (name Bob) (age 20)) =>) CLIPS> (defrule Find-Sue (person (age 34) (name Sue)) =>) CLIPS> (reset) CLIPS> (agenda) 0 Find-Sue: f-4 0 Find-Bob: f-2 For a total of 2 activations
...
CLIPS> (age (age (age (age (age 20) 20) 34) 34) 20) (friends)) (friends)) (friends)) (friends)) (friends)) 5
...
1
...
CLIPS interprets these wildcard symbols as standing in place of some part of a pattern entity
...
The multifield wildcard, denoted by a dollar sign followed by a question mark ($?), matches any value in zero or more fields in a pattern entity
...
It is illegal to use a multifield wildcard in a single field slot of a deftemplate or object pattern
...
Similarly, an unspecified multifield slot in a deftemplate/object pattern is matched against an implied multifield-wildcard
...
) where ::= | ? | $? A deftemplate pattern conditional element containing only literals and wildcards has the following basic syntax: ( ( ) • • • ( )) Example 1 This example utilizes the data-facts deffacts shown in section 5
...
1
...
CLIPS> (facts) f-0 (initial-fact) f-1 (data 1
...
9) For a total of 6 facts
...
4
...
CLIPS> (clear) CLIPS> (defrule match-all-persons (person) =>) CLIPS> (reset) CLIPS> (agenda) 0 match-all-persons: f-5 0 match-all-persons: f-4 0 match-all-persons: f-3 0 match-all-persons: f-2 0 match-all-persons: f-1 For a total of 5 activations
...
CLIPS> 20) 20) 34) 34) 20) (friends)) (friends)) (friends)) (friends)) (friends)) Multifield wildcard and literal constraints can be combined to yield some powerful pattern-matching capabilities
...
The use of multifield wildcards should be confined to cases of patterns in which the single-field wildcard cannot create a pattern that satisfies the match required, since the multifield wildcard produces every possible match combination that can be derived from a pattern entity
...
5
...
1
...
The value of the field being replaced may be captured in a variable for comparison, display, or other manipulations
...
Syntax Expanding on the syntax definition given in section 5
...
1
...
Double quotes are not allowed as part of a variable name; i
...
a string cannot be used for a variable name
...
On its first appearance, a variable acts just like a wildcard in that it will bind to any value in the field(s)
...
The binding will only be true within the scope of the rule in which it occurs
...
Bound variables can be passed to external functions
...
In other places (such as the RHS of a rule), the $ in front of a variable indicates that sequence expansion should take place before calling the function
...
All other uses of a multifield variable on the LHS of a rule, however, should use the $
...
Example 1 CLIPS> (clear) CLIPS> (reset) 38 Section 5 - Defrule Construct CLIPS Reference Manual CLIPS> (assert (data 2 blue green) (data 1 blue) (data 1 blue red)) CLIPS> (facts) f-0 (initial-fact) f-1 (data 2 blue green) f-2 (data 1 blue) f-3 (data 1 blue red) For a total of 4 facts
...
9)) CLIPS> (facts) f-0 (initial-fact) f-1 (data 1 blue) f-2 (data 1 blue red) f-3 (data 1 blue red 6
...
CLIPS> (defrule find-data-1 (data ?x $?y ?z) => (printout t "?x = " ?x crlf "?y = " ?y crlf "?z = " ?z crlf "------" crlf)) CLIPS> (run) ?x = 1 ?y = (blue red) ?z = 6
...
This applies to both single- and multifield variables
...
Example 3 CLIPS> (clear) CLIPS> (deffacts data (data red green) (data purple blue) (data purple green) (data red blue green) (data purple blue green) (data purple blue brown)) CLIPS> (defrule find-data-1 (data red ?x) (data purple ?x) =>) CLIPS> (defrule find-data-2 (data red $?x) (data purple $?x) =>) CLIPS> (reset) CLIPS> (facts) f-0 (initial-fact) f-1 (data red green) f-2 (data purple blue) f-3 (data purple green) f-4 (data red blue green) f-5 (data purple blue green) f-6 (data purple blue brown) For a total of 7 facts
...
CLIPS> 5
...
1
...
These are the & (and), | (or), and ~ (not) connective constraints
...
The | constraint is satisfied if either of the two adjoining constraints is satisfied
...
The connective constraints can be combined in almost any manner or number to constrain the value of specific fields while pattern-matching
...
Otherwise, evaluation of multiple constraints can be considered to occur from left to right
...
If the first constraint is a variable followed by an & connective constraint, then the first constraint is treated as a separate constraint which also must be satisified
...
Basic Syntax Connective constraints have the following basic syntax:&
...
| ~ where could be a single-field variable, multifield variable, constant, or connected constraint
...
4
...
3 now gives: ::= ? | $? | ::= | & | | ::= | ~ ::= | | The & constraint typically is used only in conjunction with other constraints or variable bindings
...
If the first term of a connective constraint is the first occurrence of a variable name, then the field will be constrained only by the remaining field constraints
...
If the variable has been bound previously, it is considered an additional constraint along with the remaining field constraints; i
...
, the field must have the same value already bound to the variable and must satisfy the field constraints
...
CLIPS> (agenda) 0 example1-2: f-4 0 example1-3: f-3 0 example1-1: f-1 For a total of 3 activations
...
CLIPS> (agenda) 0 example3-3: f-1,f-4 0 example3-3: f-2,f-4 0 example3-2: f-2,f-4 0 example3-1: f-2,f-3 For a total of 4 activations
...
4
...
5 Predicate Constraints Sometimes it becomes necessary to constrain a field based upon the truth of a given boolean expression
...
The predicate constraint allows a predicate function (one returning the symbol FALSE for unsatisfied and a non-FALSE value for satisfied) to be called during the pattern-matching process
...
If the predicate function returns the symbol FALSE, the constraint is not satisfied
...
Typically, predicate constraints are used in conjunction with a connective constraint and a variable binding (i
...
you have to bind the variable to be tested and then connect it to the predicate constraint)
...
4
...
4 now gives: ::= | | | : Multiple predicate constraints may be used to constrain a single field
...
2)
...
Example 1 CLIPS> (clear) CLIPS> (defrule example-1 (data ?x&:(numberp ?x)) =>) CLIPS> (assert (data 1) (data 2) (data red)) CLIPS> (agenda) 0 example-1: f-1 0 example-1: f-0 For a total of 2 activations
...
CLIPS> Example 3 CLIPS> (clear) CLIPS> (defrule example-3 (data ?x&:(numberp ?x)&:(oddp ?x)) =>) CLIPS> (assert (data 1) (data 2) (data red)) CLIPS> (agenda) 0 example-3: f-0 For a total of 1 activation
...
CLIPS> Example 5 CLIPS> (clear) CLIPS> (defrule example-5 (data $?x&:(> (length$ ?x) =>) CLIPS> (assert (data 1) (data 1 2) (data 1 2 3)) CLIPS> (agenda) 0 example-5: f-2 For a total of 1 activation
...
4
...
6 Return Value Constraints It is possible to use the return value of an external function to constrain the value of a field
...
(This constraint is different from the comparison function which uses the same symbol
...
) The return value must be one of the primitive data types
...
Note that the function is evaluated each time the constraint is checked (not just once)
...
4
...
5 now gives: ::= | | | CLIPS Basic Programming Guide 45 CLIPS Reference Manual : | = Example 1 CLIPS> (clear) CLIPS> (deftemplate data (slot x) (slot y)) CLIPS> (defrule twice (data (x ?x) (y =(* 2 ?x))) =>) CLIPS> (assert (data (x 2) (y 4)) ; f-0 (data (x 3) (y 9))) ; f-1 CLIPS> (agenda) 0 twice: f-0 For a total of 1 activation
...
CLIPS> 5
...
1
...
Patterns can only match objects for which the object’s most specific class is defined before the pattern and which are in scope for the current module
...
Even if a rule is deleted by its RHS, the classes bound to its patterns cannot be changed until after the RHS finishes executing
...
However, when a slot is changed, only those patterns which explicitly match on that slot are affected
...
Changes to non-reactive slots or instances of non-reactive classes (see sections 9
...
2
...
3
...
7) will have no effect on rules
...
Syntax ::= (object *) ::= (is-a ) | (name ) | ( *) The is-a constraint is used for specifying class constraints such as “Is this object a member of class FOO?”
...
The name constraint is used for specifying a specific instance on which to pattern-match
...
Multifield constraints (such as $?) cannot be used with the is-a or name constraints
...
As with deftemplate patterns, slot names for object patterns must be symbols and can not contain any other constraints
...
(defrule class-match-1 (object) =>) (defrule class-match-2 (object (is-a FOO)) =>) (defrule class-match-3 (object (is-a FOO | BAR)) =>) (defrule class-match-4 (object (is-a ?x)) (object (is-a ~?x)) =>) CLIPS Basic Programming Guide 47 CLIPS Reference Manual Rule class-match-1 is satisified by all instances of any reactive class
...
Rule class-match-3 is satisfied by all instances of class FOO or BAR
...
Example 2 The following rules illustrate pattern-matching on various attributes of an object's slots
...
Rule slot-match-2 is satisfied by all instances of reactive classes that contain a reactive single or multifield width slot that is bound to a single value
...
Note that a slot containing a zero length multifield value would satisfy rules slot-match-1 and slot-match-3, but not rule slot-match-2 (because the value's cardinality is zero)
...
(defrule value-match-1 (object (width 10) =>) (defrule value-match-2 (object (width ?x&:(> ?x 20))) =>) (defrule value-match-3 (object (width ?x) (height ?x)) =>) Rule value-match-1 is satisified by all instances of reactive classes that contain a reactive width slot with value 10
...
Rule value-match-3 is satisfied by all instances of reactive classes that contain a reactive width and height slots with the same value
...
4
...
8 Pattern-Addresses Certain RHS actions, such as retract and unmake-instance, operate on an entire pattern CE
...
Collectively, fact-addresses and instance-addresses bound on the LHS of a rule are referred to as pattern-addresses
...
A variable bound to a fact-address or instance-address can be compared to other variables or passed to external functions
...
It is an error to bind a varible to a not CE
...
4
...
Additional capability is provided with the test conditional element
...
As with predicate constraints, the user can compare the variable bindings that already have occurred in any manner
...
g
...
External functions also can be called which compare variables in any way that the user desires
...
User-defined predicate functions must take arguments as defined in the Advanced Programming Guide
...
1)
...
A test CE is evaluated when all proceeding CEs are satisfied
...
In order to cause the reevaluation of a test CE, a pattern entity matching a CE prior to the test CE must be changed
...
In addition, test CEs may also be automatically reordered by CLIPS
...
4
...
Example 1 This example checks to see if the difference between two numbers is greater than or equal to three: CLIPS> (clear) CLIPS> (defrule example-1 (data ?x) (value ?y) (test (>= (abs (- ?y ?x)) 3)) =>) CLIPS> (assert (data 6) (value 9)) CLIPS> (agenda) 0 example-1: f-0,f-1 For a total of 1 activation
...
CLIPS> (clear) CLIPS> (deffunction positive-slope (?x1 ?y1 ?x2 ?y2) (< 0 (/ (- ?y2 ?y1) (- ?x2 ?x1)))) CLIPS> (defrule example-2 (point ?a ?x1 ?y1) (point ?b ?x2 ?y2) 50 Section 5 - Defrule Construct CLIPS Reference Manual (test (> ?b ?a)) (test (positive-slope ?x1 ?y1 ?x2 ?y2)) =>) CLIPS> (assert (point 1 4
...
0) (point 2 5
...
0)) CLIPS> (agenda) 0 example-2: f-0,f-1 For a total of 1 activation
...
In addition, the initial-fact fact asserted or initial-object instance created when a reset command is issued should never be retracted or deleted by a program
...
4
...
If any of the conditional elements inside of the or CE is satisfied, then the or CE is satisfied
...
Note that a rule will be activated for each conditional element with an or CE that is satisfied (assuming the other conditional elements of the rule are also satisfied)
...
The or CE produces the identical effect of writing several rules with similar LHS’s and RHS’s
...
Example (defrule system-fault (error-status unknown) (or (temp high) (valve broken) (pump (status off))) => (printout t "The system has a fault
...
" crlf)) (defrule system-fault (error-status unknown) (valve broken) => (printout t "The system has a fault
...
" crlf)) 5
...
4 And Conditional Element CLIPS assumes that all rules have an implicit and conditional element surrounding the conditional elements on the LHS
...
An explicit and conditional element is provided to allow the mixing of and CEs and or CEs
...
The and CE is satisfied if all of the CEs inside of the explicit and CE are satisfied
...
Any number of conditional elements may be placed within an and CE
...
" crlf)) An and CE that has a test or not CE as its first CE has the pattern (initial-fact) or (initial-object) added as the first CE
...
For example, the following rule (defrule nothing-to-schedule (not (schedule ?)) => (printout t "Nothing to schedule
...
" crlf)) 5
...
5 Not Conditional Element Sometimes the lack of information is meaningful; i
...
, one wishes to fire a rule if a pattern entity or other CE does not exist
...
The not CE is satisfied only if the conditional element contained within it is not satisfied
...
Syntax ::= (not ) Only one CE may be negated at a time
...
Care must be taken when combining not CEs with or and and CEs; the results are not always obvious! The same holds true for variable bindings within a not CE
...
However, variables bound for the first time within a not CE can be used only in that pattern
...
For example, the following conditional element (not (test (> ?time-1 ?time-2))) CLIPS Basic Programming Guide 53 CLIPS Reference Manual is converted to (not (and (initial-fact) (test (> ?time-1 ?time-2)))) Note that it is much simpler just to convert the test CE to the following format: (test (not (> ?time-1 ?time-2))) Important Note Because the not CE can cause the addition of the initial-fact fact pattern or the initial-object instance pattern to a rule, a reset command (which creates the initial-fact fact and the initial-object instance) must be issued for the correct operation of the not CE under all circumstances
...
5
...
6 Exists Conditional Element The exists conditional element provides a mechanism for determining if a group of specified CEs is satisfied by a least one set of pattern entities
...
For example, the following rule (defrule example (exists (a ?x) (b ?x)) =>) is equivalent to the rule below (defrule example (not (not (and (a ?x) (b ?x)))) =>) Because of the way the exists CE is implemented using not CEs, the restrictions which apply to CEs found within not CEs (such as binding a pattern CE to a fact-address) also apply to the CEs found within an exists CE
...
" crlf)) CLIPS> the following commands illustrate that even though there are three facts which can match the second CE in the save-the-day rule, there is only one partial match generated
...
CLIPS> (facts) f-0 (initial-fact) f-1 (goal save-the-day) f-2 (hero (name Death Defying Man) (status unoccupied)) f-3 (hero (name Stupendous Man) (status unoccupied)) f-4 (hero (name Incredible Man) (status unoccupied)) For a total of 5 facts
...
Because the not CE can cause the addition of the initial-fact fact pattern or the initial-object instance pattern to a rule, a reset command (which creates the initial-fact fact and the initial-object instance) must be issued for the correct operation of the exists CE under all circumstances
...
CLIPS Basic Programming Guide 55 CLIPS Reference Manual 5
...
7 Forall Conditional Element The forall conditional element provides a mechanism for determining if a group of specified CEs is satisfied for every occurence of another specified CE
...
For example, the following rule (defrule example (forall (a ?x) (b ?x) (c ?x)) =>) is equivalent to the rule below (defrule example (not (and (a ?x) (not (and (b ?x) (c ?x))))) =>) Because of the way the forall CE is implemented using not CEs, the restrictions which apply to CE found within not CEs (such as binding a pattern CE to a fact-address) also apply to the CEs found within an forall CE
...
CLIPS> (clear) CLIPS> (defrule all-students-passed (forall (student ?name) (reading ?name) (writing ?name) (arithmetic ?name)) => (printout t "All students passed
...
Note that initially the all-students-passed rule is satisfied because there are no students
...
CLIPS> 56 Section 5 - Defrule Construct CLIPS Reference Manual After the (student Bob) fact is asserted, the rule is no longer satisfied since Bob has not passed reading, writing, and arithmetic
...
CLIPS> (assert (reading Bob) (writing Bob)) CLIPS> (agenda) CLIPS> Once Bob has passed arithmetic, the all-students-passed rule is reactivated
...
CLIPS> If a new student is asserted, then the rule is taken off the agenda, since John has not passed reading, writing, and arithmetic
...
CLIPS> (retract 1 5) CLIPS> (agenda) 0 all-students-passed: f-0, For a total of 1 activation
...
Because the not CE can cause the addition of the initial-fact fact pattern or the initial-object instance pattern to a rule, a reset command (which creates the initial-fact fact and the initial-object instance) must be issued for the correct operation of the forall CE under all circumstances
...
CLIPS Basic Programming Guide 57 CLIPS Reference Manual 5
...
8 Logical Conditional Element The logical conditional element provides a truth maintenance capability for pattern entities (facts or instances) created by rules which use the logical CE
...
The pattern entities matching the LHS logical patterns provide logical support to the facts and instance created by the RHS of the rule
...
If any one supporting pattern entities is removed from a group of supporting pattern entities (and there are no other supporting groups), then the pattern entity is removed
...
g
...
Unconditionally supporting a pattern entity removes all logical support (without causing the removal of the pattern entity)
...
Removing a rule that generated logical support for a pattern entity, removes the logical support generated by that rule (but does not cause the removal of the pattern entity if no logical support remains)
...
It may be used in conjunction with the and, or, and not CEs
...
For example, the following rule is legal (defrule ok (logical (a)) (logical (b)) (c) => (assert (d))) whereas the following rules are illegal (defrule not-ok-1 (logical (a)) (b) (logical (c)) => (assert (d))) (defrule not-ok-2 (a) (logical (b)) (logical (c)) => 58 Section 5 - Defrule Construct CLIPS Reference Manual (assert (d))) (defrule not-ok-3 (or (a) (logical (b))) (logical (c)) => (assert (d))) Example Given the following rules, CLIPS> (clear) CLIPS> (defrule rule1 (logical (a)) (logical (b)) (c) => (assert (g) (h))) CLIPS> (defrule rule2 (logical (d)) (logical (e)) (f) => (assert (g) (h))) CLIPS> the following commands illustrate how logical dependencies work
...
(watch rules) CLIPS> (assert (a) (b) (c) (d) (e) (f)) ==> f-0 (a) ==> f-1 (b) ==> f-2 (c) ==> Activation 0 rule1: f-0,f-1,f-2 ==> f-3 (d) ==> f-4 (e) ==> f-5 (f) ==> Activation 0 rule2: f-3,f-3,f-5 CLIPS> (run) FIRE 1 rule2: f-3,f-4,f-5 ; 1st rule adds logical support ==> f-6 (g) ==> f-7 (h) FIRE 2 rule1: f-0,f-1,f-2 ; 2nd rule adds further support CLIPS> (retract 1) <== f-0 (a) ; Removes 1st support for (g) and (h) CLIPS> (assert (h)) ; (h) is unconditionally supported FALSE CLIPS> (retract 3) <== f-3 (d) ; Removes 2nd support for (g) CLIPS Basic Programming Guide 59 CLIPS Reference Manual <== f-6 (g) CLIPS> (unwatch all) CLIPS> ; (g) has no more support As mentioned in section 5
...
1
...
This cannot be accomplished with template facts because a change to a template fact slot actually involves the retraction of the old template fact and the assertion of a new one, whereas a change to an instance slot is done in place
...
CLIPS> (run) ==> f-3 (new-fact) CLIPS> (send [a] put-foo 100) <== f-3 (new-fact) 100 CLIPS> (agenda) 0 match-A-s: [a],f-2 For a total of 1 activation
...
4
...
There are two default patterns used by CLIPS: the initial-fact fact pattern and the initial-object instance pattern
...
4
...
1 Rules Without Any LHS Pattern CEs The initial-fact pattern is added to any rule that has no patterns on its LHS (unless facts have been disabled by configuring CLIPS in which case the initial-object pattern is added to the LHS of the rule)
...
(defrule example-1 (initial-fact) =>) 5
...
9
...
An initial-fact pattern is added if the first pattern CE preceding the CE in question is a fact pattern
...
If there are no preceding pattern CEs, the type of pattern is determined by the succeeding pattern CEs using the same methodology
...
For example, the following rules (defrule example-2 (test (> 80 (startup-value))) =>) (defrule example-3 CLIPS Basic Programming Guide 61 CLIPS Reference Manual (test (> 80 (startup-value))) (object (is-a MACHINE)) =>) (defrule example-4 (machine ?x) (not (and (not (part ?x ?y)) (inventoried ?x))) =>) would be changed as follows
...
4
...
3 Test CEs Following Not CEs Test CEs that immediately follow a not CE are automatically moved by CLIPS behind the first pattern CE that precedes the not CE
...
(defrule example (a ?x) (test (> ?x 5)) (not (b ?x)) =>) 62 Section 5 - Defrule Construct CLIPS Reference Manual 5
...
9
...
For example, the following rule (defrule example (a ?x) (not (or (b ?x) (c ?x))) =>) would be changed as follows
...
4
...
5 Notes About Pattern Addition and Reordering There are several points which should be noted about the addition and reordering of pattern CEs: 1) The entire LHS of a rule is considered to be within an implicit and CE; 2) The conversion of the forall and exists CEs to equivalent not and and CEs is performed before patterns are added to the LHS of a rule; 3) In general, it is not very useful to have a test CE as the first CE within an and CE; and 4) The output of commands such as the matches command display information for the CEs that are actually added to the LHS of a rule and, because of reordering and implicit additions, may not reflect the rule exactly as defined by the user
...
5
...
10 Declaring Rule Properties This feature allows the properties or characteristics of a rule to be defined
...
A rule may only have one declare statement and it must appear before the first conditional element on the LHS (as shown in section 5
...
Syntax ::= (declare +) CLIPS Basic Programming Guide 63 CLIPS Reference Manual ::= (salience ) | (auto-focus ) ::= TRUE | FALSE 5
...
10
...
When multiple rules are in the agenda, the rule with the highest priority will fire first
...
Salience expressions may freely reference global variables and other functions (however, you should avoid using functions with side-effects)
...
Example (defrule test-1 (declare (salience 99)) (fire test-1) => (printout t "Rule test-1 firing
...
" crlf)) Salience values can be evaluated at one of three times: when a rule is defined, when a rule is activated, and every cycle of execution (the latter two situations are referred to as dynamic salience)
...
The set-salience-evaluation command can be used to change this behavior
...
e
...
Usage Note Despite the large number of possible values, with good design there’s rarely a need for more than five salience values in a simple program and ten salience values in a complex program
...
4
...
2 The Auto-Focus Rule Property The auto-focus rule property allows an automatic focus command to be executed whenever a rule becomes activated
...
If the auto-focus property for a rule is FALSE, then no action is taken when the rule is activated
...
Example (defrule VIOLATIONS::bad-age (declare (auto-focus TRUE)) (person (name ?name) (age ?x&:(< ?x 0))) => (printout t ?name " has a bad age value
...
Global variables can be accessed as part of the pattern-matching process, but changing them does not invoke the pattern-matching process
...
Global variables are reset to their original value when the reset command is performed or when bind is called for the global with no values
...
Global variables can be removed by using the clear command or the undefglobal command
...
2), then an informational message will be displayed each time the value of a global variable is changed
...
The optional indicates the module in which the defglobals will be defined
...
If a variable was defined in a previous defglobal construct, its value will be replaced by the value found in the new defglobal construct
...
Commands that operate on defglobals such as ppdefglobal and undefglobal expect the symbolic name of the global without the astericks (e
...
use the symbol max when you want to refer to the global variable ?*max*)
...
Global variables may not be used as a parameter variable for a deffunction, defmethod, or message-handler
...
Therefore, the following rule is illegal (defrule example (fact ?*x*) =>) The following rule, however, is legal
...
For example, if ?*x* is 4 and the fact (fact 3) is added, then the rule is not satisfied
...
Example (defglobal ?*x* = 3 ?*y* = ?*x* ?*z* = (+ ?*x* ?*y*) ?*q* = (create$ a b c)) Usage Note The inappropriate use of globals within rules is quite often the first resort of beginning programmers who have reached an impasse in developing a program because they do not fully understand how rules and pattern-matching work
...
Facts and instances are the primary mechanism that should be used to pass information from one rule to another specifically because they do invoke pattern-matching
...
Since a change to a global variable does not trigger pattern-matching, it is possible for a rule to remain activated based on a past value of a global variable which is undesirable in most situations
...
Just as a rule can access any global variable that’s visible (i
...
it hasn’t been hidden through the use of modules), so too can it access any fact or instance belonging to a deftemplate or defclass that’s visible
...
In the case of an instance, pattern-matching and instance-set query functions can also be used, and in addition an instance can be directly referenced by name just as a global variable can
...
First attempts at rules to accomplish this task often loop endlessly because of rules inadvertently retriggered by changes
...
A non-destructive approach makes use of temporary facts created by a helper rule: (defrule add-factoid-helper (factoid ?data) => (assert (temp-factoid ?data))) (defrule add-factoid ?f <- (temp-factoid ?data) ?c <- (collection $?collection) => (retract ?f ?c) (assert (collection ?collection ?data))) It certainly looks simpler, however, to use a global variable to collect the slot values: (defglobal ?*collection* = (create$)) (defrule add-factoid (factoid ?data) => (bind ?*collection* (create$ ?*collection* ?data))) Again, the drawback to this approach is that changes to a global variable do not trigger pattern-matching, so in spite of its greater complexity the fact-based approach is still preferable
...
If there are 1000 factoid facts, the add-factoid/add-factoid-helper rules will each fire 1000 times generating and retracting 2000 facts
...
Typically if other rules are waiting for the finished result of the collection, they would need to have lower salience so that they aren’t fired for the intermediate results: (defrule print-factoids (declare (salience -10)) (collection $?data) => (printout t "The collected data is " ?data crlf)) If the factoid facts are collected by a single rule firing, then the salience declaration is unnecessary
...
It is a rare situation where a global variable is required in order to solve a problem
...
In the following rule, a global variable is used to determine whether additional debugging information is printed: (defglobal ?*debug-print* = nil) (defrule rule-debug ?f <- (info ?info) => (retract ?f) (printout ?*debug-print* "Retracting info " ?info crlf)) If ?*debug-print* is set to nil, then the printout statement will not display any information
...
Because ?*debug-print* is a global, it can be changed interactively without causing rules to be reactivated
...
It’s possible, but a little more verbose, to achieve this same functionality using instances rather than global variables: (defclass DEBUG-INFO (is-a USER) (slot debug-print)) (definstances debug ([debug-info] of DEBUG-INFO (debug-print nil))) (defrule rule-debug ?f <- (info ?info) => (retract ?f) (printout (send [debug-info] get-debug-print) "Retracting info " ?info crlf)) Unlike fact slots, changes to a slot of an instance won’t trigger pattern matching in a rule unless the slot is specified on the LHS of that rule, thus you have explicit control over whether an instance slot triggers pattern-matching
...
CLIPS Basic Programming Guide 71 CLIPS Reference Manual Section 7 - Deffunction Construct With the deffunction construct, new functions may be defined directly in CLIPS
...
3
...
The only differences between user-defined external functions and deffunctions are that deffunctions are written in CLIPS and executed by CLIPS interpretively and user-defined external functions are written in an external language, such as C, and executed by CLIPS directly
...
A deffunction is comprised of five elements: 1) a name, 2) an optional comment, 3) a list of zero or more required parameters, 4) an optional wildcard parameter to handle a variable number of arguments and 5) a sequence of actions, or expressions, which will be executed in order when the deffunction is called
...
In particular, a deffunction cannot be overloaded like a system function (see section 8 for more detail)
...
The only exception is a self recursive deffunction
...
The regular parameters specify the minimum number of arguments that must be passed to the deffunction
...
If a wildcard parameter is present, the deffunction may be passed any number of arguments greater than or equal to the minimum
...
All arguments to a deffunction that do not correspond to a regular parameter are grouped into a multifield value that can be referenced by the wildcard parameter
...
Example CLIPS> (clear) CLIPS> (deffunction print-args (?a ?b $?c) (printout t ?a " " ?b " and " (length ?c) " extras: " ?c crlf)) CLIPS Basic Programming Guide 73 CLIPS Reference Manual CLIPS> (print-args 1 2) 1 2 and 0 extras: () CLIPS> (print-args a b c d) a b and 2 extras: (c d) CLIPS> When a deffunction is called, its actions are executed in order
...
If a deffunction has no actions, its return value is the symbol FALSE
...
Deffunctions may be self and mutually recursive
...
Example (deffunction factorial (?a) (if (or (not (integerp ?a)) (< ?a 0)) then (printout t "Factorial Error!" crlf) else (if (= ?a 0) then 1 else (* ?a (factorial (- ?a 1)))))) Mutual recursion between two deffunctions requires a forward declaration of one of the deffunctions
...
In the following example, the deffunction foo is forward declared so that it may be called by the deffunction bar
...
Example (deffunction foo ()) (deffunction bar () (foo)) (deffunction foo () (bar)) Care should be taken with recursive deffunctions; too many levels of recursion can lead to an overflow of stack memory (especially on PC-type machines)
...
Generic functions are similar to deffunctions because they can be used to define new procedural code directly in CLIPS, and they can be called like any other function (see sections 2
...
2 and 7)
...
For example, a “+” operator could be defined which performs concatenation for strings but still performs arithmetic addition for numbers
...
A generic function which has more than one method is said to be overloaded
...
For example, an overloading of the “+” operator to handle strings consists of two methods: 1) an implicit one which is the system function handling numerical addition, and 2) an explicit (user-defined) one handling string concatenation
...
Deffunctions are only provided so that basic new functions can be added directly in CLIPS without the concerns of overloading
...
In most cases, generic function methods are not called directly (the function call-specific-method described in section 12
...
8 can be used to do so, however)
...
This process is termed the generic dispatch
...
1 NOTE ON THE USE OF THE TERM METHOD Most OOP systems support procedural behavior of objects either through message-passing (e
...
Smalltalk) or by generic functions (e
...
CLOS)
...
A generic function may examine the classes of its arguments but must still use messages within the bodies of its methods to manipulate any arguments which are instances of user-defined classes
...
The fact that CLIPS supports both mechanisms leads to a confusion in terminology
...
In systems which support generic functions only, however, the term method is used to denote the different implementations of a generic function for different sets of argument restrictions
...
Thus in CLIPS, message-handlers denote the different implementations of a message for different classes, and methods denote the different implementations of a generic function for different sets of argument restrictions
...
2 PERFORMANCE PENALTY OF GENERIC FUNCTIONS A call to a generic function is computationally more expensive than a call to a system function, user-defined external function or deffunction
...
A performance penalty of 15%-20% is not unexpected
...
Also, generic functions should always have at least two methods
...
A system or user-defined external function which is not overloaded will, of course, execute as quickly as ever, since the generic dispatch is unnecessary
...
3 ORDER DEPENDENCE OF GENERIC FUNCTION DEFINITIONS If a construct which uses a system or user-defined external function is loaded before a generic function which uses that function as an implicit method, all calls to that function from that construct will bypass the generic dispatch
...
However, similar rules defined after the generic function will use the generic dispatch
...
4 DEFINING A NEW GENERIC FUNCTION A generic function is comprised of a header (similar to a forward declaration) and zero or more methods
...
A method is comprised of six elements: 1) a name (which identifies to which generic function the method belongs), 2) an optional index, 3) an optional comment , 4) a set of parameter restrictions, 5) an optional wildcard parameter restriction to handle a variable number of arguments and 6) a sequence of actions, or expressions, which will be executed in order when the method is called
...
The defgeneric construct is used to specify the generic function header, and the defmethod construct is used for each of the generic function’s methods
...
The only exception is a self recursive generic function
...
4
...
In order to reference a generic function in other constructs before any of its methods are declared, an explicit header is necessary
...
For example, two generic functions whose methods mutually call the other generic function (mutually recursive generic functions) would require explicit headers
...
4
...
Each method for a generic function is assigned a unique integer index within the group of all methods for that generic function
...
However, any difference in parameter restrictions will cause the new method to be defined in addition to the older method
...
However, the parameter restrictions of the new method must not match that of another method with a different index
...
The index assigned by CLIPS can be determined with the list-defmethods command (see section 13
...
4)
...
4
...
A parameter restriction is applied to a generic function argument at run-time to determine if a particular method will accept the argument
...
A type restriction constrains the classes of arguments that will be accepted for a parameter
...
The complexity of parameter restrictions directly affects the speed of the generic dispatch
...
However, each method of a generic function must have parameter restrictions that make it distinguishable from all of the other methods so that the generic dispatch can tell which one to call at run-time
...
5
...
A type restriction allows the user to specify a list of types (or classes), one of which must match (or be a superclass of) the class of the generic function argument
...
Section 9 describes each of these system classes
...
Generic functions which use only the first group of types in their methods will work the same whether COOL is installed or not
...
5
...
Redundant classes are not allowed in restriction class lists
...
Example (defmethod foo ((?a INTEGER NUMBER))) If the type restriction (if any) is satisfied for an argument, then a query restriction (if any) will be applied
...
CLIPS evaluates this expression, and if it evaluates to anything but the symbol FALSE, the restriction is considered satisfied
...
Since parameter restrictions are examined from left to right, queries which involve multiple parameters should be included with the rightmost parameter
...
For example, the following method delays evaluation of the query restriction until the classes of both arguments have been verified
...
If all generic function arguments are accepted by a method’s restrictions, the method itself is deemed applicable to the set of arguments
...
Method precedence is used for this purpose and will be discussed in section 8
...
2
...
The second call executes the explicit method for string concatenation, since there are two arguments and they are both strings
...
CLIPS> (clear) CLIPS> (defmethod + ((?a STRING) (?b STRING)) (str-cat ?a ?b)) CLIPS> (+ 1 2) 3 CLIPS> (+ "foo" "bar") "foobar" CLIPS> (+ "foo" "bar" "woz") [GENRCEXE1] No applicable methods for +
...
4
...
The regular parameters specify the minimum number of arguments that must be passed to the method
...
If a wildcard parameter is present, the method may be passed any number of arguments greater than or equal to the minimum
...
Method arguments which do not correspond to a regular parameter can be grouped into a multifield value that can be referenced by the wildcard parameter within the body of the method
...
If multifield values are passed as extra arguments, they will all be merged into one multifield value referenced by the wildcard parameter
...
Type and query restrictions can be applied to arguments grouped in the wildcard parameter similarly to regular parameters
...
However, expressions involving the wildcard parameter variable may be used in the query
...
This variable is only in scope within the query and has no meaning in the body of the method
...
Thus in the following example, the > and length$ functions are actually called three times, since there are three arguments: Example CLIPS> (defmethod foo (($?any (> (length$ ?any) 2))) yes) CLIPS> (foo 1 red 3) yes CLIPS> In addition, a query restriction will never be examined if there are no arguments in the wildcard parameter range
...
In cases like this where the type is irrelevant to the test, the query restriction can be attached to a regular parameter instead to improve performance (see section 8
...
1)
...
FALSE CLIPS> 80 Section 8 - Generic Functions CLIPS Reference Manual This approach should not be used if the types of the arguments grouped by the wildcard must be verified prior to safely evaluating the query restriction
...
5 GENERIC DISPATCH When a generic function is called, CLIPS selects the method for that generic function with highest precedence for which parameter restrictions are satisfied by the arguments
...
This entire process is referred to as the generic dispatch
...
METHOD STEP : Are there any uncalled methods? YES: Call the next most specific method
...
Else go to DONE
...
NO: ERROR: There are no applicable methods for this generic function
...
The solid arrows indicate automatic control transfer by the generic dispatch
...
8
...
1 Applicability of Methods Summary An explicit (user-defined) method is applicable to a generic function call if the following three conditions are met: 1) its name matches that of the generic function, 2) it accepts at least as many arguments as were passed to the generic function, and 3) every argument of the generic function satisfies the corresponding parameter restriction (if any) of the method
...
As soon as one restriction is not satisfied, the method is abandoned, and the rest of the restrictions (if any) are not examined
...
This implicit method is derived from the argument restriction string for the external DefineFunction2 call defining that function to CLIPS (see the Advanced Programming Guide)
...
The specification of this implicit method can be examined with the CLIPS Basic Programming Guide 81 CLIPS Reference Manual list-defmethods or get-method-restrictions functions
...
For example, (+ +) would yield the following method for the ‘+’ function: (defmethod + ((?first NUMBER) (?second NUMBER) ($?rest NUMBER))
...
The following system functions cannot be overloaded, and CLIPS will generate an error if an attempt is made to do so
...
5
...
Method precedence is determined when a method is defined; the list-defmethods function can be used to examine the precedence of methods for a generic function (see section 13
...
The precedence between two methods is determined by comparing their parameter restrictions
...
For example, a method which demands an integer for a particular argument will have higher precedence than a method which only demands a number
...
1) ) The parameter restrictions of both methods are positionally compared from left to right
...
The comparisons between these pairs of parameter restrictions from the two methods determine the overall precedence between the two methods
...
The following rules are applied in order to a parameter pair; the result of the first rule which establishes precedence is taken
...
1b) The most specific type restriction on a particular parameter has priority
...
1c) A parameter with a query restriction has priority over one that does not
...
3) A method without a wildcard parameter has precedence over one that does 4) A method defined before another one has priority
...
Since all precedence determination is done when the new method is defined, and the actual class of the generic function argument will not be known until run-time, arbitrary (but deterministic) rules are needed for determining the precedence between two class lists
...
g
...
The first pair containing a class and its superclass specify precedence
...
If no class pairs specify precedence, then the shorter class list has priority
...
Example 1 ; The system operator '+' is an implicit method ; #1 ; Its definition provided by the system is: ; (defmethod + ((?a NUMBER) (?b NUMBER) ($?rest NUMBER))) (defmethod (defmethod (defmethod (defmethod + + + + ((?a NUMBER) (?b INTEGER))) ((?a INTEGER) (?b INTEGER))) ((?a INTEGER) (?b NUMBER))) ((?a NUMBER) (?b NUMBER) ($?rest PRIMITIVE))) (defmethod + ((?a NUMBER) (?b INTEGER (> ?b 2)))) (defmethod + ((?a INTEGER (> ?a 2)) (?b INTEGER (> ?b 3)))) CLIPS Basic Programming Guide ; #2 ; #3 ; #4 ; #5 ; #6 ; #7 83 CLIPS Reference Manual (defmethod + ((?a INTEGER (> ?a 2)) (?b NUMBER))) ; #8 The precedence would be: #7,#8,#3,#4,#6,#2,#1,#5
...
Group A has precedence over group B because parameters with query restrictions have priority over those that do not
...
Thus, the ordering so far is: (#7,#8),(#3,#4),(#1,#2,#5,#6)
...
The next step in determining precedence between these methods considers their restrictions on the second parameter
...
#3 has priority over #4 because INTEGER is a subclass of NUMBER
...
#6 has priority over #2 because it has a query restriction and #2 does not
...
The restriction on the wildcard argument yields that #1 (the system function implicit method) has priority over #5 since NUMBER is a sublclass of PRIMITIVE
...
Example 2 (defmethod foo ((?a NUMBER STRING))) (defmethod foo ((?a INTEGER LEXEME))) ; #1 ; #2 The precedence would be #2,#1
...
Example 3 (defmethod foo ((?a MULTIFIELD STRING))) (defmethod foo ((?a LEXEME))) ; #1 ; #2 The precedence would be #2,#1 because the classes of the first pair in the type restriction (MULTIFIELD/LEXEME) are unrelated and #2 has fewer classes in its class list
...
Thus, the precedence is taken from the order of definition: #1,#2
...
5
...
Normally, only one method or system function will be applicable to a particular generic function call
...
Letting the generic dispatch automatically handle the methods in this manner is called the declarative technique, for the declarations of the method restrictions dictate which method gets executed in specific circumstances
...
15
...
15
...
This is called the imperative technique, since the method execution itself plays a role in the generic dispatch
...
In most circumstances, only one piece of code should need to be executed for a particular set of arguments
...
15
...
5
...
The lack of any applicable methods for a set of generic function arguments is considered a method execution error
...
5
...
Each applicable method that is executed can choose to ignore or capture the return value of any method that it is shadowing
...
CLIPS Basic Programming Guide 85 CLIPS Reference Manual Section 9 - CLIPS Object Oriented Language (COOL) This section provides the comprehensive details of the CLIPS Object-Oriented Language (COOL)
...
3
...
4
...
5
...
3 explain object references and structure
...
6 gives a high level overview of COOL
...
9
...
For example, object encapsulation concepts are similar to those in Smalltalk, and the Common Lisp Object System (CLOS) provides the basis for multiple inheritance rules
...
Section 8
...
9
...
The user may not delete or modify any of these classes
...
OBJECT USER PRIMITIVE NUMBER INTEGER INSTANCE INITIAL-OBJECT ADDRESS MULTIFIELD FLOAT INSTANCE-NAME LEXEME SYMBOL INSTANCE-ADDRESS FACT-ADDRESS STRING EXTERNAL-ADDRESS All of these system classes except INITIAL-OBJECT are abstract classes, which means that their only use is for inheritance (direct instances of this class are illegal)
...
However, the user may explicitly attach message-handlers to all of the system classes except for INSTANCE, INSTANCE-ADDRESS and INSTANCE-NAME
...
All user-defined classes should (but are not required to) inherit directly or indirectly from the class USER, since this class has all of the standard system CLIPS Basic Programming Guide 87 CLIPS Reference Manual message-handlers, such as initialization and deletion, attached to it
...
4 describes these system message-handlers
...
However, the three primitive system classes INSTANCE, INSTANCE-ADDRESS and INSTANCE-NAME are provided strictly for use in methods (particularly in forming implicit methods for overloaded system functions - see section 8
...
1) and as such cannot have subclasses or message-handlers attached to them
...
This system class is concrete and reactive to pattern-matching on the LHS of rules but is in other respects exactly like the system class USER
...
4
...
9
...
A defclass consists of five 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, 4) a specifier saying whether or not instances of this class can match object patterns on the LHS of rules and 5) a list of slots specific to the new class
...
Any slots explicitly given in the defclass override those gotten from inheritance
...
3
...
Facets (see section 9
...
3) further describe slots
...
Syntax Defaults are outlined
...
An error will occur if instances of the class or any of its subclasses exist
...
3
...
Every user-defined class must have at least one direct superclass, i
...
at least one class must appear in the is-a portion of the defclass
...
COOL examines the direct superclass list for a new class to establish a linear ordering called the class precedence list
...
The word precedence implies that slots and message-handlers of a class in the list override CLIPS Basic Programming Guide 89 CLIPS Reference Manual conflicting definitions of another class found later in the list
...
All class precedence lists will terminate in the system class OBJECT, and most (if not all) class precedence lists for user-defined classes will terminate in the system classes USER and OBJECT
...
11
...
4)
...
3
...
1 Multiple Inheritance Rules COOL uses the inheritance hierarchy of the direct superclasses to determine the class precedence list for a new class
...
2) A class specifies the precedence between its direct superclasses
...
This heuristic attempts to preserve “family trees” to the greatest extent possible
...
There are other orderings which would satisfy the rules (such as child mother father paternal-grandfather maternal-grandmother paternal-grandmother maternal-grandfather), but COOL chooses the one which keeps the family trees together as much as possible
...
The class precedence list for A is: A USER OBJECT
...
The class precedence list for B is: B USER OBJECT
...
The class precedence list for C is: C A B USER OBJECT
...
The class precedence list for D is: D B A USER OBJECT
...
However, C is a subclass of A and cannot succeed A in a precedence list without violating rule #1
...
Example 6 (defclass E (is-a C A)) Specifying that E inherits from A is extraneous, since C inherits from A
...
The class precedence list for E is: E C A B USER OBJECT
...
The class precedence list for F is: F C A B USER OBJECT
...
Example 8 (defclass G (is-a C D)) This is an error, for it violates rule #2
...
Example 9 (defclass H (is-a A)) (defclass I (is-a B)) (defclass J (is-a H I A B)) The respective class precedence lists of H and I are: H A USER OBJECT and I B USER OBJECT
...
COOL would normally pick the first list since it preserves the family trees (H A and I B) to the greatest extent possible
...
Usage Note For most practical applications of multiple inheritance, the order in which the superclasses are specified should not matter
...
9
...
2 Class Specifiers 9
...
2
...
A concrete class can have direct instances
...
If the abstract or concrete descriptor for a class is not specified, it is determined by inheritance
...
Thus a class which inherits from USER will be concrete if no role is specified
...
3
...
2 Reactive and Non-Reactive Classes Objects of a reactive class can match object patterns in a rule
...
An abstract class cannot be reactive
...
For the purpose of pattern-match inheritance, system defined classes behave as reactive classes unless the inheriting class is abstract
...
3
...
Each instance has a copy of the set of slots specified by the immediate class as well as any obtained from inheritance
...
The name of a slot may be any symbol with the exception of the keywords is-a and name which are reserved for use in object patterns
...
A class is more specific than its superclasses
...
3
...
5)
...
3
...
6)
...
Instances of A will have two slots: fooA and barA
...
Instances of B will have four slots: fooB, barB, fooA and barA
...
Facets describe various features of a slot that hold true for all objects which have the slot: default value, storage, access, inheritance propagation, source of other facets, pattern-matching reactivity, visibility to subclass message-handlers, the automatic creation of message-handlers to access the slot, the name of the message to send to set the slot and constraint information
...
3
...
3)
...
3
...
1 Slot Field Type A slot can hold either a single-field or multifield value
...
The keyword multislot specifies that a slot can hold a multifield value comprised of zero or more fields, and the keywords slot or single-slot specify that the slot can hold one value
...
COOL also provides functions for setting multifield slots, such as slot-insert$ (see section 12
...
4
...
2)
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (multislot foo (create-accessor read) (default abc def ghi))) CLIPS> (make-instance a of A) [a] CLIPS> (nth$ 2 (send [a] get-foo)) def CLIPS> 9
...
3
...
By default, a slot will have a default value which is derived from the slot’s constraint facets (see sections 9
...
3
...
5)
...
6
...
The default facet is a static default: the specified expression is evaluated once when the class is defined, and the result is stored with the class
...
If the keyword ?DERIVE is used for the default value, then a default value is derived from the constraints for the slot (see section 11
...
By default, the default attribute for a slot is (default ?DERIVE)
...
Using this keyword causes make-instance to require a slot-override for that slot when an instance is created
...
0, a slot now has a default even if one is not explicitly specified (unlike CLIPS 5
...
This could cause different behavior for CLIPS 5
...
The ?NONE keyword can be used to recover the original behavior for classes
...
Example CLIPS> (clear) CLIPS> (setgen 1) 1 CLIPS> (defclass A (is-a USER) (role concrete) (slot foo (default-dynamic (gensym)) (create-accessor read))) CLIPS> (make-instance a1 of A) [a1] CLIPS> (make-instance a2 of A) [a2] CLIPS> (send [a1] get-foo) gen1 CLIPS> (send [a2] get-foo) gen2 CLIPS> 9
...
3
...
The local facet specifies that the value be stored with the instance, and this is the default
...
If the slot value is locally stored, then each instance can have a separate value for the slot
...
Anytime the value is changed for a shared slot, it will be changed for all instances with that slot
...
Any changes to a shared slot will cause pattern-matching for rules to be updated for all reactive instances containing that slot
...
3
...
4 Access Facet There are three types of access facets which can be specified for a slot: read-write, read-only, and initialize-only
...
The read-only facet says the slot can only be read; the only way to set this slot is with default facets in the class definition
...
6
...
4)
...
4)
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (slot foo (create-accessor write) (access read-write)) (slot bar (access read-only) (default abc)) (slot woz (create-accessor write) (access initialize-only))) CLIPS> (defmessage-handler A put-bar (?value) (dynamic-put (sym-cat bar) ?value)) CLIPS> (make-instance a of A (bar 34)) [MSGFUN3] bar slot in [a] of A: write access denied
...
[PRCCODE4] Execution halted during the actions of message-handler put-bar primary in class A FALSE CLIPS> (send [a] put-woz 1) [MSGFUN3] woz slot in [a] of A: write access denied
...
3
...
5 Inheritance Propagation Facet An inherit facet says that a slot in a class can be given to instances of other classes that inherit from the first class
...
The no-inherit facet says that only direct instances of this class will get the slot
...
3
...
6 Source Facet When obtaining slots from the class precedence list during instance creation, the default behavior is to take the facets from the most specific class which gives the slot and give default values to any unspecified facets
...
The composite facet causes facets which are not explicitly specified by the most specific class to be taken from the next most specific class
...
Note that even though facets may be taken from superclasses, the slot is still considered to reside in the new class for purposes of visibility (see section 9
...
3
...
One good example of a use of this feature is to pick up a slot definition and change only its default value for a new derived class
...
Reactive: direct instances of this class can match defrule patterns
...
+oo] CRD:[0
...
3
...
7 Pattern-Match Reactivity Facet Normally, any change to a slot of an instance will be considered as a change to the instance for purposes of pattern-matching
...
The reactive facet specifies that changes to a slot trigger pattern-matching, and this is the default
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (pattern-match reactive) (slot foo (create-accessor write) (pattern-match non-reactive))) CLIPS> (defclass B (is-a USER) (role concrete) (pattern-match reactive) (slot foo (create-accessor write) (pattern-match reactive))) CLIPS> (defrule Create ?ins<-(object (is-a A | B)) => (printout t "Create " (instance-name ?ins) crlf)) CLIPS> (defrule Foo-Access ?ins<-(object (is-a A | B) (foo ?)) => 98 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual (printout t "Foo-Access " (instance-name ?ins) crlf)) CLIPS> (make-instance a of A) [a] CLIPS> (make-instance b of B) [b] CLIPS> (run) Create [b] Foo-Access [b] Create [a] CLIPS> (send [a] put-foo 1) 1 CLIPS> (send [b] put-foo 1) 1 CLIPS> (run) Foo-Access [b] CLIPS> 9
...
3
...
However, it is possible to allow message-handlers attached to superclasses or subclasses which inherit the slot to directly access the slot as well
...
Declaring the visibility facet to be public specifies that the message-handlers and subclasses which inherit the slot and superclasses may also directly access the slot
...
ERROR: (defmessage-handler MAIN::B get-foo () ?self:foo ) CLIPS> CLIPS Basic Programming Guide 99 CLIPS Reference Manual 9
...
3
...
By default, implicit slot-accessor message-handlers are created for every slot
...
If the value ?NONE is specified for the facet, no message-handlers are created
...
If accessors are required that do not use static slot references (see sections 9
...
2, 9
...
3 and 9
...
4), then user must define them explicitly with the defmessage-handler construct
...
If the access facet is read-write, then the default value for the create-accessor facet is read-write
...
If the access facet is initialize-only, then the default is ?NONE
...
100 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual FALSE CLIPS> 9
...
3
...
g
...
By default, all these functions attempt to set a slot with the message called put-
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (slot special (override-message special-put))) CLIPS> (defmessage-handler A special-put primary (?value) (bind ?self:special ?value)) CLIPS> (watch messages) CLIPS> (make-instance a of A (special 65)) MSG >> create ED:1 () MSG << create ED:1 () MSG >> special-put ED:1 ( 65) MSG << special-put ED:1 ( 65) MSG >> init ED:1 () MSG << init ED:1 () [a] CLIPS> (unwatch messages) CLIPS> 9
...
3
...
Static and dynamic constraint checking for classes and their instances is supported
...
Object patterns used on the LHS of a rule are also checked to determine if constraint conflicts exist among variables used in more that one slot
...
Static checking is enabled by default
...
Dynamic checking is also supported
...
g
...
This dynamic checking is disabled by default
...
If an violation occurs when dynamic checking is being performed, then execution will be halted
...
Single-field values are converted to a multifield value of length one when storing in a multifield slot
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (multislot foo (create-accessor write) (type SYMBOL) (cardinality 2 3))) CLIPS> (make-instance a of A (foo 45)) [a] CLIPS> (set-dynamic-constraint-checking TRUE) FALSE CLIPS> (make-instance a of A (foo red 5
...
0) for slot foo of instance class A does not match the allowed types
...
[PRCCODE4] Execution halted during the actions in class A FALSE CLIPS> [a] found in put-foo primary in of message-handler put-foo primary found in put-foo primary in class A of message-handler put-foo primary 9
...
4 Message-handler Documentation COOL allows the user to forward declare the message-handlers for a class within the defclass statement
...
The defmessage-handler construct must be used to actually add message-handlers to a class
...
Example CLIPS> (clear) CLIPS> (defclass rectangle (is-a USER) (slot side-a (default 1)) (slot side-b (default 1)) (message-handler find-area)) CLIPS> (defmessage-handler rectangle find-area () (* ?self:side-a ?self:side-b)) CLIPS> (defmessage-handler rectangle print-area () (printout t (send ?self find-area) crlf)) 102 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual CLIPS> 9
...
The result of a message is a useful return-value or side-effect
...
The implementation of a message is made up of pieces of procedural code called message-handlers (or handlers for short)
...
In this way, the object’s class and all its superclasses share the labor of handling the message
...
Within a class, the handlers for a particular message can be further subdivided into four types or categories: primary, before, after and around
...
Before handlers execute before the primary ones, and after message-handlers execute after the primary ones
...
Around message-handlers allow the user to wrap code around the rest of the handlers
...
A primary handler provides the part of the message implementation which is most specific to an object, and thus the primary handler attached to the class closest to the immediate class of the object overrides other primary handlers
...
When only the roles of the handlers specify which handlers get executed and in what order, the message is said to be declaratively implemented
...
For example, the results of more than one primary handler may be needed
...
This is called the imperative technique
...
Around handlers can change the environment in which other handlers execute and modify the return value for the entire message
...
CLIPS Basic Programming Guide 103 CLIPS Reference Manual A defmessage-handler is comprised of seven elements: 1) a class name to which to attach the handler (the class must have been previously defined), 2) a message name to which the handler will respond, 3) an optional type (the default is primary), 4) an optional comment, 5) a list of parameters that will be passed to the handler during execution, 6) an optional wildcard parameter and 7) a series of expressions which are executed in order when the handler is called
...
Syntax Defaults are outlined
...
Message-handlers are never called directly
...
This process is termed the message dispatch
...
" crlf)) CLIPS> (defmessage-handler USER delete after () (printout t "System completed deletion of an instance
...
<== instance [a] of A System completed deletion of an instance
...
4
...
The regular parameters specify the minimum number of arguments that must be passed to the handler
...
If a wildcard parameter is present, the handler may be passed any number of arguments greater than or equal to the minimum
...
All arguments to a handler that do not correspond to a regular parameter are grouped into a multifield value that can be referenced by the wildcard parameter
...
Handler parameters have no bearing on the applicability of a handler to a particular message (see section 9
...
1)
...
5
...
Thus, the number of arguments accepted should be consistent for all message-handlers applicable to a particular message
...
4
...
1 Active Instance Parameter The term active instance refers to an instance which is responding to a message
...
This parameter name is reserved and cannot be explicitly listed in the message-handler’s parameters, nor can it be rebound within the body of a message-handler
...
4
...
The return value of the message-handler is the result of the evaluation of the last expression in the body
...
Normally, slots can only be manipulated by sending the object slot-accessor messages (see sections 9
...
3
...
4
...
However, handlers are considered part of the encapsulation (see section 2
...
2) of an object, and thus can directly view and change the slots of the object
...
These functions are discussed in section 12
...
A shorthand notation is provided for accessing slots of the active instance from within a message-handler
...
Syntax (bind ?self: *) Example CLIPS> (defmessage-handler A set-foo (?value) (bind ?self:foo ?value)) CLIPS> (send [a] set-foo 34) 34 CLIPS> Direct slot accesses are statically bound to the appropriate slot in the defclass when the message-handler is defined
...
If the subclass has redefined the slot, the direct slot access contained in the message-handler attached to the superclass will fail
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (slot foo (create-accessor read))) CLIPS> (defclass B (is-a A) (role concrete) (slot foo)) CLIPS> (make-instance b of B) [b] CLIPS> (send [b] get-foo) [MSGPASS3] Static reference to slot foo of class A does not apply to [b] of B [PRCCODE4] Execution halted during the actions of message-handler get-foo primary in class A FALSE CLIPS> In order for direct slot accesses in a superclass message-handler to apply to new versions of the slot in subclasses, the dynamic-put and dynamic-get (see sections 12
...
4
...
16
...
11) must be used
...
3
...
8)
...
4
...
All these basic actions are implemented with primary handlers attached to the class of the instance
...
These pieces of code will then be executed whenever the basic actions are performed on the instance
...
" crlf)) CLIPS> (make-instance a of A) Initializing a new instance of class A
...
4
...
These handlers cannot be deleted or modified
...
4
...
1 Instance Initialization Syntax (defmessage-handler USER init primary ()) This handler is responsible for initializing instances with class default values after creation
...
6
...
6
...
This handler is implemented using the init-slots function (see section 12
...
User-defined init handlers should not prevent the system message-handler from responding to an init message (see section 9
...
3)
...
4
...
2 Instance Deletion Syntax (defmessage-handler USER delete primary ()) This handler is responsible for deleting an instance from the system
...
User-defined delete message-handlers should not prevent the system message-handler from responding to a delete message (see section 9
...
3)
...
Example CLIPS> (send [Rolls-Royce] delete) MSG >> delete ED:1 () HND >> delete primary in class USER ED:1 () HND << delete primary in class USER ED:1 () MSG << delete ED:1 () TRUE CLIPS> CLIPS Basic Programming Guide 109 CLIPS Reference Manual 9
...
4
...
Example CLIPS> (make-instance Rolls-Royce of CAR) MSG >> create ED:1 () HND >> create primary in class USER ED:1 () HND << create primary in class USER ED:1 () MSG << create ED:1 () MSG >> init ED:1 () HND >> init primary in class USER ED:1 () HND << init primary in class USER ED:1 () MSG << init ED:1 () [Rolls-Royce] CLIPS> (send [Rolls-Royce] print) MSG >> print ED:1 () HND >> print primary in class USER ED:1 () [Rolls-Royce] of CAR (price 75000) (model Corniche) HND << print primary in class USER ED:1 () MSG << print ED:1 () CLIPS> (unwatch messages) CLIPS
...
4
...
4 Directly Modifying an Instance Syntax (defmessage-handler USER direct-modify primary (?slot-override-expressions)) This handler modifies the slots of an instance directly rather than using put- override messages to place the slot values
...
This message is used by the functions modify-instance and active-modify-instance
...
(defmessage-handler USER direct-modify around (?overrides) (send ?self message-modify ?overrides)) 9
...
4
...
The slot-override expressions are passed as an EXTERNAL_ADDRESS data object to the message-modify handler
...
9
...
4
...
Slot values from the original instance and slot overrides are directly copied
...
The slot-override expressions are passed as an EXTERNAL_ADDRESS data object to the direct-duplicate handler
...
Example The following around message-handler could be used to insure that all duplicate message slot-overrides are handled using put- messages
...
4
...
7 Duplicating an Instance using Messages Syntax (defmessage-handler USER message-duplicate primary (?new-instance-name ?slot-override-expressions) This handler duplicates an instance using messages
...
If the name of the new instance created matches a currently existing instance-name, then the currently existing instance is deleted using a delete message
...
The slot-override expressions are passed as an EXTERNAL_ADDRESS data object to the message-duplicate handler
...
9
...
4
...
The newly created instance is sent a create message
...
The handler returns the symbol TRUE if the instance was successfully created, otherwise it returns the symbol FALSE
...
5 MESSAGE DISPATCH When a message is sent to an object using the send function, CLIPS examines the class precedence list of the active instance’s class to determine a complete set of message-handlers which are applicable to the message
...
A handler which is attached to a subclass of another message-handler’s class is said to be more specific
...
Following is a flow diagram summary: 112 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual START : Input is a list of applicable message-handlers AROUND STEP: Are there any uncalled around handlers? YES: Call most specific uncalled around handler
...
Else go to DONE
...
NO: BEFORE STEP: Are there any uncalled before handlers? YES: Call most specific uncalled before handler
...
NO: PRIMARY STEP: Are there any uncalled primary handlers? YES: Call most specific uncalled primary handler
...
NO: The solid arrows indicate automatic control transfer by the message dispatch system
...
AFTER STEP: Are there any uncalled after handlers? YES: Call least specific uncalled after handler
...
NO: DONE: Return control and values to caller
...
5
...
9
...
2 Message-handler Precedence The set of all applicable message-handlers are sorted into four groups according to role, and these four groups are further sorted by class specificity
...
The order of execution is as follows: 1) around handlers begin execution from most specific to most general (each around handler must explicitly allow execution of other handlers), 2) before handlers execute (one after the other) from most specific to most general 3) primary handlers begin execution from most specific to most general (more specific primary handlers must explicitly allow execution of more general ones), 4) primary handlers finish execution from most general to most specific, 5) after handlers execute (one after the other) from most general to most specific and 6) around handlers finish execution from most general to most specific
...
5
...
9
...
3 Shadowed Message-handlers When one handler must be called by another handler in order to be executed, the first handler is said to be shadowed by the second
...
A primary handler shadows all more general primary handlers
...
Only the handler roles will dictate which handlers get executed; only before and after handlers and the most specific primary handler are used
...
However, if around handlers or shadowed primary handlers are necessary, then the handlers must explicitly take part in the message dispatch by calling other handlers they are shadowing
...
The functions call-next-handler and override-next-handler (see section 12
...
2) allow a handler to execute the handler it is shadowing
...
Example (defmessage-handler USER my-message around () (call-next-handler)) (defmessage-handler USER my-message before ()) (defmessage-handler USER my-message () (call-next-handler)) (defmessage-handler USER my-message after ()) (defmessage-handler OBJECT my-message around () (call-next-handler)) (defmessage-handler OBJECT my-message before ()) (defmessage-handler OBJECT my-message ()) (defmessage-handler OBJECT my-message after ()) For a message sent to an instance of a class which inherits from USER, the diagram to the right illustrates the order of execution for the handlers attached to the classes USER and OBJECT
...
Handlers enclosed within a bracket are shadowed
...
5
...
114 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual A lack of applicable of primary message-handlers and a handler being called with the wrong number of arguments are common message execution errors
...
5
...
The return value of a handler is the result of the evaluation of the last action in the handler
...
An around handler can choose to ignore or capture the return value of the next most specific around or primary handler
...
9
...
This is achieved by using the send function, which takes as arguments the destination object for the message, the message itself and any arguments which are to be passed to handlers
...
4
...
The return value of send is the result of the message as explained in section 9
...
5
...
This is how COOL implements the notion of encapsulation (see Section 2
...
2)
...
There are two major exceptions: 1) objects which are not instances of user-defined classes (floating-point and integer numbers, symbols, strings, multifield values, fact-addresses and external-addresses) can be manipulated in the standard non-OOP manner of previous versions of CLIPS as well and 2) creation and initialization of an instance of a user-defined class are performed via the function make-instance
...
6
...
Likewise, all instances are deleted during the reset command, and they can be loaded and saved similarly to facts
...
A function called make-instance is used to CLIPS Basic Programming Guide 115 CLIPS Reference Manual create and initialize a new instance
...
The user can customize instance initialization with daemons
...
make-instance automatically delays all object pattern-matching activities for rules until all slot overrides have been processed
...
active-make-instance remembers the current state of delayed pattern-matching, explicitly turns delay on, and then restores it to its previous state once all slot overrides have been processed
...
The evaluation of can either be an instance-name or a symbol
...
make-instance performs the following steps in order: 1) If an instance of the specified name already exists, that instance receives a delete message, e
...
(send delete)
...
Normally, the handler attached to class USER will respond to this message (see section 9
...
5
...
2) A new and uninitialized instance of the specified class is created with the specified name
...
g
...
Normally, the handler attached to class USER will respond to this message (see section 9
...
4
...
4) All slot-overrides are immediately evaluated and placed via put- messages (see section 9
...
3
...
g
...
If there are any errors, the new instance is deleted
...
g
...
Normally, the handler attached to class USER will respond to this message (see section 9
...
4
...
This handler calls the init-slots function (see section 12
...
4
...
This function uses defaults from the class definition (if any) for any slots which do not have slot-overrides
...
If there are any errors, the new instance is deleted
...
" crlf)) CLIPS> (defmessage-handler A delete after () (printout t "Old instance deleted
...
Slot x set with message
...
TRUE CLIPS> 9
...
1
...
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 definstances constructs
...
The instances of a definstances are created in order, and if any individual creation fails, the remainder of the definstances will be aborted
...
However, if this is not desired,then the active keyword can be specified after the definstances name so that the active-make-instance function will be used
...
(definstances initial-object (initial-object of INITIAL-OBJECT)) The class INITIAL-OBJECT is a predefined system class that is a direct subclass of USER
...
The INITIAL-OBJECT class cannot be deleted, but the initial-object definstances can
...
4
...
Important Note Although you can delete the initial-object definstances, in practice you never should since many conditional elements rely on the existence of the initial-object instance for correct operation
...
9
...
2 Reinitializing Existing Instances The initialize-instance function provides the ability to reinitialize an existing instance with class defaults and new slot-overrides
...
The evaluation of can either be an instance-name, instance-address or a symbol
...
The function active-initialize-instance can be used if delayed pattern-matching is not desired
...
3
...
10), e
...
(send put- *)
...
g
...
Normally, the handler attached to class USER will respond to this message (see section 9
...
5
...
This handler calls the init-slots function (see section 12
...
4
...
This function uses defaults from the class definition (if any) for any slots which do not have slot-overrides
...
If no slot-override or class default specifies the value of a slot, that value will remain the same
...
If an error occurs, the instance will not be deleted, but the slot values may be in an inconsistent state
...
") “Hello world
...
") CLIPS> (initialize-instance a) [a] CLIPS> (send [a] print) a of A (x 34) (y abc) (z nil) CLIPS> 9
...
3 Reading Slots Sources external to an object, such as a rule or deffunction, can read an object’s slots only by sending the object a message
...
4
...
Several functions also exist which operate implicitly on the active instance for a message that can only be called by message-handlers, such as dynamic-get (see section 12
...
4
...
Section 12
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (slot x (create-accessor read) (default abc))) CLIPS> (make-instance a of A) [a] CLIPS> (sym-cat (send [a] get-x) def) abcdef CLIPS> 9
...
4 Setting Slots Sources external to an object, such as a rule or deffunction, can write an object’s slots only by sending the object a message
...
16
...
11)
...
4
...
Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) 120 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual (slot x (create-accessor write) (default abc))) CLIPS> (make-instance a of A) [a] CLIPS> (send [a] put-x "New value
...
” CLIPS> 9
...
5 Deleting Instances Sending the delete message to an instance removes it from the system
...
16) can be used to delete the active instance for a message
...
6
...
This can be accomplished using the object-pattern-match-delay function
...
This function’s primary purpose is to provide some control over performance
...
CLIPS> (make-instance b of A) [b] CLIPS> (agenda) 0 match-A: [b] 0 match-A: [a] For a total of 2 activations
...
" crlf) (agenda) (make-instance d of A) (printout t "After d
...
0 match-A: [b] 0 match-A: [a] For a total of 2 activations
...
0 match-A: [b] 0 match-A: [a] For a total of 2 activations
...
CLIPS> 9
...
7 Modifying Instances Four functions are provided for modifying instances
...
Each of these functions returns the symbol TRUE if successful, otherwise the symbol FALSE is returned
...
6
...
1 Directly Modifying an Instance with Delayed Pattern-Matching The modify-instance function uses the direct-modify message to change the values of the instance
...
Syntax (modify-instance *) Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (slot foo) (slot bar)) CLIPS> (make-instance a of A) [a] CLIPS> (watch all) CLIPS> (modify-instance a (foo 0)) MSG >> direct-modify ED:1 ( ) 122 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual HND >> direct-modify primary in class USER
...
ED:1 ( ) MSG << direct-modify ED:1 ( ) TRUE CLIPS> (unwatch all) CLIPS> 9
...
7
...
Object pattern-matching occurs as slot modifications are being performed
...
6
...
3 Modifying an Instance using Messages with Delayed Pattern-Matching The message-modify-instance function uses the message-modify message to change the values of the instance
...
Syntax (message-modify-instance *) Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (slot foo) (slot bar (create-accessor write))) CLIPS> (make-instance a of A) [a] CLIPS> (watch all) CLIPS> (message-modify-instance a (bar 4)) MSG >> message-modify ED:1 ( ) HND >> message-modify primary in class USER ED:1 ( ) MSG >> put-bar ED:2 ( 4) HND >> put-bar primary in class A ED:2 ( 4) ::= local slot bar in instance a <- 4 HND << put-bar primary in class A ED:2 ( 4) MSG << put-bar ED:2 ( 4) HND << message-modify primary in class USER ED:1 ( ) MSG << message-modify ED:1 ( ) CLIPS Basic Programming Guide 123 CLIPS Reference Manual TRUE CLIPS> (unwatch all) CLIPS> 9
...
7
...
Object pattern-matching occurs as slot modifications are being performed
...
6
...
These functions allow instance duplication and slot updates to be performed in blocks without requiring a series of put- messages
...
Each of the duplicate functions can optionally specify the name of the instance to which the old instance will be copied
...
If the target instance already exists, it will be deleted directly or with a delete message depending on which function was called
...
6
...
1 Directly Duplicating an Instance with Delayed Pattern-Matching The duplicate-instance function uses the direct-duplicate message to change the values of the instance
...
Syntax (duplicate-instance [to ]*) Example CLIPS> (clear) CLIPS> (setgen 1) 1 CLIPS> (defclass A (is-a USER) (role concrete) (slot foo (create-accessor write)) (slot bar (create-accessor write))) CLIPS> (make-instance a of A (foo 0) (bar 4)) [a] CLIPS> (watch all) CLIPS> (duplicate-instance a) 124 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual MSG >> direct-duplicate ED:1 ( [gen1] ) HND >> direct-duplicate primary in class USER ED:1 ( [gen1] ) ==> instance [gen1] of A ::= local slot foo in instance gen1 <- 0 ::= local slot bar in instance gen1 <- 4 HND << direct-duplicate primary in class USER ED:1 ( [gen1] ) MSG << direct-duplicate ED:1 ( [gen1] ) [gen1] CLIPS> (unwatch all) CLIPS> 9
...
8
...
Object pattern-matching occurs as slot modifications are being performed
...
6
...
3 Duplicating an Instance using Messages with Delayed Pattern-Matching The message-duplicate-instance function uses the message-duplicate message to change the values of the instance
...
Syntax (message-duplicate-instance [to ]*) Example CLIPS> (clear) CLIPS> (defclass A (is-a USER) (role concrete) (slot foo (create-accessor write)) (slot bar (create-accessor write))) CLIPS> (make-instance a of A (foo 0) (bar 4)) [a] CLIPS> (make-instance b of A) [b] CLIPS> (watch all) CLIPS> (message-duplicate-instance a to b (bar 6)) MSG >> message-duplicate ED:1 ( [b] ) HND >> message-duplicate primary in class USER ED:1 ( [b] ) MSG >> delete ED:2 () HND >> delete primary in class USER ED:2 () CLIPS Basic Programming Guide 125 CLIPS Reference Manual <== instance [b] of A HND << delete primary in class USER ED:2 () MSG << delete ED:2 () ==> instance [b] of A MSG >> create ED:2 () HND >> create primary in class USER ED:2 () HND << create primary in class USER ED:2 () MSG << create ED:2 () MSG >> put-bar ED:2 ( 6) HND >> put-bar primary in class A ED:2 ( 6) ::= local slot bar in instance b <- 6 HND << put-bar primary in class A ED:2 ( 6) MSG << put-bar ED:2 ( 6) MSG >> put-foo ED:2 ( 0) HND >> put-foo primary in class A ED:2 ( 0) ::= local slot foo in instance b <- 0 HND << put-foo primary in class A ED:2 ( 0) MSG << put-foo ED:2 ( 0) MSG >> init ED:2 () HND >> init primary in class USER ED:2 () HND << init primary in class USER ED:2 () MSG << init ED:2 () HND << message-duplicate primary in class USER ED:1 ( [b] ) MSG << message-duplicate ED:1 ( [b] ) [b] CLIPS> (unwatch all) CLIPS> 9
...
8
...
Object pattern-matching occurs as slot modifications are being performed
...
7 INSTANCE-SET QUERIES AND DISTRIBUTED ACTIONS COOL provides a useful query system for determining and performing actions on sets of instances of user-defined classes that satisfy user-defined queries
...
The specific details of each query function will then be given
...
5 ?car2:price)) Instance-set query (printout t ?car1 crlf)) Instance-set distributed action [Albert-Maserati] CLIPS> Instance-set member variables For all of the examples in this section, assume that the commands below have already been entered: Example CLIPS> (defclass PERSON (is-a USER) (role abstract) (slot sex (access read-only) (storage shared)) (slot age (type NUMBER) (visibility public))) CLIPS> (defmessage-handler PERSON put-age (?value) (dynamic-put age ?value)) CLIPS Basic Programming Guide 127 CLIPS Reference Manual CLIPS> (defclass FEMALE (is-a PERSON) (role abstract) (slot sex (source composite) (default female))) CLIPS> (defclass MALE (is-a PERSON) (role abstract) (slot sex (source composite) (default male))) CLIPS> (defclass GIRL (is-a FEMALE) (role concrete) (slot age (source composite) (default 4) (range 0
...
9))) CLIPS> (defclass WOMAN (is-a FEMALE) (role concrete) (slot age (source composite) (default 25) (range 18
...
0))) CLIPS> (defclass BOY (is-a MALE) (role concrete) (slot age (source composite) (default 4) (range 0
...
9))) CLIPS> (defclass MAN (is-a MALE) (role concrete) (slot age (source composite) (default 25) (range 18
...
0))) CLIPS> (definstances PEOPLE (Man-1 of MAN (age 18)) (Man-2 of MAN (age 60)) (Woman-1 of WOMAN (age 18)) (Woman-2 of WOMAN (age 60)) (Woman-3 of WOMAN) (Boy-1 of BOY (age 8)) (Boy-2 of BOY) (Boy-3 of BOY) (Boy-4 of BOY) (Girl-1 of GIRL (age 8)) (Girl-2 of GIRL)) CLIPS> (reset) CLIPS> 9
...
1 Instance-set Definition An instance-set is an ordered collection of instances
...
The class restrictions can be different for each instance-set member
...
An instance-set template is a set of instance-set member variables and their associated class restrictions
...
Variables may be used to specify the classes for the instance-set template, but if the constant names of the classes are specified, the classes must already be defined
...
Syntax ::= (+) ::= ( ) ::= ::= + Example One instance-set template might be the ordered pairs of boys or men and girls or women
...
g
...
9
...
2 Instance-set Determination COOL uses straightforward permutations to generate instance-sets that match an instance-set template from the actual instances in the system
...
2) When there is more than one class that an instance-set member can be, iterate through the classes from left to right
...
3a) Recursively examine instances of subclasses in the order that the subclasses were defined
...
Otherwise, only subclasses which are in scope of the module to which the query class belongs will be examined
...
7
...
2
...
4
...
6
...
8
...
10
...
12
...
15
...
17
...
19
...
21
...
23
...
25
...
27
...
29
...
[Boy-4] [Boy-4] [Boy-4] [Boy-4] [Boy-4] [Man-1] [Man-1] [Man-1] [Man-1] [Man-1] [Man-2] [Man-2] [Man-2] [Man-2] [Man-2] [Girl-1] [Girl-2] [Woman-1] [Woman-2] [Woman-3] [Girl-1] [Girl-2] [Woman-1] [Woman-2] [Woman-3] [Girl-1] [Girl-2] [Woman-1] [Woman-2] [Woman-3] Example Consider the following instance-set template: ((?f1 FEMALE) (?f2 FEMALE)) Twenty-five instance-sets would be generated in the following order: 1
...
[Girl-1] [Girl-2] 3
...
[Girl-1] [Woman-2] 5
...
[Girl-2] [Girl-1] 7
...
[Girl-2] [Woman-1] 9
...
[Girl-2] [Woman-3] 11
...
[Woman-1] [Girl-2] 13
...
[Woman-1] 15
...
[Woman-2] 17
...
[Woman-2] 19
...
[Woman-2] 21
...
[Woman-3] 23
...
[Woman-3] 25
...
9
...
3 Query Definition A query is a user-defined boolean expression applied to an instance-set to determine if the instance-set meets further user-defined restrictions
...
130 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual Syntax ::= Example Continuing the previous example, one query might be that the two instances in an ordered pair have the same age
...
4
...
If message-passing is not explicitly required for reading a slot (i
...
there are no accessor daemons for reads), then this second method of slot access should be used, for it gives a significant performance benefit
...
9
...
4 Distributed Action Definition A distributed action is a user-defined expression evaluated for each instance-set which satisfies a query
...
If more than one action is required, use the progn function (see section 12
...
5) to group them
...
(printout t "(" ?man-or-boy "," ?woman-or-girl ")" crlf) CLIPS Basic Programming Guide 131 CLIPS Reference Manual 9
...
5 Scope in Instance-set Query Functions An instance-set query function can be called from anywhere that a regular function can be called
...
In addition, rebinding variables within an instance-set function action is allowed
...
Binding variables is not allowed within a query
...
Example CLIPS> (deffunction count-instances (?class) (bind ?count 0) (do-for-all-instances ((?ins ?class)) TRUE (bind ?count (+ ?count 1))) ?count) CLIPS> (deffunction count-instances-2 (?class) (length (find-all-instances ((?ins ?class)) TRUE))) CLIPS> (count-instances WOMAN) 3 CLIPS> (count-instances-2 BOY) 4 CLIPS> Instance-set member variables are only in scope within the instance-set query function
...
Example CLIPS> (deffunction last-instance (?class) (any-instancep ((?ins ?class)) TRUE) ?ins) [PRCCODE3] Undefined variable ins referenced in deffunction
...
7
...
132 Section 9 - CLIPS Object Oriented Language (COOL) CLIPS Reference Manual 9
...
7 Halting and Returning Values from Query Functions The functions break and return are now valid inside the action of the instance-set query functions do-for-instance, do-for-all-instances and delayed-do-for-all-instances
...
9
...
8 Instance-set Query Functions The instance query system in COOL provides six functions
...
7
...
However, if a particular instance is deleted and recreated, the iteration order will change
...
7
...
1 Testing if Any Instance-set Satisfies a Query This function applies a query to each instance-set which matches the template
...
Otherwise, the return value is the symbol FALSE
...
7
...
2 Determining the First Instance-set Satisfying a Query This function applies a query to each instance-set which matches the template
...
Otherwise, the return value is a zero-length multifield value
...
Syntax (find-instance ) Example Find the first pair of a man and a woman who have the same age
...
7
...
3 Determining All Instance-sets Satisfying a Query This function applies a query to each instance-set which matches the template
...
This multifield value is returned when the query has been applied to all possible instance-sets
...
The first n fields correspond to the first instance-set, and so on
...
The multifield value can consume a large amount of memory due to permutational explosion, so this function should be used judiciously
...
CLIPS> (find-all-instances ((?m MAN) (?w WOMAN)) (= ?m:age ?w:age)) ([Man-1] [Woman-1] [Man-2] [Woman-2]) CLIPS> 9
...
8
...
If an instance-set satisfies the query, the specified action is executed, and the function is immediately terminated
...
If no instance-set satisfied the query, then the return value is the symbol FALSE
...
The calls to neq in the query eliminate the permutations where two or more members of the instance-set are identical
...
7
...
5 Executing an Action for All Instance-sets Satisfying a Query This function applies a query to each instance-set which matches the template
...
The return value is the evaluation of the action for the last instance-set which satisfied the query
...
Syntax (do-for-all-instances *) Example Print out all triplets of different people that have the same age
...
Without these restrictions, two instance-sets which differed only in the order of their members would both satisfy the query
...
7
...
6 Executing a Delayed Action for All Instance-sets Satisfying a Query This function is similar to do-for-all-instances except that it groups all instance-sets which satisfy the query into an intermediary multifield value
...
Otherwise, the specified action is executed for each instance-set in the multifield value, and the return value is the evaluation of the action for the last instance-set to satisfy the query
...
This function can consume large amounts of memory in the same fashion as find-all-instances
...
Syntax (delayed-do-for-all-instances *) CLIPS Basic Programming Guide 135 CLIPS Reference Manual Example Delete all boys with the greatest age
...
The action needs to be delayed until all boys have been processed, or the greatest age will decrease as the older boys are deleted
...
CLIPS modules allow a set of constructs to be grouped together such that explicit control can be maintained over restricting the access of the constructs by other modules
...
By restricting access to deftemplate and defclass constructs, modules can function as blackboards, permitting only certain facts and instances to be seen by other modules
...
10
...
Syntax (defmodule []*) ::= (export ) | (import ) ::= ?ALL | ?NONE | ?ALL | ?NONE | + ::= deftemplate | defclass | defglobal | deffunction | defgeneric A defmodule cannot be redefined or even deleted once it is defined (with the exception of the MAIN module which can be redefined once)
...
Upon startup and after a clear command, CLIPS automatically constructs the following defmodule
...
2) belong to the MAIN module
...
Discounting the previous exception, the predefined MAIN module does not import or export any constructs
...
CLIPS Basic Programming Guide 137 CLIPS Reference Manual Example (defmodule (import (import (import (export (export FOO BAR ?ALL) YAK deftemplate ?ALL) GOZ defglobal x y z) defgeneric +) defclass ?ALL)) 10
...
The deffacts, deftemplate, defrule, deffunction, defgeneric, defclass, and definstances constructs all specify the module for the construct by including it as part of the name
...
The module of a defmessage-handler is specified as part of the class specifier
...
For example, the following constructs would be placed in the DETECTION module
...
CLIPS> (set-current-module B) A CLIPS> (list-defrules) foo For a total of 1 defrule
...
3 SPECIFYING MODULES Commands such as undefrule and ppdefrule require the name of a construct on which to operate
...
With modules, however, it is possible to have a construct with the same name in two different modules
...
To explicitly specify a name’s module the module name (a symbol) is listed followed by two colons, ::, and then the name is listed
...
For example, MAIN::find-stuff, refers to the find-stuff construct in the MAIN module
...
The current module is changed whenever a defmodule construct is defined or the set-current-module function is used
...
Thus the name find-stuff would implicitly have the MAIN module as its module when CLIPS is first started
...
4 IMPORTING AND EXPORTING CONSTRUCTS Unless specifically exported and imported, the constructs of one module may not be used by another module
...
For example, if module B wants to use the foo deftemplate defined in module A, then module A must export the foo deftemplate and module B must import the foo deftemplate from module A
...
10
...
1 Exporting Constructs The export specification in a defmodule definition is used to indicate which constructs will be accessible to other modules importing from the module being defined
...
A module may export any valid constructs that are visible to it (not just constructs that it defines)
...
First, a module may export all valid constructs that are visible to it
...
Second, a module may export all valid constructs of a particular type that are visible to it
...
Third, a module may export specific constructs of a particular type that are visible to it
...
In the following code, defmodule A exports all of its constructs; defmodule B exports all of its deftemplates; and defmodule C exports the foo, bar, and yak defglobals
...
Defmethods and defmessage-handlers cannot be explicitly exported
...
Exporting a defclass automatically exports all associated defmessage-handlers
...
140 Section 10 - Defmodule Construct CLIPS Reference Manual 10
...
2 Importing Constructs The import specification in a defmodule definition is used to indicate which constructs the module being defined will use from other modules
...
There are three different types of import specifications
...
This accomplished by following the import keyword with a module name followed by the ?ALL keyword
...
This accomplished by following the import keyword with a module name followed by the name of the construct type followed by the ?ALL keyword
...
This accomplished by following the import keyword with a module name followed by the name of the construct type followed by the name of one or more visible constructs of the specified type
...
(defmodule A (import D ?ALL)) (defmodule B (import D deftemplate ?ALL)) (defmodule C (import D defglobal foo bar yak)) The ?NONE keyword may be used in place of the ?ALL keyword to indicate either that no constructs are imported from a module or that no constructs of a particular type are imported from a module
...
Importing a defgeneric automatically imports all associated defmethods
...
Deffacts, definstances, and defrules cannot be imported
...
In addition, if specific constructs are listed in the import specification, they must already be defined in the module exporting them
...
A construct can be indirectly imported from a module that directly imports and then exports the module to be used
...
5 IMPORTING AND EXPORTING FACTS AND INSTANCES Facts and instances are “owned” by the module in which their corresponding deftemplate or defclass is defined, not by the module which creates them
...
This allows a CLIPS Basic Programming Guide 141 CLIPS Reference Manual knowledge base to be partitioned such that rules and other constructs can only “see” those facts and instances which are of interest to them
...
Rules which have the initial-fact or initial-object pattern added to their LHS (such as a rule thats first CE is a not CE) will not be activated unless the corresponding construct for the pattern is imported
...
CLIPS> (facts B) f-1 (foo (x 3)) For a total of 1 fact
...
5
...
The syntax of instance-names has been extended to allow module specifications (note that the left and right brackets in bold are to be typed and do not indicate an optional part of the syntax)
...
Specifying only the :: before the name, such as [::Rolls-Royce], will search for the instance first in the current module and then recursively in the imported modules as defined in the module definition
...
Regardless of which format is specified, the class of the instance must be in scope of the current module in order for the instance to be found
...
6 MODULES AND RULE EXECUTION Each module has its own pattern-matching network for its rules and its own agenda
...
Rule execution continues until another module becomes the current focus, no rules are left on the agenda, or the return function is used from the RHS of a rule
...
Before a rule executes, the current module is changed to the module in which the executing rule is defined (the current focus)
...
See sections 5
...
4
...
2, 12
...
12 for more details
...
" crlf) (focus A B)) CLIPS> (defmodule A (import MAIN deftemplate initial-fact)) CLIPS> (defrule A::example-rule => (printout t "Firing rule in module A
...
" crlf)) CLIPS> (reset) CLIPS> (run) Firing rule in module MAIN
...
Firing rule in module B
...
The constraint information is also analyzed for the patterns on the LHS of a rule to determine if the specified constraints prevent the rule from ever firing
...
When static constraint checking is enabled, constraint violations are checked when function calls and constructs are parsed
...
When dynamic constraint checking is enabled, newly created data objects (such as deftemplate facts and instances) have their slot values checked for constraint violations
...
By default, static constraint checking is enabled and dynamic constraint checking is disabled
...
Unless dynamic constraint checking is enabled, constraint information associated with constructs is not saved when a binary image is created using the bsave command
...
Syntax ::= | | | 11
...
Syntax ::= (type ) ::= + | ?VARIABLE ::= SYMBOL | STRING | LEXEME | INTEGER | FLOAT | NUMBER | INSTANCE-NAME | INSTANCE-ADDRESS | INSTANCE | EXTERNAL-ADDRESS | FACT-ADDRESS Using NUMBER for this attribute is equivalent to using both INTEGER and FLOAT
...
Using CLIPS Basic Programming Guide 145 CLIPS Reference Manual INSTANCE for this attribute is equivalent to using both INSTANCE-NAME and INSTANCE-ADDRESS
...
11
...
The list of values provided should either be a list of constants of the specified type or the keyword ?VARIABLE which means any constant of that type is allowed
...
Note the difference between using the attribute (allowed-symbols red green blue) and (allowed-values red green blue)
...
The allowed-values attribute completely restricts the allowed values to the listed values
...
Instead, if this attribute is specified and the slot value is either an instance address or instance name, then the class to which the instance belongs must be a class specified in the allowed-classes attribute or be a subclass of one of the specified classes
...
A string or symbol must match one of the constants in the attribute list
...
Similarly, specifying the allowed-numbers attribute is equivalent to specifying constant restrictions on both integers and floats
...
1, type conversion of integers to floats and floats to integers was performed when using the allowed-numbers attribute (thus using allowed-numbers was not equivalent to using both the allowed-integers and allowed-floats attributes together)
...
0, this type conversion is no longer performed
...
1 is no longer supported
...
11
...
If a numeric value is not used in that slot, then no checking is performed
...
Integers will be temporarily converted to floats when necessary to perform range comparisons
...
If the keyword ?VARIABLE is used for the maximum value, then the maximum value is positive infinity (+∞)
...
11
...
This attribute can not be used with a single field slot
...
If the keyword ?VARIABLE is used for the minimum value, then the minimum cardinality is zero
...
If the cardinality is not specified for a multifield slot, then it is assumed to be zero to infinity
...
1 are no longer supported
...
11
...
The following rules are used (in order) to determine the default value for a slot with an unspecified default value
...
2) If the default type has an allowed constant restriction specified (such as the allowed-integers attribute for the INTEGER type), then the first value specified in the allowed constant attribute is chosen as the default value
...
4) If the default value was not specified by step 2 or 3, then the default default value is used
...
0 for type FLOAT, [nil] for type INSTANCE-NAME, a pointer to a dummy instance for type INSTANCE-ADDRESS, a pointer to a dummy fact for type FACT-ADDRESS, and the NULL pointer for type EXTERNAL-ADDRESS
...
The default value for a multifield slot is a multifield value of length zero
...
148 Section 11 - Constraint Attributes CLIPS Reference Manual 11
...
Example 1 CLIPS> (deftemplate bar (slot a (type SYMBOL INTEGER)) (slot b (type INTEGER FLOAT)) (slot c (type SYMBOL STRING))) CLIPS> (defrule error (bar (a ?x)) (bar (b ?x)) (bar (c ?x)) =>) [RULECSTR1] Variable ?x in CE #3 slot c has constraint conflicts which make the pattern unmatchable ERROR: (defrule error-4 (bar (a ?x)) (bar (b ?x)) (bar (c ?x)) =>) CLIPS> The first occurrence of the variable ?x in slot a of the first pattern restricts its allowed types to either a symbol or integer
...
The final occurence of ?x in the third pattern generates an error because slot c expects ?x to be either a symbol or a string, but its only allowed type is an integer
...
The variable ?y, found in the second pattern, can have a maximum of three fields
...
Since slot z in the the third pattern has a minimum cardinality of seven, the variables ?x and ?y cannot satisfy the minimum cardinality restriction for this slot
...
Since the > function expects numeric values for its arguments, an error occurs
...
The terms functions, actions, and commands should be thought of interchangeably
...
The term action refers to a function having no return value but performing some basic operation as a side effect (such as printout)
...
12
...
12
...
1 Testing For Numbers The numberp function returns the symbol TRUE if its argument is a float or integer, otherwise it returns the symbol FALSE
...
1
...
Syntax (floatp ) 12
...
3 Testing For Integers The integerp function returns the symbol TRUE if its argument is an integer, otherwise it returns the symbol FALSE
...
1
...
Syntax (lexemep ) 12
...
5 Testing For Strings The stringp function returns the symbol TRUE if its argument is a string, otherwise it returns the symbol FALSE
...
1
...
This function may also be called using the name wordp
...
1
...
Syntax (evenp ) 12
...
8 Testing For Odd Numbers The oddp function returns the symbol TRUE if its argument is an odd number, otherwise it returns the symbol FALSE
...
1
...
This function may also be called using the name sequencep
...
1
...
External-addresses are discussed in further detail in the Advanced Programming Guide
...
1
...
Note that eq compares types as well as values
...
0) is FALSE since 3 is an integer and 3
...
Syntax (eq +) Example CLIPS> (eq foo bar mumble foo) FALSE CLIPS> (eq foo foo foo foo) TRUE CLIPS> (eq 3 4) FALSE CLIPS> 12
...
12 Comparing for Inequality The neq function returns the symbol TRUE if its first argument is not equal in value to all its subsequent arguments, otherwise it returns the symbol FALSE
...
Thus, (neq 3 3
...
0 is a float
...
1
...
Note that = compares only numeric values and will convert integers to floats when necessary for comparison
...
0) TRUE CLIPS> (= 4 4
...
In fact, you should be aware, even if code is not being ported, that roundoff error can cause erroneous results
...
6666666666666666667
...
66666666666666666666 0
...
1
...
Note that <> compares only numeric values and will convert integers to floats when necessary for comparison
...
0) 154 Section 12 - Actions and Functions CLIPS Reference Manual FALSE CLIPS> (<> 4 4
...
1
...
12
...
15 Greater Than Comparison The > function returns the symbol TRUE if for all its arguments, argument n-1 is greater than argument n, otherwise it returns the symbol FALSE
...
Syntax (> +) Example CLIPS> (> 5 4 3) TRUE CLIPS> (> 5 3 4) FALSE CLIPS> Portability Note See portability note in section 12
...
13
...
1
...
Note that >= compares only numeric values and will convert integers to floats when necessary for comparison
...
1
...
CLIPS Basic Programming Guide 155 CLIPS Reference Manual 12
...
17 Less Than Comparison The < function returns the symbol TRUE if for all its arguments, argument n-1 is less than argument n, otherwise it returns the symbol FALSE
...
Syntax (< +) Example CLIPS> (< 3 4 5) TRUE CLIPS> (< 3 5 4) FALSE CLIPS> Portability Note See portability note in section 12
...
13
...
1
...
Note that <= compares only numeric values and will convert integers to floats when necessary for comparison
...
1
...
12
...
19 Boolean And The and function returns the symbol TRUE if each of its arguments evaluates to TRUE, otherwise it returns the symbol FALSE
...
Each argument of the function is evaluated from left to right
...
156 Section 12 - Actions and Functions CLIPS Reference Manual Syntax (and +) 12
...
20 Boolean Or The or function returns the symbol TRUE if any of its arguments evaluates to TRUE, otherwise it returns the symbol FALSE
...
Each argument of the function is evaluated from left to right
...
Syntax (or +) 12
...
21 Boolean Not The not function returns the symbol TRUE if its argument evaluates to FALSE, otherwise it returns the symbol FALSE
...
2 MULTIFIELD FUNCTIONS The following functions operate on multifield values
...
2
...
Syntax (create$ *) The return value of create$ is a multifield value regardless of the number or types of arguments (single-field or multifield)
...
Example CLIPS (create$ hammer drill saw screw pliers wrench) (hammer drill saw screw pliers wrench) CLIPS> (create$ (+ 3 4) (* 2 3) (/ 8 4)) (7 6 2) CLIPS> CLIPS Basic Programming Guide 157 CLIPS Reference Manual 12
...
2 Specifying an Element The nth$ function will return a specified field from a multifield value
...
The symbol nil will be returned if the first argument is greater than the number of fields in the second argument
...
2
...
Syntax (member$ )