Computers ind. Engng Vol. 19, Nos 1-4, pp. 107-110, 1990 Printed in Great Britain. All rights reserved
0360-8352/90 $3.00 + 0.00 Copyright © 1990 Pergamon Press plc
The Design and Implementation of an Ada-Baeed Inferenoe Ingine
Maria A. Clanci,
Ms. and Darrell G. Linton,
PhD, PE.
Computer E n g i n e e r i n g Department (CEBAI-207) U n i v e r s i t y of Central Florida Orlando, Florida 32816
Introduction Serious research in the Artificial Intelligence (AI) field started only a few years back. This research has been focused on creating systems with enough knowledge (symbolic rather than numerical) and inference mechanisms to supplant man in certain areas. Such systems are known for their complexity and length. Their design, implementation and m a i n t e n a n c e are costly in time, effort and money. Consequently, the Department of Defense (DoD) sponsored the design and i m p l e m e n t a t i o n of a p r o g r a m m i n g language which, by incorporating software engineering concepts, would help reduce software costs. The resulting language was called Ada. In 1987, the U.S. DoD declared Ada as the language required for all computers in weapon systems (directive 3405.2) and declared Ada the common computer language for all Defense computer resources (directive 3405.1). The DoD's e n f o r c e m e n t of the Ada language produced a new trend: AI and Ada. The purpose of this paper is to describe the incorporation of software design m e t h o d o l o g i e s innate in the Ada language in the design and implementation of an inference engine. General O v e r v i e w of an Ada-Based
Inference Engine
(ABIE)
ABIE [i] was implemented on a 386-Compaq machine using the 4.2 Alsys Ada compiler. The system employs a forward-chaining inference m e c h a n i s m that uses the Rete a l g o r i t h m [2] in the search technique. ABIE's e n v i r o n m e n t is command-driven, that is to say, the user interacts with the system through a series of p r e - e s t a b l i s h e d commands. Inside the environment, ABIE operates in two modes : debug and run. In the debug mode, which is also the default mode, rules can only be activated; in run mode, rules can be activated and fired. The e n v i r o n m e n t has a limited number of commands to coach the basic inference scheme. Its capabilities include loading rules from a file, asserting or retrieving facts from the data base, a c c e p t i n g the declaration of a single rule from the command line, d i s p l a y i n g the inference rules, as well as facts, or the agenda through a single command. The structure of the rules must be simple. For rule declarations, ABIE only allows testing of conditions, Left-Hand-Side (LHS) patterns and actions; however, a m i x t u r e of patterns and conditions is invalid. For testing conditions within the rule, boolean operators (= >= =< < > /=) and Lisp logical functions (Numberp, Oddp, Evenp) can be combined with arithmetic operations (+, -, *, **, /, sqrt) to produce a logical result. For patterns as well as facts, the system supports constants, single variables, multiple variables and single and multiples wild cards which can be blended to form one statement. Finally, ABIE supports assertion and r e t r a c t i o n of facts as well as of the contents of a variable, retraction of a fact by its factnumber, and p r i n t i n g of messages which combine strings and variables. A r i t h m e t i c a l l y speaking, the software permits integer and floating point operations. Combining types (e.g., integer and float) are supported for all arithmetic c o m p u t a t i o n s except the exponential function (exponent must be integer). All internal computations are done in f l o a t i n g - p o i n t for accuracy purposes. 107
108
Proceedings of the 12th Annual Conference on Computers & Industrial Engineering
lower case words in the patterns are treated as they were different terms. On the other hand, word-commands are accepted in either form since ABIE converts them to lower case before processing. The Internal Structure ABIE was entirely coded in Ada, a programming language that directly embodies many software design methodologies while being capable of modeling programming solutions. The system was built starting with the most loosely coupled, strongly cohesive packages. Abstraction and modularity concepts were applied to scale down the system complexity while uniformity, completeness and confirmability were incorporated to ensure the ease of testing and modification of ABIE. Abstraction and modularity are inherent characteristics of the Ada language. The former is provided by a series of clauses that allow the system programmer to hide internal design details from the external user. In this manner, the designer's intended use of a data structure or function can not be violated. Moreover, since Ada allows the use of packages the designer is able to code independent ideas (i.e. stacks) in separate software pieces. This concept is not only useful in breaking down the system for design, coding and maintenance purposes, but also it allows independent compilation of packages. Within the packages, uniformity was provided by supplying the same name for the same variable, in terms of context, throughout the package. In this way modification is simplified since the programmer does need to deal with different names for the same variable. Similarly, completeness was provided by furnishing those packages housing a specific data structure with the necessary functions for its manipulation. If these packages were incomplete, future inclusion of the missing functions would be required and, until modification is completed, the use of the data structure would be limited. Also, since each package is formed of inter-related functions (i.e. modularity), most of the packages can be tested independently of the others. In this manner, reliable software as well as minimization of future changes is provided. ABIE has been conceptually partitioned in four (4) major modules: Foundation, Utility, Engine Body and Interface. Each module consists of function-related packages. The Foundation shelters the most loosely coupled packages; that is to say, each package is almost independent of the parts of the ~ystem. The Utility component connects the rest of the software to provide for manipulation of data in different formats. The Engine Body is the inference engine itself. It creates and manipulates the Rete structure according to user's input and producing the Rete tree as well as the activation or deactivation of rules. Finally, the interface module is the most strongly coupled part of ABIE. It reads the user's inputs, and by calling the other modules it executes the commands printing out results if any was produced. The foundation part comprises three packages called Utility_Pkg, Conversion_Pkg and Lisp_Function_Pkg. The Utility_Pkg contains the functions to manipulate strings which are utilized by the entire system. Similarly, the Conversion Pkg comprises procedures to convert string to integer or float and vice-versa. Finally, the Lisp_Function_Pkg is the foundation of the ABIE system; in other words, all data entering the system is put into a data structure similar to the "atom/list" format of the LISP language. The Lisp_Function_Pkg houses this Lisp-like data structure and provides the means to validate the input as well as to manipulate the structure and its content. The Utility module accommodates those packages that are used to connect the other system's components. These packages are called Parser_Pkg, DBase_Pkg, Stack_Pkg and Dispatch_Pkg. The Parser_Pkg, as its name implies, determine the existence of the system command and the validity of its parameters. The knowledge base in ABIE is enclosed in the DBase_Pkg. This structure was designed as a two-dimensional array which contains the fact identification number imposed by the system and the asserted fact. Also, DBase_Pkg contains the functions required to work with the knowledge base. The Stack_Pkg encloses the structure that ABIE uses to tell the Dispatch procedure the name of the command to be performed and its input parameters so the instruction can be executed. The Dispatch_Pkg is the center of execution. It manipulates the user's input, commands or rule actions, already in the internal format and executes it by stacking the input parameters and calling the corresponding routines.
Cianci and Linton: Aria-based Inference Engine
109
counterpart. It consists of a ReteTree_Pkg, a Process Rule_Pkg, a M a t c h i n g _ P k g and a Conflict_Set_Package. The ReteTree_Pkg houses the Rete nodes in the form of an Ada v a r i a n t record structure and all the functions needed to use it as designed. The P r o c e s s _ R u l e _ P k g receives a rule in a string form, v a l i d a t e s it, and converts it to a tree branch. By the same token, the M a t c h i n g _ P k g traverses the tree finding those rules that correspond to the asserted or retracted facts and activates the matched rules. Finally, the Conflict Set_Pkg accommodates a t w o - d i m e n s i o n a l array structure that serves to keep all those rules awaiting for e x e c u t i o n and the functions for the structure operation. The p r o c e s s e s of reading input from the command line or from a file and outputing results are a c c o m p l i s h e d by the Interface Module. This section of ABIE is formed by the packages In_Out_Pkg, File_Pkg, Resolve Conflict_Pkg, Firing_Pkg and Interface_Pkg. First, the In_Out Pkg holds those functions needed to read or print all the data forms used by ABIE. The File_Pkg handles the opening, reading and closing of rule files. Moreover, the file package is designed to recognize file errors such as n o n e x i s t e n t file or end of file. R e s o l v e _ C o n f l i c t _ P k g contains the algorithm to identify the next activated rule to be executed. The execution of rules is done through the Firing_Pkg. This package p o s s e s s e s the procedures which through the D i s p a t o h _ P k g perform the R i g h t - H a n d Side of the rule. Finally, the Interface Pkg interfaces with the user while joining in one procedure the general a l g o r i t h m for the system. Testing of the Inference Engine The testing of this basic inference m e c h a n i s m is based on the ability of the system to cope with the different patterns for which it was designed. In order to d e m o n s t r a t e the activation of rules, first, the rules must be loaded into the system; then, facts must be asserted while the agenda or conflict set is watched. The following test was designed to d e m o n s t r a t e the use of w i l d - c a r d s and variables. The rules for this test are as follows: (defrule Eligible (bachelor John ?) => (print ("Eligible John "))) (defrule M u l t i p l e _ E l i g i b l e (bachelor John $?) => (print ("Eligible John "))) (defrule M V a r i a b l e _ E l i g i b l e (bachelor John $?name) => (print ("Eligible John %s")
$?name))
(defrule M V a r i a b l e s _ E l i g i b l e (bachelor $?first John $?last) => (print ("Eligible %s John %s")
$?first $?last))
Test starts: c:\abie ABIE =>
-- invoking the e n v i r o n m e n t -- in debug mode (default) (load_rules bachelor.rul)
ABIE => (see rules) Eligible Multiple_Eligible MVariable_Eligible MVariables Eligible ABIE => (agenda) Command Expected ABIE =>
(assert
-- loading rule file -- checking
loaded rules
-- error (bachelor John Smith))
ABIE => (see_agenda) Multiple_Eligible MVariable_Eligible MVariables Eligible
-- asserting
fact
-- checking agenda -- activated rules
for
110
ABIE => Eligible Eligible Eligible Eligible
Proceedings of the 12th Annual Conference on Computers & Industrial Engineering
(run) John John J o h n Smith John Smith
ABIE => (assert (bachelor Josef John)) Eligible J o s e f J o h n ABIE => (no_run) ABIE =>
(assert
-- invoking run m o d e -- a c t i v a t e d rules are -- e x e c u t e d
-- a s s e r t i n g fact that fires -- the rule M V a r i a b l e s _ E l i g i b l e -- return to d e b u g m o d e
(bachelor J o s e f J o h n Mith))
-- assert fact for a c t i v a t i o n -- of the M V a r i a b l e s _ E l i g i b l e
ABIE => (see_agenda) MVariables_Eligible
-- check agenda
ABIE => (Facts) 1 (bachelor J o h n Smith) 2 (bachelor J o s e f John) 3 (bachelor J o s e f J o h n Mith)
-- d i s p l a y KB
ABIE => (RUN) Eligible J o s e f J o h n M i t h
-- invoke run m o d e -- a c t i v a t e d rule fired
ABIE =>
-- exit the system
(Quit)
(Initial_Fact)
Conclusion This paper p r e s e n t s the design and i m p l e m e n t a t i o n of an inference engine (IE) in the l a n g u a g e d e s i g n e d and d e v e l o p e d by the U.S. DoD: Ada. As already described, A B I E ' s d e s i g n i n c o r p o r a t e s the b a s i c s o f t w a r e e n g i n e e r i n g concepts p r o v i d e d by the i m p l e m e n t a t i o n language. The result is a m a n a g e a b l e but yet u n d e r s t a n d a b l e s y s t e m that can be d e c o m p o s e d for easy m o d i f i c a t i o n and/or expansion as well as testing. The Ada language has p r o v e n to be s u i t a b l e for the d e s i g n of AI projects. As shown in the e x a m p l e above, the v e r s a t i l i t y of the language has allowed the d e v e l o p m e n t of m a n y commercial IE features into the ABIE system. Moreover, Ada has p r o v i d e d the m e a n s for the c r e a t i o n of a c l e a n - c u t system that can be r e l i a b l e and predictable. References [i] cianci, M a r i a A. ABIE: An A d a - B a s e d U n i v e r s i t y of Central Florida, 1989.
Inference
Enaine.
Thesis w o r k
for
[2] Forgy, L. Charles. "Rete : A Fast A l g o r i t h m for the M a n y P a t t e r n / M a n y Object Pattern M a t c h Problem." A 2 t i f i c i a l Intelliaence, Vol. 19, 1982. Acknowlegement This r e s e a r c h was s u p p o r t e d by the State of Florida, I n t e l l i g e n t S i m u l a t i o n T r a i n i n g Project, U n i v e r s i t y of Central Florida, Orlando, F1 32816, Dr. John E. Biegel, Project Director.