Table of Contents


preface xxi
acknowledgments xxiii
about this book xxvi
author online xxix
about the title xxx
about the cover illustration xxxi

Part 1 Introducing rule-based systems 1

1 Rules to the rescue 3
1.1 Math class melee 4
Beyond logic puzzles 8
1.2 Some real-world examples 9
Mail filtering 9
Product configuration 10
Implementing business rules 11
1.3 Summary 12
2 What are rule-based systems? 13
2.1 The cooking/driving robot 14
Declarative programming: a different approach 15
2.2 Rules and rule engines 17
Expert systems 18
2.3 Architecture of a rule-based system 19
The inference engine 20
The rule base 21
The working memory 21
The pattern matcher 22
The agenda 22
The execution engine 23
2.4 Developing rule-based systems 23
Knowledge engineering 23
Structuring data 24
Testing 24
Interface building 25
Writing the rules 25
Iterative development 26
2.5 Rule engine standards 26
2.6 Summary 27

Part 2 Jess: A rule-based programming environment 29

3 Introducing Jess 31
3.1 The Jess rule engine 32
Obtaining Jess 33
Installing Jess 33
Running Jess 34
Hello, World? 34
3.2 Jess applications 36
Command line, GUI, or embedded? 37
Jess performance 38
3.3 Summary 39
4 Getting started with the Jess language 41
4.1 The basics 42
Whitespace 42
Symbols 42
The jess.Value class 43
Numbers 43
Strings 44
Comments 44
4.2 Adding some structure 45
Lists 45
Calling functions 46
Variables 47
More about lists 49
4.3 Control flow 50
foreach 51
while 52
if/then/else 52
progn 53
apply 54
eval and build 54
4.4 Defining functions with deffunction 55
Late binding 57
4.5 Fine-tuning a function?s behavior 58
Take my advice, please 60
4.6 Summary 60
5 Scripting Java with Jess 61
5.1 Creating Java objects 62
5.2 Calling Java methods 64
Nesting function calls, and a shortcut 65
Calling static methods 66
Calling set and get methods 66
Working with arrays 67
How Jess chooses among overloaded methods 68
5.3 Accessing Java member data 69
5.4 Working with exceptions 70
5.5 Summary 73
6 Representing facts in Jess 74
6.1 Jess?s working memory 75
Manipulating the working memory 76
6.2 Just the facts, ma?am 81
6.3 Unordered facts 82
The deftemplate construct 82
Default slot values 83
Multislots 84
Changing slot values with modify 85
Copying facts with duplicate 85
6.4 Ordered facts 86
6.5 Shadow facts 87
Jess and JavaBeans 88
JavaBeans have ?slots? 88
An example JavaBean 89
Creating a deftemplate for DimmerSwitch 90
Putting a DimmerSwitch into working memory 90
Static vs. dynamic shadow facts 91
Adding PropertyChangeListener support to DimmerSwitch 92
Shadow facts and working memory functions 94
6.6 Summary 94
7 Writing rules in Jess 95
7.1 Forward-chaining rules 96
Patterns and shadow facts 100
7.2 Constraining slot data 100
Literal constraints 101
Variables as constraints 102
Connective constraints 104
Constraining matches with predicate functions 105
Return value constraints 106
Pattern bindings 107
7.3 Qualifying patterns with conditional elements 108
The and conditional element 108
The or conditional element 109
The not conditional element 111
The test conditional element 113
The logical conditional element 114
7.4 Backward-chaining rules 116
7.5 Managing the agenda 120
Conflict resolution 121
Changing rule priority with salience 121
7.6 Partitioning the rule base with defmodule 123
Defining constructs in modules 123
Modules, scope, and name resolution 124
Module focus and execution control 126
7.7 Searching working memory with defquery 128
The variable declaration 130
Query trigger facts 130
The count-query-results function 131
Backward chaining and queries 131
7.8 Summary 131
8 Under the hood: how Jess works 133
8.1 Review of the problem 134
8.2 An inefficient solution 135
8.3 The Rete algorithm 136
How Rete works 136
Handling retract 139
8.4 Easy optimizations for Rete 139
8.5 Performance of the Rete algorithm 141
Node index hash value 141
8.6 More complexity and initial-fact 142
Implementing the not conditional element 142
Implementing the test conditional element 143
Implementing backward chaining 143
8.7 Exploring the Rete network in Jess 143
The (watch compilations) command 143
The view function 144
The matches function 146
8.8 Summary 146

Part 3 Creating your first rule-based application: the Tax Forms Advisor 147

9 Collecting the knowledge 149
9.1 The Tax Forms Advisor 150
9.2 Introduction to knowledge engineering 150
Where do you start? 151
Interviews 153
Desk research 156
9.3 Collecting knowledge about tax forms 157
An interview 157
Reviewing the forms 159
Next steps 160
9.4 Summary 160
10 Designing the application 161
10.1 Organizing the data 162
10.2 Filling in details 163
Default slot values 163
10.3 More templates 164
10.4 Templates you don?t need 165
10.5 Organizing the rules 166
10.6 Building the infrastructure 166
Simple text-based I/O 167
Fetching the question text 168
10.7 Summary 170
11 Writing the application 172
11.1 Welcoming the user 173
Testing the startup module 173
11.2 Asking the user questions 174
Income and dependents 175
Dealing with special circumstances 176
Testing the interview module 177
11.3 Recommending forms 177
11.4 Explaining the results 181
Testing the report module 182
11.5 Finishing touches 182
11.6 Testing the full application 183
11.7 Summary 184

Part 4 Writing a diagnostic application: the PC Repair Assistant 185

12 Writing the PC Repair Assistant 187
12.1 Using flowcharts in knowledge engineering 188
From flowcharts to rules 189
12.2 The problem domain 190
Writing the first rules 191
12.3 Asking questions with backward chaining 193
12.4 Checking the answers 194
Modifying the ask module 195
12.5 The rest of the rules 197
Rules about sound 197
Degrading gracefully 198
To boot, or not to boot 199
RAM problems 199
Questioning authority 200
12.6 Testing 201
12.7 Summary 202
13 Adding a graphical interface 203
13.1 Getting started 204
13.2 Displaying a window 205
13.3 Displaying questions 206
13.4 Getting answers 207
The main thread vs. the event thread 209
13.5 Better input components 213
13.6 Finishing touches 214
13.7 Testing the interface 215
13.8 Summary 216

Part 5 Reasoning about reality: the HVAC Controller 219

14 The reality connection 221
14.1 The system 223
14.2 Defining the hardware interface 224
Native methods 224
Writing a simulator 225
Simulating getTemperature 227
Adding a graphical interface 229
14.3 Writing the JavaBeans 229
Rules about Thermometers 232
Writing the other Beans 233
14.4 JavaBeans and serialization 235
14.5 Summary 235
15 Extending the Jess language 237
15.1 The Userfunction interface 238
The getName method 239
The call method 239
Loading a Userfunction into Jess 239
15.2 Handling arguments 240
How many arguments? 240
Using arguments 241
Resolving variable arguments 242
Resolving function call arguments 243
15.3 Returning a value 245
Constructing Value objects 245
15.4 Beyond simple examples 246
Holding state 246
Multiple personalities 246
Userfunctions and serialization 247
Grouping functions with Userpackage 247
15.5 The HVAC functions 248
Creating a simulator 248
Counting devices 249
Matching heat pumps and floors 250
Operating the hardware 251
Implementing a Userpackage 253
15.6 Testing 253
15.7 Summary 254
16 Writing the rules 255
16.1 The control algorithm 256
Knowledge engineering with truth tables 256
How heat pumps work 257
Using guard lines 257
Saving energy 258
16.2 Setting up 258
16.3 Controlling the heat pumps 259
Enough is enough 260
The moment of truth 261
16.4 Controlling the vents 262
The vent rules 262
16.5 Testing the whole system 263
16.6 Controlling with fuzzy rules 264
Fuzzy logic, briefly 265
The Fuzzy HVAC Controller 269
Exploring the fuzzy controller 282
16.7 What?s next? 282
16.8 Summary 283

Part 6 TekMart.com: rule-based applications for the Web 285

17 Jess on the Web 287
17.1 Java architectures for the Web 288
Fat-client applications 288
Thin-client applications 290
17.2 A Jess application for the Web 292
17.3 Knowledge engineering 292
17.4 Designing data structures 293
17.5 Writing the rules 295
About testing 295
The recommend-requirements rule 296
Recommending videos and DVDs 297
Conspicuous consumption 298
More media rules 299
17.6 Refining the recommendations 300
17.7 Some useful queries 301
Maintaining the order number 302
17.8 Cleaning up 303
17.9 Summary 304
18 Embedding Jess in Java applications 306
18.1 Getting started with the Jess library 307
The executeCommand method 308
Exchanging Java objects 308
Beyond executeCommand 310
18.2 Working with Fact objects in Java 311
Multislots 313
Ordered facts 313
Removing facts 314
18.3 Working with JavaBeans 314
18.4 Calling Jess functions from Java 315
18.5 Working with JessException 316
Nested exceptions 317
Rolling your own 318
18.6 Input and output 319
Using custom routers 320
18.7 Summary 321
19 Deploying web-based applications 323
19.1 The Java Servlet API 325
19.2 J2EE and the Tomcat engine 326
Deploying the Hello servlet 326
19.3 Your first Jess servlet 329
Deploying the Jess servlet 330
Cleaning up the URL 331
19.4 Application architecture: take one 331
19.5 Starting the Catalog servlet 332
JavaServer Pages 332
Forwarding to a JSP 333
19.6 Application architecture, take two 335
19.7 The login screen 335
19.8 The Catalog servlet 337
Initializing Jess 337
Getting the login name 338
Starting a user session 339
Querying the product list 340
Invoking the JSP 340
The catalog JSP 341
19.9 Testing 343
19.10 The Recommend servlet 343
Getting started 344
Creating the order 344
Getting the recommendations 345
Forwarding to JSPs 346
19.11 The recommend JSP 346
19.12 The Purchase servlet 348
19.13 Persistence 350
19.14 Deploying the application 351
19.15 What?s next? 352
19.16 Summary 353

Part 7 Enterprise systems 355

20 Jess, XML, and the enterprise 357
20.1 Enterprise applications 358
What is the J2EE? 359
What does that stand for? 359
20.2 Rules and XML 360
Interoperability 361
Editing and other processing 362
Storage and retrieval 362
20.3 XML-based rule representations 363
RuleML 363
DAML 364
Homegrown representations 365
Strategies for representing rules in XML 365
20.4 Representing Jess rules in XML 367
An example rule 368
Transforming the XML rules into Jess rules 369
20.5 Rule editors 372
20.6 Summary 374
21 Jess in the J2EE environment 375
21.1 A quick tour of EJB concepts 376
Kinds of EJBs 376
EJB restrictions 377
Do you need to use EJBs? 377
Accessing external resources from EJBs 378
21.2 An RMI-based rule server 379
The remote interfaces 379
Implementing the interfaces 380
Implementing a main method 382
Generating the stubs 382
A sample client 382
Final polishing 383
21.3 JSR 94: the javax.rules API 383
Working with javax.rules 384
The reference implementation 386
21.4 Summary 388

A Jess functions 389
B Abridged Java API for Jess 415
C An automated testing framework 427
index 433