Material for Further Study

5 Expert Systems We will be using the Drools Java expert system language and libraries in this chap- ter. Earlier editions of this book used the Jess expert system tool but due to the new more restrictive Jess licensing terms I decided to switch to Drools because it is released under the Apache 2.0 license. The primary web site for Drools is www.jboss.orgdrools where you can download the source code and documentation. Both Jess and Drools are forward chaining inference engines that use the Rete algo- rithm and are derived from Charles Forgy’s OPS5 language. One thing to keep in mind whenever you are building a system based on the Rete algorithm is that Rete scales very well to large numbers of rules but scales at O N 2 where N is the num- ber of facts in the system. I have a long history with OPS5, porting it to Xerox Lisp Machines 1982 and the Apple Macintosh 1984 as well as building custom ver- sions supporting multiple “worlds” of data and rule spaces. One thing that I would like to make clear: Drools is the only technology that I am covering in this book that I have not used professionally. That said, I spent some effort getting up to speed on Drools as a replacement for Jess on future projects. While there is some interest in using packages like Drools for “business rules” to capture business process knowledge, often as embedded components in large sys- tems, expert systems have historically been built to approach human level expertise for very specific tasks like configuring computer systems and medical diagnosis. The examples in this chapter are very simple and are intended to show you how to embed Drools in your Java applications and to show you a few tricks for using forward chaining rule-based systems. Drools is a Domain Specific Language DSL that attempts to provide a syntax that is easier to use than a general purpose pro- gramming language. I do not usually recommend Java IDEs a personal choice but if you already use Eclipse then I suggest that you use the Drools plugins for Eclipse the “Eclipse Drools Workbench” which help setting up projects and understand the Drools rule language syntax. The Eclipse Drools Workbench can automatically generate a small demo which I will go over in some detail in the next two sections. I then design and implement two simple example rule systems for solving block world type problems and for answering help desk questions. The material in this chapter exclusively covers forward chaining production systems 73 Drools Inference Engine Working Memory Facts Rules Developer Interactive development Eclipse Drools Workbench Application programs Drools API Figure 5.1: Using Drools for developing rule-based systems and then deploying them. also called “expert systems”. Forward chaining systems start with a set of known facts, and apply rules to work towards solving one or more goals. An alternative approach, often used in Prolog programs, is to use backward chaining. Backward chaining systems start with a final goal and attempt to work backwards towards currently known facts. The phrase, expert systems, was almost synonymous with artificial intelligence in the early and mid 1980s. The application of expert system techniques to real prob- lems, like configuring DEC VAX minicomputers, medical diagnosis, and evaluating seismic data for planning oil exploration had everyone very excited. Unfortunately, expert systems were over hyped and there was an eventual backlash that affected the entire field of AI. Still, the knowledge of how to write expert systems is a useful skill. This chapter contains a tutorial for using the Drools system and also shows how to use machine learning to help generate rules when training data is available. As seen in Figure 5.1, Drools development is interactive: you will work in an en- vironment where you can quickly add and change rules and re-run test cases. This interactive style of development is similar to using PowerLoom as we saw in Chapter 3. 74 I like to refer to expert systems by a more precise name: production systems. Pro- ductions are rules for transforming state. For example, given the three production rules: a = b b = c c = d then if a production system is initialized with the state a, the state d can be derived by applying these three production rules in order. The form of these production rules is: left-hand side = right-hand side or: LHS = RHS Like the PowerLoom reasoning system used in Chapter 3, much of the power of a rule-based system comes from the ability to use variables so that the left-hand side LHS patterns can match a variety of known facts called working memory in Drools. The values of these variables set in the LHS matching process are substi- tuted for the variables on the right-hand side RHS patterns. Production rule systems are much less expressive than Description Logic style rea- soners like PowerLoom. The benefits of production rule systems is that they should have better runtime efficiency and are easier to use – a smaller learning curve. Good advice is to use production systems when they are adequate, and if not, use a more expressive knowledge representation and reasoning system like PowerLoom.

5.2 The Drools Rules Language

The basic syntax leaving out optional components of a Drools rule is: rule a name for the rule when LHS 75 then RHS end What might sample LHS and RHS statements look like? Drools rules reference POJOs “Plain Old Java Objects” in both the LHS matching expressions and RHS actions. If you use the Eclipse Drools Workbench and create a new demo project, the Workbench will automatically create for you: • Sample.drl – a sample rule file. • com.sample.DroolsTest.java – defines: a simple Java POJO class M essage that is used in the Sample.drl rule file, a utility method for loading rules, and a main method that loads rules and creates an instance of the M essage class that “fires” the first rule in Sample.drl. Even if you decide not to use the Eclipse Drools Workbench, I include these two auto-generated files in the ZIP file for this book and we will use these files to intro- duce both the syntax of rues and using rules and Drools in Java applications in the next section. Here is the Sample.drl file: package com.sample import com.sample.DroolsTest.Message; rule Hello World when m : Messagestatus == Message.HELLO, message : message then System.out.printlnmessage; m.setMessageGoodbye cruel world; m.setStatusMessage.GOODBYE; updatem; end rule GoodBye no-loop true when m : Messagestatus == Message.GOODBYE, message : message then System.out.printlnmessage; 76