A C + + Class for Rule-Base Objects

A C++ class, called Tripod, was created as a tool to assist with the development of rulebase decision support systems. The Tripod class contains data structures for the rulebase and member functions for operating on the data. The rule-base is defined by three ASCII files. These files are translated by a preprocessor into a single file that is located when a rule-base object is instantiated. The Tripod class was tested as part of a prototype decision support system (DSS) for winter highway maintenance in the Intermountain West. The DSS is composed of two principal modules: the main program, called the wrapper, and a Tripod rule-base object. The wrapper is a procedural module tho! interfaces with remote sensors and on external meterologicol database. The rule-base contains the logic for advising an inexperienced user and for assisting with the decision making process. © 1993 by John Wiley & Sons, Inc.

If the crews are di!"putdwd too early or released too late.excp,.;;jye<•o,:;ts could be incurred.Decisions rt'f('ardintr the deployment of personnel and heavy equipment are made by maintenance foremen and supervbors.Data an;ilable to them include local and natioual weather forecasts and realtimP data from n•mote sensor!". as well as pet•-:-onal ob,.,t'n ation,.

Rule-Base
Fif!ure 1 illu,.;tratesthe D~S application proct':'is.The terminolo1-.ryused in the following discu:;:-;ion eYoh-ed durin!! interTiews with the panel of exrwrts.Term,; "•ere selected that prm•ided the best communication with the experts.Three ASCII files."hown at the left of Figure 1 are prt>pared by the expt:>rts and the de,•eloper.These files are de-;;cribed in greater detail later in this paper.

The Variables File
A ,•ariable is a quantifiable characteristic of the system.The ,;;tate of the system at any rime is de-fined bv the values of the variables.The variable file (identified b,• the extension .IDY) contains a list of unique identification symbols and a;;sociated information for each variable.such as the type of the variable (i.e .. intef!er. float. logical. menu, or string).Each variable also has a question associated with it that.when needed.will be di:;played on the screen reqtwsting a ,•alue from the user.

The Actions File
An action is a consequence that is invoked when an associated rule fires (e.g .. evaluates true).The actions file (identified by extension .IDA) contains a list of unique identification symbols and associated information for each action.such as the type of action to be performed.An action may di,;play text.display graphics.or perform an operation.A variety of operations may be performed by an action, for example.manipulating the values of variables.sending a message to the wrapper.and branching to a specified rule in the rule-base.

Rules and Tests
A rule is an IF -THE:'\ statement specifying the appropriate action for a particular state of the sy.~ tem.as defined bv the values of the variables.It has the standard form: IF (variable_l ruleValue_l) fu~ (variable_2 <= ruleValue_2) AND .... THEN (action_l) The antecedent is made up of •'rests.,.shown in parentheses.For a test.if the value of the variablt> (i.e .. variable_1) satisfies the comparison (i.e .. equals rule\"alue_l), then the test is true.If all of the tests for a rule are true.then the rule is true and the action is invoked.If anv one of the tests is false.then the rule is false.The rule file (identified by the extension .IDR) contains a list of unique identification svmbols and associated information for each rule.Among other things.each rule has a list of tests that can includt> variables.actions.and other rules.

The Preprocessor
A preproce,;sor performs error chf'cking on the three files ancl produces a rule-base file that can be efficiently read by the Tripod object.Changes to the rule-base file may be made only through the variables.action,;.and rule,; fj)p,;_

The Wrapper
Figure 1 shows the wrapper intt>racting with a Tripod object and an auxiliary proce,;,; lsu<'h as a remote sensor 1 .The wrapper is a procedural main program that instantiatE's and coordinates Tripod objects a,; wE'll as performs auxiliary pnH'Ps,;;es.The wrappt>r is the ma>'tt'r of diP application: it FIGURE 1 • The Decision Support System application proce,;s.communicates with Tripod objects by means of mes,.;ages and !!athers information to respond w the messa!!es by means nf procedural l\1nctions !auxiliary processes).The wrapper is tailored for tlw rule-hase.or ,:.;et of rule-ba,:.;es. in a specific application.The "-rapper must know how to n:-spond to me;;,_;age,:.; it recein•s ..\lessages are imeger Yalues.For example.action_message .. 2" from the Tripod object request,.;data from a remote sensor buffer.The wrapper often responds by s1•ndiug a me,.;:;al!e containing the data back to the Tripod object.

The Rule Engine
The Rule En!!itw is dw memlwr function of the Tripod objet•t that t'\ aluaJc,.; the rule,.It takt'ii as a para!IWlt'l' tbt• onlt,r ntttuLwr of a rult> in the rulelw,.;t~.It "tarts l1y e\aluating tile Jir;;t t<""l ill the Ji,t of t<",.,ba,..,.,nciat<•d with tlwt rule.If the fir,-t te,..t t'\alttall'" lrtte.it !l:ot::-todw ,.;t•cond.nnd "o 011.If ull of the tt•,.,han~• tnw.tlw Hule Enl!illt' t'Hll,.; the fwwtiotl execAction ( J.This funt'tion take,., as a paranwwr ilw p<~i!ll!'l' tu an actioll.uud jWI'fonlb the operation "'f' ('t'ili<•d  If a test evaluates false.the Rult> Engine discontinues evaluatintr tests for the rule.marks the rult> as false, and goes on to the next rule in the rulebase.This process continues until a rule is encountered in which all tests evaluate true. Three types of tests are permitted: variablP.rule, and action.The variable type test provides the symbol for the variable and a value (or range of values).The Rule Engine first checks to see if the variable has been previously evaluated and if not calls a member function query () .This function displays the question associated with the ,•ariable, and compares the user's respon;;e with the test value (s ).It returns a true or false flag to tlw Rule Entrine depending on tlw re;;ults of the comparison.
A rule typrtest provides a pointer to another rule.The Rule Engine call;; iHelf to evaluatP this n1lt>.If the rule is true then the test is trut>.otherwiiie the test is false.Tlw Rule Engirw operate,; recursivelY on nested rules to anv reasonabiP depth.
An action type test provides a pointer to an action.The Rule Engine checks to see if the action has previously been invoked /i.e .. a;;sociated with a rule that has already fired i. and if not bt>gins testing all of tlw rule.'ias;;ociated with the action.If one of these rules {ires.then the action is im•oked and thf' test is true.If none of the,.;erules fire.then the test is fal5e.
Branching is handled by a special operation in an action.\\-hen an action is invoked bv execAction () it may pt>rform several types nf operations.Ont:> typ~' is to branch to a specified ntle.This is accomplished by returning a me,;sal!e to the Rule Engine directing it to leave off what it ha" been dointr and begin evaluating the ;.;pecified mle.For example.if a nested rule fires and ih action specifies a branch to another section of the mle-base.the Rule Engine does not finish evaluating the higher rules in the nt>st.   .An auxiliary JH'OI'P"'"' is normally a function prop:ramnwd by the dt'\ Plopt>r to tH'compli,.,h a specific ta,.;k: for Pxamplt>.rPading a rt'mote sf'nsor, iibtantiating another Tripod object with a differem rule-ha;;e.or di,.,playing a tPxt file on the ,o;creen.A de,•eloper may wi,;h to use kt>y ... ~-± .. to di,.;play tP.xt file ?i-t.txt on tlw ,.;cn .. en when the u,.;t'r rPque,.;tslll'lp with tlw quf'ry for a particular ,-ari-al>le.ThP deYeloper as,.;igns the ndue ?i-t to the kP,.for the Yariahle in the Yariahle file.and then prognmb a function for the wrappe-r to re,.;pond to a "ntriahle key .. me~,.;agehaYing the Yalue 3-i by di,.,playing ;~-f.txt.varQuest is the que,.;tionfor the Yariable and varNL is the numlwr of lines in the question.

The RBAct and RBOpAry Classes
The RBAct Rule-Base Action,o;) class contains data for each action.The data membe-r vOpAnchor point~ to a li,.;t of operations that the action is to pt'rform on dPcision Yariables.and nbr-VarOps is the numbe-r of ope-rations in the list.The RBOpAry class contains information for the operation.opern Otlwr data members in RBAct include actSym.actKey.and actUFC [:3~."•hich are analogous to varSym.varKey.and varUCF l:3i pn•,•iowdy defined.actDesc i,., a character ,.;triug with the description of the action.and actNL is the number of lines in the description.spsOtpt is a flag to suppre~s di,-play of the de,.;cription.actTsted is a flatr to indicate whether or not the action has been prPYiou;;ly invokt>d.St>tting this flag supprp,.;,.;p,; ,;uh,.;equPntPxecutions of thi,.; action.questlnpt i,;; a flag modifyiug the action.Three flags are permitted at this time.and they are described in the next ,o;ection.

The RBRue and RBRueTst Classes
The RBRue !Rule-Base Rules) class contains data for Pach rulP.The data member rueTstAnchor poillls to the list of tests for the rule.and nbr-Tests i;; the 11umber of test,; in the li,.;t.rueSym is a character ,.;tring containing the unique identifier for the rule.rueActSym is the symbol of the action as,.;ociated with this rule.and rueActPtr is a pointer to it.rueKey.rueUCF [3].and rueTsted are analogous to actKey.actUCF [3~.and actTsted.
The RBRueTst class contains information for each te,;t.tstType is a character indicating the type of test: .. , .. , for ,-ariable ... R .. for rule.and "A" for action.tstVarRueActidx is the identifier of the Yariable.rule.or action for this test.
tstRueAnchor is a pointer to a list of pointers to rules nested in this test.nbrTstRueMu is the number of nested rule,; in this test.not is a flag indicating whetlwr 1\'0T should Le applied to the results of this test (i.e., true becomes false).cri t is a data union for a Boolean.string, floaL or integer.It contains the values that will cause the test to eyaluate true.

PROTOTYPE APPLICATION
Decisions regarding the deployment of personnel and heavy equipment for snow and ice prevention and removal are made by the maintenance foremen in geographical districts.Districts range in size from a few hundred to several thousand square kilometers.Data available to the foremen include local and national weather forecasts.real time data from remote sensors.and personal observations.During a winter event.foremen rely upon these data in varying degrees and upon personal rules of thumb for dispatching crews and equipment, and for releasing crews near the end of the event.
During the first three meetings with the panel of experts.the scope of the problem was discussed and terminology was f'Stablished that was meaningful to the members of the panel.The objective of the smdy was focused on preparing a ~imple prototype of a DSS that would help foremen make decisions about the dispatch and release of road crews during a winter event.The prototype ;;hould provide table and graphic displays of data from remote sensors, and provide a step-by-step advisor for novice users [9].
The importance of terminology used for communicating with the panel of experts should not be underestimated.Terms were established b,• the panel defining three categories of informa.tion:variables, actions, and rules.Forms were developed to assemble information in these three categories.These forms were the basis for the input files for the preprocessor.

S.l The Variables File
The variables file contains the input data for the variables.Table 1 shows tht> inplll format for each variable in the file.A unique symbol for the variable is enclosed in parenthese:-;.The symbol may be up to 20 characters long.This is followed by the declaration of the type ~f variable: menu ('\I}.string (S).logical (L), integer (lJ, or floating point (F).A menu type declaration must be followed by First menu choice Second menu choice Third menu choice the number of items in the menu lise as shown above.The next field contains the integer .. key"' code (2).This code is passed to the wrapper with a message from the Rule Engine when an external process is requested for the variable.The next item in the first row is an ime~er specifyin~ the number of lines in the question.The last three fields (0.1, 0.2.0.3) were originally intended for coefficients quantifying uncertainty.but this feature is not yet implemented.
The second row contains the question that will be displayed for the user when this variable is encountered in a rule test.If the variable is a menu type, as shown in Table 1. the remaininp: rows contain the choices for the menu.If the \•ariable i;; an integer of float type.only one row follow,.; the question.and it contains the minimum and maximum permissible values for the variable.
Table 2 contain;; a partial li:"ting of the variables identified by the experts.A key of .. 1 .. signals the wrapper to obtain and display information from the Scan Cast weather forecasting system [10].This procedure requires accessing a remote data base over telephone lines.A key of ••2 .. signals the wrapper to obtain current meterological data from remote sensors at a highway station.This information includes air temperature.pawment temperature.wind speed.precipitation.relath•e humidity.and other p<'rtinent data.

The Actions File
The actions file contains specific actions to be performed as a consequence of tlw stare of the system.An aerion may be a screen display gi,•ing recommendations to the user.an operation to be performed on a set of variables.or a message to the wrapper to execute an external process.Table :3 shows the input format for each action in the file.A unique symbol for the action is enclo~ed in parentheses.followed by the key code.Three flags are permitted following the key: 1. IS suppresses the screen display of the action description 2. /Q signifies "qut>stionnaire .. and will force all tests of a rule hm•ing this action to be evaluated.1\'ormalh•.the entluation of tests within a rule will be.discominuf'd at the lir;;t false.:3./0 indicates that thi:-; action i;; to perform operations as specified by codes followintr the description line ( s).  identified by the experts.Each action i,; nuHlt' up of a unique symbol and an expanded dt>,.;eription.:i\o key:; were nt>eded for thi,.; application.l3!ank field:" in the input tile are interpreted a:" null by the file preprocessor.

PRELIMINARY TESTING
:\lost of the testing effort went into exercising the procedures iu the prq.Jroce,;sor.the wrapper.and the Tripod object.During this process many error traps and warnings were added in order to ensure consistency amoni! the input Iiles.The preprocessor prepares a li,.;t of all nniable and action symbols not u:-ed in the rule file so that the Jewloper can see if important elemPnts haYe been inad\•erteuth• omitted from the rule-ba;;e.In the ca:-e wlwre an ~iction operates on two Yariables.the preprocessor chec•ks to ensure that the Yariables are of the same type.If they are menu type variables.the first variable must have at least as mam• menu item,.; as the second and a warnin;:r is di.'iplayed if corresponding choice symbol,.; are not identi<'al.
The preprocessor also checks to see if the developer has used a nrle in a test for that rule.For example. in a test containing several levels of nested rules.a developer could ea,;ily include the mle within a test for itself.Obvioush• such a mi,o;take could result in run-time endlt>ss recursion.a situation that oecurred on several occasions before this trap wa,o; added.
l\"ext to displaying text for the u;;er.the branch i,o; the most useful action operation.\\•e used it a,; a means to partition a rulf'-base.For example b\• evaluating a few rules fir"t.we could branch to major section,; of a rule-base to acquire detail information about a particular subject.Also.we could design the mle-hase to skip owr major St>ctions of rult>s that are no lonw•r relevant after a particular action ha,; been invokt•d.Branchin!.! can be u,..;ed to construct loop,; in the rule-lxbe.: however, we did not have an immediate practical need for thi~ con:;truct.Like nestinf!rule:;.u~in!.! branche,; Illlbt be done carefullY in order to aYoid run-time thrashing.\\•e have not deYised a technique for tlw fWt>procPssor to detect inappropriate branchinf!.

Figure 1
Figure 1 also shows a .. Fact Data Base .. filt>.This file contains initial value::; for variables in the rulebase.It is an ASCII file with a list of variable identification symbols.each followt>d by its initial value.The Fact Data Ba,;e may be updated any time prior to the instantiation of the Tripod object.Variable,; not li:-ited in the Fact Data Base are au-tomaticallY initialized to an .. unknown•• state lJ\• tht' object.
Continuity is maintained by continually updating the Tripod data ;;tructures.The Tripod object retains the results of all previous operations.For example, it records which rules have fired, which actions have been invoked.which variables have ll; =chor .t-'ABCM;;~;~ varUCF[:31 ,.;tores t!m."e arbitrary coefficie-nts for the yariahle.Originally the,;e coefficients were going to he used to n•prt>sf'nt uncertainty.However.this feature has not yet been implemented and the coefficients are unused.varEval is a flag indicating whether or not thi;; ,-ariable has been pre,•iou;,ly e\•aluated.If it has been previously evaluated.varRsl t stores the Yalue.varRsl t is a data union that stores a logical(Boolean).string.C++ CL\~~ FOH HL LE-13:\~E OBJECTS float.menu.or intetrer depending on the type of YariabiP.
Table 4.A Partial Listing of the At:>tion>" File (.IDA) crew to replace a standard crew.

Table 6 . A Partial Listin~ ol' the Hule File (.IDH)
Til >P,; a te;;t for the rult>.The fir.•;t two dwnwtt'rs in each row indicatP the kind of tt•,;t.:\••: • • a,; tlw lir,;t character indicmes that the rPsult of tlw te,.;t is w be rPver~ed (i.e .. true becomes fabe.aftPrentluation.Three kimb of tt>,;b aw fWrmitted: the action, and finally what Yalue,o; of the Yariables would cause the action 10 he taken.In Table6, integer and floatinf!point YHriables are followed by a range of ntlucs.The ,•ariable will test true for any n1lue within this range.and false otherwise.:\lenu Yariubles are followed Lv a list of