In this chapter you’ll...
- Learn how to obtain and install your copy of Jess
- Learn how to run Jess programs
- See how Jess applications are structured
This is a hands-on book. It walks you through the development of several large software systems that use rule-based technology. You’ll see how each system is architected, and you’ll see the detailed implementation of each one. All the example systems use the Jess rule engine. In this chapter, we’ll take a closer look at Jess itself. Jess is an interpreter for the Jess rule language. The syntax of the Jess rule language is similar to that of Lisp, so it might look strange to you at first; but Jess’s language is simple, easy to learn, and well-suited to both defining rules and procedural programming. Although Jess is in some sense fairly small, it’s a very rich environment. Even after you’ve spent the next few chapters learning about Jess and its rule language, there will be plenty more to learn on the fly as you develop the major applications.
Although previous experience with Lisp might help you begin to understand the Jess rule language, it is not necessary; this book explains all you need to know. On the other hand, I assume you have some familiarity with Java, and I assume you have a Java environment installed and know how to use it to compile and run Java applications.
Jess (http://herzberg.ca.sandia.gov/jess) is a rule engine and scripting language developed at Sandia National Laboratories in Livermore, California in the late 1990s. It is written in Java, so it is an ideal tool for adding rules technology to Java-based software systems.
The CLIPS expert system shell (http://www.ghgcorp.com/clips/CLIPS.html), an open-source rule engine written in C, was the original inspiration for Jess. Jess and CLIPS were written by entirely different groups of people, however, and their implementations have always been very different. Jess is dynamic and Java-centric, so it automatically gives you access to all of Java’s powerful APIs for networking, graphics, database access, and so on; CLIPS has none of these facilities built in. Still, there is a strong similarity between the rule languages supported by these two systems. Many of the core concepts of Jess were originally derived from CLIPS, which was itself influenced by early rule engines like OPS5 and ART.
Note for Clips Users
Jess’s syntax is quite similar to CLIPS’, but Jess and CLIPS are different and unrelated systems. Some Jess constructs (defclass, definstance, defmodule) have very different semantics in CLIPS, whereas others (defrule) are virtually identical. Jess has many features (defquery, the ability to directly call functions in the host language) that CLIPS doesn’t, and Jess does not implement everything that CLIPS does (COOL, the CLIPS Object Oriented Language, is one notable example). If you have previous experience using CLIPS, don’t assume you can skip over this part of the book.
You can download a specially licensed version of Jess from the Manning Publications web site, at http://www.manning.com/friedman-hill. The license lets you use Jess for educational purposes, so you can try out and experiment with the examples in this book. In this chapter I’ll assume you’re using that version of Jess.
Jess is also available from the Jess web site, http://herzberg.ca.sandia.gov/jess. There you can immediately download a trial version, or you can obtain a Jess license and then download a full version with source code. Jess licenses are available free of charge for academic use. You can also purchase a commercial license. See the web site for details.
To run Jess, you need to have the Java 2 Platform installed. If you don’t already have it, you can download a free implementation directly from Sun Microsystems at http://java.sun.com. Versions are available for Windows, Linux, and Solaris. Apple has its own version, which is included in Macintosh OS X. If Java is properly installed, you should be able to execute the command java -version in a command window and see version information about your installed Java software. If the version number is 1.2.0 or greater, you’re ready to go.
The Jess distribution is a Zip file, and you can open it with many popular archiving tools. (WinZip [http://www.winzip.com] is one useful program for working with Zip files on Microsoft operating systems.) The Zip file contains a single item: a directory named JessXX, where XX is a number (currently 61, for Jess 6.1). Use your archiving tool to unpack that directory to a convenient location—C:\JessXX, for example. The unpacked directory will contain:
- A Java archive file jess.jar, which contains the Jess software.
- The directory examples/, which contains some simple examples of Jess programs.
- A copy of the Jess manual in the directory docs/. Open docs/index.html in your web browser to view the manual.
To complete your installation, add jess.jar to your CLASSPATH environment variable. CLASSPATH tells the Java program where to find installed Java software.
The details of setting environment variables vary between platforms. For older versions of Windows, it involves editing the C:\AUTOEXEC.BAT file. For newer Microsoft operating systems, you can set environment variables in the System control panel. For Unix-like systems, editing your .cshrc or .profile will do the trick. Refer to your operating system’s documentation for details.
CLASSPATH consists of a series of filenames joined by the character your operating system uses to separate filenames in other kinds of lists. On Windows, this is the semicolon (;) character, and on Unix-like systems it is the colon (:). Make sure that CLASSPATH, at a minimum, includes the full path to the jess.jar file and a period (.), which represents the current directory. So, on Windows, if there is no pre-existing CLASSPATH setting, you set this variable to .;C:\Jess61\jess.jar. If you find that your system already has a CLASSPATH variable defined, you can simply add these two entries to the existing list.
Jess is primarily intended as a library that can be embedded in other Java software. However, when you’re developing Jess code, it’s nice to have an interactive environment to work with, so Jess comes complete with a simple command prompt. To run Jess as a standalone command-line application, execute the class jess.Main from the JAR file like this:
C:\> java jess.Main Jess, the Java Expert System Shell Copyright (C) 2003 E.J. Friedman Hill and the Sandia Corporation Jess Version 6.1 4/9/2003 Jess>
Jess displays a welcome banner and then the Jess> prompt. When Jess displays this prompt, it is waiting for you to enter a Jess program. You can probably guess what’s coming next: You’re going to write the Jess version of “Hello, World”.
You can enter a Jess program directly at the command prompt, and Jess will execute it immediately:
Jess> (printout t "Hello, World!" crlf) Hello, World! Jess>
C:\Jess61> java jess.Main hello.clp Hello, World! C:\Jess61>
We will begin our study of the Jess language in the next chapter, but if you’re curious: printout is a function that prints formatted output; t tells printout to print to standard output; and the argument crlf starts a new line, like printing a \n in Java. This parenthesized list of symbols is the Jess way of calling a function. Jess immediately evaluates the call to printout, producing the side effect of printing “Hello, World!” to the console.
In addition to jess.Main, the class jess.Console presents the same command-line interface in a graphical console window (see figure 3.1). If you would like a more elaborate graphical interface, you can try JessWin, a free graphical development environment for Jess written by William Wheeler. JessWin (which is also written in Java) is menu-driven and contains an integrated graphical editor (see figure 3.2). You can download JessWin from the Jess web site (http://herzberg.ca.sandia.gov/jess/user.html).
Figure 3.1. The jess.Console interface for Jess. This as an alternative to the command-line jess.Main interface. You can enter Jess code in the small text field at the bottom left, and Jess output appears in the scrolling text area at the top.
Figure 3.2. The JessWin developer’s environment for Jess. JessWin is a third-party add-on that provides a graphical interface to many functions that are useful to Jess programmers.
- Expert systems that evaluate insurance claims and mortgage applications
- Agents that predict stock prices and buy and sell securities
- Network intrusion detectors and security auditors
- Design assistants that help mechanical engineers
- Smart network switches for telecommunications
- Servers to execute business rules
- Intelligent e-commerce sites
You’ll develop some fairly large applications in this book: the Tax Forms Advisor (an intelligent information kiosk), the PC Repair Assistant (a graphical help desk application), an HVAC Controller (a soft real-time control system), and the Recommendations Agent (a smart e-commerce web site).
You can program with Jess in two different but overlapping ways. First, you can use Jess as a rule engine. A rule-based program can have hundreds or even thousands of rules, and Jess will continually apply them to your data. Often the rules represent the heuristic knowledge of a human expert in some domain, and the knowledge base represents the state of an evolving situation (perhaps an interview or an emergency). In this case, the rules are said to constitute an expert system. Expert systems are widely used in many domains. The newest applications of expert systems include being used as the reasoning part of intelligent agents, in enterprise resource planning (ERP) systems, and in order validation for electronic commerce.
The Jess language is also a general-purpose programming language, and it can directly access all Java classes and libraries. For this reason, Jess is also frequently used as a dynamic scripting or rapid application development environment. Whereas Java code generally must be compiled before it can be run, Jess interprets code and executes it immediately upon being typed. This allows you to experiment with Java APIs interactively and build up large programs incrementally. It is also easy to extend the Jess language with new commands written in Java or in Jess itself, so the Jess language can be customized for specific applications.
Jess is therefore useful in a wide range of situations. In this book, you will see Jess used primarily for its rule engine capabilities—but there will be plenty of scripting along the way. How can you choose an architecture for a specific application? As with many things in Jess, lots of choices are available.
Given its flexibility, Jess can be used in command-line applications, GUI applications, servlets, and applets. Furthermore, Jess can provide the Java main() function for your program, or you can write it yourself. You can develop Jess applications (with or without GUIs) without compiling a single line of Java code. You can also write Jess applications that are controlled entirely by Java code you write, with a minimum of Jess language code. Jess has been deployed in everything from enterprise applications using J2EE on mainframes to personal productivity applications on handheld devices. If you can think of it, you can probably implement it with Jess.
The most important step in developing a Jess application is to choose an architecture from among the almost limitless range of possibilities. You must make this choice early in the development of your application. One way to organize the possibilities is to list them in increasing order of the amount of Java programming involved:
- Pure Jess language, with no Java code.
- Pure Jess language, but the program accesses Java APIs.
- Mostly Jess language code, but with some custom Java code in the form of new Jess commands written in Java.
- Half Jess language code, with a substantial amount of Java code providing custom commands and APIs. Jess provides the main() function (the entry point for the program).
- Half Jess language code, with a substantial amount of Java code providing custom commands and APIs. You write the main() function.
- Mostly Java code, which loads Jess language code at runtime.
- All Java code, which manipulates Jess entirely through its Java API.
The sample applications presented in the later parts of this book start at the beginning of this continuum (the Tax Forms Advisor developed in part 3 consists entirely of Jess code) and work their way toward the end (the business-rules systems are mostly written in Java). Experiencing the development of each type of application will help you decide what route to take in future development, based both on the requirements of the application and on the abilities of the programming team that will write it.
Some people will tell you that Java is slow. They’re wrong. Modern Java virtual machines are extremely powerful and sophisticated. In many applications, Java is as fast as comparable C or C++ code. For Jess, being written in Java is not a liability.
Jess is fast. The algorithm used for pattern matching, which we’ll study in chapter 8, is very efficient, and Jess can plow through large piles of rules and facts in little time. Using Sun’s HotSpot JVM on an 800 MHz Pentium III, Jess can fire more than 80,000 rules per second; it can perform almost 600,000 pattern-matching operations per second; it can add more than 100,000 facts to working memory per second; and a simple counting loop can do 400,000 iterations per second. Independent benchmarks have shown that Jess is significantly faster than many rule engines written in the “faster” C language. For example, on many problems, Jess outperforms CLIPS by a factor of 20 or more on the same hardware.
1 See http://aaaprod.gsfc.nasa.gov/teas/Jess/JessUMBC/sld025.htm and http://www.mail-archive.com/ email@example.com/msg03278.html for some benchmarks.
Jess’s rule engine uses an improved form of a well-known method called the Rete algorithm (Rete is Latin for net) to match rules against the working memory. We’ll look at the Rete algorithm in detail in chapter 8. The Rete algorithm explicitly trades space for speed, so Jess can use a lot of memory. Jess does contain commands that let you sacrifice some performance to decrease memory usage. Nevertheless, Jess’ memory usage is not ridiculous, and fairly large programs will fit easily into Java’s default heap size of 64 megabytes.
Because Jess is a memory-intensive application, its performance is sensitive to the behavior of the Java garbage collector. Recent JVMs from Sun feature an advanced Java runtime called HotSpot, which includes a flexible, configurable garbage collection (GC) subsystem. The garbage collector is the part of the JVM that is responsible for finding and deleting unused objects. Excellent articles on GC performance tuning are available at Sun’s web site. Although every Jess rule base is different, in general, Jess benefits if you adjust two parameters: the heap size and the object nursery size. For example, on my machine, Jess’ performance on the Miranker manners benchmark (http://www-2.cs.cmu.edu/afs/cs/project/airepository/ai/areas/expert/bench/bench/0.html) with 90 guests is improved by 25% by adjusting the initial heap size and nursery size to 32MB and 16MB, respectively, from their defaults of 64MB and 640KB. In this case, you make the default heap size smaller, which makes the garbage collector run faster because there is less memory to search. You can tune the HotSpot virtual machine in this way using the following command:
2 See in particular http://developer.java.sun.com/developer/TechTips/2000/tt1222.html#tip2.
java -XX:NewSize=16m -Xms32m -Xmx32m jess.Main <scriptfile>
The object nursery is a subset of the Java heap set aside for recently allocated objects. The total heap size in this example is 32MB, not 48MB.
Jess is a powerful environment for processing rules and scripting the Java platform. You can use it in a wide range of applications, built purely using the Jess rule language, purely in Java, or with some mixture of the two.
You can run Jess as an interactive command-line application during development using the jess.Main class. Jess programs can also be stored as plain text files and executed by jess.Main.
Jess works with any Java 2 virtual machine. Jess is very fast, but its performance is sensitive to the detailed operation of the Java garbage collector. You can often tune the performance of a Jess application by tuning the behavior of the garbage collector.
The next five chapters will teach you the Jess language in detail. First you’ll learn to use Jess as a pure programming language, and then you’ll see how to write rules. Along the way, especially in chapter 8, we’ll peer under the hood and see how Jess works. Let’s get started!