Jess in Action
Rule-Based Systems in Java
Ernest Friedman-Hill
  • June 2003
  • ISBN 9781930110892
  • 480 pages
  • printed in black & white

...clear, crisp, well-focused...the organization is smooth, well-thought-out,...this book rocks.

Ted Neward, Author of "Server-Based Java Programming"

Imagine a different way to program in which you specify rules and facts instead of the usual linear set of instructions. That's the idea behind rule-based programming. A rule engine automatically decides how to apply the rules to your facts and hands you the result. This approach is ideal for expressing business rules and is increasingly used in enterprise computing.

Jess is a popular rule engine written in Java. It's supported by Sandia Labs and has an active online community. If you have a problem that can be solved with rules, Jess in Action will show you how. (If you are not sure, read chapter 2.) Written by the creator of Jess, this book is an accessible and practical guide to rule-based system development in Java.

Table of Contents show full



about this book

author online

about the title

about the cover illustration

Part 1 Introducing rule-based systems

1. Rules to the rescue

1.1. Math class melee

1.1.1. Beyond logic puzzles

1.2. Some real-world examples

1.2.1. Mail filtering

1.2.2. Product configuration

1.2.3. Implementing business rules

1.3. Summary

2. What are rule-based systems?

2.1. The cooking/driving robot

2.1.1. Declarative programming: a different approach

2.2. Rules and rule engines

2.2.1. Expert systems

2.3. Architecture of a rule-based system

2.3.1. The inference engine

2.3.2. The rule base

2.3.3. The working memory

2.3.4. The pattern matcher

2.3.5. The agenda

2.3.6. The execution engine

2.4. Developing rule-based systems

2.4.1. Knowledge engineering

2.4.2. Structuring data

2.4.3. Testing

2.4.4. Interface building

2.4.5. Writing the rules

2.4.6. Iterative development

2.5. Rule engine standards

2.6. Summary

Part 2 Jess: A rule-based programming environment

3. Introducing Jess

3.1. The Jess rule engine

3.1.1. Obtaining Jess

3.1.2. Installing Jess

3.1.3. Running Jess

3.1.4. Hello, World?

3.2. Jess applications

3.2.1. Command line, GUI, or embedded?

3.2.2. Jess performance

3.3. Summary

4. Getting started with the Jess language

4.1. The basics

4.1.1. Whitespace

4.1.2. Symbols

4.1.3. The jess.Value class

4.1.4. Numbers

4.1.5. Strings


4.2. Adding some structure

4.2.1. Lists

4.2.2. Calling functions

4.2.3. Variables

4.2.4. More about lists

4.3. Control flow

4.3.1. foreach

4.3.2. while

4.3.3. if/then/else

4.3.4. progn

4.3.5. apply

4.3.6. eval and build

4.4. Defining functions with deffunction

4.4.1. Late binding

4.5. Fine-tuning a function’s behavior

4.5.1. Take my advice, please

4.6. Summary

5. Scripting Java with Jess

5.1. Creating Java objects

5.2. Calling Java methods

5.2.1. Nesting function calls, and a shortcut

5.2.2. Calling static methods

5.2.3. Calling set and get methods

5.2.4. Working with arrays

5.2.5. How Jess chooses among overloaded methods

5.3. Accessing Java member data

5.4. Working with exceptions

5.5. Summary

6. Representing facts in Jess

6.1. Jess’s working memory

6.1.1. Manipulating the working memory

6.2. Just the facts, ma’am

6.3. Unordered facts

6.3.1. The deftemplate construct

6.3.2. Default slot values

6.3.3. Multislots

6.3.4. Changing slot values with modify

6.3.5. Copying facts with duplicate

6.4. Ordered facts

6.5. Shadow facts

6.5.1. Jess and JavaBeans

6.5.2. JavaBeans have "slots"

6.5.3. An example JavaBean

6.5.4. Creating a deftemplate for DimmerSwitch

6.5.5. Putting a DimmerSwitch into working memory

6.5.6. Static vs. dynamic shadow facts

6.5.7. Adding PropertyChangeListener support to DimmerSwitch

6.5.8. Shadow facts and working memory functions

6.6. Summary

7. Writing rules in Jess

7.1. Forward-chaining rules

7.1.1. Patterns and shadow facts

7.2. Constraining slot data

7.2.1. Literal constraints

7.2.2. Variables as constraints

7.2.3. Connective constraints

7.2.4. Constraining matches with predicate functions

7.2.5. Return value constraints

7.2.6. Pattern bindings

7.3. Qualifying patterns with conditional elements

7.3.1. The and conditional element

7.3.2. The or conditional element

7.3.3. The not conditional element

7.3.4. The test conditional element

7.3.5. The logical conditional element

7.4. Backward-chaining rules

7.5. Managing the agenda

7.5.1. Conflict resolution

7.5.2. Changing rule priority with salience

7.6. Partitioning the rule base with defmodule

7.6.1. Defining constructs in modules

7.6.2. Modules, scope, and name resolution

7.6.3. Module focus and execution control

7.7. Searching working memory with defquery

7.7.1. The variable declaration

7.7.2. Query trigger facts

7.7.3. The count-query-results function

7.7.4. Backward chaining and queries

7.8. Summary

8. Under the hood: how Jess works

8.1. Review of the problem

8.2. An inefficient solution

8.3. The Rete algorithm

8.3.1. How Rete works

8.3.2. Handling retract

8.4. Easy optimizations for Rete

8.5. Performance of the Rete algorithm

8.5.1. Node index hash value

8.6. More complexity and initial-fact

8.6.1. Implementing the not conditional element

8.6.2. Implementing the test conditional element

8.6.3. Implementing backward chaining

8.7. Exploring the Rete network in Jess

8.7.1. The (watch compilations) command

8.7.2. The view function

8.7.3. The matches function

8.8. Summary

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

9. Collecting the knowledge

9.1. The Tax Forms Advisor

9.2. Introduction to knowledge engineering

9.2.1. Where do you start?

9.2.2. Interviews

9.2.3. Desk research

9.3. Collecting knowledge about tax forms

9.3.1. An interview

9.3.2. Reviewing the forms

9.3.3. Next steps

9.4. Summary

10. Designing the application

10.1. Organizing the data

10.2. Filling in details

10.2.1. Default slot values

10.3. More templates

10.4. Templates you don’t need

10.5. Organizing the rules

10.6. Building the infrastructure

10.6.1. Simple text-based I/O

10.6.2. Fetching the question text

10.7. Summary

11. Writing the application

11.1. Welcoming the user

11.1.1. Testing the startup module

11.2. Asking the user questions

11.2.1. Income and dependents

11.2.2. Dealing with special circumstances

11.2.3. Testing the interview module

11.3. Recommending forms

11.4. Explaining the results

11.4.1. Testing the report module

11.5. Finishing touches

11.6. Testing the full application

11.7. Summary

Part 4 Writing a diagnostic application: the PC Repair Assistant

12. Writing the PC Repair Assistant

12.1. Using flowcharts in knowledge engineering

12.1.1. From flowcharts to rules

12.2. The problem domain

12.2.1. Writing the first rules

12.3. Asking questions with backward chaining

12.4. Checking the answers

12.4.1. Modifying the ask module

12.5. The rest of the rules

12.5.1. Rules about sound

12.5.2. Degrading gracefully

12.5.3. To boot, or not to boot

12.5.4. RAM problems

12.5.5. Questioning authority

12.6. Testing

12.7. Summary

13. Adding a graphical interface

13.1. Getting started

13.2. Displaying a window

13.3. Displaying questions

13.4. Getting answers

13.4.1. The main thread vs. the event thread

13.5. Better input components

13.6. Finishing touches

13.7. Testing the interface

13.8. Summary

Part 5 Reasoning about reality: the HVAC Controller

14. The reality connection

14.1. The system

14.2. Defining the hardware interface

14.2.1. Native methods

14.2.2. Writing a simulator

14.2.3. Simulating getTemperature

14.2.4. Adding a graphical interface

14.3. Writing the JavaBeans

14.3.1. Rules about Thermometers

14.3.2. Writing the other Beans

14.4. JavaBeans and serialization

14.5. Summary

15. Extending the Jess language

15.1. The Userfunction interface

15.1.1. The getName method

15.1.2. The call method

15.1.3. Loading a Userfunction into Jess

15.2. Handling arguments

15.2.1. How many arguments?

15.2.2. Using arguments

15.2.3. Resolving variable arguments

15.2.4. Resolving function call arguments

15.3. Returning a value

15.3.1. Constructing Value objects

15.4. Beyond simple examples

15.4.1. Holding state

15.4.2. Multiple personalities

15.4.3. Userfunctions and serialization

15.4.4. Grouping functions with Userpackage

15.5. The HVAC functions

15.5.1. Creating a simulator

15.5.2. Counting devices

15.5.3. Matching heat pumps and floors

15.5.4. Operating the hardware

15.5.5. Implementing a Userpackage

15.6. Testing

15.7. Summary

16. Writing the rules

16.1. The control algorithm

16.1.1. Knowledge engineering with truth tables

16.1.2. How heat pumps work

16.1.3. Using guard lines

16.1.4. Saving energy

16.2. Setting up

16.3. Controlling the heat pumps

16.3.1. Enough is enough

16.3.2. The moment of truth

16.4. Controlling the vents

16.4.1. The vent rules

16.5. Testing the whole system

16.6. Controlling with fuzzy rules

16.6.1. Fuzzy logic, briefly

16.6.2. The Fuzzy HVAC Controller

16.6.3. Exploring the fuzzy controller

16.7. What’s next?

16.8. Summary

Part 6 rule-based applications for the Web

17. Jess on the Web

17.1. Java architectures for the Web

17.1.1. Fat-client applications

17.1.2. Thin-client applications

17.2. A Jess application for the Web

17.3. Knowledge engineering

17.4. Designing data structures

17.5. Writing the rules

17.5.1. About testing

17.5.2. The recommend-requirements rule

17.5.3. Recommending videos and DVDs

17.5.4. Conspicuous consumption

17.5.5. More media rules

17.6. Refining the recommendations

17.7. Some useful queries

17.7.1. Maintaining the order number

17.8. Cleaning up

17.9. Summary

18. Embedding Jess in Java applications

18.1. Getting started with the Jess library

18.1.1. The executeCommand method

18.1.2. Exchanging Java objects

18.1.3. Beyond executeCommand

18.2. Working with Fact objects in Java

18.2.1. Multislots

18.2.2. Ordered facts

18.2.3. Removing facts

18.3. Working with JavaBeans

18.4. Calling Jess functions from Java

18.5. Working with JessException

18.5.1. Nested exceptions

18.5.2. Rolling your own

18.6. Input and output

18.6.1. Using custom routers

18.7. Summary

19. Deploying web-based applications

19.1. The Java Servlet API

19.2. J2EE and the Tomcat engine

19.2.1. Deploying the Hello servlet

19.3. Your first Jess servlet

19.3.1. Deploying the Jess servlet

19.3.2. Cleaning up the URL

19.4. Application architecture: take one

19.5. Starting the Catalog servlet

19.5.1. JavaServer Pages

19.5.2. Forwarding to a JSP

19.6. Application architecture, take two

19.7. The login screen

19.8. The Catalog servlet

19.8.1. Initializing Jess

19.8.2. Getting the login name

19.8.3. Starting a user session

19.8.4. Querying the product list

19.8.5. Invoking the JSP

19.8.6. The catalog JSP

19.9. Testing

19.10. The Recommend servlet

19.10.1. Getting started

19.10.2. Creating the order

19.10.3. Getting the recommendations

19.10.4. Forwarding to JSPs

19.11. The recommend JSP

19.12. The Purchase servlet

19.13. Persistence

19.14. Deploying the application

19.15. What’s next?

19.16. Summary

Part 7 Enterprise systems

20. Jess, XML, and the enterprise

20.1. Enterprise applications

20.1.1. What is the J2EE?

20.1.2. What does that stand for?

20.2. Rules and XML

20.2.1. Interoperability

20.2.2. Editing and other processing

20.2.3. Storage and retrieval

20.3. XML-based rule representations

20.3.1. RuleML

20.3.2. DAML

20.3.3. Homegrown representations

20.3.4. Strategies for representing rules in XML

20.4. Representing Jess rules in XML

20.4.1. An example rule

20.4.2. Transforming the XML rules into Jess rules

20.5. Rule editors

20.6. Summary

21. Jess in the J2EE environment

21.1. A quick tour of EJB concepts

21.1.1. Kinds of EJBs

21.1.2. EJB restrictions

21.1.3. Do you need to use EJBs?

21.1.4. Accessing external resources from EJBs

21.2. An RMI-based rule server

21.2.1. The remote interfaces

21.2.2. Implementing the interfaces

21.2.3. Implementing a main method

21.2.4. Generating the stubs

21.2.5. A sample client

21.2.6. Final polishing

21.3. JSR 94: the javax.rules API

21.3.1. Working with javax.rules

21.3.2. The reference implementation

21.4. Summary

Appendix A: Jess functions

Appendix B: Abridged Java API for Jess

Appendix C: An automated testing framework


About the book

Jess in Action first introduces rule programming concepts and teaches you the Jess language. Armed with this knowledge, you then progress through a series of fully-developed applications chosen to expose you to practical rule-based development. The book shows you how you can add power and intelligence to your Java software.

What's inside

  • Introduction to rule-based thinking
  • Jess language tutorial
  • Complete examples of ...
    • Tax forms advisor
    • Diagnostic assistant
    • Fuzzy logic controller
    • Web agent
    • J2EE apps

About the author

Dr. Friedman-Hill is a Principal Member of the Technical Staff at Sandia National Laboratories in Livermore, California. He is the developer of Jess, the Java rule engine. He has taught Java programming to over 3,000 students for the University of California Extensions in Berkeley, in San Diego, and online. Dr. Friedman-Hill's current work at Sandia includes the development of software for mechanical design and analysis. He lives in Gaithersburg, MD.

Ernest Friedman-Hill Interview

Read Jason Morris's interview with the author of Jess in Action.

eBook $25.00 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

...the Jess book. A nice balance between an introduction and a reference...

John D. Mitchell, Coauthor of "Making Sense of Java"

Friedman-Hill writes clearly. The topic is complicated, and he does an excellent job explaining it...I recommend this book.

Roedy Green, Author of "The Java Glossary"

---intuitive and clever examples that show the reader how to build intelligent Java applications with Jess.

Robert B. Trelease, Ph.D., UCLA Brain Research Institute