9. Jess Application Design

9.1. What makes a good Jess application?

Jess is a rule engine - a special kind of software that very efficiently applies rules to data. A rule-based program can have hundreds or even thousands of rules, and Jess will continually apply them to data stored in its working memory. The rules might represent the knowledge of a human expert in some domain, a set of business rules, a technical specification, or the rules of a game. The working memory might represent the state of an evolving situation (an interview, an emergency), the contents of a database, the status of a Web user's session, or a commercial account.

Many articles, and indeed, entire books have been written about rule-based systems and how to design them. It's worth reading some of this literature to give yourself an idea of what rule engines can and can't do. The most important piece of advice I can give you is that if you already know how to solve a problem procedurally, then do it that way. Don't use a rule engine and then try to force it to execute a specific sequence of rules. The whole point of a rule engine is that it's good at finding the right sequence of rules to apply, all by itself.

9.2. Command-line, GUI, or embedded?

As we've discussed, Jess can be used in many ways. Besides the different categories of problems Jess can be applied to, being a library, it is amenable to being used in many different kinds of Java programs. Jess can be used in command-line applications, GUI applications, servlets, and applets. Furthermore, Jess can either provide the Java main() 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 which are controlled entirely by Java code you write, with a minumum of Jess language code.

The most important step in developing a Jess application is to choose an architecture from among the almost limitless range of possibilities. One way to organize the possibilities is to list them in increasing order of the amount of Java programming involved.

  1. Pure Jess language scripts. No Java code at all.
  2. Pure Jess language scripts, but the scripts access Java APIs.
  3. Mostly Jess language scripts, but some custom Java code in the form of new Jess commands written in Java.
  4. Half Jess language scripts, with a substantial amount of Java code providing custom commands and APIs; main() provided by Jess.
  5. Half Jess language scripts, with a substantial amount of Java code providing custom commands and APIs; main() provided by you or an application server.
  6. Mostly Java code, which loads Jess language scripts at runtime.
  7. All Java code, which manipulates Jess entirely through its Java API.

Examples of some of these types of applications are packaged with Jess. The basic examples like wordgame.clp, zebra.clp, and fullmab.clp are all type 1) programs. awtdraw.clp, swingdraw.clp and jframe.clp are type 2) programs. The example in this chapter is a type 6 application.

Your choice can be guided by many factors, but ultimately it will depend on what you feel most comfortable with. Types 4) and 5) are most prevalent in real-world applications.