![]() |
|
Module 4: Creating your first knowledge base
A knowledge base defines an expert system as a collection of if-then rules,
definitions of how users will be asked for information and control variables
that influence the way the consultation is carried out.
We'll assume the following before we start this exercise:
The scenario: You're going to build a very basic expert system
designed to help someone prepare for the weather as they head out to work
in the morning. As a first step we'll build the simplest possible
knowledge base consisting of one if-then rule,
one prompt for user input and one definition of
a goal for the consultation with the expert system. Later we'll
extend the system's functionality by adding more rules, prompts and
control statements. Interative refinement of a knowledge base is
the recommended approach for realistic problems as well as this training
example.
Here's our first cut at a knowledge base. You will need to type
(or cut and paste) this into your text editor, then save it as "weather.kb"
in your "expertsys" directory you created in Module 2.
Let's dissect this example to see how its elements
interact to define a knowledge base. Full details of each knowledge
base elements are describe in tables in the
Reference.
REM statements: These are single line comments, ignored when
the knowledge base is processed. The REM command must appear at the
beginning of the line, and everything else on that line is ignored.
Blank lines: Empty lines are optional and are ignored.
They may be used to improve readability of the knowledge base.
RULE definitions: Possible elements of each rule include:
PROMPT definitions: Prompts should be included in the
knowledge base after all rules are defined. Possible elements of
each prompt include:
GOAL definitions: Each GOAL is defined on a single line that
begins with GOAL followed by the name of the goal attribute enclosed
in square brackets. GOALs may be specified before or after PROMPTS, but
must follow RULE definitions. There has to be at least one GOAL
statement in a knowledge base: GOALs are the attributes for which the
inference engine seeks values. The inferencing process ends when all
GOALs have been resolved or cannot be resolved.
Installing and running the knowledge base:
To use the example you will need to construct a Web page that
loads the e2gLite applet and identifies the knowledge base.
Type in (or cut and paste) the following Web page then save it
with the name weather.html. To keep things simple, create a
subdirectory named "expertsys" on your Web server and put the
Web page (weather.html), the knowledge base (weather.kb)
and the e2gLite applet archive (e2glite.jar) in this subdirectory.
The APPLET CODE and ARCHIVE values are case sensitive and must be
entered with all lower case characters.
The WIDTH and HEIGHT properties of the APPLET tag represent the
display size of the applet. 450 by 300 is a reasonable minimum size.
e2gLite PROMPTs do not scroll, so the applet must be sized adequately
to allow display of all PROMPT questions, choices and the control buttons.
PROMPT questions or choices that are too long to fit across the applet will
be truncated and terminated with ellipses (...). Lines are not wrapped.
If the necessary components (weather.html, weather.kb and e2glite.jar)
are in the "expertsys" subdirectory of your Web server root directory,
start the applet as follows:
REM Test Knowledge Base (weather.kb)
RULE [Is it going to rain?]
If [precipitation] = "expected"
Then [the recommendation] = "wear a raincoat"
PROMPT [precipitation] MultChoice CF
"According to the weather forecast, precipitation is:"
"expected"
"not expected"
GOAL [the recommendation]
<HTML>
<HEAD><TITLE>Weather Advice (weather.html)</TITLE></HEAD>
<BODY>
<APPLET CODE="e2glite.e2g.class" ARCHIVE="e2glite.jar" WIDTH=450 HEIGHT=300>
<PARAM NAME="KBURL" VALUE="weather.kb">
Java is required to view this page!
</APPLET>
</BODY>
</HTML>
If you have placed the components in another virtual directory, you will need to adjust the Web address accordingly. If you are using an academic or other free Web site, your address will probably look something like the following:
If you are running with files, you need to load the weather.html Web page as a file. Assuming weather.html, weather.kb and e2glite.jar are all in the expertsys subdirectory on a floppy disk, you could start the expert system from Netscape or Internet Explorer as follows:
Here's what you should see when you access the weather.html Web page:

If you see an error message displayed on the e2gLite opening screen you probably have made a typographical error in entering either the knowledge base or the Web page. The meaning of the error codes is included in the Reference materials, but at this point it might be best to cut-and-paste the text into your editor to avoid errors.
If you do not see the opening screen at all, open the browser's Java console. Your browser will provide a menu item for doing this. It is on the "View" menu in Internet Explorer and the "Communicator/Tools" menu in Netscape. For Internet Explorer you must also set a browser preference that allows use of the console. The option is under "Microsoft VM" on the "Advanced" panel accessed from the "Internet Options" entry on the "Tools" menu. There may be a message displayed in the Java console that indicates a problem in your installation of the components required to use e2gLite. If the applet (.class file) or archive (.jar file) cannot be found, make sure you have the names in the <APPLET> tag spelled correctly and in the correct (lower) case. Error messages that indicate problems with missing methods or missing classes suggest you're using a browser that installs a version 1.0.2 Java Virtual Machine (JVM). To allow the use of e2gLite with these browsers, you must unpack the e2glite.jar with WinZip or a similar program. This places the .class files defining the applet into a subdirectory named e2glite from which they can be loaded by JVM versions that cannot load from an archive.
Click the button labeled "Start the Consultation" and the knowledge base will be loaded. You should see the first PROMPT. If you see an error message, you have mistyped something in the knowledge base. For now, try cutting-and-pasting the knowledge base into your file. We'll discuss the debug mode for identifying errors shortly.

Click the Why ask? button to see the inference engine's explanation of why this question has been asked. You'll see the goal or subgoal the inference engine is currently working on, the attribute it's trying to find and the rule that needs the value of this attribute:

Click Return then the expected radio button and the Submit your response button. The inference engine has enough information to conclude the interview with a recommendation to wear a raincoat.

If you click the Explain button with all conclusions showing in the drop down list of resolved attribute values you will see the following detailed explanation of the recommendation.

Running in the debug mode: Before enhancing the knowledge base, let's take a look at e2gLite's optional debugging information. To run in debug mode, the following parameter specification must be added to the <APPLET> definition in weather.html:
The parameter name DEBUG has to be in upper case or it will be ignored. Make this change to weather.html and reload the Web page. If you are using Netscape and do not see the e2gLite opening page and "Start the consultation" button, try reloading the page from the address line rather than with the reload button. If this doesn't work, exit and reload the browser. When you click the start button, a movable and resizeable debug window opens on the screen:

When the debug window opens, e2gLite is running with the trace mode on: a step-by-step description of the inference engine's actions is displayed. Clicking the Trace is ON/OFF button toggles this output off and on. We'll examine the Display KB dump and Analyze KB buttons after adding a few enhancements to the weather knowledge base. Clicking the Close debug window button removes the window. It can only be reopened by reloading the Web page to restart the e2gLite applet.
The debug window provides information beyond that displayed in the applet window about syntax errors discovered when a knowledge base is loaded by clicking the "Start the consultation" button. Suppose we have omitted the left square bracket from the "precipitation" attribute name in the rule premise. When the "Start the consultation" button is clicked, the following error message appears in the debug window:

A complete list of error codes is found in the Reference materials. Error 400 occurs when a delimited attribute name or value was expected but not found.
Knowledge base enhancements: As a first enhancement, we'll add some parameters between the <APPLET> tags to give your demo a title, subtitle, custom start button message and custom background color. A complete description of all available parameters is provided in the Reference section. Here's an updated version of weather.html with the necessary parameters added:
<HTML> <HEAD><TITLE>Weather Advice (weather.html)</TITLE></HEAD> <BODY> <APPLET CODE="e2glite.e2g.class" ARCHIVE="e2glite.jar" WIDTH=450 HEIGHT=300> <PARAM NAME="KBURL" VALUE="weather.kb"> <PARAM NAME="DEBUG" VALUE="true"> <PARAM NAME="APPTITLE" VALUE="Weather Demonstration"> <PARAM NAME="APPSUBTITLE" VALUE="by The Weather Wiz"> <PARAM NAME="TITLECOLOR" VALUE="#FF0000"> <PARAM NAME="PROMPTCOLOR" VALUE="#FF0000"> <PARAM NAME="BGCOLOR" VALUE="#00FFFF"> <PARAM NAME="STARTBUTTON" VALUE="Recommend what to wear"> Java is required to view this page! </APPLET> </BODY> </HTML> |
Colors are specified using 24 bit hexadecimal values as is done in HTML. The example parameters set the background color to cyan (blue-green) and the text colors to red.
Next, we'll modify the weather.kb knowledge base to increase the scope of the expert system's possible recommendations.
REM Test Knowledge Base (weather.kb) RULE [Is it going to rain?] If [precipitation] = "expected" and [the expected temperature] > 32 Then [the recommendation] = "wear a raincoat" RULE [Is it going to snow?] If [precipitation] = "expected" and [the expected temperature] < 33 Then [the recommendation] = "wear your boots" @ 95 PROMPT [precipitation] MultChoice CF "According to the weather forecast, precipitation is:" "expected" "not expected" PROMPT [the expected temperature] Numeric CF "The expected daytime temperature (degrees Fahrenheit) is:" "-30" "120" GOAL [the recommendation] DEFAULT [the recommendation] = "wear whatever you want" @ 90 |
The enhancement deals with seasonal precipitation and illustrates a Numeric PROMPT that will accept values between -30 and 120. It also shows how to assert a fact with less than absolute certainty: the recommendation to "wear your boots" is made with 95% confidence. Finally, the enhanced knowledge base introduces the use of a DEFAULT. If the value of the recommendation cannot be found any other way, it will be set to "wear whatever you want" with 90% confidence.
Update your weather.html and weather.kb files and run the example by loading weather.html. You might want to decrease the size of the browser window and rearrange the windows on your screen so that you can view the applet and debug output at the same time. After clicking the Recommend what to wear button that loads the knowledge base, click the Analyze KB button in the debug window.

The knowledge base analysis is helpful in finding typographical and logical errors in your knowledge base that are not identified when the knowledge base is loaded. These are often the result of misspelling attribute names or attribute values.
In the ATTRIBUTE USAGE section of this output, all attribute names found in the knowledge base are listed in alphabetical order. First, each knowledge base component (PROMPTs and RULEs) that can determine a value for the attribute is shown. Then, each RULE that uses the attribute is listed. An attribute that is used but never determined represents a logical error, and is flagged with a question mark before the attribute name. This typically occurs because an attribute name has been misspelled leading to the existence of two attributes when only one is intended.
In the VALUE USAGE section of the report, all text values found in the knowledge base are listed in alphabetical order. Each value is followed by its length enclosed in parentheses. Then its location in the knowledge base (in a rule premise, rule consequent or PROMPT) is identified along with the associated attribute. Again, this output should help to identify values with slightly different spelling, spacing or other punctuation that makes two values that are intended to match appear unmatched to the inference engine.
Next click the expected radio button in the applet window and submit this value as your response to the PROMPT: According to the weather forecast, precipitation is:. Before entering the numeric temperature value that will be requested on the next screen, click the Dump KB button to get a snapshot view of the current status of the attributes and rules in the knowledge base.

The only attribute with a known value is precipitation and its value ("expected") is shown in the dump. The data type (S for string, N for numeric and B for Boolean) is shown in parentheses following the current value. The certainty factor (100%) for the attribute's value is shown next in parentheses: 0% certainty implies that the value has not yet been determined. If a DEFAULT value is available for an attribute, its value, type, and the confidence with which it will be assigned is shown last.
Each rule is displayed along with its description and status. U means the status of the rule is unknown, and both rules in the knowledge base have unknown values at this point. Other possible values are T if the rule has been proven true and has therefore fired, F if the rule has been proven false and X if the inference engine has determined that it is impossible to prove the rule true or false. The certainty factor for the rule is displayed at the end of the line. The data type (S for string, N for numeric and B for Boolean) for each value used in the premise or consequent is shown in parentheses after the value. he certainty with which a consequent value will be assigned is shown in parentheses at the end of each consequent clause.
Finally, the current contents of the goal stack is displayed. The inference engine is trying to find the last (highest numbered) value shown on the goal stack. Once its value is found, it will be removed from the stack. When the goal stack is empty, the consultation ends.
A PROMPT requesting numeric input has the following appearance. If the user enters a value outside the acceptable range specified in the knowledge base (-30 to 120 in this case) the out of range value is displayed in the data entry field enclosed in question marks. You must enter a value in the acceptable range or leave the field blank for a numeric PROMPT. Enter a numeric value and click the submit button to finish the consultation.

This completes the Creating your first knowledge base tutorial. You are encouraged to experiment with this simple knowledge base, entering different responses to the prompts, observing the trace in the debug window and examining knowledge base dumps to see the interim results.
To gain a better understanding of the activities of the inference engine that show up in the debug window when tracing is on, a look at Module 5: Inference methods and uncertainty is recommended. Then, when you are ready to start designing your own knowledge bases review Module 6: Introduction to knowledge engineering and the additional materials on building and debugging e2gLite knowledge bases (including converting knowledge bases to languages other than English) in Module 7.